Problem p01407 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct MinimumCostFlow {
  using Flow = int;
  using Cost = int;
  const Cost kInfCost = INF;
  struct Edge {
    int to, rev;
    Flow cap;
    Cost cost;
    Edge() {}
    Edge(int to, int rev, Flow cap, Cost cost)
        : to(to), rev(rev), cap(cap), cost(cost) {}
  };
  int n;
  vector<vector<Edge>> g;
  vector<Cost> dist;
  vector<int> prevv, preve;
  MinimumCostFlow(int n) : n(n), g(n), dist(n), prevv(n), preve(n) {}
  void addArc(int from, int to, Flow cap, Cost cost) {
    g[from].emplace_back(to, (int)g[to].size(), cap, cost);
    g[to].emplace_back(from, (int)g[from].size() - 1, Flow(), -cost);
  }
  // s??????t????????????f???????°??????¨???
  // ??????????????´?????? kInfCost
  Cost minimumCostFlow(int s, int t, Flow f) {
    Cost total = Cost();
    while (f > 0) {
      // Bellman-Ford
      fill(dist.begin(), dist.end(), kInfCost);
      dist[s] = 0;
      bool update = true;
      while (update) {
        update = false;
        for (int v = 0; v < n; v++) {
          if (dist[v] == kInfCost)
            continue;
          for (int i = 0; i < g[v].size(); i++) {
            Edge &e = g[v][i];
            if (e.cap > Flow() && 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 (dist[t] == kInfCost)
        return kInfCost;
      // ?????????????????£??????????????????
      Flow d = f;
      for (int v = t; v != s; v = prevv[v])
        d = min(d, g[prevv[v]][preve[v]].cap);
      f -= d;
      total += dist[t] * d;
      for (int v = t; v != s; v = prevv[v]) {
        Edge &e = g[prevv[v]][preve[v]];
        e.cap -= d;
        g[v][e.rev].cap += d;
      }
    }
    return total;
  }
};

template <class T, class... Tail>
void tiedSort(vector<T> &a, vector<Tail> &...tail) {
  int n = a.size();
  using S = tuple<T, Tail...>;
  vector<S> s(n);
  for (int i = 0; i < n; i++)
    s[i] = make_tuple(a[i], tail[i]...);
  sort(s.begin(), s.end());
  for (int i = 0; i < n; i++)
    tie(a[i], tail[i]...) = s[i];
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, V;
  cin >> N >> V;
  int L, R;
  cin >> L >> R;
  vector<int> X(N), T(N), P(N);
  rep(i, 0, N) { cin >> X[i] >> T[i] >> P[i]; }
  tiedSort(T, X, P);
  MinimumCostFlow mcf(N + N + 4);
  int s = mcf.n - 4, t = s + 1, l = t + 1, r = l + 1;

  mcf.addArc(s, l, 1, 0);
  mcf.addArc(s, r, 1, 0);
  rep(i, 0, N) {
    if (abs(X[i] - L) <= T[i] * V)
      mcf.addArc(l, i, 1, 0);
    if (abs(X[i] - R) <= T[i] * V)
      mcf.addArc(r, i, 1, 0);
    mcf.addArc(i, i + N, 1, -P[i]);
    mcf.addArc(i, i + N, 1, 0);
    mcf.addArc(i + N, t, 2, 0);
  }
  vector<vector<bool>> exists(N, vector<bool>(N));
  rep(i, 0, N) {
    rep(j, i + 1, N) {
      if (abs(X[j] - X[i]) <= (T[j] - T[i]) * V) {
        exists[i][j] = true;
      }
    }
  }
  rep(i, 0, N) {
    rrep(j, i + 2, N) {
      if (!exists[i][j])
        continue;
      rep(k, i + 1, j) {
        if (exists[i][k] && exists[k][j]) {
          exists[i][j] = false;
          break;
        }
      }
    }
  }

  rep(i, 0, N) {
    rep(j, i + 1, N) {
      if (exists[i][j])
        mcf.addArc(i + N, j, 2, 0);
    }
  }

  mcf.addArc(s, t, 2, 0);
  cout << -mcf.minimumCostFlow(s, t, 2) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct MinimumCostFlow {
  using Flow = int;
  using Cost = int;
  const Cost kInfCost = INF;
  struct Edge {
    int to, rev;
    Flow cap;
    Cost cost;
    Edge() {}
    Edge(int to, int rev, Flow cap, Cost cost)
        : to(to), rev(rev), cap(cap), cost(cost) {}
  };
  int n;
  vector<vector<Edge>> g;
  vector<Cost> dist;
  vector<int> prevv, preve;
  MinimumCostFlow(int n) : n(n), g(n), dist(n), prevv(n), preve(n) {}
  void addArc(int from, int to, Flow cap, Cost cost) {
    g[from].emplace_back(to, (int)g[to].size(), cap, cost);
    g[to].emplace_back(from, (int)g[from].size() - 1, Flow(), -cost);
  }
  // s??????t????????????f???????°??????¨???
  // ??????????????´?????? kInfCost
  Cost minimumCostFlow(int s, int t, Flow f) {
    Cost total = Cost();
    while (f > 0) {
      // Bellman-Ford
      fill(dist.begin(), dist.end(), kInfCost);
      dist[s] = 0;
      bool update = true;
      while (update) {
        update = false;
        for (int v = 0; v < n; v++) {
          if (dist[v] == kInfCost)
            continue;
          for (int i = 0; i < g[v].size(); i++) {
            Edge &e = g[v][i];
            if (e.cap > Flow() && 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 (dist[t] == kInfCost)
        return kInfCost;
      // ?????????????????£??????????????????
      Flow d = f;
      for (int v = t; v != s; v = prevv[v])
        d = min(d, g[prevv[v]][preve[v]].cap);
      f -= d;
      total += dist[t] * d;
      for (int v = t; v != s; v = prevv[v]) {
        Edge &e = g[prevv[v]][preve[v]];
        e.cap -= d;
        g[v][e.rev].cap += d;
      }
    }
    return total;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, V;
  cin >> N >> V;
  int L, R;
  cin >> L >> R;
  vector<int> X(N), T(N), P(N);
  rep(i, 0, N) { cin >> X[i] >> T[i] >> P[i]; }

  MinimumCostFlow mcf(N + N + 4);
  int s = mcf.n - 4, t = s + 1, l = t + 1, r = l + 1;

  mcf.addArc(s, l, 1, 0);
  mcf.addArc(s, r, 1, 0);
  rep(i, 0, N) {
    if (abs(X[i] - L) <= T[i] * V)
      mcf.addArc(l, i, 1, 0);
    if (abs(X[i] - R) <= T[i] * V)
      mcf.addArc(r, i, 1, 0);
    mcf.addArc(i, i + N, 1, -P[i]);
    mcf.addArc(i + N, t, 2, 0);
  }
  rep(i, 0, N) {
    rep(j, 0, N) {
      if (i == j)
        continue;
      if (T[i] > T[j])
        continue;
      if (abs(X[j] - X[i]) <= (T[j] - T[i]) * V) {
        mcf.addArc(i + N, j, 2, 0);
      }
    }
  }
  mcf.addArc(s, t, 2, 0);
  cout << -mcf.minimumCostFlow(s, t, 2) << endl;
  return 0;
}