Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MaxN = 3e2 + 3;
const int INF = 1e9;

int N[5];
int cur[5];
int n;

set<pair<int, int>, greater<pair<int, int>>> s[5][5];
int a[MaxN][5];

int main() {
  //    freopen("input.txt", "r", stdin);
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N[1] >> N[2] >> N[3];
  n = N[1] + N[2] + N[3];

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 3; ++j)
      cin >> a[i][j];
  }

  long long ans = 0;

  for (int i = 1; i <= n; ++i) {
    ans += a[i][1];
    ++cur[1];
    s[1][2].insert(make_pair(a[i][2] - a[i][1], i));
    s[1][3].insert(make_pair(a[i][3] - a[i][1], i));
  }
  // cout << ans << '\n';
  while (cur[2] < N[2] && cur[3] < N[3]) {
    int p12 = -INF - INF;
    int p13 = -INF - INF;
    if (!s[1][2].empty())
      p12 = (*s[1][2].begin()).first;
    if (!s[1][3].empty())
      p13 = (*s[1][3].begin()).first;
    if (p12 > p13) {
      pair<int, int> curp = (*s[1][2].begin());
      // cout << "YO " << p12 << ' ' << curp.second << '\n';
      s[1][2].erase(s[1][2].begin());
      s[1][3].erase(
          make_pair(a[curp.second][3] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;
    } else {
      pair<int, int> curp = (*s[1][3].begin());
      // cout << "YE " << p13 << ' ' << curp.second << '\n';
      s[1][3].erase(s[1][3].begin());
      s[1][2].erase(
          make_pair(a[curp.second][2] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;
    }
  }

  while (cur[2] < N[2]) {
    int p12 = (*s[1][2].begin()).first;
    int p32 = (*s[1][3].begin()).first + (*s[3][2].begin()).first;
    if (p12 > p32) {

      pair<int, int> curp = (*s[1][2].begin());
      // cout << p12 << "  " << curp.second << '\n';
      s[1][2].erase(s[1][2].begin());
      s[1][3].erase(
          make_pair(a[curp.second][3] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;
    } else {
      // cout << p32 << " B" << '\n';
      pair<int, int> curp = (*s[3][2].begin());
      // cout << p32 << "  " << curp.second << '\n';
      s[3][2].erase(s[3][2].begin());
      --cur[3];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;

      curp = (*s[1][3].begin());
      // cout << p32 << "  " << curp.second << '\n';
      s[1][3].erase(s[1][3].begin());
      s[1][2].erase(
          make_pair(a[curp.second][2] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;
    }
  }
  while (cur[3] < N[3]) {
    int p13 = (*s[1][3].begin()).first;
    int p23 = (*s[1][2].begin()).first + (*s[2][3].begin()).first;
    if (p13 > p23) {
      pair<int, int> curp = (*s[1][3].begin());
      s[1][3].erase(s[1][3].begin());
      s[1][2].erase(
          make_pair(a[curp.second][2] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;
    } else {
      pair<int, int> curp = (*s[2][3].begin());
      s[2][3].erase(s[2][3].begin());
      --cur[2];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;

      curp = (*s[1][2].begin());
      s[1][2].erase(s[1][2].begin());
      s[1][3].erase(
          make_pair(a[curp.second][3] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;
    }
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MaxN = 3e5 + 3;
const int INF = 1e9;

int N[5];
int cur[5];
int n;

set<pair<int, int>, greater<pair<int, int>>> s[5][5];
int a[MaxN][5];

int main() {
  //    freopen("input.txt", "r", stdin);
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N[1] >> N[2] >> N[3];
  n = N[1] + N[2] + N[3];

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 3; ++j)
      cin >> a[i][j];
  }

  long long ans = 0;

  for (int i = 1; i <= n; ++i) {
    ans += a[i][1];
    ++cur[1];
    s[1][2].insert(make_pair(a[i][2] - a[i][1], i));
    s[1][3].insert(make_pair(a[i][3] - a[i][1], i));
  }
  // cout << ans << '\n';
  while (cur[2] < N[2] && cur[3] < N[3]) {
    int p12 = -INF - INF;
    int p13 = -INF - INF;
    if (!s[1][2].empty())
      p12 = (*s[1][2].begin()).first;
    if (!s[1][3].empty())
      p13 = (*s[1][3].begin()).first;
    if (p12 > p13) {
      pair<int, int> curp = (*s[1][2].begin());
      // cout << "YO " << p12 << ' ' << curp.second << '\n';
      s[1][2].erase(s[1][2].begin());
      s[1][3].erase(
          make_pair(a[curp.second][3] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;
    } else {
      pair<int, int> curp = (*s[1][3].begin());
      // cout << "YE " << p13 << ' ' << curp.second << '\n';
      s[1][3].erase(s[1][3].begin());
      s[1][2].erase(
          make_pair(a[curp.second][2] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;
    }
  }

  while (cur[2] < N[2]) {
    int p12 = (*s[1][2].begin()).first;
    int p32 = (*s[1][3].begin()).first + (*s[3][2].begin()).first;
    if (p12 > p32) {

      pair<int, int> curp = (*s[1][2].begin());
      // cout << p12 << "  " << curp.second << '\n';
      s[1][2].erase(s[1][2].begin());
      s[1][3].erase(
          make_pair(a[curp.second][3] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;
    } else {
      // cout << p32 << " B" << '\n';
      pair<int, int> curp = (*s[3][2].begin());
      // cout << p32 << "  " << curp.second << '\n';
      s[3][2].erase(s[3][2].begin());
      --cur[3];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;

      curp = (*s[1][3].begin());
      // cout << p32 << "  " << curp.second << '\n';
      s[1][3].erase(s[1][3].begin());
      s[1][2].erase(
          make_pair(a[curp.second][2] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;
    }
  }
  while (cur[3] < N[3]) {
    int p13 = (*s[1][3].begin()).first;
    int p23 = (*s[1][2].begin()).first + (*s[2][3].begin()).first;
    if (p13 > p23) {
      pair<int, int> curp = (*s[1][3].begin());
      s[1][3].erase(s[1][3].begin());
      s[1][2].erase(
          make_pair(a[curp.second][2] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;
    } else {
      pair<int, int> curp = (*s[2][3].begin());
      s[2][3].erase(s[2][3].begin());
      --cur[2];
      ++cur[3];
      s[3][2].insert(
          make_pair(a[curp.second][2] - a[curp.second][3], curp.second));
      ans += curp.first;

      curp = (*s[1][2].begin());
      s[1][2].erase(s[1][2].begin());
      s[1][3].erase(
          make_pair(a[curp.second][3] - a[curp.second][1], curp.second));
      --cur[1];
      ++cur[2];
      s[2][3].insert(
          make_pair(a[curp.second][3] - a[curp.second][2], curp.second));
      ans += curp.first;
    }
  }
  cout << ans << '\n';
  return 0;
}