Problem p02610 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define int long long

using namespace std;

#ifdef LOCAL
#include "/media/brayand/BrayanD/debugger.h"
#else
template <typename... Args> void db(Args... args) {}
#endif

int32_t main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(0);

  int T;
  cin >> T;

  while (T--) {
    int N;
    cin >> N;

    vector<pair<int, pair<int, int>>> v1;
    vector<pair<int, pair<int, int>>> v2;

    for (int i = 0; i < N; i++) {
      int K, L, R;
      cin >> K >> L >> R;
      if (L > R)
        v1.push_back({K, {L - R, R}});
      else
        v2.push_back({K, {R - L, L}});
    }

    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    reverse(v2.begin(), v2.end());

    int res = 0;

    int sz1 = v1.size();
    int sz2 = v2.size();

    multiset<pair<int, int>> st1, st2;

    for (int i = sz1; i >= 1; i--) {
      while (v1.size() && v1.back().first >= i) {
        st1.insert({v1.back().second.first, v1.back().second.second});
        v1.pop_back();
      }

      if (st1.size()) {
        pair<int, int> ppp = *(--st1.end());

        res += ppp.first + ppp.second;

        st1.erase(--st1.end());
      }
    }

    for (auto x : v1) {
      res += x.second.second;
    }
    for (auto x : st1) {
      res += x.second;
    }

    for (int i = sz1 + 1; i <= N; i++) {
      while (v2.size() && v2.back().first < i) {
        st2.insert({v2.back().second.first, v2.back().second.second});
        v2.pop_back();
      }

      if (st2.size()) {
        pair<int, int> ppp = *(--st2.end());

        res += ppp.first + ppp.second;

        st2.erase(--st2.end());
      }
    }

    for (auto x : v2) {
      res += x.second.second;
    }
    for (auto x : st2) {
      res += x.second;
    }

    cout << res << '\\n';
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long

using namespace std;

#ifdef LOCAL
#include "/media/brayand/BrayanD/debugger.h"
#else
template <typename... Args> void db(Args... args) {}
#endif

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int T;
  cin >> T;

  while (T--) {
    int N;
    cin >> N;

    vector<pair<int, pair<int, int>>> v1;
    vector<pair<int, pair<int, int>>> v2;

    for (int i = 0; i < N; i++) {
      int K, L, R;
      cin >> K >> L >> R;
      if (L > R)
        v1.push_back({K, {L - R, R}});
      else
        v2.push_back({K, {R - L, L}});
    }

    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    reverse(v2.begin(), v2.end());

    int res = 0;

    int sz1 = v1.size();
    int sz2 = v2.size();

    set<pair<int, int>> st1, st2;

    for (int i = 1; i <= sz1; i--) {
      while (v1.size() && v1.back().first <= i) {
        st1.insert({v1.back().second.first, v1.back().second.second});
        v1.pop_back();
      }

      if (st1.size()) {
        pair<int, int> ppp = *(--st1.end());

        res += ppp.first + ppp.second;
      }
    }

    for (auto x : v1) {
      res += x.second.second;
    }
    for (auto x : st1) {
      res += x.second;
    }

    for (int i = N; i >= sz1 + 1; i--) {
      while (v2.size() && v2.back().first > i) {
        st2.insert({v2.back().second.first, v2.back().second.second});
        v2.pop_back();
      }

      if (st2.size()) {
        pair<int, int> ppp = *(--st2.end());

        res += ppp.first + ppp.second;
      }
    }

    for (auto x : v2) {
      res += x.second.second;
    }
    for (auto x : st2) {
      res += x.second;
    }

    cout << res << '\\n';
  }

  return 0;
}