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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Autor: Mikołaj Janusz
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

int main() {
    ll n;
    cin >> n;

    int byte = 0;
    string result = "";
    vector<ll> occurences(2);
    string bytes;
    cin >> bytes;

    for (ll i = 0; i < 8 * n; i++) {
        if (bytes[i] == '0') {
            occurences[0]++;
        } else {
            occurences[1]++;
        }
    }

    if (occurences[0] - occurences[1] > 2 * n) {
        cout << "NIE";
        return 0;
    } else if (occurences[1] - occurences[0] > 4 * n) {
        cout << "NIE";
        return 0;
    }

    string res = "";
    while (occurences[0] + occurences[1] > 0) {
        if (occurences[0] - occurences[1] == 0) {
            occurences[0] -= 4;
            occurences[1] -= 4;
            res += "c";
        } else {
            if (occurences[0] > occurences[1]) {
                occurences[0] -= 5;
                occurences[1] -= 3;
                res += "a";
            } else {
                if (abs(occurences[1] - occurences[0]) < 4) {
                    occurences[0] -= 3;
                    occurences[1] -= 5;
                    res += "g";
                } else {
                    occurences[0] -= 2;
                    occurences[1] -= 6;
                    res += "o";
                }
            }
        }
    }

    if (occurences[0] != 0 || occurences[1] != 0) {
        cout << "NIE";
    } else {
        cout << res;
    }

    return 0;
}