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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
/// source: https://atcoder.jp/contests/abc149/submissions/9237911
long long n, m;
vector<long long> a, sum;

long long solve();
long long check(long long x);

int main() {
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  sort(a.begin(), a.end(), greater<long long>());
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = 0, l = 0, r = 2 * a[0] + 1;
  while (r > l) {
    long long now = (l + r) / 2;
    if (check(now) >= m)
      l = now;
    else
      r = now;
  }
  sum = a;
  for (int i = 1; i < n; ++i)
    sum[i] += sum[i - 1];
  for (int i = 0; i < n; ++i) {
    if (a[0] + a[i] < l)
      continue;
    long long now =
        upper_bound(a.begin(), a.end(), l - a[i], greater<long long>()) -
        a.begin();

    res += now * a[i] + sum[now - 1];
  }
  res -= l * (check(l) - m);
  return res;
}

long long check(long long x) {
  long long cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (a[0] + a[i] < x)
      continue;
    cnt += upper_bound(a.begin(), a.end(), x - a[i], greater<long long>()) -
           a.begin();
  }
  return cnt;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/// source: https://atcoder.jp/contests/abc149/submissions/9237911
long long n, m;
vector<long long> a, sum;

long long solve();
long long check(long long x);

int main() {
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  sort(a.begin(), a.end(), greater<long long>());
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = 0, l = 0, r = 2 * a[0] + 1;
  /// while(r  > l) { DILE TLE ASHE

  while (r > l + 1) {
    long long now = (l + r) / 2;
    if (check(now) >= m)
      l = now;
    else
      r = now;
  }
  sum = a;
  for (int i = 1; i < n; ++i)
    sum[i] += sum[i - 1];
  for (int i = 0; i < n; ++i) {
    if (a[0] + a[i] < l)
      continue;
    long long now =
        upper_bound(a.begin(), a.end(), l - a[i], greater<long long>()) -
        a.begin();

    res += now * a[i] + sum[now - 1];
  }
  res -= l * (check(l) - m);
  return res;
}

long long check(long long x) {
  long long cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (a[0] + a[i] < x)
      continue;
    cnt += upper_bound(a.begin(), a.end(), x - a[i], greater<long long>()) -
           a.begin();
  }
  return cnt;
}
Problem p02821 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>

namespace {
template <int n, typename T>
void mult(const T *__restrict a, const T *__restrict b, T *__restrict res) {
  if (n <= 64) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        res[i + j] += a[i] * b[j];
      }
    }
  } else {
    const int mid = n / 2;
    alignas(64) T btmp[n], E[n] = {};
    auto atmp = btmp + mid;
    for (int i = 0; i < mid; i++) {
      atmp[i] = a[i] + a[i + mid];
      btmp[i] = b[i] + b[i + mid];
    }
    mult<mid>(atmp, btmp, E);
    mult<mid>(a + 0, b + 0, res);
    mult<mid>(a + mid, b + mid, res + n);
    for (int i = 0; i < mid; i++) {
      const auto tmp = res[i + mid];
      res[i + mid] += E[i] - res[i] - res[i + 2 * mid];
      res[i + 2 * mid] += E[i + mid] - tmp - res[i + 3 * mid];
    }
  }
}
} // namespace

using ll = long long;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  const int NMAX = (1 << 11) * 49;
  alignas(64) static ll a[NMAX], b[NMAX * 2];
  int n;
  ll k;
  std::cin >> n >> k;
  for (int i = 0, val; i < n; i++) {
    std::cin >> val;
    a[val]++;
  }
  mult<NMAX>(a, a, b);
  ll answ(0);
  for (int i = 2 * NMAX - 1; k > 0 && i >= 0; i--) {
    answ += std::min(k, b[i]) * i;
    k -= b[i];
  }
  std::cout << answ << std::endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx")
#include <bits/stdc++.h>

namespace {
template <int n, typename T>
void mult(const T *__restrict a, const T *__restrict b, T *__restrict res) {
  if (n <= 64) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        res[i + j] += a[i] * b[j];
      }
    }
  } else {
    const int mid = n / 2;
    alignas(64) T btmp[n], E[n] = {};
    auto atmp = btmp + mid;
    for (int i = 0; i < mid; i++) {
      atmp[i] = a[i] + a[i + mid];
      btmp[i] = b[i] + b[i + mid];
    }
    mult<mid>(atmp, btmp, E);
    mult<mid>(a + 0, b + 0, res);
    mult<mid>(a + mid, b + mid, res + n);
    for (int i = 0; i < mid; i++) {
      const auto tmp = res[i + mid];
      res[i + mid] += E[i] - res[i] - res[i + 2 * mid];
      res[i + 2 * mid] += E[i + mid] - tmp - res[i + 3 * mid];
    }
  }
}
} // namespace

using ll = long long;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  const int NMAX = (1 << 11) * 49;
  alignas(64) static ll a[NMAX], b[NMAX * 2];
  int n;
  ll k;
  std::cin >> n >> k;
  for (int i = 0, val; i < n; i++) {
    std::cin >> val;
    a[val]++;
  }
  mult<NMAX>(a, a, b);
  ll answ(0);
  for (int i = 2 * NMAX - 1; k > 0 && i >= 0; i--) {
    answ += std::min(k, b[i]) * i;
    k -= b[i];
  }
  std::cout << answ << std::endl;
  return 0;
}
Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inv2 = 500000004;
const int MAXN = 200005;
int n, ans, pw2[MAXN];
int fir[MAXN], to[MAXN << 1], nxt[MAXN], cnt;
inline void link(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = fir[u];
  fir[u] = cnt;
  to[++cnt] = u;
  nxt[cnt] = fir[v];
  fir[v] = cnt;
}
int sz[MAXN];
void dfs(int u, int ff) {
  sz[u] = 1;
  int p0 = 1, p1 = 0, q0, q1, p;
  for (int i = fir[u], v; i; i = nxt[i])
    if ((v = to[i]) != ff) {
      dfs(v, u);
      sz[u] += sz[v];
      p = pw2[sz[v]];
      q0 = 1ll * p0 * p % mod;
      q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod;
      p0 = q0, p1 = q1;
    }
  p = pw2[n - sz[u]];
  q0 = 1ll * p0 * p % mod;
  q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod;
  p0 = q0, p1 = q1;
  ans = (ans + (1ll + mod + mod - p0 - p1) % mod * inv2 % mod) % mod;
}
int main() {
  scanf("%d", &n);
  pw2[0] = 1;
  for (int i = 1, x, y; i < n; ++i)
    scanf("%d%d", &x, &y), link(x, y), pw2[i] = 1ll * pw2[i - 1] * inv2 % mod;
  pw2[n] = 1ll * pw2[n - 1] * inv2 % mod;
  dfs(1, 0);
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inv2 = 500000004;
const int MAXN = 200005;
int n, ans, pw2[MAXN];
int fir[MAXN], to[MAXN << 1], nxt[MAXN << 1], cnt;
inline void link(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = fir[u];
  fir[u] = cnt;
  to[++cnt] = u;
  nxt[cnt] = fir[v];
  fir[v] = cnt;
}
int sz[MAXN];
void dfs(int u, int ff) {
  sz[u] = 1;
  int p0 = 1, p1 = 0, q0, q1, p;
  for (int i = fir[u], v; i; i = nxt[i])
    if ((v = to[i]) != ff) {
      dfs(v, u);
      sz[u] += sz[v];
      p = pw2[sz[v]];
      q0 = 1ll * p0 * p % mod;
      q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod;
      p0 = q0, p1 = q1;
    }
  p = pw2[n - sz[u]];
  q0 = 1ll * p0 * p % mod;
  q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod;
  p0 = q0, p1 = q1;
  ans = (ans + (1ll + mod + mod - p0 - p1) % mod * inv2 % mod) % mod;
}
int main() {
  scanf("%d", &n);
  pw2[0] = 1;
  for (int i = 1, x, y; i < n; ++i)
    scanf("%d%d", &x, &y), link(x, y), pw2[i] = 1ll * pw2[i - 1] * inv2 % mod;
  pw2[n] = 1ll * pw2[n - 1] * inv2 % mod;
  dfs(1, 0);
  printf("%d\n", ans);
}
Problem p02822 (C++)

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

using namespace std;

#define MOD 1000000007
#define HALF ((MOD + 1) / 2)

int n;
vector<int> v[100005];
int dp[100005];
long long ans;

long long po(long long b, int p) {
  long long re = 1;
  while (p) {
    if (p % 2)
      re = re * b % MOD;
    b = b * b % MOD;
    p /= 2;
  }
  return re;
}

void dfs(int a, int b) {
  dp[a] = 1;
  vector<int> tv;
  for (int i : v[a])
    if (i != b) {
      dfs(i, a);
      dp[a] += dp[i];
      tv.push_back(dp[i]);
    }
  tv.push_back(n - dp[a]);
  long long tmp = 1;
  tmp = (tmp + MOD - po(HALF, n - 1)) % MOD;
  for (int i : tv)
    tmp =
        (tmp + MOD - po(HALF, n - 1 - i) * (1 + MOD - po(HALF, i)) % MOD) % MOD;
  ans = (ans + HALF * tmp) % MOD;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int ta, tb;
    scanf("%d%d", &ta, &tb);
    v[ta].push_back(tb);
    v[tb].push_back(ta);
  }
  dfs(1, 0);
  printf("%lld\n", ans);
}

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

using namespace std;

#define MOD 1000000007
#define HALF ((MOD + 1) / 2)

int n;
vector<int> v[200005];
int dp[200005];
long long ans;

long long po(long long b, int p) {
  long long re = 1;
  while (p) {
    if (p % 2)
      re = re * b % MOD;
    b = b * b % MOD;
    p /= 2;
  }
  return re;
}

void dfs(int a, int b) {
  dp[a] = 1;
  vector<int> tv;
  for (int i : v[a])
    if (i != b) {
      dfs(i, a);
      dp[a] += dp[i];
      tv.push_back(dp[i]);
    }
  tv.push_back(n - dp[a]);
  long long tmp = 1;
  tmp = (tmp + MOD - po(HALF, n - 1)) % MOD;
  for (int i : tv)
    tmp =
        (tmp + MOD - po(HALF, n - 1 - i) * (1 + MOD - po(HALF, i)) % MOD) % MOD;
  ans = (ans + HALF * tmp) % MOD;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int ta, tb;
    scanf("%d%d", &ta, &tb);
    v[ta].push_back(tb);
    v[tb].push_back(ta);
  }
  dfs(1, 0);
  printf("%lld\n", ans);
}
Problem p02822 (C++)

Buggy submission (status: Runtime Error):
//
// Created by 陈基彤 on 2020-01-09.
//
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1e5 + 5;
const LL mod = 1e9 + 7;
vector<int> edge[maxn];
int n;
LL sum[maxn], num[maxn];
LL tt;
void dfs(int x, int fa) {
  LL nows = 0, nown = 0, tot = 0;
  for (auto i : edge[x])
    if (i != fa) {
      dfs(i, x);
      tot = (tot + nown * num[i] % mod + nown * sum[i] % mod +
             num[i] * nows % mod) %
            mod;
      nows = (nown * sum[i] % mod + num[i] * nows % mod + sum[i] + nows) % mod;
      nown = (nown * num[i] % mod + num[i] + nown) % mod;
    }
  num[x] = (nown * 2 + 1) % mod;
  sum[x] = (nows * 2 + nown) % mod;
  tt = (tt + tot + nows) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  LL inv2 = (mod + 1) / 2;
  //    printf("inv2 is %lld\n",inv2);
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(1, 0);
  //    for(int i=1;i<=n;i++)
  //        printf("%lld %lld %lld\n",i,num[i],sum[i]);
  tt = (tt % mod + mod) % mod;
  //    printf("%lld\n",tt);
  LL dv = 1;
  for (int i = 1; i <= n; i++)
    dv = (dv * inv2) % mod;
  cout << tt * dv % mod << endl;
  return 0;
}

/*
5
1 2
1 5
2 3
2 4
*/


Fixed submission:
//
// Created by 陈基彤 on 2020-01-09.
//
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 2e5 + 5;
const LL mod = 1e9 + 7;
vector<int> edge[maxn];
int n;
LL sum[maxn], num[maxn];
LL tt;
void dfs(int x, int fa) {
  LL nows = 0, nown = 0, tot = 0;
  for (auto i : edge[x])
    if (i != fa) {
      dfs(i, x);
      tot = (tot + nown * num[i] % mod + nown * sum[i] % mod +
             num[i] * nows % mod) %
            mod;
      nows = (nown * sum[i] % mod + num[i] * nows % mod + sum[i] + nows) % mod;
      nown = (nown * num[i] % mod + num[i] + nown) % mod;
    }
  num[x] = (nown * 2 + 1) % mod;
  sum[x] = (nows * 2 + nown) % mod;
  tt = (tt + tot + nows) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  LL inv2 = (mod + 1) / 2;
  //    printf("inv2 is %lld\n",inv2);
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(1, 0);
  //    for(int i=1;i<=n;i++)
  //        printf("%lld %lld %lld\n",i,num[i],sum[i]);
  tt = (tt % mod + mod) % mod;
  //    printf("%lld\n",tt);
  LL dv = 1;
  for (int i = 1; i <= n; i++)
    dv = (dv * inv2) % mod;
  cout << tt * dv % mod << endl;
  return 0;
}

/*
5
1 2
1 5
2 3
2 4
*/
Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

const ll mod = 1e9 + 7;

ll mod_pow(ll x, ll n) {
  ll ret = 1;
  while (n) {
    if (n & 1) {
      ret = ret * x % mod;
      x = x * x % mod;
      n >>= 1;
    } else {
      x = x * x % mod;
      n >>= 1;
    }
  }
  return ret;
}

int n;
vector<int> g[101010];
ll ans = 0;

ll dfs(int v, int p) {
  vector<int> child;
  int sz = 0;

  for (auto &u : g[v]) {
    if (u == p)
      continue;
    child.push_back(dfs(u, v));
    sz += child[child.size() - 1];
  }

  child.push_back(n - 1 - sz);

  ll prob = mod_pow(mod_pow(2, n), mod - 2);
  for (auto &c : child) {
    prob += mod_pow(mod_pow(2, n - c), mod - 2) *
            (1 - mod_pow(mod_pow(2, c), mod - 2)) % mod;
  }
  ans += (mod_pow(2, mod - 2) - prob) % mod;
  ans %= mod;

  return sz + 1;
}

int main() {
  cin >> n;

  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }

  dfs(0, -1);
  cout << ans << endl;
}


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

const ll mod = 1e9 + 7;

ll mod_pow(ll x, ll n) {
  ll ret = 1;
  while (n) {
    if (n & 1) {
      ret = ret * x % mod;
      x = x * x % mod;
      n >>= 1;
    } else {
      x = x * x % mod;
      n >>= 1;
    }
  }
  return ret;
}

int n;
vector<int> g[201010];
ll ans = 0;

ll dfs(int v, int p) {
  vector<int> child;
  int sz = 0;

  for (auto &u : g[v]) {
    if (u == p)
      continue;
    child.push_back(dfs(u, v));
    sz += child[child.size() - 1];
  }

  child.push_back(n - 1 - sz);

  ll prob = mod_pow(mod_pow(2, n), mod - 2);
  for (auto &c : child) {
    prob += mod_pow(mod_pow(2, n - c), mod - 2) *
            (1 - mod_pow(mod_pow(2, c), mod - 2)) % mod;
  }
  ans += (mod_pow(2, mod - 2) - prob) % mod;
  ans %= mod;

  return sz + 1;
}

int main() {
  cin >> n;

  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }

  dfs(0, -1);
  cout << ans << endl;
}
Problem p02822 (C++)

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

using ll = long long;

const ll MOD = 1e9 + 7;
const int MAX_N = 200000;
map<pair<int, int>, int> mp;
// mp[make_pair(i, j)] iから見てjの先にあるnodeの数
vector<int> edge[MAX_N];
int N;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int nodes(int i, int j) {
  pair<int, int> k = make_pair(i, j);
  if (mp.find(k) != mp.end())
    return mp[k];
  if (edge[j].size() == 1) {
    mp[k] = 1;
    mp[make_pair(j, i)] = N - 1;
    return 1;
  }
  int ret = 1;
  rep(l, edge[j].size()) {
    if (edge[j][l] == i)
      continue;
    ret += nodes(j, edge[j][l]);
  }
  mp[k] = ret;
  return ret;
}

int main() {
  int A, B;
  cin >> N;
  rep(i, N - 1) {
    cin >> A >> B;
    A--;
    B--;
    edge[A].push_back(B);
    edge[B].push_back(A);
  }
  /*
  rep(i, N) {
      rep(j, edge[i].size()) {
          cout << "nodes[" << i << "," << edge[i][j] << "]=" << nodes(i,
  edge[i][j]) << endl;
      }
  }
  */
  ll ans = 0;
  rep(i, N) {
    ans = (ans + mod_pow(2, N - 1, MOD) - 1) % MOD;
    rep(j, edge[i].size()) {
      ans = (ans - mod_pow(2, nodes(i, edge[i][j]), MOD) + 1 + MOD) % MOD;
    }
  }
  cout << (ans * mod_pow(mod_pow(2, N, MOD), MOD - 2, MOD)) % MOD << "\n";
}

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

using ll = long long;

const ll MOD = 1e9 + 7;
const int MAX_N = 200000;
map<pair<int, int>, int> mp;
// mp[make_pair(i, j)] iから見てjの先にあるnodeの数
vector<int> edge[MAX_N];
int N;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int nodes(int i, int j) {
  pair<int, int> k = make_pair(i, j);
  if (mp.find(k) != mp.end())
    return mp[k];
  if (edge[j].size() == 1) {
    mp[k] = 1;
    mp[make_pair(j, i)] = N - 1;
    return 1;
  }
  int ret = 1;
  rep(l, edge[j].size()) {
    if (edge[j][l] == i)
      continue;
    ret += nodes(j, edge[j][l]);
  }
  mp[k] = ret;
  mp[make_pair(j, i)] = N - ret;
  return ret;
}

int main() {
  int A, B;
  cin >> N;
  rep(i, N - 1) {
    cin >> A >> B;
    A--;
    B--;
    edge[A].push_back(B);
    edge[B].push_back(A);
  }
  /*
  rep(i, N) {
      rep(j, edge[i].size()) {
          cout << "nodes[" << i << "," << edge[i][j] << "]=" << nodes(i,
  edge[i][j]) << endl;
      }
  }
  */
  ll ans = 0;
  rep(i, N) {
    ans = (ans + mod_pow(2, N - 1, MOD) - 1) % MOD;
    rep(j, edge[i].size()) {
      ans = (ans - mod_pow(2, nodes(i, edge[i][j]), MOD) + 1 + MOD) % MOD;
    }
  }
  cout << (ans * mod_pow(mod_pow(2, N, MOD), MOD - 2, MOD)) % MOD << "\n";
}
Problem p02822 (C++)

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

#define MOD 1000000007

#define int long long

int n;
vector<int> graph[100010];

int dp1[200010];
int dp2[200010];

int zi[200010];

int pow_mod(int i, int j) {
  int ans = 1;
  while (j > 0) {
    if (j & 1)
      ans = (ans * i) % MOD;
    i = (i * i) % MOD;
    j >>= 1;
  }

  return ans;
}

void dp(int idx, int par) {
  int ans = 1;
  int ans2 = zi[n - 1];

  for (int i = 0; i < graph[idx].size(); i++) {
    int child = graph[idx][i];
    if (child == par)
      continue;

    dp(child, idx);

    ans += dp1[child];
    ans2 -= (MOD + zi[dp1[child]] - 1) % MOD;
    if (ans2 < 0)
      ans2 = MOD + ans2;
  }

  dp1[idx] = ans;
  ans2 -= zi[n - ans];
  if (ans2 < 0)
    ans2 = MOD + ans2;

  dp2[idx] = ans2;
  return;
}

signed main() {
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  zi[0] = 1;
  for (int i = 1; i < 200010; i++) {
    zi[i] = zi[i - 1] * 2 % MOD;
  }

  dp(0, -1);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp2[i];
    ans %= MOD;
  }

  cout << ans * pow_mod(zi[n], MOD - 2) % MOD << endl;
  return 0;
}


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

#define MOD 1000000007

#define int long long

int n;
vector<int> graph[200010];

int dp1[200010];
int dp2[200010];

int zi[200010];

int pow_mod(int i, int j) {
  int ans = 1;
  while (j > 0) {
    if (j & 1)
      ans = (ans * i) % MOD;
    i = (i * i) % MOD;
    j >>= 1;
  }

  return ans;
}

void dp(int idx, int par) {
  int ans = 1;
  int ans2 = zi[n - 1];

  for (int i = 0; i < graph[idx].size(); i++) {
    int child = graph[idx][i];
    if (child == par)
      continue;

    dp(child, idx);

    ans += dp1[child];
    ans2 -= (MOD + zi[dp1[child]] - 1) % MOD;
    if (ans2 < 0)
      ans2 = MOD + ans2;
  }

  dp1[idx] = ans;
  ans2 -= zi[n - ans];
  if (ans2 < 0)
    ans2 = MOD + ans2;

  dp2[idx] = ans2;
  return;
}

signed main() {
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  zi[0] = 1;
  for (int i = 1; i < 200010; i++) {
    zi[i] = zi[i - 1] * 2 % MOD;
  }

  dp(0, -1);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp2[i];
    ans %= MOD;
  }

  cout << ans * pow_mod(zi[n], MOD - 2) % MOD << endl;
  return 0;
}
Problem p02822 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *AB = map(int, read().split())
    G = [[] for _ in range(N)]
    for a, b in zip(AB[::2], AB[1::2]):
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    stack = deque([0])
    order = []
    prev = [0] * N
    prev[0] = -1
    while stack:
        v = stack.pop()
        order.append(v)
        for nv in G[v]:
            if nv != prev[v]:
                stack.append(nv)
                prev[nv] = v

    pow2 = [0] * (N + 1)
    pow2[0] = 1
    for i in range(N):
        pow2[i + 1] = pow2[i] * 2

    nodes = [1] * N
    numer = 0

    for v in reversed(order):
        if prev[v] != -1:
            nodes[prev[v]] += nodes[v]
        numer = (numer + pow2[N - 1] - 1 - (pow2[N - nodes[v]] - 1)) % MOD
        for nv in G[v]:
            if nv != prev[v]:
                numer = (numer - (pow2[nodes[nv]] - 1)) % MOD

    denom = pow2[N]
    ans = (numer * pow(denom, MOD - 2, MOD)) % MOD
    print(ans)
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *AB = map(int, read().split())
    G = [[] for _ in range(N)]
    for a, b in zip(AB[::2], AB[1::2]):
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    stack = deque([0])
    order = []
    prev = [0] * N
    prev[0] = -1
    while stack:
        v = stack.pop()
        order.append(v)
        for nv in G[v]:
            if nv != prev[v]:
                stack.append(nv)
                prev[nv] = v

    pow2 = [0] * (N + 1)
    pow2[0] = 1
    for i in range(N):
        pow2[i + 1] = pow2[i] * 2 % MOD

    nodes = [1] * N
    numer = 0

    for v in reversed(order):
        if prev[v] != -1:
            nodes[prev[v]] += nodes[v]
        numer = (numer + pow2[N - 1] - 1 - (pow2[N - nodes[v]] - 1)) % MOD
        for nv in G[v]:
            if nv != prev[v]:
                numer = (numer - (pow2[nodes[nv]] - 1)) % MOD

    denom = pow2[N]
    ans = (numer * pow(denom, MOD - 2, MOD)) % MOD
    print(ans)
    return


if __name__ == "__main__":
    main()
Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

ll ans = 0;
int n;
vector<int> E[101010];
VI v(101010);
VI w(101010);

VI tw(202020);

void dfs(int cu, int pa = -1) {
  v[cu] = 1;
  w[cu] = tw[n - 1] - 1;
  for (int to : E[cu]) {
    if (to != pa) {
      //
      dfs(to, cu);
      //
      v[cu] += v[to];
      w[cu] += w[to];
      w[cu] += (mod - tw[v[to]] + 1) % mod;
      w[cu] %= mod;
    }
  }
  w[cu] += (mod - (tw[n - v[cu]]) + 1) % mod;
  w[cu] %= mod;
}

ll modinv(ll a) {
  ll m = mod;
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  int i, j;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].pb(b);
    E[b].pb(a);
  }
  tw[0] = 1;
  for (i = 1; i < 202020; i++) {
    tw[i] = tw[i - 1] * 2 % mod;
  }
  dfs(0);
  for (i = 0; i < n; i++) {
    // cout<<i+1<<" "<<w[i]<<" "<<v[i]<<endl;
  }
  ll ans = w[0] * modinv(tw[n]) % mod;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

ll ans = 0;
int n;
vector<int> E[201010];
VI v(201010);
VI w(201010);

VI tw(202020);

void dfs(int cu, int pa = -1) {
  v[cu] = 1;
  w[cu] = tw[n - 1] - 1;
  for (int to : E[cu]) {
    if (to != pa) {
      //
      dfs(to, cu);
      //
      v[cu] += v[to];
      w[cu] += w[to];
      w[cu] += (mod - tw[v[to]] + 1) % mod;
      w[cu] %= mod;
    }
  }
  w[cu] += (mod - (tw[n - v[cu]]) + 1) % mod;
  w[cu] %= mod;
}

ll modinv(ll a) {
  ll m = mod;
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  int i, j;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].pb(b);
    E[b].pb(a);
  }
  tw[0] = 1;
  for (i = 1; i < 202020; i++) {
    tw[i] = tw[i - 1] * 2 % mod;
  }
  dfs(0);
  for (i = 0; i < n; i++) {
    // cout<<i+1<<" "<<w[i]<<" "<<v[i]<<endl;
  }
  ll ans = w[0] * modinv(tw[n]) % mod;
  cout << ans << endl;

  return 0;
}
Problem p02822 (C++)

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

using namespace std;
#define int long long
const int N = 1e5 + 10, inf = 1e9, MOD = 1e9 + 7;
int a[N], sz[N], n, ans, X;
vector<int> g[N];

int Pow(int a, int m) {
  if (m == 0)
    return 1;
  int x = Pow(a, m / 2);
  return (((x * x) % MOD) * (m % 2 ? a : 1)) % MOD;
}

int dfs(int v, int p = 0) {
  sz[v] = 1;
  vector<int> vec;
  for (int u : g[v])
    if (u != p) {
      dfs(u, v);
      sz[v] += sz[u];
      vec.push_back(sz[u]);
    }
  vec.push_back(n - sz[v]);
  int val = 0;
  for (int i : vec) {
    val = (val + Pow(X, n - i - 1) - Pow(X, n - 1) + MOD) % MOD;
  }
  val = (val + Pow(X, n - 1)) % MOD;
  ans = (ans + X * (1 - val + MOD)) % MOD;
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  X = Pow(2, MOD - 2);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  cout << ans << "\n";
}


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

using namespace std;
#define int long long
const int N = 2e5 + 10, inf = 1e9, MOD = 1e9 + 7;
int a[N], sz[N], n, ans, X;
vector<int> g[N];

int Pow(int a, int m) {
  if (m == 0)
    return 1;
  int x = Pow(a, m / 2);
  return (((x * x) % MOD) * (m % 2 ? a : 1)) % MOD;
}

int dfs(int v, int p = 0) {
  sz[v] = 1;
  vector<int> vec;
  for (int u : g[v])
    if (u != p) {
      dfs(u, v);
      sz[v] += sz[u];
      vec.push_back(sz[u]);
    }
  vec.push_back(n - sz[v]);
  int val = 0;
  for (int i : vec) {
    val = (val + Pow(X, n - i - 1) - Pow(X, n - 1) + MOD) % MOD;
  }
  val = (val + Pow(X, n - 1)) % MOD;
  ans = (ans + X * (1 - val + MOD)) % MOD;
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  X = Pow(2, MOD - 2);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  cout << ans << "\n";
}
Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

const ll mod = 1e9 + 7;
ll mu(ll a, ll b) { return (ll)a * b % mod; }
ll ad(ll a, ll b) {
  if ((a += b) >= mod)
    a -= mod;
  return a;
}
ll pw(ll a, ll b, ll c = 1LL) {
  for (; b; b >>= 1, a = mu(a, a))
    if (b & 1)
      c = mu(c, a);
  return c;
}
ll dv(ll a, ll b) { return mu(a, pw(b, mod - 2)); }

ll ans = 0;
const int n_max = 1e5;
vector<int> g[n_max];
ll pwall;
ll n;

int dfs(int v, int p) {
  vector<int> kv;
  int sum = 1;
  for (auto i : g[v]) {
    if (i == p)
      continue;
    int k = dfs(i, v);
    sum += k;
    kv.pb(k);
  }
  kv.pb(n - sum);
  ll out = 1;
  rep(j, kv.size()) {
    //    cout << kv[j] << endl;
    out = ad(out, ad(pw(2, kv[j]) - 1 + mod, 0));
  }
  ll ok = ad(pwall - out + mod, 0);

  if (sum != 1) {
    ans = ad(ans, ok);
  }
  return sum;
}

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

  cin >> n;

  pwall = pw(2, n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(0, -1);
  //  cout << "場合の数　" << ans << endl;
  cout << dv(ans, pw(2, n)) << endl;

  //  cout << endl << endl;
  //  cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

const ll mod = 1e9 + 7;
ll mu(ll a, ll b) { return (ll)a * b % mod; }
ll ad(ll a, ll b) {
  if ((a += b) >= mod)
    a -= mod;
  return a;
}
ll pw(ll a, ll b, ll c = 1LL) {
  for (; b; b >>= 1, a = mu(a, a))
    if (b & 1)
      c = mu(c, a);
  return c;
}
ll dv(ll a, ll b) { return mu(a, pw(b, mod - 2)); }

ll ans = 0;
const int n_max = 2e5 + 10;
vector<int> g[n_max];
ll pwall;
ll n;

int dfs(int v, int p) {
  vector<int> kv;
  int sum = 1;
  for (auto i : g[v]) {
    if (i == p)
      continue;
    int k = dfs(i, v);
    sum += k;
    kv.pb(k);
  }
  kv.pb(n - sum);
  ll out = 1;
  rep(j, kv.size()) {
    //    cout << kv[j] << endl;
    out = ad(out, ad(pw(2, kv[j]) - 1 + mod, 0));
  }
  ll ok = ad(pwall - out + mod, 0);

  if (sum != 1) {
    ans = ad(ans, ok);
  }
  return sum;
}

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

  cin >> n;

  pwall = pw(2, n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(0, -1);
  //  cout << "場合の数　" << ans << endl;
  cout << dv(ans, pw(2, n)) << endl;

  //  cout << endl << endl;
  //  cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
}
Problem p02822 (C++)

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

typedef long long LL;
const int M = 1e5 + 5;
const int MO = 1e9 + 7;

int add(int x, int y) {
  x += y;
  if (x >= MO)
    x -= MO;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += MO;
  return x;
}
int mul(int x, int y) { return 1LL * x * y % MO; }

int qp(int a, int n) {
  int ans = 1;
  while (n) {
    if (n & 1)
      ans = mul(ans, a);
    a = mul(a, a);
    n >>= 1;
  }
  return ans;
}

vector<int> g[M];
int sz[M];
int dfs1(int u, int fa) {
  sz[u] = 1;
  for (int v : g[u]) {
    if (v == fa)
      continue;
    sz[u] += dfs1(v, u);
  }
  return sz[u];
}

int ans = 0;
int n;
void dfs2(int u, int fa) {
  ans = add(ans, sub(qp(2, n - 1), 1));
  if (fa)
    ans = sub(ans, sub(qp(2, n - sz[u]), 1));
  for (int v : g[u]) {
    if (v == fa)
      continue;
    ans = sub(ans, sub(qp(2, sz[v]), 1));
    dfs2(v, u);
  }
}

int main() {
  scanf("%d", &n);
  int u, v;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  printf("%d\n", mul(ans, qp(qp(2, n), MO - 2)));
  return 0;
}

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

typedef long long LL;
const int M = 2e5 + 5;
const int MO = 1e9 + 7;

int add(int x, int y) {
  x += y;
  if (x >= MO)
    x -= MO;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += MO;
  return x;
}
int mul(int x, int y) { return 1LL * x * y % MO; }

int qp(int a, int n) {
  int ans = 1;
  while (n) {
    if (n & 1)
      ans = mul(ans, a);
    a = mul(a, a);
    n >>= 1;
  }
  return ans;
}

vector<int> g[M];
int sz[M];
int dfs1(int u, int fa) {
  sz[u] = 1;
  for (int v : g[u]) {
    if (v == fa)
      continue;
    sz[u] += dfs1(v, u);
  }
  return sz[u];
}

int ans = 0;
int n;
void dfs2(int u, int fa) {
  ans = add(ans, sub(qp(2, n - 1), 1));
  if (fa)
    ans = sub(ans, sub(qp(2, n - sz[u]), 1));
  for (int v : g[u]) {
    if (v == fa)
      continue;
    ans = sub(ans, sub(qp(2, sz[v]), 1));
    dfs2(v, u);
  }
}

int main() {
  scanf("%d", &n);
  int u, v;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  printf("%d\n", mul(ans, qp(qp(2, n), MO - 2)));
  return 0;
}
Problem p02823 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
using namespace std;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll fac(ll a) { return a > 1 ? fac(a - 1) * a : 1; }
int main() {
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  ll n, a, b;
  cin >> n >> a >> b;
  ll A, B, C, D;
  if (abs(b - a) % 2 == 0) {
    A = (b - a) / 2;
    cout << A << endl;
  } else {
    return -1;
  }
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
using namespace std;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll fac(ll a) { return a > 1 ? fac(a - 1) * a : 1; }
int main() {
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  ll n, a, b;
  cin >> n >> a >> b;
  ll A, B, C, D;
  if (abs(b - a) % 2 == 0) {
    A = (b - a) / 2;
    cout << A << endl;
  } else {
    A = max(abs(n - b), abs(n - a));
    B = max(abs(1 - b), abs(1 - a));
    ll aa = a;
    ll bb = b;
    C += abs(1 - a);
    bb -= C;
    C++;
    bb--;
    if (bb != 1)
      C += abs(1 - bb) / 2;

    D += abs(n - b);
    aa += D;
    D++;
    a++;
    if (aa != n)
      D += abs(n - aa) / 2;
    cout << min({A, B, C, D}) << endl;
  }
  return 0;
}
Problem p02823 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll n, a, b;
  cin >> n >> a >> b;

  ll ans = 0;
  if ((b - a) % 2 == 0) {
    ans = (b - a) / 2;
  } else {
    while ((b - a) % 2 != 0) {
      ans++;
      b--;
      a--;
      a = a > 0 ? a : 1;
      if ((b - a) % 2 == 0)
        ans += (b - a) / 2;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll n, a, b;
  cin >> n >> a >> b;

  ll ans = 0;
  if ((b - a) % 2 == 0) {
    ans = (b - a) / 2;
  } else {
    ans = min(n - b, a - 1) + 1 + (b - a - 1) / 2;
  }
  cout << ans << endl;
}
Problem p02823 (C++)

Buggy submission (status: Time Limit Exceeded):
/********************************************************************************

        Code by a weak man who named CYJian, and he hope the code can get more
 points.

        Algorithm:

 ********************************************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

int main() {
#ifndef ONLINE_JUDGE
  FILE("");
#endif
  ll n = rl, A = rl, B = rl;
  if ((A ^ B) & 1) {
    if (A > B)
      swap(A, B);
    cout << min(A, n - B + 1) + (B - A) / 2 << endl;
  } else
    cout << abs(A - B) / 2 << endl;
  return 0;
}

Fixed submission:
/********************************************************************************

        Code by a weak man who named CYJian, and he hope the code can get more
 points.

        Algorithm:

 ********************************************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

int main() {
  ll n = rl, A = rl, B = rl;
  if ((A ^ B) & 1) {
    if (A > B)
      swap(A, B);
    cout << min(A, n - B + 1) + (B - A) / 2 << endl;
  } else
    cout << abs(A - B) / 2 << endl;
  return 0;
}
Problem p02823 (Python)

Buggy submission (status: Runtime Error):
def main():
    N, A, B = [int(i) for i in input().split()]
    diff = B - A
    if diff % 2 == 0:
        ans = diff // 2
    else:
        ans = min(
            B - 1, N - A, (A - 1) + 1 + (diff - 1) // 2, (N - B) + 1(diff - 1) // 2
        )
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, A, B = [int(i) for i in input().split()]
    diff = B - A
    if diff % 2 == 0:
        ans = diff // 2
    else:
        ans = min(
            B - 1, N - A, (A - 1) + 1 + (diff - 1) // 2, (N - B) + 1 + (diff - 1) // 2
        )
    print(ans)


if __name__ == "__main__":
    main()
Problem p02823 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long int64;
int64 solve(int64 N, int64 A, int64 B) {
  if ((B - A) % 2 == 0)
    return (B - A) / 2;
  int64 ret = min(B - 1, N - A);
  ret = min(ret, solve(N, 1, N - A) + A);
  ret = min(ret, solve(N, A + (N - B) + 1, N) + (N - B) + 1);
  return ret;
}
int main() {
  int64 N, A, B;
  scanf("%lld%lld%lld", &N, &A, &B);
  int64 ans = solve(N, A, B);
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long int64;
int64 solve(int64 N, int64 A, int64 B) {
  if ((B - A) % 2 == 0)
    return (B - A) / 2;
  int64 ret = min(B - 1, N - A);
  ret = min(ret, solve(N, 1, B - A) + A);
  ret = min(ret, solve(N, A + (N - B) + 1, N) + (N - B) + 1);
  return ret;
}
int main() {
  int64 N, A, B;
  scanf("%lld%lld%lld", &N, &A, &B);
  int64 ans = solve(N, A, B);
  printf("%lld\n", ans);
}
Problem p02823 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll N = 5e5 + 5;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, a, b;
  cin >> n >> a >> b;
  ll ans = min(b - 1, n - a);
  if (a % 2 == b % 2)
    ans = min(ans, (b - a) / 2);

  if (a % 2 != b % 2)
    while (1) {
      ;
    }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll N = 5e5 + 5;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, a, b;
  cin >> n >> a >> b;
  ll ans = min(b - 1, n - a);
  if (a % 2 == b % 2)
    ans = min(ans, (b - a) / 2);
  else {
    ll val = min(a - 1, n - b);
    val = val + (b - a + 1) / 2;
    ans = min(ans, val);
  }
  cout << ans;
}
Problem p02823 (C++)

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

using namespace std;
typedef long long ll;
#define int ll

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
const int MAX_N = 100010;

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

  ll N, A, B;
  cin >> N >> A >> B;

  ll ab_diff = abs(A - B);
  ll ans = LLINF;
  if (ab_diff % 2 == 0) {
    ans = ab_diff / 2;
  } else {
    assert(false);
    ans = min(max(A - 1, B - 1), max(N - A, N - B));
  }
  printf("%lld\n", ans);

  return 0;
}


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

using namespace std;
typedef long long ll;
#define int ll

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
const int MAX_N = 100010;

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

  ll N, A, B;
  cin >> N >> A >> B;

  ll ab_diff = abs(A - B);
  ll ans = LLINF;
  if (ab_diff % 2 == 0) {
    ans = ab_diff / 2;
  } else {
    // 左端に移動する場合
    ll cand1 = A - 1; // 左に行き着くのにかかるターン数
    ll tmp_b = B - cand1;
    if (tmp_b % 2 != 0) {
      tmp_b--;
      cand1++;
    }
    cand1 += tmp_b / 2;

    // 右端に移動する場合
    ll cand2 = N - B;
    tmp_b = (N - A) - cand2;
    if (tmp_b % 2 != 0) {
      tmp_b--;
      cand2++;
    }
    cand2 += tmp_b / 2;
    ans = min(cand1, cand2);
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p02823 (C++)

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

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int N, A, B;
  cin >> N >> A >> B;
  assert(A != B);
  if (A > B)
    swap(A, B);
  assert(A < B);
  if (A % 2 == B % 2) {
    cout << (B - A) / 2 << '\n';
  } else {
    cout << min((N - B) + 1 + (N - A), (A - 1) + 1 + (B - 1)) / 2 << '\n';
  }

  return 0;
}


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

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long N, A, B;
  cin >> N >> A >> B;
  assert(A != B);
  if (A > B)
    swap(A, B);
  assert(A < B);
  if (A % 2 == B % 2) {
    cout << (B - A) / 2 << '\n';
  } else {
    cout << min((N - B) + 1 + (N - A), (A - 1) + 1 + (B - 1)) / 2 << '\n';
  }

  return 0;
}
Problem p02823 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ll N, A, B;
  cin >> N >> A >> B;

  ll ans;
  if ((B - A) % 2 == 0)
    ans = (B - A) / 2;
  else {
    while (1) {
    }
    ans = min(B - 1, N - A);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ll N, A, B;
  cin >> N >> A >> B;

  ll ans;
  if ((B - A) % 2 == 0)
    ans = (B - A) / 2;
  else {
    ans = min(A - 1, N - B) + 1 + (B - A - 1) / 2;
  }

  cout << ans << endl;

  return 0;
}
Problem p02824 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
typedef long long ll;
int n, m, v, p, a[100005], b[100005], ans, pth_min;
bool judge(int pth) {
  ll res = 0;
  int i;
  for (i = 1; i <= n; i++)
    b[i] = min(m, pth - a[i]);
  sort(b + 1, b + n + 1);
  if (b[p] < 0)
    return 0;
  for (i = 1; i <= n; i++)
    res += i <= p - 1 ? m : b[i];
  return res >= (ll)m * v;
}
int solve() {
  int l = 0, r = 2e9, mid, res = -1;
  while (l <= r) {
    mid = (ll)(l + r) >> 1;
    if (judge(mid))
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  return res;
}
int main() {
  scanf("%d%d%d%d", &n, &m, &v, &p);
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  pth_min = solve();
  for (i = 1; i <= n; i++)
    ans += a[i] + m >= pth_min;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
typedef long long ll;
int n, m, v, p, a[100005], b[100005], ans, pth_min;
bool judge(int pth) {
  ll res = 0;
  int i;
  for (i = 1; i <= n; i++)
    b[i] = min(m, pth - a[i]);
  sort(b + 1, b + n + 1);
  if (b[p] < 0)
    return 0;
  for (i = 1; i <= n; i++)
    res += i <= p - 1 ? m : b[i];
  return res >= (ll)m * v;
}
int solve() {
  int l = 0, r = 2e9, mid, res = -1;
  while (l <= r) {
    mid = ((ll)l + r) >> 1;
    if (judge(mid))
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  return res;
}
int main() {
  scanf("%d%d%d%d", &n, &m, &v, &p);
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  pth_min = solve();
  for (i = 1; i <= n; i++)
    ans += a[i] + m >= pth_min;
  printf("%d\n", ans);
  return 0;
}
Problem p02824 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int n, m, v, p, a[100010];

bool calc(int mid, ll cnt = 0) {
  int x = a[mid] + m;
  if (a[p - 1] > x)
    return false;
  cnt += (ll)(p - 1) * m;
  cnt += (ll)(n - mid) * m;
  for (int i = p - 1; i <= x - 1; ++i) {
    cnt += min(m, max(0, x - a[i]));
  }
  return cnt >= (ll)m * v;
}

int main() {
  cin >> n >> m >> v >> p;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);
  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (calc(mid))
      l = mid;
    else
      r = mid;
  }
  // 1-indexedに修正
  cout << l + 1 << endl;
}

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

int n, m, v, p, a[100010];

bool calc(int mid, ll cnt = 0) {
  int x = a[mid] + m;
  if (a[p - 1] > x)
    return false;
  cnt += (ll)(p - 1) * m;
  cnt += (ll)(n - mid) * m;
  for (int i = p - 1; i <= mid - 1; ++i) {
    cnt += min(m, max(0, x - a[i]));
  }
  return cnt >= (ll)m * v;
}

int main() {
  cin >> n >> m >> v >> p;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);
  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (calc(mid))
      l = mid;
    else
      r = mid;
  }
  // 1-indexedに修正
  cout << l + 1 << endl;
}