Safe Gambling
Time limit1sMemory limit128 MB
On an odd circular wheel of N = 2K+1 priced pockets, choose three arcs of K consecutive pockets covering every pocket, minimizing the total sum of the arcs.
- Level
Medium7 of 10
- Topics
- Array, Sliding window, Brute force, Implementation
- Solved
- No attempts yet
Problem
A roulette wheel has pockets arranged in a circle. The number of pockets is always odd, and pocket has a price . The pockets lie on a circle, so the last pocket is adjacent to the first one.
A single bet covers exactly consecutive pockets on the wheel, and the price of that bet is the sum of the prices of the pockets it covers.
To cover every pocket you must place exactly three bets. Because is odd, two bets of pockets always leave one pocket uncovered, while three bets can cover the whole wheel (even though some pockets may then be covered more than once).
Place three bets that together cover all pockets so that the sum of the three bet prices is as small as possible. A pocket covered by several bets is counted once for each bet that covers it. Output this minimum total price.
Input
The input contains several roulettes. Each roulette is described on two lines.
The first line contains one odd integer (), the number of pockets on the wheel.
The second line contains integers (), separated by spaces, giving the price of each pocket in the order they appear around the circle. The last pocket is adjacent to the first one.
The line after the last roulette contains a single , which marks the end of the input.
Output
For each roulette, print on its own line the minimum price of a set of bets satisfying all of the following:
- It contains exactly three bets.
- Each bet covers consecutive pockets.
- The three bets together cover every pocket of the wheel (some pockets may be covered twice).
- The sum of the three bet prices is the minimum over all such sets.
The price of a bet is the sum of the prices of the individual pockets it covers.