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

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = n - 1; i >= m; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

typedef long long ll;
using namespace std;

bool paircmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m, ans = 0;
    cin >> n >> m;

    vector<pair<int, int>> v(n);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v[i] = {a, b};
    }
    sort(ALL(v), paircmp);
    REP(i, n) {
      int a = min(m, v[i].first);
      m -= a;
      v[i].first -= a;
      ans += v[i].first * v[i].second;
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define RFOR(i, m, n) for (int i = n - 1; i >= m; --i)
#define ALL(v) v.begin(), v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

typedef long long ll;
using namespace std;

bool paircmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m, ans = 0;
    cin >> n >> m;
    if (!n)
      break;
    vector<pair<int, int>> v(n);
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v[i] = {a, b};
    }
    sort(ALL(v), paircmp);
    REP(i, n) {
      int a = min(m, v[i].first);
      m -= a;
      v[i].first -= a;
      ans += v[i].first * v[i].second;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int d[11], n, m, i, a, b, sum, e;
int main() {
  while (true) {
    memset(d, 0, sizeof(d));
    sum = 0;
    cin >> n >> m;
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      d[b] += a;
    }
    for (i = 10; i >= 0; i--) {
      e = min(d[i], m);
      m -= e;
      sum += i * (d[i] - e);
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int d[11], n, m, i, a, b, sum, e;
int main() {
  while (true) {
    memset(d, 0, sizeof(d));
    sum = 0;
    cin >> n >> m;
    if (!n) {
      break;
    }
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      d[b] += a;
    }
    for (i = 10; i >= 0; i--) {
      e = min(d[i], m);
      m -= e;
      sum += i * (d[i] - e);
    }
    cout << sum << endl;
  }
}
Problem p01144 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<pair<int, int>> v;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      ans += d * p;
      v.push_back(make_pair(p, d));
    }
    sort(v.begin(), v.end());
    int i = v.size() - 1;
    while (m > 0) {
      ans -= min(m, v[i].second) * v[i].first;
      m -= min(m, v[i].second);
      i--;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<pair<int, int>> v;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int d, p;
      cin >> d >> p;
      ans += d * p;
      v.push_back(make_pair(p, d));
    }
    sort(v.begin(), v.end());
    int i = v.size() - 1;
    while (m > 0) {
      ans -= min(m, v[i].second) * v[i].first;
      m -= min(m, v[i].second);
      i--;
      if (i < 0)
        break;
    }
    cout << ans << endl;
  }
}
Problem p01144 (C++)

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

vector<pii> ans;

int main() {
  while (1) {

    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    ans.clear();
    for (int i = 0; i < N; i++) {
      int d, p;
      cin >> d >> p;
      ans.push_back(pii(p, d));
    }
    sort(ans.begin(), ans.end());
    long long sum = 0;
    while (M > 0) {
      int id = ans.size() - 1;
      if (M > ans[id].second) {
        M -= ans[id].second;
        ans.pop_back();
      } else if (M > 0) {
        ans[id].second -= M;
        M = 0;
      }
    }

    for (auto x : ans) {
      sum += x.first * x.second;
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

vector<pii> ans;

int main() {
  while (1) {

    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    ans.clear();
    for (int i = 0; i < N; i++) {
      int d, p;
      cin >> d >> p;
      ans.push_back(pii(p, d));
    }
    sort(ans.begin(), ans.end());
    long long sum = 0;
    while (M > 0 and ans.size()) {
      int id = ans.size() - 1;
      if (M > ans[id].second) {
        M -= ans[id].second;
        ans.pop_back();
      } else if (M > 0) {
        ans[id].second -= M;
        M = 0;
      }
    }

    for (auto x : ans) {
      sum += x.first * x.second;
    }
    cout << sum << endl;
  }
}
Problem p01147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

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

using namespace std;
vector<string> uvs;
pair<int, string> solve(void) {
  string answer = "";
  for (string &s : uvs) {
    int ol = min(answer.size(), s.size());
    for (; ol > 0; --ol) {
      bool flag = true;
      REP(i, ol) if (answer[answer.size() - ol + i] != s[i]) {
        flag = false;
        break;
      }
      if (flag)
        break;
    }
    answer += string(s.begin() + ol, s.end());
  }
  return make_pair(answer.size(), answer);
}
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<pair<int, string>> vs(n);
    uvs.clear();
    REP(i, n) {
      string s;
      cin >> s;
      vs[i] = make_pair(-(int)s.size(), s);
    }
    SORT(vs);
    REP(i, n) {
      bool flag = true;
      REP(j, i) REP(k, vs[i].first - vs[j].first) {
        string tmp = vs[j].second;
        string r(tmp.begin() + k, tmp.begin() + k - vs[i].first);
        if (vs[i].second == r)
          flag = false;
      }
      if (flag)
        uvs.push_back(vs[i].second);
    }
    SORT(uvs);
    pair<int, string> answer(1000, "");
    do {
      answer = min(answer, solve());
    } while (next_permutation(uvs.begin(), uvs.end()));
    cout << answer.second << endl;
  }
}

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

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

using namespace std;
vector<string> uvs;
pair<int, string> solve(void) {
  string answer = "";
  for (string &s : uvs) {
    int ol = min(answer.size(), s.size());
    for (; ol > 0; --ol) {
      bool flag = true;
      REP(i, ol) if (answer[answer.size() - ol + i] != s[i]) {
        flag = false;
        break;
      }
      if (flag)
        break;
    }
    for (auto i = s.begin() + ol; i != s.end(); ++i)
      answer.push_back(*i);
  }
  return make_pair(answer.size(), answer);
}
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<pair<int, string>> vs(n);
    uvs.clear();
    REP(i, n) {
      string s;
      cin >> s;
      vs[i] = make_pair(-(int)s.size(), s);
    }
    SORT(vs);
    REP(i, n) {
      bool flag = true;
      REP(j, i) REP(k, vs[i].first - vs[j].first) {
        string tmp = vs[j].second;
        string r(tmp.begin() + k, tmp.begin() + k - vs[i].first);
        if (vs[i].second == r)
          flag = false;
      }
      if (flag)
        uvs.push_back(vs[i].second);
    }
    SORT(uvs);
    pair<int, string> answer(1000, "");
    do {
      answer = min(answer, solve());
    } while (next_permutation(uvs.begin(), uvs.end()));
    cout << answer.second << endl;
  }
}
Problem p01153 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n, a[107], dp[107][107], K[107][107];
int main() {
  int t;
  cin >> t;
  for (int p = 0; p < t; p++) {
    for (int i = 0; i < 11449; i++)
      dp[i / 107][i % 107] = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int maxn = 0;
    for (int u = 0; u < n; u++) {
      int G = a[0];
      for (int v = 1; v < n; v++)
        a[v - 1] = a[v];
      a[n - 1] = G;
      for (int v = 0; v < 11449; v++)
        dp[v / 107][v % 107] = 0;
      for (int v = 0; v < n; v++) {
        for (int w = 0; w < n; w++)
          K[v][w] = abs(a[v] - a[w]);
      }
      for (int v = 0; v < 8; v++) {
        for (int i = 0; i < n; i++) {
          for (int j = i + 1; j < n; j++) {
            for (int k = i; k < j; k++) {
              dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j] + K[i][k + 1]);
            }
          }
        }
      }
      maxn = max(maxn, dp[0][n - 1]);
    }
    cout << maxn << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n, a[107], dp[107][107], K[107][107];
int main() {
  int t;
  cin >> t;
  for (int p = 0; p < t; p++) {
    for (int i = 0; i < 11449; i++)
      dp[i / 107][i % 107] = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int maxn = 0;
    for (int u = 0; u < n; u++) {
      int G = a[0];
      for (int v = 1; v < n; v++)
        a[v - 1] = a[v];
      a[n - 1] = G;
      for (int v = 0; v < 11449; v++)
        dp[v / 107][v % 107] = 0;
      for (int v = 0; v < n; v++) {
        for (int w = 0; w < n; w++)
          K[v][w] = abs(a[v] - a[w]);
      }
      for (int v = 1; v <= n; v++) {
        for (int w = 0; w < n; w++) {
          int I = w, J = v + w;
          if (I >= J || J >= n)
            continue;
          for (int k = I; k < J; k++) {
            dp[I][J] = max(dp[I][J], dp[I][k] + dp[k + 1][J] + K[I][k + 1]);
          }
        }
      }
      maxn = max(maxn, dp[0][n - 1]);
    }
    cout << maxn << endl;
  }
  return 0;
}
Problem p01156 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
string S[15] = {"Rock",  "Fire",   "Scissors", "Snake",     "Human",
                "Tree",  "Wolf",   "Sponge",   "Paper",     "Air",
                "Water", "Dragon", "Devil",    "Lightning", "Gun"};
int N, a[10000], c[15];
string T;
bool solve(int a, int b) {
  if (a > b)
    b += 15;
  if (b - a <= 7)
    return true;
  return false;
}
int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> T;
      for (int j = 0; j < 15; j++) {
        if (T == S[j])
          a[i] = j;
      }
    }
    for (int i = 0; i < 15; i++)
      c[i] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (!solve(a[i], a[j]))
          goto E;
      }
      c[a[i]] = 1;
    E:;
    }
    int G = 0;
    for (int i = 0; i < N - 1; i++) {
      if (S[i] != S[i + 1])
        G = 1;
    }
    int P = 0;
    if (G == 0)
      goto F;
    for (int i = 0; i < 15; i++) {
      if (c[i] == 1) {
        P = 1;
        cout << S[i] << endl;
      }
    }
  F:;
    if (P == 0)
      cout << "Draw" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
string S[15] = {"Rock",  "Fire",   "Scissors", "Snake",     "Human",
                "Tree",  "Wolf",   "Sponge",   "Paper",     "Air",
                "Water", "Dragon", "Devil",    "Lightning", "Gun"};
int N, a[10000], c[15];
string T;
bool solve(int a, int b) {
  if (a > b)
    b += 15;
  if (b - a <= 7)
    return true;
  return false;
}
int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> T;
      for (int j = 0; j < 15; j++) {
        if (T == S[j])
          a[i] = j;
      }
    }
    for (int i = 0; i < 15; i++)
      c[i] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (!solve(a[i], a[j]))
          goto E;
      }
      c[a[i]] = 1;
    E:;
    }
    int G = 0;
    for (int i = 0; i < N - 1; i++) {
      if (a[i] != a[i + 1])
        G = 1;
    }
    int P = 0;
    if (G == 0)
      goto F;
    for (int i = 0; i < 15; i++) {
      if (c[i] == 1) {
        P = 1;
        cout << S[i] << endl;
      }
    }
  F:;
    if (P == 0)
      cout << "Draw" << endl;
  }
  return 0;
}
Problem p01160 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int X[3001][3001];
int Y[3001][3001];

string lcs(string a, string b) {
  int n = a.size();
  int m = b.size();
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < m + 1; j++) {
      X[i][j] = 0;
      Y[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        X[i + 1][j + 1] = X[i][j] + 1;
        Y[i + 1][j + 1] = 0;
      } else if (X[i + 1][j] < X[i][j + 1]) {
        X[i + 1][j + 1] = X[i][j + 1];
        Y[i + 1][j + 1] = +1;
      } else {
        X[i + 1][j + 1] = X[i + 1][j];
        Y[i + 1][j + 1] = -1;
      }
    }
  }

  string answer;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y[i][j] > 0) {
      i--;
    } else if (Y[i][j] < 0) {
      j--;
    } else {
      answer += a[i - 1];
      i--;
      j--;
    }
  }
  reverse(answer.begin(), answer.end());
  return answer;
}

int main() {
  string input;
  while (cin >> input) {
    string a = input;
    string b = input;
    reverse(b.begin(), b.end());
    string answer = lcs(a, b);
    cout << answer << endl;
  }
  return 0;
}

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

short X[3001][3001];
short Y[3001][3001];

string lcs(string a, string b) {
  int n = a.size();
  int m = b.size();
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < m + 1; j++) {
      X[i][j] = 0;
      Y[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        X[i + 1][j + 1] = X[i][j] + 1;
        Y[i + 1][j + 1] = 0;
      } else if (X[i + 1][j] < X[i][j + 1]) {
        X[i + 1][j + 1] = X[i][j + 1];
        Y[i + 1][j + 1] = +1;
      } else {
        X[i + 1][j + 1] = X[i + 1][j];
        Y[i + 1][j + 1] = -1;
      }
    }
  }

  string answer;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y[i][j] > 0) {
      i--;
    } else if (Y[i][j] < 0) {
      j--;
    } else {
      answer += a[i - 1];
      i--;
      j--;
    }
  }
  reverse(answer.begin(), answer.end());
  return answer;
}

int main() {
  string input;
  while (cin >> input) {
    string a = input;
    string b = input;
    reverse(b.begin(), b.end());
    string answer = lcs(a, b);
    cout << answer << endl;
  }
  return 0;
}
Problem p01160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string s, t;
int dp[1011][1011];
int used[1011][1011];
string solve() {
  string ans = "";
  int i = s.size() - 1;
  int j = t.size() - 1;
  while (i > 0 && j > 0) {
    if (used[i][j] == 1)
      ans += t[j], i--, j--;
    else if (used[i][j] == 2)
      i--;
    else
      j--;
  }
  return ans;
}
int main() {
  while (cin >> s) {
    memset(dp, 0, sizeof(dp));
    memset(used, 0, sizeof(used));
    t = s;
    reverse(s.begin(), s.end());
    t = "@" + t;
    s = "@" + s;
    for (int i = 1; i < s.size(); i++)
      for (int j = 1; j < t.size(); j++)
        if (s[i] == t[j]) {
          if (dp[i - 1][j] >= dp[i - 1][j - 1] + 1)
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;

          else
            dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1), used[i][j] = 1;
        } else {
          if (dp[i - 1][j] >= dp[i][j - 1])
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;
          else
            dp[i][j] = max(dp[i][j], dp[i][j - 1]), used[i][j] = 3;
        }
    cout << solve() << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string s, t;
int dp[2011][2011];
int used[2011][2011];
string solve() {
  string ans = "";
  int i = s.size() - 1;
  int j = t.size() - 1;
  while (i > 0 && j > 0) {
    if (used[i][j] == 1)
      ans += t[j], i--, j--;
    else if (used[i][j] == 2)
      i--;
    else
      j--;
  }
  return ans;
}
int main() {
  while (cin >> s) {
    memset(dp, 0, sizeof(dp));
    memset(used, 0, sizeof(used));
    t = s;
    reverse(s.begin(), s.end());
    t = "@" + t;
    s = "@" + s;
    for (int i = 1; i < s.size(); i++)
      for (int j = 1; j < t.size(); j++)
        if (s[i] == t[j]) {
          if (dp[i - 1][j] >= dp[i - 1][j - 1] + 1)
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;

          else
            dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1), used[i][j] = 1;
        } else {
          if (dp[i - 1][j] >= dp[i][j - 1])
            dp[i][j] = max(dp[i][j], dp[i - 1][j]), used[i][j] = 2;
          else
            dp[i][j] = max(dp[i][j], dp[i][j - 1]), used[i][j] = 3;
        }
    cout << solve() << endl;
  }
}
Problem p01160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 2050
string ss[2][N + 1];
int c[N + 1][N + 1];
string lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  ss[0][0] = "";
  memset(c, 0, sizeof(c));
  for (int i = 0; i <= m; i++)
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    c[0][j] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
        c[i][j] = c[i - 1][j - 1] + 1,
        ss[i % 2][j] = ss[(i - 1) % 2][j - 1] + X[i];
      else {
        if (c[i - 1][j] >= c[i][j - 1])
          c[i][j] = c[i - 1][j], ss[i % 2][j] = ss[(i - 1) % 2][j];
        else
          c[i][j] = c[i][j - 1], ss[i % 2][j] = ss[i % 2][j - 1];
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  string ret = ss[m % 2][n], ret2 = ret;
  reverse(ret2.begin(), ret2.end());
  // assert(ret==ret2);
  assert((int)ret.size() == maxl);
  return ss[m % 2][n];
}
int main() {
  string s;
  while (cin >> s) {
    string t = s;
    reverse(t.begin(), t.end());
    cout << lcs(s, t) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 2050
string ss[2][N + 1];
int c[N + 1][N + 1];
string lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  ss[0][0] = "";
  memset(c, 0, sizeof(c));
  for (int i = 0; i < N + 1; i++)
    ss[0][i] = ss[1][i] = "";
  for (int i = 0; i <= m; i++)
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    c[0][j] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
        c[i][j] = c[i - 1][j - 1] + 1,
        ss[i % 2][j] = ss[(i - 1) % 2][j - 1] + X[i];
      else {
        if (c[i - 1][j] >= c[i][j - 1])
          c[i][j] = c[i - 1][j], ss[i % 2][j] = ss[(i - 1) % 2][j];
        else
          c[i][j] = c[i][j - 1], ss[i % 2][j] = ss[i % 2][j - 1];
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  string ret = ss[m % 2][n], ret2 = ret;
  reverse(ret2.begin(), ret2.end());
  // assert(ret==ret2);
  assert((int)ret.size() == maxl);
  return ss[m % 2][n];
}
int main() {
  string s;
  while (cin >> s) {
    string t = s;
    reverse(t.begin(), t.end());
    cout << lcs(s, t) << endl;
  }
  return 0;
}
Problem p01163 (C++)

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

const int MAX = 30000;
int main() {
  vector<int> pr;
  bool p[MAX + 1] = {false};
  for (int i = 2; i <= MAX; i++)
    p[i] = 1;

  for (int i = 2; i * i <= MAX; i++)
    if (p[i])
      for (int k = i + i; k <= MAX; k += i)
        p[k] = 0;

  for (int i = 2; i <= MAX; i++)
    if (p[i])
      pr.push_back(i);

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i < pr.size(); i++)
      for (int j = i; j < pr.size() && pr[i] + pr[j] <= N; j++)
        for (int k = j; k < pr.size() && pr[i] + pr[j] + pr[k] <= N; k++)
          if (pr[i] + pr[j] + pr[k] == N && pr[i] + pr[j] > pr[k])
            cnt++;
    cout << cnt << endl;
  }
}

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

const int MAX = 30000;
int main() {
  vector<int> pr;
  bool p[MAX + 1] = {false};
  for (int i = 2; i <= MAX; i++)
    p[i] = 1;

  for (int i = 2; i * i <= MAX; i++)
    if (p[i])
      for (int k = i + i; k <= MAX; k += i)
        p[k] = 0;

  for (int i = 2; i <= MAX; i++)
    if (p[i])
      pr.push_back(i);

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i < pr.size(); i++)
      for (int j = i; j < pr.size() && pr[i] + pr[j] <= N; j++) {
        int k = N - pr[i] - pr[j];
        if (!p[k])
          continue;
        if (pr[j] > k)
          continue;
        if (pr[i] + pr[j] > k)
          cnt++;
      }
    cout << cnt << endl;
  }
}
Problem p01168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int m(int a, int k) {
  string s;
  while (a) {
    int tmp = a % k;
    if (tmp > 9)
      return -1;
    s += '0' + tmp;
    a /= k;
  }
  reverse(s.begin(), s.end());
  return stoll(s);
}
signed main() {
  int a, b, c;
  while (cin >> a >> b >> c, ~a) {
    bool f = 1;
    for (int i = 2; i <= 16; i++)
      f &= m(a, i) != b;
    int res = b;
    for (int j = a; j <= c; j++) {
      int tmp = -1;
      for (int i = 2; i <= 16; i++)
        if (m(j, i) >= res)
          if (tmp < 0 || m(j, i) < tmp)
            tmp = m(j, i);
      if (tmp < 0) {
        f = 1;
        break;
      }
      res = tmp;
    }
    cout << (f ? -1 : res) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int m(int a, int k) {
  if (!a)
    return 0;
  string s;
  while (a) {
    int tmp = a % k;
    if (tmp > 9)
      return -1;
    s += '0' + tmp;
    a /= k;
  }
  reverse(s.begin(), s.end());
  return stoll(s);
}
signed main() {
  int a, b, c;
  while (cin >> a >> b >> c, ~a) {
    bool f = 1;
    for (int i = 2; i <= 16; i++)
      f &= m(a, i) != b;
    int res = b;
    for (int j = a; j <= c; j++) {
      int tmp = -1;
      for (int i = 2; i <= 16; i++)
        if (m(j, i) >= res)
          if (tmp < 0 || m(j, i) < tmp)
            tmp = m(j, i);
      if (tmp < 0) {
        f = 1;
        break;
      }
      res = tmp;
    }
    cout << (f ? -1 : res) << endl;
  }
  return 0;
}
Problem p01171 (C++)

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

#define MAXNUM 1000001
bool prime[MAXNUM];

void eratosthenes() {
  prime[0] = prime[1] = false;

  for (int i = 2; i < MAXNUM; i++) {
    prime[i] = true;
  }

  for (int i = 2; i * i < MAXNUM; i++) {
    if (prime[i]) {
      for (int j = i * i; j < MAXNUM; j += i) {
        prime[j] = false;
      }
    }
  }
}

int getkey(int a) {
  int t;
  for (int i = a; i >= 2; i--) {
    if (a % i != 0)
      continue;
    if (prime[i]) {
      t = i;
      break;
    }
  }
  int ret = t;
  for (int i = t - 1; i >= 2; i--) {
    if (a % i != 0)
      continue;
    if (prime[i])
      ret -= i;
  }

  return ret;
}

int main() {
  eratosthenes();
  int a, b;
  while (scanf("%d%d", &a, &b)) {
    if (getkey(a) > getkey(b))
      puts("a");
    else
      puts("b");
  }
}

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

#define MAXNUM 1000001
bool prime[MAXNUM];

void eratosthenes() {
  prime[0] = prime[1] = false;

  for (int i = 2; i < MAXNUM; i++) {
    prime[i] = true;
  }

  for (int i = 2; i * i < MAXNUM; i++) {
    if (prime[i]) {
      for (int j = i * i; j < MAXNUM; j += i) {
        prime[j] = false;
      }
    }
  }
}

int getkey(int a) {
  int t;
  for (int i = a; i >= 2; i--) {
    if (a % i != 0)
      continue;
    if (prime[i]) {
      t = i;
      break;
    }
  }
  int ret = t;
  for (int i = t - 1; i >= 2; i--) {
    if (a % i != 0)
      continue;
    if (prime[i])
      ret -= i;
  }

  return ret;
}

int main() {
  eratosthenes();
  int a, b;
  while (scanf("%d%d", &a, &b), (a || b)) {
    if (getkey(a) > getkey(b))
      puts("a");
    else
      puts("b");
  }
}
Problem p01171 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <stdio.h>
#include <vector>
const int N = 100000;
int main() {
  std::bitset<N> p;
  std::vector<int> v, A, B;
  int i, j, a, b;
  for (i = 2; i < N; ++i)
    if (!p[i])
      for (v.push_back(i), j = i * 2; j < N; j += i)
        p[j] = 1;
  while (scanf("%d%d", &a, &b), a) {
    A.clear(), B.clear();
    for (i = 0; i < v.size(); ++i) {
      if (a % v[i] == 0)
        A.push_back(v[i]);
      if (b % v[i] == 0)
        B.push_back(v[i]);
    }
    for (a = A.back(), i = 0; i < A.size() - 1; ++i)
      a -= A[i];
    for (b = B.back(), i = 0; i < B.size() - 1; ++i)
      b -= B[i];
    puts(a > b ? "a" : "b");
  }
  return 0;
}

Fixed submission:
#include <bitset>
#include <stdio.h>
#include <vector>
const int N = 1000000;
int main() {
  std::bitset<N> p;
  std::vector<int> v, A, B;
  int i, j, a, b;
  for (i = 2; i < N; ++i)
    if (!p[i])
      for (v.push_back(i), j = i * 2; j < N; j += i)
        p[j] = 1;
  while (scanf("%d%d", &a, &b), a) {
    A.clear(), B.clear();
    for (i = 0; i < v.size(); ++i) {
      if (a % v[i] == 0)
        A.push_back(v[i]);
      if (b % v[i] == 0)
        B.push_back(v[i]);
    }
    for (a = A.back(), i = 0; i < A.size() - 1; ++i)
      a -= A[i];
    for (b = B.back(), i = 0; i < B.size() - 1; ++i)
      b -= B[i];
    puts(a > b ? "a" : "b");
  }
  return 0;
}
Problem p01171 (C++)

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

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

typedef long long LL;
typedef pair<int, int> PII;

const int NUM = 200000;
int prime[NUM];
vector<int> primelist;

int gen() {
  for (int i = 0; i < NUM; i++) {
    prime[i] = 1;
  }
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i * i <= NUM; i++) {
    for (int j = 2 * i; j < NUM; j += i) {
      prime[j] = 0;
    }
  }

  for (int i = 0; i < NUM; i++) {
    if (prime[i] == 1)
      primelist.push_back(i);
  }
}

int main() {
  gen();

  while (true) {
    int a, b;
    int keya = 0, keyb = 0;

    scanf("%d %d", &a, &b);
    if (a == 0)
      break;

    int k;
    for (int i = 0; primelist[i] <= a; i++) {
      if (a % primelist[i] == 0) {
        keya -= primelist[i];
        k = primelist[i];
      }
    }
    keya += 2 * k;

    for (int i = 0; primelist[i] <= b; i++) {
      if (b % primelist[i] == 0) {
        keyb -= primelist[i];
        k = primelist[i];
      }
    }
    keyb += 2 * k;

    if (keya < keyb)
      printf("b\n");
    else
      printf("a\n");
  }
}

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

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

typedef long long LL;
typedef pair<int, int> PII;

const int NUM = 2000000;
int prime[NUM];
vector<int> primelist;

int gen() {
  for (int i = 0; i < NUM; i++) {
    prime[i] = 1;
  }
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i * i <= NUM; i++) {
    for (int j = 2 * i; j < NUM; j += i) {
      prime[j] = 0;
    }
  }

  for (int i = 0; i < NUM; i++) {
    if (prime[i] == 1)
      primelist.push_back(i);
  }
}

int main() {
  gen();

  while (true) {
    int a, b;
    int keya = 0, keyb = 0;

    scanf("%d %d", &a, &b);
    if (a == 0)
      break;

    int k;
    for (int i = 0; primelist[i] <= a; i++) {
      if (a % primelist[i] == 0) {
        keya -= primelist[i];
        k = primelist[i];
      }
    }
    keya += 2 * k;

    for (int i = 0; primelist[i] <= b; i++) {
      if (b % primelist[i] == 0) {
        keyb -= primelist[i];
        k = primelist[i];
      }
    }
    keyb += 2 * k;

    if (keya < keyb)
      printf("b\n");
    else
      printf("a\n");
  }
}
Problem p01171 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : A.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

bool is_prime(int a) {
  for (int i = 2; i * i <= a; ++i) {
    if (a % i == 0)
      return false;
  }
  return true;
}

vector<int> ps;

int f(int a) {
  vector<ll> v;
  for (int p : ps) {
    if (a % p == 0)
      v.push_back(p);
  }
  if (v.size() == 0)
    return 0;
  ll ret = v.back();
  for (int i = 0; i < (int)v.size() - 1; i++) {
    ret -= v[i];
  }
  return ret;
}

int main() {
  for (int i = 0; i <= 200000; i++) {
    if (is_prime(i))
      ps.push_back(i);
  }

  int n, m;
  while (cin >> n >> m, n + m) {
    if (f(n) > f(m)) {
      cout << "a" << endl;
    } else {
      cout << "b" << endl;
    }
  }
}

Fixed submission:
//============================================================================
// Name        : A.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

bool is_prime(int a) {
  for (int i = 2; i * i <= a; ++i) {
    if (a % i == 0)
      return false;
  }
  return true;
}

vector<int> ps;

int f(int a) {
  vector<ll> v;
  for (int p : ps) {
    if (a % p == 0)
      v.push_back(p);
  }
  if (v.size() == 0)
    return 0;
  ll ret = v.back();
  for (int i = 0; i < (int)v.size() - 1; i++) {
    ret -= v[i];
  }
  return ret;
}

int main() {
  for (int i = 2; i <= 2000000; i++) {
    if (is_prime(i))
      ps.push_back(i);
  }

  int n, m;
  while (cin >> n >> m, n + m) {
    if (f(n) > f(m)) {
      cout << "a" << endl;
    } else {
      cout << "b" << endl;
    }
  }
}
Problem p01171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef vector<int> vi;
const int lim = 1000001;

vi prime_fact(int x, const vi &table) {
  vi res;
  while (x > 1) {
    res.push_back(table[x]);
    x /= table[x];
  }
  sort(all(res));
  res.erase(unique(all(res)), res.end());
  return res;
}

int main() {
  vi table(lim, 1);
  for (int i = 2; i * i <= lim; i++) {
    if (table[i] == 1) {
      for (int j = i; j <= lim; j += i)
        table[j] = i;
    }
  }

  int a, b;
  while (cin >> a >> b, a) {
    vi va = prime_fact(a, table), vb = prime_fact(b, table);
    a = 2 * va.back() - accumulate(all(va), 0);
    b = 2 * vb.back() - accumulate(all(vb), 0);
    cout << (a > b ? "a" : "b") << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef vector<int> vi;
const int lim = 1000001;

vi prime_fact(int x, const vi &table) {
  vi res;
  while (x > 1) {
    res.push_back(table[x]);
    x /= table[x];
  }
  sort(all(res));
  res.erase(unique(all(res)), res.end());
  return res;
}

int main() {
  vi table(lim, 1);
  for (int i = 2; i <= lim; i++) {
    if (table[i] == 1) {
      for (int j = i; j <= lim; j += i)
        table[j] = i;
    }
  }

  int a, b;
  while (cin >> a >> b, a) {
    vi va = prime_fact(a, table), vb = prime_fact(b, table);
    a = 2 * va.back() - accumulate(all(va), 0);
    b = 2 * vb.back() - accumulate(all(vb), 0);
    cout << (a > b ? "a" : "b") << endl;
  }
}
Problem p01172 (C++)

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

int gcd(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  for (int r; (r = a % b) != 0;) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int x, y;
  while (cin >> x >> y) {
    const int g = gcd(x, y);
    x /= g;
    y /= g;
    vector<int> seen(y, -1);
    seen[x] = 0;
    for (int c = 1;; c++) {
      x = (10 * x) % y;
      if (x == 0) {
        cout << c << " 0" << endl;
        break;
      } else if (seen[x] != -1) {
        cout << seen[x] << ' ' << c - seen[x] << endl;
        break;
      }
      seen[x] = c;
    }
  }
  return 0;
}

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

int gcd(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  for (int r; (r = a % b) != 0;) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int x, y;
  while (cin >> x >> y && x != 0) {
    const int g = gcd(x, y);
    x /= g;
    y /= g;
    vector<int> seen(y, -1);
    seen[x] = 0;
    for (int c = 1;; c++) {
      x = (10 * x) % y;
      if (x == 0) {
        cout << c << " 0" << endl;
        break;
      } else if (seen[x] != -1) {
        cout << seen[x] << ' ' << c - seen[x] << endl;
        break;
      }
      seen[x] = c;
    }
  }
  return 0;
}
Problem p01172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a) {
    int s[1000000011] = {}, c = 0, g = __gcd(a, b);
    a /= g, b /= g;
    while (!s[a]) {
      s[a] = (++c);
      a = (a * 10) % b;
      if (a < 0)
        return 0;
    }
    if (a)
      c++;
    cout << s[a] - 1 << ' ' << c - s[a] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a) {
    int s[1000000] = {};
    int c = 0, g = __gcd(a, b);
    a /= g, b /= g;
    while (!s[a]) {
      s[a] = (++c);
      a = (a * 10) % b;
      if (a < 0)
        return 0;
    }
    if (a)
      c++;
    cout << s[a] - 1 << ' ' << c - s[a] << endl;
  }
}
Problem p01172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 1000005
using namespace std;
int m[N];
int a, b, cnt, ans1, ans2;

int main() {
  while (1) {
    cin >> a >> b;
    if (!a && !b)
      break;
    memset(m, -1, sizeof(m));
    a %= b;
    cnt = 0;
    while (1) {
      a *= 10;
      if (m[a] != -1) {
        ans1 = m[a], ans2 = cnt - m[a];
        break;
      }
      m[a] = cnt;
      a %= b;
      if (!a) {
        ans1 = cnt + 1, ans2 = 0;
        break;
      }
      cnt++;
    }
    cout << ans1 << ' ' << ans2 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 10000005
using namespace std;
int m[N];
int a, b, cnt, ans1, ans2;

int main() {
  while (1) {
    cin >> a >> b;
    if (!a && !b)
      break;
    memset(m, -1, sizeof(m));
    a %= b;
    cnt = 0;
    while (1) {
      a *= 10;
      if (m[a] != -1) {
        ans1 = m[a], ans2 = cnt - m[a];
        break;
      }
      m[a] = cnt;
      a %= b;
      if (!a) {
        ans1 = cnt + 1, ans2 = 0;
        break;
      }
      cnt++;
    }
    cout << ans1 << ' ' << ans2 << endl;
  }
  return 0;
}
Problem p01172 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

int adiv(int a) {
  map<int, int> mp;
  int num(a);
  for (int i = 2; i <= a; ++i) {
    while (num % i == 0) {
      mp[i]++;
      num /= i;
    }
  }
  int anum = 0;
  for (auto m : mp) {
    anum -= m.first;
  }
  anum += 2 * prev(mp.end())->first;
  return anum;
}

int main() {
  while (true) {

    int x, y;
    cin >> x >> y;
    if (!x)
      break;
    map<int, int> mp;
    vector<int> v(1e6 + 1, -1);
    int ans1 = 0, ans2 = 0;
    while (true) {
      x *= 10;
      int a = x / y;
      int b = x % y;

      if (b == 0) {
        ans1++;
        break;
      } else {
        if (v[x / 10] == -1) {
          v[x / 10] = ans1;
          ans1++;
          x = b;
        } else {
          ans2 = ans1 - v[x];
          ans1 = v[x];
          break;
        }
      }
    }
    cout << ans1 << " " << ans2 << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

int adiv(int a) {
  map<int, int> mp;
  int num(a);
  for (int i = 2; i <= a; ++i) {
    while (num % i == 0) {
      mp[i]++;
      num /= i;
    }
  }
  int anum = 0;
  for (auto m : mp) {
    anum -= m.first;
  }
  anum += 2 * prev(mp.end())->first;
  return anum;
}

int main() {
  while (true) {

    int x, y;
    cin >> x >> y;
    if (!x)
      break;
    map<int, int> mp;
    vector<int> v(1e6 + 1, -1);
    int ans1 = 0, ans2 = 0;
    while (true) {
      x *= 10;
      int a = x / y;
      int b = x % y;

      if (b == 0) {
        ans1++;
        break;
      } else {
        if (v[x / 10] == -1) {
          v[x / 10] = ans1;
          ans1++;
          x = b;
        } else {
          ans2 = ans1 - v[x / 10];
          ans1 = v[x / 10];
          break;
        }
      }
    }
    cout << ans1 << " " << ans2 << endl;
  }
  return 0;
}
Problem p01172 (C++)

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

int main() {
  int x, y;
  while (cin >> x >> y && (x || y)) {
    map<int, int> mp;
    int cnt = 0;
    while (!mp.count(x % y)) {
      if (x % y == 0)
        break;
      mp[x % y] = cnt++;
      x %= y;
      x *= 10;
    }
    if (x % y == 0) {
      cout << cnt << " " << 0 << endl;
    } else {
      cout << mp[x % y] << " " << cnt - mp[x % y] << endl;
    }
  }
}

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

int main() {
  int x, y;
  while (cin >> x >> y && (x || y)) {
    unordered_map<int, int> mp;
    int cnt = 0;
    while (!mp.count(x % y)) {
      if (x % y == 0)
        break;
      mp[x % y] = cnt++;
      x %= y;
      x *= 10;
    }
    if (x % y == 0) {
      cout << cnt << " " << 0 << endl;
    } else {
      cout << mp[x % y] << " " << cnt - mp[x % y] << endl;
    }
  }
}
Problem p01183 (C++)

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

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

using namespace std;

// 三角形が作れるかどうか
bool tri(int a, int b, int c) {
  int p[] = {a, b, c};
  sort(p, p + 3);
  return p[0] + p[1] > p[2];
}

double volume(double U, double V, double W, double u, double v, double w) {
  double X = (w - U + v) * (U + v + w);
  double x = (U - v + w) * (v - w + U);
  double Y = (u - V + w) * (V + w + u);
  double y = (V - w + u) * (w - u + V);
  double Z = (v - W + u) * (W + u + v);
  double z = (W - u + v) * (u - v + W);
  double a = sqrt(x * Y * Z);
  double b = sqrt(y * Z * X);
  double c = sqrt(z * X * Y);
  double d = sqrt(x * y * z);
  return sqrt((-a + b + c + d) * (a - b + c + d) * (a + b - c + d) *
              (a + b + c - d)) /
         (192 * u * v * w);
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    int L[15];
    rep(i, n) scanf("%d", L + i);

    double ans = 0;
    rep(a, n) rep(b, n) if (a != b) rep(c, n) if (a != c && b != c)
        rep(d, n) if (a != d && b != d && c != d)
            rep(e, n) if (a != e && b != e && c != e && d != e)
                rep(f, n) if (a != f && b != f && c != f && d != f && e != f) {
      if (tri(L[a], L[b], L[c]) && tri(L[a], L[e], L[f]) &&
          tri(L[b], L[f], L[d]) && tri(L[c], L[d], L[e]))
        ans = max(ans, volume(L[a], L[b], L[c], L[d], L[e], L[f]));
    }
    printf("%.9f\n", ans);
  }

  return 0;
}

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

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

using namespace std;

// 三角形が作れるかどうか
bool tri(int a, int b, int c) {
  int p[] = {a, b, c};
  sort(p, p + 3);
  return p[0] + p[1] > p[2];
}

double volume(double U, double V, double W, double u, double v, double w) {
  double X = (w - U + v) * (U + v + w);
  double x = (U - v + w) * (v - w + U);
  double Y = (u - V + w) * (V + w + u);
  double y = (V - w + u) * (w - u + V);
  double Z = (v - W + u) * (W + u + v);
  double z = (W - u + v) * (u - v + W);
  double a = sqrt(x * Y * Z);
  double b = sqrt(y * Z * X);
  double c = sqrt(z * X * Y);
  double d = sqrt(x * y * z);
  return sqrt((-a + b + c + d) * (a - b + c + d) * (a + b - c + d) *
              (a + b + c - d)) /
         (192 * u * v * w);
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    int L[15];
    rep(i, n) scanf("%d", L + i);

    double ans = 0;
    int p[15] = {1, 1, 1, 1, 1, 1};
    do {
      int m = 0, q[6];
      rep(i, n) if (p[i] == 1) q[m++] = i;
      do {
        int a = L[q[0]];
        int b = L[q[1]];
        int c = L[q[2]];
        int d = L[q[3]];
        int e = L[q[4]];
        int f = L[q[5]];
        if (tri(a, b, c) && tri(a, e, f) && tri(b, f, d) && tri(c, d, e))
          ans = max(ans, volume(a, b, c, d, e, f));
      } while (next_permutation(q, q + 5)); // 一つの辺は固定していい
    } while (prev_permutation(p, p + n));

    printf("%.9f\n", ans);
  }

  return 0;
}
Problem p01192 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
int c[100];

int greedy(int x) {
  int res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += x / c[i];
    x %= c[i];
  }
  return res;
}

int dp[5000];

void solve() {
  if (c[0] != 1) {
    printf("Cannot pay some amount");
    return;
  }

  fill(dp, dp + 5000, 1e9);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    int val = c[i];
    for (int j = 0; j < 2000; j++) {
      if (j - val >= 0)
        dp[j] = min(dp[j], dp[j - val] + 1);
    }
  }

  for (int num = 0; num < 2000; num++) {
    int cost = greedy(num);
    if (cost != dp[num]) {
      //      cout<<num<<' '<<cost<<' '<<dp[num]<<endl;
      printf("Cannot use greedy algorithm");
      return;
    }
  }

  for (int j = 2000; j < 5000; j++) {

    for (int i = 0; i < n; i++) {
      int val = c[i];
      dp[j] = min(dp[j], dp[j - val] + 1);
    }

    if (dp[j] != dp[j - c[n - 2]] + 1) {
      printf("Cannot use greedy algorithm");
      return;
    }
  }
  printf("OK");
}

int main() {
  for (int tc = 1;; tc++) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &c[i]);
    printf("Case #%d: ", tc);
    solve();
    printf("\n");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
int c[100];

int greedy(int x) {
  int res = 0;
  for (int i = n - 1; i >= 0; i--) {
    res += x / c[i];
    x %= c[i];
  }
  return res;
}

int dp[5000];

void solve() {
  if (c[0] != 1) {
    printf("Cannot pay some amount");
    return;
  }

  fill(dp, dp + 5000, 1e9);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    int val = c[i];
    for (int j = 0; j < 2000; j++) {
      if (j - val >= 0)
        dp[j] = min(dp[j], dp[j - val] + 1);
    }
  }

  for (int num = 0; num < 2000; num++) {
    int cost = greedy(num);
    if (cost != dp[num]) {
      //      cout<<num<<' '<<cost<<' '<<dp[num]<<endl;
      printf("Cannot use greedy algorithm");
      return;
    }
  }

  for (int j = 2000; j < 5000; j++) {

    for (int i = 0; i < n; i++) {
      int val = c[i];
      dp[j] = min(dp[j], dp[j - val] + 1);
    }

    if (dp[j] != dp[j - c[n - 1]] + 1) {
      printf("Cannot use greedy algorithm");
      return;
    }
  }
  printf("OK");
}

int main() {
  for (int tc = 1;; tc++) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &c[i]);
    printf("Case #%d: ", tc);
    solve();
    printf("\n");
  }
  return 0;
}
Problem p01213 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int n;
string S, dp[320][320];
int dp2[320][320];
inline string LCS(string &a1, string &a2) {
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++)
      dp2[i][j] = -1;
  }
  dp2[0][0] = 0;
  for (int i = 0; i <= a1.size(); i++)
    dp[i][0] = "";
  for (int i = 0; i <= a2.size(); i++)
    dp[0][i] = "";
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++) {
      if (i < a1.size() && j < a2.size() && a1[i] == a2[j]) {
        if (dp2[i + 1][j + 1] < dp2[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + a1[i];
          dp2[i + 1][j + 1] = dp2[i][j] + 1;
        }
      }
      if (dp2[i + 1][j] < dp2[i][j]) {
        dp[i + 1][j] = dp[i][j];
        dp2[i + 1][j] = dp2[i][j];
      }
      if (dp2[i][j + 1] < dp2[i][j]) {
        dp[i][j + 1] = dp[i][j];
        dp2[i + 1][j] = dp2[i][j];
      }
    }
  }
  return dp[a1.size()][a2.size()];
}
int main() {
  while (cin >> S) {
    for (int i = 0; i < 102400; i++)
      dp[i / 320][i % 320] = "";
    if (S == "#END")
      break;
    n = S.size();
    string V = "";
    for (int i = 1; i < n; i++) {
      string T = S.substr(0, i), U = S.substr(i, n - i);
      string W = LCS(T, U);
      if (V.size() < W.size())
        V = W;
    }
    cout << V << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int n;
string S, dp[320][320];
int dp2[320][320];
inline string LCS(string &a1, string &a2) {
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++)
      dp2[i][j] = -1;
  }
  dp2[0][0] = 0;
  for (int i = 0; i <= a1.size(); i++)
    dp[i][0] = "";
  for (int i = 0; i <= a2.size(); i++)
    dp[0][i] = "";
  for (int i = 0; i <= a1.size(); i++) {
    for (int j = 0; j <= a2.size(); j++) {
      if (i < a1.size() && j < a2.size() && a1[i] == a2[j]) {
        if (dp2[i + 1][j + 1] < dp2[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + a1[i];
          dp2[i + 1][j + 1] = dp2[i][j] + 1;
        }
      }
      if (dp2[i + 1][j] < dp2[i][j]) {
        dp[i + 1][j] = dp[i][j];
        dp2[i + 1][j] = dp2[i][j];
      }
      if (dp2[i][j + 1] < dp2[i][j]) {
        dp[i][j + 1] = dp[i][j];
        dp2[i][j + 1] = dp2[i][j];
      }
    }
  }
  return dp[a1.size()][a2.size()];
}
int main() {
  while (cin >> S) {
    for (int i = 0; i < 102400; i++)
      dp[i / 320][i % 320] = "";
    if (S == "#END")
      break;
    n = S.size();
    string V = "";
    for (int i = 1; i < n; i++) {
      string T = S.substr(0, i), U = S.substr(i, n - i);
      string W = LCS(T, U);
      if (V.size() < W.size())
        V = W;
    }
    cout << V << endl;
  }
  return 0;
}