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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, m;
int danger[155][1010];
vector<int> X[155];
ll dp[155][11][80];

const ll inf = 1LL << 55;

ll solve(int col, int row, int jump) {
  if (col >= n - 1)
    return 0;
  if (col >= n - 2 && jump < m)
    return 0;
  if (jump > m)
    return inf;
  if (~dp[col][row][jump])
    return dp[col][row][jump];
  ll ret = inf;
  for (int i = 0; i < X[col + 1].size(); i++) {
    ll D = (danger[col][X[col][row]] + danger[col + 1][X[col + 1][i]]) *
           abs(X[col][row] - X[col + 1][i]);
    ret = min(ret, solve(col + 1, i, jump) + D);
  }
  if (jump < m && col < n - 2) {
    for (int i = 0; i < X[col + 2].size(); i++) {
      ll D = (danger[col][X[col][row]] + danger[col + 2][X[col + 2][i]]) *
             abs(X[col][row] - X[col + 2][i]);
      ret = min(ret, solve(col + 2, i, jump + 1) + D);
    }
  }
  return dp[col][row][jump] = ret;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 155; i++)
      X[i].clear();
    for (int i = 0; i < 155; i++) {
      for (int j = 0; j < 11; j++)
        danger[i][j] = -1;
    }
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x, d;
        cin >> x >> d;
        --x;
        danger[i][x] = d;
        X[i].push_back(x);
      }
    }

    memset(dp, -1, sizeof(dp));
    ll ans = inf;
    for (int i = 0; i < X[0].size(); i++) {
      ans = min(ans, solve(0, i, 0));
    }
    if (m > 0 && n > 1) {
      for (int i = 0; i < X[1].size(); i++) {
        ans = min(ans, solve(1, i, 1));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, m;
int danger[155][1010];
vector<int> X[155];
ll dp[155][1010][80];

const ll inf = 1LL << 55;

ll solve(int col, int row, int jump) {
  if (col >= n - 1)
    return 0;
  if (col >= n - 2 && jump < m)
    return 0;
  if (jump > m)
    return inf;
  if (~dp[col][row][jump])
    return dp[col][row][jump];
  ll ret = inf;
  for (int i = 0; i < X[col + 1].size(); i++) {
    if (danger[col + 1][X[col + 1][i]] == inf)
      continue;
    ll D = (danger[col][row] + danger[col + 1][X[col + 1][i]]) *
           abs(row - X[col + 1][i]);
    ret = min(ret, solve(col + 1, X[col + 1][i], jump) + D);
  }
  if (jump < m && col < n - 2) {
    for (int i = 0; i < X[col + 2].size(); i++) {
      if (danger[col + 2][X[col + 2][i]] == inf)
        continue;
      ll D = (danger[col][row] + danger[col + 2][X[col + 2][i]]) *
             abs(row - X[col + 2][i]);
      ret = min(ret, solve(col + 2, X[col + 2][i], jump + 1) + D);
    }
  }
  return dp[col][row][jump] = ret;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 155; i++)
      X[i].clear();
    for (int i = 0; i < 155; i++) {
      for (int j = 0; j < 11; j++)
        danger[i][j] = -1;
    }
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x, d;
        cin >> x >> d;
        --x;
        danger[i][x] = d;
        X[i].push_back(x);
      }
    }

    memset(dp, -1, sizeof(dp));
    ll ans = inf;
    for (int i = 0; i < X[0].size(); i++) {
      if (danger[0][X[0][i]] == inf)
        continue;
      ans = min(ans, solve(0, X[0][i], 0));
    }
    if (m > 0 && n > 1) {
      for (int i = 0; i < X[1].size(); i++) {
        if (danger[1][X[1][i]] == inf)
          continue;
        ans = min(ans, solve(1, X[1][i], 1));
      }
    }
    cout << ans << endl;
  }
  return 0;
}