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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
#define INF 999999999999
#define EPS 1.0e-6

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int n, m;
int c[20];
ll x[21000];
ll d[21000][260];

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }

    for (int i = 1; i <= n; i++) {
      cin >> x[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= 256; j++) {
        d[i][j] = INF;
      }
    }
    d[0][128] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        if (d[i][j] != INF) {
          for (int k = 0; k < m; k++) {
            int l = j + c[k];
            // cout << l << endl;
            if (l < 0)
              d[i + 1][0] = min(d[i + 1][0], d[i][j] + x[i + 1] * x[i + 1]);
            else if (l > 255) {
              d[i + 1][255] = min(
                  d[i + 1][255], d[i][j] + (x[i + 1] - 255) * (x[i + 1] - 255));
              // cout << d[i + 1][256] << endl;
            } else {
              d[i + 1][l] =
                  min(d[i + 1][l], d[i][j] + (x[i + 1] - l) * (x[i + 1] - l));
              // cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l];
            }
            // cout << i + 1 << " " << j << " " << l << " " << d[i +
            // 1][l]<<endl;
          }
        }
      }
      /*
              for (int j = 0; j <= 256; j++) {
                      cout << i<< "j:" <<j<<" "<<d[i][j] << endl;

              }
              cout << endl;
              */
    }
    ll ans = INF;
    for (int i = 0; i <= 255; i++) {
      // cout <<"d[n-1][i]:"<< d[n][i] << endl;
      ans = min(ans, d[n][i]);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
#define INF 999999999
#define EPS 1.0e-6

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int n, m;
int c[20];
ll x[21000];
ll d[1000][260];

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }

    for (int i = 1; i <= n; i++) {
      cin >> x[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= 256; j++) {
        d[i][j] = INF;
      }
    }
    d[0][128] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        if (d[i][j] != INF) {
          for (int k = 0; k < m; k++) {
            int l = j + c[k];
            // cout << l << endl;
            if (l < 0)
              d[i + 1][0] = min(d[i + 1][0], x[i + 1] * x[i + 1]);
            else if (l > 255) {
              d[i + 1][255] = min(
                  d[i + 1][256], d[i][j] + (x[i + 1] - 255) * (x[i + 1] - 255));
              // cout << d[i + 1][256] << endl;
            } else {
              d[i + 1][l] =
                  min(d[i + 1][l], d[i][j] + (x[i + 1] - l) * (x[i + 1] - l));
              // cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l];
            }
            // cout << i + 1 << " " << j << " " << l << " " << d[i +
            // 1][l]<<endl;
          }
        }
      }
      /*
              for (int j = 0; j <= 256; j++) {
                      cout << i<< "j:" <<j<<" "<<d[i][j] << endl;

              }
              cout << endl;
              */
    }
    ll ans = INF;
    for (int i = 0; i <= 255; i++) {
      // cout <<"d[n-1][i]:"<< d[n][i] << endl;
      ans = min(ans, d[n][i]);
    }
    cout << ans << endl;
  }

  return 0;
}