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

int main() {
    using namespace std;
    using ll = long long;
    using tiib = tuple<int, int, bool>;
    int n;
    cin >> n;
    vector<tiib> A(n);
    for(int i = 0; i < n; i++) {
        cin >> get<0>(A[i]);
        get<1>(A[i]) = i;
    }
    sort(A.begin(), A.end());
    int a = 0, b = n, c;
    while(a < b) {
        c = (a + b) / 2;
        ll q = get<0>(A[c]);
        int i;
        for(i = 0; i < n; i++) {
            if(i == c) {
                continue;
            } else if(q > get<0>(A[i])) {
                q += get<0>(A[i]);
            } else {
                break;
            }
        }
        if(i == n)
            b = c;
        else
            a = c + 1;
    }
    for(; a < n; a++) get<2>(A[a]) = true;
    sort(A.begin(), A.end(), [](const tiib& a, const tiib& b) {
                return get<1>(a) < get<1>(b);
            });
    string result; // TODO prealloc, CAUTION counterintuitive constructor
    for(int i = 0; i < n; i++) {
        if(get<2>(A[i]))
            result += 'T';
        else
            result += 'N';
    }
    cout << result << "\n";

    return 0;
}