1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(0);
    int n;
    cin >> n;
    int ones = -2 * n, zeros = -n;
    for (int i = 1; i <= 8 * n; i++) {
        char a;
        cin >> a;
        (a == '1' ? ones : zeros) += 1;
    }
    function<bool(int, int, int)> dfs = [&](int o, int z, int c) {
        if (c > n || o > ones || z > zeros || o + n - c > ones || z + n - c > zeros || o + (n - c) * 4 < ones || z + 4 * (n - c) < zeros) return false;
        if (c == n && o == ones && z == zeros) return true;
        if (dfs(o + 1, z + 4, c + 1)) {
            cout << "a";
            return true;
        } else if (dfs(o + 4, z + 1, c + 1)) {
            cout << "o";
            return true;
        } else if (dfs(o + 2, z + 3, c + 1)) {
            cout << "c";
            return true;
        } else if (dfs(o + 3, z + 2, c + 1)) {
            cout << "g";
            return true;
        }
        return false;
    };
    if (!dfs(0, 0, 0))
        cout << "NIE\n";
}