#P1626C. Monsters And Spells
Monsters And Spells
No submission language available for this problem.
Description
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.
The level he's currently on contains monsters. The -th of them appears seconds after the start of the level and has health points. As an additional constraint, for all . All are different.
Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is . Otherwise, let the damage at the previous second be . Then he can choose the damage to be either or . A spell uses mana: casting a spell with damage uses mana. Mana doesn't regenerate.
To kill the -th monster, Monocarp has to cast a spell with damage at least at the exact moment the monster appears, which is .
Note that Monocarp can cast the spell even when there is no monster at the current second.
The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters.
It can be shown that it's always possible to kill all monsters under the constraints of the problem.
The first line contains a single integer () — the number of testcases.
The first line of the testcase contains a single integer () — the number of monsters in the level.
The second line of the testcase contains integers () — the number of second from the start the -th monster appears at. All are different, are provided in the increasing order.
The third line of the testcase contains integers () — the health of the -th monster.
The sum of over all testcases doesn't exceed .
For each testcase, print a single integer — the least amount of mana required for Monocarp to kill all monsters.
Input
The first line contains a single integer () — the number of testcases.
The first line of the testcase contains a single integer () — the number of monsters in the level.
The second line of the testcase contains integers () — the number of second from the start the -th monster appears at. All are different, are provided in the increasing order.
The third line of the testcase contains integers () — the health of the -th monster.
The sum of over all testcases doesn't exceed .
Output
For each testcase, print a single integer — the least amount of mana required for Monocarp to kill all monsters.
Samples
Note
In the first testcase of the example, Monocarp can cast spells and seconds from the start with damages and , respectively. The damage dealt at seconds is , which is indeed greater than or equal to the health of the monster that appears.
In the second testcase of the example, Monocarp can cast spells and seconds from the start with damages and , respectively.
In the third testcase of the example, Monocarp can cast spells and seconds from the start with damages and , respectively.