atcoder#ARC094C. [ARC094E] Tozan and Gezan
[ARC094E] Tozan and Gezan
Score : points
Problem Statement
You are given sequences and consisting of non-negative integers. The lengths of both and are , and the sums of the elements in and are equal. The -th element in is , and the -th element in is .
Tozan and Gezan repeats the following sequence of operations:
- If and are equal sequences, terminate the process.
- Otherwise, first Tozan chooses a positive element in and decrease it by .
- Then, Gezan chooses a positive element in and decrease it by .
- Then, give one candy to Takahashi, their pet.
Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible. Find the number of candies given to Takahashi when both of them perform the operations optimally.
Constraints
- The sums of the elements in and are equal.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.
2
1 2
3 2
2
When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:
- Tozan decreases by .
- Gezan decreases by .
- One candy is given to Takahashi.
- Tozan decreases by .
- Gezan decreases by .
- One candy is given to Takahashi.
- As and are equal, the process is terminated.
3
8 3
0 1
4 8
9
1
1 1
0