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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>

using namespace std;

struct TestCase
{
  size_t fish_count;
  vector<size_t> fish_sizes;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  cin >> tc.fish_count;
  tc.fish_sizes.resize(tc.fish_count);
  for (auto& fish : tc.fish_sizes) cin >> fish;
  return tc;
}

typedef unsigned long long fish_size_t;

struct Fish
{
  size_t index;
  fish_size_t size;
};

struct Shoal
{
  vector<Fish> fish;
  fish_size_t fish_size;
  size_t size;
};

bool operator<(const Shoal& lhs, const Shoal& rhs)
{
  return lhs.fish_size < rhs.fish_size;
}

vector<Shoal> make_shoals(const TestCase& tc)
{
  map<size_t, Shoal> size_to_shoal;
  for (size_t i = 0; i < tc.fish_count; i++)
  {
    size_to_shoal[tc.fish_sizes[i]].fish.push_back({i, tc.fish_sizes[i]});
    size_to_shoal[tc.fish_sizes[i]].fish_size = tc.fish_sizes[i];
    size_to_shoal[tc.fish_sizes[i]].size++;
  }
  vector<Shoal> shoals;
  for (auto [_, shoal] : size_to_shoal) shoals.push_back(shoal);
  sort(shoals.begin(), shoals.end());
  return shoals;
}

void mark_shoal_true(const Shoal& shoal, vector<bool>& can_become_king)
{
  for (const auto& fish : shoal.fish) can_become_king[fish.index] = true;
}

void solve_test_case(const TestCase& tc)
{
  auto shoals = make_shoals(tc);
  vector<fish_size_t> size_of_all_fish_to(shoals.size() + 1);
  size_of_all_fish_to[0] = 0;
  for (size_t i = 0; i < shoals.size(); i++)
    size_of_all_fish_to[i + 1] =
        size_of_all_fish_to[i] +
        static_cast<fish_size_t>(shoals[i].size) * shoals[i].fish_size;

  vector<bool> can_become_king(tc.fish_count, false);

  vector<fish_size_t> eaten_fish(shoals.size(), 0);

  for (size_t i = 1; i < shoals.size(); i++)
  {
    if (eaten_fish[i - 1] > i)
    {
      eaten_fish[i] = eaten_fish[i - 1];
      continue;
    }
    fish_size_t size = size_of_all_fish_to[i + 1];
    size_t j = i;
    while (j + 1 < shoals.size() and size > shoals[j + 1].fish_size)
    {
      size += shoals[j + 1].fish_size * shoals[j + 1].size;
      j++;
    }
    eaten_fish[i] = j + 1;
  }

  if (tc.fish_count == 1) can_become_king[0] = true;

  for (size_t i = 0; i < shoals.size(); i++)
  {
    auto size = size_of_all_fish_to[eaten_fish[i]];
    if (size > shoals[shoals.size() - 1].fish_size)
      mark_shoal_true(shoals[i], can_become_king);
  }

  for (auto b : can_become_king) cout << (b ? "T" : "N");
  cout << "\n";
}