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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

ll calc(const vector<pair<int, int>> &v) {
  ll ret = 0;

  priority_queue<int> que;
  int j = 0;
  for (int i = v.size() - 1; i >= 0; i--) {
    while (j < v.size() && v[j].first >= i) {
      que.push(v[j].second);
      j++;
    }

    if (!que.empty()) {
      ret += que.top();
      que.pop();
    }
  }

  return ret;
}

void solve() {
  int N;
  cin >> N;
  vector<pair<int, int>> lhs, rhs;
  ll sum = 0;
  rep(i, N) {
    int k, l, r;
    cin >> k >> l >> r;
    k--;
    int mn = min(l, r);
    sum += mn;
    l -= mn;
    r -= mn;

    if (l > 0) {
      lhs.emplace_back(k, l);
    } else {
      rhs.emplace_back(N - 1 - (k + 1), r);
    }
  }

  sort(ALLOF(lhs));
  reverse(ALLOF(lhs));
  sort(ALLOF(rhs));
  reverse(ALLOF(rhs));

  sum += calc(lhs);
  sum += calc(rhs);

  cout << sum << endl;
}

int main() {
  int T;
  cin >> T;
  rep(t, T) { solve(); }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

template <class F, class C> class MinCostFlow {
  struct Edge {
    int rev, from, to;
    F cap, icap;
    C cost;
    Edge(int rev, int from, int to, F cap, C cost)
        : rev(rev), from(from), to(to), cap(cap), icap(cap), cost(cost) {}
  };

  int N;
  vector<vector<Edge>> G;
  const C INF;

public:
  MinCostFlow(int N) : N(N), G(N), INF(numeric_limits<C>::max()) {}

  void add_edge(int from, int to, F cap, C cost) {
    G[from].emplace_back((int)(G[to].size()), from, to, cap, cost);
    G[to].emplace_back((int)(G[from].size()) - 1, to, from, 0, -cost);
  }

  C solve(int s, int t, F init_f) {
    vector<C> dist(N);
    vector<int> prevv(N);
    vector<int> preve(N);

    C ret = 0;
    F f = init_f;
    while (f > 0) {
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      while (true) {
        bool update = false;
        for (int v = 0; v < G.size(); v++) {
          if (dist[v] == INF)
            continue;
          for (int i = 0; i < G[v].size(); i++) {
            Edge &e = G[v][i];
            if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
              dist[e.to] = dist[v] + e.cost;
              prevv[e.to] = v;
              preve[e.to] = i;
              update = true;
            }
          }
        }
        if (!update)
          break;
      }

      if (dist[t] == INF)
        return 0;

      F d = f;
      for (int v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      ret += dist[t] * d;
      for (int v = t; v != s; v = prevv[v]) {
        Edge &e = G[prevv[v]][preve[v]];
        Edge &re = (e.from != e.to) ? G[e.to][e.rev] : G[e.to][e.rev + 1];
        e.cap -= d;
        re.cap += d;
      }
    }
    return ret;
  }

  vector<int> get_selected_edges(int i) {
    vector<int> ret;
    for (int j = 0; j < G[i].size(); j++) {
      Edge &e = G[i][j];
      if (e.cap == 0 && e.icap == 1)
        ret.push_back(e.to);
    }
    return ret;
  }
};

class MaximumWeightedMatching {
  int N, M;
  int s, t;
  vector<int> x;
  MinCostFlow<ll, ll> mcf;

public:
  MaximumWeightedMatching(int N, int M)
      : N(N), M(M), x(N), mcf(N + M + 2), s(N + M), t(N + M + 1) {}

  void set_x(int i, int num) { x[i] = num; }

  void add_edge(int i, int j, ll gain) { mcf.add_edge(i, j, 1, -gain); }

  pair<ll, vector<pair<int, int>>> solve() {
    int sum = 0;
    for (int i = 0; i < N; i++) {
      mcf.add_edge(s, i, x[i], 0);
      sum += x[i];
    }
    for (int i = 0; i < M; i++) {
      mcf.add_edge(N + i, t, 1, 0);
    }

    ll score = -mcf.solve(s, t, sum);
    vector<pair<int, int>> selected_edges;
    for (int i = 0; i < N; i++) {
      vector<int> se = mcf.get_selected_edges(i);
      for (int j = 0; j < se.size(); j++) {
        selected_edges.emplace_back(i, se[j]);
      }
    }
    return make_pair(score, selected_edges);
  }
};

void solve(int N, const vector<vector<int>> &v) {
  MaximumWeightedMatching mwm(N, N);
  rep(i, N) { mwm.set_x(i, 1); }

  rep(i, N) {
    rep(j, N) {
      if (j <= v[i][0]) {
        mwm.add_edge(i, N + j, v[i][1]);
      } else {
        mwm.add_edge(i, N + j, v[i][2]);
      }
    }
  }

  pair<ll, vector<pair<int, int>>> ret = mwm.solve();

  cout << ret.first << endl;
}

int main() {
  int T;
  cin >> T;
  rep(t, T) {
    int N;
    cin >> N;
    vector<vector<int>> v;
    rep(i, N) {
      int k, l, r;
      cin >> k >> l >> r;
      k--;
      v.push_back({k, l, r});
    }

    solve(N, v);
  }

  return 0;
}