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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define f first
#define s second
using namespace std;

int main() {
  int N, x, y, w;
  map<pair<int, int>, int> M;
  vector<pair<int, int>> V;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x >> y >> w;
    M[make_pair(x, y)] = w;
    V.push_back(make_pair(x, y));
  }
  int ans = 0, sum;
  for (int i = 0; i < N; i++) {
    sum = M[make_pair(V[i].f, V[i].s)];
    sum += M[make_pair(V[i].f, V[i].s + 1)];
    sum += M[make_pair(V[i].f + 1, V[i].s)];
    sum += M[make_pair(V[i].f + 1, V[i].s + 1)];
    ans = max(ans, sum);
    sum = M[make_pair(V[i].f, V[i].s)];
    sum += M[make_pair(V[i].f, V[i].s - 1)];
    sum += M[make_pair(V[i].f + 1, V[i].s)];
    sum += M[make_pair(V[i].f + 1, V[i].s - 1)];
    ans = max(ans, sum);
  }
  cout << ans << " / 1" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define f first
#define s second
using namespace std;

int main() {
  int N, x, y, w;
  int a[10000000], b[1000000000];
  vector<pair<pair<int, int>, int>> V;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x >> y >> w;
    V.push_back(make_pair(make_pair(x, y), w));
  }
  // sort(V.begin(),V.end());
  int ans = 0, sum;
  for (int i = 0; i < N; i++) {
    sum = V[i].s;
    for (int j = 1; j < N; j++) {
      if (V[i].f.f == V[j].f.f && V[i].f.s + 1 == V[j].f.s) {
        sum += V[j].s;
      } else if (V[i].f.f + 1 == V[j].f.f && V[i].f.s == V[j].f.s) {
        sum += V[j].s;
      } else if (V[i].f.f + 1 == V[j].f.f && V[i].f.s + 1 == V[j].f.s) {
        sum += V[j].s;
      }
    }
    ans = max(ans, sum);
  }
  cout << ans << " / 1" << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> pp;

int main(void) {
  int n;
  cin >> n;
  int ans = 0;
  vector<pp> a;
  vector<pii> b;
  vector<int> c;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a.push_back(make_pair(x, make_pair(y, z)));
  }
  sort(a.begin(), a.end());

  for (int i = 0; i < n; i++) {
    int l = i;
    map<int, int> t, u;
    int y = a[i].second.first;
    int z = a[i].second.second;
    t[y] = z;
    while (i + 1 < n && a[i].first == a[i + 1].first) {
      i++;
      y = a[i].second.first;
      z = a[i].second.second;
      t[y] = z;
    }
    int j = i;
    if (a[j].first + 1 == a[j + 1].first) {
      j++;
      y = a[j].second.first;
      z = a[j].second.second;
      u[y] = z;
      while (j + 1 < n && a[j].first == a[j + 1].first) {
        j++;
        y = a[j].second.first;
        z = a[j].second.second;
        u[y] = z;
      }
    }
    for (int j = l; j < i + 1; j++) {
      y = a[j].second.first;
      ans = max(ans, t[y] + t[y + 1] + u[y] + u[y + 1]);
      ans = max(ans, t[y] + t[y - 1] + u[y] + u[y - 1]);
    }
  }
  cout << ans << " / 1" << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;
map<int, int> t[101010];
vector<int> u[101010];
int main(void) {
  int n;
  cin >> n;
  int ans = 0;

  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    t[x][y] = z;
    u[x].push_back(y);
    ans = max(ans, z);
  }
  for (int i = 0; i < 100001; i++) {
    if (!u[i].size())
      continue;
    for (int j = 0; j < u[i].size(); j++) {
      int a = u[i][j];
      for (int k = -1; k < 2; k += 2) {
        int sum = t[i][a];
        if (t[i].count(a + k))
          sum += t[i][a + k];
        if (t[i + 1].count(a))
          sum += t[i + 1][a];
        if (t[i + 1].count(a + k))
          sum += t[i + 1][a + k];
        ans = max(ans, sum);
      }
    }
  }
  cout << ans << " / 1" << endl;
}
Problem p01733 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <tuple>
using namespace std;
tuple<int, int, int> x[1 << 17];
int n, maxn, a, b, c, s;
int dx[2][3] = {{1, 1, 0}, {0, -1, -1}}, dy[2][3] = {{0, 1, 1}, {1, 1, 0}};
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    x[i] = make_tuple(a, b, c);
  }
  sort(x, x + n);
  for (int i = 0; i < n; i++) {
    for (int h = 0; h < 2; h++) {
      for (int j = 0; j < 8; j++) {
        s = 0;
        for (int k = 0; k < 3; k++) {
          if ((j / (1 << k)) % 2 == 0) {
            int c = lower_bound(x, x + n,
                                make_tuple(get<0>(x[i]) + dx[h][k],
                                           get<1>(x[i]) + dy[h][k], 0)) -
                    x;
            if (get<0>(x[c]) == get<0>(x[i]) + dx[h][k] &&
                get<1>(x[c]) == get<1>(x[i]) + dy[h][k]) {
              s += get<2>(x[c]);
            }
          }
        }
        maxn = max(maxn, s + get<2>(x[i]));
      }
    }
  }
  cout << maxn << " / 1" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
using namespace std;
tuple<int, int, int> x[1 << 17];
int n, maxn, a, b, c, s;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    x[i] = make_tuple(a, b, c);
  }
  sort(x, x + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 256; j++) {
      s = 0;
      for (int k = 0; k < 8; k++) {
        if ((j / (1 << k)) % 2 == 0) {
          int c = lower_bound(x, x + n,
                              make_tuple(get<0>(x[i]) + dx[k],
                                         get<1>(x[i]) + dy[k], 0)) -
                  x;
          if (get<0>(x[c]) == get<0>(x[i]) + dx[k] &&
              get<1>(x[c]) == get<1>(x[i]) + dy[k]) {
            s += get<2>(x[c]);
          }
        }
      }
      maxn = max(maxn, s + get<2>(x[i]));
    }
  }
  cout << maxn << " / 1" << endl;
  return 0;
}
Problem p01733 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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

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

  int n;
  cin >> n;
  vector<P> v(n);
  map<P, int> mp;
  for (int i = 0; i < n; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    v[i] = P(x, y);
    mp[v[i]] = w;
  }
  int ma = 0;
  for (int i = 0; i < n; i++) {
    int x = v[i].first, y = v[i].second;
    for (int j = 0; j < 8; j += 2) {
      int sum = mp[P(x, y)] + mp[P(x + dx[j], y + dy[j])] +
                mp[P(x + dx[(j + 1) % 8], y + dy[(j + 1) % 8])] +
                mp[P(x + dx[(j + 2) % 8], y + dy[(j + 2) % 8])];
      ma = max(ma, sum);
    }
  }
  cout << ma << " / 1" << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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

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

  int n;
  cin >> n;
  vector<P> v(n);
  map<P, int> mp;
  for (int i = 0; i < n; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    v[i] = P(x, y);
    mp[v[i]] = w;
  }
  int ma = -1;
  for (int i = 0; i < n; i++) {
    int x = v[i].first, y = v[i].second;
    for (int j = 0; j < 8; j += 2) {
      int sum = mp[P(x, y)] + mp[P(x + dx[j], y + dy[j])] +
                mp[P(x + dx[j + 1], y + dy[j + 1])] +
                mp[P(x + dx[j + 2], y + dy[j + 2])];
      ma = max(ma, sum);
    }
  }
  cout << ma << " / " << 1 << endl;
}
Problem p01733 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

map<int, map<int, int>> pop;
int x[123456], y[123456];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int w;
    cin >> x[i] >> y[i] >> w;
    pop[y[i]][x[i]] = w;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = -1; j <= 0; j++) {
      for (int k = -1; k <= 0; k++) {
        int c = 0;
        for (int l = 0; l < 2; l++) {
          for (int m = 0; m < 2; m++) {
            c += pop[y[i] + j + l][x[i] + k + m];
          }
        }
        ans = max(ans, c);
      }
    }
  }
  cout << ans << " / 1" << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

map<int, int> pop[123456];
int x[123456], y[123456];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int w;
    cin >> x[i] >> y[i] >> w;
    pop[y[i]][x[i]] = w;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = -1; j <= 0; j++) {
      for (int k = -1; k <= 0; k++) {
        int c = 0;
        for (int l = 0; l < 2; l++) {
          for (int m = 0; m < 2; m++) {
            c += pop[y[i] + j + l][x[i] + k + m];
          }
        }
        ans = max(ans, c);
      }
    }
  }
  cout << ans << " / 1" << endl;
}
Problem p01733 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> x(N), y(N), w(N);
  map<pair<int, int>, int> W;
  for (int i = 0; i < N; ++i) {
    cin >> x[i] >> y[i] >> w[i];
    W[make_pair(x[i], y[i])] = w[i];
  }

  int ans = 0;
  for (int i = 0; i < N; ++i) {
    int sum = 0;
    for (int dx = 0; dx <= 1; ++dx) {
      for (int dy = 0; dy <= 1; ++dy) {
        auto p = make_pair(x[i] + dx, y[i] + dy);
        if (W.count(p))
          sum += W[p];
      }
    }
    ans = max(ans, sum);

    sum = 0;
    for (int dx = -1; dx <= 0; ++dx) {
      for (int dy = 0; dy <= 1; ++dy) {
        auto p = make_pair(x[i] + dx, y[i] + dy);
        if (W.count(p))
          sum += W[p];
      }
    }
    ans = max(ans, sum);
  }

  cout << ans << " / 1" << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <tuple>

using namespace std;

int main() {
  int N;
  cin >> N;

  map<pair<int, int>, int> W;
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    cin >> W[make_pair(x, y)];
  }

  int ans = 0;
  for (auto pp : W) {
    int x, y;
    tie(x, y) = pp.first;

    int sum = 0;
    for (int dx = 0; dx <= 1; ++dx) {
      for (int dy = 0; dy <= 1; ++dy) {
        auto p = make_pair(x + dx, y + dy);
        sum += W[p];
      }
    }
    ans = max(ans, sum);
  }

  cout << ans << " / 1" << endl;
  return 0;
}
Problem p01742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#define mod 1000000007
using namespace std;
int n, p[22], e[55][22], dp[55][55][22];
string s[55];
int solve(int l, int r, int k) {
  if (r - l == 0)
    return 1;
  if (r - l == 1)
    return p[e[l][k]];
  if (dp[l][r][k])
    return dp[l][r][k] - 1;
  int dp2[55][30] = {0};
  dp2[l][0] = 1;
  for (int a = l + 1; a <= r; a++) {
    int sum[55] = {0};
    for (int b = 1; b <= 26; b++) {
      for (int c = l; c < r; c++)
        sum[c] = (sum[c] + dp2[c][b - 1]) % mod;
      for (int c = a - 1; c >= l; c--) {
        if ((s[c][k] != '?' && s[c][k] != b + 96) ||
            (c + 1 != a && s[c + 1].size() == k + 1))
          break;
        int res = (s[c].size() == k + 1) ? solve(c + 1, a, k + 1)
                                         : solve(c, a, k + 1);
        dp2[a][b] = (dp2[a][b] + 1LL * sum[c] * res) % mod;
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= 26; i++)
    ret = (ret + dp2[r][i]) % mod;
  dp[l][r][k] = ret + 1;
  return ret;
}
int main() {
  cin >> n;
  p[0] = 1;
  for (int i = 1; i <= 20; i++)
    p[i] = 1LL * p[i - 1] * 26 % mod;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = s[i].size() - 1; j >= 0; j--)
      e[i][j] = e[i][j + 1] + (s[i][j] == '?' ? 1 : 0);
  }
  printf("%d\\n", solve(0, n, 0));
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#define mod 1000000007
using namespace std;
int n, p[21], c[50][21], dp[50][51][21];
string s[50];
int solve(int l, int r, int k) {
  if (r - l == 1)
    return p[c[l][k]];
  if (dp[l][r][k])
    return dp[l][r][k] - 1;
  int dp2[51][27] = {0};
  dp2[l][0] = 1;
  for (int a = l + 1; a <= r; a++) {
    for (int b = 1; b <= 26; b++) {
      for (int c = a - 1; c >= l; c--) {
        if ((s[c][k] != '?' && s[c][k] != b + 96) ||
            (c + 1 != a && s[c].size() == k + 1))
          break;
        int res = solve(c, a, k + 1), r = 0;
        for (int d = b - 1; d >= 0; d--)
          r = (r + dp2[c][d]) % mod;
        dp2[a][b] = (dp2[a][b] + 1LL * r * res) % mod;
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= 26; i++)
    ret += dp2[r][i];
  dp[l][r][k] = ret + 1;
  return ret;
}
int main() {
  cin >> n;
  p[0] = 1;
  for (int i = 1; i <= 20; i++)
    p[i] = 1LL * p[i - 1] * 26 % mod;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = s[i].size() - 1; j >= 0; j--)
      c[i][j] = c[i][j + 1] + (s[i][j] == '?' ? 1 : 0);
  }
  printf("%d\\n", solve(0, n, 0));
}
Problem p01742 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
int N;
string S[50];
int dp[50][51][25][128];

int rec(int top, int bottom, int left, int x) {
  if (top >= bottom || x > 'z')
    return 0;
  if (left == 20)
    return top + 1 == bottom;
  if (~dp[top][bottom][left][x])
    return dp[top][bottom][left][x];
  int &ret = dp[top][bottom][left][x];
  ret = rec(top, bottom, left, x + 1);
  for (int i = top; i < bottom; i++) {
    if ((x < 'a' || S[i][left] != '?') && S[i][left] != x)
      return ret;
    ret += 1LL * rec(top, i + 1, left + 1, 'a' - 2) *
           rec(i + 1, bottom, left, x + 1) % mod;
    ret %= mod;
  }
  ret += rec(top, bottom, left + 1, 'a' - 2);
  ret %= mod;
  return ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    S[i] += (char)('a' - 2);
    while (S[i].size() < 21)
      S[i] += (char)('a' - 1);
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(0, N, 0, 'a') << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
int N;
string S[50];
int dp[50][50][25][128];

int rec(int top, int bottom, int left, int x) {
  if (top >= bottom || x > 'z')
    return 0;
  if (left == 20)
    return top + 1 == bottom;
  if (~dp[top][bottom][left][x])
    return dp[top][bottom][left][x];
  int &ret = dp[top][bottom][left][x];
  ret = rec(top, bottom, left, x + 1);
  for (int i = top; i < bottom; i++) {
    if ((x < 'a' || S[i][left] != '?') && S[i][left] != x)
      return ret;
    ret += 1LL * rec(top, i + 1, left + 1, 'a' - 1) *
           rec(i + 1, bottom, left, x + 1) % mod;
    ret %= mod;
  }
  ret += rec(top, bottom, left + 1, 'a' - 2);
  ret %= mod;
  return ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    S[i] += (char)('a' - 2);
    while (S[i].size() < 21)
      S[i] += (char)('a' - 1);
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(0, N, 0, 'a') << endl;
}
Problem p01743 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

char dp[1 << 26];

inline int tapris(int bit) { return (dp[bit]); }

int N, A[5];
int latte[10];
int malta[5];

int main() {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < 1 << 26; i++)
    dp[i] = __builtin_popcount(i);

  cin >> N;

  int64 sum = 0LL;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }

  int proc = 0;
  for (int i = 0; i < 1 << 5; i++) {
    if (tapris(i) <= 1)
      continue;
    int ei = 0;
    for (int j = 0; j < 5; j++) {
      for (int k = j + 1; k < 5; k++) {
        if ((i >> j) & 1 && (i >> k) & 1) {
          latte[ei] |= 1 << proc;
        }
        ei++;
      }
    }
    for (int j = 0; j < 5; j++) {
      if ((i >> j) & 1)
        malta[j] |= 1 << proc;
    }
    ++proc;
  }

  int64 ret = 0;
  for (int i = 0; i < 1 << proc; i++) {
    bool iz = true;
    for (int j = 0; j < 10; j++) {
      iz &= tapris(i & latte[j]) <= 1;
    }
    int64 beet = 0LL;
    for (int j = 0; j < 5; j++) {
      int get = tapris(i & malta[j]);
      iz &= get <= A[j];
      beet += get;
    }
    if (iz)
      ret = max(ret, beet - tapris(i));
  }

  cout << sum - ret << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

int N, A[5];
int latte[10];
int malta[5];

int main() {
  cin >> N;

  int64 sum = 0LL;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }

  int proc = 0;
  for (int i = 0; i < 1 << 5; i++) {
    if (__builtin_popcount(i) <= 1)
      continue;
    int ei = 0;
    for (int j = 0; j < 5; j++) {
      for (int k = j + 1; k < 5; k++) {
        if ((i >> j) & 1 && (i >> k) & 1) {
          latte[ei] |= 1 << proc;
        }
        ei++;
      }
    }
    for (int j = 0; j < 5; j++) {
      if ((i >> j) & 1)
        malta[j] |= 1 << proc;
    }
    ++proc;
  }

  int64 ret = 0;
  for (int i = 0; i < 1 << proc; i++) {
    bool iz = true;
    for (int j = 0; j < 10; j++) {
      iz &= __builtin_popcount(i & latte[j]) <= 1;
    }
    int64 beet = 0LL;
    for (int j = 0; j < 5; j++) {
      int get = __builtin_popcount(i & malta[j]);
      iz &= get <= A[j];
      beet += get;
    }
    if (iz)
      ret = max(ret, beet - __builtin_popcount(i));
  }

  cout << sum - ret << endl;
}
Problem p01743 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
int n;

ll a[5];

int main(void) {
  scanf("%d", &n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  sort(a, a + n);
  if (n == 1)
    printf("%lld\\n", sum);
  if (n == 2)
    printf("%lld\\n", sum - 1LL);
  if (n == 3)
    printf("%lld\\n", sum - 3LL);
  if (n == 4) {
    if (a[0] >= 3)
      printf("%lld\\n", sum - 6LL);
    else if (a[3] >= 3)
      printf("%lld\\n", sum - 5LL);
    else
      printf("%lld\\n", sum - 4LL);
  }
  if (n == 5) {
    if (a[0] >= 4)
      printf("%lld\\n", sum - 10LL);
    else if (a[0] == 3) {
      if (a[3] >= 4)
        printf("%lld\\n", sum - 9LL);
      else
        printf("%lld\\n", sum - 8LL);
    } else {
      if (a[1] >= 3)
        printf("%lld\\n", sum - 8LL);
      else if (a[3] >= 3 || a[4] >= 4)
        printf("%lld\\n", sum - 7LL);
      else
        printf("%lld\\n", sum - 6LL);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
int n;

ll a[5];

int main(void) {
  scanf("%d", &n);
  ll res = 0;
  vector<ll> a;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  sort(a.begin(), a.end());
  if (n == 1)
    printf("%lld\\n", sum);
  if (n == 2)
    printf("%lld\\n", sum - 1LL);
  if (n == 3)
    printf("%lld\\n", sum - 3LL);
  if (n == 4) {
    if (a[0] >= 3)
      printf("%lld\\n", sum - 6LL);
    else if (a[3] >= 3)
      printf("%lld\\n", sum - 5LL);
    else
      printf("%lld\\n", sum - 4LL);
  }
  if (n == 5) {
    if (a[0] >= 4)
      printf("%lld\\n", sum - 10LL);
    else if (a[0] == 3) {
      if (a[2] >= 4)
        printf("%lld\\n", sum - 9LL);
      else
        printf("%lld\\n", sum - 8LL);
    } else {
      if (a[1] >= 3)
        printf("%lld\\n", sum - 8LL);
      else if (a[3] >= 3)
        printf("%lld\\n", sum - 7LL);
      else if (a[4] >= 3)
        printf("%lld\\n", sum - 6LL);
      else
        printf("%lld\\n", sum - 5LL);
    }
  }

  printf("%lld\\n", res);
  return 0;
}
Problem p01743 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

bool canUseVer(int v, int ver, vector<long long> &c, bool u[20][20]) {
  rep(i, c.size()) {
    if (u[c[i]][ver])
      return false;
  }
  return true;
}

bool f;
int st;
vector<long long> dfs(vector<long long> c, int v, int n, bool u[20][20]) {
  if (n == c.size()) {
    return c;
  }

  vector<long long> res;
  int s = c.empty() ? 0 : c.back() + 1;
  s = f and s == 0 ? st : s;
  range(i, s, v) {
    if (canUseVer(v, i, c, u)) {
      c.emplace_back(i);
      res = dfs(c, v, n, u);
      if (res.size() == n)
        return res;
      c.pop_back();
    }
  }
  return vector<long long>();
}

bool search(bool u[20][20], int v, int n) {
  vector<long long> tmp;
  vector<long long> c = dfs(tmp, v, n, u);
  if (c.size() == n) {
    for (auto i : c) {
      // cout << i << ' ' ;
      for (auto j : c) {
        u[i][j] = u[j][i] = true;
      }
    }
    // cout << endl;
    return true;
  }
  return false;
}

bool C(int v, vector<long long> &a) {
  bool u[20][20] = {{0}};
  rep(i, a.size()) {
    f = true;
    st = i / 2;
    if (not search(u, v, a[i])) {
      return false;
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;

  long long ans = 0;
  vector<long long> a(n);
  rep(i, n) {
    cin >> a[i];
    ans += max<long long>(a[i] - 4L, 0L);
    a[i] -= max<long long>(a[i] - 4L, 0L);
  }
  sort(all(a), greater<long long>());

  int right = 20, left = 1;
  rep(i, 10) {
    int mid = (right + left) / 2;
    if (C(mid, a))
      right = mid;
    else
      left = mid;
  }
  C(10, a);
  cout << ans + right << endl;
}


Error: 0
ans = 3
ans = 5


Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

int main() {
  int n;
  cin >> n;

  vector<long long> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(all(a), greater<long long>());

  long long ans = 0;
  rep(i, n) {
    bool f = false;
    rep(j, i) {
      if (a[j] > 0 and a[i] > 0) {
        f = true;
        a[i]--;
        a[j]--;
      }
    }
    if (not f and i)
      ans--;
    ans += a[i];
    show(ans)
  }
  cout << ans << endl;
}