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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

using namespace std;

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

    int n;
    char a;
    int count_1 = 0;
    int min_n, max_n;
    int tab[7];
    int count = 0;

    char c[7] = {'!', '!', '!', 'a', 'c', 'g', 'o'};

    for (int i = 0; i < 7; ++i) {
        tab[i] = 0;
    }

    cin >> n;

    for (int i = 0; i < 8 * n; ++i) {
        cin >> a;
        if (a == '1') {
            ++count_1;
        }
    }

    int r = count_1 % 6;
    int x = count_1 / 6;

    if (r != 0) {
        min_n = x + 1;
    } else {
        min_n = x;
    }

    max_n = count_1 / 3;

    if (n < min_n || n > max_n) {
        cout << "NIE";
        return 0;
    }

    if (r == 0) {
        tab[6] = x;
    }

    if (r == 1) {
        tab[6] = --x;
        tab[3] = 1;
        tab[4] = 1;
    }

    if (r == 2) {
        tab[6] = --x;
        tab[4] = 2;
    }

    if (r == 3 || r == 4 || r == 5) {
        tab[6] = x;
        tab[r] = 1;
    }

    for (int i = 3; i < 7; ++i) {
        count += tab[i];
    }

    while (count != n) {
        --tab[6];
        tab[3] += 2;
        ++count;
    }

    for (int i = 3; i < 7; ++i) {
        for (int j = 0; j < tab[i]; ++j) {
            cout << c[i];
        }
    }

    return 0;
}