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
#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;

int n, onesCount, divN, remN;
string s;
vector<int> toAdd;
unordered_map<int, char> numToChar = {{3, 'a'}, {4, 'c'}, {5, 'g'}, {6, 'o'}};

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> n;
    cin >> s;

    for(const char c : s)
    {
        if(c == '1')
        {
            ++onesCount;
        }
    }

    divN = onesCount/n;
    remN = onesCount%n;

    if(divN < 3 || divN > 6 || (divN == 6 && remN > 0))
    {
        cout << "NIE" << endl;
        return 0;
    }

    while(remN > 0)
    {
        const int maxToAdd = 6-divN;
        const int actToAdd = min(maxToAdd, remN);

        toAdd.push_back(actToAdd);
        remN -= actToAdd;
    }

    if(toAdd.size() > n)
    {
        cout << "NIE" << endl;
        return 0;
    }

    for(int i = 0; i < n; ++i)
    {
        const int actToAdd = (i < toAdd.size() ? toAdd[i] : 0);
        cout << numToChar[divN+actToAdd];
    }

    cout << endl;

    return 0;
}