Banks
Time limit5sMemory limit256 MB
Flipping a negative bank in a circle to positive takes the same amount from both neighbors; the goal is the fewest flips leaving every capital nonnegative.
- Level
Medium7 of 10
- Topics
- Greedy, Simulation, Math
- Solved
- No attempts yet
Problem
Wall Street in Wonderland has banks standing in a circle, so every bank has one left neighbour and one right neighbour. The left neighbour of the first bank is the last bank, and the right neighbour of the last bank is the first bank. The banks are numbered from to . The left neighbour of bank is bank and its right neighbour is bank .
Bank has capital . The capitals of all banks add up to a positive number.
Whenever the capital of some bank is negative, the Bank Fairy spends one magic move and turns that capital into a positive one. If , then after the magic move. Both neighbours pay for it: the capital of the left neighbour and the capital of the right neighbour each drop by . If the left neighbour held and the right neighbour held , they hold and after the move.
The drop applies once per neighbour relation. When the remaining bank is both the left and the right neighbour, so its capital drops by . When the only capital is positive, so no magic move is ever possible.
What is the minimal number of magic moves the Bank Fairy has to make so that the capital of every bank is greater than or equal to ?
Input
The first line contains the number of banks ().
The second line contains the capitals in the order in which the banks stand on Wall Street, separated by single spaces. Each capital is an integer with , and their sum is positive.
Output
Print the minimal number of magic moves on a single line.