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

#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
struct po {
  int x, d;
};
long long dp[155][15][80];

int calc(po a, po b) { return (a.d + b.d) * (abs(a.x - b.x)); }

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      break;

    vector<po> mp[200];
    for (int i = 0; i < 200; i++)
      mp[i].clear();

    for (int i = 0, k; i < n; i++) {
      cin >> k;
      mp[i].resize(k);
      for (int j = 0; j < k; j++)
        cin >> mp[i][j].x >> mp[i][j].d;
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < (int)mp[i].size(); j++)
        for (int k = 0; k <= m; k++)
          dp[i][j][k] = INF;

    for (int i = 0; i < (int)mp[0].size(); i++)
      dp[0][i][0] = 0;
    if (m)
      for (int i = 0; i < (int)mp[1].size(); i++)
        dp[1][i][1] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (int)mp[i].size(); j++)
        for (int k = 0; k <= m; k++) {
          for (int l = 0; l < (int)mp[i + 1].size(); l++)
            dp[i + 1][l][k] = min(dp[i + 1][l][k],
                                  dp[i][j][k] + calc(mp[i][j], mp[i + 1][l]));

          for (int l = 0; l < (int)mp[i + 2].size() && k != m; l++)
            dp[i + 2][l][k + 1] =
                min(dp[i + 2][l][k + 1],
                    dp[i][j][k] + calc(mp[i][j], mp[i + 2][l]));
        }
    }

    long long ans = INF;
    for (int i = 0; i < (int)mp[n - 1].size(); i++)
      for (int j = 0; j <= m; j++)
        ans = min(dp[n - 1][i][j], ans);

    for (int i = 0; i < (int)mp[n - 2].size(); i++)
      for (int j = 0; j < m; j++)
        ans = min(dp[n - 2][i][j], ans);
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
struct po {
  int x, d;
};
long long dp[155][1001][80];

int calc(po a, po b) { return (a.d + b.d) * (abs(a.x - b.x)); }

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      break;

    vector<po> mp[200];
    for (int i = 0; i < 200; i++)
      mp[i].clear();
    for (int i = 0, k; i < n; i++) {
      cin >> k;
      mp[i].resize(k);
      for (int j = 0; j < k; j++)
        cin >> mp[i][j].x >> mp[i][j].d;
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < (int)mp[i].size(); j++)
        for (int k = 0; k <= m; k++)
          dp[i][j][k] = INF;

    for (int i = 0; i < (int)mp[0].size(); i++)
      dp[0][i][0] = 0;
    if (m)
      for (int i = 0; i < (int)mp[1].size(); i++)
        dp[1][i][1] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (int)mp[i].size(); j++)
        for (int k = 0; k <= m; k++) {
          for (int l = 0; l < (int)mp[i + 1].size(); l++)
            dp[i + 1][l][k] = min(dp[i + 1][l][k],
                                  dp[i][j][k] + calc(mp[i][j], mp[i + 1][l]));

          for (int l = 0; l < (int)mp[i + 2].size() && k != m; l++)
            dp[i + 2][l][k + 1] =
                min(dp[i + 2][l][k + 1],
                    dp[i][j][k] + calc(mp[i][j], mp[i + 2][l]));
        }
    }

    long long ans = INF;
    for (int i = 0; i < (int)mp[n - 1].size(); i++)
      for (int j = 0; j <= m; j++)
        ans = min(dp[n - 1][i][j], ans);

    for (int i = 0; i < (int)mp[n - 2].size(); i++)
      for (int j = 0; j < m; j++)
        ans = min(dp[n - 2][i][j], ans);
    cout << ans << endl;
  }

  return 0;
}