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

#include <bits/stdc++.h>
#define LL long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
static const LL INF = 1LL << 60LL;
typedef pair<LL, LL> PLL;

LL N, M;
LL ans = INF;
LL V[160][15];
LL K[160][15];
LL mem[160][15][80][3];

LL cal(int y, int x, int m, int f) {
  LL res = INF;
  if (mem[y][x][m][f] != -1) {
    return mem[y][x][m][f];
  }
  if (y == N - 1 && f == 2)
    return 0;
  else if (y == N && f == 1)
    return 0;
  rep(i, 10) {
    LL d1, d2;
    if (m > 0 && K[y + 1][i] && K[y + 2][i] && y + 2 <= N) {
      d1 = (K[y][x] + K[y + 1][i]) * abs(V[y][x] - V[y + 1][i]);
      d2 = (K[y][x] + K[y + 2][i]) * abs(V[y][x] - V[y + 2][i]);
      res =
          min(cal(y + 1, i, m, f) + d1, min(cal(y + 2, i, m - 1, f) + d2, res));
    } else if (K[y + 1][i]) {
      d1 = (K[y][x] + K[y + 1][i]) * abs(V[y][x] - V[y + 1][i]);
      res = min(cal(y + 1, i, m, f) + d1, res);
    } else if (K[y + 2][i] && m > 0 && y + 2 <= N) {
      d2 = (K[y][x] + K[y + 2][i]) * abs(V[y][x] - V[y + 2][i]);
      res = min(cal(y + 2, i, m - 1, f) + d2, res);
    }
  }
  return mem[y][x][m][f] = res;
}

int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    memset(mem, -1, sizeof(mem));
    memset(V, 0, sizeof(V));
    memset(K, 0, sizeof(K));
    FOR(i, 1, N) {
      LL t;
      cin >> t;
      rep(j, t) {
        LL x, k;
        cin >> x >> k;
        V[i][j] = x;
        K[i][j] = k;
      }
    }
    rep(j, 10) {
      if (K[1][j] && K[2][j] && M > 0) {
        ans = min(cal(1, j, M, 1), min(cal(2, j, M - 1, 2), ans));
      } else if (K[1][j]) {
        ans = min(cal(1, j, M, 1), ans);
      } else if (M > 0)
        ans = min(cal(2, j, M - 1, 2), ans);
    }
    cout << ans << endl;
    ans = INF;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<LL, LL> PLL;

LL N, M;
LL ans;
LL V[160][1010];
LL mem[160][1010][80];

LL cal(int y, int x, int m) {
  LL res = INF;
  if (mem[y][x][m] != -1)
    return mem[y][x][m];
  if (y >= N)
    return 0;
  if (y == 0) {
    FOR(i, 1, 1000) {
      if (V[y + 1][i] != INF)
        res = cal(y + 1, i, m);
    }
  } else {
    if (V[y][x] != INF) {
      FOR(i, 1, 1000) {
        if (m > 0 && V[y + 1][i] != INF && V[y + 2][i] != INF) {
          LL d1 = (V[y][x] + V[y + 1][i]) * abs(x - i);
          LL d2 = (V[y][x] + V[y + 2][i]) * abs(x - i);
          res = min(cal(y + 1, i, m) + d1, min(cal(y + 2, i, m - 1) + d2, res));
        } else if (V[y + 1][i] != INF && V[y + 2][i] == INF) {
          LL d1 = (V[y][x] + V[y + 1][i]) * abs(x - i);
          res = min(cal(y + 1, i, m) + d1, res);
        } else if (V[y + 1][i] == INF && V[y + 2][i] != INF && m > 0) {
          LL d2 = (V[y][x] + V[y + 2][i]) * abs(x - i);
          res = min(cal(y + 2, i, m) + d2, res);
        }
      }
    }
  }
  return mem[y][x][m] = res;
}

int main() {
  cin >> N >> M;
  memset(mem, -1, sizeof(mem));
  rep(i, 151) rep(j, 1001) V[i][j] = INF;
  FOR(i, 1, N) {
    LL t;
    cin >> t;
    rep(j, t) {
      LL x, k;
      cin >> x >> k;
      V[i][x] = k;
    }
  }
  ans = cal(0, 0, M);
  cout << ans << endl;
}