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

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void f(int n, int m) {
  int c[1000000];

  rep(i, m) { cin >> c[i]; }

  int dp[21000][300] = {};
  rep(i, 21000) {
    rep(j, 300) { dp[i][j] = inf; }
  }

  dp[0][128] = 0;

  FOR(i, 1, n + 1) {
    int x;
    cin >> x;
    rep(j, 256) {
      rep(k, m) {
        int a = j + c[k];
        a = min(a, 255);
        a = max(a, 0);
        dp[i][a] = min(dp[i][a], dp[i - 1][j] + (x - a) * (x - a));
      }
    }
  }
  int ans = inf;
  rep(i, 256) { ans = min(ans, dp[n][i]); }

  cout << ans << endl;
}

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    f(n, m);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void f(int n, int m) {
  int c[1000000];

  rep(i, m) { cin >> c[i]; }

  int dp[20][300] = {};
  rep(i, 20) {
    rep(j, 300) { dp[i][j] = inf; }
  }

  dp[0][128] = 0;

  FOR(i, 1, n + 1) {
    int x;
    cin >> x;
    rep(j, 256) {
      rep(k, m) {
        int a = j + c[k];
        a = min(a, 255);
        a = max(a, 0);
        dp[i][a] = min(dp[i][a], dp[i - 1][j] + (x - a) * (x - a));
      }
    }
  }
  int ans = inf;
  rep(i, 256) { ans = min(ans, dp[n][i]); }

  cout << ans << endl;
}

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    f(n, m);
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> code(m), value(n);
    for (int i = 0; i < m; i++)
      cin >> code[i];
    for (int i = 0; i < n; i++)
      cin >> value[i];
    vector<vector<int>> dp(n + 1, vector<int>(256, 1000000000));
    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 val = min(255, max(0, j + code[k]));
          dp[i + 1][val] = min(dp[i + 1][val],
                               dp[i][j] + (val - value[i]) * (val - value[i]));
        }
      }
    }
    int ans = 1000000000;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -6
free(): invalid size


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> code(m), value(n);
    for (int i = 0; i < m; i++)
      cin >> code[i];
    for (int i = 0; i < n; i++)
      cin >> value[i];
    vector<vector<int>> dp(256, vector<int>(n + 1, 1000000000));
    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 val = min(255, max(0, j + code[k]));
          dp[i + 1][val] = min(dp[i + 1][val],
                               dp[i][j] + (val - value[i]) * (val - value[i]));
        }
      }
    }
    int ans = 1000000000;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 28;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<vector<int>> dp(n + 1, vector<int>(256, INF));

    vector<int> c(m);
    vector<int> x(n);

    for (auto &i : c)
      cin >> i;
    for (auto &i : x)
      cin >> i;

    dp[0][128] = 0;

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

          dp[i][pre] = min(dp[i][pre],
                           dp[i - 1][j] + (pre - x[i - 1]) * (pre - x[i - 1]));
        }
      }
    }

    int res = INF;
    for (int i = 0; i < 256; i++) {
      res = min(res, dp[n][i]);
    }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 1 << 28;
int res;

vector<int> c;
vector<int> x;

void dfs(int y, int n, int sum) {
  if (res < sum)
    return;
  if (n == x.size()) {
    res = min(res, sum);
    return;
  }

  for (auto i : c) {
    int yn = y + i;
    if (yn > 255)
      yn = 255;
    if (yn < 0)
      yn = 0;
    dfs(yn, n + 1, sum + ((yn - x[n]) * (yn - x[n])));
  }
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    res = INF;

    c.resize(m);
    x.resize(n);

    for (auto &i : c)
      cin >> i;
    for (auto &i : x)
      cin >> i;

    dfs(128, 0, 0);

    cout << res << endl;
  }

  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll dp[20001][256];

const ll INF = 1e14;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    rep(i, 20001) rep(j, 256) dp[i][j] = INF;
    vector<int> c(m);
    vector<ll> x(n);
    rep(i, m) cin >> c[i];
    rep(i, n) cin >> x[i];

    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k < 256; k++) {
          if (dp[i][k] == INF)
            continue;
          int z = k + c[j];
          if (z < 0)
            z = 0;
          else if (256 <= z)
            z = 255;
          dp[i + 1][z] = min(dp[i + 1][z], dp[i][k] + (x[i] - z) * (x[i] - z));
        }
      }
    }
    ll res = INF;
    for (int i = 0; i < 256; i++) {
      res = min(res, dp[n][i]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct node {
  ll cost;
  int ind;
  ll val;
  node(ll c, int i, int v) {
    cost = c;
    ind = i;
    val = v;
  }
};

bool checked[20001][256];

bool operator<(const node &lhs, const node &rhs) {
  if (lhs.cost == rhs.cost) {
    return lhs.ind < rhs.ind;
  }
  return lhs.cost > rhs.cost;
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    memset(checked, 0, sizeof(checked));
    vector<ll> c(m), x(n);
    rep(i, m) cin >> c[i];
    sort(c.begin(), c.end());
    rep(i, n) cin >> x[i];
    priority_queue<node> que;
    que.push(node(0, 0, 128));
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (nd.ind == n) {
        cout << nd.cost << endl;
        break;
      }
      if (checked[nd.ind][nd.val]) {
        continue;
      }
      checked[nd.ind][nd.val] = true;
      rep(i, m) {
        ll nval = min(max(nd.val + c[i], 0LL), 255LL);
        if (!checked[nd.ind + 1][nval]) {
          que.push(node(nd.cost + (x[nd.ind] - nval) * (x[nd.ind] - nval),
                        nd.ind + 1, nval));
        }
      }
    }
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 100000000
using namespace std;
int main() {
  int n, m, x, cb[20]; // now,now
  int temp;
  while (cin >> n >> m, n || m) {
    vector<int> input;
    vector<int> dp[256];
    for (int i = 0; i < m; i++) {
      cin >> cb[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> x;
      input.push_back(x);
    }
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= 255; j++)
        dp[j].push_back(INF);
    dp[128][0] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= 255; j++) {

        if (dp[j][i] != INF) {
          for (int k = 0; k < m; k++) {
            temp = j + cb[k];
            if (temp > 255)
              temp = 255;
            else if (temp < 0)
              temp = 0;
            dp[temp][i + 1] =
                min(dp[temp][i + 1],
                    dp[j][i] + (temp - input[i]) * (temp - input[i]));
          }
        }
      }
    int ans = INF;
    for (int i = 0; i <= 255; i++) {
      if (ans > dp[i][n])
        ans = dp[i][n];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 100000000
using namespace std;
int main() {
  int n, m, x, dp[20][260], cb[20]; // now,now
  int temp;
  while (cin >> n >> m, n || m) {
    vector<int> input;
    for (int i = 0; i < m; i++)
      cin >> cb[i];
    for (int i = 0; i < n; i++) {
      cin >> x;
      input.push_back(x);
    }
    for (int i = 0; i <= n; i++)
      fill(dp[i], dp[i] + 256, INF);
    dp[0][128] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= 255; j++) {
        if (dp[i][j] != INF) {
          for (int k = 0; k < m; k++) {
            temp = j + cb[k];
            if (temp > 255)
              temp = 255;
            else if (temp < 0)
              temp = 0;
            dp[i + 1][temp] =
                min(dp[i + 1][temp],
                    dp[i][j] + (temp - input[i]) * (temp - input[i]));
          }
        }
      }
    int ans = INF;
    for (int i = 0; i <= 255; i++) {
      if (ans > dp[n][i])
        ans = dp[n][i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct Edge {
  int from, to;
  ll cost;
};

const ll MOD = 1000000007;
const long double EPS = 10e-10;

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) && n) {
    ll x[n + 1], c[m], dp[n + 2][256];
    rep(m) scanf("%lld", &c[i]);
    x[0] = 128;
    rep(n) scanf("%lld", &x[i + 1]);

    fill(dp[0], dp[n + 2], LLONG_MAX);
    dp[0][128] = 0LL;
    rep(n) {
      repp(j, 256) {
        if (dp[i][j] == LLONG_MAX)
          continue;
        repp(k, m) {
          ll jj = max(0LL, min(255LL, j + c[k]));
          ll diff = jj - x[i + 1];
          dp[i + 1][jj] = min(dp[i + 1][jj], dp[i][j] + diff * diff);
        }
      }
    }

    ll ans = LLONG_MAX;
    repp(j, 256) ans = min(ans, dp[n][j]);
    printf("%lld\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

int n, m;
ll ans = LLONG_MAX;
ll c[16], x[200001];

void dfs(int index, ll score, ll prev) {
  if (index == n) {
    ans = min(ans, score);
    return;
  }
  rep(m) {
    ll now = min(255LL, max(0LL, prev + c[i]));
    ll diff = x[index] - now;
    dfs(index + 1, score + diff * diff, now);
  }
}

int main() {
  while (scanf("%d%d", &n, &m) && n) {
    rep(m) scanf("%lld", &c[i]);
    rep(n) scanf("%lld", &x[i]);
    dfs(0, 0LL, 128LL);
    printf("%lld\\n", ans);
  }
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int c[16], x[20001];
int dp[20001][256];
int d(int a) {
  if (a < 0)
    return 0;
  if (a > 255)
    return 255;
  return a;
}
int diff(int i, int a) { return (x[i] - a) * (x[i] - a); }
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, m) scanf("%d", &c[i]);
    rep(i, n) scanf("%d", &x[i]);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][128] = 0;
    rep(i, n) rep(j, 256) {
      if (dp[i][j] == INF)
        continue;
      rep(k, m) {
        int a = d(j + c[k]);
        dp[i + 1][a] = min(dp[i + 1][a], dp[i][j] + diff(i, a));
      }
    }
    int Min = INF;
    rep(j, 256) Min = min(Min, dp[n][j]);
    printf("%d\\n", Min);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int c[16], x[16];
int dp[40001][256];
int d(int a) {
  if (a < 0)
    return 0;
  if (a > 255)
    return 255;
  return a;
}
int diff(int i, int a) { return (x[i] - a) * (x[i] - a); }
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, m) scanf("%d", &c[i]);
    rep(i, n) scanf("%d", &x[i]);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][128] = 0;
    rep(i, n) rep(j, 256) {
      if (dp[i][j] == INF)
        continue;
      rep(k, m) {
        int a = d(j + c[k]);
        if (i + 1 >= 20001)
          return 0;
        dp[i + 1][a] = min(dp[i + 1][a], dp[i][j] + diff(i, a));
      }
    }
    int Min = INF;
    rep(j, 256) Min = min(Min, dp[n][j]);
    printf("%d\\n", Min);
  }
}
Problem p01316 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(j, n) for (int(j) = 0; j < (int)(n); ++j)
#define FOR(j, k, n) for (int(j) = (k); j < (int)(n); ++j)
#define FOREQ(j, k, n) for (int(j) = (k); j <= (int)(n); ++j)

#define MEMSET(z, val) memset(z, val, sizeof(z))

int n, m;
int x[30000];
int c[100];
int memo[20010][260];

inline ll square(ll x) { return x * x; }

ll calc(int depth, int color) {
  assert(0 <= color && color <= 255);
  if (memo[depth][color] != -1) {
    return memo[depth][color];
  }
  if (depth == n) {
    return memo[depth][color] = 0;
  }
  ll ret = 1LL << 60;
  REP(i, m) {
    int ncolor = color + c[i];
    ncolor = max(0, min(255, ncolor));
    int cost = square(abs(x[depth + 1] - ncolor));
    ret = min(ret, calc(depth + 1, ncolor) + cost);
  }
  return memo[depth][color] = ret;
}

int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    MEMSET(memo, -1);
    REP(i, m) { scanf("%d", &c[i]); }
    REP(i, n) { scanf("%d", &x[i + 1]); }
    printf("%lld\\n", calc(0, 128));
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(j, n) for (int(j) = 0; j < (int)(n); ++j)
#define FOR(j, k, n) for (int(j) = (k); j < (int)(n); ++j)
#define FOREQ(j, k, n) for (int(j) = (k); j <= (int)(n); ++j)

#define MEMSET(z, val) memset(z, val, sizeof(z))

int n, m;
int x[30000];
int c[100];
int memo[30000][300];

inline ll square(ll x) { return x * x; }

ll calc(int depth, int color) {
  assert(0 <= color && color <= 255);
  if (memo[depth][color] != -1) {
    return memo[depth][color];
  }
  if (depth == n) {
    return memo[depth][color] = 0;
  }
  ll ret = 1LL << 60;
  REP(i, m) {
    int ncolor = color + c[i];
    ncolor = max(0, min(255, ncolor));
    int cost = square(abs(x[depth + 1] - ncolor));
    ret = min(ret, calc(depth + 1, ncolor) + cost);
  }
  return memo[depth][color] = ret;
}

int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    MEMSET(memo, -1);
    REP(i, m) { scanf("%d", &c[i]); }
    REP(i, n) { scanf("%d", &x[i + 1]); }
    printf("%lld\\n", calc(0, 128));
  }
}
Problem p01316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 2e9;
double EPS = 1e-10;
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    int a[n], b[m];
    ll dp[2][256];
    REP(i, m) cin >> b[i];
    REP(i, n) cin >> a[i];
    memset(dp, -1, sizeof(dp));
    dp[0][128] = 0;
    REP(i, n) {
      int l = i & 1;
      REP(j, 256) dp[l ^ 1][j] = -1;
      REP(j, 256) {
        if (dp[l][j] == -1)
          continue;
        REP(k, m) {
          int nxt = j + b[k];
          nxt = max(0, min(255, nxt));
          if (dp[l ^ 1][nxt] == -1)
            dp[l ^ 1][nxt] = dp[l][j] + (a[i] - nxt) * (a[i] - nxt);
          else
            dp[l ^ 1][nxt] =
                min(dp[l ^ 1][nxt], dp[l][j] + (a[i] - nxt) * (a[i] - nxt));
        }
      }
    }
    ll ans = -1;
    REP(i, 256) if (dp[n & 1][i] != -1) {
      if (ans == -1)
        ans = dp[n & 1][i];
      else
        ans = min(ans, dp[n & 1][i]);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 2e9;
double EPS = 1e-10;
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    int a[n], b[m];
    int dp[n + 1][256];
    REP(i, m) cin >> b[i];
    REP(i, n) cin >> a[i];
    REP(i, n + 1) REP(j, 256) dp[i][j] = -1;
    dp[0][128] = 0;
    REP(i, n) {
      REP(j, 256) {
        if (dp[i][j] == -1)
          continue;
        REP(k, m) {
          int nxt = j + b[k];
          nxt = max(0, min(255, nxt));
          if (dp[i + 1][nxt] == -1)
            dp[i + 1][nxt] = dp[i][j] + (a[i] - nxt) * (a[i] - nxt);
          else
            dp[i + 1][nxt] =
                min(dp[i + 1][nxt], dp[i][j] + (a[i] - nxt) * (a[i] - nxt));
        }
      }
    }
    int ans = -1;
    REP(i, 256) if (dp[n][i] != -1) {
      if (ans == -1)
        ans = dp[n][i];
      else
        ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}
Problem p01316 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

const long long inf = 1LL << 60;
long long memo[256], memo2[256];

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

    for (int j = 0; j < 256; j++)
      memo[j] = memo2[j] = inf;

    memo[128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        if (memo[j] == inf)
          continue;
        for (int k = 0; k < m; k++) {
          int next = min(255, max(0, j + c[k]));
          memo2[next] =
              min(memo2[next], memo[j] + (next - x[i]) * (next - x[i]));
        }
      }
      for (int j = 0; j < 256; j++)
        memo[j] = memo2[j], memo2[j] = inf;
    }

    long long emin = inf;
    for (int j = 0; j < 256; j++)
      emin = min(emin, memo[j]);
    printf("%lld\\n", emin);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

const long long inf = 1LL << 60;
long long memo[20000][256];

int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int c[16];
    static int x[20001];
    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 <= n; i++)
      for (int j = 0; j < 256; j++)
        memo[i][j] = inf;

    memo[0][128] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        if (memo[i - 1][j] == inf)
          continue;
        for (int k = 0; k < m; k++) {
          int next = min(255, max(0, j + c[k]));
          memo[i][next] =
              min(memo[i][next],
                  memo[i - 1][j] + (next - x[i - 1]) * (next - x[i - 1]));
        }
      }
    }

    long long emin = inf;
    for (int j = 0; j < 256; j++)
      emin = min(emin, memo[n][j]);
    printf("%lld\\n", emin);
  }

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

#include <iostream>

using namespace std;

long long int ans;

int min(int a, int b) {
  if (a >= b)
    return b;
  else
    return a;
}

int main() {

  int n, m;
  long long int dp[256];
  long long int nextDp[256];
  int next;

  while (cin >> n >> m) {
    ans = 1000000000000;
    if (n == 0 && m == 0) {
      break;
    }
    int *c;
    int *x;
    c = new int[m];
    x = new int[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 < 256; i++) {
      dp[i] = ans;
      nextDp[i] = ans;
    }

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

    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

long long int ans;

int min(int a, int b) {
  if (a >= b)
    return b;
  else
    return a;
}

int main() {

  int n, m;
  long long int dp[20010][256];
  int next;

  while (cin >> n >> m) {
    ans = 1000000000000;
    if (n == 0 && m == 0) {
      break;
    }
    int *c;
    int *x;
    c = new int[m];
    x = new int[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] = 10000000000;
      }
    }

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

    cout << ans << endl;
  }

  return 0;
}