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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
const int INF = 1e9;
int n, m;
int bmemo[160][1010];          // b[y][x]
vector<pair<int, int>> b[160]; //
int dp[160][1010][80]; // dp[y??§?¨?][x??§?¨?][1????£???°????????£??????????????°]
                       // = ????????????????°????
int main(void) {
  while (1) {
    // input
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    rep(i, 160) rep(j, 1010) bmemo[i][j] = INF;
    rep(i, 160) b[i].clear(); //?????????
    rep(i, n) {
      int k;
      cin >> k;
      rep(j, k) { // k???????????§?¨?
        int x, s;
        cin >> x >> s;
        x--;
        b[i].push_back(make_pair(x, s));
        bmemo[i][x] = s;
      }
    }

    rep(i, 160) rep(j, 1010) rep(k, 80) dp[i][j][k] = INF;
    for (auto start1 : b[0]) { //???????????????????????£??????
      dp[0][start1.fi][0] = 0;
    }

    for (auto start2 : b[1]) { //????????????????£???°????????£??????
      dp[1][start2.fi][1] = 0;
    }

    //??????dp
    for (int i = 0; i < n - 1; ++i) {
      for (int j = 0; j < 1010; ++j) {
        for (int k = 0; k <= m; ++k) {
          if (dp[i][j][k] == INF)
            continue;
          //??????????????£?????????
          for (auto next : b[i + 1]) {
            int score = (bmemo[i][j] + next.se) *
                        abs(j - next.fi); //?????????????????±??????
            dp[i + 1][next.fi][k] =
                min(dp[i + 1][next.fi][k], dp[i][j][k] + score);
          }
          if (k == m || i == n - 2)
            continue;
          //???????£???°????????£??????
          for (auto next : b[i + 2]) {
            int score = (bmemo[i][j] + next.se) *
                        abs(j - next.fi); //?????????????????±??????
            dp[i + 2][next.fi][k + 1] =
                min(dp[i + 2][next.fi][k + 1], dp[i][j][k] + score);
          }
        }
      }
    }

    //????°????????????????
    int ans = INF;
    for (int j = 0; j < 1010; ++j) {
      for (int k = 0; k <= m; ++k) {
        ans = min(ans, dp[n - 1][j][k]);
      }
    }

    for (int j = 0; j < 1010; ++j) {
      for (int k = 0; k <= m - 1; ++k) {
        ans = min(ans, dp[n - 2][j][k]);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
const int INF = 1e9;
int n, m;
int bmemo[160][4];             // b[y][x]
vector<pair<int, int>> b[160]; //
int dp[160][4][80]; // dp[y??§?¨?][x??§?¨?][1????£???°????????£??????????????°]
int main(void) {
  while (1) {
    // input
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    rep(i, 160) rep(j, 4) bmemo[i][j] = INF;
    rep(i, n) {
      int k;
      cin >> k;
      rep(j, k) { // k???????????§?¨?
        int x, s;
        cin >> x >> s;
        x--;
        b[i].push_back(make_pair(x, s));
        bmemo[i][x] = s;
      }
    }

    rep(i, 160) rep(j, 4) rep(k, 80) dp[i][j][k] = INF;

    rep(i, 4) {
      if (bmemo[0][i] != INF)
        dp[0][i][0] = 0;
    }

    //??????dp
    for (int i = 0; i < n - 1; ++i) {
      for (int j = 0; j < 4; ++j) {
        for (int k = 0; k <= m; ++k) {
          // printf("%d %d %d\\n", i, j, k);
          if (dp[i][j][k] == INF)
            continue;
          //??????????????£?????????
          // printf("ok %d %d %d\\n", i, j, k);
          for (auto next : b[i + 1]) {
            int score = (bmemo[i][j] + next.se) *
                        abs(j - next.fi); //?????????????????±??????
            // printf("bmemo %d\\n", bmemo[i][j]);
            // printf("score %d\\n", score );
            dp[i + 1][next.fi][k] =
                min(dp[i + 1][next.fi][k], dp[i][j][k] + score);
            // printf("ue %d\\n", dp[i + 1][next.fi][k]);
          }
          if (k == m || i == n - 2)
            continue;
          for (auto next : b[i + 2]) {
            int score = (bmemo[i][j] + next.se) *
                        abs(j - next.fi); //?????????????????±??????
            // printf("score %d\\n", score );
            dp[i + 2][next.fi][k + 1] =
                min(dp[i + 2][next.fi][k + 1], dp[i][j][k] + score);
          }
        }
      }
    }

    //????°????????????????
    int ans = INF;
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k <= m; ++k) {
        ans = min(ans, dp[n - 1][j][k]);
      }
    }
    cout << ans << endl;
  }
}