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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 2000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

typedef pair<P, vector<int>> P2;

vector<int> emp;

int main() {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;

    vector<P2> DP[152];
    rep1(i, n) {
      int k;
      scanf("%d", &k);
      rep(j, k) {
        int x, y;
        scanf("%d%d", &x, &y);
        DP[i].pb(P2(P(x, y), emp));
      }
    }

    rep1(i, n) {
      rep(j, DP[i].size()) {
        rep(k, m + 1) {
          int dp = INF;
          if (i == 1) {
            dp = 0;
          } else if (i == 2 && k > 0) {
            dp = 0;
          } else {
            rep(l, DP[i - 1].size()) {
              dp = min(dp, DP[i - 1][l].sc[k] +
                               (DP[i][j].fr.sc + DP[i - 1][l].fr.sc) *
                                   abs(DP[i][j].fr.fr - DP[i - 1][l].fr.fr));
            }
            if (k > 0) {
              rep(l, DP[i - 2].size()) {
                dp = min(dp, DP[i - 2][l].sc[k - 1] +
                                 (DP[i][j].fr.sc + DP[i - 2][l].fr.sc) *
                                     abs(DP[i][j].fr.fr - DP[i - 2][l].fr.fr));
              }
            }
          }
          DP[i][j].sc.pb(dp);
        }
      }
    }

    int ret = INF;

    rep(i, DP[n].size()) {
      rep(j, m + 1) { ret = min(ret, DP[n][i].sc[j]); }
    }
    if (n > 1) {
      rep(i, DP[n - 1].size()) {
        rep(j, m) { ret = min(ret, DP[n - 1][i].sc[j]); }
      }
    }

    printf("%d\\n", ret);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int z(int x) {
  if (x < 0)
    return -x;
  return x;
}

int main() {
  const long long int INF = 1000000000000000000;
  long long int n, m, k;
  static long long int a[1002], b[152][1002], dp[152][1002][102];
  while (1) {
    for (int i = 0; i < 152; i++)
      for (int j = 0; j < 1002; j++)
        for (k = 0; k < 102; k++)
          dp[i][j][k] = INF;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        scanf("%d", &a[j]);
        scanf("%d", &b[i][a[j]]);
        for (int c = 0; c <= m; c++) {
          if (i == 1)
            dp[i][a[j]][c] = 0;
          else {
            for (int l = 0; l < 1002; l++) {
              if (dp[i - 1][l][c] != INF) {
                dp[i][a[j]][c] = min(
                    dp[i][a[j]][c],
                    dp[i - 1][l][c] + z(l - a[j]) * (b[i][a[j]] + b[i - 1][l]));
              }
              if (c != 0) {
                if (dp[i - 2][l][c - 1] != INF) {
                  dp[i][a[j]][c] =
                      min(dp[i][a[j]][c],
                          dp[i - 2][l][c - 1] +
                              z(l - a[j]) * (b[i][a[j]] + b[i - 2][l]));
                }
              }
            }
          }
        }
      }
    }
    long long int ret = INF;
    for (int i = 0; i < 1002; i++)
      for (int j = 0; j <= m; j++)
        ret = min(ret, dp[n][i][j]);
    printf("%d\\n", ret);
  }
}