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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>

using namespace std;

#define int long long

typedef pair<int, int> pii;

#define fst first
#define scd second
#define rep(i, x) for (int i = 0; i < (x); ++i)

template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int inf = 1e9;

int k[150];
vector<pii> stone[150];
int N, M;

int memo[150][150][10];

int rec(int n, int m, int i) {
  if (n >= N - 1)
    return 0;

  if (~memo[n][m][i])
    return memo[n][m][i];

  int res = inf;

  rep(j, k[n + 1]) {
    int dist = (stone[n][i].scd + stone[n + 1][j].scd) *
               abs(stone[n][i].fst - stone[n + 1][j].fst);
    chmin(res, dist + rec(n + 1, m, j));
  }

  if (m < M) {
    if (n + 2 >= N)
      return 0;
    rep(j, k[n + 2]) {
      int dist = (stone[n][i].scd + stone[n + 2][j].scd) *
                 abs(stone[n][i].fst - stone[n + 2][j].fst);
      chmin(res, dist + rec(n + 2, m + 1, j));
    }
  }

  return memo[n][m][i] = res;
}

signed main() {
  while (scanf("%lld %lld", &N, &M), N || M) {
    rep(i, N) {
      scanf("%lld", &k[i]);

      stone[i].resize(k[i]);

      rep(j, k[i]) {
        int x, d;
        scanf("%lld %lld", &x, &d);
        stone[i][j].fst = x, stone[i][j].scd = d;
      }
    }

    int ans = inf;

    rep(i, k[0]) {
      memset(memo, -1, sizeof(memo));
      chmin(ans, rec(0, 0, i));
    }

    if (M > 0) {
      rep(i, k[1]) {
        memset(memo, -1, sizeof(memo));
        chmin(ans, rec(1, 1, i));
      }
    }

    printf("%lld\\n", ans);
  }
}

Error: MLE

Fixed submission:

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#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 >= 1; --i)
#define FOR(i, a, x) for (int i = (a); i < (x); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  rep(i, vec.size()) is >> vec[i];
  return is;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;

int k[151];
vector<pii> stone[150];
int N, M;

int memo[150][150][1000];

int rec(int n, int m, int i) {
  if (n >= N - 1)
    return 0;

  if (~memo[n][m][i])
    return memo[n][m][i];

  int res = inf;

  rep(j, k[n + 1]) {
    int dist = (stone[n][i].scd + stone[n + 1][j].scd) *
               abs(stone[n][i].fst - stone[n + 1][j].fst);
    chmin(res, dist + rec(n + 1, m, j));
  }

  if (m > 0) {
    if (n + 2 >= N)
      return 0;
    rep(j, k[n + 2]) {
      int dist = (stone[n][i].scd + stone[n + 2][j].scd) *
                 abs(stone[n][i].fst - stone[n + 2][j].fst);
      chmin(res, dist + rec(n + 2, m - 1, j));
    }
  }

  return memo[n][m][i] = res;
}

signed main() {
  while (scanf("%d %d", &N, &M), N || M) {
    rep(i, N) {
      scanf("%d", &k[i]);

      stone[i].resize(k[i]);

      rep(j, k[i]) {
        int x, d;
        scanf("%d %d", &x, &d);
        stone[i][j].fst = x, stone[i][j].scd = d;
      }
    }

    int ans = inf;

    rep(i, k[0]) {
      memset(memo, -1, sizeof(memo));
      chmin(ans, rec(0, M, i));
    }

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