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

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define MAX
using namespace std;

int n, m;
ll dp[155][80][10], INF = 1000000000000000LL;
vector<int> x[155], d[155];

void solve() {
  for (;;) {
    cin >> n >> m;
    if (!n && !m)
      break;
    int kk, xx, dd;
    FOR(i, n) {
      cin >> kk;
      FOR(j, kk) {
        cin >> xx >> dd;
        x[i + 1].pb(xx);
        d[i + 1].pb(dd);
      }
    }
    FOR(i, 155) FOR(j, 80) FOR(k, 10) dp[i][j][k] = INF;
    FOR(i, m + 1) FOR(j, x[n].size()) dp[n][i][j] = 0LL;
    for (int i = n - 1; i > 0; i--) {
      FOR(j, m + 1) {
        FOR(k, x[i].size()) {
          if (j) {
            if (i == n - 1)
              dp[i][j][k] = 0;
            else {
              FOR(l, x[i + 2].size()) {
                dp[i][j][k] =
                    min(dp[i][j][k],
                        dp[i + 2][j - 1][l] + (ll)(d[i][k] + d[i + 2][l]) *
                                                  abs(x[i][k] - x[i + 2][l]));
              }
            }
          }
          FOR(l, x[i + 1].size()) {
            dp[i][j][k] = min(dp[i][j][k],
                              dp[i + 1][j][l] + (ll)(d[i][k] + d[i + 1][l]) *
                                                    abs(x[i][k] - x[i + 1][l]));
          }
        }
      }
    }
    ll mi = INF;
    FOR(i, x[1].size()) mi = min(mi, dp[1][m][i]);
    if (m)
      FOR(i, x[2].size()) mi = min(mi, dp[2][m - 1][i]);
    cout << mi << endl;
    FOR(i, 155) {
      x[i].clear();
      d[i].clear();
    }
  }
}

int main() {
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define MAX
using namespace std;

int n, m;
ll dp[155][80][1010], INF = 1000000000000000LL;
vector<int> x[155], d[155];

void solve() {
  for (;;) {
    cin >> n >> m;
    if (!n)
      break;
    int kk, xx, dd;
    FOR(i, n) {
      cin >> kk;
      FOR(j, kk) {
        cin >> xx >> dd;
        x[i + 1].pb(xx);
        d[i + 1].pb(dd);
      }
    }
    FOR(i, 155) FOR(j, 80) FOR(k, 1010) dp[i][j][k] = INF;
    FOR(i, m + 1) FOR(j, x[n].size()) dp[n][i][x[n][j]] = 0LL;
    for (int i = n - 1; i >= 0; i--) {
      FOR(j, m + 1) {
        FOR(k, x[i].size()) {
          if (j) {
            if (i == n - 1)
              dp[i][j][x[i][k]] = 0;
            else {
              FOR(l, x[i + 2].size()) {
                dp[i][j][x[i][k]] =
                    min(dp[i][j][x[i][k]], dp[i + 2][j - 1][x[i + 2][l]] +
                                               (d[i][k] + d[i + 2][l]) *
                                                   abs(x[i][k] - x[i + 2][l]));
              }
            }
          }
          FOR(l, x[i + 1].size()) {
            dp[i][j][x[i][k]] =
                min(dp[i][j][x[i][k]],
                    dp[i + 1][j][x[i + 1][l]] +
                        (d[i][k] + d[i + 1][l]) * abs(x[i][k] - x[i + 1][l]));
          }
        }
      }
    }
    ll mi = INF;
    FOR(i, x[1].size()) mi = min(mi, dp[1][m][x[1][i]]);
    if (m)
      FOR(i, x[2].size()) mi = min(mi, dp[2][m - 1][x[2][i]]);
    cout << mi << endl;
  }
}

int main() {
  solve();
  return 0;
}