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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PA;
#define INF (1 << 30)
int suberi(PA a, PA b) { return (a.second + b.second); }
int yoko(PA a, PA b) { return (abs(a.first - b.first)); }
int dp[151][10][80];
int main() {
  int n, m, k, x, d;
  while (scanf("%d %d", &n, &m), n != 0 || m != 0) {
    vector<PA> vec[151];
    fill_n(**dp, 151 * 10 * 80, INF);
    for (int i = 0; i < n; i++) {
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        scanf("%d %d", &x, &d);
        vec[i].push_back(PA(x, d));
        if (i == 0)
          dp[0][j][0] = 0;
        if (i == 1)
          dp[1][j][1] = 0;
      }
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < vec[i].size(); j++) {
        for (int l = 0; l <= m; l++) {
          for (int z = 0; z < vec[i + 1].size(); z++) {
            dp[i + 1][z][l] =
                min(dp[i + 1][z][l], suberi(vec[i][j], vec[i + 1][z]) *
                                             yoko(vec[i][j], vec[i + 1][z]) +
                                         dp[i][j][l]);
          }
          if (l == m || i == n - 2)
            continue;
          for (int z = 0; l <= m, z < vec[i + 2].size(); z++) {
            dp[i + 2][z][l + 1] = min(dp[i + 2][z][l + 1],
                                      suberi(vec[i][j], vec[i + 2][z]) *
                                              yoko(vec[i][j], vec[i + 2][z]) +
                                          dp[i][j][l]);
          }
        }
      }
    }
    int ans = INF;
    for (int i = n - 2; i < n; i++) {
      for (int j = 0; j < vec[i].size(); j++) {
        for (int l = 0; l <= m; l++) {
          if (i != n - 2 || l != m)
            ans = min(ans, dp[i][j][l]);
        }
      }
    }
    printf("%d\\n", ans);
  }
  return (0);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PA;
#define INF (1 << 30)
int suberi(PA a, PA b) { return (a.second + b.second); }
int yoko(PA a, PA b) { return (abs(a.first - b.first)); }
int main() {
  int n, m, k, x, d;
  vector<PA> vec[150];
  while (scanf("%d %d", &n, &m), n != 0 || m != 0) {
    int dp[151][10][80];
    fill_n(**dp, 151 * 10 * 80, INF);
    for (int i = 0; i < n; i++) {
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        scanf("%d %d", &x, &d);
        vec[i].push_back(PA(x, d));
        if (i == 0)
          dp[0][j][0] = 0;
        if (i == 1)
          dp[1][j][1] = 0;
      }
    }
    int ans = INT_MAX;
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < vec[i].size(); j++) {
        for (int l = 0; l <= m; l++) {
          for (int z = 0; z < vec[i + 1].size(); z++) {
            dp[i + 1][z][l] =
                min(dp[i + 1][z][l], suberi(vec[i][j], vec[i + 1][z]) *
                                             yoko(vec[i][j], vec[i + 1][z]) +
                                         dp[i][j][l]);
            if (i + 1 == n - 1)
              ans = min(ans, dp[n - 1][z][l]);
          }
          if (l == m || i == n - 2)
            continue;
          for (int z = 0; l <= m, z < vec[i + 2].size(); z++) {
            dp[i + 2][z][l + 1] =
                min(dp[i + 1][z][l], suberi(vec[i][j], vec[i + 2][z]) *
                                             yoko(vec[i][j], vec[i + 2][z]) +
                                         dp[i][j][l]);
            if (i + 2 == n - 1)
              ans = min(ans, dp[n - 1][z][l + 1]);
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return (0);
}