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

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

using namespace std;

#define fo(i, n) for (int i = 1; i <= n; ++i)

typedef long long ll;

#define pb push_back

const int N = 250000;
int n, a, b, c, d;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i + 2 < s.length(); ++i) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      s[i] = 'B';
      s[i + 1] = 'C';
      s[i + 2] = 'A';
      ans++;
      i -= 2;
    }
  }
  cout << ans;
  return 0;
}

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

using namespace std;

#define fo(i, n) for (int i = 1; i <= n; ++i)

typedef long long ll;

#define pb push_back

const int N = 250000;
int n, a, b, c, d;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  ll ans = 0, cnta = 0;

  for (int i = 0; i + 1 < s.length(); ++i) {
    if (s[i] == 'A')
      cnta++;
    else if (s[i] == 'B' && s[i + 1] == 'C') {
      ans += cnta;
      i++;
    } else
      cnta = 0;
  }
  cout << ans;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  string s;
  cin >> s;
  vector<string> a;
  int begin = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A') {
    } else if (s[i] == 'B' && s[i + 1] == 'C')
      i++;
    else {
      a.push_back(s.substr(begin, i - begin));
      begin = i + 1;
    }
  }
  // cout << 1 << endl;
  // cout << begin << endl;
  if (s.size() - 1 > begin)
    a.push_back(s.substr(begin, s.size() - begin + 1));
  ll res = 0;
  cout << a[0].size() << endl;
  // cout << 2 << endl;
  // cout << a[1] << endl;
  for (int i = 0; i < a.size(); i++) {
    ll count = 0;
    string sub;
    sub = a[i];
    // cout << "sub " << sub << endl;
    for (int j = sub.size() - 1; j >= 0; j--) {
      // cout << sub[j] << endl;
      if (sub[j] == 'C') {
        count++;
        j--;
      }
      if (sub[j] == 'A')
        res += count;
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  string s;
  cin >> s;
  vector<string> a;
  int begin = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A') {
    } else if (s[i] == 'B' && s[i + 1] == 'C')
      i++;
    else {
      a.push_back(s.substr(begin, i - begin));
      begin = i + 1;
    }
  }
  // cout << 1 << endl;
  // cout << begin << endl;
  if (s.size() - 1 > begin)
    a.push_back(s.substr(begin, s.size() - begin + 1));
  ll res = 0;
  // cout << a[0].size() << endl;
  // cout << 2 << endl;
  // cout << a[1] << endl;
  for (int i = 0; i < a.size(); i++) {
    ll count = 0;
    string sub;
    sub = a[i];
    // cout << "sub " << sub << endl;
    for (int j = sub.size() - 1; j >= 0; j--) {
      // cout << sub[j] << endl;
      if (sub[j] == 'C') {
        count++;
        j--;
      }
      if (sub[j] == 'A')
        res += count;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000003
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

// AGC034
signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n, x;
  cin >> n >> x;
  vector<int> b(n), l(n), u(n), val(n);
  rep(i, n) {
    cin >> b[i] >> l[i] >> u[i];
    val[i] = b[i] * l[i] + (x - b[i]) * u[i];
  }

  vector<int> num(n);
  iota(all(num), 0);
  sort(all(num), [&](int s, int t) { return val[s] >= val[t]; });
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000003
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

// AGC034
signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n, x;
  cin >> n >> x;
  vector<int> b(n), l(n), u(n), val(n);
  rep(i, n) {
    cin >> b[i] >> l[i] >> u[i];
    val[i] = b[i] * l[i] + (x - b[i]) * u[i];
  }

  vector<int> num(n);
  iota(all(num), 0);
  sort(all(num), [&](int s, int t) { return val[s] > val[t]; });

  vector<int> sums(n + 1);
  sums[0] = 0;
  rep(i, n) {
    int j = num[i];
    sums[i + 1] = sums[i] + val[j];
  }

  int score = 0;
  rep(i, n) score += b[i] * l[i];

  int left = -1, right = n * x + 1;
  while (right - left > 1) {
    int mid = (right + left) / 2;
    int take_all = mid / x;
    int incmp = mid - take_all * x;
    int final_sum = 0;

    rep(i, n) {
      int j = num[i];
      int tmp_sum;
      if (i < take_all and take_all < n)
        tmp_sum = sums[take_all + 1] - val[j];
      else
        tmp_sum = sums[take_all];
      tmp_sum +=
          max<int>(0, incmp - b[j]) * u[j] + min<int>(incmp, b[j]) * l[j];
      chmax(final_sum, tmp_sum);
    }

    if (final_sum >= score)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;
  return 0;
}
Problem p03019 (C++)

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////
#define INF 1e15
ll N, X, b[101010];
ll l[101010], u[101010];
vector<pll> mxv;
ll mk[101010];
ll i, j, k, aa, bb, dd, bs, ans;
int main() {
  scanf("%lld%lld", &N, &X);
  bs = 0;
  fornum(i, 0, N) {
    scanf("%lld%lld%lld", &b[i], &l[i], &u[i]);
    bs += l[i] * b[i];
    mxv.push_back({u[i] * (X - b[i]) + l[i] * b[i], i});
  }
  sort(mxv.begin(), mxv.end(), greater<pll>());
  // printf("%lld\n", bs);
  while (bb < N && aa < bs) {
    aa += mxv[bb].first;
    ll a = mxv[bb].second;
    mk[a] = 1;
    dd += X;
    // printf("%lld %lld\n", mxv[i].second, bs);
    bb++;
  }
  bb--;
  ans = INF;
  fornum(i, 0, bb) {
    ll cc = bs - (aa - mxv[i].first);
    ll ii = mxv[i].second;
    ll a = dd - X;
    if (cc <= b[ii] * l[ii]) {
      a += (cc - 1) / l[ii] + 1;
    } else {
      a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
    }
    if (a < ans) {
      ans = a;
    }
    // printf("%llda\n", ans);
  }
  fornum(i, bb, N) {
    ll cc = bs - (aa - mxv[bb].first);
    ll ii = mxv[i].second;
    ll a = dd - X;
    if (cc <= b[ii] * l[ii]) {
      a += (cc - 1) / l[ii] + 1;
    } else {
      a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
    }
    if (a < ans) {
      ans = a;
    }
    // printf("%lld\n", ans);
  }
  printf("%lld", ans);
  return 0;
}

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////
#define INF 1e15
ll N, X, b[101010];
ll l[101010], u[101010];
vector<pll> mxv;
ll mk[101010];
ll i, j, k, aa, bb, dd, bs, ans;
int main() {
  scanf("%lld%lld", &N, &X);
  bs = 0;
  fornum(i, 0, N) {
    scanf("%lld%lld%lld", &b[i], &l[i], &u[i]);
    bs += l[i] * b[i];
    mxv.push_back({u[i] * (X - b[i]) + l[i] * b[i], i});
  }
  sort(mxv.begin(), mxv.end(), greater<pll>());
  // printf("%lld\n", bs);
  if (bs == 0) {
    printf("0");
    return 0;
  }
  while (bb < N && aa < bs) {
    aa += mxv[bb].first;
    ll a = mxv[bb].second;
    mk[a] = 1;
    dd += X;
    // printf("%lld %lld\n", mxv[i].second, bs);
    bb++;
  }
  bb--;
  ans = INF;
  fornum(i, 0, bb) {
    ll cc = bs - (aa - mxv[i].first);
    ll ii = mxv[i].second;
    ll a = dd - X;
    if (cc <= b[ii] * l[ii]) {
      a += (cc - 1) / l[ii] + 1;
    } else {
      a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
    }
    if (a < ans) {
      ans = a;
    }
    // printf("%llda\n", ans);
  }
  fornum(i, bb, N) {
    ll cc = bs - (aa - mxv[bb].first);
    ll ii = mxv[i].second;
    ll a = dd - X;
    if (cc <= b[ii] * l[ii]) {
      a += (cc - 1) / l[ii] + 1;
    } else {
      a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
    }
    if (a < ans) {
      ans = a;
    }
    // printf("%lld\n", ans);
  }
  printf("%lld", ans);
  return 0;
}
Problem p03019 (C++)

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

/*
  REMEMBER:
  1.Think TWICE, code ONCE!
  Are there any counterexamples to your algo?

  2.Be careful about the BOUNDARIES in your algo!
  N=1? P=1? Is P a prime? Something about 0?

  3.Never make STUPID MISTAKES!
  Integer overflow? Array size? Time complexity? Memory usage? Precition error?
*/

const int _ = 1e5 + 7;
struct test {
  long long B, L, R, benefit;
  friend bool operator<(test p, test q) { return p.benefit > q.benefit; }
} now[_];
long long N, X, cost;

bool check(long long mid) {
  int t = mid / X + 1;
  long long lft = mid % X;
  long long sum = 0, mx = 0;
  for (int i = 1; i <= t; ++i)
    sum += now[i].benefit;
  for (int i = 1; i <= N; ++i)
    mx = max(mx, sum - now[min(i, t)].benefit + min(now[i].B, lft) * now[i].L +
                     max(0ll, lft - now[i].B) * now[i].R);
  return mx >= cost;
}

int main() {
  cin >> N >> X;
  for (int i = 1; i <= N; ++i) {
    cin >> now[i].B >> now[i].L >> now[i].R;
    cost += now[i].B * now[i].L;
    now[i].benefit = now[i].L * now[i].B + now[i].R * (X - now[i].B);
  }
  sort(now + 1, now + N + 1);
  long long L = 0, R = N * X;
  while (L < R) {
    int mid = (L + R) >> 1;
    check(mid) ? R = mid : L = mid + 1;
  }
  cout << L;
  return 0;
}

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

/*
  REMEMBER:
  1.Think TWICE, code ONCE!
  Are there any counterexamples to your algo?

  2.Be careful about the BOUNDARIES in your algo!
  N=1? P=1? Is P a prime? Something about 0?

  3.Never make STUPID MISTAKES!
  Integer overflow? Array size? Time complexity? Memory usage? Precition error?
*/

const int _ = 1e5 + 7;
struct test {
  long long B, L, R, benefit;
  friend bool operator<(test p, test q) { return p.benefit > q.benefit; }
} now[_];
long long N, X, cost;

bool check(long long mid) {
  int t = mid / X + 1;
  long long lft = mid % X;
  long long sum = 0, mx = 0;
  for (int i = 1; i <= t; ++i)
    sum += now[i].benefit;
  for (int i = 1; i <= N; ++i)
    mx = max(mx, sum - now[min(i, t)].benefit + min(now[i].B, lft) * now[i].L +
                     max(0ll, lft - now[i].B) * now[i].R);
  return mx >= cost;
}

int main() {
  cin >> N >> X;
  for (int i = 1; i <= N; ++i) {
    cin >> now[i].B >> now[i].L >> now[i].R;
    cost += now[i].B * now[i].L;
    now[i].benefit = now[i].L * now[i].B + now[i].R * (X - now[i].B);
  }
  sort(now + 1, now + N + 1);
  long long L = 0, R = N * X;
  while (L < R) {
    long long mid = (L + R) >> 1;
    check(mid) ? R = mid : L = mid + 1;
  }
  cout << L;
  return 0;
}
Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mx = 15;
long long n, x, mid, l = -1, r, d, ac[mx];
struct Test {
  long long b, l, u;
  long long s(long long t = x) { return t < b ? t * l : b * l + (t - b) * u; }
} t[mx];
int main() {
  scanf("%lld%lld", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &t[i].b, &t[i].l, &t[i].u);
    d += t[i].b * t[i].l;
  }
  sort(t, t + n, [](Test a, Test b) { return a.s() > b.s(); });
  for (int i = 0; i < n; i++)
    ac[i + 1] = ac[i] + t[i].s();
  r = x * n;
  while (l + 1 < r) {
    mid = (r + l) / 2;
    long long xx = mid / x, yy = mid % x;
    bool ok = false;
    for (int i = 0; i < n; i++) {
      if (d <= (i < xx ? ac[xx + 1] - t[i].s() : ac[xx]) + t[i].s(yy)) {
        ok = true;
        break;
      }
    }
    (ok ? r : l) = mid;
  }
  printf("%lld\n", r);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mx = 100005;
long long n, x, mid, l = -1, r, d, ac[mx];
struct Test {
  long long b, l, u;
  long long s(long long t = x) { return t < b ? t * l : b * l + (t - b) * u; }
} t[mx];
int main() {
  scanf("%lld%lld", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &t[i].b, &t[i].l, &t[i].u);
    d += t[i].b * t[i].l;
  }
  sort(t, t + n, [](Test a, Test b) { return a.s() > b.s(); });
  for (int i = 0; i < n; i++)
    ac[i + 1] = ac[i] + t[i].s();
  r = x * n;
  while (l + 1 < r) {
    mid = (r + l) / 2;
    long long xx = mid / x, yy = mid % x;
    bool ok = false;
    for (int i = 0; i < n; i++) {
      if (d <= (i < xx ? ac[xx + 1] - t[i].s() : ac[xx]) + t[i].s(yy)) {
        ok = true;
        break;
      }
    }
    (ok ? r : l) = mid;
  }
  printf("%lld\n", r);
}
Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
int N, X;
long long l[300010], r[300010], b[300010];

bool good(long long need) {
  long long extra = need % X;
  need /= X;
  if (need >= N)
    return true;

  long long sum = 0;
  vector<pair<long long, int>> v;
  for (int i = 1; i <= N; i++) {
    sum -= l[i] * b[i];
    v.emplace_back(r[i] * (X - b[i]) + l[i] * b[i], i);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < need; i++) {
    sum += v[i].first;
  }
  if (extra == 0)
    return sum >= 0;
  long long ans = sum;
  for (int i = need; i < N; i++) {
    int id = v[i].second;
    long long diff = extra - b[id];
    ans = max(ans, sum + l[id] * diff + l[id] * b[id]);
    ans = max(ans, sum + r[id] * diff + l[id] * b[id]);
  }
  sum += v[need].first;
  for (int i = 0; i < need; i++) {
    int id = v[i].second;
    long long diff = extra - b[id];
    ans = max(ans, sum - r[id] * (X - b[id]) + l[id] * diff);
    ans = max(ans, sum - r[id] * (X - b[id]) + r[id] * diff);
  }
  return ans >= 0;
}

long long search(long long b, long long e) {
  if (b == e) {
    return b;
  }
  int mid = (b + e) >> 1;
  if (good(mid)) {
    return search(b, mid);
  } else {
    return search(mid + 1, e);
  }
}

int main(int argc, char const *argv[]) {
  scanf("%d %d", &N, &X);
  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld %lld", &b[i], &l[i], &r[i]);
  }
  printf("%lld\n", search(0, 1LL * X * N));
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
int N, X;
long long l[300010], r[300010], b[300010];

bool good(long long need) {
  long long extra = need % X;
  need /= X;
  if (need >= N)
    return true;

  long long sum = 0;
  vector<pair<long long, int>> v;
  for (int i = 1; i <= N; i++) {
    sum -= l[i] * b[i];
    v.emplace_back(r[i] * (X - b[i]) + l[i] * b[i], i);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < need; i++) {
    sum += v[i].first;
  }
  if (extra == 0)
    return sum >= 0;
  long long ans = sum;
  for (int i = need; i < N; i++) {
    int id = v[i].second;
    long long diff = extra - b[id];
    ans = max(ans, sum + l[id] * diff + l[id] * b[id]);
    ans = max(ans, sum + r[id] * diff + l[id] * b[id]);
  }
  sum += v[need].first;
  for (int i = 0; i < need; i++) {
    int id = v[i].second;
    long long diff = extra - b[id];
    ans = max(ans, sum - r[id] * (X - b[id]) + l[id] * diff);
    ans = max(ans, sum - r[id] * (X - b[id]) + r[id] * diff);
  }
  return ans >= 0;
}

long long search(long long b, long long e) {
  if (b == e) {
    return b;
  }
  long long mid = (b + e) >> 1;
  if (good(mid)) {
    return search(b, mid);
  } else {
    return search(mid + 1, e);
  }
}

int main(int argc, char const *argv[]) {
  scanf("%d %d", &N, &X);
  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld %lld", &b[i], &l[i], &r[i]);
  }
  printf("%lld\n", search(0, 1LL * X * N));
  return 0;
}
Problem p03019 (C++)

Buggy submission (status: Runtime Error):
// High above the clouds there is a rainbow...

#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n, k) (((n) >> (k)) & 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int maxn = 2010, mod = 1e9 + 7;
const ll inf = 1e18;

pair<int, pii> p[maxn];
ll sm[maxn];

ll f(pair<int, pii> p, int x) {
  if (x <= p.F)
    return 1ll * x * p.S.F;
  return 1ll * p.F * p.S.F + 1ll * (x - p.F) * p.S.S;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, x;
  cin >> n >> x;
  ll SM = 0;
  for (int i = 0; i < n; i++) {
    cin >> p[i].F >> p[i].S.F >> p[i].S.S;
    SM += 1ll * p[i].S.F * p[i].F;
  }

  sort(p, p + n,
       [&](pair<int, pii> a, pair<int, pii> b) { return f(a, x) > f(b, x); });

  for (int i = 1; i <= n; i++) {
    sm[i] = sm[i - 1] + f(p[i - 1], x);
  }

  ll l = -1, r = 1ll * n * x;
  while (r - l > 1) {
    ll mid = (l + r) >> 1;
    ll num = 0;
    int cnt = mid / x, rem = mid % x;
    for (int i = 0; i < cnt; i++) {
      num = max(num, sm[cnt + 1] - f(p[i], x) + f(p[i], rem));
    }
    for (int i = cnt; i < n; i++) {
      num = max(num, sm[cnt] + f(p[i], rem));
    }
    if (num >= SM)
      r = mid;
    else
      l = mid;
  }
  return cout << r << endl, 0;
}
// Deathly mistakes:
//  * Read the problem curfully.
//  * Check maxn.
//  * Overflows.


Fixed submission:
// High above the clouds there is a rainbow...

#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n, k) (((n) >> (k)) & 1)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int maxn = 1e5 + 10, mod = 1e9 + 7;
const ll inf = 1e18;

pair<int, pii> p[maxn];
ll sm[maxn];

ll f(pair<int, pii> p, int x) {
  if (x <= p.F)
    return 1ll * x * p.S.F;
  return 1ll * p.F * p.S.F + 1ll * (x - p.F) * p.S.S;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, x;
  cin >> n >> x;
  ll SM = 0;
  for (int i = 0; i < n; i++) {
    cin >> p[i].F >> p[i].S.F >> p[i].S.S;
    SM += 1ll * p[i].S.F * p[i].F;
  }

  sort(p, p + n,
       [&](pair<int, pii> a, pair<int, pii> b) { return f(a, x) > f(b, x); });

  for (int i = 1; i <= n; i++) {
    sm[i] = sm[i - 1] + f(p[i - 1], x);
  }

  ll l = -1, r = 1ll * n * x;
  while (r - l > 1) {
    ll mid = (l + r) >> 1;
    ll num = 0;
    int cnt = mid / x, rem = mid % x;
    for (int i = 0; i < cnt; i++) {
      num = max(num, sm[cnt + 1] - f(p[i], x) + f(p[i], rem));
    }
    for (int i = cnt; i < n; i++) {
      num = max(num, sm[cnt] + f(p[i], rem));
    }
    if (num >= SM)
      r = mid;
    else
      l = mid;
  }
  return cout << r << endl, 0;
}
// Deathly mistakes:
//  * Read the problem curfully.
//  * Check maxn.
//  * Overflows.
Problem p03019 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ld = long double;

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, x;
  cin >> n >> x;
  vector<ll> b(n), l(n), r(n);
  ll res = 0;
  vector<pair<ll, ll>> a(n);
  rep(i, n) {
    cin >> b[i] >> l[i] >> r[i];
    res += b[i] * l[i];
    a[i].first = (x - b[i]) * r[i] + b[i] * l[i];
    a[i].second = i;
  }
  if (res == 0) {
    cout << 0;
    return 0;
  }
  sort(rall(a));
  vector<ll> ps(n + 1);
  rep(i, n) ps[i + 1] = ps[i] + a[i].first;
  int mc = 0;
  while (ps[mc] <= res)
    mc++;
  ll ans = mc * x;
  rep(i, n) {
    int j = a[i].second;
    ll cs = (mc - 1 <= i ? ps[mc - 1] : ps[mc] - a[i].first);
    if (cs + b[j] * l[j] >= res)
      ans = min(ans, (res - cs + l[j] - 1) / l[j] + (mc - 1) * x);
    else if (cs + b[j] * l[j] + (x - b[j]) * r[j] >= res) {
      ans = min(ans, (res - cs - b[j] * l[j] + b[j] * r[j] + r[j] - 1) / r[j] +
                         (mc - 1) * x);
    }
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ld = long double;

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, x;
  cin >> n >> x;
  vector<ll> b(n), l(n), r(n);
  ll res = 0;
  vector<pair<ll, ll>> a(n);
  rep(i, n) {
    cin >> b[i] >> l[i] >> r[i];
    res += b[i] * l[i];
    a[i].first = (x - b[i]) * r[i] + b[i] * l[i];
    a[i].second = i;
  }
  if (res == 0) {
    cout << 0;
    return 0;
  }
  sort(rall(a));
  vector<ll> ps(n + 1);
  rep(i, n) ps[i + 1] = ps[i] + a[i].first;
  int mc = 0;
  while (ps[mc] < res)
    mc++;
  ll ans = mc * x;
  rep(i, n) {
    int j = a[i].second;
    ll cs = (mc - 1 <= i ? ps[mc - 1] : ps[mc] - a[i].first);
    if (cs + b[j] * l[j] >= res)
      ans = min(ans, (res - cs + l[j] - 1) / l[j] + (mc - 1) * x);
    else if (cs + b[j] * l[j] + (x - b[j]) * r[j] >= res) {
      ans = min(ans, (res - cs - b[j] * l[j] + b[j] * r[j] + r[j] - 1) / r[j] +
                         (mc - 1) * x);
    }
  }
  cout << ans;
}
Problem p03019 (C++)

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

int n, x;
int b[111111], l[111111], u[111111];
long long m;
struct P {
  long long x, y;
};
P o[111111];

bool as(P a, P b) { return a.x > b.x; }

bool f(int a) {
  long long t = 0, t2 = 0;
  for (int t = 1; t <= n; t++)
    o[t] = {(long long)(x - b[t]) * u[t] + (long long)b[t] * l[t], t};
  sort(o + 1, o + n + 1, as);
  for (int i = 1; i <= a / x + 1; i++) {
    if (i < a / x + 1)
      t += o[i].x;
    t2 += o[i].x;
  }
  if (t + m >= 0)
    return 1;
  for (int i = 1; i <= a / x + 1; i++) {
    long long z = t2 - o[i].x +
                  max((long long)(a % x - b[o[i].y]) * l[o[i].y],
                      (long long)(a % x - b[o[i].y]) * u[o[i].y]) +
                  (long long)b[o[i].y] * l[o[i].y];
    if (z + m >= 0)
      return 1;
  }
  for (int i = a / x + 2; i <= n; i++) {
    long long z = t +
                  max((long long)(a % x - b[o[i].y]) * l[o[i].y],
                      (long long)(a % x - b[o[i].y]) * u[o[i].y]) +
                  (long long)b[o[i].y] * l[o[i].y];
    if (z + m >= 0)
      return 1;
  }
  return 0;
}

int main() {
  scanf("%d %d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &b[i], &l[i], &u[i]);
    m -= (long long)b[i] * l[i];
  }
  long long a = 0, b = (long long)x * n;
  for (; a < b;) {
    long long h = (a + b) / 2;
    if (f(h))
      b = h;
    else
      a = h + 1;
  }
  printf("%lld", a);
}


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

int n, x;
int b[111111], l[111111], u[111111];
long long m;
struct P {
  long long x, y;
};
P o[111111];

bool as(P a, P b) { return a.x > b.x; }

bool f(long long a) {
  long long t = 0, t2 = 0;
  for (int t = 1; t <= n; t++)
    o[t] = {(long long)(x - b[t]) * u[t] + (long long)b[t] * l[t], t};
  sort(o + 1, o + n + 1, as);
  for (int i = 1; i <= a / x + 1; i++) {
    if (i < a / x + 1)
      t += o[i].x;
    t2 += o[i].x;
  }
  if (t + m >= 0)
    return 1;
  for (int i = 1; i <= a / x + 1; i++) {
    long long z = t2 - o[i].x +
                  max((long long)(a % x - b[o[i].y]) * l[o[i].y],
                      (long long)(a % x - b[o[i].y]) * u[o[i].y]) +
                  (long long)b[o[i].y] * l[o[i].y];
    if (z + m >= 0)
      return 1;
  }
  for (int i = a / x + 2; i <= n; i++) {
    long long z = t +
                  max((long long)(a % x - b[o[i].y]) * l[o[i].y],
                      (long long)(a % x - b[o[i].y]) * u[o[i].y]) +
                  (long long)b[o[i].y] * l[o[i].y];
    if (z + m >= 0)
      return 1;
  }
  return 0;
}

int main() {
  scanf("%d %d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &b[i], &l[i], &u[i]);
    m -= (long long)b[i] * l[i];
  }
  long long a = 0, b = (long long)x * n;
  for (; a < b;) {
    long long h = (a + b) / 2;
    if (f(h))
      b = h;
    else
      a = h + 1;
  }
  printf("%lld", a);
}
Problem p03020 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#define M 20002
#define N 2019
int A[N], B[N], C[N], S, T, a[N], b[M], c[M], d[M], e[M], g[N], i, l, n, q[N],
    r, t;
long long f[N];
bool h[N];
inline void add(int u, int v, int x, int y) {
  b[++t] = a[u], c[a[u] = t] = v, d[t] = x, e[t] = y, b[++t] = a[v],
  c[a[v] = t] = u, e[t] = -y;
}
int u, v, w;
long long x;
inline bool bfs() {
  memset(f, 0x3f, T + 5 << 3), f[q[l = 0] = S] = 0, r = 1;
  while (l != r)
    for (h[u = q[l++]] = false, l = l == N ? 0 : l, i = a[u]; i; i = b[i])
      if (d[i] && f[u] + e[i] < f[c[i]]) {
        f[c[i]] = f[u] + e[i], g[c[i]] = i;
        if (!h[c[i]])
          h[q[r++] = c[i]] = true;
      }
  if (f[T] == 0x3f3f3f3f3f3f3f3fll)
    return false;
  for (u = 10, i = T; i != S; i = c[g[i] ^ 1])
    u = d[g[i]] < u ? d[g[i]] : u;
  for (x -= u * f[i = T]; i != S; i = c[g[i] ^ 1])
    d[g[i]] -= u, d[g[i] ^ 1] += u;
  return true;
}
int main() {
  scanf("%d", &n), T = n << 1 | 1;
  for (i = t = 1; i <= n; i++)
    scanf("%d%d%d", &u, &v, &w), add(S, i, w, 0), add(i, T + 1, 10, u + v),
        add(i, T + 2, 10, u - v), add(i, T + 3, 10, v - u),
        add(i, T + 4, 10, -u - v);
  for (i = n + 1; i < T; i++)
    scanf("%d%d%d", &u, &v, &w), add(T + 1, i, 10, -u - v),
        add(T + 2, i, 10, v - u), add(T + 3, i, 10, u - v),
        add(T + 4, i, 10, u + v), add(i, T, w, 0);
  while (bfs())
    ;
  return 0 * printf("%lld\n", x);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#define M 20002
#define N 2019
int A[N], B[N], C[N], S, T, a[N], b[M], c[M], d[M], e[M], g[N], i, l, n, q[N],
    r, t;
long long f[N];
bool h[N];
inline void add(int u, int v, int x, int y) {
  b[++t] = a[u], c[a[u] = t] = v, d[t] = x, e[t] = y, b[++t] = a[v],
  c[a[v] = t] = u, e[t] = -y;
}
int u, v, w;
long long x;
inline bool bfs() {
  memset(f, 0x3f, T + 5 << 3), f[q[l = 0] = S] = 0, r = 1;
  while (l != r)
    for (h[u = q[l++]] = false, l = l == N ? 0 : l, i = a[u]; i; i = b[i])
      if (d[i] && f[u] + e[i] < f[c[i]]) {
        f[c[i]] = f[u] + e[i], g[c[i]] = i;
        if (!h[c[i]])
          h[q[r++] = c[i]] = true, r = r == N ? 0 : r;
      }
  if (f[T] == 0x3f3f3f3f3f3f3f3fll)
    return false;
  for (u = 10, i = T; i != S; i = c[g[i] ^ 1])
    u = d[g[i]] < u ? d[g[i]] : u;
  for (x -= u * f[i = T]; i != S; i = c[g[i] ^ 1])
    d[g[i]] -= u, d[g[i] ^ 1] += u;
  return true;
}
int main() {
  scanf("%d", &n), T = n << 1 | 1;
  for (i = t = 1; i <= n; i++)
    scanf("%d%d%d", &u, &v, &w), add(S, i, w, 0), add(i, T + 1, 10, u + v),
        add(i, T + 2, 10, u - v), add(i, T + 3, 10, v - u),
        add(i, T + 4, 10, -u - v);
  for (i = n + 1; i < T; i++)
    scanf("%d%d%d", &u, &v, &w), add(T + 1, i, 10, -u - v),
        add(T + 2, i, 10, v - u), add(T + 3, i, 10, u - v),
        add(T + 4, i, 10, u + v), add(i, T, w, 0);
  while (bfs())
    ;
  return 0 * printf("%lld\n", x);
}
Problem p03021 (C++)

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

#define mp make_pair
#define X first
#define Y second
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FOD(i, a, b) for (int i = a; i >= b; i--)

using namespace std;

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

const int N = 2010;
const int INF = 1e9 + 7;

int n, low[N], high[N], cnt[N];
bool hasPices[N];
vector<int> adj[N];

void dfs(int u, int p) {
  cnt[u] = hasPices[u];
  high[u] = 0;
  int maxLow = 0;
  priority_queue<int, vector<int>, greater<int>> Q;
  for (int v : adj[u])
    if (v != p) {
      dfs(v, u);
      Q.push(high[v] + cnt[v]);
      if (Q.size() > 2)
        Q.pop();
      cnt[u] += cnt[v];
      high[u] += high[v] + cnt[v];
      maxLow = max(maxLow, low[v] + cnt[v]);
    }
  if (Q.empty()) {
    low[u] = 0;
  } else {
    int x = maxLow;
    int sum = 0;
    for (int v : adj[u])
      if (v != p)
        sum += min(high[v] + cnt[v], x - abs(high[v] + cnt[v] - x) % 2);
    if (x * 2 > sum)
      low[u] = x * 2 - sum;
    else
      low[u] = sum % 2;
  }
}

int main() {
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  string s;
  cin >> s;
  FOR(i, 1, n)
  hasPices[i] = (s[i - 1] == '1');
  FOR(i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  int ans = INF;
  FOR(i, 1, n) {
    dfs(i, i);
    if (low[i] == 0)
      ans = min(ans, high[i] / 2);
  }
  cout << (ans == INF ? -1 : ans);

  return 0;
}

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

#define mp make_pair
#define X first
#define Y second
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FOD(i, a, b) for (int i = a; i >= b; i--)

using namespace std;

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

const int N = 2010;
const int INF = 1e9 + 7;

int n, low[N], high[N], cnt[N];
bool hasPices[N];
vector<int> adj[N];

void dfs(int u, int p) {
  cnt[u] = hasPices[u];
  high[u] = 0;
  int maxLow = 0;
  priority_queue<int, vector<int>, greater<int>> Q;
  for (int v : adj[u])
    if (v != p) {
      dfs(v, u);
      Q.push(high[v] + cnt[v]);
      if (Q.size() > 2)
        Q.pop();
      cnt[u] += cnt[v];
      high[u] += high[v] + cnt[v];
      maxLow = max(maxLow, low[v] + cnt[v]);
    }
  if (Q.empty()) {
    low[u] = 0;
  } else {
    int x = maxLow;
    int sum = 0;
    for (int v : adj[u])
      if (v != p)
        sum += min(high[v] + cnt[v], x - abs(high[v] + cnt[v] - x) % 2);
    if (x * 2 > sum)
      low[u] = x * 2 - sum;
    else
      low[u] = sum % 2;
  }
}

int main() {
  //    freopen("inp.txt", "r", stdin);
  //    freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  string s;
  cin >> s;
  FOR(i, 1, n)
  hasPices[i] = (s[i - 1] == '1');
  FOR(i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  int ans = INF;
  FOR(i, 1, n) {
    dfs(i, i);
    if (low[i] == 0)
      ans = min(ans, high[i] / 2);
  }
  cout << (ans == INF ? -1 : ans);

  return 0;
}
Problem p03021 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 2005;
int n, sum[N], dep[N], sz[N];
char s[N];
int pp, lnk[N], nxt[N * 2], to[N * 2];
void ae(int k1, int k2) { to[++pp] = k2, nxt[pp] = lnk[k1], lnk[k1] = pp; }
void pre(int k1, int k2) {
  sz[k1] = s[k1], sum[k1] = 0;
  for (int i = lnk[k1]; i; i = nxt[i])
    if (to[i] != k2)
      dep[to[i]] = dep[k1] + 1, pre(to[i], k1), sz[k1] += sz[to[i]],
      sum[k1] += sum[to[i]] + sz[to[i]];
}
int dfs(int k1, int k2) {
  int v = -1;
  for (int i = lnk[k1]; i; i = nxt[i])
    if (to[i] != k2) {
      dfs(to[i], k1);
      if (v == -1 || sum[to[i]] > sum[v])
        v = to[i];
    }
  if (v == -1)
    return 0;
  int k = dfs(v, k1) + sz[v];
  if (sum[k1] - sum[v] - sz[v] >= k)
    return sum[k1] & 1;
  else
    return k - (sum[k1] - sum[v] - sz[v]);
}
int main() {
  scanf("%d%s", &n, s + 1);
  rep(i, 1, n) s[i] &= 1;
  rep(i, 2, n) {
    int k1, k2;
    scanf("%d%d", &k1, &k2);
    ae(k1, k2), ae(k2, k1);
  }
  int ans = 1e9;
  rep(k1, 1, n) {
    pre(k1, 0);
    if (dfs(k1, 0) == 0)
      ans = min(ans, sum[k1] / 2);
  }
  if (ans == 1e9)
    ans = -1;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 2005;
int n, sum[N], dep[N], sz[N];
char s[N];
int pp, lnk[N], nxt[N * 2], to[N * 2];
void ae(int k1, int k2) { to[++pp] = k2, nxt[pp] = lnk[k1], lnk[k1] = pp; }
void pre(int k1, int k2) {
  sz[k1] = s[k1], sum[k1] = 0;
  for (int i = lnk[k1]; i; i = nxt[i])
    if (to[i] != k2)
      dep[to[i]] = dep[k1] + 1, pre(to[i], k1), sz[k1] += sz[to[i]],
      sum[k1] += sum[to[i]] + sz[to[i]];
}
int dfs(int k1, int k2) {
  int v = -1;
  for (int i = lnk[k1]; i; i = nxt[i])
    if (to[i] != k2) {
      if (v == -1 || sum[to[i]] > sum[v])
        v = to[i];
    }
  if (v == -1)
    return 0;
  int k = dfs(v, k1) + sz[v];
  if (sum[k1] - sum[v] - sz[v] >= k)
    return sum[k1] & 1;
  else
    return k - (sum[k1] - sum[v] - sz[v]);
}
int main() {
  scanf("%d%s", &n, s + 1);
  rep(i, 1, n) s[i] &= 1;
  rep(i, 2, n) {
    int k1, k2;
    scanf("%d%d", &k1, &k2);
    ae(k1, k2), ae(k2, k1);
  }
  int ans = 1e9;
  rep(k1, 1, n) {
    pre(k1, 0);
    if (dfs(k1, 0) == 0)
      ans = min(ans, sum[k1] / 2);
  }
  if (ans == 1e9)
    ans = -1;
  printf("%d\n", ans);
  return 0;
}
Problem p03021 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct number {
  int siz, low, high;
  number() : siz(0), low(0), high(0) {}
};
int main() {
  do {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
  } while (false);
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<vector<int>> adj(n);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[--u].push_back(--v);
    adj[v].push_back(u);
  }
  auto add = [&](number a, number b) {
    number res;
    res.siz = a.siz + b.siz;
    res.high = a.high + b.high;
    if (a.low > b.low) {
      swap(a, b);
    }
    if (a.high > b.low) {
      res.low = res.high % 2;
    } else {
      res.low = b.low - a.high;
    }
    return res;
  };
  int ans = INT_MAX;
  for (int root = 0; root < n; root++) {
    vector<number> dp(n);
    function<int(int, int, int)> dfs = [&](int u, int fa, int depth) {
      int res = 0;
      for (int v : adj[u]) {
        if (v != fa) {
          res += dfs(v, u, depth + 1);
          dp[v].low += dp[v].siz;
          dp[v].high += dp[v].siz;
          dp[u] = add(dp[u], dp[v]);
        }
      }
      if (str[u] == '1') {
        dp[u].siz++;
        res += depth;
      }
      return res;
    };
    int result = dfs(root, -1, 0);
    if (!dp[result].low) {
      ans = min(ans, result / 2);
    }
  }
  cout << (ans == INT_MAX ? -1 : ans) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct number {
  int siz, low, high;
  number() : siz(0), low(0), high(0) {}
};
int main() {
  do {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
  } while (false);
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<vector<int>> adj(n);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[--u].push_back(--v);
    adj[v].push_back(u);
  }
  auto add = [&](number a, number b) {
    number res;
    res.siz = a.siz + b.siz;
    res.high = a.high + b.high;
    if (a.low > b.low) {
      swap(a, b);
    }
    if (a.high > b.low) {
      res.low = res.high % 2;
    } else {
      res.low = b.low - a.high;
    }
    return res;
  };
  int ans = INT_MAX;
  for (int root = 0; root < n; root++) {
    vector<number> dp(n);
    function<int(int, int, int)> dfs = [&](int u, int fa, int depth) {
      int res = 0;
      for (int v : adj[u]) {
        if (v != fa) {
          res += dfs(v, u, depth + 1);
          dp[v].low += dp[v].siz;
          dp[v].high += dp[v].siz;
          dp[u] = add(dp[u], dp[v]);
        }
      }
      if (str[u] == '1') {
        dp[u].siz++;
        res += depth;
      }
      return res;
    };
    int result = dfs(root, -1, 0);
    if (!dp[root].low) {
      ans = min(ans, result / 2);
    }
  }
  cout << (ans == INT_MAX ? -1 : ans) << endl;
  return 0;
}
Problem p03021 (C++)

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

int n, x, y, hd[2010], cnt, dis[2010], q[2010], l, r, f[2010], fa[2010], ans,
    siz[2010], col[2010], g[2010], c[2];
struct node {
  int to, next;
} e[4010];
char s[2010];

void addedge(int x, int y) {
  e[++cnt] = (node){y, hd[x]}, hd[x] = cnt;
  e[++cnt] = (node){x, hd[y]}, hd[y] = cnt;
}

void dfs(int x) {
  int ma = 0, maa, sum = 0;
  siz[x] = (s[x] == '1');
  for (int i = hd[x]; i; i = e[i].next)
    if (e[i].to != fa[x]) {
      fa[e[i].to] = x, dfs(e[i].to), sum += f[e[i].to] + siz[e[i].to],
      siz[x] += siz[e[i].to];
      if (f[e[i].to] + siz[e[i].to] > ma)
        ma = f[e[i].to] + siz[e[i].to], maa = e[i].to;
    }
  f[x] = sum;
  if (sum - ma >= g[maa] + siz[maa])
    g[x] = 0;
  else
    g[x] = g[maa] + siz[maa] - (sum - ma);
}

void getcol(int x) {
  for (int i = hd[x]; i; i = e[i].next)
    if (e[i].to != fa[x])
      fa[e[i].to] = x, col[e[i].to] = col[x] ^ 1, getcol(e[i].to);
}

int main() {
  scanf("%d", &n), ans = 1000000000, scanf("%s", s + 1);
  for (int i = 1; i < n; i++)
    scanf("%d%d", &x, &y), addedge(x, y);
  col[1] = 0, getcol(1), c[0] = c[1] = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '1')
      c[col[i]]++;
  if ((c[0] & 1) && (c[1] & 1))
    return puts("-1"), 0;
  for (int i = 1; i <= n; i++)
    if (!(c[col[i] ^ 1] & 1)) {
      memset(fa, 0, sizeof(fa)), dfs(i);
      if (g[i] == 0)
        ans = min(ans, f[i] / 2);
    }
  if (ans == 1000000000)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}

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

int n, x, y, hd[2010], cnt, dis[2010], q[2010], l, r, f[2010], fa[2010], ans,
    siz[2010], col[2010], g[2010], c[2];
struct node {
  int to, next;
} e[4010];
char s[2010];

void addedge(int x, int y) {
  e[++cnt] = (node){y, hd[x]}, hd[x] = cnt;
  e[++cnt] = (node){x, hd[y]}, hd[y] = cnt;
}

void dfs(int x) {
  int ma = 0, maa, sum = 0;
  siz[x] = (s[x] == '1');
  for (int i = hd[x]; i; i = e[i].next)
    if (e[i].to != fa[x]) {
      fa[e[i].to] = x, dfs(e[i].to), sum += f[e[i].to] + siz[e[i].to],
      siz[x] += siz[e[i].to];
      if (f[e[i].to] + siz[e[i].to] > ma)
        ma = f[e[i].to] + siz[e[i].to], maa = e[i].to;
    }
  if (!ma)
    f[x] = g[x] = 0;
  else {
    f[x] = sum;
    if (sum - ma >= g[maa] + siz[maa])
      g[x] = 0;
    else
      g[x] = g[maa] + siz[maa] - (sum - ma);
  }
}

void getcol(int x) {
  for (int i = hd[x]; i; i = e[i].next)
    if (e[i].to != fa[x])
      fa[e[i].to] = x, col[e[i].to] = col[x] ^ 1, getcol(e[i].to);
}

int main() {
  scanf("%d", &n), ans = 1000000000, scanf("%s", s + 1);
  for (int i = 1; i < n; i++)
    scanf("%d%d", &x, &y), addedge(x, y);
  col[1] = 0, getcol(1), c[0] = c[1] = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '1')
      c[col[i]]++;
  if ((c[0] & 1) && (c[1] & 1))
    return puts("-1"), 0;
  for (int i = 1; i <= n; i++)
    if (!(c[col[i] ^ 1] & 1)) {
      memset(fa, 0, sizeof(fa)), dfs(i);
      if (g[i] == 0)
        ans = min(ans, f[i] / 2);
    }
  if (ans == 1000000000)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
Problem p03022 (C++)

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

const int p = 998244353, inv2 = (p + 1) / 2;
int qpow(int a, int k) {
  int ans = 1;
  while (k) {
    if (k & 1)
      ans = 1LL * ans * a % p;
    a = 1LL * a * a % p;
    k >>= 1;
  }
  return ans;
}

int FWT_xor(int d[], int n0, int flg) {
  for (int i = 1; i < (1 << n0); i <<= 1)
    for (int j = 0; j < (1 << n0); j += (i << 1))
      for (int k = 0; k < i; k++) {
        int a0 = d[j | k], a1 = d[i | j | k];
        d[j | k] = (a0 + a1) % p, d[i | j | k] = (a0 - a1 + p) % p;
        if (flg == -1)
          d[j | k] = 1LL * d[j | k] * inv2 % p,
                d[i | j | k] = 1LL * d[i | j | k] * inv2 % p;
      }
}
int F[1 << 18], Q[1 << 18], P[1 << 18], SP;

int N;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < (1 << N); i++)
    scanf("%d", &P[i]), SP = (SP + P[i]) % p;
  SP = qpow(SP, p - 2);
  for (int i = 0; i < (1 << N); i++)
    P[i] = 1LL * P[i] * SP % p;
  P[0] = (P[0] - 1 + p) % p;
  Q[0] = (1 << N) - 1;
  for (int i = 1; i < (1 << N); i++)
    Q[i] = p - 1;
  FWT_xor(P, N, 1);
  FWT_xor(Q, N, 1);
  for (int i = 1; i < (1 << N); i++)
    F[i] = 1LL * Q[i] * qpow(P[i], p - 2) % p;
  FWT_xor(F, N, -1);
  int ans = p - F[0];
  for (int i = 0; i < (1 << N); i++)
    printf("%d\n", (ans + F[i]) % p);
}

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

const int p = 998244353, inv2 = (p + 1) / 2;
int qpow(int a, int k) {
  int ans = 1;
  while (k) {
    if (k & 1)
      ans = 1LL * ans * a % p;
    a = 1LL * a * a % p;
    k >>= 1;
  }
  return ans;
}

void FWT_xor(int d[], int n0, int flg) {
  for (int i = 1; i < (1 << n0); i <<= 1)
    for (int j = 0; j < (1 << n0); j += (i << 1))
      for (int k = 0; k < i; k++) {
        int a0 = d[j | k], a1 = d[i | j | k];
        d[j | k] = (a0 + a1) % p, d[i | j | k] = (a0 - a1 + p) % p;
        if (flg == -1)
          d[j | k] = 1LL * d[j | k] * inv2 % p,
                d[i | j | k] = 1LL * d[i | j | k] * inv2 % p;
      }
}
int F[1 << 18], Q[1 << 18], P[1 << 18], SP;

int N;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < (1 << N); i++)
    scanf("%d", &P[i]), SP = (SP + P[i]) % p;
  SP = qpow(SP, p - 2);
  for (int i = 0; i < (1 << N); i++)
    P[i] = 1LL * P[i] * SP % p;
  P[0] = (P[0] - 1 + p) % p;
  Q[0] = (1 << N) - 1;
  for (int i = 1; i < (1 << N); i++)
    Q[i] = p - 1;
  FWT_xor(P, N, 1);
  FWT_xor(Q, N, 1);
  for (int i = 1; i < (1 << N); i++)
    F[i] = 1LL * Q[i] * qpow(P[i], p - 2) % p;
  FWT_xor(F, N, -1);
  int ans = p - F[0];
  for (int i = 0; i < (1 << N); i++)
    printf("%d\n", (ans + F[i]) % p);
}
Problem p03022 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 18, M = 998244353;
using namespace std;
int n, l, a[N], b[N], t0, t1, sum;
inline int inv(int x) {
  int y = 1, t = M - 2;
  for (; t; t >>= 1)
    t & 1 ? y = 1ll * y * x % M : 0, x = 1ll * x * x % M;
  return y;
}
inline void fwt(int *c, int t) {
  for (int i = 1; i < l; i <<= 1)
    for (int j = 0; j < l; j++)
      if (j & i)
        t0 = (c[j ^ i] + c[j]) % M, t1 = (c[j ^ i] - c[j]) % M,
        c[j] = t1 * 1ll * t % M, c[j ^ i] = t0 * 1ll * t % M;
}
int main() {
  scanf("%d", &n), b[0] = l = 1 << n;
  for (int i = 0; i < l; i++)
    scanf("%d", a + i), sum = (sum + a[i]) % M, b[i]--;
  sum = inv(sum);
  for (int i = 0; i < l; i++)
    a[i] = 1ll * a[i] * sum % M;
  a[0]--;               /*
                 for(int i=0;i<l;i++)
                 cerr<<a[i]<<' ';cerr<<endl;*/
  fwt(a, 1), fwt(b, 1); /*
      for(int i=0;i<l;i++)
      cerr<<a[i]<<' ';cerr<<endl;
      for(int i=0;i<l;i++)
      cerr<<b[i]<<' ';cerr<<endl;*/
  for (int i = 1; i < l; i++)
    a[i] = b[i] = 1ll * b[i] * inv(a[i]) % M;
  fwt(a, M + 1 >> 1);
  b[0] = -a[0] * 1ll * l % M;
  fwt(b, M + 1 >> 1);
  for (int i = 0; i < l; i++)
    printf("%d\n", (b[i] + M) % M);
}

Fixed submission:
#include <bits/stdc++.h>
const int N = 18, M = 998244353;
using namespace std;
int n, l, a[1 << N], b[1 << N], t0, t1, sum;
inline int inv(int x) {
  int y = 1, t = M - 2;
  for (; t; t >>= 1)
    t & 1 ? y = 1ll * y * x % M : 0, x = 1ll * x * x % M;
  return y;
}
inline void fwt(int *c, int t) {
  for (int i = 1; i < l; i <<= 1)
    for (int j = 0; j < l; j++)
      if (j & i)
        t0 = (c[j ^ i] + c[j]) % M, t1 = (c[j ^ i] - c[j]) % M,
        c[j] = t1 * 1ll * t % M, c[j ^ i] = t0 * 1ll * t % M;
}
int main() {
  scanf("%d", &n), b[0] = l = 1 << n;
  for (int i = 0; i < l; i++)
    scanf("%d", a + i), sum = (sum + a[i]) % M, b[i]--;
  sum = inv(sum);
  for (int i = 0; i < l; i++)
    a[i] = 1ll * a[i] * sum % M;
  a[0]--;               /*
                 for(int i=0;i<l;i++)
                 cerr<<a[i]<<' ';cerr<<endl;*/
  fwt(a, 1), fwt(b, 1); /*
      for(int i=0;i<l;i++)
      cerr<<a[i]<<' ';cerr<<endl;
      for(int i=0;i<l;i++)
      cerr<<b[i]<<' ';cerr<<endl;*/
  for (int i = 1; i < l; i++)
    a[i] = b[i] = 1ll * b[i] * inv(a[i]) % M;
  fwt(a, M + 1 >> 1);
  b[0] = -a[0] * 1ll * l % M;
  fwt(b, M + 1 >> 1);
  for (int i = 0; i < l; i++)
    printf("%d\n", (b[i] + M) % M);
}
Problem p03023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define pb(v, a) v.push_back(a)
#define Sort(v) sort(v.begin(), v.end())
#define For(m) for (auto it = m.begin(); it != m.end(); it++)
#define mp(p1, p2) make_pair(p1, p2)
#define mod 1000000007
#define PI 3.14159265358979323846
#define pii pair<ll, ll>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast ll n;
  cin >> n;
  cout << (n - 2) * 180;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define pb(v, a) v.push_back(a)
#define Sort(v) sort(v.begin(), v.end())
#define For(m) for (auto it = m.begin(); it != m.end(); it++)
#define mp(p1, p2) make_pair(p1, p2)
#define mod 1000000007
#define PI 3.14159265358979323846
#define pii pair<ll, ll>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  fast ll n;
  cin >> n;
  cout << (n - 2) * 180;

  return 0;
}