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

using namespace std;
typedef long long LL;

const int MAX = 500005;
struct V {
    int id;
    LL val;
};
V a[MAX];
LL sums[MAX];
char res[MAX];



int main() {
    ios_base::sync_with_stdio(false);
    int n = 0;
    cin >> n;
    
    for(int i = 0; i < n; ++i) {
        cin >> a[i].val;
        a[i].id = i;
    }
    sort(a, a+n, [](V a, V b) {
        return a.val < b.val;
    });
    LL s = 0;
    for(int i = 0; i < n; ++i) {
        s += a[i].val;
        sums[i] = s;
    }

    for(int i = 0; i < n; ++i) {
        if(a[i].val == a[0].val) {
            res[a[i].id] = 'N';
            continue;
        }
        
        int j = i;
        while(1) {
            if(j >= n-1) {
                res[a[i].id] = 'T';
                break;
            }
            if(a[j+1].val >= sums[j]) {
                res[a[i].id] = 'N';
                break;
            }
            // szukaj ostatniej ryby w przedziale [j+1..n) < sums[j]
            LL sms = sums[j];
            V* up = upper_bound<V*, V>(a+j+1, a+n, {0, sms-1}, [](V a, V b) {
                return a.val < b.val;
            })-1;
            j += distance(a+j, up);
        }
    }

    for(int i = 0; i < n; ++i) 
        cout << res[i];
    cout << endl;
}