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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll n;
struct P {
  ll slip, place;
};
P a[152][10];
ll dp[152][10][1000];
ll siz[152];
ll solve(ll i, ll j, ll m) {
  if (i == n)
    return 0;
  ll sum = 80000000000000000;
  for (ll z = 0; z < siz[i + 1]; z++) {
    ll res = (a[i + 1][z].place - a[i][j].place);
    res = max(res, 0 - res);
    res *= (a[i + 1][z].slip + a[i][j].slip);
    if (dp[i + 1][z][m] != -1) {
      sum = min(sum, res + dp[i + 1][z][m]);
    } else {
      sum = min(sum, res + (dp[i + 1][z][m] = solve(i + 1, z, m)));
    }
  }
  if (m > 0 && i + 2 <= n) {
    for (ll z = 0; z < siz[i + 2]; z++) {
      ll res = (a[i + 2][z].place - a[i][j].place);
      res = max(res, 0 - res);
      res *= (a[i + 2][z].slip + a[i][j].slip);
      if (dp[i + 2][z][m - 1] != -1) {
        sum = min(sum, res + dp[i + 2][z][m - 1]);
      } else {
        sum = min(sum, res + (dp[i + 2][z][m - 1] = solve(i + 2, z, m - 1)));
      }
    }
  } else if (m > 0 && i + 2 == n + 1) {
    sum = 0;
  }
  return sum;
}
int main() {
  ll m;
  while (cin >> n >> m && (n != 0 || m != 0)) {
    for (ll i = 1; i <= n; i++) {
      ll k;
      cin >> k;
      siz[i] = k;
      for (ll j = 0; j < k; j++) {
        cin >> a[i][j].place >> a[i][j].slip;
        for (ll z = 0; z <= m; z++)
          dp[i][j][z] = -1;
      }
    }
    ll sum = 80000000000000;
    for (ll i = 0; i < siz[1]; i++) {
      sum = min(sum, solve(1, i, m));
    }
    if (m > 0 && 2 <= n) {
      for (ll i = 0; i < siz[2]; i++) {
        if (dp[2][i][m - 1] != -1)
          sum = min(sum, dp[2][i][m - 1]);
        else
          sum = min(sum, dp[2][i][m - 1] = solve(2, i, m - 1));
      }
    }
    cout << sum << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll n;
struct P {
  ll slip, place;
};
P a[152][10];
ll dp[150][10][1000];
ll siz[152];
ll solve(ll i, ll j, ll m) {
  if (i == n)
    return 0;
  ll sum = 80000000000000000;
  for (ll z = 0; z < siz[i + 1]; z++) {
    ll res = (a[i + 1][z].place - a[i][j].place);
    res = max(res, 0 - res);
    res *= (a[i + 1][z].slip + a[i][j].slip);
    if (dp[i + 1][z][m] != -1) {
      sum = min(sum, res + dp[i + 1][z][m]);
    } else {
      sum = min(sum, res + (dp[i + 1][z][m] = solve(i + 1, z, m)));
    }
  }
  if (m > 0 && i + 1 != n) {
    for (ll z = 0; z < siz[i + 2]; z++) {
      ll res = (a[i + 2][z].place - a[i][j].place);
      res = max(res, 0 - res);
      res *= (a[i + 2][z].slip + a[i][j].slip);
      if (dp[i + 2][z][m - 1] != -1) {
        sum = min(sum, res + dp[i + 2][z][m - 1]);
      } else {
        sum = min(sum, res + (dp[i + 2][z][m - 1] = solve(i + 2, z, m - 1)));
      }
    }
  }
  return sum;
}
int main() {
  ll m;
  while (cin >> n >> m) {
    for (ll i = 1; i <= n; i++) {
      ll k;
      cin >> k;
      siz[i] = k;
      for (ll j = 0; j < k; j++) {
        cin >> a[i][j].place >> a[i][j].slip;
        for (ll z = 0; z <= m; z++)
          dp[i][j][z] = -1;
      }
    }
    ll sum = 80000000000000;
    for (ll i = 0; i < siz[1]; i++) {
      sum = min(sum, solve(1, i, m));
    }
    cout << sum << endl;
  }
}