出题人题解。
由于 是 的倍数,那么贪心地从大往小取一定最优。
首先,发现无解情况共有两个: 或 。前者购买 元的商品需要 枚硬币,后者购买 元的商品需要 枚硬币,他们都单调不降。
否则,如果 ,那么 。购买 元的商品分别需要 枚硬币,而购买 元的商品只需要 枚硬币。显然有 。
如果 ,以上的构造就失效了,因为购买 元或 元的商品都只需要 枚硬币,此时 。购买 元的商品分别需要 枚硬币,而购买 元的商品只需要 枚硬币。其中 ,所以 。
综上,答案如下:
//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;
mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
uniform_int_distribution<int> dist(L, R);
return dist(rnd);
}
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
const int N = 15;
int n, a[N];
int main() {
scanf("%d", &n);
rep(i, 1, n) scanf("%d", &a[i]);
if(n == 1) puts("-1");
else if(n == 2 && a[2] == 2) puts("-1");
else if(a[2] == 2) printf("%d\n", a[3]);
else printf("%d\n", a[2]);
return 0;
}