prithivMLmods/Coder-Stat huggingface dataset:
Problem p01294 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int n, d, m[110], s[110], x;

int main() {
  while (cin >> n >> d && n) {
    vi c[110];
    fill(s, s + n, 0);
    rep(i, n) {
      cin >> m[i];
      rep(j, m[i]) cin >> x, c[i].pb(x), s[i] += x;
    }
    int f;
    while (1) {
      f = 0;
      rep(i, n) {
        s[i] -= c[i][m[i] - 1];
        if (*max_element(s, s + n) - *min_element(s, s + n) <= d) {
          c[i].erase(c[i].end() - 1), m[i]--, f = 1;
        } else
          s[i] += c[i][m[i] - 1];
      }
      if (!f || !accumulate(m, m + n, 0))
        break;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int n, d, m[110], s[110], x;

int main() {
  while (cin >> n >> d && n) {
    vi c[110];
    fill(s, s + n, 0);
    rep(i, n) {
      cin >> m[i];
      rep(j, m[i]) cin >> x, c[i].pb(x), s[i] += x;
    }
    int f;
    while (1) {
      f = 0;
      rep(i, n) if (m[i]) {
        s[i] -= c[i][m[i] - 1];
        if (*max_element(s, s + n) - *min_element(s, s + n) <= d) {
          c[i].erase(c[i].end() - 1), m[i]--, f = 1;
        } else
          s[i] += c[i][m[i] - 1];
      }
      if (!f || !accumulate(m, m + n, 0))
        break;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}
Problem p01295 (C++)

Buggy submission (status: Runtime Error):
#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 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> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll solve(ll n) {
  ll t = 1, d = 1, cnt = 0;
  while (t * 10 < n) {
    cnt += (t * 10 - t) * d;
    t *= 10;
    d++;
  }
  cnt += d * (n - t);
  return cnt;
}
int main() {
  int n, k;
  while (cin >> n >> k, n) {
    ll lb = 0, ub = INT_MAX;
    ll cnt;
    while (ub - lb > 1) {
      ll mid = (lb + ub) / 2;
      cnt = solve(mid);
      if (cnt >= n)
        ub = mid;
      else
        lb = mid;
    }
    string s;
    REP(i, 100) s += to_string(lb + i);
    cout << s.substr(n - cnt - 1, k) << endl;
  }
  return 0;
}

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 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> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll solve(ll n) {
  ll t = 1, d = 1, cnt = 0;
  while (t * 10 < n) {
    cnt += (t * 10 - t) * d;
    t *= 10;
    d++;
  }
  cnt += d * (n - t);
  return cnt;
}
int main() {
  int n, k;
  while (cin >> n >> k, n) {
    ll lb = 0, ub = INT_MAX;
    ll cnt;
    while (ub - lb > 1) {
      ll mid = (lb + ub) / 2;
      cnt = solve(mid);
      if (cnt >= n)
        ub = mid;
      else
        lb = mid;
    }
    string s;
    cnt = solve(lb);
    REP(i, 100) s += to_string(lb + i);
    cout << s.substr(n - cnt - 1, k) << endl;
  }
  return 0;
}
Problem p01295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll len(ll x) {
  ll a = 1, k = 0, j = 1;
  while (a * 10 <= x)
    k += 9 * a * j, a *= 10, j++;
  k += (x - a + 1) * j;
  return k;
}
int main() {
  ll n, k;
  while (cin >> n >> k, n || k) {
    ll l = 0, r = 100000000LL, m, i;
    string s;
    while (l + 1 < r)
      (len(m = (l + r) / 2) < n) ? l = m : r = m;
    for (i = 1; i < 10000; i++)
      s += to_string(l + i);
    for (i = 0; i < k; i++)
      cout << s[n - len(l) - 1 + i];
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll len(ll x) {
  ll a = 1, k = 0, j = 1;
  while (a * 10 <= x)
    k += 9 * a * j, a *= 10, j++;
  k += (x - a + 1) * j;
  return k;
}
int main() {
  ll n, k;
  while (cin >> n >> k, n || k) {
    ll l = 0, r = 1000000000LL, m, i;
    string s;
    while (l + 1 < r)
      (len(m = (l + r) / 2) < n) ? l = m : r = m;
    for (i = 1; i < 10000; i++)
      s += to_string(l + i);
    for (i = 0; i < k; i++)
      cout << s[n - len(l) - 1 + i];
    cout << endl;
  }
  return 0;
}
Problem p01295 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  long long int N, K;
  while (1) {
    scanf("%lld%lld", &N, &K);
    long long int base = 9;
    for (int d = 1; d < 10; d++) {
      long long int sum = d * base;
      if (N > sum) {
        N -= sum;
        base *= 10;
      } else {
        long long int div = (N - 1) / d + base / 9, mo = (N - 1 + d) % d;
        string s = "";
        for (int i = 0; s.length() < K + 10; i++) {
          s += to_string(div + i);
        }
        cout << s.substr(mo, K) << endl;
        break;
      }
    }
  }
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  long long int N, K;
  while (1) {
    scanf("%lld%lld", &N, &K);
    if (!N)
      break;
    long long int base = 9;
    for (int d = 1; d < 10; d++) {
      long long int sum = d * base;
      if (N > sum) {
        N -= sum;
        base *= 10;
      } else {
        long long int div = (N - 1) / d + base / 9, mo = (N - 1 + d) % d;
        string s = "";
        for (int i = 0; s.length() < K + 10; i++) {
          s += to_string(div + i);
        }
        cout << s.substr(mo, K) << endl;
        break;
      }
    }
  }
}
Problem p01295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, K;

void solve() {
  N--;
  int d = 1, t = 1;
  while (true) {
    if (N < t * 9)
      break;
    N -= t * 9 * d;
    d++;
    t *= 10;
  }

  int tmp = N / d;
  N %= d;
  t += tmp;
  stringstream ss;
  while (ss.str().size() < 120) {
    ss << t;
    t++;
  }
  string s = ss.str();
  cout << s.substr(N, K) << endl;
}

signed main() {
  while (cin >> N >> K, N || K)
    solve();
  return 0;
}

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, K;

void solve() {
  N--;
  int d = 1, t = 1;
  while (true) {
    if (N < t * 9 * d)
      break;
    N -= t * 9 * d;
    d++;
    t *= 10;
  }

  int tmp = N / d;
  N %= d;
  t += tmp;
  stringstream ss;
  while (ss.str().size() < 120) {
    ss << t;
    t++;
  }
  string s = ss.str();
  cout << s.substr(N, K) << endl;
}

signed main() {
  while (cin >> N >> K, N || K)
    solve();
  return 0;
}
Problem p01295 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>

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

using namespace std;

typedef long long ll;

int main() {
  ll order[9];
  rep(i, 9) order[i] = (i + 1) * 9 * (ll)pow(10., i);

  for (int n, k; scanf("%d%d", &n, &k), n;) {
    int p;
    for (p = 0; p < 9; p++) {
      if (n <= order[p])
        break;
      n -= order[p];
    }

    int startnum = (int)pow(10., p) + (n - 1) / (p + 1);
    int startdgt = (n - 1) % (p + 1);
    char ans[128] = "", tmp[128];
    sprintf(tmp, "%d", startnum);
    strcat(ans, tmp + startdgt);
    for (int i = 0, j = startnum + 1; i < k; i += p + 1, j++) {
      sprintf(tmp, "%d", j);
      strcat(ans, tmp);
    }
    ans[k] = '\0';
    puts(ans);
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>

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

using namespace std;

typedef long long ll;

int main() {
  ll order[9];
  rep(i, 9) order[i] = (i + 1) * 9 * (ll)pow(10., i);

  for (int n, k; scanf("%d%d", &n, &k), n;) {
    int p;
    for (p = 0; p < 9; p++) {
      if (n <= order[p])
        break;
      n -= order[p];
    }

    int startnum = (int)pow(10., p) + (n - 1) / (p + 1);
    int startdgt = (n - 1) % (p + 1);
    char ans[1024] = "", tmp[1024];
    sprintf(tmp, "%d", startnum);
    strcat(ans, tmp + startdgt);
    for (int i = 0, j = startnum + 1; i < k; i += p + 1, j++) {
      sprintf(tmp, "%d", j);
      strcat(ans, tmp);
    }
    ans[k] = '\0';
    puts(ans);
  }

  return 0;
}
Problem p01296 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
char c;
int x[20001][2], y[20001][2];
vector<int> v[40001];
int b[40001], f, n;
void dfs(int p, int col) {
  b[p] = col;
  r(i, v[p].size()) {
    if (!(p == v[p][i] + n || p + n == v[p][i]) && b[v[p][i]] &&
        b[v[p][i]] != col)
      f++;
    else if (!b[v[p][i]]) {
      if (p == v[p][i] + n || p + n == v[p][i]) {
        if (col == 1)
          dfs(v[p][i], 2);
        else
          dfs(v[p][i], 1);
      } else
        dfs(v[p][i], col);
    }
  }
}
int main() {
  while (cin >> n, n) {
    memset(b, 0, sizeof(b));
    r(i, 40001) v[i].clear();
    r(i, n) {
      cin >> x[i][0] >> y[i][0] >> c;
      if (c == 'x')
        x[i][1] = x[i][0] + 1, y[i][1] = y[i][0];
      else
        x[i][1] = x[i][0], y[i][1] = y[i][0] + 1;
    }
    r(i, n) v[i].push_back(i + n), v[i + n].push_back(i);
    r(i, n) r(j, n) if (i != j && i < j) r(l, 2) r(k, 2) {
      if (abs(x[i][l] - x[j][k]) + abs(y[i][l] - y[j][k]) == 1) {
        int p1 = i, p2 = j;
        if (l)
          p1 += n;
        if (k)
          p2 += n;
        v[p1].push_back(p2);
        v[p2].push_back(p1);
      } else if (abs(x[i][l] - x[j][k]) == 3 && abs(y[i][l] - y[j][k]) == 0) {
        int p1 = i, p2 = j;
        if (l)
          p1 += n;
        if (k)
          p2 += n;
        v[p1].push_back(p2);
        v[p2].push_back(p1);
      }
      if (abs(x[i][l] - x[j][k]) == 0 && abs(y[i][l] - y[j][k]) == 3) {
        int p1 = i, p2 = j;
        if (l)
          p1 += n;
        if (k)
          p2 += n;
        v[p1].push_back(p2);
        v[p2].push_back(p1);
      }
    }
    f = 0;
    r(i, n * 2) if (!b[i]) dfs(i, 1);
    cout << (f ? "No" : "Yes") << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
char c;
int x[20001][2], y[20001][2];
vector<int> v[40001];
int b[40001], f, n;
void dfs(int p, int col) {
  b[p] = col;
  r(i, v[p].size()) {
    if (!(p == v[p][i] + n || p + n == v[p][i]) && b[v[p][i]] &&
        b[v[p][i]] != col)
      f++;
    else if (!b[v[p][i]]) {
      if (p == v[p][i] + n || p + n == v[p][i]) {
        if (col == 1)
          dfs(v[p][i], 2);
        else
          dfs(v[p][i], 1);
      } else
        dfs(v[p][i], col);
    }
  }
}
int main() {
  while (cin >> n, n) {
    memset(b, 0, sizeof(b));
    r(i, 40001) v[i].clear();
    r(i, n) {
      cin >> x[i][0] >> y[i][0] >> c;
      if (c == 'x')
        x[i][1] = x[i][0] + 1, y[i][1] = y[i][0];
      else
        x[i][1] = x[i][0], y[i][1] = y[i][0] + 1;
    }
    r(i, n) v[i].push_back(i + n), v[i + n].push_back(i);
    r(i, n) r(j, n) if (i != j && i < j) r(l, 2) r(k, 2) {
      if (abs(x[i][l] - x[j][k]) + abs(y[i][l] - y[j][k]) == 1) {
        int p1 = i, p2 = j;
        if (l)
          p1 += n;
        if (k)
          p2 += n;
        v[p1].push_back(p2);
        v[p2].push_back(p1);
      }
    }
    f = 0;
    r(i, n * 2) if (!b[i]) dfs(i, 1);
    cout << (f ? "No" : "Yes") << endl;
  }
}
Problem p01298 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

const ld day = 86400;

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, l;
  while (cin >> n >> l, n) {
    vector<pair<pii, int>> v;
    vi use(day);
    REP(i, n) {
      int s, t, u;
      cin >> s >> t >> u;
      FOR(j, s, t) use[j] = u;
    }
    ld lb = 0, ub = 1000000;
    REP(bs, 200) {
      ld mid = (lb + ub) / 2.0;
      ld vol = l;
      ld tmp;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      tmp = vol;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      if (tmp > vol)
        lb = mid;
      else
        ub = mid;
    end:;
    }
    cout << lb << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

const ld day = 86400;

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, l;
  while (cin >> n >> l, n) {
    vector<pair<pii, int>> v;
    vi use(day);
    REP(i, n) {
      int s, t, u;
      cin >> s >> t >> u;
      FOR(j, s, t) use[j] = u;
    }
    ld lb = 0, ub = 1000000;
    REP(bs, 150) {
      ld mid = (lb + ub) / 2.0;
      ld vol = l;
      ld tmp;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      tmp = vol;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      if (tmp > vol)
        lb = mid;
      else
        ub = mid;
    end:;
    }
    cout << lb << endl;
  }
  return 0;
}
Problem p01298 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int MOD = 1e9 + 7;

vector<double> cap(200000, 0);

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

  int n;
  while (cin >> n, n) {
    double l;
    cin >> l;
    double low = 0, high = 2000000;
    vector<double> v(200000, 0);
    for (int i = 0; i < n; i++) {
      int st, gt;
      cin >> st >> gt;
      double u;
      cin >> u;
      for (int j = st + 1; j <= gt; j++) {
        v[j] = v[j + 86400] = u;
      }
      for (int k = 0; k < 50; k++) {
        cap[0] = l;
        double mid = (low + high) / 2;
        bool f = true;
        for (int i = 1; i <= 86400 * 2; i++) {
          cap[i] = min(l, cap[i - 1] + (mid - v[i]));
          if (cap[i] < 0)
            f = false;
        }
        if (cap[86400] <= cap[86400 * 2] && f) {
          high = mid;
        } else {
          low = mid;
        }
      }
    }
    cout << fixed << setprecision(10) << low << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int MOD = 1e9 + 7;

vector<double> cap(200000, 0);

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

  int n;
  while (cin >> n, n) {
    double l;
    cin >> l;
    double low = 0, high = 2000000;
    vector<double> v(200000, 0);
    for (int i = 0; i < n; i++) {
      int st, gt;
      cin >> st >> gt;
      double u;
      cin >> u;
      for (int j = st + 1; j <= gt; j++) {
        v[j] = v[j + 86400] = u;
      }
    }
    for (int k = 0; k < 100; k++) {
      cap[0] = l;
      double mid = (low + high) / 2;
      bool f = true;
      for (int i = 1; i <= 86400 * 2; i++) {
        cap[i] = min(l, cap[i - 1] + (mid - v[i]));
        if (cap[i] < 0)
          f = false;
      }
      if (cap[86400] <= cap[86400 * 2] && f) {
        high = mid;
      } else {
        low = mid;
      }
    }
    cout << fixed << setprecision(10) << low << endl;
  }
}
Problem p01298 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) ((abs((a) - (b))) < EPS)

int a[100001];
int n, l;

bool check(double f) {
  double lf = l;
  double rec = 0;
  bool flag = false;
  for (int j = 0; j < 3; j++) {
    for (int i = 1; i <= 86400; i++) {
      if (j == 1 && a[i] != 0 && !flag) {
        rec = lf;
        flag = true;
      } else if (j == 2 && a[i] != 0) {
        if (EQ(rec, lf))
          return true;
        return false;
      }
      double b = -a[i] + f;
      lf += b;
      if (EQ(lf, 0) || lf < 0)
        return false;
      lf = min(lf, 1.0 * l);
    }
  }
  return true;
}

int main() {

  while (cin >> n >> l && !(n == 0 && l == 0)) {
    memset(a, 0, sizeof(a));
    int s, t, u;
    for (int j = 0; j < n; j++) {
      cin >> s >> t >> u;
      for (int i = s + 1; i <= t; i++)
        a[i] = u;
    }
    double ub = 10000000;
    double lb = 0;
    int cnt = 100;
    while (cnt--) {
      double mid = (ub + lb) / 2;
      if (check(mid))
        ub = mid;
      else
        lb = mid;
    }
    printf("%.10f\n", ub);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) ((abs((a) - (b))) < EPS)

int a[100001];
int n, l;

bool check(double f) {
  double lf = l;
  double rec = 0;
  bool flag = false;
  for (int j = 0; j < 3; j++) {
    for (int i = 1; i <= 86400; i++) {
      if (j == 1 && a[i] != 0 && !flag) {
        rec = lf;
        flag = true;
      } else if (j == 2 && a[i] != 0) {
        if (EQ(rec, lf))
          return true;
        return false;
      }
      double b = -a[i] + f;
      lf += b;
      if (EQ(lf, 0) || lf < 0)
        return false;
      lf = min(lf, 1.0 * l);
    }
  }
  return true;
}

int main() {

  while (cin >> n >> l && !(n == 0 && l == 0)) {
    memset(a, 0, sizeof(a));
    int s, t, u;
    for (int j = 0; j < n; j++) {
      cin >> s >> t >> u;
      for (int i = s + 1; i <= t; i++)
        a[i] = u;
    }
    double ub = 10000000;
    double lb = 0;
    int cnt = 80;
    while (cnt--) {
      double mid = (ub + lb) / 2;
      if (check(mid))
        ub = mid;
      else
        lb = mid;
    }
    printf("%.10f\n", ub);
  }
  return 0;
}
Problem p01300 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

string S;
int dp[80000][11];

int rec(int idx, int mod) {
  if (idx == S.size())
    return (mod == 0);
  if (~dp[idx][mod])
    return (dp[idx][mod]);
  return (rec(idx + 1, (mod * 10 + S[idx] - '0') % 11) + (mod == 0));
}

int main() {
  while (cin >> S, S != "0") {
    memset(dp, -1, sizeof(dp));
    int ret = 0;
    for (int i = 0; i < S.size(); i++) {
      if (S[i] != '0')
        ret += rec(i + 1, S[i] - '0');
    }
    cout << ret << endl;
  }
}

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

string S;
int dp[80000][11];

int rec(int idx, int mod) {
  if (idx == S.size())
    return (mod == 0);
  if (~dp[idx][mod])
    return (dp[idx][mod]);
  return (dp[idx][mod] =
              rec(idx + 1, (mod * 10 + S[idx] - '0') % 11) + (mod == 0));
}

int main() {
  while (cin >> S, S != "0") {
    memset(dp, -1, sizeof(dp));
    int ret = 0;
    for (int i = 0; i < S.size(); i++) {
      if (S[i] != '0')
        ret += rec(i + 1, S[i] - '0');
    }
    cout << ret << endl;
  }
}
Problem p01300 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdlib.h>

int main() {
  while (1) {
    // 入力
    std::string number;
    std::cin >> number;
    // 演算
    if (number == "0")
      return 0;
    int N = number.length();
    int dp[801][11];
    int answer = 0;
    for (int i = 0; i < 11; i++) {
      dp[0][i] = 0;
    }
    for (int i = 0; i < N; i++) {
      int digit = number.at(i) - '0';
      for (int j = 0; j < 11; j++) {
        dp[i + 1][(j * 10 + digit) % 11] = dp[i][j];
      }
      if (digit != 0) {
        dp[i + 1][digit]++;
      }
      /* for (int j = 0; j < 11; j++) {
              std::cout << "dp[" << i + 1 << "][" << j << "]: " << dp[i + 1][j]
      << std::endl;
      } */
      answer += dp[i + 1][0];
    }
    // 出力
    /* for (int i = 0; i < 11; i++) {
            std::cout << "dp[" << N << "][" << i << "]: " << dp[N][i] <<
    std::endl;
    } */
    std::cout << answer << std::endl;
  }
}

Fixed submission:
#include <iostream>
#include <stdlib.h>

int main() {
  while (1) {
    // 入力
    std::string number;
    std::cin >> number;
    // 演算
    if (number == "0")
      return 0;
    int N = number.length();
    int dp[80001][11];
    int answer = 0;
    for (int i = 0; i < 11; i++) {
      dp[0][i] = 0;
    }
    for (int i = 0; i < N; i++) {
      int digit = number.at(i) - '0';
      for (int j = 0; j < 11; j++) {
        dp[i + 1][(j * 10 + digit) % 11] = dp[i][j];
      }
      if (digit != 0) {
        dp[i + 1][digit]++;
      }
      /* for (int j = 0; j < 11; j++) {
              std::cout << "dp[" << i + 1 << "][" << j << "]: " << dp[i + 1][j]
      << std::endl;
      } */
      answer += dp[i + 1][0];
    }
    // 出力
    /* for (int i = 0; i < 11; i++) {
            std::cout << "dp[" << N << "][" << i << "]: " << dp[N][i] <<
    std::endl;
    } */
    std::cout << answer << std::endl;
  }
}
Problem p01304 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int mt[16][16][16][16];
int dp[16][16];
int i, j, k, l, m, n, o, a, b, c, d, cnt, X, Y;
void init() {
  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      dp[i][j] = 0;
      for (int k = 0; k < 16; k++) {
        for (int l = 0; l < 16; l++) {
          mt[i][j][k][l] = 0;
        }
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int data = 0; data < n; data++) {
    init();
    cin >> X >> Y >> m;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c >> d;
      mt[a][b][c][d] = 1;
      mt[c][d][a][b] = 1;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= X; i++) {
      if (mt[i][0][i - 1][0] == 1) {
        dp[i][0] = 0;
      } else {
        dp[i][0] = dp[i - 1][0];
      }
    }

    for (int i = 1; i <= Y; i++) {

      if (mt[0][i][0][i - 1]) {
        dp[0][i] = 0;
      } else {
        dp[0][i] = dp[0][i - 1];
      }

      for (int j = 1; j <= X; j++) {
        if (mt[j][i][j - 1][i] == 0) {
          dp[j][i] += dp[j - 1][i];
        }
        if (mt[j][i][j][i - 1] == 0) {
          dp[j][i] += dp[j][i - 1];
        }
      }
    }
    if (dp[X][Y] == 0) {
      cout << "Miserable Hokusai!" << endl;
    } else {
      cout << dp[X][Y] << endl;
    }
  }
  return n;
}

Fixed submission:
#include <iostream>
using namespace std;
int mt[16][16][16][16];
int dp[16][16];
int i, j, k, l, m, n, o, a, b, c, d, cnt, X, Y;
void init() {
  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      dp[i][j] = 0;
      for (int k = 0; k < 16; k++) {
        for (int l = 0; l < 16; l++) {
          mt[i][j][k][l] = 0;
        }
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int data = 0; data < n; data++) {
    init();
    cin >> X >> Y >> m;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c >> d;
      mt[a][b][c][d] = 1;
      mt[c][d][a][b] = 1;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= X; i++) {
      if (mt[i][0][i - 1][0] == 1) {
        dp[i][0] = 0;
      } else {
        dp[i][0] = dp[i - 1][0];
      }
    }

    for (int i = 1; i <= Y; i++) {

      if (mt[0][i][0][i - 1]) {
        dp[0][i] = 0;
      } else {
        dp[0][i] = dp[0][i - 1];
      }

      for (int j = 1; j <= X; j++) {
        if (mt[j][i][j - 1][i] == 0) {
          dp[j][i] += dp[j - 1][i];
        }
        if (mt[j][i][j][i - 1] == 0) {
          dp[j][i] += dp[j][i - 1];
        }
      }
    }
    if (dp[X][Y] == 0) {
      cout << "Miserable Hokusai!" << endl;
    } else {
      cout << dp[X][Y] << endl;
    }
  }
  return 0;
}
Problem p01314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int l = 1;
    int r = 1;

    int sum = 0;
    int cnt = 0;
    while (true) {
      if (r >= n)
        break;

      while (sum > n) {
        sum -= l;
        l++;
        if (sum == n)
          cnt++;
      }

      sum += r;
      r++;
      if (sum == n)
        cnt++;
      cerr << sum << " " << l << " " << r << endl;
    }
    cout << cnt << endl;
  }

  return 0;
}

// EOF

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int l = 1;
    int r = 1;

    int sum = 0;
    int cnt = 0;
    while (true) {
      if (r >= n)
        break;

      while (sum > n) {
        sum -= l;
        l++;
        if (sum == n)
          cnt++;
      }

      sum += r;
      r++;
      if (sum == n)
        cnt++;
      // cerr << sum << " " << l << " " << r << endl;
    }
    cout << cnt << endl;
  }

  return 0;
}

// EOF
Problem p01314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int n, x;
  while (true) {
    scanf("%d", &n);
    x = 0;
    if (n == 0)
      break;
    for (int i = 2; i <= n; i++) {
      for (int j = 1; j < n; j++) {
        int count = 0;
        for (int k = 0; k < i; k++)
          count += j + k;
        if (count == n)
          x++;
      }
    }
    printf("%d\n", x);
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int n, x;
  while (true) {
    scanf("%d", &n);
    x = 0;
    if (n == 0)
      break;
    for (int i = 2; i <= n; i++) {
      for (int j = 1; j < n; j++) {
        int count = 0;
        for (int k = j; k < j + i; k++)
          count += k;
        if (count == n)
          x++;
      }
    }
    printf("%d\n", x);
  }
  return 0;
}
Problem p01314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main() {
  int n, sum, ans, left, right;
  while (cin >> n) {
    if (!n)
      break;
    left = 1;
    right = 2;
    ans = 0;
    sum = left + right;
    while (n - 1 != left) {
      if (n == sum)
        ans++;
      if (sum <= n && right < n) {
        right++;
        sum += right;
      } else if (right - left < 2 && right < n) {
        right++;
        sum += right;
      } else if (sum > n && right - left >= 2) {
        sum -= left;
        left++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main() {
  int n, sum, ans, left, right;
  while (cin >> n) {
    if (!n)
      break;
    left = 1;
    right = 2;
    ans = 0;
    sum = left + right;
    if (n < 3) {
      cout << 0 << endl;
      continue;
    }
    while (n - 1 > left) {
      if (n == sum)
        ans++;
      if (sum <= n && right < n) {
        right++;
        sum += right;
      } else if (right - left < 2 && right < n) {
        right++;
        sum += right;
      } else if (sum > n && right - left >= 2) {
        sum -= left;
        left++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01314 (C++)

Buggy submission (status: Time Limit Exceeded):
// 52
#include <iostream>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int ans = 0;
    for (int i = 2; i <= 1000; i++) {
      for (int j = 1; j <= 1000; j++) {
        int s = 0;
        for (int k = 0; k < i; k++) {
          s += j + k;
        }
        ans += s == n;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
// 52
#include <iostream>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int ans = 0;
    for (int i = 1; i <= 1000; i++) {
      for (int j = 2; j <= 1000; j++) {
        ans += n == i * j + j * (j - 1) / 2;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01315 (C++)

Buggy submission (status: 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;
          }
          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;
}

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;
            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;
}
Problem p01315 (C++)

Buggy submission (status: 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 ((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;
}

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 (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;
}
Problem p01315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct cmp {
  bool operator()(const pair<double, string> &lhs,
                  const pair<double, string> &rhs) const {
    return greater<double>()(lhs.first, rhs.first)
               ? true
               : less<string>()(lhs.second, rhs.second);
  }
};

int main() {
  int N;
  while (cin >> N && N != 0) {
    vector<pair<double, string>> v(N);
    for (int i = 0; i < N; i++) {
      string L;
      int P, A, B, C, D, E, F, S, M;
      cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M;
      int t = A + B + C + (D + E) * M;
      int c = F * S * M;
      double e = double(c - P) / t;
      v[i] = make_pair(e, L);
    }
    sort(v.begin(), v.end(), cmp());
    for (int i = 0; i < N; i++) {
      cout << v[i].second << endl;
    }
    cout << '#' << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct cmp {
  bool operator()(const pair<double, string> &lhs,
                  const pair<double, string> &rhs) const {
    return lhs.first == rhs.first ? lhs.second < rhs.second
                                  : lhs.first > rhs.first;
  }
};

int main() {
  int N;
  while (cin >> N && N != 0) {
    vector<pair<double, string>> v(N);
    for (int i = 0; i < N; i++) {
      string L;
      int P, A, B, C, D, E, F, S, M;
      cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M;
      int t = A + B + C + (D + E) * M;
      int c = F * S * M;
      double e = double(c - P) / t;
      v[i] = make_pair(e, L);
    }
    sort(v.begin(), v.end(), cmp());
    for (int i = 0; i < N; i++) {
      cout << v[i].second << endl;
    }
    cout << '#' << endl;
  }
  return 0;
}
Problem p01316 (C++)

Buggy submission (status: Runtime Error):
#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;
  }
}

Fixed submission:
#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;
  }
}
Problem p01316 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p01316 (C++)

Buggy submission (status: 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[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;
}


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[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;
}