#include <bits/stdc++.h>
using namespace std;
long long pw[18];
long long res = 1e18;
bool in[10];
void go(vector<int> vec, long long cur, int cnt, bool zero) {
if (cur >= res) return;
if (vec.size() == 1) {
vector<int> buf;
for (int i = 9; i >= 0; --i) {
if (vec[0] >> i & 1) buf.push_back(i);
}
if (buf.size() == 1 && buf[0] == 0) zero = 1;
else if (buf.size() && buf.back() == 0) {
swap(buf[buf.size() - 2], buf[buf.size() - 1]), zero = 0;
}
else if (buf.size()) zero = 0;
for (auto i : buf) cur += i * pw[cnt++];
if (zero) cur += pw[cnt];
res = min(res, cur);
return;
}
vector<int> buf;
int j = 0, mask = 0;
bool flag = 0;
for (auto i : vec) {
if (!j && !flag) {}
else if (i >> j & 1) i ^= 1 << j;
mask |= i, j++;
if (j == 10) {
buf.push_back(mask), mask = j = 0;
flag = 1;
}
}
if (j) buf.push_back(mask);
if (buf.size() == 1 && !buf[0]) res = min(res, cur);
for (int i = 0; i < 10; ++i) {
vector<int> buf;
int k = i, mask = 0;
for (auto j : vec) {
if (j >> k & 1) j ^= 1 << k;
mask |= j, k++;
if (k == 10) {
buf.push_back(mask), mask = k = 0;
}
}
if (k) buf.push_back(mask);
go(buf, cur + i * pw[cnt], cnt + 1, !i);
}
}
int main() {
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> vec;
for (int i = 0; i < n; ++i) {
int x; cin >> x, vec.push_back(1 << x);
}
pw[0] = 1;
for (int i = 1; i < 18; ++i) pw[i] = pw[i - 1] * 10;
go(vec, 0, 0, 0);
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
14 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
6 ms |
488 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
648 KB |
Output is correct |
7 |
Correct |
3 ms |
688 KB |
Output is correct |
8 |
Correct |
5 ms |
688 KB |
Output is correct |
9 |
Correct |
3 ms |
688 KB |
Output is correct |
10 |
Correct |
16 ms |
688 KB |
Output is correct |
11 |
Correct |
9 ms |
688 KB |
Output is correct |
12 |
Correct |
4 ms |
688 KB |
Output is correct |
13 |
Correct |
8 ms |
688 KB |
Output is correct |
14 |
Correct |
4 ms |
688 KB |
Output is correct |
15 |
Correct |
4 ms |
688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
688 KB |
Output is correct |
2 |
Correct |
12 ms |
688 KB |
Output is correct |
3 |
Correct |
3 ms |
688 KB |
Output is correct |
4 |
Correct |
6 ms |
688 KB |
Output is correct |
5 |
Correct |
2 ms |
688 KB |
Output is correct |
6 |
Correct |
2 ms |
688 KB |
Output is correct |
7 |
Correct |
5 ms |
688 KB |
Output is correct |
8 |
Correct |
3 ms |
688 KB |
Output is correct |
9 |
Correct |
4 ms |
688 KB |
Output is correct |
10 |
Correct |
2 ms |
688 KB |
Output is correct |
11 |
Correct |
27 ms |
688 KB |
Output is correct |
12 |
Correct |
17 ms |
688 KB |
Output is correct |
13 |
Correct |
6 ms |
688 KB |
Output is correct |
14 |
Correct |
5 ms |
688 KB |
Output is correct |
15 |
Correct |
8 ms |
688 KB |
Output is correct |
16 |
Correct |
5 ms |
688 KB |
Output is correct |
17 |
Correct |
4 ms |
688 KB |
Output is correct |
18 |
Correct |
6 ms |
688 KB |
Output is correct |
19 |
Correct |
11 ms |
688 KB |
Output is correct |
20 |
Correct |
36 ms |
688 KB |
Output is correct |
21 |
Correct |
27 ms |
688 KB |
Output is correct |
22 |
Correct |
40 ms |
764 KB |
Output is correct |
23 |
Correct |
40 ms |
764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
764 KB |
Output is correct |
2 |
Correct |
45 ms |
768 KB |
Output is correct |
3 |
Correct |
48 ms |
768 KB |
Output is correct |
4 |
Correct |
37 ms |
768 KB |
Output is correct |
5 |
Correct |
66 ms |
768 KB |
Output is correct |
6 |
Correct |
29 ms |
768 KB |
Output is correct |
7 |
Correct |
156 ms |
1276 KB |
Output is correct |
8 |
Correct |
109 ms |
1276 KB |
Output is correct |
9 |
Correct |
192 ms |
1528 KB |
Output is correct |
10 |
Correct |
215 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
21 ms |
1528 KB |
Output is correct |
3 |
Correct |
2 ms |
1528 KB |
Output is correct |
4 |
Correct |
5 ms |
1528 KB |
Output is correct |
5 |
Correct |
863 ms |
1528 KB |
Output is correct |
6 |
Correct |
2 ms |
1528 KB |
Output is correct |
7 |
Correct |
3 ms |
1528 KB |
Output is correct |
8 |
Correct |
5 ms |
1528 KB |
Output is correct |
9 |
Correct |
3 ms |
1528 KB |
Output is correct |
10 |
Correct |
5 ms |
1528 KB |
Output is correct |
11 |
Correct |
494 ms |
1528 KB |
Output is correct |
12 |
Correct |
264 ms |
1528 KB |
Output is correct |
13 |
Correct |
2 ms |
1528 KB |
Output is correct |
14 |
Correct |
25 ms |
1528 KB |
Output is correct |
15 |
Correct |
17 ms |
1528 KB |
Output is correct |
16 |
Correct |
8 ms |
1528 KB |
Output is correct |
17 |
Correct |
5 ms |
1528 KB |
Output is correct |
18 |
Correct |
8 ms |
1528 KB |
Output is correct |
19 |
Correct |
5 ms |
1528 KB |
Output is correct |
20 |
Correct |
5 ms |
1528 KB |
Output is correct |
21 |
Correct |
5 ms |
1528 KB |
Output is correct |
22 |
Correct |
8 ms |
1528 KB |
Output is correct |
23 |
Correct |
40 ms |
1528 KB |
Output is correct |
24 |
Correct |
11 ms |
1528 KB |
Output is correct |
25 |
Correct |
53 ms |
1528 KB |
Output is correct |
26 |
Correct |
41 ms |
1528 KB |
Output is correct |
27 |
Correct |
49 ms |
1528 KB |
Output is correct |
28 |
Correct |
50 ms |
1528 KB |
Output is correct |
29 |
Correct |
43 ms |
1528 KB |
Output is correct |
30 |
Correct |
89 ms |
1528 KB |
Output is correct |
31 |
Correct |
20 ms |
1528 KB |
Output is correct |
32 |
Correct |
146 ms |
1528 KB |
Output is correct |
33 |
Correct |
125 ms |
1528 KB |
Output is correct |
34 |
Correct |
234 ms |
1648 KB |
Output is correct |
35 |
Correct |
298 ms |
1648 KB |
Output is correct |
36 |
Execution timed out |
1078 ms |
1648 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |