c1=1인 동전 시스템에서 그리디 알고리즘이 최적해보다 많은 동전을 쓰는 가장 작은 목표값을 찾고, 없으면 -1을 출력한다.
어려움8동적 계획법그리디아직 제출이 없습니다시간 제한2초메모리 제한512 MBThe change-making problem is a classical competitive programming problem. The problem is about a currency system. In this system, there are n available denominations of coins \c1,c2, . . . , \cnwherec1=1andc2,...,cnareallintegers.Assumeyoucanhaveunlimitedsupplyofeachdenominationofcoins.Thechange−makingproblemis:givenatargetx,makeachangeofx with minimum number of coins.
The change-making problem has appeared so many times in programming contest. Many contestants know how to solve this problem. However, there are also many contestants doing it wrongly. For example, many of them use greedy algorithm which does not work. This greedy algorithm repeatedly takes the coin with greatest value which does not exceed x. This algorithm actually works for some currency system but not for this case: \1,3, \4andthetargetx=6.Wecallsuchcaseacounterexampletothisgreedyalgorithm,andx=6isawitnessofthecurrencysystem1, \3,4.
Please write a program to find the minimum witness for a given currency system. If there does not exist a witness or the minimum witness is greater than 105, your program should output −1.
The first line contains an integer n to indicate the number of denominations in the currency system. The second line contains n integers c1, . . . , cn where the currency system has \c1,...,cn.
Output the minimum witness x if x ≤ 105. Otherwise output −1.