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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vec vector
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

using namespace std;

int main() {
  vector<int> v, oddv;
  for (ll i = 1;; i++) {
    if (i * (i + 1) * (i + 2) / 6 <= 1000000) {
      v.pb(i * (i + 1) * (i + 2) / 6);
    } else
      break;
  }
  int ans1[1000010], ans2[1000010];
  rep(i, 0, 1000010) ans1[i] = ans2[i] = INF;
  ans1[0] = 0;
  ans2[0] = 0;
  rep(i, 0, 1000000) {
    ans1[i] = i;
    ans2[i] = i;
    for (int j = 0; i >= v[j]; j++) {
      ans1[i] = min(ans1[i - v[j]] + 1, ans1[i]);
      if (v[j] % 2 == 1)
        ans2[i] = min(ans2[i], ans2[i - v[j]] + 1);
    }
  }

  int n;
  while (cin >> n && n) {
    cout << ans1[n] << " " << ans2[n] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vec vector
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

using namespace std;

int main() {
  vector<int> v, oddv;
  for (ll i = 1;; i++) {
    if (i * (i + 1) * (i + 2) / 6 <= 2000000) {
      v.pb(i * (i + 1) * (i + 2) / 6);
    } else
      break;
  }
  int ans1[1000010], ans2[1000010];
  rep(i, 0, 1000010) ans1[i] = ans2[i] = INF;
  ans1[0] = 0;
  ans2[0] = 0;
  rep(i, 0, 1000000) {
    ans1[i] = i;
    ans2[i] = i;
    for (int j = 0; i >= v[j]; j++) {
      ans1[i] = min(ans1[i - v[j]] + 1, ans1[i]);
      if (v[j] % 2 == 1)
        ans2[i] = min(ans2[i], ans2[i - v[j]] + 1);
    }
  }

  int n;
  while (cin >> n && n) {
    cout << ans1[n] << " " << ans2[n] << endl;
  }

  return 0;
}
Problem p00748 (C++)

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

int dp[2][1000001];

int f(int n) { return n * (n + 1) * (n + 2) / 6; }

int main() {
  int n;
  for (int k = 0; k < 2; k++)
    fill(dp[k], dp[k] + 1000001, 1000000);

  dp[0][0] = dp[1][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int k = 1;; k++) {
      int num = f(k);
      if (i - num >= 0) {
        dp[0][i] = min(dp[0][i], dp[0][i - num] + 1);
        if ((num % 2) == 1) {
          dp[1][i] = min(dp[1][i], dp[1][i - num] + 1);
        }
      } else {
        break;
      }
    }
  }

  while (cin >> n && n) {
    cout << dp[0][n] << " " << dp[1][n] << endl;
  }
}

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

int dp[2][1000001];

int f(int n) { return n * (n + 1) * (n + 2) / 6; }

int main() {
  int n;
  for (int k = 0; k < 2; k++)
    fill(dp[k], dp[k] + 1000001, 1000000);

  dp[0][0] = dp[1][0] = 0;
  for (int i = 1; i <= 1000000; i++) {
    for (int k = 1;; k++) {
      int num = f(k);
      if (i - num >= 0) {
        dp[0][i] = min(dp[0][i], dp[0][i - num] + 1);
        if ((num % 2) == 1) {
          dp[1][i] = min(dp[1][i], dp[1][i - num] + 1);
        }
      } else {
        break;
      }
    }
  }

  while (cin >> n && n) {
    cout << dp[0][n] << " " << dp[1][n] << endl;
  }
}
Problem p00748 (C++)

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

const int MAX = 100010;

vector<int> tetras;
vector<int> oddTetras;

int dp[MAX];
int oddDp[MAX];

int main() {
  for (int i = 1; i < 200; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    tetras.push_back(n);
    if (n % 2 == 1)
      oddTetras.push_back(n);
  }

  for (int i = 0; i < MAX; i++) {
    dp[i] = oddDp[i] = i;
  }
  for (int i = 1; i < MAX; i++) {
    for (int j = 0; tetras[j] <= i; j++) {
      dp[i] = min(dp[i], dp[i - tetras[j]] + 1);
    }
    for (int j = 0; oddTetras[j] <= i; j++) {
      oddDp[i] = min(oddDp[i], oddDp[i - oddTetras[j]] + 1);
    }
  }

  int n;
  while (cin >> n, n != 0) {
    cout << dp[n] << " " << oddDp[n] << endl;
  }
}

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

const int MAX = 1000100;

vector<int> tetras;
vector<int> oddTetras;

int dp[MAX];
int oddDp[MAX];

int main() {
  for (int i = 1; i < 200; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    tetras.push_back(n);
    if (n % 2 == 1)
      oddTetras.push_back(n);
  }

  for (int i = 0; i < MAX; i++) {
    dp[i] = oddDp[i] = i;
  }
  for (int i = 1; i < MAX; i++) {
    for (int j = 0; tetras[j] <= i; j++) {
      dp[i] = min(dp[i], dp[i - tetras[j]] + 1);
    }
    for (int j = 0; oddTetras[j] <= i; j++) {
      oddDp[i] = min(oddDp[i], oddDp[i - oddTetras[j]] + 1);
    }
  }

  int n;
  while (cin >> n, n != 0) {
    cout << dp[n] << " " << oddDp[n] << endl;
  }
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#define pi 3.14159
#define Inf (int)pow(2., 12.)

using namespace std;
typedef std::pair<int, int> mypair;
queue<mypair> qu;
#define M 1000000

int odd[M], all[M];

void update(int *ar, int x) {
  for (int i = x; i <= M; ++i) {
    ar[i] = min(ar[i], ar[i - x] + 1);
  }
}

int main() {
  odd[0] = all[0] = 0;
  for (int i = 1; i <= M; ++i) {
    odd[i] = all[i] = 10000000;
  }

  for (int i = 1;; ++i) {
    int x = i * (i + 1) * (i + 2) / 6;
    if (x > M)
      break;
    update(all, x);
    if (x % 2 == 1)
      update(odd, x);
  }

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << all[n] << " " << odd[n] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#define pi 3.14159
#define Inf (int)pow(2., 12.)

using namespace std;
typedef std::pair<int, int> mypair;
queue<mypair> qu;
#define M 1000000

int odd[M + 10], all[M + 10];

void update(int *ar, int x) {
  for (int i = x; i <= M; ++i) {
    ar[i] = min(ar[i], ar[i - x] + 1);
  }
}

int main() {
  odd[0] = all[0] = 0;
  for (int i = 1; i <= M; ++i) {
    odd[i] = all[i] = 10000000;
  }

  for (int i = 1;; ++i) {
    int x = i * (i + 1) * (i + 2) / 6;
    if (x > M)
      break;
    update(all, x);
    if (x % 2 == 1)
      update(odd, x);
  }

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << all[n] << " " << odd[n] << endl;
  }
}
Problem p00748 (C++)

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

using namespace std;

int all[1000001];
int odd[1000001];

int const INF = 1 << 29;

int main() {

  fill(all, all + 1000001, INF);
  fill(odd, odd + 1000001, INF);

  all[0] = odd[0] = 0;

  for (int i = 1;; i++) {
    int n = i * (i + 1) * (i + 2);
    for (int j = n; j <= 1000000; j++) {
      all[j] = min(all[j - n] + 1, all[j]);
      if (n % 2) {
        odd[j] = min(odd[j - n] + 1, odd[j]);
      }
    }
  }

  int N;
  while (cin >> N && N) {
    cout << all[N] << ' ' << odd[N] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int all[1000001];
int odd[1000001];

int const INF = 1 << 29;

int main() {

  fill(all, all + 1000001, INF);
  fill(odd, odd + 1000001, INF);

  all[0] = odd[0] = 0;

  for (int i = 1;; i++) {
    int n = i * (i + 1) * (i + 2) / 6;
    if (n > 1000000)
      break;
    for (int j = n; j <= 1000000; j++) {
      all[j] = min(all[j - n] + 1, all[j]);
      if (n % 2) {
        odd[j] = min(odd[j - n] + 1, odd[j]);
      }
    }
  }

  int N;
  while (cin >> N && N) {
    cout << all[N] << ' ' << odd[N] << endl;
  }

  return 0;
}
Problem p00748 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

int a[201];
int dp1[1000001], dp2[1000001];
int main() {
  FOR(i, 1, 201) { a[i - 1] = i * (i + 1) * (i + 2) / 6; }
  int n;
  while (cin >> n, n) {
    rep(i, n + 1) dp1[i] = dp2[i] = 1000100010;
    dp1[n] = 0, dp2[n] = 0;
    rep(i, 200) for (int j = n; j >= 0; --j) {
      if (j >= a[199 - i])
        dp1[j - a[199 - i]] = min(dp1[j - a[199 - i]], dp1[j] + 1);
      if (j >= a[199 - i] && a[199 - i] % 2 == 1)
        dp2[j - a[199 - i]] = min(dp2[j - a[199 - i]], dp2[j] + 1);
    }
    cout << dp1[0] << " " << dp2[0] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

int a[201];
int dp1[1000001], dp2[1000001];
int main() {
  FOR(i, 1, 201) { a[i - 1] = i * (i + 1) * (i + 2) / 6; }
  int n;
  while (cin >> n, n) {
    rep(i, n + 1) dp1[i] = dp2[i] = 1000100010;
    dp1[n] = 0, dp2[n] = 0;
    rep(i, 200) for (int j = n; j >= a[199 - i]; --j) {
      dp1[j - a[199 - i]] = min(dp1[j - a[199 - i]], dp1[j] + 1);
      if (a[199 - i] % 2 == 1)
        dp2[j - a[199 - i]] = min(dp2[j - a[199 - i]], dp2[j] + 1);
    }
    cout << dp1[0] << " " << dp2[0] << endl;
  }
  return 0;
}
Problem p00748 (C++)

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

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i, n) for (ll i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (ll i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b) for (ll i = (a)-1, i##_cond = (b); i >= i##_cond; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
#define SUM(a) accumulate(all(a), 0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;

const ll inf = 1ll << 62;
const ll mod = 1e9 + 7;

#define n 1000000

int main() {
#define int ll
  int tmp = 1;
  vi a, b;
  while (1) {
    int x = tmp * (tmp + 1) * (tmp + 2) / 6;
    if (x >= 1000000)
      break;
    a.pb(x);
    if (x & 1)
      b.pb(x);
    tmp++;
  }
  dump(a, b);

  vi dpa(n + 1, inf), dpb(n + 1, inf);
  dpa[0] = 0;
  dpb[0] = 0;
  rep(i, n) {
    for (int j = 0; a[j] <= i + 1; ++j)
      for (int k = 1; a[j] * k <= i + 1; ++k)
        dpa[i + 1] = min(dpa[i + 1], dpa[i + 1 - k * a[j]] + k);
    for (int j = 0; b[j] <= i + 1; ++j)
      for (int k = 1; b[j] * k <= i + 1; ++k)
        dpb[i + 1] = min(dpb[i + 1], dpb[i + 1 - k * b[j]] + k);
  }
  while (1) {
    int m;
    cin >> m;
    if (m == 0)
      break;

    cout << dpa[m] << " " << dpb[m] << endl;
  }
}


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

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i, n) for (ll i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (ll i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b) for (ll i = (a)-1, i##_cond = (b); i >= i##_cond; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
#define SUM(a) accumulate(all(a), 0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;

const ll inf = 1ll << 62;
const ll mod = 1e9 + 7;

#define n 1000000

int main() {
#define int ll
  int tmp = 1;
  vi a, b;
  while (1) {
    int x = tmp * (tmp + 1) * (tmp + 2) / 6;
    if (x >= 1000000)
      break;
    a.pb(x);
    if (x & 1)
      b.pb(x);
    tmp++;
  }
  dump(a, b);

  vi dpa(n + 1, inf), dpb(n + 1, inf);
  dpa[0] = 0;
  dpb[0] = 0;
  rep(i, n) {
    for (int j = 0; j < sz(a) and a[j] <= i + 1; ++j)
      dpa[i + 1] = min(dpa[i + 1], dpa[i + 1 - a[j]] + 1);
    for (int j = 0; j < sz(b) and b[j] <= i + 1; ++j)
      dpb[i + 1] = min(dpb[i + 1], dpb[i + 1 - b[j]] + 1);
  }
  while (1) {
    int m;
    cin >> m;
    if (m == 0)
      break;

    cout << dpa[m] << " " << dpb[m] << endl;
  }
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  const int N = 100001;
  vector<int> full(N);
  vector<int> odd(N);
  for (int i = 0; i < N; i++)
    full[i] = i;
  for (int i = 0; i < N; i++)
    odd[i] = i;
  for (int n = 2;; n++) {
    const int t = (n * (n + 1) * (n + 2)) / 6;
    if (t >= N)
      break;
    for (int i = 0; i + t < N; i++) {
      const int k = i + t;
      if (full[i] + 1 < full[k])
        full[k] = full[i] + 1;
      if (t & 1) {
        if (odd[i] + 1 < odd[k])
          odd[k] = odd[i] + 1;
      }
    }
  }

  for (int t = 1;; t++) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << full[n] << " " << odd[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  const int N = 1000001;
  vector<int> full(N);
  vector<int> odd(N);
  for (int i = 0; i < N; i++)
    full[i] = i;
  for (int i = 0; i < N; i++)
    odd[i] = i;
  for (int n = 2;; n++) {
    const int t = (n * (n + 1) * (n + 2)) / 6;
    if (t >= N)
      break;
    for (int i = 0; i + t < N; i++) {
      const int k = i + t;
      if (full[i] + 1 < full[k])
        full[k] = full[i] + 1;
      if (t & 1) {
        if (odd[i] + 1 < odd[k])
          odd[k] = odd[i] + 1;
      }
    }
  }

  for (int t = 1;; t++) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << full[n] << " " << odd[n] << endl;
  }
  return 0;
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  constexpr int N = 1e4;
  V<> a, b;
  for (int i = 1;; ++i) {
    a.push_back(i * (i + 1) * (i + 2) / 6);
    if (a.back() >= N) {
      a.pop_back();
      break;
    }
    if (a.back() & 1)
      b.push_back(a.back());
  }
  V<> p(N, -1), q(N, -1);
  p[0] = q[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : a)
      if (i + e < N and p[i + e] == -1) {
        p[i + e] = p[i] + 1;
        que.push(i + e);
      }
  }
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : b)
      if (i + e < N and q[i + e] == -1) {
        q[i + e] = q[i] + 1;
        que.push(i + e);
      }
  }
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    cout << p[n] << ' ' << q[n] << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  constexpr int N = 1e6;
  V<> a, b;
  for (int i = 1;; ++i) {
    a.push_back(i * (i + 1) * (i + 2) / 6);
    if (a.back() >= N) {
      a.pop_back();
      break;
    }
    if (a.back() & 1)
      b.push_back(a.back());
  }
  V<> p(N, -1), q(N, -1);
  p[0] = q[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : a)
      if (i + e < N and p[i + e] == -1) {
        p[i + e] = p[i] + 1;
        que.push(i + e);
      }
  }
  que.push(0);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int e : b)
      if (i + e < N and q[i + e] == -1) {
        q[i + e] = q[i] + 1;
        que.push(i + e);
      }
  }
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    cout << p[n] << ' ' << q[n] << '\n';
  }
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

const int MAX_N = 1e6;
vector<int> dp1, dp2;
vector<int> tetra;

int main(void) {
  int n = 1;
  do {
    tetra.push_back(n * (n + 1) * (n + 2) / 6);
    ++n;
  } while (tetra.back() <= MAX_N);
  dp1.push_back(0);
  dp2.push_back(0);
  for (int i = 1; i <= n; ++i) {
    int a1 = INT_MAX, a2 = INT_MAX;
    for (int t : tetra) {
      if (t > i)
        break;
      a1 = min(a1, dp1[i - t] + 1);
      if (t % 2) {
        a2 = min(a2, dp2[i - t] + 1);
      }
    }
    dp1.push_back(a1);
    dp2.push_back(a2);
  }
  int x;
  while (cin >> x, x) {
    cout << dp1[x] << " " << dp2[x] << endl;
  }
  return 0;
}

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

const int MAX_N = 1e6;
vector<int> dp1, dp2;
vector<int> tetra;

int main(void) {
  int n = 1;
  do {
    tetra.push_back(n * (n + 1) * (n + 2) / 6);
    ++n;
  } while (tetra.back() <= MAX_N);
  dp1.push_back(0);
  dp2.push_back(0);
  for (int i = 1; i <= MAX_N; ++i) {
    int a1 = INT_MAX, a2 = INT_MAX;
    for (int t : tetra) {
      if (t > i)
        break;
      a1 = min(a1, dp1[i - t] + 1);
      if (t % 2) {
        a2 = min(a2, dp2[i - t] + 1);
      }
    }
    dp1.push_back(a1);
    dp2.push_back(a2);
  }
  int x;
  while (cin >> x, x) {
    cout << dp1[x] << " " << dp2[x] << endl;
  }
  return 0;
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int dp[1000010] = {};
  int dp2[1000010] = {};
  for (int i = 1; i <= 1000000; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  vector<ll> gu;
  vector<ll> ki;
  ll cnt = 1;
  while (1) {
    if ((cnt * (cnt + 1) * (cnt + 2) / 6) > 1000000)
      break;
    if (((cnt * (cnt + 1) * (cnt + 2)) / 6) % 2) {
      ki.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[ki[ki.size() - 1]] = 1;
      dp2[gu[gu.size() - 1]] = 1;
    } else {
      gu.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[gu[gu.size() - 1]] = 1;
    }
    cnt++;
  }
  dp[1] = 1;
  dp2[1] = 1;
  for (int i = 1; i < 1000000; i++) {
    for (int j = 0; j < gu.size(); j++) {
      if (i + gu[j] > 1000000)
        break;
      dp[i + gu[j]] = min(dp[i + gu[j]], (dp[i] + 1));
    }
    for (int j = 0; j < ki.size(); j++) {
      if (i + ki[j] > 1000000)
        break;
      dp[i + ki[j]] = min(dp[i + ki[j]], (dp[i] + 1));
      dp2[i + ki[j]] = min(dp2[i + ki[j]], (dp2[i] + 1));
    }
  }
  int n;
  while (cin >> n, n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int dp[1000010] = {};
  int dp2[1000010] = {};
  for (int i = 1; i <= 1000000; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  vector<ll> gu;
  vector<ll> ki;
  ll cnt = 1;
  while (1) {
    if ((cnt * (cnt + 1) * (cnt + 2) / 6) > 1000000)
      break;
    if (((cnt * (cnt + 1) * (cnt + 2)) / 6) % 2) {
      ki.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[ki[ki.size() - 1]] = 1;
      dp2[ki[ki.size() - 1]] = 1;
    } else {
      gu.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[gu[gu.size() - 1]] = 1;
    }
    cnt++;
  }
  dp[1] = 1;
  dp2[1] = 1;
  for (int i = 1; i < 1000000; i++) {
    for (int j = 0; j < gu.size(); j++) {
      if (i + gu[j] > 1000000)
        break;
      dp[i + gu[j]] = min(dp[i + gu[j]], (dp[i] + 1));
    }
    for (int j = 0; j < ki.size(); j++) {
      if (i + ki[j] > 1000000)
        break;
      dp[i + ki[j]] = min(dp[i + ki[j]], (dp[i] + 1));
      dp2[i + ki[j]] = min(dp2[i + ki[j]], (dp2[i] + 1));
    }
  }
  int n;
  while (cin >> n, n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}
Problem p00748 (C++)

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

int main() {
  int num[1000010], num2[1000010];
  for (int i = 0; i < 1000010; i++)
    num[i] = num2[i] = 10e8;
  num[0] = num2[0] = 0;
  for (int j = 0; j < 1000010; j++) {
    for (int i = 0; i < 1500; i++) {
      int m = i * (i + 1) * (i + 2) / 6;
      if (1000010 <= m + j)
        continue;
      num[j + m] = min(num[j] + 1, num[j + m]);

      if (m == 0)
        continue;
      if (m % 2 == 0)
        continue;
      num2[j + m] = min(num2[j] + 1, num2[j + m]);
    }
  }
  int n;
  while (cin >> n, n) {
    cout << num[n] << " " << num2[n] << endl;
  }
}

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

int main() {
  int num[1000010], num2[1000010];
  for (int i = 0; i < 1000010; i++)
    num[i] = num2[i] = 10e8;
  num[0] = num2[0] = 0;
  for (int j = 0; j < 1000010; j++) {
    for (int i = 0; i < 1200; i++) {
      int m = i * (i + 1) * (i + 2) / 6;
      if (1000010 <= m + j)
        continue;
      num[j + m] = min(num[j] + 1, num[j + m]);

      if (m == 0)
        continue;
      if (m % 2 == 0)
        continue;
      num2[j + m] = min(num2[j] + 1, num2[j + m]);
    }
  }
  int n;
  while (cin >> n, n) {
    cout << num[n] << " " << num2[n] << endl;
  }
}
Problem p00748 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int dp[1000010];
int dp2[1000010];

int main() {
  int a = 1, b;
  vi v;
  vi u;
  while ((b = a * (a + 1) * (a + 2) / 6) < 1e6) {
    v.pb(b);
    if (b % 2 == 1) {
      u.pb(b);
    }
    a++;
  }
  // cout<<v.size()<<endl;
  for (int i = 0; i < 1000010; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  dp[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = v[i]; j < 1000010; j++) {
      dp[j] = min(dp[j], dp[j - v[i]] + 1);
      if (i < u.size()) {
        dp2[j] = min(dp2[j], dp2[j - u[i]] + 1);
      }
    }
  }
  // cout<<"OK"<<endl;
  int n;
  while (cin >> n && n > 0) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int dp[1000010];
int dp2[1000010];

int main() {
  int a = 1, b;
  vi v;
  vi u;
  while ((b = a * (a + 1) * (a + 2) / 6) < 1e6) {
    v.pb(b);
    if (b % 2 == 1) {
      u.pb(b);
    }
    a++;
  }
  // cout<<v.size()<<endl;
  for (int i = 0; i < 1000010; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  dp[0] = 0;
  dp2[0] = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = v[i]; j < 1000010; j++) {
      dp[j] = min(dp[j], dp[j - v[i]] + 1);
      if (i < u.size() && j >= u[i]) {
        dp2[j] = min(dp2[j], dp2[j - u[i]] + 1);
      }
    }
  }
  // cout<<"OK"<<endl;
  int n;
  while (cin >> n && n > 0) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
}
Problem p00749 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

char p[16][64];
bool visit[16][64];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

bool dfs(int y, int x, int &rn, int &rd) {
  int n = 0, d = 0;
  int xl = 100, xr = -100;
  char ch = p[y][x];

  queue<int> q;
  q.push(y << 8 | x);
  visit[y][x] = true;
  while (!q.empty()) {
    y = q.front() >> 8;
    x = q.front() & 0xff;
    q.pop();

    n += x;
    ++d;

    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!visit[ny][nx] && p[ny][nx] == ch) {
        visit[ny][nx] = true;
        q.push(ny << 8 | nx);
      }
    }

    if (!visit[y - 1][x] && isdigit(p[y - 1][x])) {
      if (!dfs(y - 1, x, n, d)) {
        return false;
      }
    }

    if (p[y + 1][x] != ch) {
      if (p[y + 1][x] == '!' || isdigit(p[y + 1][x])) {
        xl = min(xl, x);
        xr = max(xr, x + 1);
      }
    }
  }

  rn += n;
  rd += d;

  // xl < n / d + 0.5 < xr
  return 2 * d * xl < 2 * n + d && 2 * n + d < 2 * d * xr;
}

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    memset(p, '.', sizeof p);
    memset(p[h + 1], '!', sizeof p[h + 1]);
    memset(visit, 0, sizeof visit);

    for (int i = 1; i <= h; ++i) {
      scanf(" %s", p[i] + 1);
    }

    int x;
    for (x = 1; !isdigit(p[h][x]); ++x)
      ;
    int n = 0, d = 0;
    puts(dfs(h, x, n, d) ? "STABLE" : "UNSTABLE");
  }
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

char p[64][16];
bool visit[64][16];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

bool dfs(int y, int x, int &rn, int &rd) {
  int n = 0, d = 0;
  int xl = 100, xr = -100;
  char ch = p[y][x];

  queue<int> q;
  q.push(y << 8 | x);
  visit[y][x] = true;
  while (!q.empty()) {
    y = q.front() >> 8;
    x = q.front() & 0xff;
    q.pop();

    n += x;
    ++d;

    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!visit[ny][nx] && p[ny][nx] == ch) {
        visit[ny][nx] = true;
        q.push(ny << 8 | nx);
      }
    }

    if (!visit[y - 1][x] && isdigit(p[y - 1][x])) {
      if (!dfs(y - 1, x, n, d)) {
        return false;
      }
    }

    if (p[y + 1][x] != ch) {
      if (p[y + 1][x] == '!' || isdigit(p[y + 1][x])) {
        xl = min(xl, x);
        xr = max(xr, x + 1);
      }
    }
  }

  rn += n;
  rd += d;

  // xl < n / d + 0.5 < xr
  return 2 * d * xl < 2 * n + d && 2 * n + d < 2 * d * xr;
}

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    memset(p, '.', sizeof p);
    memset(p[h + 1], '!', sizeof p[h + 1]);
    memset(visit, 0, sizeof visit);

    for (int i = 1; i <= h; ++i) {
      scanf(" %s", p[i] + 1);
    }

    int x;
    for (x = 1; !isdigit(p[h][x]); ++x)
      ;
    int n = 0, d = 0;
    puts(dfs(h, x, n, d) ? "STABLE" : "UNSTABLE");
  }
}
Problem p00750 (C++)

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

#define MAX (6 * N)

class Node {
public:
  vector<int> to;
  vector<string> sp;
  Node() {}
};

class State {
public:
  int p;
  string str;
  State(int p, string str) : p(p), str(str) {}

  bool operator<(const State &s) const { return str > s.str; }
};

int main() {

  ifstream cin("E1.txt");
  ofstream cout("out.txt");
  int N, A, S, G;
  while (cin >> N >> A >> S >> G, (N || A || S || G)) {
    Node node[45];
    for (int i = 0; i < A; i++) {
      int s, d;
      string str;
      cin >> s >> d >> str;
      node[s].to.push_back(d);
      node[s].sp.push_back(str);
    }

    bool v[40][500] = {0};
    priority_queue<State> q;
    q.push(State(S, ""));

    bool g = false;
    while (!q.empty()) {
      State s = q.top();
      q.pop();

      if (v[s.p][s.str.size()])
        continue;
      v[s.p][s.str.size()] = 1;

      if (s.p == G) {
        if (s.str.size() > MAX)
          break;

        g = true;
        cout << s.str << endl;
        break;
      }

      for (int i = 0; i < node[s.p].to.size(); i++) {
        int next = node[s.p].to[i];
        string spl = node[s.p].sp[i];

        if (s.str.size() + spl.size() > MAX * 2)
          continue;
        if (v[next][s.str.size() + spl.size()])
          continue;

        q.push(State(next, s.str + spl));
      }
    }
    if (!g)
      cout << "NO" << endl;
  }
}

Fixed submission:
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define MAX (6 * N)

class Node {
public:
  vector<int> to;
  vector<string> sp;
  Node() {}
};

class State {
public:
  int p;
  string str;
  State(int p, string str) : p(p), str(str) {}

  bool operator<(const State &s) const { return str > s.str; }
};

int main() {

  //	ifstream cin("E1.txt");
  //	ofstream cout("out.txt");
  int N, A, S, G;
  while (cin >> N >> A >> S >> G, (N || A || S || G)) {
    Node node[45];
    for (int i = 0; i < A; i++) {
      int s, d;
      string str;
      cin >> s >> d >> str;
      node[s].to.push_back(d);
      node[s].sp.push_back(str);
    }

    bool v[40][500] = {0};
    priority_queue<State> q;
    q.push(State(S, ""));

    bool g = false;
    while (!q.empty()) {
      State s = q.top();
      q.pop();

      if (v[s.p][s.str.size()])
        continue;
      v[s.p][s.str.size()] = 1;

      if (s.p == G) {
        if (s.str.size() > MAX)
          break;

        g = true;
        cout << s.str << endl;
        break;
      }

      for (int i = 0; i < node[s.p].to.size(); i++) {
        int next = node[s.p].to[i];
        string spl = node[s.p].sp[i];

        if (s.str.size() + spl.size() > MAX * 2)
          continue;
        if (v[next][s.str.size() + spl.size()])
          continue;

        q.push(State(next, s.str + spl));
      }
    }
    if (!g)
      cout << "NO" << endl;
  }
}
Problem p00761 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> a;
int L;

string intString(int n) {
  stringstream ss;
  int keta = 1, tmp;
  tmp = n;
  while (tmp != 0) {
    tmp /= 10;
    keta++;
  }
  for (int i = 0; i <= (L - keta); i++)
    ss << 0;

  ss << n;

  return ss.str();
}

int func(string str) {
  sort(str.begin(), str.end());
  int low = stoi(str);
  sort(str.rbegin(), str.rend());
  int high = stoi(str);
  return high - low;
}

void solve() {
  // 桁変更
  string str = intString(a[0]);
  for (int i = 1;; i++) {
    a[i] = func(str);
    for (int j = 0; j < i; j++) {
      if (a[i] == a[j]) {
        cout << j << ' ' << a[i] << ' ' << (i - j) << endl;
        a.clear();
        return;
      }
    }
    str = intString(a[i]);
  }
}

int main() {
  int a0;
  while (true) {
    cin >> a0 >> L;
    if (a0 + L == 0)
      break;
    a.push_back(a0);
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> a;
int L;

string intString(int n) {
  stringstream ss;
  int keta = 1, tmp;
  tmp = n;
  while (tmp != 0) {
    tmp /= 10;
    keta++;
  }
  for (int i = 0; i <= (L - keta); i++)
    ss << 0;

  ss << n;

  return ss.str();
}

int func(string str) {
  sort(str.begin(), str.end());
  int low = stoi(str);
  sort(str.rbegin(), str.rend());
  int high = stoi(str);
  return high - low;
}

void solve() {
  // 桁変更
  string str = intString(a[0]);
  for (int i = 1;; i++) {
    a.push_back(func(str));
    for (int j = 0; j < i; j++) {
      if (a[i] == a[j]) {
        cout << j << ' ' << a[i] << ' ' << (i - j) << endl;
        a.clear();
        return;
      }
    }
    str = intString(a[i]);
  }
}

int main() {
  int a0;
  while (true) {
    cin >> a0 >> L;
    if (a0 + L == 0)
      break;
    a.push_back(a0);
    solve();
  }
  return 0;
}
Problem p00761 (C++)

Buggy submission (status: Runtime Error):
// 18
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  for (int n, l; cin >> n >> l, n | l;) {
    stringstream ss;
    ss << setw(l) << setfill('0') << n;
    string a[20];
    a[0] = ss.str();
    for (int i = 1;; i++) {
      string h = a[i - 1], lo = a[i - 1];
      sort(h.begin(), h.end());
      sort(lo.rbegin(), lo.rend());
      stringstream st;
      st << setw(l) << setfill('0') << atoi(lo.c_str()) - atoi(h.c_str());
      a[i] = st.str();
      string *p = find(a, a + i, a[i]);
      if (p != a + i) {
        cout << p - a << ' ' << atoi(p->c_str()) << ' ' << a + i - p << endl;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
// 18
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  for (int n, l; cin >> n >> l, n | l;) {
    stringstream ss;
    ss << setw(l) << setfill('0') << n;
    string a[21];
    a[0] = ss.str();
    for (int i = 1;; i++) {
      string h = a[i - 1], lo = a[i - 1];
      sort(h.begin(), h.end());
      sort(lo.rbegin(), lo.rend());
      stringstream st;
      st << setw(l) << setfill('0') << atoi(lo.c_str()) - atoi(h.c_str());
      a[i] = st.str();
      string *p = find(a, a + i, a[i]);
      if (p != a + i) {
        cout << p - a << ' ' << atoi(p->c_str()) << ' ' << a + i - p << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00761 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<string> tmp;
int ans, cnt, n;
string s;
string rec(string x) {
  while (1) {
    // cout << x << endl;
    bool flag = false;
    sort(x.begin(), x.end());
    string a = x;
    if (a.empty() == 1)
      while (a.front() != '0')
        a.erase(a.begin());
    int mn = atoi(a.c_str());
    reverse(x.begin(), x.end());
    int mx = atoi(x.c_str());
    x = to_string(mx - mn);
    if (x.size() != n)
      x = '0' + x;
    // cout << mx << " " <<  mx << " " << x << endl;
    for (int i = 0; i < tmp.size(); i++) {
      // cout << tmp[i] << " ";
      if (x == tmp[i]) {
        ans = i;
        flag = true;
        break;
      }
    }
    tmp.push_back(x);
    cnt++;
    if (flag == false)
      return rec(x);
    else
      return x;
  }
  return 0;
}
int main() {
  while (1) {
    cin >> s >> n;
    if (s == "0" && n == 0)
      break;
    cnt = 0;
    ans = 0;
    while (1) {
      while (s.size() != n)
        s = '0' + s;
      tmp.push_back(s);
      s = rec(s);
      cout << ans << " " << atoi(s.c_str()) << " " << cnt - ans << endl;
      tmp.clear();
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<string> tmp;
int ans, cnt, n;
string s;
string rec(string x) {
  while (1) {
    // cout << x << endl;
    bool flag = false;
    sort(x.begin(), x.end());
    string a = x;
    if (a.empty() == 1)
      while (a.front() != '0')
        a.erase(a.begin());
    int mn = atoi(a.c_str());
    reverse(x.begin(), x.end());
    int mx = atoi(x.c_str());
    x = to_string(mx - mn);
    if (x.size() != n)
      x = '0' + x;
    // cout << mx << " " <<  mx << " " << x << endl;
    for (int i = 0; i < tmp.size(); i++) {
      // cout << tmp[i] << " ";
      if (x == tmp[i]) {
        ans = i;
        flag = true;
        break;
      }
    }
    tmp.push_back(x);
    cnt++;
    if (flag == false)
      return rec(x);
    else
      return x;
  }
  return 0;
}
int main() {
  while (1) {
    cin >> s >> n;
    if (s == "0" && n == 0)
      break;
    cnt = 0;
    ans = 0;
    while (s.size() != n)
      s = '0' + s;
    tmp.push_back(s);
    s = rec(s);
    cout << ans << " " << atoi(s.c_str()) << " " << cnt - ans << endl;
    tmp.clear();
  }
  return 0;
}
Problem p00763 (C++)

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

#define INF (1 << 28)
#define REP(i, n) for (int i = 0; i < n; i++)

int a[110][110], wf[30][110][110], p[30], q[30][100], r[30][100];
int n, m, c, s, g, x, y, d, id, tmp;

int main() {
  while (scanf("%d%d%d%d%d", &n, &m, &c, &s, &g), n) {
    REP(i, n) REP(j, n) {
      a[i][j] = INF;
      REP(k, n) wf[k][i][j] = INF;
    }

    REP(i, m) {
      scanf("%d%d%d%d", &x, &y, &d, &id);
      x--;
      y--;
      id--;
      wf[id][x][y] = min(wf[id][x][y], d);
      wf[id][y][x] = min(wf[id][y][x], d);
    }

    REP(l, c) {
      scanf("%d", &p[l]);
      REP(k, n)
      REP(i, n) REP(j, n) wf[l][i][j] =
          min(wf[l][i][j], wf[l][i][k] + wf[l][k][j]);
    }

    REP(k, c) {
      q[k][0] = 0;
      REP(i, p[k] - 1) scanf("%d", &q[k][i + 1]);
      q[k][p[k]] = INF;
      REP(i, p[k]) scanf("%d", &r[k][i]);

      REP(i, n) for (int j = i; j < n; j++) {
        if (wf[k][i][j] < INF) {
          tmp = 0;
          REP(l, p[k]) {
            if (wf[k][i][j] <= q[k][l + 1]) {
              tmp += r[k][l] * (wf[k][i][j] - q[k][l]);
              break;
            } else
              tmp += r[k][l] * (q[k][l + 1] - q[k][l]);
          }
          a[i][j] = a[j][i] = min(a[i][j], tmp);
        }
      }
    }

    REP(k, n) REP(i, n) REP(j, n) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);

    if (a[s - 1][g - 1] >= INF)
      printf("-1\n");
    else
      printf("%d\n", a[s - 1][g - 1]);
  }
}

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

#define INF (1 << 28)
#define REP(i, n) for (int i = 0; i < n; i++)

int a[110][110], wf[30][110][110], p[30], q[30][100], r[30][100];
int n, m, c, s, g, x, y, d, id, tmp;

int main() {
  while (scanf("%d%d%d%d%d", &n, &m, &c, &s, &g), n) {
    REP(i, n) REP(j, n) {
      a[i][j] = INF;
      REP(k, c) wf[k][i][j] = INF;
    }

    REP(i, m) {
      scanf("%d%d%d%d", &x, &y, &d, &id);
      x--;
      y--;
      id--;
      wf[id][x][y] = min(wf[id][x][y], d);
      wf[id][y][x] = min(wf[id][y][x], d);
    }

    REP(l, c) {
      scanf("%d", &p[l]);
      REP(k, n)
      REP(i, n) REP(j, n) wf[l][i][j] =
          min(wf[l][i][j], wf[l][i][k] + wf[l][k][j]);
    }

    REP(k, c) {
      q[k][0] = 0;
      REP(i, p[k] - 1) scanf("%d", &q[k][i + 1]);
      q[k][p[k]] = INF;
      REP(i, p[k]) scanf("%d", &r[k][i]);

      REP(i, n) for (int j = i; j < n; j++) {
        if (wf[k][i][j] < INF) {
          tmp = 0;
          REP(l, p[k]) {
            if (wf[k][i][j] <= q[k][l + 1]) {
              tmp += r[k][l] * (wf[k][i][j] - q[k][l]);
              break;
            } else
              tmp += r[k][l] * (q[k][l + 1] - q[k][l]);
          }
          a[i][j] = a[j][i] = min(a[i][j], tmp);
        }
      }
    }

    REP(k, n) REP(i, n) REP(j, n) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);

    if (a[s - 1][g - 1] >= INF)
      printf("-1\n");
    else
      printf("%d\n", a[s - 1][g - 1]);
  }
}
Problem p00767 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))

typedef pair<int, int> pii;
typedef pair<int, pii> P;

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    P in = make_pair(w * w + h * h, pii(h, w));
    P ans = make_pair(1e9, pii(100000, 100000));
    reep(i, 1, 10000) {
      reep(j, i + 1, 10000) {
        P tmp = make_pair(i * i + j * j, pii(i, j));
        if (tmp > in) {
          ans = min(ans, tmp);
        }
      }
    }
    cout << ans.second.first << " " << ans.second.second << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))

typedef pair<int, int> pii;
typedef pair<int, pii> P;

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    P in = make_pair(w * w + h * h, pii(h, w));
    P ans = make_pair(1e9, pii(100000, 100000));
    reep(i, 1, 1000) {
      reep(j, i + 1, 1000) {
        P tmp = make_pair(i * i + j * j, pii(i, j));
        if (tmp > in) {
          ans = min(ans, tmp);
        }
      }
    }
    cout << ans.second.first << " " << ans.second.second << endl;
  }
}
Problem p00767 (C++)

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

using namespace std;

#define INF 1e9

int len(int i, int j) {
  int res = i * i + j * j;
  return res;
}

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

  while (1) {
    int h, w;
    cin >> h >> w;

    int L = len(h, w);

    pair<int, int> ans = {10000, 10000};

    for (int i = 1; i < 1000; i++) {
      for (int j = i + 1; j < 1000; j++) {
        if (i == h && j == w)
          continue;
        int LL = len(i, j);
        if (LL == L && i < h)
          continue;
        if (LL >= L && LL < len(ans.first, ans.second)) {
          ans = {i, j};
        }
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF 1e9

int len(int i, int j) {
  int res = i * i + j * j;
  return res;
}

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

  while (1) {
    int h, w;
    cin >> h >> w;

    if (h == 0)
      break;

    int L = len(h, w);

    pair<int, int> ans = {10000, 10000};

    for (int i = 1; i < 1000; i++) {
      for (int j = i + 1; j < 1000; j++) {
        if (i == h && j == w)
          continue;
        int LL = len(i, j);
        if (LL == L && i < h)
          continue;
        if (LL >= L && LL < len(ans.first, ans.second)) {
          ans = {i, j};
        }
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
}
Problem p00768 (C++)

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

struct Info {
  int id, cost, ac;
};

bool operator<(const Info &a, const Info &b) {
  if (a.ac != b.ac)
    return a.ac > b.ac;
  if (a.cost != b.cost)
    return a.cost < b.cost;
  return a.id > b.id;
}

int main() {
  int M, T, P, R;
  while (cin >> M >> T >> P >> R) {
    if (M == 0)
      break;
    vector<vector<int>> num_WA(T, vector<int>(P, 0));
    vector<int> cost(T, 0);
    vector<int> num_AC(T, 0);
    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      t--;
      if (j == 0) {
        cost[t] += m + num_WA[t][p] * 20;
        num_AC[t]++;
      } else {
        num_WA[t][p]++;
      }
    }
    vector<Info> temp;
    for (int i = 0; i < T; i++) {
      Info hoge;
      hoge.cost = cost[i];
      hoge.id = i + 1;
      hoge.ac = num_AC[i];
      temp.push_back(hoge);
    }
    sort(temp.begin(), temp.end());
    int bef_ac = 0, bef_cost = 0;
    for (int i = 0; i < temp.size(); i++) {
      if (i != 0) {
        if (bef_ac == temp[i].ac && bef_cost == temp[i].cost) {
          cout << "=";
        } else
          cout << ",";
      }
      cout << temp[i].id;
      bef_ac = temp[i].ac;
      bef_cost = temp[i].cost;
    }
    cout << endl;
  }
  return 0;
}

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

struct Info {
  int id, cost, ac;
};

bool operator<(const Info &a, const Info &b) {
  if (a.ac != b.ac)
    return a.ac > b.ac;
  if (a.cost != b.cost)
    return a.cost < b.cost;
  return a.id > b.id;
}

int main() {
  int M, T, P, R;
  while (cin >> M >> T >> P >> R) {
    if (M == 0)
      break;
    vector<vector<int>> num_WA(T, vector<int>(P, 0));
    vector<int> cost(T, 0);
    vector<int> num_AC(T, 0);
    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      t--;
      p--;
      if (j == 0) {
        cost[t] += m + num_WA[t][p] * 20;
        num_AC[t]++;
      } else {
        num_WA[t][p]++;
      }
    }
    vector<Info> temp;
    for (int i = 0; i < T; i++) {
      Info hoge;
      hoge.cost = cost[i];
      hoge.id = i + 1;
      hoge.ac = num_AC[i];
      temp.push_back(hoge);
    }
    sort(temp.begin(), temp.end());
    int bef_ac = 0, bef_cost = 0;
    for (int i = 0; i < temp.size(); i++) {
      if (i != 0) {
        if (bef_ac == temp[i].ac && bef_cost == temp[i].cost) {
          cout << "=";
        } else
          cout << ",";
      }
      cout << temp[i].id;
      bef_ac = temp[i].ac;
      bef_cost = temp[i].cost;
    }
    cout << endl;
  }
  return 0;
}