cswork / OAOpen practice workspace

AmazonOA #27

Get Minimum Health (Armor Round)

Estimated EasyJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

You take each round of damage in order, and your health must always stay strictly greater than 0. In one round you may use the armor, which blocks min(armor, damage this round) points of damage. Find the minimum initial health.

Input

The first line contains n armor (1≤n≤100000, 1≤armor≤10⁹); the second line contains n damage values (1..10⁹).

Output

Output a single integer, the answer.

Example 1

Input:

4 5
1 2 6 7

Output:

12

Example 2

Input:

1 9
1

Output:

1

Example 3

Input:

2 1
5 5

Output:

10

Example notes

Example 1: the total damage is 16 and the armor blocks 5, leaving 11 damage, so the initial health must be 12. Example 2: the armor blocks the only 1 point of damage, so an initial health of 1 is enough. Example 3: after blocking 1, the total damage is 9, so 10 is needed.

Editorial

Loading solution…