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
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;

int n, poczJedynek, poczZer;
char ch;

void calc(int jedynek, int zer, int o, int g, int c, int a) {
    if (jedynek < 0 || zer < 0) return;
    else if ((jedynek == 0 && zer != 0) || (zer == 0 && jedynek != 0)) return;
    else if (jedynek == 0 && zer == 0) {
        if (o + g + c + a == n) {
            for (int i = 0; i < a; ++i) {
                cout << "a";
            }
            for (int i = 0; i < c; ++i) {
                cout << "c";
            }
            for (int i = 0; i < g; ++i) {
                cout << "g";
            }
            for (int i = 0; i < o; ++i) {
                cout << "o";
            }
            cout << endl;
            exit(0);
        } else {
            return;
        }
    }
    calc(jedynek - 6, zer - 2, o + 1, g, c, a);
    calc(jedynek - 5, zer - 3, o, g + 1, c, a);
    calc(jedynek - 4, zer - 4, o, g, c + 1, a);
    calc(jedynek - 3, zer - 5, o, g, c, a + 1);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n * 8; ++i) {
        cin >> ch;
        if (ch == '1') poczJedynek++;
        else poczZer++;
    }

    calc(poczJedynek, poczZer, 0, 0, 0, 0);
    cout << "NIE" << endl;
}