#include <bits/stdc++.h>
using namespace std;
const int MAX = 100001;
string solve(vector<int> &arr, bool chk = false) {
    int N = arr.size(), X = 0, K = 0;
    string Y, ans = string(N + 1, '9');
    vector<int> lst, tmp;
    for (int i : arr)
        X |= i;
    if (X == 0)
        return "";
    for (int i = 0; i < 10; i++)
        if (X & (1 << i))
            lst.push_back(i);
    if (N == 1) {
        ans = "";
        if (lst.size() == 1 && lst[0] == 0)
            lst.push_back(1);
        if (lst[0] == 0)
            lst.erase(lst.begin()), K = 1;
        for (int i : lst) {
            ans += to_string(i);
            if (K)
                ans += '0', K = 0;
        }
        return ans;
    }
    assert(N > 1);
    for (int i = 0; i < 10; i++) {
        tmp.clear(), X = 0;
        for (int j = 0; j < N; j++) {
            K = (i + j) % 10;
            if (K == 0 && j != 0)
                tmp.push_back(X), X = 0;
            X |= (1023 ^ (1 << K)) & arr[j];
        }
        if (chk && N == 2 && tmp.size() == 1)
            continue;
        tmp.push_back(X), Y = solve(tmp, N == 2 && tmp.size() == 2) + to_string(i);
        if (Y == "0")
            Y = "10";
        if (Y.size() < ans.size() || (Y.size() == ans.size() && Y < ans))
            ans = Y;
    }
    return ans;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int N, B;
    vector<int> arr;
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> B, arr.push_back(1 << B);
    cout << solve(arr) << '\n';
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |