#arc142f. [arc142_f]Paired Wizards
[arc142_f]Paired Wizards
Problem Statement
Two wizards, and , are fighting with a monster.
Initially, each of the wizards has a magic power of . They know the following two spells.
- Spell : Increases the caster's magic power by .
- Spell : Deals damage equal to the caster's magic power to the monster.
After each wizard uses a spell times, they will withdraw from the battle.
For each , they must use one of the following combinations of spells for their -th spells.
- casts Spell , and casts Spell .
- casts Spell , and casts Spell .
Find the maximum total damage that can be dealt to the monster before the withdrawal.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
1 1 2 2
2 1 2 2
2 1 1 1
Sample Output 1
3
The maximum total damage can be achieved as follows.
- As the -st spells, use , increasing both 's and 's magic powers to .
- As the -nd spells, use , dealing points of damage in total.
- As the -rd spells, use , dealing point of damage by 's spell and increasing 's magic power to .
Sample Input 2
5
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
Sample Output 2
0
Casting Spell with a magic power of deals no damage.
Sample Input 3
8
1 1 2 2
2 2 2 1
1 1 2 1
1 1 2 2
2 1 1 1
1 2 1 2
2 1 1 2
2 1 2 1
Sample Output 3
20
Sample Input 4
20
2 1 2 1
2 1 1 1
1 2 1 1
2 2 1 2
2 2 2 1
1 1 2 1
1 2 2 2
2 2 2 1
1 1 1 2
1 2 1 2
1 2 2 2
2 1 1 2
2 1 1 1
1 2 1 2
1 2 1 2
1 1 1 2
1 1 2 1
2 2 1 1
1 2 2 2
2 1 1 2
Sample Output 4
138