#D318. Wizard Tower
Wizard Tower
Wizard Tower
Problem statement
The spellbook you have contains of spells.
Magic is numbered from to , and the cost of magic is initially an integer .
Your goal is to cast all the spells in the spellbook each.
You can eat of cookies before you start casting magic. It doesn't take long to eat cookies.
For every you eat a cookie, you can choose a magic with a positive cost of and reduce that cost by .
After eating the cookie, you start casting magic.
Your MP is initially . You repeat either of the following to cast of magic in any order.
- Choose an integer for and cast the magical . However, the current MP must cost more than the magical .
- Time does not elapse.
- Consume MP for the cost of magical .
- Rest. However, if the current MP is , then must be.
- Time elapses .
- Recover MP.
Find the minimum amount of time it will take you to cast of spells each in any order.
Constraint
- All inputs are integers.
input
Input is given from standard input in the following format.
output
Print the minimum amount of time it takes to cast of magic each.
Input example 1
2 4 0 twenty four
Output example 1
3
Since the cost of any magic cannot be reduced, I will continue to cast magic as it is.
- First, cast the magic . It consumes of MP, so the remaining MP is .
You cannot cast magic as it is because you need MP to cast magic .
- I'll take a rest. Time elapses seconds. It recovers of MP, so the remaining MP is .
- I'll take a rest. Time elapses seconds. It recovers of MP, so the remaining MP is .
- Cast magic . It consumes of MP, so the remaining MP is .
From the above, if the time is seconds, the magic can be cast each time. You can't cast all the spells in less time, so the answer you're looking for is .
Input example 2
3 9 6 2 3 9
Output example 2
0
With a final magic cost of , you can cast all the magic without a break.
Input example 3
3 16 2 6 9 9
Output example 3
twenty one
Input example 4
2 1000000 0 1000000 1000000
Output example 4
500000500000
The answer may not fit in the range that can be represented by a 32-bit integer.
Example
Input
2 4 0 2 4
Output
3
inputFormat
inputs are integers.
input
Input is given from standard input in the following format.
outputFormat
output
Print the minimum amount of time it takes to cast of magic each.
Input example 1
2 4 0 twenty four
Output example 1
3
Since the cost of any magic cannot be reduced, I will continue to cast magic as it is.
- First, cast the magic . It consumes of MP, so the remaining MP is .
You cannot cast magic as it is because you need MP to cast magic .
- I'll take a rest. Time elapses seconds. It recovers of MP, so the remaining MP is .
- I'll take a rest. Time elapses seconds. It recovers of MP, so the remaining MP is .
- Cast magic . It consumes of MP, so the remaining MP is .
From the above, if the time is seconds, the magic can be cast each time. You can't cast all the spells in less time, so the answer you're looking for is .
Input example 2
3 9 6 2 3 9
Output example 2
0
With a final magic cost of , you can cast all the magic without a break.
Input example 3
3 16 2 6 9 9
Output example 3
twenty one
Input example 4
2 1000000 0 1000000 1000000
Output example 4
500000500000
The answer may not fit in the range that can be represented by a 32-bit integer.
Example
Input
2 4 0 2 4
Output
3
样例
2 4 0
2 4
3