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
#include <cstdio>

unsigned int buf[9];

int main() {
    int n;
    scanf("%d", &n); getchar();
    for(int j = 0; j < 9; ++j) {
        buf[j] = 0;
    }
    for (int i = 0; i < n; ++i) {
        int cnt = 0;
        for(int j = 0; j < 8; ++j) {
            char c = getchar();
            if (c == '1') ++cnt;
        }
        ++buf[cnt];
    }
    // niedomiar
    for (int i = 0; i < 3; ++i) {
        while (buf[i] > 0) {
            int found = 0;
            for(int j = 8; j > 3; --j) {
                if (buf[j] <= 0) continue;
                found = 1;
                --buf[i];
                --buf[j];
                ++buf[j-1];
                ++buf[i+1];
                break;
            }
            if (found == 0) {
                printf("NIE\n");
                return 0;
            }
        }
    }
    // nadmiar (nie ma juz 0,1 i 2)
    for (int i = 8; i > 6; --i) {
        while (buf[i] > 0) {
            int found = 0;
            for(int j = 3; j < 6; ++j) {
                if (buf[j] <= 0) continue;
                found = 1;
                --buf[i];
                --buf[j];
                ++buf[i-1];
                ++buf[j+1];
                break;
            }
            if (found == 0) {
                printf("NIE\n");
                return 0;
            }
        }
    }
    while(buf[3]-- > 0) printf("a");
    while(buf[4]-- > 0) printf("c");
    while(buf[5]-- > 0) printf("g");
    while(buf[6]-- > 0) printf("o");
    printf("\n");
    return 0;
}