alexjercan/bugnet huggingface dataset:
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
#define min(a, b) a > b ? b : a

int main(void) {
  int n, m, i, j, c[16], x, t;
  long long int f[2][256], k;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < m; i++)
      scanf("%d", &c[i]);
    memset(f, -1, sizeof(f));
    f[0][128] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &x);
      for (k = 0; k < 256; k++) { // 現在位置
        if (f[i % 2][k] != -1) {
          for (j = 0; j < m; j++) {
            t = k + c[j];
            if (t < 0)
              t = 0;
            else if (t > 255)
              t = 255;
            if (f[(i + 1) % 2][t] == -1 ||
                f[(i + 1) % 2][t] > f[i % 2][k] + (x - t) * (x - t))
              f[(i + 1) % 2][t] = f[i % 2][k] + (x - t) * (x - t);
          }
        }
      }
      for (j = 0; j < 256; j++)
        f[i % 2][j] = -1;
    }
    k = 0x7FFFFFFF;
    for (i = 0; i < 256; i++) {
      if (f[n % 2][i] != -1)
        k = min(f[n % 2][i], k);
    }
    printf("%d\\n", k);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <queue>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(void) {
  int n, m, i, j, c[16], x, t;
  long long int f[17][256], k;
  queue<int> que1, que2;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < m; i++)
      scanf("%d", &c[i]);
    memset(f, -1, sizeof(f));
    que1.push(128);
    f[0][128] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &x);
      for (k = 0; k < 256; k++) { // 現在位置
        if (f[i][k] != -1) {
          for (j = 0; j < m; j++) {
            t = k + c[j];
            if (t < 0)
              t = 0;
            else if (t > 255)
              t = 255;
            if (f[i + 1][t] == -1 || f[i + 1][t] > f[i][k] + (x - t) * (x - t))
              f[i + 1][t] = f[i][k] + (x - t) * (x - t);
          }
        }
      }
    }
    k = 0x7FFFFFFF;
    for (i = 0; i < 256; i++) {
      if (f[n][i] != -1)
        k = min(f[n][i], k);
    }
    printf("☆%d\\n", k);
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int inf = 1e8;
int memo[20000 + 1][256];

void hoge(int N, int M) {

  int c[M];
  for (int i = 0; i < M; i++)
    cin >> c[i];
  int x[N];
  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++) {
      memo[i][j] = inf;
    }
  }

  memo[0][128] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      if (memo[i][j] == inf)
        continue;
      for (int k = 0; k < M; k++) {
        int p = max(0, min(255, j + c[k]));
        memo[i + 1][p] =
            min(memo[i][j] + (p - x[i]) * (p - x[i]), memo[i + 1][p]);
      }
    }
  }
  int mi = 1e8;
  for (int j = 0; j < 256; j++)
    mi = min(mi, memo[N][j]);
  cout << mi << endl;
}

int main() {

  while (1) {
    int N, M;
    cin >> N >> M;
    if (!(N || M))
      break;
    hoge(N, M);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
const int inf = 1e8;

void hoge(int N, int M) {

  int c[M];
  for (int i = 0; i < M; i++)
    cin >> c[i];
  int x[N];
  for (int i = 0; i < N; i++)
    cin >> x[i];
  int memo[N + 1][256];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < 256; j++) {
      memo[i][j] = inf;
    }
  }

  memo[0][128] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      if (memo[i][j] == inf)
        continue;
      for (int k = 0; k < M; k++) {
        int p = max(0, min(255, j + c[k]));
        memo[i + 1][p] =
            min(memo[i][j] + (p - x[i]) * (p - x[i]), memo[i + 1][p]);
      }
    }
  }
  int mi = 1e8;
  for (int j = 0; j < 256; j++)
    mi = min(mi, memo[N][j]);
  cout << mi << endl;
}

int main() {

  while (1) {
    int N, M;
    cin >> N >> M;
    if (!(N || M))
      break;
    hoge(N, M);
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    int cs[m], xs[n];
    for (int i = 0; i < m; i++)
      cin >> cs[i];
    for (int i = 0; i < n; i++)
      cin >> xs[i];

    static int dp[20001][256];
    memset(dp, 0x7f, sizeof dp);
    dp[0][128] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == 0x7f7f7f7f)
          continue;
        for (int k = 0; k < m; k++) {
          int l = max(min(j + cs[k], 255), 0);
          dp[i + 1][l] =
              min(dp[i + 1][l], dp[i][j] + (l - xs[i]) * (l - xs[i]));
        }
      }

    cout << *min_element(dp[n], dp[n] + 256) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <tuple>

using namespace std;

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    int cs[m], xs[n];
    for (int i = 0; i < m; i++)
      cin >> cs[i];
    for (int i = 0; i < n; i++)
      cin >> xs[i];

    queue<tuple<int, int, int>> q; // (index,value,sqrsum)
    q.emplace(0, 128, 0);

    int dp[n + 1][256];
    for (int i = 0; i <= n; i++)
      fill(dp[i], dp[i] + 256, numeric_limits<int>::max());

    while (!q.empty()) {
      auto cur = q.front();
      q.pop();
      int ci = get<0>(cur), cval = get<1>(cur), csum = get<2>(cur);

      if (dp[ci][cval] <= csum)
        continue;
      dp[ci][cval] = csum;

      if (ci == n)
        continue;

      for (int i = 0; i < m; i++) {
        int nval = max(min(cval + cs[i], 255), 0);
        int nsum = csum + (nval - xs[ci]) * (nval - xs[ci]);
        q.emplace(ci + 1, nval, nsum);
      }
    }

    cout << *min_element(dp[n], dp[n] + 256) << endl;
  }

  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    VVI dp(n + 1, vector<ll>(256, INF));
    std::vector<int> c(m), x(n + 1);
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 1; i <= n; i++)
      cin >> x[i];
    // ll dp[n+1][256];
    for (int i = 0; i < n + 1; i++)
      for (int j = 0; j < 256; j++)
        dp[i][j] = INF;
    dp[0][128] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          if (j + c[k] > 255)
            dp[i + 1][255] = min(
                dp[i + 1][255], dp[i][j] + (255 - x[i + 1]) * (255 - x[i + 1]));
          else if (j + c[k] < 0)
            dp[i + 1][0] =
                min(dp[i + 1][0], dp[i][j] + (x[i + 1]) * (x[i + 1]));
          else
            dp[i + 1][j + c[k]] =
                min(dp[i + 1][j + c[k]],
                    dp[i][j] + (j + c[k] - x[i + 1]) * (j + c[k] - x[i + 1]));
        }
      }
    ll res = INF;
    for (int i = 0; i < 256; i++)
      res = min(res, dp[n][i]);
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    std::vector<int> c(m), x(n + 1);
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 1; i <= n; i++)
      cin >> x[i];
    ll dp[n + 1][256];
    for (int i = 0; i < n + 1; i++)
      for (int j = 0; j < 256; j++)
        dp[i][j] = INF;
    dp[0][128] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          if (j + c[k] > 255)
            dp[i + 1][255] = min(
                dp[i + 1][255], dp[i][j] + (255 - x[i + 1]) * (255 - x[i + 1]));
          else if (j + c[k] < 0)
            dp[i + 1][0] =
                min(dp[i + 1][0], dp[i][j] + (x[i + 1]) * (x[i + 1]));
          else
            dp[i + 1][j + c[k]] =
                min(dp[i + 1][j + c[k]],
                    dp[i][j] + (j + c[k] - x[i + 1]) * (j + c[k] - x[i + 1]));
        }
      }
    ll res = INF;
    for (int i = 0; i < 256; i++)
      res = min(res, dp[n][i]);
    cout << res << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 20001;
const int MAXM = 16;
const int MAXX = 256;
const int INF = 1 << 28;

int N, M;
int C[MAXM], x[MAXN];
int dp[2][MAXX];

int main() {
  while (cin >> N >> M && (N | M)) {
    for (int i = 0; i < M; ++i)
      cin >> C[i];
    for (int i = 0; i < N; ++i)
      cin >> x[i];
    fill(dp[0], dp[2], INF);
    dp[0][128] = 0;
    for (int i = 0; i < N; ++i) {
      const int now = i & 1, next = 1 - now;
      for (int j = 0; j < MAXX; ++j)
        dp[next][j] = INF;
      for (int j = 0; j < MAXX; ++j) {
        for (int k = 0; k < M; ++k) {
          int nj = max(0, min(MAXX - 1, j + C[k]));
          dp[next][nj] =
              min(dp[next][nj], dp[now][j] + (x[i] - nj) * (x[i] - nj));
        }
      }
    }
    cout << *min_element(dp[N & 1], dp[N & 1] + MAXX) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 20001;
const int MAXM = 16;
const int MAXX = 256;
const int INF = 1 << 28;

int N, M;
int C[MAXM], x[MAXN];
int dp[2][MAXX];

int main() {
  while (cin >> N >> M && (N | M)) {
    for (int i = 0; i < M; ++i)
      cin >> C[i];
    for (int i = 0; i < N; ++i)
      cin >> x[i];
    fill(dp[0], dp[2], INF);
    dp[0][128] = 0;
    for (int i = 0; i < N; ++i) {
      const int now = i & 1, next = 1 - now;
      for (int j = 0; j < MAXX; ++j)
        dp[next][j] = INF;
      for (int j = 0; j < MAXX; ++j) {
        for (int k = 0; k < M; ++k) {
          int nj = min(MAXX - 1, j + C[k]);
          dp[next][nj] =
              min(dp[next][nj], dp[now][j] + (x[i] - nj) * (x[i] - nj));
        }
      }
    }
    int res = INF;
    for (int j = 0; j < MAXX; ++j) {
      res = min(res, dp[N & 1][j]);
    }
    cout << res << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

const int INF = 1e+8;
const int MAX_M = 16;
const int MAX_N = 20001;

// dp[i][y] := i 番目までの差分の総和
int dp[MAX_N][256];

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int C[MAX_M] = {0};

    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }

    // 初期化
    for (int i = 0; i <= MAX_N; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = INF;
      }
    }
    dp[0][128] = 0;

    for (int i = 1; i <= N; i++) {
      int x;
      cin >> x;
      for (int prev = 0; prev < 256; prev++) {
        for (int m = 0; m < M; m++) {
          int next = prev + C[m];
          if (next < 0)
            next = 0;
          if (next > 255)
            next = 255;
          int diff = (next - x) * (next - x);
          dp[i][next] = min(dp[i][next], dp[i - 1][prev] + diff);
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

const int INF = 1e+8;
const int MAX_M = 16;
const int MAX_N = 20000;

// dp[i][y] := i 番目までの差分の総和
int dp[MAX_N][256];

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int C[MAX_M] = {0};

    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }

    // 初期化
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = INF;
      }
    }
    dp[0][128] = 0;

    for (int i = 1; i <= N; i++) {
      int x;
      cin >> x;
      for (int prev = 0; prev < 256; prev++) {
        for (int m = 0; m < M; m++) {
          int next = prev + C[m];
          if (next < 0)
            next = 0;
          if (next > 255)
            next = 255;
          int diff = (next - x) * (next - x);
          dp[i][next] = min(dp[i][next], dp[i - 1][prev] + diff);
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

typedef int ll;

ll dp[20010][256];
int book[20];
int input[20010];
int N, M;

static const ll MAX = 100000000;

void init() { fill(&dp[0][0], &dp[20009][256], MAX); }

void solve() {
  dp[0][128] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      for (int k = 0; k < M; k++) {
        int tar = j + book[k];
        if (tar < 0) {
          tar = 0;
        } else if (tar > 255) {
          tar = 255;
        }
        ll sub = tar - input[i];
        sub *= sub;
        dp[i + 1][tar] = min(dp[i + 1][tar], dp[i][j] + sub);
      }
    }
  }

  ll mn = MAX;
  for (int i = 0; i < 256; i++) {
    mn = min(mn, dp[N][i]);
  }
  printf("%ld\\n", mn);
}

int main() {
  while (true) {
    scanf("%d %d ", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }
    init();
    for (int i = 0; i < M; i++) {
      scanf("%d ", &book[i]);
    }
    for (int i = 0; i < N; i++) {
      scanf("%d ", &input[i]);
    }
    solve();
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

ll dp[20010][256];
int book[20];
int input[20010];
int N, M;

static const ll MAX = 1000000000;

void init() { fill(&dp[0][0], &dp[20009][256], MAX); }

void solve() {
  dp[0][128] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      for (int k = 0; k < M; k++) {
        int tar = j + book[k];
        if (tar < 0) {
          tar = 0;
        } else if (tar > 255) {
          tar = 255;
        }
        ll sub = tar - input[i];
        sub *= sub;
        dp[i + 1][tar] = min(dp[i + 1][tar], dp[i][j] + sub);
      }
    }
  }

  ll mn = MAX;
  for (int i = 0; i < 256; i++) {
    mn = min(mn, dp[N][i]);
  }
  printf("%ld\\n", mn);
}

int main() {
  while (true) {
    scanf("%d %d ", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }
    init();
    for (int i = 0; i < M; i++) {
      scanf("%d ", &book[i]);
    }
    for (int i = 0; i < N; i++) {
      scanf("%d ", &input[i]);
    }
    solve();
  }
}
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int n, m;
int c[20];
int x[20005];
int memo[20005][300];

int dp(int y, int i) {
  if (i == n)
    return 0;
  if (memo[i][y] != INF)
    return memo[i][y];
  int res = INF;
  REP(j, m) {
    int tmp = y;
    tmp += c[j];
    if (tmp > 255)
      tmp = 255;
    if (tmp < 0)
      tmp = 0;
    res = min(res, dp(tmp, i + 1) + (tmp - x[i]) * (tmp - x[i]));
  }
  return memo[i][y] = res;
}

int main() {
  while (cin >> n >> m, n) {
    REP(i, 20005) REP(j, 300) memo[i][j] = INF;
    REP(i, m) cin >> c[i];
    REP(i, n) cin >> x[i];
    cout << dp(128, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int n, m;
int c[20];
int x[20005];
int memo[20005][300];

int dp(int y, int i) {
  if (i == n)
    return 0;
  if (memo[i][y] != INF)
    return memo[i][y];
  int res = INF;
  REP(j, m) {
    int tmp = y;
    tmp += c[j];
    if (tmp > 255)
      tmp = 255;
    if (tmp < 0)
      tmp = 0;
    res = min(res, dp(tmp, i + 1) + (tmp - x[i]) * (tmp - x[i]));
  }
  return memo[y][i] = res;
}

int main() {
  while (cin >> n >> m, n) {
    REP(i, 20005) REP(j, 300) memo[i][j] = INF;
    REP(i, m) cin >> c[i];
    REP(i, n) cin >> x[i];
    cout << dp(128, 0) << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define INF 1000000000
using namespace std;

int code[18];
int num[20001];
int N, M;
int minn;
int count = 0;
int nm[20001][256];
void saiki(int y, int sum, int kai) {
  count++;
  //  cout <<"count=" << count << endl;
  // cout <<"y="<< y <<" sum=" << sum << " kai="<< kai<< endl;

  if (kai == N && y != -1)
    nm[N][y] = 0; //,printf("N,nm[%d][%d] = %d\\n",kai,y,nm[kai][y]);

  if (kai < N && sum < minn && nm[kai][y] == INF) {
    for (int i = 0; i < M; i++) {
      int yn = y + code[i];
      if (yn < 0)
        yn = 0;
      if (yn > 255)
        yn = 255;
      saiki(yn, sum + (num[kai] - yn) * (num[kai] - yn), kai + 1);
      if (nm[kai + 1][yn] + (num[kai] - yn) * (num[kai] - yn) < nm[kai][y])
        nm[kai][y] = nm[kai + 1][yn] + (num[kai] - yn) * (num[kai] - yn);
      //  printf("nm[%d][%d] = %d\\n",kai,y,nm[kai][y]);
    }

  } else if (kai < N)
    saiki(-1, nm[kai][y] + sum, N);
}

int main() {

  while (1) {
    cin >> N >> M;

    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < M; i++)
      cin >> code[i];
    for (int i = 0; i < N; i++)
      cin >> num[i];

    minn = INF;
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= 255; j++)
        nm[i][j] = INF;

    saiki(128, 0, 0);

    cout << nm[0][128] << endl;

    // cout << minn << endl;
    // cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 1000000000;
using namespace std;

int code[20001];
int num[17];
int N, M;
int minn;
int count = 0;

void saiki(int y, int sum, int kai) {
  count++;
  //  cout <<"y="<< y <<" sum=" << sum << " kai="<< kai<< endl;
  if (kai == N && sum < minn)
    minn = sum;

  else if (kai < N && sum < minn) {
    for (int i = 0; i < M; i++) {
      int yn = y + code[i];
      if (yn < 0)
        yn = 0;
      if (yn > 255)
        yn = 255;
      saiki(yn, sum + (num[kai] - yn) * (num[kai] - yn), kai + 1);
    }
  }
}

int main() {

  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < M; i++)
      cin >> code[i];
    for (int i = 0; i < N; i++)
      cin >> num[i];

    minn = INF;
    saiki(128, 0, 0);
    cout << minn << endl;
    //  cout << count << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 1000000000;

int i, j, k, n, m, c[17], N[20001], dp[256][20001], x;

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (i = 0; i < m; i++)
      cin >> c[i];
    for (i = 0; i < n; i++)
      cin >> N[i];

    for (i = 0; i < 256; i++) {
      for (j = 0; j <= n; j++) {
        dp[i][j] = MAX;
      }
    }

    dp[128][0] = 0;
    //  int data[20000];
    for (j = 0; j < n; j++) {
      for (i = 0; i <= 255; i++) {
        //	int Min = 100000;
        for (k = 0; k < m; k++) {
          x = (i + c[k]);
          if (x < 0)
            x = 0;
          if (x > 255)
            x = 255;
          // dp[x][0] = 0;
          dp[x][j + 1] = min(dp[x][j + 1], dp[i][j] + (x - N[j]) * (x - N[j]));

          /*  if( Min > ( x - N[j] ) * ( x - N[j] ) ){
            Min = ( x - N[j] ) * ( x - N[j] );
            cout << "Min" << " " << Min << endl;
            data[j] = x;
          }
          */
        }
      }
    }

    /* for( i = 0; i < 256; i++ ){
    cout << "i" << " " << i << " ";
    for( j = 0; j <= n; j++ ){
      cout <<  dp[i][j] << " ";
    }
    cout << endl;
    }*/
    int Min = 1000000000;

    for (i = 0; i < 256; i++) {
      if (Min > dp[i][n]) {
        Min = dp[i][n];
      }
    }
    cout << Min << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 100000000;

int main() {
  int i, j, k, n, m, c[17], N[20001], dp[256][17], x;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (i = 0; i < m; i++)
      cin >> c[i];
    for (i = 0; i < n; i++)
      cin >> N[i];

    for (i = 0; i < 256; i++) {
      for (j = 0; j <= n; j++) {
        dp[i][j] = MAX;
      }
    }

    dp[128][0] = 0;
    //  int data[20000];
    for (j = 0; j < n; j++) {
      for (i = 0; i <= 255; i++) {
        //	int Min = 100000;
        for (k = 0; k < m; k++) {
          x = (i + c[k]);
          if (x < 0)
            x = 0;
          if (x > 255)
            x = 255;
          // dp[x][0] = 0;
          dp[x][j + 1] = min(dp[x][j + 1], dp[i][j] + (x - N[j]) * (x - N[j]));

          /*  if( Min > ( x - N[j] ) * ( x - N[j] ) ){
            Min = ( x - N[j] ) * ( x - N[j] );
            cout << "Min" << " " << Min << endl;
            data[j] = x;
          }
          */
        }
      }
    }

    /* for( i = 0; i < 256; i++ ){
    cout << "i" << " " << i << " ";
    for( j = 0; j <= n; j++ ){
      cout <<  dp[i][j] << " ";
    }
    cout << endl;
    }*/
    int Min = 1000000;

    for (i = 0; i < 256; i++) {
      if (Min > dp[i][n]) {
        Min = dp[i][n];
      }
    }
    cout << Min << endl;
  }

  return 0;
}