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
79
80
81
82
83
84
#include <bits/stdc++.h>

using namespace std;

const int M_SIZE = 5e5+1;

long long a[M_SIZE];

int n;

bool res[M_SIZE];

vector<pair<int, int> > v;

long long lower_sum[M_SIZE];
long long eq_sum[M_SIZE];

bool test_ans(int k) {
    long long sum = lower_sum[k] + v[k].first;
    if(lower_sum[k] > 0)
        sum += eq_sum[k];
    //cout<<"_"<<sum<<"_ ";
    while(k < v.size() - 1) {
        if(sum <= v[k+1].first){
            return false;
        } else {
            sum += v[k+1].first;
            k++;
        }
    }
    return (sum > v[v.size()-2].first);
}

int main() {

    cin>>n;

    for(size_t i=0; i<n; i++){
        cin>>a[i];
        v.emplace_back(a[i], i);
    }

    sort(v.begin(), v.end());

    long long temp_sum=0;

    for(size_t i=1; i<v.size(); i++){
        if(v[i].first > v[i-1].first){
            lower_sum[i] += v[i-1].first + temp_sum + lower_sum[i-1];
            temp_sum = 0;
        } else{ 
            temp_sum += v[i-1].first;
            eq_sum[i] += v[i-1].first;
            lower_sum[i] += lower_sum[i-1];
        }
    }

    int p=0, q=n-1, s;
    int first_res=n;

    while(p <= q){
        s = (p+q) / 2;
        //cout<<s<<" "<<test_ans(s)<<endl;
        if(test_ans(s)){
            first_res = s;
            q = s - 1;
        }else{
            p = s + 1;
        }

    }

    for(int i=first_res; i<n; i++)
        res[v[i].second] = 1;

    for(size_t i=0; i<v.size(); i++){
        if(res[i])
            cout<<"T";
        else
            cout<<"N";    
    }

    return 0;
}