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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int dp[160][1010][100];
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vvi in(n, vi(1010));
    rep(i, n) {
      int k;
      cin >> k;
      while (k--) {
        int a, b;
        cin >> a >> b;
        a--;
        in[i][a] = b;
      }
    }
    rep(i, 160) rep(j, 1010) rep(k, 100) dp[i][j][k] = inf;
    rep(i, 1010) if (in[0][i]) dp[0][i][0] = 0;
    rep(i, 1010) if (in[1][i]) dp[1][i][1] = 0;
    loop(i, 1, n) rep(j, 1010) rep(k, m + 1) if (in[i][j]) {
      rep(l, 1010) if (in[i - 1][l]) dp[i][j][k] =
          min(dp[i][j][k],
              dp[i - 1][l][k] + (in[i][j] + in[i - 1][l]) * (abs(l - j)));
      if (k && i != 1) {
        rep(l, 1010) if (in[i - 2][l]) dp[i][j][k] =
            min(dp[i][j][k],
                dp[i - 2][l][k - 1] + (in[i][j] + in[i - 2][l]) * (abs(j - l)));
      }
    }
    //		rep(i,n){rep(j,4)cout<<" "<<dp[i][j][1];cout<<endl;}
    int out = inf;
    rep(i, 1010) rep(j, m + 1) out = min(out, dp[n - 1][i][j]);
    rep(i, 1010) rep(j, m) out = min(out, dp[n - 2][i][j]);
    cout << out << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int dp[160][1010][100];
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vvi in(n, vi(1010));
    rep(i, n) {
      int k;
      cin >> k;
      while (k--) {
        int a, b;
        cin >> a >> b;
        a--;
        in[i][a] = b;
      }
    }
    rep(i, 200) rep(j, 1010) rep(k, 100) dp[i][j][k] = inf;
    rep(i, 1010) if (in[0][i]) dp[0][i][0] = 0;
    rep(i, 1010) if (in[1][i]) dp[1][i][1] = 0;
    loop(i, 1, n) rep(j, 1010) rep(k, m + 1) if (in[i][j]) {
      rep(l, 1010) if (in[i - 1][l]) dp[i][j][k] =
          min(dp[i][j][k],
              dp[i - 1][l][k] + (in[i][j] + in[i - 1][l]) * (abs(l - j)));
      if (k && i != 1) {
        rep(l, 1010) if (in[i - 2][l]) dp[i][j][k] =
            min(dp[i][j][k],
                dp[i - 2][l][k - 1] + (in[i][j] + in[i - 2][l]) * (abs(j - l)));
      }
    }
    //		rep(i,n){rep(j,4)cout<<" "<<dp[i][j][1];cout<<endl;}
    int out = inf;
    rep(i, 1010) rep(j, m + 1) out = min(out, dp[n - 1][i][j]);
    rep(i, 1010) rep(j, m) out = min(out, dp[n - 2][i][j]);
    cout << out << endl;
  }
}