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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long int lli;
const lli INF = 1 << 30;
const int MAX_N = 21000;
const int MAX_M = 20;
lli table[MAX_N + 1][256];
int x[MAX_N];
int book[MAX_M];
void db_print(int line[]) {
  for (int i = 100; i < 200; ++i) {
    cout << i << "[" << line[i] << "]" << endl;
  }
  cout << endl;
}
int main() {
  int N, M;
  while (true) {
    cin >> N >> M;
    if ((N | M) == 0) {
      break;
    }
    for (int i = 0; i < M; ++i) {
      cin >> book[i];
    }
    for (int i = 0; i < N; ++i) {
      cin >> x[i];
    }
    for (int i = 0; i < N + 1; ++i) {
      for (int j = 0; j < 256; ++j) {
        table[i][j] = INF;
      }
    }
    // fill_n((int *) table, (N+1) * 256, INF);
    table[0][128] = 0;
    for (int i = 0; i < N; ++i) {
      // db_print(table[i]);
      for (int j = 0; j < 256; ++j) {
        for (int b = 0; b < M; ++b) {
          int d = book[b];
          if (0 <= j + d && j + d <= 255) {
            int t = (x[i] - (j + d)) * (x[i] - (j + d));
            table[i + 1][j + d] = min(table[i + 1][j + d], table[i][j] + t);
          }
          if (j + d < 0) {
            int t = (x[i] - 0) * (x[i] - 0);
            table[i + 1][0] = min(table[i + 1][0], table[i][j] + t);
          }
          if (j + d > 255) {
            int t = (x[i] - 255) * (x[i] - 255);
            table[i + 1][255] = min(table[i + 1][255], table[i][j] + t);
          }
        }
      }
    }
    // db_print(table[N]);
    lli ans = INF;
    for (int i = 0; i < 256; ++i) {
      ans = min(ans, table[N][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int INF = 1 << 22;
void db_print(int line[]) {
  for (int i = 100; i < 200; ++i) {
    cout << i << "[" << line[i] << "]" << endl;
  }
  cout << endl;
}
int main() {
  int N, M;
  while (true) {
    cin >> N >> M;
    if ((N | M) == 0) {
      break;
    }
    int book[M];
    for (int i = 0; i < M; ++i) {
      cin >> book[i];
    }
    int x[N];
    for (int i = 0; i < N; ++i) {
      cin >> x[i];
    }
    int table[N + 1][256];
    fill_n((int *)table, (N + 1) * 256, INF);
    table[0][128] = 0;
    for (int i = 0; i < N; ++i) {
      // db_print(table[i]);
      for (int j = 0; j < 256; ++j) {
        for (int b = 0; b < M; ++b) {
          int d = book[b];
          if (0 <= j + d && j + d <= 255) {
            int t = (x[i] - (j + d)) * (x[i] - (j + d));
            table[i + 1][j + d] = min(table[i + 1][j + d], table[i][j] + t);
          }
          if (j + d < 0) {
            int t = (x[i] - 0) * (x[i] - 0);
            table[i + 1][0] = min(table[i + 1][0], table[i][j] + t);
          }
          if (j + d > 255) {
            int t = (x[i] - 255) * (x[i] - 255);
            table[i + 1][255] = min(table[i + 1][255], table[i][j] + t);
          }
        }
      }
    }
    // db_print(table[N]);
    int ans = INF;
    for (int i = 0; i < 256; ++i) {
      ans = min(ans, table[N][i]);
    }
    cout << ans << endl;
  }
  return 0;
}