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
#include <iostream>
#include <algorithm>
using namespace std;

int n;
long long sum[500000];
long long sum_sorted[500000];

int main()
{
  ios_base::sync_with_stdio(0);

  cin >> n;
  for (int i=0; i<n; i++) {
    cin >> sum[i];
    sum_sorted[i] = sum[i];
  }

  sort(sum_sorted, sum_sorted + n);

  if (sum_sorted[0] == sum_sorted[n - 1]) {
    // no king in the pond
    for (int i=0; i<n; i++)
      cout << 'N';
  }
  else {
    int king_idx = 0;
    int food_idx = 1;
    long long king_weight = sum_sorted[0];

    while (king_weight <= sum_sorted[n - 1]) {
      if (king_weight <= sum_sorted[food_idx]) {
        // we need larger king
        int current_threshold = sum_sorted[food_idx - 1];
        while (sum_sorted[food_idx - 1] == current_threshold) {
          king_weight += sum_sorted[food_idx];
          food_idx++;
        }
        king_idx = food_idx - 1;
      }
      else {
        king_weight += sum_sorted[food_idx];
        food_idx++;
      }
    }

    int king_threshold = sum_sorted[king_idx];

    for (int i=0; i<n; i++) {
      if (sum[i] >= king_threshold)
        cout << 'T';
      else
        cout << 'N';
    }
  }

  return 0;
}