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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define INF 1145141919

int N, M;
int W;
int T[160][1010];
int dp[160][1010][80];

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < 160; i++) {
      for (int j = 0; j < 1010; j++) {
        T[i][j] = -1;
      }
    }
    W = 0;
    for (int i = 0; i < N; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x, d;
        cin >> x >> d;
        T[i][x - 1] = d;
        W = max(W, x);
      }
    }

    for (int i = 0; i < 160; i++) {
      for (int j = 0; j < 1010; j++) {
        for (int m = 0; m < 80; m++) {
          dp[i][j][m] = INF;
        }
      }
    }
    for (int i = 0; i < W; i++) {
      if (T[0][i] > 0)
        dp[0][i][0] = 0;
      if (T[1][i] > 0)
        dp[1][i][1] = 0;
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < W; j++) {
        for (int m = 0; m <= M; m++) {
          if (dp[i][j][m] == INF)
            continue;
          for (int nj = 0; nj < W; nj++) {
            if (T[i + 1][nj] > 0) {
              dp[i + 1][nj][m] =
                  min(dp[i + 1][nj][m],
                      dp[i][j][m] + abs(j - nj) * (T[i + 1][nj] + T[i][j]));
            }
            if (T[i + 2][nj] > 0) {
              dp[i + 2][nj][m + 1] =
                  min(dp[i + 2][nj][m + 1],
                      dp[i][j][m] + abs(j - nj) * (T[i + 2][nj] + T[i][j]));
            }
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < W; i++) {
      for (int m = 0; m <= M; m++) {
        if (m < M)
          ans = min(ans, dp[N - 2][i][m]);
        ans = min(ans, dp[N - 1][i][m]);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define INF 1145141919

int N, M;
int W;
int T[200][1010];
int dp[200][1010][100];

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < 200; i++) {
      for (int j = 0; j < 1010; j++) {
        T[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;
        T[i][x - 1] = d;
        W = max(W, x);
      }
    }

    for (int i = 0; i < 200; i++) {
      for (int j = 0; j < 1010; j++) {
        for (int m = 0; m < 100; m++) {
          dp[i][j][m] = INF;
        }
      }
    }
    for (int i = 0; i < W; i++) {
      if (T[0][i] > 0)
        dp[0][i][0] = 0;
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < W; j++) {
        for (int m = 0; m <= M; m++) {
          if (dp[i][j][m] == INF)
            continue;
          for (int nj = 0; nj < W; nj++) {
            if (T[i + 1][nj] > 0) {
              dp[i + 1][nj][m] =
                  min(dp[i + 1][nj][m],
                      dp[i][j][m] + abs(j - nj) * (T[i + 1][nj] + T[i][j]));
            }
            if (T[i + 2][nj] > 0) {
              dp[i + 2][nj][m + 1] =
                  min(dp[i + 2][nj][m],
                      dp[i][j][m] + abs(j - nj) * (T[i + 2][nj] + T[i][j]));
            }
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < W; i++) {
      for (int m = 0; m <= M; m++) {
        ans = min(ans, dp[N - 1][i][m]);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}