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

int main()
{
    int N;
    scanf("%d", &N);
    std::vector<unsigned> weights(N);
    for (int i=0; i<N; ++i) {
        scanf("%u", &weights[i]);
    }
    std::vector<unsigned> sorted_weights(weights);
    std::sort(sorted_weights.begin(), sorted_weights.end());

    std::vector<unsigned long> cumulative(N);
    cumulative[0] = 0;
    unsigned long count_same_weight = 1;
    for (int i=1; i<N; ++i) {
        if (sorted_weights[i-1] == sorted_weights[i]) {
            cumulative[i] = cumulative[i-1];
            ++count_same_weight;
        } else {
            cumulative[i] = cumulative[i-1] + sorted_weights[i-1] * count_same_weight;
            count_same_weight = 1;
        }
    }

    for (int i=0; i<N; ++i) {
        int old_index = std::lower_bound(sorted_weights.begin(), sorted_weights.end(), weights[i]) - sorted_weights.begin();
        unsigned long new_weight = weights[i] + cumulative[old_index];
        while (true) {
            int new_index = std::lower_bound(sorted_weights.begin(), sorted_weights.end(), new_weight) - sorted_weights.begin();
            if (new_index == N) {
                putchar('T');
                break;
            }
            if (new_index <= old_index) {
                putchar('N');
                break;
            }
            new_weight = cumulative[new_index];
            old_index = new_index;
        }
    }
    putchar('\n');
}