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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream> 
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
const int MAXN = 500010;

char wn[MAXN] = {0};
long long ps[MAXN] = {0};
long long same[MAXN] = {0};


int main() {
    ios::sync_with_stdio(false);
    
    int n;
    vector<pair<long long,long long>> v;
    cin >> n;

    for(int i = 0; i < n; i++) {
        long long x;
        cin >> x;
        v.push_back(make_pair(x, (long long)i));
    }
    sort(v.begin(), v.end());
    long long curr = 0, prev = 0, smaller = 0, mn = 999999999999LL;

    prev = v[0].first;
    for(int i = 0; i < n; i++) {
        mn = min(mn, v[i].first);
        if (v[i].first != prev) {
            smaller += curr;

            int j = i-2;
            while(j >= 0 && v[j].first == v[i-1].first) {
                same[i-1] = curr;
                same[j] = curr;
                j--;
            }

            curr = 0;
        }
        ps[i] = smaller;
        curr += v[i].first;
        prev = v[i].first;
    }

    bool failed = false;

    if(v[n-1].first > mn) {
        wn[v[n-1].second] = 'T';
    } else {
        wn[v[n-1].second] = 'N';
        failed = true;
    }

    prev = v[n-1].first;
    for(int i = n-2; i >= 0; i--) {
        long long maybe_same = ps[i] > 0 ? same[i] : 0;
        // cout << v[i].second << ' ' << v[i].first << ' ' << ps[i] << ' ' << maybe_same << ' ' << prev << endl;
        if (failed) {
            wn[v[i].second] = 'N';
        } else if(v[i].first + ps[i] + maybe_same > prev) {
            wn[v[i].second] = 'T';
        } else {
            failed = true;
            wn[v[i].second] = 'N';
        }
        prev = v[i].first;
    }
    for(int i = 0; i < n; i++) {
        cout << wn[i];
    }
    cout << endl;
}