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
#include <cstdio>
#include <cstdlib>
#include <cassert>

#include <algorithm>

struct catfish {
    int idx;
    long long int weight;
};

catfish catfishes[500 * 1000 + 1];
char response[500 * 1000 + 1];
long long int suffix_requirement[500 * 1000 + 1];

int main() {
    int n;
    assert(1 == scanf("%d", &n));

    for (int i = 0; i < n; i++) {
        catfishes[i].idx = i;
        assert(1 == scanf("%lld", &catfishes[i].weight));
    }

    std::sort(catfishes, catfishes + n, [] (const catfish& c1, const catfish& c2) {
        return c1.weight < c2.weight;
    });

    suffix_requirement[n] = 0;

    for (int i = n - 1; i > 0; i--) {
        suffix_requirement[i] = std::max(
            catfishes[i].weight + 1,
            suffix_requirement[i + 1] - catfishes[i].weight
        );
    }

    long long int prefix_sum = 0;
    long long int prefix_requirement = 0;

    for (int i = 0; i < n; i++) {
        // printf("prefix_requirement: %lld\n", prefix_requirement);
        // printf("suffix_requirement: %lld\n", suffix_requirement[i + 1]);
        char ans = 'N';
        if (catfishes[i].weight >= prefix_requirement) {
            // We can eat everybody before us
            if (catfishes[i].weight + prefix_sum >= suffix_requirement[i + 1]) {
                ans = 'T';
            } else {
                // printf("Catfish #%d can't eat suffix", catfishes[i].idx);
            }
        } else {
            // printf("Catfish #%d can't eat prefix", catfishes[i].idx);
        }
        response[catfishes[i].idx] = ans;

        prefix_requirement = std::max(
            prefix_requirement,
            catfishes[i].weight - prefix_sum + 1
        );
        prefix_sum += catfishes[i].weight;
    }

    response[n] = '\0';
    puts(response);

    return 0;
}