0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-12 15:46:28 +00:00
OI-codes/Gym/103860/F/F.cpp

54 lines
777 B
C++
Raw Normal View History

#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 22;
int n;
long long a[N], x, ans;
bool vis[N];
void dfs(int idx, long long now, int cnt) {
if (now < ans) return;
if (cnt == n || now < a[1]) {
ans = std::max(ans, now);
return;
}
vis[idx] = true;
for (int i = 1; i <= n; i++) {
if (now < a[i]) break;
if (vis[i]) continue;
dfs(i, now % a[i], cnt + 1);
}
vis[idx] = false;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> x;
std::sort(a + 1, a + 1 + n);
dfs(0, x, 0);
cout << ans << endl;
return 0;
}