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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
int n, m;
int stone[155][11][2];
int dp[155][11][76];
int num[155];
int cost(int a, int b, int c) {
  if (a == n || (a == n - 1 && c < m)) {
    return 0;
  }
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  int mini = INF;
  for (int i = 1; i <= num[a + 1]; i++) {
    mini = min(mini, (stone[a + 1][i][1] + stone[a][b][1]) *
                             abs(stone[a][b][0] - stone[a + 1][i][0]) +
                         cost(a + 1, i, c));
  }
  if (c != m) {
    for (int i = 1; i <= num[a + 2]; i++) {
      mini = min(mini, (stone[a + 2][i][1] + stone[a][b][1]) *
                               abs(stone[a][b][0] - stone[a + 2][i][0]) +
                           cost(a + 2, i, 1 + c));
    }
  }
  return dp[a][b][c] = mini;
}
int main() {
  while (scanf("%d %d", &n, &m) && n) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &num[i]);
      for (int g = 1; g <= num[i]; g++) {
        scanf("%d %d", &stone[i][g][0], &stone[i][g][1]);
      }
    }
    memset(dp, -1, sizeof(dp));
    int ans = INF;
    for (int i = 1; i <= num[1]; i++) {
      ans = min(ans, cost(1, i, 0));
    }
    if (m > 0) {
      for (int i = 1; i <= num[2]; i++) {
        ans = min(ans, cost(2, i, 1));
      }
    }
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
int dp[155][1000][76] = {};
bool p(P a, P b) {
  if (a.first < b.first) {
    return true;
  } else {
    return false;
  }
}
int main() {
  int n, m;
  while (1) {
    vector<P> vec[155];
    scanf("%d %d", &n, &m);
    if (!n)
      break;
    for (int i = 1; i <= n; i++) {
      int a;
      scanf("%d", &a);
      for (int j = 0; j < a; j++) {
        int r, u;
        scanf("%d %d", &r, &u);
        vec[i].pb(mp(r, u));
      }
    }
    for (int i = 1; i <= n; i++) {
      sort(vec[i].begin(), vec[i].end(), p);
    }
    for (int i = 0; i <= 150; i++) {
      for (int j = 0; j < 1000; j++) {
        for (int k = 0; k < 76; k++) {
          dp[i][j][k] = INF;
        }
      }
    }
    int y = vec[1].size();
    for (int i = 0; i < y; i++) {
      dp[1][vec[1][i].first][0] = 0;
    }
    for (int i = 2; i <= n; i++) {
      int eee = vec[i].size();
      for (int j = 0; j < eee; j++) {
        for (int k = 0; k <= m; k++) {
          int r = vec[i - 1].size();
          for (int l = 0; l < r; l++) {
            dp[i][vec[i][j].first][k] =
                min(dp[i - 1][vec[i - 1][l].first][k] +
                        abs(vec[i][j].first - vec[i - 1][l].first) *
                            (vec[i - 1][l].second + vec[i][j].second),
                    dp[i][vec[i][j].first][k]);
          }
          if (k != 0 && i != 2) {
            int rr = vec[i - 2].size();
            for (int l = 0; l < rr; l++) {
              dp[i][vec[i][j].first][k] =
                  min(dp[i - 2][vec[i - 2][l].first][k - 1] +
                          abs(vec[i][j].first - vec[i - 2][l].first) *
                              (vec[i - 2][l].second + vec[i][j].second),
                      dp[i][vec[i][j].first][k]);
            }
          }
        }
      }
      int rp[10] = {};
      for (int k = 0; k <= m; k++) {
        int u = (int)vec[i].size();
        for (int j = 0; j < u; j++) {
          rp[j] = dp[i][vec[i][j].first][k];
        }
        sort(rp, rp + u);
        for (int l = 0; l < u; l++) {
          int q;
          for (int e = 0; e < u; e++) {
            if (rp[l] == dp[i][vec[i][e].first][k]) {
              q = e;
            }
          }
          for (int g = 0; g < u; g++) {
            if (q != g) {
              dp[i][vec[i][g].first][k] =
                  min(dp[i][vec[i][g].first][k],
                      dp[i][vec[i][q].first][k] +
                          abs(vec[i][g].first - vec[i][q].first) *
                              (vec[i][g].second + vec[i][q].second));
            }
          }
        }
      }
    }
    int res = INF;
    for (int i = 0; i < vec[n].size(); i++) {
      for (int k = 0; k <= m; k++) {
        res = min(res, dp[n][vec[n][i].first][k]);
      }
    }
    printf("%d\\n", res);
  }
}