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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    ll dp[2000][100];
    ll i, j, k, x, d, a;
    ll inf = 1 << 30;
    ll ans = inf;
    vector<PP> v[200];
    for (i = 0; i < 200; i++)
      v[i].clear();
    ll c = 0;
    for (i = 0; i < n; i++) {
      cin >> k;
      for (j = 0; j < k; j++) {
        cin >> x >> d;
        v[i].push_back(PP(c, P(x, d)));
        c++;
      }
    }
    // cout << c << endl;
    fill(dp[0], dp[1999], inf);
    for (i = 0; i < v[0].size(); i++) {
      dp[v[0][i].first][0] = 0;
    }
    for (i = 0; i < v[1].size(); i++) {
      dp[v[1][i].first][1] = 0;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < v[i].size(); j++) {

        for (a = 0; a <= m; a++) {

          if (dp[v[i][j].first][a] == inf)
            continue;

          for (k = 0; k < v[i + 1].size(); k++) {
            dp[v[i + 1][k].first][a] = min(
                dp[v[i + 1][k].first][a],
                dp[v[i][j].first][a] +
                    abs(v[i + 1][k].second.first - v[i][j].second.first) *
                        (v[i + 1][k].second.second + v[i][j].second.second));
          }
          for (k = 0; k < v[i + 2].size(); k++) {
            dp[v[i + 2][k].first][a + 1] = min(
                dp[v[i + 2][k].first][a + 1],
                dp[v[i][j].first][a] +
                    abs(v[i + 2][k].second.first - v[i][j].second.first) *
                        (v[i + 2][k].second.second + v[i][j].second.second));
          }
        }
      }
    }
    ans = inf;
    for (i = 0; i < v[n - 1].size(); i++) {
      for (j = 0; j <= m; j++) {
        ans = min(ans, dp[v[n - 1][i].first][j]);
      }
    }
    for (i = 0; i < v[n - 2].size(); i++) {
      for (j = 0; j <= m - 1; j++) {
        ans = min(ans, dp[v[n - 2][i].first][j]);
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int dp[200][200];
    int i, j, k, x, d, a;
    int inf = 1 << 20;
    int ans = inf;
    vector<PP> v[200];
    for (i = 0; i < 200; i++)
      v[i].clear();
    int c = 0;
    for (i = 0; i < n; i++) {
      cin >> k;
      for (j = 0; j < k; j++) {
        cin >> x >> d;
        v[i].push_back(PP(c, P(x, d)));
        c++;
      }
    }
    fill(dp[0], dp[199], inf);
    for (i = 0; i < v[0].size(); i++) {
      dp[v[0][i].first][0] = 0;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < v[i].size(); j++) {

        for (a = 0; a <= m; a++) {

          if (dp[v[i][j].first][a] == inf)
            continue;

          for (k = 0; k < v[i + 1].size(); k++) {
            dp[v[i + 1][k].first][a] = min(
                dp[v[i + 1][k].first][a],
                dp[v[i][j].first][a] +
                    abs(v[i + 1][k].second.first - v[i][j].second.first) *
                        (v[i + 1][k].second.second + v[i][j].second.second));
          }
          for (k = 0; k < v[i + 2].size(); k++) {
            dp[v[i + 2][k].first][a + 1] = min(
                dp[v[i + 2][k].first][a + 1],
                dp[v[i][j].first][a] +
                    abs(v[i + 2][k].second.first - v[i][j].second.first) *
                        (v[i + 2][k].second.second + v[i][j].second.second));
          }
        }
      }
    }

    for (i = 0; i < v[n - 1].size(); i++) {
      for (j = 0; j <= m; j++) {
        ans = min(ans, dp[v[n - 1][i].first][j]);
      }
    }
    for (i = 0; i < v[n - 2].size(); i++) {
      for (j = 0; j <= m - 1; j++) {
        ans = min(ans, dp[v[n - 2][i].first][j]);
      }
    }

    cout << ans << endl;
  }
  return 0;
}