Snacks within 300 Yen
InterviewTime limit8sMemory limit1024 MB
Greedy George walks a shelf of snack prices left to right and buys each snack that keeps his total within 300 yen. The task is to output his total spending for each dataset.
- Level
Easy2 of 10
- Topics
- Greedy, Simulation
- Solved
- No attempts yet
Problem
Your friend, Greedy George, visits a candy store to buy snacks for a school excursion. He holds 300 yen in his hand. The candy store places one item of each kind of snack in a line on the shelf. After entering the store, George picks up a shopping basket and checks the snacks on the shelf from left to right. If the sum of the prices of the snacks in the basket does not exceed 300 yen, he puts the checked snack into the basket right away. If the sum would exceed 300 yen, he tearfully skips that snack. After checking every snack up to the right end, George goes to the checkout.
Given the list of snack prices in the store, compute how much money Greedy George spends.

Figure A-1: The first dataset of the sample input
Input
The input consists of multiple datasets, each in the following format. The number of datasets does not exceed 50.
() is the number of snacks sold in the candy store. () is a positive integer at most 1000, meaning that the price of the -th snack from the left is yen.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, output on one line how many yen Greedy George spends on snacks.