p546.ans1
======================
2
111022

=================
p546.ans2
======================
4
00000

=================
p546.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, a, b;
string s;

void read() {
    cin >> n >> a >> b >> s;
}

void solve() {
    // We need exactly a zeros and b ones; everything else becomes a two.
    //
    // Subtract the zeros and ones already present from a and b, so a and b
    // become the signed deficits: a positive value means we still need that
    // many of the digit, a negative value means we have that many in excess.
    //
    // If a + b > n there is no way to fit the required zeros and ones, so the
    // answer is -1.
    //
    // A surplus zero (a < 0) can directly cover a needed one (b > 0) by a
    // single relabel, and vice versa; each such swap fixes two deficits with
    // one replacement, so we greedily apply those first. Whatever deficit
    // remains is settled against the twos: each leftover replacement turns a
    // surplus 0/1 into a 2 or a 2 into a needed 0/1. The total number of
    // replacements is the sum of the remaining absolute deficits plus the
    // swaps already made.

    if(a + b > n) {
        cout << -1 << '\n';
        return;
    }

    for(char c: s) {
        if(c == '0') {
            a--;
        } else if(c == '1') {
            b--;
        }
    }

    int answer = 0;
    if(a > 0 && b < 0) {
        int q = min(a, -b);
        answer += q;
        b += q;
        a -= q;
        for(char& c: s) {
            if(c == '1' && q) {
                c = '0';
                q--;
            }
        }
    }

    if(b > 0 && a < 0) {
        int q = min(-a, b);
        answer += q;
        b -= q;
        a += q;
        for(char& c: s) {
            if(c == '0' && q) {
                c = '1';
                q--;
            }
        }
    }

    answer += abs(a) + abs(b);

    for(char& c: s) {
        if(a < 0 && c == '0') {
            a++;
            c = '2';
        } else if(a > 0 && c == '2') {
            a--;
            c = '0';
        } else if(b < 0 && c == '1') {
            b++;
            c = '2';
        } else if(b > 0 && c == '2') {
            b--;
            c = '1';
        }
    }

    cout << answer << '\n';
    cout << s << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p546.in1
======================
6 1 3
012022

=================
p546.in2
======================
5 5 0
02211

=================
statement.txt
======================
546. Ternary Password
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



In the ternary world all passwords are ternary, that is, they consist only of digits "0", "1" and "2". Terentius is trying to register on a famous internet service site Toogle, but the problem is, according to the security policy of this service the password must contain exactly a characters "0" and exactly b characters "1". All other characters of the password must be "2".

Terentius was fond of his password, he spent much time trying to remember it and now he can type it even with his eyes closed. That's the reason Terentius wants to replace the minimum number of characters in his password so that it meets the strict requirements of the Toogle policy. Terentius wants only to replace (substitute) some characters in password, he doesn't intend to perform other operations with the password.

Help Terentius find the minimum number of replacements and print the corresponding possible variant of the password to Toogle.

Input
The first line of the input contains three integers n, a and b (1 ≤ n ≤ 200; 0 ≤ a,b ≤ 200) — the length of Terentius's password, the expected number of characters "0" and the expected number of characters "1" in the password to Toogle. The second line contains Terentius's password. All characters in this line are digits "0", "1" or "2".

Output
In the first output line print t — the minimum number of replacements. In the second line print the possible variant of the Toogle password — the password that satisfies the Toogle safety policy, that differs from Terentius's password in exactly t positions. If there are several passwords, print any of them. Obviously, the length of the printed password must equal n.

It is possible that Terentius's password already meets the Toogle policy. In this case the first line must contain "0", and the second line must contain Terentius's password.

If the solution doesn't exist, that is, if it is impossible to get a password to Toogle if Terentius uses replacements only, then print "-1" in the first line of the output. In this case print empty second line or don't print the second line at all.

Example(s)
sample input
sample output
6 1 3
012022
2
111022

sample input
sample output
5 5 0
02211
4
00000

=================
