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

#include <bits/stdc++.h>
using namespace std;

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int T, N, k;
  long long int total;
  cin >> T;
  for (int t = 0; t < T; t++) {
    total = 0;
    cin >> N;
    long long int l, r;
    vector<long long int> Ph[N], Pt[N];
    priority_queue<long long int, vector<long long int>, greater<long long int>>
        H;
    priority_queue<long long int, vector<long long int>, greater<long long int>>
        T;
    for (int n = 0; n < N; n++) {
      cin >> k >> l >> r;
      k = k - 1;
      if (l > r) {
        Ph[k].push_back(l - r);
        total = total + r;
      } else {
        Pt[k].push_back(r - l);
        total = total + l;
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j : Ph[i]) {
        H.push(j);
      }
      while (H.size() > i + 1) {
        H.pop();
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j : Pt[N - 1 - i]) {
        T.push(j);
      }
      while (T.size() > i) {
        T.pop();
      }
    }
    while (!H.empty()) {
      total = total + H.top();
      // cout << H.top() <<endl;
      H.pop();
    }
    while (!T.empty()) {
      total = total + T.top();
      // cout << T.top() <<endl;
      T.pop();
    }
    cout << total << endl;
  }

  return 0;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int T, N;
  long long int total;
  cin >> T;
  for (int t = 0; t < T; t++) {
    total = 0;
    cin >> N;
    long long int K[N][3];
    priority_queue<long long int, vector<long long int>, greater<long long int>>
        H;
    priority_queue<long long int, vector<long long int>, greater<long long int>>
        T;
    for (int n = 0; n < N; n++) {
      cin >> K[n][0] >> K[n][1] >> K[n][2];
      K[n][0] = K[n][0] - 1;
      total = total + min(K[n][1], K[n][2]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[j][0] == i && K[j][1] > K[j][2]) {
          H.push(K[j][1] - K[j][2]);
        }
      }
      while (H.size() > i + 1) {
        H.pop();
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[j][0] == N - 1 - i && K[j][1] < K[j][2]) {
          T.push(K[j][2] - K[j][1]);
        }
      }
      while (T.size() > i) {
        T.pop();
      }
    }
    while (!H.empty()) {
      total = total + H.top();
      // cout << H.top() <<endl;
      H.pop();
    }
    while (!T.empty()) {
      total = total + T.top();
      // cout << T.top() <<endl;
      T.pop();
    }
    cout << total << endl;
  }

  return 0;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}