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

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3
#define mod 1000000007

int N, M, K, L;
vector<P> g[2010];
bool vis[2010];
bool iscycle[2010];
vector<int> vs[2010];
int mat[2010][2010];
vector<int> w[2010];

void dfs(int v, int pre) {
  vis[v] = true;
  vs[K].push_back(v);
  for (P e : g[v]) {
    if (e.fi == pre)
      continue;
    if (!vis[e.fi])
      dfs(e.fi, v);
  }
}

int main() {
  cin >> N >> M >> L;
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].push_back(P(b, c));
    g[b].push_back(P(a, c));
    mat[a][b] = mat[b][a] = c;
  }
  rep(i, N) { // path
    if (g[i].size() == 2 || vis[i])
      continue;
    dfs(i, -1);
    iscycle[K] = false;
    K++;
  }
  rep(i, N) { // cycle
    if (vis[i])
      continue;
    dfs(i, g[i][0].fi);
    iscycle[K] = true;
    K++;
  }

  rep(i, K) {
    int sz = vs[i].size();
    vector<vector<vector<vector<int>>>> dp(
        2, vector<vector<vector<int>>>(
               sz + 1, vector<vector<int>>(2, vector<int>(2, -INF))));
    int crt = 0, nxt = 1;
    dp[crt][0][0][0] = 0;
    dp[crt][1][1][1] = 0;
    repl(j, 1, sz) {
      rep(k, sz + 1) rep(f1, 2) rep(f2, 2) dp[nxt][k][f1][f2] = -INF;
      rep(k, sz + 1) {
        rep(f1, 2) {   // pre
          rep(f2, 2) { // start
            if (dp[crt][k][f1][f2] == -INF)
              continue;
            int con = (j == sz - 1 && f2 && iscycle[i] ? 1 : 0);
            if (f1) {
              if (k + 1 <= sz && mat[vs[i][j - 1]][vs[i][j]] != 0 &&
                  (con == 0 || (mat[vs[i][j]][vs[i][0]] != 0)))
                maxch(dp[nxt][k + 1][1][f2],
                      dp[crt][k][f1][f2] + mat[vs[i][j - 1]][vs[i][j]] +
                          (con ? mat[vs[i][j]][vs[i][0]] : 0));
              maxch(dp[nxt][k][0][f2], dp[crt][k][f1][f2]);
            } else {
              if (k + 1 <= sz && (con == 0 || (mat[vs[i][j]][vs[i][0]] != 0)))
                maxch(dp[nxt][k + 1][1][f2],
                      dp[crt][k][f1][f2] + (con ? mat[vs[i][j]][vs[i][0]] : 0));
              maxch(dp[nxt][k][0][f2], dp[crt][k][f1][f2]);
            }
          }
        }
      }
      swap(crt, nxt);
    }
    rep(k, sz + 1) w[i].push_back(max({dp[crt][k][0][0], dp[crt][k][0][1],
                                       dp[crt][k][1][0], dp[crt][k][1][1]}));
  }

  vector<vector<int>> dp(2, vector<int>(L + 1, -INF));
  int crt = 0, nxt = 1;
  dp[crt][0] = 0;
  rep(i, K) {
    rep(j, L + 1) dp[nxt][j] = -INF;
    rep(j, L + 1) {
      if (dp[crt][j] == -INF)
        continue;
      rep(k, w[i].size()) {
        if (j + k <= L)
          maxch(dp[nxt][j + k], dp[crt][j] + w[i][k]);
      }
    }
    swap(crt, nxt);
  }

  if (dp[crt][L] == -INF)
    cout << "Impossible" << endl;
  else
    cout << dp[crt][L] << endl;

  return 0;
}


Error: MLE

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3
#define mod 1000000007

int N, M, K, L;
vector<P> g[2010];
bool vis[2010];
bool iscycle[2010];
vector<int> vs[2010];
int mat[2010][2010];
vector<int> w[2010];

void dfs(int v, int pre) {
  vis[v] = true;
  vs[K].push_back(v);
  for (P e : g[v]) {
    if (e.fi == pre)
      continue;
    if (!vis[e.fi])
      dfs(e.fi, v);
  }
}

int main() {
  cin >> N >> M >> L;
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].push_back(P(b, c));
    g[b].push_back(P(a, c));
    mat[a][b] = mat[b][a] = c;
  }
  rep(i, N) { // path
    if (g[i].size() == 2 || vis[i])
      continue;
    dfs(i, -1);
    iscycle[K] = false;
    K++;
  }
  rep(i, N) { // cycle
    if (vis[i])
      continue;
    dfs(i, g[i][0].fi);
    iscycle[K] = true;
    K++;
  }

  rep(i, K) {
    int sz = vs[i].size();
    vector<vector<vector<vector<int>>>> dp(
        sz + 1, vector<vector<vector<int>>>(
                    sz + 1, vector<vector<int>>(2, vector<int>(2, -INF))));
    dp[1][0][0][0] = 0;
    dp[1][1][1][1] = 0;
    repl(j, 1, sz) {
      rep(k, sz + 1) {
        rep(f1, 2) {   // pre
          rep(f2, 2) { // start
            if (dp[j][k][f1][f2] == -INF)
              continue;
            int con = (j == sz - 1 && f2 && iscycle[i] ? 1 : 0);
            if (f1) {
              if (k + 1 <= sz && mat[vs[i][j - 1]][vs[i][j]] != 0 &&
                  (con == 0 || (mat[vs[i][j]][vs[i][0]] != 0)))
                maxch(dp[j + 1][k + 1][1][f2],
                      dp[j][k][f1][f2] + mat[vs[i][j - 1]][vs[i][j]] +
                          (con ? mat[vs[i][j]][vs[i][0]] : 0));
              maxch(dp[j + 1][k][0][f2], dp[j][k][f1][f2]);
            } else {
              if (k + 1 <= sz && (con == 0 || (mat[vs[i][j]][vs[i][0]] != 0)))
                maxch(dp[j + 1][k + 1][1][f2],
                      dp[j][k][f1][f2] + (con ? mat[vs[i][j]][vs[i][0]] : 0));
              maxch(dp[j + 1][k][0][f2], dp[j][k][f1][f2]);
            }
          }
        }
      }
    }
    rep(k, sz + 1) w[i].push_back(max(
        {dp[sz][k][0][0], dp[sz][k][0][1], dp[sz][k][1][0], dp[sz][k][1][1]}));
  }

  vector<vector<int>> dp(K + 1, vector<int>(L + 1, -INF));
  dp[0][0] = 0;
  rep(i, K) {
    rep(j, L + 1) {
      if (dp[i][j] == -INF)
        continue;
      rep(k, w[i].size()) {
        if (j + k <= L)
          maxch(dp[i + 1][j + k], dp[i][j] + w[i][k]);
      }
    }
  }

  if (dp[K][L] == -INF)
    cout << "Impossible" << endl;
  else
    cout << dp[K][L] << endl;

  return 0;
}