cswork / OAOpen practice workspace

AmazonOA #303

Minimum Starting Health to Win the Game

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

You take the damage in the power array in order, and after every round your health must be strictly greater than 0. The armor can be used at most once; it reduces that round's damage by min(armor,power[i]) and does not restore health. Find the minimum initial health.

Input

The first line contains n armor and the second line contains power. The original bounds are 1≤n≤100000, 1≤power[i],armor≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

4 4
2 7 4 3

Output:

13

Example 2

Input:

1 10
1

Output:

1

Example 3

Input:

2 1
4 4

Output:

8

Example notes

The answers are 13, 1 and 8. In the second example all the damage can be blocked, but the initial health must still be at least 1.

Editorial

Loading solution…