CF10E.Greedy Change

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of nn coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum xx with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding xx . Obviously, if among the coins' face values exists the face value 1, any sum xx can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values 1,3,4{1,3,4} and it is asked to collect the sum 66 , greedy algorithm will represent the sum as 4+1+14+1+1 , while the optimal representation is 3+33+3 , containing one coin less. By the given set of face values find out if there exist such a sum xx that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums.

输入格式

The first line contains an integer nn ( 1<=n<=4001<=n<=400 ) — the amount of the coins' face values. The second line contains nn integers aia_{i} ( 1<=ai<=1091<=a_{i}<=10^{9} ), describing the face values. It is guaranteed that a1>a2>...>ana_{1}>a_{2}>...>a_{n} and an=1a_{n}=1 .

输出格式

If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.

输入输出样例

  • 输入#1

    5
    25 10 5 2 1
    

    输出#1

    -1
    
  • 输入#2

    3
    4 3 1
    

    输出#2

    6
    
首页