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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n;
bool flg;

void rec(vector<int> c, int sum, vector<int> &vv) {
  if (sum == 0) {
    flg = true;
    return;
  }

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

    if (c[i] && sum % i == 0) {
      int m = i;
      vv.push_back(i);
      c[i]--;
      rec(c, sum - m, vv);
      if (flg)
        return;
      vv.pop_back();
      c[i]++;
    }
  }
}

int main() {
  while (cin >> n, n) {
    int sum = 0, a;
    vector<int> c(14, 0);

    for (int i = 0; i < n; i++) {
      cin >> a;
      sum += a;
      c[a]++;
    }

    flg = false;
    vector<int> v;
    rec(c, sum, v);

    if (v.size()) {
      int s = (int)v.size();
      for (int i = 0; i < s; i++) {
        if (!i)
          cout << v[s - 1 - i];
        else
          cout << " " << v[s - 1 - i];
      }
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n;
bool flg;

void rec(vector<int> a, int sum, int n, vector<int> &vv) {
  if (sum == 0) {
    flg = true;
    return;
  }

  vector<int> tmp;
  tmp = a;

  for (int i = 0; i < n; i++) {

    if (sum % a[i] == 0) {
      int m = a[i];
      vv.push_back(a[i]);
      a.erase(a.begin() + i);
      rec(a, sum - m, a.size(), vv);
      if (flg)
        return;
      vv.pop_back();
      a = tmp;
    }
  }
}

int main() {
  while (cin >> n, n) {
    int sum = 0;
    vector<int> a(n);

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    flg = false;
    vector<int> v;
    rec(a, sum, n, v);

    if (v.size()) {
      int s = (int)v.size();
      for (int i = 0; i < s; i++) {
        if (!i)
          cout << v[s - 1 - i];
        else
          cout << " " << v[s - 1 - i];
      }
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;

int u[14] = {}, n, sum, tot;
int w[52];
int memo[1000][52];

int f(int s) {
  if (memo[sum][s] >= 10000)
    return false;
  else
    memo[sum][s]++;

  if (s == 0) {
    for (int i = n; i > 0; i--) {
      cout << w[i] << (i != 1 ? " " : "\\n");
    }
    return true;
  }
  if (s == n) {
    for (int i = 1; i <= 13; i++) {
      if (u[i]) {
        --u[i];
        sum += i;
        w[s] = i;
        if (f(s - 1))
          return true;
        sum -= i;
        ++u[i];
      }
    }
  } else {

    bool chk = 0;
    for (int i = 1; i <= 13; i++) {
      if (u[i] && tot % i == 0)
        chk = true;
    }

    if (!chk) {

      return false;
    }
    for (int i = 1; i <= 13; i++) {
      if (u[i] && sum % i == 0) {
        --u[i];
        sum += i;
        w[s] = i;
        if (f(s - 1))
          return true;
        sum -= i;
        ++u[i];
      }
    }
  }
  return false;
}
int main() {
  while (cin >> n && n) {

    tot = 0;
    memset(memo, 0, sizeof(memo));
    for (int i = 1; i <= 13; i++) {
      u[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      ++u[a];
      tot += a;
    }
    sum = 0;
    if (!f(n))
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;

int u[14] = {}, n, sum, tot;
int w[52];
int memo[360361][52];

int f(int s) {
  if (memo[sum][s] >= 50000)
    return false;
  else
    memo[sum][s]++;

  if (s == 0) {
    for (int i = n; i > 0; i--) {
      cout << w[i] << (i != 1 ? " " : "\\n");
    }
    return true;
  }
  if (s == n) {
    for (int i = 1; i <= 13; i++) {
      if (u[i]) {
        --u[i];
        sum += i;
        w[s] = i;
        if (f(s - 1))
          return true;
        sum -= i;
        ++u[i];
      }
    }
  } else {

    bool chk = 0;
    for (int i = 1; i <= 13; i++) {
      if (u[i] && tot % i == 0)
        chk = true;
    }

    if (!chk) {

      return false;
    }
    for (int i = 1; i <= 13; i++) {
      if (u[i] && sum % i == 0) {
        --u[i];
        sum += i;
        w[s] = i;
        if (f(s - 1))
          return true;
        sum -= i;
        ++u[i];
      }
    }
  }
  return false;
}
int main() {
  while (cin >> n && n) {

    tot = 0;
    memset(memo, 0, sizeof(memo));
    for (int i = 1; i <= 13; i++) {
      u[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      ++u[a];
      tot += a;
    }
    sum = 0;
    if (!f(n))
      cout << "No" << endl;
  }
}
Problem p01153 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int n;
int k;
int a[101];

int dp[101][101];
int dfs(int from, int to) {
  if (dp[from][to] != -1)
    return dp[from][to];
  int res = 0;
  for (int i = (from + 1) % k; i != (to + 1) % k; i = (i + 1) % k)
    res =
        max(res, abs(a[from] - a[i]) + dfs(from, (i - 1 + k) % k) + dfs(i, to));
  return dp[from][to] = res;
}

int main() {

  cin >> n;
  while (n--) {
    memset(dp, -1, sizeof(dp));
    cin >> k;
    for (int i = 0; i < k; i++)
      cin >> a[i];
    int res = 0;
    for (int i = 0; i < k; i++)
      res = max(res, dfs(i, (i - 1 + k) % k));
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int n;
int k;
int a[101];

int dp[101][101];
int dfs(int from, int to) {
  if (dp[from][to] != -1)
    return dp[from][to];
  int res = 0;
  for (int i = (from + 1) % k; i != (to + 1) % k; i = (i + 1) % k)
    res =
        max(res, abs(a[from] - a[i]) + dfs(from, (i - 1 + k) % k) + dfs(i, to));
  return dp[from][to] = res;
}

int main() {

  cin >> n;
  while (n--) {
    cin >> k;
    for (int i = 0; i < k; i++)
      cin >> a[i];
    int res = 0;
    for (int i = 0; i < k; i++) {
      memset(dp, -1, sizeof(dp));
      res = max(res, dfs(i, (i - 1 + k) % k));
    }
    cout << res << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

int dp[101][101], a[100];

int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> a[i];

    memset(dp, 0, sizeof(dp));
    int ans = 0;
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = j; k < j + i; k++) {
          ans = max(ans, dp[j][(j + i) % n] =
                             max(dp[j][(j + i) % n],
                                 dp[j][k % n] + dp[(k + 1) % n][(j + i) % n] +
                                     abs(a[j] - a[(k + 1) % n])));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int dp[101][101], a[100];

int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> a[i];

    int ans = 0;
    for (int p = 0; p < n; p++) {
      memset(dp, 0, sizeof(dp));
      for (int i = 1; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int k = j; k < j + i; k++) {
            ans = max(ans, dp[(j + p) % n][(j + i + p) % n] = max(
                               dp[(j + p) % n][(j + i + p) % n],
                               dp[(j + p) % n][(k + p) % n] +
                                   dp[(k + 1 + p) % n][(j + i + p) % n] +
                                   abs(a[(j + p) % n] - a[(k + 1 + p) % n])));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01153 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int b[110];
int dp[110][110];
int tmp[110][110];
int n;
inline int ABS(int a) { return max(a, -a); }
int ad[110][110];
int solve(int l, int r) {
  if (~dp[l][r])
    return dp[l][r];
  if (l == r)
    return dp[l][r] = 0;
  int sz = (r - l + n) % n;
  for (int i = 0; i <= sz; i++) {
    tmp[l][i] = -99999999;
  }
  tmp[l][0] = 0;
  for (int i = 0; i < sz; i++) {
    dp[l][ad[l][i]] = tmp[l][i];
    int t = ABS(b[l] - b[ad[l + 1][i]]);
    for (int j = sz - i; j >= 1; j--) {
      tmp[l][i + j] = max(
          tmp[l][i + j], tmp[l][i] + t + solve(ad[l][i + 1] % n, ad[l][i + j]));
    }
  }
  // printf("%d %d: %d\\n",l,r,tmp[(r-l+n)%n]);
  return dp[l][r] = tmp[l][sz];
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int a;
    scanf("%d", &a);
    for (int i = 0; i < a; i++)
      scanf("%d", b + i);
    n = a;
    for (int i = 0; i < 110; i++)
      for (int j = 0; j < 110; j++)
        ad[i][j] = (i + j) % n;
    int ret = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        dp[i][j] = -1;
    for (int i = 0; i < a; i++) {
      ret = max(ret, solve(i, (i + a - 1) % a));
    }
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int b[110];
int dp[110][110];
int tmp[110][110];
int n;
inline int ABS(int a) { return max(a, -a); }
int solve(int l, int r) {
  if (~dp[l][r])
    return dp[l][r];
  if (l == r)
    return dp[l][r] = 0;
  int sz = (r - l + n) % n;
  for (int i = 0; i <= sz; i++) {
    tmp[l][i] = -99999999;
  }
  tmp[l][0] = 0;
  for (int i = 0; i < sz; i++) {
    dp[l][(l + i) % n] = tmp[l][i];
    int t = ABS(b[l] - b[(l + i + 1) % n]);
    for (int j = sz - i; j >= 1; j--) {
      tmp[l][i + j] =
          max(tmp[l][i + j],
              tmp[l][i] + t + solve((l + i + 1) % n, (l + i + j) % n));
    }
  }
  // printf("%d %d: %d\\n",l,r,tmp[(r-l+n)%n]);
  return dp[l][r] = tmp[l][sz];
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int a;
    scanf("%d", &a);
    for (int i = 0; i < a; i++)
      scanf("%d", b + i);
    n = a;
    int ret = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        dp[i][j] = -1;
    for (int i = 0; i < a; i++) {
      ret = max(ret, solve(i, (i + a - 1) % a));
    }
    printf("%d\\n", ret);
  }
}
Problem p01153 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

const int INF = 1000000000;

int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];

    int dp[101][101] = {};
    REP(i, n) REP(j, n) dp[i][j] = -INF;
    REP(i, n) dp[i][i] = 0;
    for (int l = 1; l < n; l++) {
      for (int i = 0; i < n; i++) {
        int j = (i + l) % n;
        int max_k = 0;
        for (int h = 0; h < l; h++) {
          int k = (i + h) % n;
          int k2 = (i + h + 1) % n;
          int val = dp[i][k] + dp[k2][j] + abs(a[i] - a[k2]);
          if (dp[i][j] < val) {
            max_k = k;
            dp[i][j] = val;
          }
        }
        // printf("dp[%d][%d] = %d (dp[%d][%d] + dp[%d][%d])\\n", i, j, dp[i][j],
        // i, max_k, (max_k + 1) % n, j);
      }
    }

    int ans = -INF;
    REP(i, n) ans = max(ans, dp[i][(i + n - 1) % n]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

void update(int &x, int y) {
  if (x < y)
    x = y;
}

int calc(vector<int> &v) {
  int n = v.size();
  int dp[111][111][2] = {};
  const int INF = 100000000;
  REP(i, n + 1) REP(j, n + 1) dp[i][j][0] = dp[i][j][1] = -INF;
  dp[0][0][0] = 0;
  int ans = 0;
  for (int i = 0; i + 1 < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k <= i; k++) {
        if (dp[i][j][0] >= 0) {
          if (j == k || k == i)
            update(dp[i + 1][k][0], dp[i][j][0] + abs(v[i + 1] - v[k]));
          if (j > k)
            update(dp[i + 1][k][1], dp[i][j][0] + abs(v[i + 1] - v[k]));
        }
        if (dp[i][j][1] >= 0 && j >= k) {
          update(dp[i + 1][k][1], dp[i][j][1] + abs(v[i + 1] - v[k]));
        }
        // printf("dp[%d][%d][%d] = %d\\n", i + 1, k, 0, dp[i + 1][k][0]);
        // printf("dp[%d][%d][%d] = %d\\n", i + 1, k, 1, dp[i + 1][k][1]);
        update(ans, dp[i + 1][k][0]);
        update(ans, dp[i + 1][k][1]);
      }
    }
  }

  return ans;
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    vector<int> b(n);
    int ans = 0;
    REP(s, n) {
      REP(i, n) b[i] = a[(s + i) % n];
      ans = max(ans, calc(b));
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01153 (C++) — buggy submission, verdict: 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 = 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;
}

Error: TLE

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 = 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;
}
Problem p01153 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

#pragma warning(disable : 4996)
using namespace std;
string root_sts = "C ,C#,D ,D#,E ,F ,F#,G ,G#,A ,A#,B ";

int solve(int l, int r, const vector<int> &nums, vector<vector<int>> &dp) {
  if (dp[l][r] != -1) {
  } else {
    if (l == r)
      dp[l][r] = 0;
    else if (l + 1 == r)
      dp[l][r] = 0;
    else {
      vector<int> memo(r - l);
      memo[0] = 0;
      for (int x = 0; x < r - l - 1; ++x) {
        for (int num = 1; num <= r - l - x - 1; ++num) {
          int plus = abs(nums[l] - nums[l + x + 1]) +
                     solve(l + x + 1, l + x + num + 1, nums, dp);
          memo[x + num] = max(memo[x + num], memo[x] + plus);
        }
      }
      dp[l][r] = memo[r - l - 1];
    }
  }
  return dp[l][r];
}

int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    int N;
    cin >> N;
    vector<int> nums(N);
    for (int i = 0; i < N; ++i)
      cin >> nums[i];
    nums.insert(nums.end(), nums.begin(), nums.end());
    vector<vector<int>> dp(2 * N, vector<int>(2 * N, -1));
    int ans = 0;
    for (int i = 0; i < N; ++i) {
      ans = max(ans, solve(i, i + N, nums, dp));
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

int memo[100][100][101];
vector<int> ns;

int next(int a) { return (a + 1) % ns.size(); }
int solve(int l, int r, int from) {
  if (memo[l][r][from] != -1)
    return memo[l][r][from];
  else {
    if (l == r) {
      return memo[l][r][from] = 0;
    } else if (next(l) == r) {
      return memo[l][r][from] = abs(from - ns[l]);
    }
    int ans = 0;
    ans = max(ans, solve(next(l), r, from) + abs(from - ns[l]));
    ans = max(ans, solve(next(l), r, ns[l]) + abs(from - ns[l]));
    for (int m = next(l); next(m) != r; m = next(m)) {
      int nans =
          solve(next(l), m, ns[l]) + abs(from - ns[l]) + solve(m, r, from);
      ans = max(ans, nans);
    }
    return memo[l][r][from] = ans;
  }
}
int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    for (int i = 0; i < 100; ++i) {
      for (int j = 0; j < 100; ++j) {
        for (int k = 0; k < 101; ++k) {
          memo[i][j][k] = -1;
        }
      }
    }
    int T;
    cin >> T;
    ns.clear();
    for (int i = 0; i < T; ++i) {
      int a;
      cin >> a;
      ns.push_back(a);
    }
    int ans = 0;
    for (int i = 0; i < T; ++i) {
      ans = max(solve(next(i), i, ns[i]), ans);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01155 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int func(vector<int> num) {
  int ans = 0;
  for (int i = 0; i < num.size() - 1; i++)
    ans += (num[i + 1] - num[i]) * (num[i + 1] - num[i]);
  return ans;
}

int main(void) {

  int a, b, tmp;
  vector<int> num;

  while (cin >> a >> b, a | b) {
    int res = INT_MAX;
    for (int i = 1; i * i <= a; i++)
      for (int j = 1; j * j <= b; j++)
        if (a % i == 0 && b % j == 0) {
          num.clear();
          num.push_back(a / i);
          num.push_back(i);
          num.push_back(b / j);
          num.push_back(j);
          sort(num.begin(), num.end());
          res = min(res, func(num));
        }

    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int main(void) {

  int a, b, i, tmp;
  vector<int> num;

  while (cin >> a >> b, a | b) {
    num.clear();

    for (i = sqrt(a) + 1; i <= a; i++) {
      if (a % i == 0) {
        num.push_back(i);
        num.push_back(a / i);
        break;
      }
    }

    for (i = sqrt(b) + 1; i <= b; i++) {
      if (b % i == 0) {
        num.push_back(i);
        num.push_back(b / i);
        break;
      }
    }
    sort(num.begin(), num.end());

    long long sum = 0;
    for (i = 0; i < num.size() - 1; i++)
      sum += (num[i + 1] - num[i]) * (num[i + 1] - num[i]);

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
#define r(i, a, n) for (int i = a; i < n; i++)
#define R(i, a, n) for (int i = a; i > n; i--)
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a) {
    int aa[5001][2], bb[5001][2], c = 0, cc = 0;
    bool ac[10001], bc[10001];
    r(i, 0, 10001) ac[i] = 0;
    r(i, 0, 10001) bc[i] = 0;
    r(i, 1, a + 1) {
      if (!ac[i] && a % i == 0)
        R(j, a, 0) {
          if (i * j == a) {
            aa[c][0] = i;
            ac[i] = 1;
            ac[j] = 1;
            aa[c++][1] = j;
            break;
          }
        }
    }
    r(i, 1, b + 1) {
      if (!bc[i] && b % i == 0)
        R(j, b, 0) {
          if (i * j == b) {
            bb[cc][0] = i;
            bc[i] = 1;
            bc[j] = 1;
            bb[cc++][1] = j;
            break;
          }
        }
    } // cout<<bb[0][0]<<bb[0][1];
    long long l[7];
    long long m = 999999999999;
    r(i, 0, c) {
      r(j, 0, cc) {
        int t[4];
        t[0] = aa[i][0];
        t[1] = aa[i][1];
        t[2] = bb[j][0];
        t[3] = bb[j][1];
        sort(t, t + 4);
        l[0] = t[1] - t[0];
        l[1] = t[2] - t[1];
        l[2] = t[3] - t[2];
        m = min(m, l[0] * l[0] + l[1] * l[1] + l[2] * l[2]);
      }
    }
    // if(m==100000000)m=0;
    cout << m << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, a, n) for (int i = a; i < n; i++)
#define R(i, a, n) for (int i = a; i > n; i--)
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a) {
    int aa[a / 2 + 1][2], bb[b / 2 + 1][2], c = 0, cc = 0;
    r(i, 1, a / 2) {
      R(j, a, 0) {
        if (i * j == a) {
          aa[c][0] = i;
          aa[c++][1] = j;
          break;
        }
      }
    }
    r(i, 1, b / 2) {
      R(j, b, 0) {
        if (i * j == b) {
          bb[cc][0] = i;
          bb[cc++][1] = j;
          break;
        }
      }
    } // cout<<bb[0][0]<<bb[0][1];
    int l[7], m = 100000000, p;
    r(i, 0, c) {
      r(j, 0, cc) { // cout<<1;
        int t[4];
        t[0] = aa[i][0];
        t[1] = aa[i][1];
        t[2] = bb[j][0];
        t[3] = bb[j][1];
        sort(t, t + 4);
        p = 0;
        r(i, 0, 3) {
          r(j, i + 1, 4) { l[p++] = t[j] - t[i]; }
        }
        sort(l, l + p);
        m = min(m, l[0] * l[0] + l[1] * l[1] + l[2] * l[2]);
      }
    }
    cout << m << endl;
  }
}
Problem p01156 (C++) — buggy submission, verdict: 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 (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;
}

Error: 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 (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;
}