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

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n;
  string l[50];
  double ans[50];
  int a[9];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> l[i];
      for (int j = 0; j < 9; j++)
        cin >> a[j];
      ans[i] = a[6] * a[7] * a[8] - a[0];
      ans[i] /= a[1] + a[2] + a[3] + (a[4] + a[5]) * a[8];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (ans[j] > ans[j - 1]) {
          double tmp = ans[j];
          ans[j] = ans[j - 1];
          ans[j - 1] = tmp;
          string smp = l[j];
          l[j] = l[j - 1];
          l[j - 1] = smp;
        }
        if (ans[j] == ans[j - 1]) {
          bool t = true;
          int i = 0;
          while (true) {
            if (i == l[j].size() && i != l[j - 1].size()) {
              t = false;
              break;
            }
            if (i == l[j - 1].size() && i != l[j].size())
              break;
            if (l[j][i] < l[j - 1][i]) {
              t = false;
              break;
            }
            if (l[j][i] > l[j - 1][i])
              break;
            i++;
          }
          if (t == false) {
            double tmp = ans[j];
            ans[j] = ans[j - 1];
            ans[j - 1] = tmp;
            string smp = l[j];
            l[j] = l[j - 1];
            l[j - 1] = smp;
          }
        }
      }
    }
    for (int i = 0; i < n; i++)
      cout << l[i] << endl;
    cout << "#" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n;
  string l[50];
  double ans[50];
  int a[9];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> l[i];
      for (int j = 0; j < 9; j++)
        cin >> a[j];
      ans[i] = a[6] * a[7] * a[8] - a[0];
      ans[i] /= a[1] + a[2] + a[3] + (a[4] + a[5]) * a[8];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (ans[j] > ans[j - 1]) {
          double tmp = ans[j];
          ans[j] = ans[j - 1];
          ans[j - 1] = tmp;
          string smp = l[j];
          l[j] = l[j - 1];
          l[j - 1] = smp;
        }
        if (ans[j] == ans[j - 1]) {
          bool t = true;
          int i = 0;
          while (true) {
            if (i == l[j].size() && i != l[j - 1].size()) {
              t = false;
              break;
            }
            if (i == l[j - 1].size() && i != l[j].size())
              break;
            if (l[j][i] < l[j - 1][i]) {
              t = false;
              break;
            }
            if (l[j][i] > l[j - 1][i])
              break;
          }
          if (t == false) {
            double tmp = ans[j];
            ans[j] = ans[j - 1];
            ans[j - 1] = tmp;
            string smp = l[j];
            l[j] = l[j - 1];
            l[j - 1] = smp;
          }
        }
      }
    }
    for (int i = 0; i < n; i++)
      cout << l[i] << endl;
    cout << "#" << endl;
  }
  return 0;
}
Problem p01315 (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;
typedef pair<double, string> P;
bool comp(P a, P b) {
  if (fabs(a.first - b.first) <= 0.00000001) {
    return a.second <= b.second;
  }
  return a.first >= b.first;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<P> v(n);
    FOR(i, 0, n) {
      string L;
      cin >> L;
      ll p, a, b, c, d, e, f, s, m;
      cin >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      ll t = a + b + c + (d + e) * m;
      double buy = (double)(m * f * s - p) / t;
      v[i] = P(buy, L);
    }
    sort(v.begin(), v.end(), comp);
    FOR(i, 0, n) cout << v[i].second << endl;
    puts("#");
  }
  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;
typedef pair<double, string> P;
bool comp(P a, P b) {
  if ((a.first - b.first) <= 0.000001) {
    return a.second <= b.second;
  }
  return a.first >= b.first;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<P> v(n);
    FOR(i, 0, n) {
      string L;
      cin >> L;
      ll p, a, b, c, d, e, f, s, m;
      cin >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      ll t = a + b + c + (d + e) * m;
      double buy = (double)(m * f * s - p) / t;
      v[i] = P(buy, L);
    }
    sort(v.begin(), v.end(), comp);
    FOR(i, 0, n) cout << v[i].second << endl;
    puts("#");
  }
  return 0;
}
Problem p01315 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  while (cin >> n, n) {
    pair<double, string> p[50];
    string s;
    for (int i = 0; i < n; i++) {
      double pr, a, b, c, d, e, f, sh, m;
      cin >> s >> pr >> a >> b >> c >> d >> e >> f >> sh >> m;
      p[i] = make_pair(-(sh * f * m - pr) / (m * (e + d) + a + b + c), s);
    }
    sort(p, p + n);
    for (int i = 0; i < n; i++)
      cout << p[i].second << endl;
    cout << '#' << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    pair<double, string> p[50];
    string s;
    while (n--) {
      double pr, a, b, c, d, e, f, sh, m;
      cin >> s >> pr >> a >> b >> c >> d >> e >> f >> sh >> m;
      p[n] = make_pair(-(sh * f * m - pr) / (m * (e + d) + a + b + c), s);
    }
    sort(p, p + n);
    for (int i = 0; i < n; i++)
      cout << p[i].second << endl;
    cout << '#' << endl;
  }
  return 0;
}
Problem p01315 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef pair<double, string> pds;

bool hikaku(pds lhs, pds rhs) {
  if (lhs.first > rhs.first)
    return true;
  else if (lhs.first == rhs.first)
    return lhs.second < rhs.second;
  else
    return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    string l;
    double p, a, b, c, d, e, f, s, m;
    vector<pds> v;
    for (int i = 0; i < n; i++) {
      cin >> l >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      double eff = (f * s * m - p) / (a + b + c + m * (d + e));
      v.push_back(make_pair(eff, l));
      // cout<<eff<<" "<<l<<endl;
    }
    sort(v.begin(), v.end(), hikaku);
    for (int i = 0; i < n; i++) {
      cout << v[i].second << endl; //<< " "<<v[i].first<< endl;
    }
    cout << "#" << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<double, string> pds;

bool hikaku(pds lhs, pds rhs) {
  if (lhs.first > rhs.first)
    return true;
  else
    return lhs.second < rhs.second;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    string l;
    int p, a, b, c, d, e, f, s, m;
    vector<pds> v;
    for (int i = 0; i < n; i++) {
      cin >> l >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      double eff = (f * s * m - p) / (a + b + c + m * (d + e));
      v.push_back(make_pair(eff, l));
    }
    sort(v.begin(), v.end(), hikaku);
    for (int i = 0; i < n; i++) {
      cout << v[i].second << endl; // " "<<v[i].first<< endl;
    }
    cout << "#" << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <vector>
int square(int x) { return x * x; }
int main() {
  using namespace std;
  int N, M, i, j, c, k, v, y, sub_sq, min;
  vector<int> C, x;
  while (true) {
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    vector<vector<int>> dp(N + 1);
    C.resize(M);
    x.resize(N);
    for (i = 0; i <= N; i++) {
      dp[i].resize(256);
      for (j = 0; j < 256; j++)
        dp[i][j] = INT_MAX;
    }
    for (i = 0; i < M; i++)
      cin >> C[i];
    for (i = 0; i < N; i++)
      cin >> x[i];
    dp[0][128] = 0;
    for (i = 0; i < N; i++) {
      for (k = 0; k < 256; k++) {
        v = dp[i][k];
        if (v == INT_MAX)
          continue;
        for (j = 0; j < M; j++) {
          c = C[j];
          if (k + c > 255)
            y = 255;
          else if (k + c < 0)
            y = 0;
          else
            y = k + c;
          sub_sq = square(x[i] - y);
          if (v + sub_sq < dp[i + 1][y])
            dp[i + 1][y] = v + sub_sq;
        }
      }
    }
    min = INT_MAX;
    for (k = 0; k < 256; k++)
      min = min <= dp[N][k] ? min : dp[N][k];
    cout << min << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_map>
#include <vector>
int square(int x) { return x * x; }
int main() {
  using namespace std;
  int N, M, i, j, c, k, v, y, sub_sq, min;
  vector<int> C, x;
  unordered_map<int, int>::iterator it;
  while (true) {
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    C.resize(M);
    x.resize(N);
    for (i = 0; i < M; i++)
      cin >> C[i];
    for (i = 0; i < N; i++)
      cin >> x[i];
    unordered_map<int, int> dp;
    dp[128] = 0;
    for (i = 0; i < N; i++) {
      unordered_map<int, int> tmpdp;
      for (it = dp.begin(); it != dp.end(); it++) {
        k = it->first;
        v = it->second;
        for (j = 0; j < M; j++) {
          c = C[j];
          if (k + c > 255)
            y = 255;
          else if (k + c < 0)
            y = 0;
          else
            y = k + c;
          sub_sq = square(x[i] - y);
          if (not(tmpdp.count(y) != 0 and v + sub_sq >= tmpdp[y]))
            tmpdp[y] = v + sub_sq;
        }
      }
      dp = tmpdp;
    }
    it = dp.begin();
    min = it->second;
    it++;
    for (; it != dp.end(); it++)
      min = min < it->second ? min : it->second;
    cout << min << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int result[20001][256];

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        result[i][j] = 1000000000;
      }
    }
    result[0][128] = 0;
    int c[17], x[20001];
    for (int i = 1; i < m + 1; i++) {
      cin >> c[i];
    }
    for (int i = 1; i < n + 1; i++) {
      cin >> x[i];
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 1; k < m + 1; k++) {
          int nexty = j + c[k];
          if (nexty < 0)
            nexty = 0;
          else if (nexty > 255)
            nexty = 255;
          result[i][nexty] =
              min(result[i][nexty],
                  result[i - 1][j] + ((x[i] - nexty) * (x[i] - nexty)));
        }
      }
    }
    int ans = 1000000000;
    for (int i = 0; i < 256; i++)
      ans = min(ans, result[n][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int result[20001][256];
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        result[i][j] = 10000000;
      }
    }
    result[0][128] = 0;
    int c[17], x[20001];
    for (int i = 1; i < m + 1; i++) {
      cin >> c[i];
    }
    for (int i = 1; i < n + 1; i++) {
      cin >> x[i];
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 1; k < m + 1; k++) {
          int nexty = j + c[k];
          if (nexty < 0)
            nexty = 0;
          else if (nexty > 255)
            nexty = 255;
          result[i][nexty] =
              min(result[i][nexty],
                  result[i - 1][j] + (x[i] - nexty) * (x[i] - nexty));
        }
      }
    }
    int ans = 1000000;
    for (int i = 1; i < 256; i++)
      ans = min(ans, result[n][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
LL dp[256][2];
int d(int v) { return v * v; }
int ran(int v) {
  if (v < 0)
    return 0;
  return min(255, v);
}
int main(void) {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    vector<int> alt;
    alt.resize(m);
    REP(i, m) cin >> alt[i];
    REP(i, 256) dp[i][0] = 1147483647;
    dp[128][0] = 0;
    REP(i, n) {
      int x;
      cin >> x;
      REP(j, 256) dp[j][(i + 1) % 2] = 2147483647;
      REP(j, 256) {
        REP(k, m) {
          int nv = ran(j + alt[k]);
          dp[nv][(i + 1) % 2] =
              min(dp[nv][(i + 1) % 2], dp[j][i % 2] + d(nv - x));
        }
      }
    }
    LL answer = 2147483647;
    REP(i, 256) answer = min(answer, dp[i][n % 2]);
    cout << answer << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
LL dp[256][2];
int d(int v) { return v * v; }
int ran(int v) {
  if (v < 0)
    return 0;
  return min(255, v);
}
int main(void) {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    vector<int> alt;
    alt.resize(m);
    REP(i, m) cin >> alt[i];
    REP(i, 256) dp[i][0] = 2147483647;
    dp[128][0] = 0;
    REP(i, n) {
      int x;
      cin >> x;
      REP(j, 256) dp[j][(i + 1) % 2] = 2147483647;
      REP(j, 256) {
        REP(k, m)
        dp[ran(j + alt[k])][(i + 1) % 2] =
            min(dp[ran(j + alt[k])][(i + 1) % 2],
                dp[j][i] + d(ran(j + alt[k]) - x));
      }
      // REP(j,256) if(dp[j][(i+1)%2]<100000) cout << j << ' ' << i << ' ' <<
      // dp[j][(i+1)%2] << endl;
    }
    LL answer = 2147483647;
    // REP(i,256) cout << dp[i][n%2] << endl;;
    REP(i, 256) answer = min(answer, dp[i][n % 2]);
    cout << answer << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1LL << 50

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<int> c, x;
    rep(i, m) {
      int a;
      cin >> a;
      c.pb(a);
    }
    rep(i, n) {
      int a;
      cin >> a;
      x.pb(a);
    }

    static ll dp[20100][260];
    rep(i, 20100) rep(j, 260) dp[i][j] = INF;
    dp[0][128] = 0;

    for (int i = 0; i < n; i++) {
      rep(j, 256) {
        rep(k, m) {
          int val = j + c[k];
          if (val > 255)
            val = 255;
          if (val < 0)
            val = 0;
          int pow2 = (x[i] - val) * (x[i] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
          //                    if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] =
          //                    min(dp[i][j],dp[i-1][j-c[k]]+pow2);
        }
      }
    }
    ll mini = INF;
    rep(i, 256) { mini = min(mini, dp[n][i]); }
    cout << mini << endl;
  }
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1LL << 50

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<int> c, x;
    rep(i, m) {
      int a;
      cin >> a;
      c.pb(a);
    }
    rep(i, n) {
      int a;
      cin >> a;
      x.pb(a);
    }

    static ll dp[20100][260];
    rep(i, 30000) rep(j, 300) dp[i][j] = INF;
    dp[0][128] = 0;

    for (int i = 0; i < n; i++) {
      rep(j, 256) {
        rep(k, m) {
          int val = j + c[k];
          if (val > 255)
            val = 255;
          if (val < 0)
            val = 0;
          int pow2 = (x[i] - val) * (x[i] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
          //                    if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] =
          //                    min(dp[i][j],dp[i-1][j-c[k]]+pow2);
        }
      }
    }
    ll mini = INF;
    rep(i, 256) { mini = min(mini, dp[n][i]); }
    cout << mini << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m;
int c[MAX_M + 1], x[MAX_N + 1];
int dp[MAX_N + 1][256 + 1];

int POW(int num) { return num * num; }

int main() {
  while (cin >> n >> m && n + m) {
    REP(i, MAX_N + 1) {
      REP(j, 256 + 1) { dp[i][j] = INF; }
    }
    REP(i, m) cin >> c[i];
    REP(i, n) cin >> x[i];
    dp[0][128] = 0;
    REP(i, n) {
      REP(j, 256) {
        if (dp[i][j] == INF)
          continue;
        REP(k, m) {
          int num = j + c[k];
          if (num < 0)
            num = 0;
          if (num > 255)
            num = 255;
          dp[i + 1][num] = min(dp[i][j] + POW(x[i] - num), dp[i + 1][num]);
        }
      }
    }
    int ans = INF;
    REP(i, 256) { ans = min(ans, dp[n][i]); }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m;
int c[160], x[200000];
int dp[160][2560];

int main() {
  while (cin >> n >> m && n + m) {
    REP(i, 160) {
      REP(j, 2560) { dp[i][j] = INF; }
    }
    REP(i, m) { cin >> c[i]; }
    REP(i, n) { cin >> x[i]; }
    dp[0][128] = 0;
    REP(i, n) {
      REP(j, m) {
        REP(k, 256) {
          int num = k + c[j];
          if (num < 0)
            num = 0;
          if (num > 255)
            num = 255;
          dp[i + 1][num] =
              min(dp[i][k] + (x[i] - num) * (x[i] - num), dp[i + 1][num]);
        }
      }
    }
    int ans = INF;
    REP(i, 2560) { ans = min(ans, dp[n][i]); }
    cout << ans << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int c[m];
    REP(i, m)
    cin >> c[i];
    int x[n];
    REP(i, n)
    cin >> x[i];
    long long dp[256];
    REP(i, 256) dp[i] = INF;
    dp[128] = 0;

    REP(i, n) {
      long long next[256];
      REP(j, 256) next[j] = INF;
      REP(j, 256) {
        if (dp[j] == INF)
          continue;
        REP(k, m) {
          long long hoge = max(min(j + c[k], 255), 0);
          next[hoge] = min(next[hoge], dp[j] + (hoge - x[i]) * (hoge - x[i]));
        }
      }
      // REP(j,256){
      //   if (next[j]!=INF)
      //     printf("%d,%d ",j,next[j]);
      // }
      // cout << endl;
      memcpy(dp, next, sizeof(next));
    }
    cout << *min_element(dp, dp + 256) << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int c[m];
    REP(i, m)
    cin >> c[i];
    int x[m];
    REP(i, n)
    cin >> x[i];
    int dp[256];
    REP(i, 256) dp[i] = INF;
    dp[128] = 0;

    REP(i, n) {
      int next[256];
      REP(j, 256) next[j] = INF;
      REP(j, 256) {
        REP(k, m) {
          int hoge = max(min(j + c[k], 255), 0);
          next[hoge] = min(next[hoge], dp[j] + (hoge - x[i]) * (hoge - x[i]));
        }
      }
      // REP(j,256){
      //   if (next[j]!=INF)
      //     printf("%d,%d ",j,next[j]);
      // }
      // cout << endl;
      memcpy(dp, next, sizeof(next));
    }
    cout << *min_element(dp, dp + 256) << endl;
  }
}