MicrosoftOA #52
Minimum Tokens Remaining
Estimated MediumJudge-ready
Topics (AI-labelled)Bits & MathSimulation
There are infinitely many stacks numbered from 0. The heights of the first N stacks are given, and the rest are empty. Each operation takes 2 tokens from one stack and adds 1 token to the next stack. Find the minimum total number of tokens after any number of operations.
This site uses a standard input/output protocol.
Input
The first line contains N, and the second line contains N heights. This site uses 1≤N≤200000 and heights 0..10⁹.
Output
Output a single integer, the answer.
Example 1
Input:
2
2 3
Output:
1
Example 2
Input:
2
0 0
Output:
0
Example 3
Input:
1
3
Output:
2
Example notes
Example 1: the total weight is 2+3×2=8, ending with 1 token on stack 3. Example 2: there are no tokens, so 0. Example 3: 3 tokens can become 1 token each on stacks 0 and 1, for 2 tokens in total.
Editorial
Loading solution…