#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<int> A(N);
vector<int> S(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
S[i] = 1 << A[i];
}
const auto CheckAns = [&](lint K) {
for (int i = 0; i < N; i++) {
auto str = to_string(K);
if (count(begin(str), end(str), '0' + A[i]) == 0) {
return false;
}
K += 1;
}
return true;
};
const lint INF = 1e17;
const auto Solve = [&](const auto &self, vector<int> S, int lastNonZero, int depth) -> lint {
if (depth >= 17) return INF;
if (S.size() == 1) {
if (S[0] == 0) return lastNonZero ? 0 : 1;
int b = 0;
lint res = 0;
if (S[0] & 1) { // must have 0
lint opt = 1;
for (b = 1; b <= 9; b++) {
if ((S[0] >> b) & 1) {
opt = b;
break;
}
}
res = opt * 10;
}
for (b++; b <= 9; b++) {
if ((S[0] >> b) & 1) {
res = res * 10 + b;
}
}
return res;
}
lint res = INF;
for (int st = 0; st <= 9; st++) {
int dig = st;
vector<int> newS;
int last = 0;
for (auto add : S) {
if ((add >> dig) & 1) add ^= 1 << dig;
last |= add;
if (dig == 9) {
newS.emplace_back(last);
last = 0;
}
dig = (dig + 1) % 10;
}
if (dig != 0) {
newS.emplace_back(last);
}
lint newRes = self(self, newS, st != 0, depth + 1);
res = min(res, newRes * 10ll + st);
}
return res;
};
lint ans = Solve(Solve, S, 0, 0);
assert(ans > 0 && CheckAns(ans));
for (lint i = 1; i <= 1000; i++) {
if (CheckAns(i)) {
ans = min(ans, i);
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
7 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
312 KB |
Output is correct |
8 |
Correct |
12 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
10 ms |
324 KB |
Output is correct |
11 |
Correct |
12 ms |
308 KB |
Output is correct |
12 |
Correct |
2 ms |
204 KB |
Output is correct |
13 |
Correct |
3 ms |
312 KB |
Output is correct |
14 |
Correct |
13 ms |
328 KB |
Output is correct |
15 |
Correct |
14 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
5 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
3 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
17 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
204 KB |
Output is correct |
9 |
Correct |
12 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
16 ms |
332 KB |
Output is correct |
12 |
Correct |
10 ms |
328 KB |
Output is correct |
13 |
Correct |
13 ms |
368 KB |
Output is correct |
14 |
Correct |
2 ms |
204 KB |
Output is correct |
15 |
Correct |
4 ms |
204 KB |
Output is correct |
16 |
Correct |
12 ms |
328 KB |
Output is correct |
17 |
Correct |
21 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
204 KB |
Output is correct |
19 |
Correct |
4 ms |
204 KB |
Output is correct |
20 |
Correct |
11 ms |
204 KB |
Output is correct |
21 |
Correct |
4 ms |
312 KB |
Output is correct |
22 |
Correct |
11 ms |
204 KB |
Output is correct |
23 |
Correct |
13 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
136 ms |
472 KB |
Output is correct |
3 |
Correct |
127 ms |
464 KB |
Output is correct |
4 |
Correct |
110 ms |
484 KB |
Output is correct |
5 |
Correct |
110 ms |
484 KB |
Output is correct |
6 |
Correct |
120 ms |
432 KB |
Output is correct |
7 |
Correct |
692 ms |
1356 KB |
Output is correct |
8 |
Correct |
451 ms |
1000 KB |
Output is correct |
9 |
Correct |
987 ms |
1792 KB |
Output is correct |
10 |
Correct |
981 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
5 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
428 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
12 ms |
440 KB |
Output is correct |
9 |
Correct |
3 ms |
308 KB |
Output is correct |
10 |
Correct |
12 ms |
316 KB |
Output is correct |
11 |
Correct |
991 ms |
1800 KB |
Output is correct |
12 |
Correct |
963 ms |
1860 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
12 ms |
332 KB |
Output is correct |
15 |
Correct |
13 ms |
320 KB |
Output is correct |
16 |
Correct |
12 ms |
328 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
204 KB |
Output is correct |
19 |
Correct |
13 ms |
276 KB |
Output is correct |
20 |
Correct |
13 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
204 KB |
Output is correct |
22 |
Correct |
4 ms |
204 KB |
Output is correct |
23 |
Correct |
16 ms |
320 KB |
Output is correct |
24 |
Correct |
4 ms |
204 KB |
Output is correct |
25 |
Correct |
11 ms |
204 KB |
Output is correct |
26 |
Correct |
11 ms |
332 KB |
Output is correct |
27 |
Correct |
114 ms |
460 KB |
Output is correct |
28 |
Correct |
187 ms |
468 KB |
Output is correct |
29 |
Correct |
113 ms |
444 KB |
Output is correct |
30 |
Correct |
146 ms |
492 KB |
Output is correct |
31 |
Correct |
88 ms |
432 KB |
Output is correct |
32 |
Correct |
786 ms |
1368 KB |
Output is correct |
33 |
Correct |
453 ms |
1004 KB |
Output is correct |
34 |
Execution timed out |
1004 ms |
1796 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |