#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC target("sse4,avx2,fma,avx")
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
ll ans = LLONG_MAX, pw[18];
void solve(vector<int> sections, ll curr = 0, int level = 0, bool had_zero = false) {
if (curr > ans) return;
int n = sections.size();
if (n == 1) {
int mask = sections[0];
if (mask == 0) {
if (curr == 0 || had_zero) curr += pw[level];
} else if (mask == 1) curr += pw[level + 1];
else {
if (mask & 1) {
int mn = 10;
for (int i = 9; i; i--) {
if (mask & (1 << i)) mn = i;
}
for (int i = 9; i; i--) {
if ((mask & (1 << i)) && i != mn) curr += i * pw[level++];
}
curr += mn * pw[++level];
} else {
for (int i = 9; i; i--) {
if (mask & (1 << i)) curr += i * pw[level++];
}
}
}
ans = min(ans, curr);
return;
}
FOR(i, 0, 10) {
vector<int> merged;
int ins = 0;
bool zero = false;
int piv = i;
FOR(j, 0, n) {
zero |= (piv == 0 && (sections[j] & 1));
ins |= (sections[j] & (~(1 << piv)));
piv++;
if (piv == 10) {
merged.push_back(ins);
ins = piv = 0;
}
}
if (piv) merged.push_back(ins);
solve(merged, curr + pw[level] * i, level + 1, zero);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> sections(n);
FOR(i, 0, n) {
int x;
cin >> x;
sections[i] = 1 << x;
}
pw[0] = 1;
FOR(i, 1, 18) pw[i] = 10 * pw[i - 1];
solve(sections);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
276 KB |
Output is correct |
10 |
Correct |
7 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
12 ms |
376 KB |
Output is correct |
14 |
Correct |
4 ms |
376 KB |
Output is correct |
15 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
388 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
296 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
10 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
428 KB |
Output is correct |
13 |
Correct |
3 ms |
276 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
4 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
416 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
20 |
Correct |
13 ms |
416 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
14 ms |
380 KB |
Output is correct |
23 |
Correct |
14 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
416 KB |
Output is correct |
2 |
Correct |
27 ms |
504 KB |
Output is correct |
3 |
Correct |
24 ms |
424 KB |
Output is correct |
4 |
Correct |
26 ms |
504 KB |
Output is correct |
5 |
Correct |
37 ms |
604 KB |
Output is correct |
6 |
Correct |
13 ms |
504 KB |
Output is correct |
7 |
Correct |
98 ms |
1144 KB |
Output is correct |
8 |
Correct |
71 ms |
860 KB |
Output is correct |
9 |
Correct |
138 ms |
1400 KB |
Output is correct |
10 |
Correct |
137 ms |
1416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
211 ms |
760 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
243 ms |
1404 KB |
Output is correct |
12 |
Correct |
140 ms |
1400 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
10 ms |
424 KB |
Output is correct |
15 |
Correct |
7 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
296 KB |
Output is correct |
20 |
Correct |
4 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
376 KB |
Output is correct |
22 |
Correct |
4 ms |
376 KB |
Output is correct |
23 |
Correct |
13 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
14 ms |
376 KB |
Output is correct |
26 |
Correct |
13 ms |
376 KB |
Output is correct |
27 |
Correct |
25 ms |
504 KB |
Output is correct |
28 |
Correct |
28 ms |
476 KB |
Output is correct |
29 |
Correct |
26 ms |
632 KB |
Output is correct |
30 |
Correct |
30 ms |
524 KB |
Output is correct |
31 |
Correct |
8 ms |
396 KB |
Output is correct |
32 |
Correct |
98 ms |
1140 KB |
Output is correct |
33 |
Correct |
71 ms |
824 KB |
Output is correct |
34 |
Correct |
137 ms |
1400 KB |
Output is correct |
35 |
Correct |
117 ms |
1528 KB |
Output is correct |
36 |
Correct |
541 ms |
1180 KB |
Output is correct |
37 |
Correct |
682 ms |
1340 KB |
Output is correct |
38 |
Correct |
649 ms |
1008 KB |
Output is correct |
39 |
Execution timed out |
1043 ms |
1400 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |