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

using namespace std;

int n;

vector<pair<long long, char>> d(500001);
vector<pair<long long, char>*> v(500001);
vector<long long> prefixSum(500001);

bool comp(pair<long long, char>* p1, pair<long long, char>* p2){
    if(p1->first < p2->first){
        return true;
    }
    return false;
}

bool isValidSolution(int idx){
    long long val = v[idx]->first;
    for(int i = 0; i < n; ++i){
        if(i != idx){
            if(v[i]->first < val){
                val += v[i]->first;
            }
            else{
                return false;
            }
        }
    }
    return true;
}

int main(){
    cin >> n;
    long long temp;
    for(int i = 0; i < n; ++i){
        cin >> temp;
        d[i].first = temp;
        v[i] = &d[i];
    }
    sort(v.begin(), v.begin() + n, comp);
    long long prev = -1LL;
    int cnt = 0;
    long long currSum1 = 0LL; // updatowana po cigu tych samych wielkosci
    long long currSum2 = 0LL; // prawdziwa
    
    for(int i = 0; i < n; ++i){
        if(v[i]->first != prev){
            prev = v[i]->first;
            currSum1 = currSum2;
        }
        prefixSum[i] = currSum1;
        currSum2 += v[i]->first;
    }
    int p = 0, q = n - 1;
    int mid;
    int res = 500001;
    while(p <= q){
        mid = (p + q) / 2;
        if(isValidSolution(mid)){
            res = mid;
            q = mid - 1;
        }
        else{
            p = mid + 1;
        }
    }
    for(int i = 0; i < n; ++i){
        if(i < res){
            v[i]->second = 'N';
        }
        else{
            v[i]->second = 'T';
        }
    }
    for(int i = 0; i < n; ++i){
        cout << d[i].second;
    }
    cout << '\n';
    return 0;
}