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

int32_t main(){
    ios::sync_with_stdio(false);
    map<int, char> onesToChar;
    for(int i='a';i<='z';i++)
        onesToChar[__builtin_popcount(i)] = (char)i;

    int L = onesToChar.begin()->first;
    int R = onesToChar.rbegin()->first;
    for(int i=L;i<=R;i++)
        assert(onesToChar.count(i));

    int n;
    cin >> n;
    string s;
    cin >> s;
    int ones = count(s.begin(), s.end(), '1');
    vector<int> sol(n,L);
    ones -= n*L;
    int pos = 0;
    while(pos < n && ones > 0) {
        int diff = min(ones, R-L);
        sol[pos] = L+diff;
        ones -= diff;
        pos++; 
    }
    if(ones != 0)
        cout<<"NIE";
    else
        for(auto a:sol)
            cout<<onesToChar[a];
}