alexjercan/bugnet huggingface dataset:
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

typedef long long ll;

const int MAX_N = 20000;
const int MAX_M = 16;
const ll INF = 1e12;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
ll dp[MAX_N + 10][260];

int main() {
  while (cin >> N >> M, N || M) {
    // Input
    for (int i = 0; i < M; ++i)
      cin >> C[i];
    for (int i = 0; i < N; ++i)
      cin >> x[i];

    // Init
    for (int i = 0; i <= N; ++i)
      fill(dp[i], dp[i] + 259, INF);
    dp[0][128] = 0;

    // Solve
    for (int n = 0; n < N; ++n) {
      for (int y = 0; y <= 255; ++y) {
        for (int i = 0; i < M; ++i) {
          int next_y = y + C[i];
          if (next_y < 0)
            next_y = 0;
          else if (next_y > 255)
            next_y = 255;

          dp[n + 1][next_y] = min(dp[n + 1][next_y],
                                  dp[n][y] + (x[n] - next_y) * (x[n] - next_y));
        }
      }
    }

    ll ans = INF;
    for (int i = 0; i <= 255; ++i)
      ans = min(ans, dp[N][i]);

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int MAX_N = 20000;
const int MAX_M = 16;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
int ans;

void Rec(int index, int sum, int y) {
  if (index == N) {
    ans = min(ans, sum);
    return;
  }

  for (int i = 0; i < M; ++i) {
    int y_n = y + C[i];
    if (y_n < 0)
      y_n = 0;
    else if (y_n > 255)
      y_n = 255;

    int sum_n = sum + (x[index] - y_n) * (x[index] - y_n);
    if (sum_n < ans)
      Rec(index + 1, sum_n, y_n);
  }
}

int main() {
  while (cin >> N >> M, N || M) {
    // Input
    for (int i = 0; i < M; ++i)
      cin >> C[i];
    for (int i = 0; i < N; ++i)
      cin >> x[i];

    int y = 128;
    ans = 0;
    for (int i = 0; i < N; ++i) {
      y += C[M - 1];
      if (y < 0)
        y = 0;
      else if (y > 255)
        y = 255;
      ans += (x[i] - y) * (x[i] - y);
    }

    Rec(0, 0, y0);
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#define NUM 256
using namespace std;

int dp[20001][NUM];

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i <= n; i++)
      for (int j = 0; j < NUM; j++)
        dp[i][j] = 1 << 30;
    dp[0][128] = 0;
    int c[m], x[n];
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 0; i < n; i++)
      cin >> x[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < NUM; j++) {
        for (int k = 0; k < m; k++) {
          if (j + c[k] <= 0) {
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x[i] * x[i]);
          } else if (j + c[k] >= 255) {
            dp[i + 1][255] =
                min(dp[i + 1][255], dp[i][j] + (x[i] - 255) * (x[i] - 255));
          } else {
            dp[i + 1][j + c[k]] =
                min(dp[i + 1][j + c[k]],
                    dp[i][j] + (x[i] - j - c[k]) * (x[i] - j - c[k]));
          }
        }
      }
    }
    // for(int j=0;j<256;j++){
    //   for(int i=0;i<n;i++){
    // 	cout << dp[i][j] << " ";
    //   }
    //   cout << endl;
    // }

    int res = 1 << 30;
    for (int i = 0; i < NUM; i++)
      res = min(res, dp[n][i]);
    cout << res << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#define NUM 256
using namespace std;

int main() {
  int n, m;
  cout << "hello world" << endl;
  while (cin >> n >> m, n) {
    int dp[n + 1][NUM];
    for (int i = 0; i <= n; i++)
      for (int j = 0; j < NUM; j++)
        dp[i][j] = 1 << 30;
    dp[0][128] = 0;
    int c[20], x[n + 1];
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 0; i < n; i++)
      cin >> x[i];

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

    int res = 1 << 30;
    for (int i = 0; i < NUM; i++)
      res = min(res, dp[n][i]);
    cout << res << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    int C[M];
    long long int INF = 10000000000LL;
    for (int i = 0; i < M; ++i)
      cin >> C[i];
    sort(C, C + M);
    int x[N];
    for (int i = 0; i < N; ++i)
      cin >> x[i];
    vector<vector<long long int>> dp(N, vector<long long int>(256, INF));
    for (int i = 0; i < M; ++i) {
      int t = max(min(255, 128 + C[i]), 0);
      dp[0][t] = (long long int)(x[0] - t) * (x[0] - t);
    }
    for (int i = 1; i < N; ++i) {
      for (int j = 0; j < 256; ++j) {
        if (dp[i - 1][j] == INF)
          continue;
        for (int k = 0; k < M; ++k) {
          int t = max(min(255, j + C[k]), 0);
          dp[i][t] = min(dp[i][t],
                         dp[i - 1][j] + (long long int)(x[i] - t) * (x[i] - t));
          if (j + C[k] > 255)
            break;
        }
      }
    }

    long long int ans = INF;
    for (int i = 0; i < 256; ++i) {
      ans = min(ans, dp[N - 1][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    long long int C[M];
    long long int INF = 10000000000LL;
    for (int i = 0; i < M; ++i)
      cin >> C[i];
    sort(C, C + M);
    long long int x[N];
    for (int i = 0; i < N; ++i)
      cin >> x[i];
    long long int dp[N][256];
    for (int i = 0; i < N; ++i) {
      fill(dp[i], dp[i] + 256, INF);
    }
    for (int i = 0; i < M; ++i) {
      int t = max(min(255, 128 + (int)C[i]), 0);
      dp[0][t] = (x[0] - t) * (x[0] - t);
    }
    for (int i = 1; i < N; ++i) {
      for (int j = 0; j < 256; ++j) {
        if (dp[i - 1][j] == INF)
          continue;
        for (int k = 0; k < M; ++k) {
          int t = max(min(255, j + (int)C[k]), 0);
          dp[i][t] = min(dp[i][t], dp[i - 1][j] + (x[i] - t) * (x[i] - t));
          if (j + C[k] > 255)
            break;
        }
      }
    }
    long long int ans = INF;
    for (int i = 0; i < 256; ++i) {
      ans = min(ans, dp[N - 1][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

inline int normalize(int y) { return min(255, max(0, y)); }

inline int get_error(int y, int y0) {
  y -= y0;
  return y * y;
}

int errors[20001][256];
int solve(int n, int m, int c[], int x[]) {
  int min_error = -1;
  for (int t = 0; t <= n; ++t) {
    for (int y = 0; y < 256; ++y) {
      errors[t][y] = -1;
    }
  }
  errors[0][128] = 0;
  for (int t = 0; t < n; ++t) {
    for (int y = 0; y < 256; ++y) {
      if (errors[t][y] < 0) {
        continue;
      }
      for (int i = 0; i < m; ++i) {
        int yn = normalize(y + c[i]);
        int e = errors[t][y] + get_error(yn, x[t]);
        if (errors[t + 1][yn] < 0 || e < errors[t + 1][yn]) {
          errors[t + 1][yn] = e;
        }
      }
    }
  }
  for (int y = 0; y < 256; ++y) {
    if (errors[n][y] < 0) {
      continue;
    }
    if (min_error < 0 || errors[n][y] < min_error) {
      min_error = errors[n][y];
    }
  }
  return min_error;
}

int main() {
  int n, m;
  int c[16], x[20000];
  while (true) {
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    for (int i = 0; i < m; ++i) {
      cin >> c[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> x[i];
    }
    cout << solve(n, m, c, x) << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

inline int normalize(int y) { return min(255, max(0, y)); }

inline int get_error(int y, int y0) {
  y -= y0;
  return y * y;
}

int solve(int n, int m, int c[], int x[]) {
  int min_error = -1;
  int errors[n + 1][256];
  for (int t = 0; t <= n; ++t) {
    for (int y = 0; y < 256; ++y) {
      errors[t][y] = -1;
    }
  }
  errors[0][128] = 0;
  for (int t = 0; t < n; ++t) {
    for (int y = 0; y < 256; ++y) {
      if (errors[t][y] < 0) {
        continue;
      }
      for (int i = 0; i < m; ++i) {
        int yn = normalize(y + c[i]);
        int e = errors[t][y] + get_error(yn, x[t]);
        if (errors[t + 1][yn] < 0 || e < errors[t + 1][yn]) {
          errors[t + 1][yn] = e;
        }
      }
    }
  }
  for (int y = 0; y < 256; ++y) {
    if (errors[n][y] < 0) {
      continue;
    }
    if (min_error < 0 || errors[n][y] < min_error) {
      min_error = errors[n][y];
    }
  }
  return min_error;
}

int main() {
  int n, m;
  int c[16], x[20000];
  while (true) {
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    for (int i = 0; i < m; ++i) {
      cin >> c[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> x[i];
    }
    cout << solve(n, m, c, x) << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cstddef>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

uint64_t dp[20001][256];
//     ^???????????????????????????i
//            ^y_i??????

int round(int x) {
  if (x < 0) {
    return 0;
  }
  if (x > 255) {
    return 255;
  }
  return x;
}

int main() {
  int N, M;
  while (std::cin >> N >> M) {
    if (N == 0 && M == 0) {
      break;
    }
    std::vector<int> x(N);
    std::vector<int> C(M);
    for (int i = 0; i < M; i++) {
      std::cin >> C[i];
    }
    for (int i = 0; i < N; i++) {
      std::cin >> x[i];
    }

    uint64_t inf = 1ull << 30;
    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++) {
        if (dp[i][j] == inf) {
          continue;
        }

        for (auto c : C) {
          int y = round(j + c);
          int diff = x[i] - y;
          int new_value = dp[i][j] + diff * diff;
          if (new_value < dp[i + 1][y]) {
            dp[i + 1][y] = new_value;
          }
        }
      }
    }

    int min = inf;
    for (int j = 0; j < 256; j++) {
      if (dp[N][j] < min) {
        min = dp[N][j];
      }
    }
    std::cout << min << std::endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

int solve(int N, int M) {
  std::vector<int> data(N);
  std::vector<int> codes(M);
  for (int i = 0; i < M; i++) {
    std::cin >> codes[i];
  }
  for (int i = 0; i < N; i++) {
    std::cin >> data[i];
  }

  struct Traverse {
    int n;
    int y;
    int diff;
  };
  auto compare = [](Traverse const &lhs, Traverse const &rhs) {
    return lhs.diff > rhs.diff;
  };
  std::priority_queue<Traverse, std::vector<Traverse>, decltype(compare)> queue(
      compare);
  queue.push(Traverse{0, 128, 0});

  while (!queue.empty()) {
    auto node = queue.top();
    queue.pop();
    if (node.n == N) {
      return node.diff;
    }
    for (auto code : codes) {
      int new_y = node.y + code;
      new_y = new_y < 0 ? 0 : new_y > 255 ? 255 : new_y;
      int diff = (new_y - data[node.n]) * (new_y - data[node.n]);
      queue.push(Traverse{node.n + 1, new_y, node.diff + diff});
    }
  }
}

int main() {
  int N, M;
  while (std::cin >> N >> M) {
    if (N == 0 && M == 0) {
      break;
    }
    std::cout << solve(N, M) << std::endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long int lli;
int c[30], x[30000];
lli dp[30000][256];

int sq(int x) { return x * x; }
int add(int x, int y) { return max(0, min(255, x + y)); }
int main(void) {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    memset(dp, 0x3F, sizeof(dp));

    for (int i = 0; i < m; i++)
      scanf("%d", &c[i]);
    for (int i = 0; i < n; i++)
      scanf("%d", &x[i]);

    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++)
          dp[i + 1][add(j, c[k])] =
              min(dp[i + 1][add(j, c[k])], dp[i][j] + sq(x[i] - add(j, c[k])));

    lli res = 0xFFFFFFFFFFFF;
    for (int j = 0; j < 256; j++)
      res = min(res, dp[n][j]);

    printf("%lld\\n", res);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int c[30], x[30000];
int dp[300000][256];

int sq(int x) { return x * x; }
int add(int x, int y) { return max(0, min(255, x + y)); }
int main(void) {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    memset(dp, 0x6F, sizeof(dp));

    for (int i = 0; i < m; i++)
      scanf("%d", &c[i]);
    for (int i = 0; i < n; i++)
      scanf("%d", &x[i]);

    for (int i = 0; i < 256; i++)
      dp[0][i] = 0xFFFFFF;
    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++)
          dp[i + 1][add(j, c[k])] =
              min(dp[i + 1][add(j, c[k])], dp[i][j] + sq(x[i] - add(j, c[k])));

    int res = 0xFFFFFF;
    for (int j = 0; j < 256; j++)
      res = min(res, dp[n][j]);

    printf("%d\\n", res);
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll dp[20004][256];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  while (cin >> N >> M, N || M) {
    int c[M];
    FOR(i, 0, M) cin >> c[i];
    int x[N];
    FOR(i, 0, N) cin >> x[i];
    FOR(i, 0, N + 1) FOR(j, 0, 256) dp[i][j] = 1e18;
    dp[0][128] = 0;
    FOR(i, 0, N) {
      FOR(j, 0, 256) {
        if (dp[i][j] == 1e15)
          continue;
        FOR(k, 0, M) {
          int y = max(0, min(255, j + c[k]));
          dp[i + 1][y] =
              min(dp[i + 1][y], dp[i][j] + (ll)(x[i] - y) * (ll)(x[i] - y));
        }
      }
    }
    ll ans = 1e18;
    FOR(i, 0, 256) ans = min(ans, dp[N][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  while (cin >> N >> M, N || M) {
    int c[M];
    FOR(i, 0, M) cin >> c[i];
    int x[N];
    FOR(i, 0, N) cin >> x[i];
    ll dp[N + 1][256];
    FOR(i, 0, N + 1) FOR(j, 0, 256) dp[i][j] = 1e18;
    dp[0][128] = 0;
    FOR(i, 0, N) {
      FOR(j, 0, 256) {
        if (dp[i][j] == 1e15)
          continue;
        FOR(k, 0, M) {
          int y = max(0, min(255, j + c[k]));
          dp[i + 1][y] =
              min(dp[i + 1][y], dp[i][j] + (ll)(x[i] - y) * (ll)(x[i] - y));
        }
      }
    }
    ll ans = 1e18;
    FOR(i, 0, 256) ans = min(ans, dp[N][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <utility>
#include <vector>

#define INF (1 << 29)

using namespace std;

int fix(int num) { return min(255, max(num, 0)); }

int main() {
  int N, M;
  while (cin >> N >> M, (N)) {

    vector<int> in_C(M);
    for (auto &_c : in_C) {
      cin >> _c;
    }

    vector<int> in_X(N);
    for (auto &_x : in_X) {
      cin >> _x;
    }

    int dp[2][256];
    for (int i = 0; i < 256; i++) {
      dp[0][i] = dp[1][i] = INF;
    }

    for (int i = 0; i < M; i++) {
      int sub = fix(128 + in_C[i]);
      dp[0][sub] = pow(in_X[0] - sub, 2.0);
    }

    for (int n = 1; n < N; n++) {
      for (int i = 0; i < 256; i++) {
        for (int j = 0; j < M; j++) {
          int sub = fix(i + in_C[j]);
          int nextScore = dp[0][i] + pow(in_X[n] - sub, 2.0);
          dp[1][sub] = min(dp[1][sub], nextScore);
          /*
          cout << sub << endl;
          cout << dp[0][sub] << endl;
          cout << pow( in_X[n] - sub, 2.0 ) << endl;
          cout << endl;
          //*/
        }
      }
      for (int i = 0; i < 256; i++) {
        dp[0][i] = dp[1][i];
        dp[1][i] = INF;
      }
    }

    int ans = INF;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[0][i]);
      // cout << i << " : "<< ans << endl;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 2

Fixed submission:

#include <complex>
#include <iostream>
#include <utility>
#include <vector>

#define INF (1 << 29)

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, (N)) {

    vector<int> in_C(M);
    for (auto &_c : in_C) {
      cin >> _c;
    }

    vector<int> in_X(N);
    for (auto &_x : in_X) {
      cin >> _x;
    }
    exit(514);

    pair<int, int> dp[20000][16];
    for (int i = 0; i < M; i++) {
      int score, nextX;
      nextX = in_X[0] + in_C[i];
      score = (128 - nextX) * (128 - nextX);

      dp[0][i] = make_pair(score, nextX);
    }
    exit(114);

    for (int n = 1; n < N; n++) {
      for (int i = 0; i < M; i++) {
        int nextScore = INF;
        int nextX = 0;
        for (int j = 0; j < M; j++) {
          int tmpScore = dp[n - 1][i].first +
                         pow((dp[n - 1][i].second + in_C[i] - in_X[n]), 2.0);
          if (tmpScore < nextScore) {
            nextScore = tmpScore;
            nextX = dp[n - 1][i].second + in_C[i];
          }
        }
        dp[n][i] = make_pair(nextScore, nextX);
      }
    }

    int ans = INF;
    for (int i = 0; i < M; i++) {
      if (dp[N - 1][i].first < ans)
        ans = dp[N - 1][i].first;
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

ll c[16], x[20000];

ll dp[20001][256];

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0)
      break;
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 0; i < n; i++)
      cin >> x[i];

    for (int i = 0; i <= 20000; i++)
      for (int j = 0; j < 256; j++)
        dp[i][j] = 1e18;
    dp[0][128] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        for (int k = 0; k < m; k++) {
          ll nxt = 0;
          nxt = j + c[k];
          if (nxt < 0)
            nxt = 0;
          if (nxt > 255)
            nxt = 255;

          dp[i + 1][nxt] =
              min(dp[i + 1][nxt], dp[i][j] + (x[i] - nxt) * (x[i] - nxt));
        }
      }
    }
    ll ans = 1e18;

    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

ll c[20000], x[16];

ll dp[21][256];

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0)
      break;
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 0; i < n; i++)
      cin >> x[i];

    for (int i = 0; i < 21; i++)
      for (int j = 0; j < 256; j++)
        dp[i][j] = 1e18;
    dp[0][128] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        for (int k = 0; k < m; k++) {
          int nxt = 0;
          if (j + c[k] < 0)
            nxt = 0;
          if (j + c[k] > 255)
            nxt = 255;
          nxt = j + c[k];
          dp[i + 1][nxt] =
              min(dp[i + 1][nxt], dp[i][j] + (x[i] - nxt) * (x[i] - nxt));
        }
      }
    }

    ll ans = 1e18;

    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }

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

#include "bits/stdc++.h"
using namespace std;
int C[16];
int x[20001];
int dp[20001][256];
const int INF = 1 << 30;
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> x[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 = 0; i < N; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < M; k++) {
          int nj = j + C[k];
          if (0 > nj)
            nj = 0;
          if (nj > 255)
            nj = 255;
          int tmp = (nj - x[i]) * (nj - x[i]);
          if (dp[i][j] + tmp < dp[i + 1][nj]) {
            dp[i + 1][nj] = dp[i][j] + tmp;
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int C[16];
int x[2001];
int dp[2001][256];
const int INF = 1 << 30;
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> x[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 = 0; i < N; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < M; k++) {
          int nj = j + C[k];
          if (0 > nj)
            nj = 0;
          if (nj > 255)
            nj = 255;
          int tmp = (nj - x[i]) * (nj - x[i]);
          if (dp[i][j] + tmp < dp[i + 1][nj]) {
            dp[i + 1][nj] = dp[i][j] + tmp;
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
}