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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdbi;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1 << 30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

// #define int long long
#define addm(X, Y) ((X) = ((X) + (Y) % mod) % mod)

int n, m;
int dp[155][80][1005];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<vector<pair<int, int>>> st(155);
    rep(i, n) {
      int k;
      cin >> k;
      rep(j, k) {
        int x, d;
        cin >> x >> d;
        x--;
        st[i].push_back({x, d});
      }
    }
    rep(i, 155) rep(j, 80) rep(k, 1005) dp[i][j][k] = INT_MAX;
    for (auto p : st[0]) {
      dp[0][0][p.fi] = 0;
    }
    if (m != 0) {
      for (auto p : st[1]) {
        dp[1][1][p.fi] = 0;
      }
    }
    rep(i, n) rep(j, m + 1) {
      for (auto now : st[i]) {
        if (dp[i][j][now.fi] == INT_MAX)
          continue;
        if (i + 1 >= n)
          continue;
        for (auto nx : st[i + 1]) {
          dp[i + 1][j][nx.fi] =
              min(dp[i + 1][j][nx.fi],
                  dp[i][j][now.fi] + abs(now.fi - nx.fi) * (now.se + nx.se));
        }
        if (i + 2 >= n || j + 1 > m)
          continue;
        for (auto nx : st[i + 2]) {
          dp[i + 2][j + 1][nx.fi] =
              min(dp[i + 2][j + 1][nx.fi],
                  dp[i][j][now.fi] + abs(now.fi - nx.fi) * (now.se + nx.se));
        }
      }
    }
    int ans = INT_MAX;
    rep(j, m + 1) rep(k, 1001) ans = min(ans, dp[n - 1][j][k]);
    rep(j, m) rep(k, 1001) ans = min(ans, dp[n - 2][j][k]);
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdbi;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1 << 30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

// #define int long long
#define addm(X, Y) ((X) = ((X) + (Y) % mod) % mod)

int n, m;
ll dp[155][80][1005];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<vector<pair<int, int>>> st(155);
    rep(i, n) {
      int k;
      cin >> k;
      rep(j, k) {
        int x, d;
        cin >> x >> d;
        x--;
        st[i].push_back({x, d});
      }
    }
    rep(i, 155) rep(j, 80) rep(k, 1005) dp[i][j][k] = LLONG_MAX / 2;
    for (auto p : st[0]) {
      dp[0][0][p.fi] = 0;
    }
    if (m != 0) {
      for (auto p : st[1]) {
        dp[1][1][p.fi] = 0;
      }
    }
    rep(i, n) rep(j, m + 1) {
      for (auto now : st[i]) {
        if (dp[i][j][now.fi] == LLONG_MAX / 2)
          continue;
        if (i + 1 >= n)
          continue;
        for (auto nx : st[i + 1]) {
          dp[i + 1][j][nx.fi] =
              min(dp[i + 1][j][nx.fi],
                  dp[i][j][now.fi] + abs(now.fi - nx.fi) * (now.se + nx.se));
        }
        if (i + 2 >= n || j + 1 > m)
          continue;
        for (auto nx : st[i + 2]) {
          dp[i + 2][j + 1][nx.fi] =
              min(dp[i + 2][j + 1][nx.fi],
                  dp[i][j][now.fi] + abs(now.fi - nx.fi) * (now.se + nx.se));
        }
      }
    }
    ll ans = LLONG_MAX / 2;
    rep(j, m + 1) rep(k, 1001) ans = min(ans, dp[n - 1][j][k]);
    rep(j, m) rep(k, 1001) ans = min(ans, dp[n - 2][j][k]);
    cout << ans << endl;
  }
}