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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int n, m;
  for (;;) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    vector<vector<vector<int>>> mdan(n, vector<vector<int>>(m + 1));
    vector<vector<pair<int, int>>> sube(n);
    int ki, x, d;
    for (int i = 0; i < n; i++) {
      scanf("%d", &ki);
      for (int j = 0; j < ki; j++) {
        scanf("%d%d", &x, &d);
        sube[i].push_back(make_pair(x, d));
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= m; j++) {
        mdan[i][j] = vector<int>(sube[i].size(), -1);
      }
    }
    for (int j = 0; j < sube[0].size(); j++) {
      mdan[0][0][j] = 0;
    }
    if (m > 0) {
      for (int j = 0; j < sube[1].size(); j++) {
        mdan[1][1][j] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < sube[i].size(); j++) {
        if (i == 0)
          mdan[i][0][j] = 0;
        if (i >= 1) {
          for (int l = 0; l < sube[i - 1].size(); l++) {
            for (int k = 0; k <= m; k++) {
              if (mdan[i - 1][k][l] != -1) {
                int newd = mdan[i - 1][k][l] +
                           (sube[i - 1][l].second + sube[i][j].second) *
                               abs(sube[i - 1][l].first - sube[i][j].first);
                if (mdan[i][k][j] == -1 || mdan[i][k][j] > newd)
                  mdan[i][k][j] = newd;
              }
            }
          }
        }
        if (i >= 2) {
          for (int l = 0; l < sube[i - 2].size(); l++) {
            for (int k = 0; k <= m; k++) {
              if (k >= 1 && i >= 2 && mdan[i - 2][k - 1][l] != -1) {
                int newd = mdan[i - 2][k - 1][l] +
                           (sube[i - 2][l].second + sube[i][j].second) *
                               abs(sube[i - 2][l].first - sube[i][j].first);
                if (mdan[i][k][j] == -1 || mdan[i][k][j] > newd)
                  mdan[i][k][j] = newd;
              }
            }
          }
        }
      }
    }
    int mi = INT_MAX;
    for (int l = 0; l < sube[n - 1].size(); l++) {
      for (int k = 0; k <= m; k++) {
        if (mdan[n - 1][k][l] != -1)
          mi = min(mi, mdan[n - 1][k][l]);
      }
    }
    if (n - 2 >= 0) {
      for (int l = 0; l < sube[n - 2].size(); l++) {
        for (int k = 0; k < m; k++) {
          if (mdan[n - 2][k][l] != -1)
            mi = min(mi, mdan[n - 2][k][l]);
        }
      }
    }
    printf("%d\\n", mi);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int n, m;
  for (;;) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    vector<vector<vector<int>>> mdan(n, vector<vector<int>>(m + 1));
    vector<vector<pair<int, int>>> sube(n);
    int ki, x, d;
    for (int i = 0; i < n; i++) {
      scanf("%d", &ki);
      for (int j = 0; j < ki; j++) {
        scanf("%d%d", &x, &d);
        sube[i].push_back(make_pair(x, d));
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= m; j++) {
        mdan[i][j] = vector<int>(sube[i].size(), -1);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < sube[i].size(); j++) {
        if (i == 0)
          mdan[i][0][j] = 0;
        else {
          for (int l = 0; l < sube[i - 1].size(); l++) {
            for (int k = 0; k <= m; k++) {
              if (mdan[i - 1][k][l] != -1) {
                int newd = mdan[i - 1][k][l] +
                           (sube[i - 1][l].second + sube[i][j].second) *
                               abs(sube[i - 1][l].first - sube[i][j].first);
                if (mdan[i][k][j] == -1 || mdan[i][k][j] > newd)
                  mdan[i][k][j] = newd;
              }
              if (k > 0 && i >= 2 && mdan[i - 2][k - 1][l] != -1) {
                int newd = mdan[i - 2][k - 1][l] +
                           (sube[i - 2][l].second + sube[i][j].second) *
                               abs(sube[i - 2][l].first - sube[i][j].first);
                if (mdan[i][k][j] == -1 || mdan[i][k][j] > newd)
                  mdan[i][k][j] = newd;
              }
            }
          }
        }
      }
    }
    int mi = INT_MAX;
    for (int l = 0; l < sube[n - 1].size(); l++) {
      for (int k = 0; k <= m; k++) {
        if (mdan[n - 1][k][l] != -1)
          mi = min(mi, mdan[n - 1][k][l]);
      }
    }
    printf("%d\\n", mi);
  }
}