HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Unsorted > problem:


50282 - Coins

Guest
• Discussion of problem (1)

Section problems

• 50273 - Two Captains
• 50274 - Boundary Troops
• 50278 - ATM
• 50277 - Unusual Lottery
• 50283 - Tetris 3D
• 50285 - Meat store
• 50279 - Bit Decoder
• 50284 - Knights of the Rook
• 50282 - Coins
• 50281 - Primes
• 50287 - Providers
• 50476 - Meditation
• 50879 - Passed or Failed
• 50428 - Three Times
• 50387 - Passengers in the Train
• 50391 - Cinema Tickets
• 50402 - Hello World!

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 1500/2000/2000/2000 ms. Memory limit 65000/65000/65000/65000 Kb.
Автор: Игорь Андрианов, ВоГТУ.

Monetary system in Byteland contains of N kinds of coins of different denominations. During ATM software developing, there occured a problem to give out the requested sum with minimal quantity of coins. To solve it, the following greedy algorithm was introduced. At first, maximal quantity of coins of greatest denomination are taken. Then, for the remaining sum, maximal quantity of coins of lesser denomination (next one in descending order) are taken, and so forth.

For example, we have coins of denominations 1, 2 and 5. To pay sum of 13, two coins of 5 are taken, then one coin of 2 and then one coin of 1 are taken.

Your task is to define, if this algorithm works correctly in all cases for a given monetary system.

Input
The first line of the input contains N (1 ≤ N ≤ 1000). The second line contains N coin denominations in ascending order, separated by single spaces (1 ≤ Di ≤ 1000). The first denomination is always 1.

Output
If the introduced algorithm really gives out any sum with minimal quantity of coins, output 0. Otherwise output the minimal sum, that the algorithm will give out with non-minimal quantity of coins.

Input 1 Output 1
3
1 2 5
0
Input 2 Output 2
4
1 3 4 5
7

Для отправки решений необходимо выполнить вход.

www.contester.ru