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
85
86
87
88
89
#include <bits/stdc++.h>

using namespace std;

constexpr int LIMIT = 500007;

// first = numer
// second = i(d)
pair<int, int> tab[LIMIT];
long long prefix[LIMIT];

// odpowiedz dla danego i, 0 = N, 1 = T
bitset<LIMIT> odp;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> tab[i].first;
        tab[i].second = i;
    }
    sort(tab, tab+n);

    //for(int i = 0; i < n; i++) {
    //    cout << tab[i].first << " ";
    //}
    //cout << "\n";

    prefix[0] = tab[0].first;
    for(int i = 1; i < n; i++) {
        prefix[i] = prefix[i-1] + tab[i].first;
    }

    //for(int i = 0; i < n; i++) {
    //    cout << prefix[i] << " ";
    //}

    int last = n-1;
    odp[tab[last].second] = true;
    for(int i = last-1; i >= 0; i--) {
        if (tab[i].first == tab[last].first) {
            odp[tab[i].second] = true;
            last = i;
        } else {
            break;
        }
    }
    if (last == 0) {
        for(int i = 0; i < n; i++) {
            cout << "N";
        }
        return 0;
    }
    //for(int i = 0; i < n; i++) {
    //    cout << odp[i] << " ";
    //}
    //cout << endl << endl;
    int howmanytofix = 1;
    last--;
    for(int i = last; i > 0; i--) {
        //cout << "i: " << i << " last: " << last << endl;;
        if(tab[i-1].first == tab[last].first) {
            //cout << i << ": Repeating digit" << endl;
            howmanytofix++;
        } else {
            if (prefix[i] > tab[last+1].first) {
                //cout << i << ": OMW" << endl;
                for(int j = i; j < i+howmanytofix; j++) {
                    //cout << j << ": Fixed" << endl;
                    odp[tab[j].second] = true;
                }
                howmanytofix = 1;
                last = i-1;
            } else {
                //cout << i << ": BREAK." << endl;
                break;
            }
        }
    }
    //cout << "\n\n";
    for(int i = 0; i < n; i++) {
        cout << ((odp[i]) ? "T" : "N");
    }

    return 0;
}