Problem p00453 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int fie[152][1002][78];
int main() {
  while (1) {
    int M, N;
    cin >> M >> N;
    if (M == 0 && N == 0)
      break;
    int lis[152][1002];
    for (int i = 0; i < 152; i++) {
      for (int j = 0; j < 1002; j++) {
        if (i != 0 && i != M + 1)
          lis[i][j] = -1;
        else
          lis[i][j] = 0;
      }
    }
    for (int i = 0; i < 152; i++) {
      for (int j = 0; j < 1002; j++) {
        for (int k = 0; k < 78; k++) {
          if (i != 0 || k != 0)
            fie[i][j][k] = 2000000000;
          else
            fie[i][j][k] = 0;
        }
      }
    }
    for (int i = 0; i < M; i++) {
      int a;
      cin >> a;
      for (int j = 1; j <= a; j++) {
        int b, c;
        cin >> b >> c;
        lis[i + 1][b] = c;
      }
    }
    for (int i = 0; i <= M; i++) {
      for (int j = 0; j < 1002; j++) {
        for (int k = 0; k < 76; k++) {
          if (fie[i][j][k] < 2000000000) {
            for (int l = 0; l < 1002; l++) {
              if (lis[i + 1][l] != -1)
                fie[i + 1][l][k] =
                    min(fie[i + 1][l][k],
                        fie[i][j][k] +
                            (lis[i][j] + lis[i + 1][l]) * (int)fabs(j - l));
              if (lis[i + 2][l] != -1)
                fie[i + 2][l][k + 1] =
                    min(fie[i + 2][l][k + 1],
                        fie[i][j][k] +
                            (lis[i][j] + lis[i + 2][l]) * (int)fabs(j - l));
            }
          }
        }
      }
    }
    int ans = 2000000000;
    for (int j = 0; j < 1002; j++) {
      for (int k = 0; k < 76; k++) {
        if (k <= N)
          ans = min(ans, fie[M + 1][j][k]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int fie[152][1002][78];
int main() {
  while (1) {
    int M, N;
    cin >> M >> N;
    if (M == 0 && N == 0)
      break;
    int lis[154][1002];
    for (int i = 0; i < 154; i++) {
      for (int j = 0; j < 1002; j++) {
        if (i != 0 && i != M + 1)
          lis[i][j] = -1;
        else
          lis[i][j] = 0;
      }
    }
    for (int i = 0; i < 152; i++) {
      for (int j = 0; j < 1002; j++) {
        for (int k = 0; k < 78; k++) {
          if (i != 0 || k != 0)
            fie[i][j][k] = 2000000000;
          else
            fie[i][j][k] = 0;
        }
      }
    }
    for (int i = 0; i < M; i++) {
      int a;
      cin >> a;
      for (int j = 1; j <= a; j++) {
        int b, c;
        cin >> b >> c;
        lis[i + 1][b] = c;
      }
    }
    for (int i = 0; i <= M; i++) {
      for (int j = 0; j < 1002; j++) {
        for (int k = 0; k < 76; k++) {
          if (fie[i][j][k] < 2000000000) {
            for (int l = 0; l < 1002; l++) {
              if (lis[i + 1][l] != -1)
                fie[i + 1][l][k] =
                    min(fie[i + 1][l][k],
                        fie[i][j][k] +
                            (lis[i][j] + lis[i + 1][l]) * (int)fabs(j - l));
              if (lis[i + 2][l] != -1)
                fie[i + 2][l][k + 1] =
                    min(fie[i + 2][l][k + 1],
                        fie[i][j][k] +
                            (lis[i][j] + lis[i + 2][l]) * (int)fabs(j - l));
            }
          }
        }
      }
    }
    int ans = 2000000000;
    for (int j = 0; j < 1002; j++) {
      for (int k = 0; k < 76; k++) {
        if (k <= N)
          ans = min(ans, fie[M + 1][j][k]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}