#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int n;
long long res = 1e16, p[17];
void solve(vector<int> bit, long long cb, int depth, int gotZero){
if(cb > res) return;
if(bit.size() == 1){
int r = bit[0];
if(r == 0){
if(gotZero) cb += p[depth];
}
else if(r == 1){
// signifies that we need zeros
cb += p[depth] * 10;
}
else{
vector<int> v;
for (int i=9; i>=0; i--) {
if((r>>i)&1) v.push_back(i);
}
if(v.back() == 0){
swap(v[v.size() - 2],v.back());
}
for (int i=0; i<v.size(); i++) {
cb += p[depth] * v[i];
depth++;
}
}
res = min(res,cb);
return;
}
for (int i=0; i<10; i++) {
vector<int> v2;
int piv = i;
v2.push_back(0);
for (int j=0; j<bit.size(); j++) {
v2.back() |= (bit[j] & (~(1<<piv)));
if(piv == 9 && j != bit.size() - 1){
v2.push_back(0);
piv = -1;
}
piv++;
}
solve(v2,cb + p[depth] * i,depth+1,(i == 0));
}
}
int main(){
vector<int> v;
scanf("%d",&n);
p[0] = 1;
for (int i=1; i<17; i++) {
p[i] = p[i-1] * 10;
}
for (int i=0; i<n; i++) {
int t;
scanf("%d",&t);
v.push_back(1<<t);
}
solve(v,0,0,0);
printf("%lld",res);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1240 KB |
Output is correct |
2 |
Incorrect |
12 ms |
1240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1240 KB |
Output is correct |
2 |
Incorrect |
12 ms |
1240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1240 KB |
Output is correct |
2 |
Correct |
44 ms |
1240 KB |
Output is correct |
3 |
Correct |
44 ms |
1240 KB |
Output is correct |
4 |
Correct |
24 ms |
1240 KB |
Output is correct |
5 |
Correct |
40 ms |
1240 KB |
Output is correct |
6 |
Correct |
12 ms |
1240 KB |
Output is correct |
7 |
Correct |
132 ms |
2024 KB |
Output is correct |
8 |
Correct |
120 ms |
1676 KB |
Output is correct |
9 |
Correct |
208 ms |
2144 KB |
Output is correct |
10 |
Correct |
220 ms |
2144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1240 KB |
Output is correct |
2 |
Incorrect |
12 ms |
1240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |