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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 152;
const int MAX_K = 22;

int a[MAX_N];
int dp[MAX_N][MAX_K][(MAX_N + 1) / 2];
int n, m, K;
vector<int> S[MAX_N];
vector<int> T[MAX_N];

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    rep(i, n + 1) {
      S[i].clear();
      T[i].clear();
    }
    rep(i, n) {
      scanf("%d", &K);
      rep(j, K) {
        int x, y;
        scanf("%d%d", &x, &y);
        S[i].push_back(x - 1);
        T[i].push_back(y);
      }
    }
    rep(i, S[n - 2].size()) {
      S[n].push_back(S[n - 2][i]);
      T[n].push_back(0);
    }
    rep(i, S[n - 1].size()) {
      S[n].push_back(S[n - 1][i]);
      T[n].push_back(0);
    }
    fill(dp[0][0], dp[n + 1][0], INF);
    rep(j, S[0].size()) { dp[0][j][0] = 0; }
    rep(j, S[1].size()) {
      rep(l, S[0].size()) {
        dp[1][j][0] =
            min(dp[1][j][0], (T[0][l] + T[1][j]) * abs(S[1][j] - S[0][l]));
      }
      if (m >= 1) {
        dp[1][j][1] = 0;
      }
    }
    for (int i = 1; i < n; i++) {
      rep(j, S[i + 1].size()) {
        rep(k, m + 1) {
          rep(l, S[i].size()) {
            dp[i + 1][j][k] = min(dp[i + 1][j][k],
                                  dp[i][l][k] + (T[i][l] + T[i + 1][j]) *
                                                    abs(S[i + 1][j] - S[i][l]));
          }
          if (k >= 1) {
            rep(l, S[i - 1].size()) {
              dp[i + 1][j][k] =
                  min(dp[i + 1][j][k],
                      dp[i - 1][l][k - 1] + (T[i - 1][l] + T[i + 1][j]) *
                                                abs(S[i + 1][j] - S[i - 1][l]));
            }
          }
        }
      }
    }
    int ans = INF;
    rep(j, S[n].size()) {
      rep(k, m + 1) { ans = min(ans, dp[n][j][k]); }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 152;
const int MAX_K = 12;

int a[MAX_N];
int dp[MAX_N][MAX_K][(MAX_N + 1) / 2];
int n, m, K;
vector<int> S[MAX_N];
vector<int> T[MAX_N];

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    rep(i, n) {
      scanf("%d", &K);
      rep(j, K) {
        int x, y;
        scanf("%d%d", &x, &y);
        S[i].push_back(x - 1);
        T[i].push_back(y);
      }
    }
    rep(i, S[n - 2].size()) {
      S[n].push_back(S[n - 2][i]);
      T[n].push_back(0);
    }
    rep(i, S[n - 1].size()) {
      S[n].push_back(S[n - 1][i]);
      T[n].push_back(0);
    }
    fill(dp[0][0], dp[n + 1][0], INF);
    rep(j, S[0].size()) { dp[0][j][0] = 0; }
    rep(j, S[1].size()) {
      rep(l, S[0].size()) {
        dp[1][j][0] =
            min(dp[1][j][0], (T[0][l] + T[1][j]) * abs(S[1][j] - S[0][l]));
      }
      if (m >= 1) {
        dp[1][j][1] = 0;
      }
    }
    for (int i = 1; i < n; i++) {
      rep(j, S[i + 1].size()) {
        rep(k, m + 1) {
          rep(l, S[i].size()) {
            dp[i + 1][j][k] = min(dp[i + 1][j][k],
                                  dp[i][l][k] + (T[i][l] + T[i + 1][j]) *
                                                    abs(S[i + 1][j] - S[i][l]));
          }
          if (k >= 1) {
            rep(l, S[i - 1].size()) {
              dp[i + 1][j][k] =
                  min(dp[i + 1][j][k],
                      dp[i - 1][l][k - 1] + (T[i - 1][l] + T[i + 1][j]) *
                                                abs(S[i + 1][j] - S[i - 1][l]));
            }
          }
        }
      }
    }
    int ans = INF;
    rep(j, S[n].size()) {
      rep(k, m + 1) { ans = min(ans, dp[n][j][k]); }
    }
    printf("%d\\n", ans);
  }
  return 0;
}