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
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
inline std::string sum(const std::vector<int>& sums)
{
    std::vector<int> sorted_sums(sums.size());
    // O(Nlog(N))
    std::partial_sort_copy(sums.begin(),
                           sums.end(),
                           sorted_sums.begin(),
                           sorted_sums.end());
    std::unordered_map<int, char> can_be_king{};
    std::vector<int> weights(sums.size());
    int total_weight{0};
    int fattest{sorted_sums.back()};
    // O(N)
    for (int i = 0; i < sorted_sums.size(); ++i) {
        if (i && sorted_sums[i] > sorted_sums[i - 1]) {
            weights[i] = total_weight + sorted_sums[i];
        } else {
            weights[i] = sorted_sums[i];
        }
        total_weight += sorted_sums[i];
        int j = i;
        while (j < sorted_sums.size() - 1 && weights[i] > sorted_sums[j + 1]) {
            weights[i] += sorted_sums[j + 1];
            j++;
        }

        if (weights[i] > fattest) {
            can_be_king.try_emplace(sorted_sums[i], 'T');
        } else {
            can_be_king.try_emplace(sorted_sums[i], 'N');
        }
    }
    std::string output{};
    // O(N)
    for (const int val : sums) {
        output += can_be_king[val];
    }
    return output;
}

int main()
{
    int sum_count{0};
    std::cin >> sum_count;
    std::vector<int> sums(sum_count);
    for (int i = 0; i < sum_count; ++i) {
        std::cin >> sums[i];
    }
    std::cout << sum(sums) << "\n";
}