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

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

typedef long long ll;

int main() {
  freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

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

  for (int i = 1; i * i <= sum; ++i) {
    if (sum % i == 0) {
      factors.push_back(i);
      factors.push_back(sum / i);
    }
  }

  multiset<int> s;
  int ans = 0;
  for (int j = 0; j < (int)factors.size(); ++j) {
    int d = factors[j];
    // cout<<"d : " <<d<<endl;
    int totalCost = 0;
    s.clear();
    for (int i = 0; i < n; ++i) {
      int mod = A[i] % d;
      s.insert(mod);
      totalCost += (d - mod);
    }

    int cnt = 0;
    for (auto e : s) {
      // cout<<e<<" ";
      cnt += e;
      totalCost -= d;
      if (totalCost == 0 && cnt <= k) {
        ans = max(ans, d);
      }
    }
  }
  cout << ans << endl;

  return 0;
}

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

typedef long long ll;

int main() {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

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

  for (int i = 1; i * i <= sum; ++i) {
    if (sum % i == 0) {
      factors.push_back(i);
      factors.push_back(sum / i);
    }
  }

  multiset<int> s;
  int ans = 0;
  for (int j = 0; j < (int)factors.size(); ++j) {
    int d = factors[j];
    // cout<<"d : " <<d<<endl;
    int totalCost = 0;
    s.clear();
    for (int i = 0; i < n; ++i) {
      int mod = A[i] % d;
      s.insert(mod);
      totalCost += (d - mod);
    }

    int cnt = 0;
    for (auto e : s) {
      // cout<<e<<" ";
      cnt += e;
      totalCost -= d;
      if (totalCost == 0 && cnt <= k) {
        ans = max(ans, d);
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02955 (C++)

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

using namespace std;

int n, k;
int a[505];
int factor[100000];
int need[100000];
int sum = 0;

bool good(int x) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int t = a[i] % x;
    if (t)
      need[++cnt] = t;
  }

  sort(need + 1, need + 1 + cnt);
  int have = k;
  int l = 1, r = cnt;
  while (l < r) {
    int t = min(need[l], x - need[r]);
    if (t > have)
      return false;
    have -= t;
    need[l] -= t;
    if (need[l] == 0)
      l++;
    need[r] += t;
    if (need[r] == x)
      r--;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
#else
  freopen("in.in", "r", stdin);
#endif

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

  for (int i = 1; i * i < sum; i++) {
    if (sum % i == 0) {
      factor[++factor[0]] = i;
      factor[++factor[0]] = sum / i;
    }
  }

  sort(factor + 1, factor + 1 + factor[0]);

  int ans;
  for (int i = factor[0]; i >= 1; i--) {
    if (good(factor[i])) {
      ans = factor[i];
      break;
    }
  }

  cout << ans << endl;
  return 0;
}


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

using namespace std;

int n, k;
int a[505];
int factor[100000];
int need[100000];
int sum = 0;

bool good(int x) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int t = a[i] % x;
    if (t)
      need[++cnt] = t;
  }

  sort(need + 1, need + 1 + cnt);
  int have = k;
  int l = 1, r = cnt;
  while (l < r) {
    int t = min(need[l], x - need[r]);
    if (t > have)
      return false;
    have -= t;
    need[l] -= t;
    if (need[l] == 0)
      l++;
    need[r] += t;
    if (need[r] == x)
      r--;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  // freopen("in.in","r",stdin);

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

  for (int i = 1; i * i < sum; i++) {
    if (sum % i == 0) {
      factor[++factor[0]] = i;
      factor[++factor[0]] = sum / i;
    }
  }

  sort(factor + 1, factor + 1 + factor[0]);

  int ans;
  for (int i = factor[0]; i >= 1; i--) {
    if (good(factor[i])) {
      ans = factor[i];
      break;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02955 (C++)

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

#define int long long int
#define fi first
#define so second
#define ps push_back
#define ld long double
#define mod 1000000007

int check(vector<int> v, int n, int k) {
  vector<int> p = v, s = v;
  int sum = 0, i;
  for (i = 1; i < v.size(); i++) {
    p[i] += p[i - 1];
  }
  sum = 0;
  for (i = v.size() - 1; i >= 0; i--) {
    s[i] = sum + n - v[i];
    sum += n - v[i];
  }
  for (i = 0; i < v.size() - 1; i++)
    if (p[i] == s[i + 1] && p[i] <= k)
      return 1;
  return 0;
}

signed main() {
  int n, i, k;
  cin >> n >> k;
  int a[n], sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  vector<int> div;
  for (i = 1; i <= sqrt(sum); i++) {
    if (sum % i == 0) {
      div.ps(i);
      div.ps(sum / i);
    }
  }
  sort(div.begin(), div.end());
  // cout<<endl;
  vector<int> v;
  for (i = div.size() - 1; i >= 0; i--) {
    for (int j = 0; j < n; j++) {
      if (a[j] % div[i] != 0)
        v.ps(a[j] % div[i]);
    }
    sort(v.begin(), v.end());
    // cout<<"checking for "<<div[i]<<endl;
    if (check(v, div[i], k))
      break;
    v.clear();
  }
  cout << div[i];
}


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

#define int long long int
#define fi first
#define so second
#define ps push_back
#define ld long double
#define mod 1000000007

int check(vector<int> v, int n, int k) {
  vector<int> p = v, s = v;
  int sum = 0, i;
  for (i = 1; i < v.size(); i++) {
    p[i] += p[i - 1];
  }
  sum = 0;
  for (i = v.size() - 1; i >= 0; i--) {
    s[i] = sum + n - v[i];
    sum += n - v[i];
  }
  for (i = 0; i < v.size() - 1; i++)
    if (p[i] == s[i + 1] && p[i] <= k)
      return 1;
  return 0;
}

signed main() {
  int n, i, k;
  cin >> n >> k;
  int a[n], sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  vector<int> div;
  for (i = 1; i <= sqrt(sum); i++) {
    if (sum % i == 0) {
      div.ps(i);
      div.ps(sum / i);
    }
  }
  sort(div.begin(), div.end());
  // cout<<endl;
  vector<int> v;
  for (i = div.size() - 1; i >= 0; i--) {
    for (int j = 0; j < n; j++) {
      if (a[j] % div[i] != 0)
        v.ps(a[j] % div[i]);
    }
    if (v.size() == 0)
      break;
    sort(v.begin(), v.end());
    // cout<<"checking for "<<div[i]<<endl;
    if (check(v, div[i], k))
      break;
    v.clear();
  }
  cout << div[i];
}
Problem p02955 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
const int MOD = 1e18;
int mpow(int a, int b, int p = MOD) {
  a = a % p;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const int N = 1e5 + 3;
int a[555];
int n, k;
bool chk(int mid) {
  int cnt = 0;
  int an = 0;
  for (int i = 0; i < n; i++) {
    int o = a[i] / mid;
    cnt += -(a[i] - o * mid);
  }
  if (abs(cnt) % mid == 0) {
    int ff = abs(cnt) / mid;
    vector<int> ooo;
    for (int i = 0; i < n; i++) {
      ooo.pb(((a[i] / mid + 1) * mid - a[i]));
    }
    sort(all(ooo));
    for (int i = 0; i < n && ff; i++) {
      an += ooo[i];
      ff--;
    }
    if (an <= k) {
      return true;
    } else
      return false;
  } else {
    return false;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  int s = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i], s += a[i];
  int lo = 1, high = 1e18;
  int ans = -1;
  for (int i = 1; i <= s; i++) {
    if (s % i)
      continue;
    if (chk(i))
      ans = max(ans, i);
    if (chk(s / i))
      ans = max(ans, s / i);
  }
  cout << ans;
}
// I never lose. I either win or I learn


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
const int MOD = 1e18;
int mpow(int a, int b, int p = MOD) {
  a = a % p;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const int N = 1e5 + 3;
int a[555];
int n, k;
bool chk(int mid) {
  int cnt = 0;
  int an = 0;
  for (int i = 0; i < n; i++) {
    int o = a[i] / mid;
    cnt += -(a[i] - o * mid);
  }
  if (abs(cnt) % mid == 0) {
    int ff = abs(cnt) / mid;
    vector<int> ooo;
    for (int i = 0; i < n; i++) {
      ooo.pb(((a[i] / mid + 1) * mid - a[i]));
    }
    sort(all(ooo));
    for (int i = 0; i < n && ff; i++) {
      an += ooo[i];
      ff--;
    }
    if (an <= k) {
      return true;
    } else
      return false;
  } else {
    return false;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  int s = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i], s += a[i];
  int lo = 1, high = 1e18;
  int ans = -1;
  for (int i = 1; i * i <= s; i++) {
    if (s % i)
      continue;
    if (chk(i))
      ans = max(ans, i);
    if (chk(s / i))
      ans = max(ans, s / i);
  }
  cout << ans;
}
// I never lose. I either win or I learn
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll sum = 0LL;
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0LL; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  set<ll> s;
  for (ll i = 1LL; i * i <= sum; i++) {
    if (sum % i == 0LL) {
      s.insert(i);
      s.insert(sum % i);
    }
  }
  ll ans = 1LL;
  for (auto ne : s) {

    vector<ll> rem(N);
    for (ll i = 0LL; i < N; i++) {
      rem[i] = A[i] % ne;
    }

    sort(rem.begin(), rem.end());

    ll lsum = 0LL;
    ll rsum = 0LL;
    for (ll i = 0; i < N; i++) {
      rsum += ne - rem[i];
    }
    ll cnt = 1LL << 60;
    for (ll i = 0LL; i < N; i++) {
      lsum += rem[i];
      rsum -= ne - rem[i];
      if (lsum != rsum) {
        continue;
      }
      cnt = min(cnt, lsum);
    }

    if (cnt <= K) {
      ans = max(ans, ne);
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll sum = 0LL;
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0LL; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  set<ll> s;
  for (ll i = 1LL; i * i <= sum; i++) {
    if (sum % i == 0LL) {
      s.insert(i);
      s.insert(sum / i);
    }
  }
  ll ans = 1LL;
  for (auto ne : s) {

    vector<ll> rem(N);
    for (ll i = 0LL; i < N; i++) {
      rem[i] = A[i] % ne;
    }

    sort(rem.begin(), rem.end());

    ll lsum = 0LL;
    ll rsum = 0LL;
    for (ll i = 0; i < N; i++) {
      rsum += ne - rem[i];
    }
    ll cnt = 1LL << 60;
    for (ll i = 0LL; i < N; i++) {
      lsum += rem[i];
      rsum -= ne - rem[i];
      if (lsum != rsum) {
        continue;
      }
      cnt = min(cnt, lsum);
    }

    if (cnt <= K) {
      ans = max(ans, ne);
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02955 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define itn int
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) a.begin(), a.end()
#define print(A, n)                                                            \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i]; }                             \
  cout << endl;
#define pprint(A, m, n)                                                        \
  rep(j, 0, m) { print(A[j], n); }
const long mod = 1e9 + 7;
const int size = 1e5;
const int INF = 1e9;
int main() {
  int N, K;
  cin >> N >> K;
  int A[N];
  int sum = 0;
  rep(i, 0, N) {
    cin >> A[i];
    sum += A[i];
  }
  deque<int> d;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      d.push_back(i);
      d.push_back(sum / i);
    }
  }
  sort(d.begin(), d.end());
  int check, k;
  int u;
  int B[N];
  for (int i = d.size() - 1; i >= 0; i--) {
    u = d[i];
    check = 0;
    rep(j, 0, N) {
      k = A[j] % u;
      B[j] = -k;
      check += k;
    }
    if (check % u == 0) {
      rep(k, 0, N) {
        sort(B, B + N);
        int temp = accumulate(B, B + (check / u), 0);
        if (check + temp <= K) {
          cout << u << endl;
          return 0;
        }
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define itn int
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) a.begin(), a.end()
#define print(A, n)                                                            \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i]; }                             \
  cout << endl;
#define pprint(A, m, n)                                                        \
  rep(j, 0, m) { print(A[j], n); }
const long mod = 1e9 + 7;
const int size = 1e5;
const int INF = 1e9;
int main() {
  int N, K;
  cin >> N >> K;
  int A[N];
  int sum = 0;
  rep(i, 0, N) {
    cin >> A[i];
    sum += A[i];
  }
  deque<int> d;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      d.push_back(i);
      d.push_back(sum / i);
    }
  }
  sort(d.begin(), d.end());
  int check, k;
  int u;
  int B[N];
  for (int i = d.size() - 1; i >= 0; i--) {
    u = d[i];
    check = 0;
    rep(j, 0, N) {
      k = A[j] % u;
      B[j] = -k;
      check += k;
    }
    if (check % u == 0) {
      sort(B, B + N);
      int temp = accumulate(B, B + (check / u), 0);
      if (check + temp <= K) {
        cout << u << endl;
        return 0;
      }
    }
  }
}
Problem p02955 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

divs = []
maxA = sum(A)
for i in range(1, maxA + 1):
    if maxA % i == 0:
        divs.append(i)
        divs.append(maxA // i)

divs.sort(reverse=True)

for d in divs:
    rest = [a % d for a in A]
    rest.sort(reverse=True)
    restSum = sum(rest) // d
    cnt = 0
    for i in range(restSum):
        cnt += d - rest[i]
    if cnt <= K:
        print(d)
        exit()


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))

divs = []
maxA = sum(A)
for i in range(1, int(maxA**0.5) + 1):
    if maxA % i == 0:
        divs.append(i)
        divs.append(maxA // i)

divs.sort(reverse=True)

for d in divs:
    rest = [a % d for a in A]
    rest.sort(reverse=True)
    restSum = sum(rest) // d
    cnt = 0
    for i in range(restSum):
        cnt += d - rest[i]
    if cnt <= K:
        print(d)
        exit()
Problem p02955 (C++)

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

int main() {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  vector<long long> a(n);
  long long sum_a = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum_a += a[i];
  }
  for (int i = sum_a; i >= 0; i--) {
    if (sum_a % i != 0)
      continue;
    vector<long long> r(n);
    for (int j = 0; j < n; j++) {
      r[j] = a[j] % i;
    }
    sort(r.begin(), r.end());
    long long total = accumulate(r.begin(), r.end(), 0LL);
    int l = n - total / i;
    long long count = accumulate(r.begin(), r.begin() + l, 0LL);
    if (count <= k) {
      cout << i << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  vector<long long> a(n);
  long long sum_a = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum_a += a[i];
  }
  set<long long> factors;
  for (int i = 1; i * i <= sum_a; i++) {
    if (sum_a % i == 0) {
      factors.insert(i);
      factors.insert(sum_a / i);
    }
  }
  for (auto it = factors.rbegin(); it != factors.rend(); it++) {
    long long i = *it;
    if (sum_a % i != 0)
      continue;
    vector<long long> r(n);
    for (int j = 0; j < n; j++) {
      r[j] = a[j] % i;
    }
    sort(r.begin(), r.end());
    long long total = accumulate(r.begin(), r.end(), 0LL);
    int l = n - total / i;
    long long count = accumulate(r.begin(), r.begin() + l, 0LL);
    if (count <= k) {
      cout << i << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 998244353
#define M 200007
using namespace std;
struct mo {
  int x, y;
} a[M];
int n, X[M], Y[M], c[M];
long long ans, pre[507] = {1};
bool Mo(mo x, mo y) { return x.y < y.y; }
void add(int x) {
  while (x < M)
    c[x]++, x += x & -x;
}
int query(int x) {
  int res = 0;
  while (x)
    res += c[x], x -= x & -x;
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] * 2 % mod;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y), X[i] = a[i].x, Y[i] = a[i].y;
  sort(X + 1, X + n + 1), sort(Y + 1, Y + n + 1);
  for (int i = 1; i <= n; i++) {
    a[i].x = lower_bound(X + 1, X + n + 1, a[i].x) - X;
    a[i].y = lower_bound(Y + 1, Y + n + 1, a[i].y) - Y;
  }
  ans = n * (pre[n] - 1) % mod;
  for (int i = 1; i <= n; i++) {
    ans = (ans - pre[i] + 2 + mod) % mod;
    ans = (ans - pre[n - i + 1] + 2 + mod) % mod;
  }
  sort(a + 1, a + n + 1, Mo);
  for (int i = 1; i <= n; i++) {
    int p = query(a[i].x);
    ans = (ans + pre[p] - 1) % mod;
    ans = (ans + pre[i - p - 1] - 1) % mod;
    add(a[i].x);
  }
  memset(c, 0, sizeof c);
  for (int i = n; i >= 1; i--) {
    int p = query(a[i].x);
    ans = (ans + pre[p] - 1) % mod;
    ans = (ans + pre[n - i - p] - 1) % mod;
    add(a[i].x);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 998244353
#define M 200007
using namespace std;
struct mo {
  int x, y;
} a[M];
int n, X[M], Y[M], c[M];
long long ans, pre[M] = {1};
bool Mo(mo x, mo y) { return x.y < y.y; }
void add(int x) {
  while (x < M)
    c[x]++, x += x & -x;
}
int query(int x) {
  int res = 0;
  while (x)
    res += c[x], x -= x & -x;
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] * 2 % mod;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y), X[i] = a[i].x, Y[i] = a[i].y;
  sort(X + 1, X + n + 1), sort(Y + 1, Y + n + 1);
  for (int i = 1; i <= n; i++) {
    a[i].x = lower_bound(X + 1, X + n + 1, a[i].x) - X;
    a[i].y = lower_bound(Y + 1, Y + n + 1, a[i].y) - Y;
  }
  ans = n * (pre[n] - 1) % mod;
  for (int i = 1; i <= n; i++) {
    ans = (ans - pre[i] + 2 + mod) % mod;
    ans = (ans - pre[n - i + 1] + 2 + mod) % mod;
  }
  sort(a + 1, a + n + 1, Mo);
  for (int i = 1; i <= n; i++) {
    int p = query(a[i].x);
    ans = (ans + pre[p] - 1) % mod;
    ans = (ans + pre[i - p - 1] - 1) % mod;
    add(a[i].x);
  }
  memset(c, 0, sizeof c);
  for (int i = n; i >= 1; i--) {
    int p = query(a[i].x);
    ans = (ans + pre[p] - 1) % mod;
    ans = (ans + pre[n - i - p] - 1) % mod;
    add(a[i].x);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02957 (C++)

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

int main() {
  ll A, B;
  cin >> A >> B;
  ll m;
  m = max(A, B);
  for (ll k = -m; k <= m; k++) {
    ll a = abs(A - k);
    ll b = abs(B - k);
    if (a == b) {
      cout << k << endl;
      return 0;
    }
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}

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

int main() {
  ll A, B;
  cin >> A >> B;
  ll m;
  m = max(A, B);
  for (ll k = 0; k <= m; k++) {
    ll a = abs(A - k);
    ll b = abs(B - k);
    if (a == b) {
      cout << k << endl;
      return 0;
    }
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

int main() {
  ll a, b;
  cin >> a >> b;
  for (ll i = 0; i <= (a + b); i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i;
      return 0;
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

int main() {
  ll a, b;
  cin >> a >> b;
  for (ll i = 0; i <= 1000000000; i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i;
      return 0;
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  for (int i = -0; i < 100000000; i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i << endl;
    } else {
      cout << "IMPOSSIBLE" << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  int ans;
  if ((a + b) % 2 == 0) {
    ans = (a + b) / 2;
    cout << ans << endl;

  } else {
    cout << "IMPOSSIBLE" << endl;
  }
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define nl cout << '\n';
int main() {
  int a, b, c, d;
  cin >> a >> b;
  if ((a + b) % ((a + b) / 2) == 2)
    cout << (a + b) / 2;
  else
    cout << "IMPOSSIBLE";
  nl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define nl cout << '\n';
int main() {
  int a, b, c, d;
  cin >> a >> b;
  c = (a + b) / 2;
  if (abs(a - c) == abs(b - c))
    cout << c;
  else
    cout << "IMPOSSIBLE";
  nl;
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e9 + 1;
int main() {
  int a, b, c;
  cin >> a >> b;
  for (int i = -maxn; i <= maxn; i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i << endl;
      return 0;
    }
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e9 + 1;
int main() {
  int a, b, c;
  cin >> a >> b;
  for (int i = 0; i <= maxn; i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i << endl;
      return 0;
    }
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Time Limit Exceeded):

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

int main() {
  long double a, b;
  cin >> a >> b;
  for (int i = 0; i <= max(a, b); i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i << endl;
      return 0;
    }
    /* code */
  }

  cout << "IMPOSSIBLE" << endl;
  return 0;
}


Fixed submission:

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

int main() {
  int a, b;
  cin >> a >> b;
  for (int i = 0; i <= max(a, b); i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i << endl;
      return 0;
    }
    /* code */
  }

  cout << "IMPOSSIBLE" << endl;
  return 0;
}
Problem p02957 (C++)

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

// for loop
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define repe(i, a, n) for (int i = (a); i <= (n); ++i)
#define rrep(i, n, a) for (int i = (n); n > (a); --i)
#define rrepe(i, n, a) for (int i = (n); n >= (a); --i)

// abbreviati
#define PB(v) push_back(v)
#define MP make_pair
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define ENDL << "\n"

using ll = long long;
using P = pair<int, int>;
using itn = int;

// constant
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007;
const long double PI = (acos(-1));

/***Snippet***************************************************
ifelse, isprime, torad, todeg, lcm, bfs,chmin,chmax
**************************************************************/

// adjacency list
vector<int> to[100005];

// Graph
using Graph = vector<vector<int>>;

// Grid
int H, W;
vector<vector<int>> field;

// 4方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int a, b;
  cin >> a >> b;
  int i = 0;
  while ((a + b) >= i) {
    if (abs(a - i) == abs(b - i)) {
      cout << i ENDL;
      return 0;
    }
    ++i;
  }

  cout << "IMPOSSIBLE" ENDL;
  return 0;
}

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

// for loop
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define repe(i, a, n) for (int i = (a); i <= (n); ++i)
#define rrep(i, n, a) for (int i = (n); n > (a); --i)
#define rrepe(i, n, a) for (int i = (n); n >= (a); --i)

// abbreviati
#define PB(v) push_back(v)
#define MP make_pair
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define ENDL << "\n"

using ll = long long;
using P = pair<int, int>;
using itn = int;

// constant
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007;
const long double PI = (acos(-1));

/***Snippet***************************************************
ifelse, isprime, torad, todeg, lcm, bfs,chmin,chmax
**************************************************************/

// adjacency list
vector<int> to[100005];

// Graph
using Graph = vector<vector<int>>;

// Grid
int H, W;
vector<vector<int>> field;

// 4方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int a, b;
  cin >> a >> b;
  int i = 0;
  while ((a) >= i || b >= i) {
    if (abs(a - i) == abs(b - i)) {
      cout << i ENDL;
      return 0;
    }
    ++i;
  }

  cout << "IMPOSSIBLE" ENDL;
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back

#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  speed int a, b;
  cin >> a >> b;
  int mini = min(a, b);
  int maxi = max(a, b);
  if ((maxi - mini) % 2) {
    cout << "IMPOSSIBLE";
  } else {
    cout << (mini + ((maxi - mini) / 2));
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back

#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
signed main() {
  speed int a, b;
  cin >> a >> b;
  int mini = min(a, b);
  int maxi = max(a, b);
  if ((maxi - mini) % 2) {
    cout << "IMPOSSIBLE";
  } else {
    cout << (mini + ((maxi - mini) / 2));
  }
}
Problem p02957 (Python)

Buggy submission (status: Runtime Error):
A, B = map(int, input())
print((A + B) // 2 if abs(A - B) % 2 == 0 else "IMPOSSIBLE")


Fixed submission:
A, B = map(int, input().split())
print((A + B) // 2 if abs(A - B) % 2 == 0 else "IMPOSSIBLE")
Problem p02957 (Python)

Buggy submission (status: Time Limit Exceeded):
A, B = map(int, input().split())
for K in range(10**9):
    if abs(A - K) == abs(B - K):
        print(K)
    else:
        print("IMPOSSIBLE")


Fixed submission:
A, B = map(int, input().split())
if (A + B) % 2 != 0:
    print("IMPOSSIBLE")
else:
    print((A + B) // 2)
Problem p02957 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-
a, b = map(int, input().split())
if isinstance((a + b) / 2):
    print((a + b) / 2)
else:
    print("IMPOSSIBLE")


Fixed submission:
# -*- coding: utf-8 -*-
a, b = map(int, input().split())
if (a + b) % 2 == 0:
    print((a + b) // 2)
else:
    print("IMPOSSIBLE")
Problem p02957 (C++)

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

using namespace std;
int main() {
  long long a, b;
  cin >> a >> b;
  for (long long i = 0; i <= a + b; i++) {
    if (abs(a - i) == abs(b - i)) {
      cout << i << endl;
      return 0;
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}

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

using namespace std;
int main() {
  long long a, b;
  cin >> a >> b;
  if ((a + b) % 2 == 0)
    cout << (a + b) / 2;
  else
    cout << "IMPOSSIBLE";
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define DEBUG 1
#define fi first
#define se second
#define pb push_back
#define fore(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= (_b); ++i)
#define ford(i, a, b) for (int i = (a), _b = (b); i >= (_b); --i)

using namespace std;

typedef long long LL;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

const int INF = 1e9 + 3;
const int MOD = 1e9 + 7;

int main() {
  if (DEBUG) {
    freopen("CP.inp", "r", stdin);
    // freopen("CP.out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  cin >> a >> b;
  if ((a + b) & 1)
    cout << "IMPOSSIBLE\n";
  else
    cout << (a + b) / 2 << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define DEBUG 0
#define fi first
#define se second
#define pb push_back
#define fore(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= (_b); ++i)
#define ford(i, a, b) for (int i = (a), _b = (b); i >= (_b); --i)

using namespace std;

typedef long long LL;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

const int INF = 1e9 + 3;
const int MOD = 1e9 + 7;

int main() {
  if (DEBUG) {
    freopen("CP.inp", "r", stdin);
    // freopen("CP.out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  cin >> a >> b;
  if ((a + b) & 1)
    cout << "IMPOSSIBLE\n";
  else
    cout << (a + b) / 2 << '\n';
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define N_MAX (100000)
#define LL_MAX_NUM (1LL << 60)
#define INF 1e7

typedef long long ll;
typedef long long int lli;

int main() {
  int A, B;
  cin >> A >> B;
  cout << ((A + B) % 2 == 0 ? (char *)((A + B) / 2) : "IMPOSSIBLE") << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define N_MAX (100000)
#define LL_MAX_NUM (1LL << 60)
#define INF 1e7

typedef long long ll;
typedef long long int lli;

int main() {
  int A, B;
  cin >> A >> B;
  cout << ((A + B) % 2 == 0 ? to_string((A + B) / 2) : "IMPOSSIBLE") << endl;
  return 0;
}
Problem p02957 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define fori(i, a, b) for (int i = a; i <= b; i++)
#define ford(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mk make_pair
#define fs first
#define sc second

int main() {
  ll m, n;
  cin >> m >> n;
  int l = max(n, m);
  for (ll i = 0; i < l * l; i++) {
    if (abs(n - i) == abs(m - i)) {
      cout << i;
      exit(0);
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define fori(i, a, b) for (int i = a; i <= b; i++)
#define ford(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mk make_pair
#define fs first
#define sc second

int main() {
  ll m, n;
  cin >> m >> n;
  int l = max(n, m);
  for (ll i = 0; i < l; i++) {
    if (abs(n - i) == abs(m - i)) {
      cout << i;
      exit(0);
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}
Problem p02957 (C++)

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

int main() {

  int A, B;
  cin >> A >> B;

  for (int i = 1; i < 2147483647; i++) {
    if (abs(A - i) == abs(B - i)) {
      cout << i << endl;
      break;
    }
    if (i == 2147483646) {
      cout << "IMPOSSIBLE" << endl;
    }
  }
}

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

int main() {

  int A, B;
  cin >> A >> B;

  int K = (A + B);

  if (K % 2 == 0) {
    cout << K / 2 << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
}
Problem p02957 (C++)

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

int main() {

  int A, B;
  cin >> A >> B;

  for (int i = 0;; i++) {
    if ((A == 0) || (B == 0)) {
      cout << "IMPOSSIBLE" << endl;
      break;
    }
    if (abs(A - i) == abs(B - i)) {
      cout << i << endl;
      break;
    }
  }
}

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

int main() {

  int A, B;
  cin >> A >> B;

  for (int i = 0;; i++) {
    if (abs(A - B) % 2 != 0) {
      cout << "IMPOSSIBLE" << endl;
      break;
    }
    if (abs(A - i) == abs(B - i)) {
      cout << i << endl;
      break;
    }
  }
}
Problem p02957 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
void solve() {
  ll a, b, k;
  cin >> a >> b;
  k = (a + b) / 2;
  if (abs(a - k) == abs(b - k))
    cout << k;
  else
    cout << "IMPOSSIBLE";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  clock_t tStart = clock();
  ll t, k;
  // cin>>t;
  // while(t--)
  solve();
  // printf("\nTime taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
void solve() {
  ll a, b, k;
  cin >> a >> b;
  k = (a + b) / 2;
  if (abs(a - k) == abs(b - k))
    cout << k;
  else
    cout << "IMPOSSIBLE";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  clock_t tStart = clock();
  ll t, k;
  // cin>>t;
  // while(t--)
  solve();
  // printf("\nTime taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}
Problem p02957 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<long long> A(n + 1), B(n);
  for (int i = 0; i < n + 1; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    cin >> B[i];
  int count = 0, tmp = 0;
  for (int i = 0; i < n; ++i) {
    count += min(A[i], B[i]);
    B[i] -= min(A[i], B[i]);
    count += min(A[i + 1], B[i]);
    A[i + i] -= min(A[i + 1], B[i]);
  }
  cout << count << endl;
  return 0;
}

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

int main() {
  int a, b;
  cin >> a >> b;
  if ((a + b) % 2 == 0)
    cout << (a + b) / 2 << endl;
  else
    cout << "IMPOSSIBLE" << endl;
  return 0;
}
Problem p02958 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> p;
  int cnt = 0;
  rep(i, N) {
    cin >> p[i];
    if (p[i] != i + 1) {
      ++cnt;
    }
  }

  if (cnt <= 2) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> p(N);
  int cnt = 0;
  rep(i, N) {
    cin >> p[i];
    if (p[i] != i + 1) {
      ++cnt;
    }
  }

  if (cnt <= 2) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}
Problem p02958 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
cnt = 0
for a, b in p, sorted_p:
    if a != b:
        cnt += 1
if cnt > 2:
    print("NO")
else:
    print("YES")


Fixed submission:
n = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
cnt = 0
for a, b in zip(p, sorted_p):
    if a != b:
        cnt += 1
if cnt > 2:
    print("NO")
else:
    print("YES")
Problem p02958 (C++)

Buggy submission (status: Runtime Error):
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (ll i##_len = (n), i = i##_len - 1; i >= 0; --i)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define printd(val) std::cout << #val " = " << val << "\n";
#include <bits/stdc++.h>
using ll = long long;
using pii = std::pair<int, int>;
using namespace std;
template <class T> T chmin(T a, T b) {
  if (a > b) {
    a ^= b;
    b ^= a;
    a ^= b;
    return 1;
  }
  return 0;
}
template <class T> T chmax(T b, T a) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
    return 1;
  }
  return 0;
}

ll n;
vector<ll> p;
void input() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
  }
}
void src() {
  ll count = 0;
  for (int i = 0; i < n; ++i) {
    if (p[i] != i + 1)
      count++;
  }
  cout << (count == 2 or count == 0 ? "YES" : "NO");
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  //   ifstream in( argv[1] ); cin.rdbuf(in.rdbuf());
  input();
  src();
  return 0;
}

Fixed submission:
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (ll i##_len = (n), i = i##_len - 1; i >= 0; --i)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define printd(val) std::cout << #val " = " << val << "\n";
#include <bits/stdc++.h>
using ll = long long;
using pii = std::pair<int, int>;
using namespace std;
template <class T> T chmin(T a, T b) {
  if (a > b) {
    a ^= b;
    b ^= a;
    a ^= b;
    return 1;
  }
  return 0;
}
template <class T> T chmax(T b, T a) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
    return 1;
  }
  return 0;
}

ll n;
vector<ll> p;
void input() {
  cin >> n;
  p.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
  }
}
void src() {
  ll count = 0;
  for (int i = 0; i < n; ++i) {
    if (p[i] != i + 1)
      count++;
  }
  cout << (count == 2 or count == 0 ? "YES" : "NO");
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  //   ifstream in( argv[1] ); cin.rdbuf(in.rdbuf());
  input();
  src();
  return 0;
}
Problem p02958 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
p = list(map(int, input().split()))
q = sorted(p)
k = 0
for i in range(1, N + 1):
    if p[i] - q[i] != 0:
        k += 1

if k > 2:
    print("NO")
else:
    print("YES")


Fixed submission:
N = int(input())
p = list(map(int, input().split()))
q = sorted(p)
k = 0
for i in range(N):
    if p[i] - q[i] != 0:
        k += 1

if k > 2:
    print("NO")
else:
    print("YES")
Problem p02958 (C++)

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

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

  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
  }

  vector<int> a(n);
  for (int i = 1; i <= n; i++) {
    a.at(i) = i;
  }

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a.at(i) != p.at(i))
      cnt++;
  }

  if (cnt <= 2)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


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

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

  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
  }

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    a.at(i) = i + 1;
  }

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a.at(i) != p.at(i))
      cnt++;
  }

  if (cnt <= 2)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p02958 (C++)

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

using namespace std;

int main() {
  int i, j;
  int k, m;
  int N;
  cin >> N;
  vector<int> p(N);
  for (i = 0; i < N; i++)
    cin >> p[i];

  int count = 0;
  bool flag = false;

  for (i = 0; i < N; i++) {
    if (!(i == 0)) {
      if (p[i] < p[i - 1]) {
        count++;
        flag = true;
        if (count == 1) {
          k = i - 1;
        }
        if (count == 2) {
          m = i;
        }
      }
      if (count == 3) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  if (flag) {
    int tmp = p[k];
    p[k] = p[m];
    p[m] = tmp;
  }

  for (i = 0; i < N; i++) {
    if (!(i == 0)) {
      if (p[i] < p[i - 1]) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  cout << "YES" << endl;

  return 0;
}

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

using namespace std;

int main() {
  int i, j;
  int k = 0;
  int m = 0;
  int N;
  cin >> N;
  vector<int> p(N);
  for (i = 0; i < N; i++)
    cin >> p[i];

  int count = 0;
  bool flag = false;

  for (i = 0; i < N; i++) {
    if (!(i == 0)) {
      if (p[i] < p[i - 1]) {
        count++;
        flag = true;
        if (count == 1) {
          k = i - 1;
        }
        if (count == 2) {
          m = i;
        }
      }
      if (count == 3) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  if (flag) {
    int tmp = p[k];
    p[k] = p[m];
    p[m] = tmp;
  }

  for (i = 0; i < N; i++) {
    if (!(i == 0)) {
      if (p[i] < p[i - 1]) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }

  cout << "YES" << endl;

  return 0;
}
Problem p02958 (C++)

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

int main() {
  int N;
  cin >> N;
  int ans = 0, count = 0;
  int p[N], q[N], l[2], m[2];
  for (int i = 0; i < N; ++i) {
    cin >> p[i];
    q[i] = i + 1;
  }
  int j = 0;
  for (int i = 0; i < N; ++i) {
    if (p[i] != q[i]) {
      count++;
      l[j] = i + 1;
      m[j] = p[i];
      j++;
    }
  }
  if (count == 0)
    cout << "YES" << endl;
  else if (count == 2) {
    if (l[0] == m[1] && l[1] == m[0])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  int ans = 0, count = 0;
  int p[N], q[N], l[N], m[N];
  for (int i = 0; i < N; ++i) {
    cin >> p[i];
    q[i] = i + 1;
  }
  int j = 0;
  for (int i = 0; i < N; ++i) {
    if (p[i] != q[i]) {
      count++;
      l[j] = i + 1;
      m[j] = p[i];
      j++;
    }
  }
  if (count == 0)
    cout << "YES" << endl;
  else if (count == 2) {
    if (l[0] == m[1] && l[1] == m[0])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}
Problem p02958 (C++)

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

int main() {
  int n, cnt = 0;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  if (is_sorted(p.begin(), p.end())) {
    cout << "YES\n";
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; i < n; j++) {
      if (p[i] > p[j]) {
        int tmp = p[i];
        p[i] = p[j];
        p[j] = tmp;
        if (is_sorted(p.begin(), p.end())) {
          cout << "YES\n";
          return 0;
        }
        tmp = p[i];
        p[i] = p[j];
        p[j] = tmp;
      }
    }
  }
  cout << "NO\n";
  return 0;
}


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

int main() {
  int n, cnt = 0;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  if (is_sorted(p.begin(), p.end())) {
    cout << "YES\n";
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (p[i] > p[j]) {
        int tmp = p[i];
        p[i] = p[j];
        p[j] = tmp;
        if (is_sorted(p.begin(), p.end())) {
          cout << "YES\n";
          return 0;
        }
        tmp = p[i];
        p[i] = p[j];
        p[j] = tmp;
      }
    }
  }
  cout << "NO\n";
  return 0;
}
Problem p02958 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }

  int cnt = 0;
  vector<int> a(2);
  for (int i = 0; i < N - 1; i++) {
    if (p.at(i + 1) < p.at(i)) {
      a.at(cnt) = i;
      cnt++;
    }
    if (cnt > 2) {
      break;
    }
  }

  if (cnt == 0) {
    cout << "YES" << endl;
  } else if (cnt == 2) {
    bool flag = true;
    int ind1 = a.at(0);
    int ind2 = a.at(1) + 1;

    if (ind1 - 1 >= 0) {
      if (p.at(ind1 - 1) > p.at(ind2)) {
        flag = false;
      }
    }
    if (p.at(ind1 + 1) < p.at(ind2)) {
      flag = false;
    }

    if (p.at(ind2 - 1) > p.at(ind1)) {
      flag = false;
    }
    if (ind2 + 1 < N) {
      if (p.at(ind2 + 1) < p.at(ind1)) {
        flag = false;
      }
    }
    if (flag) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  } else {
    cout << "NO" << endl;
  }

  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }

  int cnt = 0;
  vector<int> a(3);
  for (int i = 0; i < N - 1; i++) {
    if (p.at(i + 1) < p.at(i)) {
      a.at(cnt) = i;
      cnt++;
    }
    if (cnt > 2) {
      break;
    }
  }

  if (cnt == 0) {
    cout << "YES" << endl;
  } else if (cnt == 2) {
    bool flag = true;
    int ind1 = a.at(0);
    int ind2 = a.at(1) + 1;

    if (ind1 - 1 >= 0) {
      if (p.at(ind1 - 1) > p.at(ind2)) {
        flag = false;
      }
    }
    if (p.at(ind1 + 1) < p.at(ind2)) {
      flag = false;
    }

    if (p.at(ind2 - 1) > p.at(ind1)) {
      flag = false;
    }
    if (ind2 + 1 < N) {
      if (p.at(ind2 + 1) < p.at(ind1)) {
        flag = false;
      }
    }
    if (flag) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  } else {
    cout << "NO" << endl;
  }

  return 0;
}