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
#pragma GCC optimize ("O3")

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    char ascii[] = {' ', ' ', ' ', 'a', 'c', 'g', 'o'};
    float n, no1 = 0.0;
    int med_let;
    string word, result;
    cin >> n;
    cin >> word;
    no1 = count(word.begin(), word.end(), '1');
    int dif = 0, lower, higher;
    med_let = ceil(no1 / n);
    for (int i = 0; i < n; i++) {
        if (!(med_let <= 6 && med_let >= 3)) {
            result = "NIE";
            i = n;
        }
        lower = no1 - (med_let - 1) * (n - i);
        higher = (med_let + 1) * (n - i) - no1;
        if (lower == higher) {
            result += string(n - i, ascii[med_let]);
            i = n;
        } else if (lower <= n && lower > i) {
            result += string(lower - i, ascii[med_let]);
            no1 -= med_let * (lower - i);
            med_let--;
            i = lower - 1;
        } else if (higher <= n && higher > i) {
            result += string(higher - i, ascii[med_let]);
            no1 -= med_let * (lower - i);
            med_let++;
            i = higher - 1;
        } else {
            result += string(n - i, ascii[med_let]);
            i = n;
        }
    }
    cout << result;
    return 0;
}