Problem p00453 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>
#define INF 1e+9
using namespace std;

typedef pair<int, int> P;

int main() {
  int n, m;
  int ki;
  vector<P> stone[150];
  int dp[150][10][80];
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++) {
      stone[i].clear();
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k <= m; k++)
          dp[i][j][k] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> ki;
      for (int j = 0; j < ki; j++) {
        int a, b;
        cin >> a >> b;
        stone[i].push_back(P(a - 1, b));
      }
    }
    for (int i = 0; i < stone[0].size(); i++) {
      dp[0][i][0] = 0;
    }
    for (int i = 0; i < stone[1].size(); i++) {
      dp[1][i][1] = 0;
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < stone[i].size(); j++) {
        for (int k = 0; k <= m; k++) {
          for (int l = 0; l < stone[i - 1].size(); l++) {
            dp[i][j][k] =
                min(dp[i][j][k],
                    dp[i - 1][l][k] +
                        (stone[i][j].second + stone[i - 1][l].second) *
                            abs(stone[i][j].first - stone[i - 1][l].first));
          }
          if (i >= 2 && k >= 1) {
            for (int l = 0; l < stone[i - 2].size(); l++) {
              dp[i][j][k] =
                  min(dp[i][j][k],
                      dp[i - 2][l][k - 1] +
                          (stone[i][j].second + stone[i - 2][l].second) *
                              abs(stone[i][j].first - stone[i - 2][l].first));
            }
          }
        }
      }
    }
    int mi = INF;
    for (int i = 0; i < stone[n - 1].size(); i++) {
      for (int j = 0; j <= m; j++) {
        mi = min(mi, dp[n - 1][i][j]);
      }
    }
    if (m) {
      for (int i = 0; i < stone[n - 2].size(); i++) {
        for (int j = 0; j < m; j++) {
          mi = min(mi, dp[n - 2][i][j]);
        }
      }
    }
    cout << mi << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
#define INF 1e+9
using namespace std;

typedef pair<int, int> P;

int main() {
  int n, m;
  int ki;
  vector<P> stone[150];
  int dp[150][10][80];
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++) {
      stone[i].clear();
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k <= m; k++)
          dp[i][j][k] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> ki;
      for (int j = 0; j < ki; j++) {
        int a, b;
        cin >> a >> b;
        stone[i].push_back(P(a - 1, b));
      }
    }
    for (int i = 0; i < stone[0].size(); i++) {
      dp[0][stone[0][i].first][0] = 0;
    }
    for (int i = 0; i < stone[1].size(); i++) {
      dp[1][stone[1][i].first][1] = 0;
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < stone[i].size(); j++) {
        for (int k = 0; k <= m; k++) {
          for (int l = 0; l < stone[i - 1].size(); l++) {
            dp[i][stone[i][j].first][k] =
                min(dp[i][stone[i][j].first][k],
                    dp[i - 1][stone[i - 1][l].first][k] +
                        (stone[i][j].second + stone[i - 1][l].second) *
                            abs(stone[i][j].first - stone[i - 1][l].first));
          }
          if (i >= 2 && k >= 1) {
            for (int l = 0; l < stone[i - 2].size(); l++) {
              dp[i][stone[i][j].first][k] =
                  min(dp[i][stone[i][j].first][k],
                      dp[i - 2][stone[i - 2][l].first][k - 1] +
                          (stone[i][j].second + stone[i - 2][l].second) *
                              abs(stone[i][j].first - stone[i - 2][l].first));
            }
          }
        }
      }
    }
    int mi = INF;
    for (int i = 0; i < stone[n - 1].size(); i++) {
      for (int j = 0; j <= m; j++) {
        mi = min(mi, dp[n - 1][stone[n - 1][i].first][j]);
      }
    }
    cout << mi << endl;
  }
  return 0;
}