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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[] = {{1, 0}, {0, 1}};
#define int ll
int dp[1502][152];
int n, m, s;
vector<vector<int>> mp;
vector<pll> st;
int func(int pos, int stnum, int spc) {
  if (spc == m + 1)
    return -1;
  if (stnum == s)
    return 0;
  if (dp[stnum][spc] != -1)
    return dp[stnum][spc];
  int res = LLONG_MAX;
  int w = st[stnum].first;
  int sb = st[stnum].second;
  REP(i, mp[pos + 1].size()) {
    int cand = func(pos + 1, mp[pos + 1][i], spc);
    if (cand == -1)
      continue;
    if (sb >= 0 && mp[pos + 1][i] != s) {
      cand +=
          (sb + st[mp[pos + 1][i]].second) * abs(w - st[mp[pos + 1][i]].first);
    }
    res = min(res, cand);
  }
  REP(i, mp[pos + 2].size()) {
    int cand = func(pos + 2, mp[pos + 2][i], spc + 1);
    if (cand == -1)
      continue;
    if (sb >= 0 && mp[pos + 2][i] != s) {
      cand +=
          (sb + st[mp[pos + 2][i]].second) * abs(w - st[mp[pos + 2][i]].first);
    }
    res = min(res, cand);
  }
  dp[stnum][spc] = res;
  if (res == LLONG_MAX)
    res = -1;
  return res;
}

signed main() {
  while (scanf("%lld %lld", &n, &m)) {
    if (n == 0)
      break;
    Fill(dp, -1);
    mp.clear();
    mp.resize(n + 2);
    st.clear();
    s = 0;
    REP(i, n) {
      int c;
      scanf("%lld", &c);
      REP(j, c) {
        int a, b;
        scanf("%lld %lld", &a, &b);
        mp[i].push_back(s);
        st.push_back({a, b});
        s++;
      }
    }
    mp[n].push_back(s);
    st.push_back({-1, -1});
    st.push_back({-1, -1});
    printf("%lld\\n", func(-1, s + 1, 0));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[] = {{1, 0}, {0, 1}};

int dp[150][150];
int n, m;
vector<vector<int>> mp;
vector<pii> st;
int func(int pos, int stnum, int spc) {
  if (spc == m + 1)
    return -1;
  if (pos == n - 1)
    return 0;
  if (dp[stnum][spc] != -1)
    return dp[stnum][spc];
  int res = INT_MAX;
  int w = st[stnum].first;
  int sb = st[stnum].second;
  REP(i, mp[pos + 1].size()) {
    int cand = func(pos + 1, mp[pos + 1][i], spc);
    if (cand == -1)
      continue;
    if (sb >= 0) {
      cand +=
          (sb + st[mp[pos + 1][i]].second) * abs(w - st[mp[pos + 1][i]].first);
    }
    res = min(res, cand);
  }
  REP(i, mp[pos + 2].size()) {
    int cand = func(pos + 2, mp[pos + 2][i], spc + 1);
    if (cand == -1)
      continue;
    if (sb >= 0) {
      cand +=
          (sb + st[mp[pos + 2][i]].second) * abs(w - st[mp[pos + 2][i]].first);
    }
    res = min(res, cand);
  }
  dp[stnum][spc] = res;
  return res;
}

signed main() {
  while (scanf("%d %d", &n, &m)) {
    if (n == 0)
      break;
    Fill(dp, -1);
    mp.resize(n + 2);
    int s = 0;
    REP(i, n) {
      int c;
      scanf("%d", &c);
      REP(j, c) {
        int a, b;
        scanf("%d %d", &a, &b);
        mp[i].push_back(s);
        st.push_back({a, b});
        s++;
      }
    }
    st.push_back({-1, -1});
    printf("%d\\n", func(-1, s, 0));
  }
  return 0;
}