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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const ll INF = 100000000000000LL;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    static ll dp[150][10][77] = {};
    REP(i, 150) REP(j, 10) REP(k, 77) dp[i][j][k] = INF;
    int k[151], x[151][151], s[151][151];
    REP(i, n) {
      cin >> k[i];
      REP(j, k[i]) cin >> x[i][j] >> s[i][j];
    }
    REP(i, k[0]) dp[0][i][0] = 0;
    REP(i, k[1]) dp[1][i][1] = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < k[i]; j++) {
        int from_x = x[i][j];
        for (int l = 0; l < k[i + 1]; l++) {
          int to_x = x[i + 1][l];
          for (int use = 0; use <= m; use++) {
            dp[i + 1][l][use] =
                min(dp[i + 1][l][use], dp[i][j][use] + (s[i][j] + s[i + 1][l]) *
                                                           abs(from_x - to_x));
          }
        }
        if (i + 2 < n) {
          for (int l = 0; l < k[i + 2]; l++) {
            int to_x = x[i + 2][l];
            for (int use = 0; use < m; use++) {
              dp[i + 2][l][use + 1] = min(
                  dp[i + 2][l][use + 1],
                  dp[i][j][use] + (s[i][j] + s[i + 2][l]) * abs(from_x - to_x));
            }
          }
        }
      }
    }
    ll ans = INF;
    REP(i, k[n - 1]) REP(j, m + 1) ans = min(ans, dp[n - 1][i][j]);
    REP(i, k[n - 2]) REP(j, m) ans = min(ans, dp[n - 2][i][j]);
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    static int dp[151][1001][80] = {};
    REP(i, 151) REP(j, 1001) REP(k, 80) dp[i][j][k] = INF;
    int k[151], x[150][151], s[151][151];
    REP(i, n) {
      cin >> k[i];
      REP(j, k[i]) cin >> x[i][j] >> s[i][j];
    }
    REP(i, k[0]) dp[0][x[0][i]][0] = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < k[i]; j++) {
        int from_x = x[i][j];
        for (int l = 0; l < k[i + 1]; l++) {
          int to_x = x[i + 1][l];
          for (int use = 0; use <= m; use++) {
            dp[i + 1][to_x][use] =
                min(dp[i + 1][to_x][use],
                    dp[i][from_x][use] +
                        (s[i][j] + s[i + 1][l]) * abs(from_x - to_x));
          }
        }
        if (i + 2 < n) {
          for (int l = 0; l < k[i + 2]; l++) {
            int to_x = x[i + 2][l];
            for (int use = 0; use < m; use++) {
              dp[i + 2][to_x][use + 1] =
                  min(dp[i + 2][to_x][use + 1],
                      dp[i][from_x][use] +
                          (s[i][j] + s[i + 2][l]) * abs(from_x - to_x));
            }
          }
        }
      }
    }
    int ans = INF;
    REP(i, k[n - 1]) REP(j, m + 1) ans = min(ans, dp[n - 1][x[n - 1][i]][j]);
    REP(i, k[n - 2]) REP(j, m) ans = min(ans, dp[n - 2][x[n - 2][i]][j]);
    cout << ans << endl;
  }

  return 0;
}