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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
  bool operator<(const node s) const { return x > s.x; }
} b[100010];
int a[100010], c[100010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    cin >> b[i].x;
    c[i - 1] = b[i].x;
    b[i].y = i - 1;
  }
  sort(b + 1, b + n + 1);
  long long ans = 0;
  bool fla = 1;
  while (fla) {
    fla = 0;
    for (int i = 1; i <= n; i++) {
      int pl = b[i].y;
      int L = c[(pl + 1) % n], R = c[(pl + n - 1) % n];
      if (c[pl] - a[pl] >= L + R)
        fla = 1;
      ans += (c[pl] - a[pl]) / (L + R);
      c[pl] -= (c[pl] - a[pl]) / (L + R) * (L + R);
    }
  }
  for (int i = 0; i < n; i++)
    if (a[i] != c[i])
      return puts("-1"), 0;
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
  bool operator<(const node s) const { return x > s.x; }
} b[1000010];
int a[1000010], c[1000010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    cin >> b[i].x;
    c[i - 1] = b[i].x;
    b[i].y = i - 1;
  }
  sort(b + 1, b + n + 1);
  long long ans = 0;
  bool fla = 1;
  while (fla) {
    fla = 0;
    for (int i = 1; i <= n; i++) {
      int pl = b[i].y;
      int L = c[(pl + 1) % n], R = c[(pl + n - 1) % n];
      if (c[pl] - a[pl] >= L + R)
        fla = 1;
      ans += (c[pl] - a[pl]) / (L + R);
      c[pl] -= (c[pl] - a[pl]) / (L + R) * (L + R);
    }
  }
  for (int i = 0; i < n; i++)
    if (a[i] != c[i])
      return puts("-1"), 0;
  cout << ans;
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n, raod, a[300005], b[300005];
ll kk(ll x) {
  if (x == 1)
    return b[n] + b[2];
  if (x == n)
    return b[1] + b[n - 1];
  return b[x - 1] + b[x + 1];
}
set<pair<ll, ll>> st;
set<pair<ll, ll>>::iterator it;
int main() {
  cin >> n;

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

  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    st.insert(make_pair(b[i], (ll)i));
  }

  while (st.size()) {
    it = st.end();
    it--;
    pair<ll, ll> p = (*it);
    if (p.f == a[p.s]) {
      st.erase(it);
      continue;
    }
    if (p.f < a[p.s]) {
      cout << "-1";
      return 0;
    }
    st.erase(it);
    b[p.s] -= kk(p.s);
    raod++;
    if (b[p.s] < a[p.s]) {
      cout << "-1";
      return 0;
    }
    st.insert(make_pair(b[p.s], p.s));
  }

  cout << raod << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n, raod, a[300005], b[300005];
ll kk(ll x) {
  if (x == 1)
    return b[n] + b[2];
  if (x == n)
    return b[1] + b[n - 1];
  return b[x - 1] + b[x + 1];
}
set<pair<ll, ll>> st;
set<pair<ll, ll>>::iterator it;
int main() {
  cin >> n;

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

  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    st.insert(make_pair(b[i], (ll)i));
  }

  while (st.size()) {
    it = st.end();
    it--;
    pair<ll, ll> p = (*it);
    if (p.f == a[p.s]) {
      st.erase(it);
      continue;
    }
    if (p.f < a[p.s]) {
      cout << "-1";
      return 0;
    }
    st.erase(it);
    ll ff = 0;
    if (p.s == 1)
      ff = max(b[n], b[2]);
    if (p.s == n)
      ff = max(b[n - 1], b[1]);
    ff = max(b[p.s - 1], b[p.s + 1]);
    ff = max(ff, a[p.s]);
    ll g = (b[p.s] - ff) / kk(p.s);
    g = max(g, 1LL);
    b[p.s] -= g * kk(p.s);
    raod += g;
    if (b[p.s] < a[p.s]) {
      cout << "-1";
      return 0;
    }
    st.insert(make_pair(b[p.s], p.s));
  }

  cout << raod << endl;

  return 0;
}
Problem p02941 (C++)

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

using namespace std;

int const N = 2e5 + 5;
int const MAX = 105;

int n, a[N], d[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  priority_queue<pair<int, int>> que;
  for (int i = 1; i <= n; i++) {
    que.push(make_pair(a[i], i));
  }
  a[0] = a[n];
  a[n + 1] = a[1];
  long long ans = 0;
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    int val = p.first;
    int id = p.second;
    int pre = a[id - 1];
    int nxt = a[id + 1];
    if (a[id] == d[id]) {
      continue;
    }
    if (a[id] < pre + nxt) {
      continue;
    }
    ans += (a[id] - d[id]) / (pre + nxt);
    a[id] = (a[id] - d[id]) % (pre + nxt) + d[id];

    a[0] = a[n];
    a[n + 1] = a[1];
    que.push(make_pair(a[id], id));
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] != d[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;
}

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

using namespace std;

int const N = 2e5 + 5;
int const MAX = 105;

int n, a[N], d[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  priority_queue<pair<int, int>> que;
  for (int i = 1; i <= n; i++) {
    que.push(make_pair(a[i], i));
  }
  a[0] = a[n];
  a[n + 1] = a[1];
  long long ans = 0;
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    int val = p.first;
    int id = p.second;
    int pre = a[id - 1];
    int nxt = a[id + 1];
    if (a[id] == d[id]) {
      continue;
    }
    if (a[id] - d[id] < pre + nxt) {
      continue;
    }
    ans += (a[id] - d[id]) / (pre + nxt);
    a[id] = (a[id] - d[id]) % (pre + nxt) + d[id];

    a[0] = a[n];
    a[n + 1] = a[1];
    que.push(make_pair(a[id], id));
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] != d[i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;
}
Problem p02941 (C++)

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  set<pair<int, int>> s;
  for (int i = 0; i < n; i++) {
    s.insert({b[i], i});
  }
  long long ans = 0;
  while (!s.empty()) {
    auto p = *s.rbegin();
    s.erase(p);
    int x = p.first;
    int i = p.second;
    if (a[i] > b[i]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[i] == b[i]) {
      continue;
    }
    int i0 = (i + n - 1) % n;
    int i1 = (i + 1) % n;
    int once = (b[i0] + b[i1]);
    int mx = max(b[i0], b[i1]);
    mx = max(mx, a[i]);
    int need = b[i] - mx;
    if (need < 0) {
      cout << -1 << endl;
      return 0;
    }
    int times = (need + once - 1) / once;
    ans += times;
    b[i] -= once * times;
    s.insert({b[i], i});
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  set<pair<int, int>> s;
  for (int i = 0; i < n; i++) {
    s.insert({b[i], i});
  }
  long long ans = 0;
  while (!s.empty()) {
    auto p = *s.rbegin();
    s.erase(p);
    int x = p.first;
    int i = p.second;
    if (a[i] > b[i]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[i] == b[i]) {
      continue;
    }
    int i0 = (i + n - 1) % n;
    int i1 = (i + 1) % n;
    int once = (b[i0] + b[i1]);
    int mx = max(b[i0], b[i1]);
    mx = max(mx, a[i]);
    int need = b[i] - mx;
    if (need < 0) {
      cout << -1 << endl;
      return 0;
    }
    int times = (need + once - 1) / once;
    ans += times;
    b[i] -= once * times;
    if (b[i] < a[i]) {
      cout << -1 << endl;
      return 0;
    }
    if (b[i] > a[i]) {
      s.insert({b[i], i});
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  priority_queue<pair<ll, ll>> task; // <B[i], i>;
  rep(i, N) {
    if (A[i] < B[i]) {
      task.push(make_pair(B[i], i));
    }
  }

  ll ans = 0;
  while (task.size() > 0) {
    ll id = task.top().second;
    task.pop();
    ll s = B[(id + N - 1) % N] + B[(id + 1) % N];
    if (B[id] > s) {
      ll op = (B[id] - A[id]) / s;
      ans += op;
      B[id] -= op * s;
      if (A[id] < B[id]) {
        task.push(make_pair(B[id], id));
      }
    } else {
      cout << -1 << endl;
      return 0;
    }
  }

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

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  priority_queue<pair<ll, ll>> task; // <B[i], i>;
  rep(i, N) {
    if (A[i] < B[i]) {
      task.push(make_pair(B[i], i));
    }
  }

  ll ans = 0;
  while (task.size() > 0) {
    ll id = task.top().second;
    task.pop();
    ll s = B[(id + N - 1) % N] + B[(id + 1) % N];
    if (B[id] - A[id] >= s) {
      ll op = (B[id] - A[id]) / s;
      ans += op;
      B[id] -= op * s;
      if (A[id] < B[id]) {
        task.push(make_pair(B[id], id));
      }
    } else {
      cout << -1 << endl;
      return 0;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <queue>
#include <stdio.h>
using namespace std;
long long n;
long long a[200000];
long long b[200000];
long long s, t, x, y, z;
long long ans;
priority_queue<long long> q;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    s += a[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", b + i);
    t += b[i];
    q.push(b[i] * n + i);
  }
  while (t > s) {
    if (q.empty())
      break;
    x = q.top();
    x %= n;
    if (b[x] == a[x]) {
      q.pop();
      continue;
    }
    if (b[x] - b[(x + n - 1) % n] - b[(x + 1) % n] <= 0)
      break;
    q.pop();
    y = b[(x + n - 1) % n] + b[(x + 1) % n];
    z = (b[x] - a[x]) / y;
    b[x] -= z * y;
    t -= z * y;
    ans += z;
    q.push(b[x] * n + x);
  }
  y = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i])
      y = 0;
  }
  if (y)
    printf("%lld\n", ans);
  else
    printf("-1\n");
}

Fixed submission:
#include <queue>
#include <stdio.h>
using namespace std;
long long n;
long long a[200000];
long long b[200000];
long long s, t, x, y, z;
long long ans;
priority_queue<long long> q;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    s += a[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", b + i);
    t += b[i];
    q.push(b[i] * n + i);
  }
  while (t > s) {
    if (q.empty())
      break;
    x = q.top();
    x %= n;
    if (b[x] == a[x]) {
      q.pop();
      continue;
    }
    if (b[x] - b[(x + n - 1) % n] - b[(x + 1) % n] < a[x])
      break;
    q.pop();
    y = b[(x + n - 1) % n] + b[(x + 1) % n];
    z = (b[x] - a[x]) / y;
    b[x] -= z * y;
    t -= z * y;
    ans += z;
    q.push(b[x] * n + x);
  }
  y = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i])
      y = 0;
  }
  if (y)
    printf("%lld\n", ans);
  else
    printf("-1\n");
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>
#include <set>
#include <string>

using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  set<pair<int, int>> s;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    s.insert({b[i], i});
  }

  long long result = 0;
  for (;;) {
    if (s.empty())
      break;

    int i = prev(s.end())->second;
    s.erase(prev(s.end()));

    int i1 = (i + 1) % n;
    int i2 = (i + n - 1) % n;

    int sum = b[i1] + b[i2];

    if (b[i] < a[i]) {
      cout << -1 << endl;
      return 0;
    }

    if (b[i] == a[i]) {
      continue;
    }
    if (b[i] <= sum) {
      cout << -1 << endl;
      return 0;
    }
    int t = (b[i] - a[i]) / sum;
    assert(t > 0);
    result += t;
    b[i] -= t * sum;
    s.insert({b[i], i});
  }
  cout << result << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>
#include <set>
#include <string>

using namespace std;

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  set<pair<int, int>> s;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    s.insert({b[i], i});
  }

  long long result = 0;
  for (;;) {
    if (s.empty())
      break;

    int i = prev(s.end())->second;
    s.erase(prev(s.end()));

    int i1 = (i + 1) % n;
    int i2 = (i + n - 1) % n;

    int sum = b[i1] + b[i2];

    if (b[i] < a[i]) {
      cout << -1 << endl;
      return 0;
    }

    if (b[i] == a[i]) {
      continue;
    }
    if (b[i] <= sum) {
      cout << -1 << endl;
      return 0;
    }
    int t = (b[i] - a[i]) / sum;
    if (t == 0) {
      cout << -1 << endl;
      return 0;
    }
    result += t;
    b[i] -= t * sum;
    s.insert({b[i], i});
  }
  cout << result << endl;

  return 0;
}
Problem p02941 (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;

int main() {
  int N;
  ll ans = 0;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i];
  priority_queue<vector<int>> q;
  rep(i, N) {
    cin >> B[i];
    q.push({B[i], i});
  }
  while (!q.empty()) {
    auto t = q.top();
    q.pop();
    if (t[0] <= A[t[1]])
      continue;
    int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
    int r = (t[0] - A[t[1]]) / d;
    t[0] -= d * r;
    ans += r;
    if (t[0] < A[t[1]]) {
      cout << -1 << endl;
      return 0;
    }
    if (t[0] > A[t[1]]) {
      q.push({t[0], t[1]});
    }
    B[t[1]] = t[0];
  }
  rep(i, N) {
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  /*
  9817 1108 6890 4343 8704
  0005
                      4356
            1439
                      0008
                  2896
                  1449
                     2
             329
        774
        440
        106
             221
             113
              5
         96
         86
         76
         66
         56
         46
         36
         26
         16
          6
                          1
  */
}

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

using ll = long long;

int main() {
  int N;
  ll ans = 0;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i];
  priority_queue<vector<int>> q;
  rep(i, N) {
    cin >> B[i];
    q.push({B[i], i});
  }
  while (!q.empty()) {
    auto t = q.top();
    q.pop();
    if (t[0] <= A[t[1]])
      continue;
    int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
    int r = (t[0] - A[t[1]]) / d;
    t[0] -= d * r;
    ans += r;
    if (t[0] < A[t[1]] || r == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (t[0] > A[t[1]]) {
      q.push({t[0], t[1]});
    }
    B[t[1]] = t[0];
  }
  rep(i, N) {
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  /*
  9817 1108 6890 4343 8704
  0005
                      4356
            1439
                      0008
                  2896
                  1449
                     2
             329
        774
        440
        106
             221
             113
              5
         96
         86
         76
         66
         56
         46
         36
         26
         16
          6
                          1
  */
}
Problem p02941 (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;

int main() {
  int N;
  ll ans = 0;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i];
  priority_queue<vector<int>> q;
  rep(i, N) {
    cin >> B[i];
    q.push({B[i], i});
  }
  while (!q.empty()) {
    auto t = q.top();
    q.pop();
    if (t[0] <= A[t[1]])
      continue;
    int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
    int r = (t[0] - max(A[t[1]], d) + d - 1) / d;
    t[0] -= d * r;
    ans += r;
    if (t[0] < A[t[1]]) {
      cout << -1 << endl;
      return 0;
    }
    if (t[0] > A[t[1]]) {
      q.push({t[0], t[1]});
    }
    B[t[1]] = t[0];
  }
  rep(i, N) {
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  /*
  9817 1108 6890 4343 8704
  0005
                      4356
            1439
                      0008
                  2896
                  1449
                     2
             329
        774
        440
        106
             221
             113
              5
         96
         86
         76
         66
         56
         46
         36
         26
         16
          6
                          1
  */
}

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

using ll = long long;

int main() {
  int N;
  ll ans = 0;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i];
  priority_queue<vector<int>> q;
  rep(i, N) {
    cin >> B[i];
    q.push({B[i], i});
  }
  while (!q.empty()) {
    auto t = q.top();
    q.pop();
    if (t[0] <= A[t[1]])
      continue;
    int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
    int r = (t[0] - max(A[t[1]], d) + d - 1) / d;
    t[0] -= d * r;
    ans += r;
    if (t[0] < A[t[1]] || r == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (t[0] > A[t[1]]) {
      q.push({t[0], t[1]});
    }
    B[t[1]] = t[0];
  }
  rep(i, N) {
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  /*
  9817 1108 6890 4343 8704
  0005
                      4356
            1439
                      0008
                  2896
                  1449
                     2
             329
        774
        440
        106
             221
             113
              5
         96
         86
         76
         66
         56
         46
         36
         26
         16
          6
                          1
  */
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ff(i, x, y) for (int i = x; i <= y; i++)
#define fs(i, x, y) for (int i = x; i >= y; i--)
#define maxn 200005
using namespace std;
typedef long long ll;
typedef struct node {
  int id, val;
  node(int a1, int a2) {
    id = a1;
    val = a2;
  }
};
bool operator<(const node &a1, const node &a2) {
  if (a1.val <= a2.val)
    return true;
  return false;
}
int a[maxn], b[maxn], n;
priority_queue<node> p;
int main() {
  scanf("%d", &n);
  ff(i, 1, n) scanf("%d", &a[i - 1]);
  ff(i, 1, n) scanf("%d", &b[i - 1]);
  ff(i, 1, n) {
    if (a[i - 1] < b[i - 1])
      p.push(node(i - 1, b[i - 1]));
  }
  int flag = 1;
  ll ans = 0;
  while (!p.empty()) {
    node temp = p.top();
    p.pop();
    int pos = temp.id, v = temp.val;
    // printf("%d\n", v);
    int mod;
    if (pos == 0)
      mod = b[n - 1] + b[1];
    else if (pos == n - 1)
      mod = b[0] + b[n - 2];
    else
      mod = b[pos - 1] + b[pos + 1];
    if (v <= mod) {
      flag = 0;
      break;
    }
    v = (b[pos] - a[pos]) % mod;
    ans += (b[pos] - a[pos]) / mod;
    b[pos] = a[pos] + v;
    if (v == 0)
      ;
    else
      p.push(node(pos, b[pos]));
  }
  while (!p.empty()) {
    p.pop();
  }
  if (flag)
    printf("%lld", ans);
  else
    printf("-1");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ff(i, x, y) for (int i = x; i <= y; i++)
#define fs(i, x, y) for (int i = x; i >= y; i--)
#define maxn 200005
using namespace std;
typedef long long ll;
typedef struct node {
  int id, val;
  node(int a1, int a2) {
    id = a1;
    val = a2;
  }
};
bool operator<(const node &a1, const node &a2) {
  if (a1.val <= a2.val)
    return true;
  return false;
}
int a[maxn], b[maxn], n;
priority_queue<node> p;
int main() {
  scanf("%d", &n);
  ff(i, 1, n) scanf("%d", &a[i - 1]);
  ff(i, 1, n) scanf("%d", &b[i - 1]);
  ff(i, 1, n) {
    if (a[i - 1] < b[i - 1])
      p.push(node(i - 1, b[i - 1]));
    else if (a[i - 1] > b[i - 1]) {
      printf("-1");
      return 0;
    }
  }
  int flag = 1;
  ll ans = 0;
  while (!p.empty()) {
    node temp = p.top();
    p.pop();
    int pos = temp.id, v = temp.val;
    // printf("%d\n", v);
    int mod;
    if (pos == 0)
      mod = b[n - 1] + b[1];
    else if (pos == n - 1)
      mod = b[0] + b[n - 2];
    else
      mod = b[pos - 1] + b[pos + 1];
    if (v <= mod) {
      flag = 0;
      break;
    }
    v = (b[pos] - a[pos]) % mod;
    ans += (b[pos] - a[pos]) / mod;
    b[pos] = a[pos] + v;
    if (v == 0)
      ;
    else
      p.push(node(pos, b[pos]));
  }
  while (!p.empty()) {
    p.pop();
  }
  if (flag)
    printf("%lld", ans);
  else
    printf("-1");
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
typedef long long ll;
const int N = 5e5 + 5, M = 5e7 + 5;
int a[N], b[N], las[N], nxt[N], n;
ll res;
inline bool ck(R int x) { return b[x] > a[x] && b[x] > b[las[x]] + b[nxt[x]]; }
int q[M];
int main() {
  scanf("%d", &n);
  fp(i, 1, n) las[i] = i - 1, nxt[i] = i + 1;
  las[1] = n, nxt[n] = 1;
  fp(i, 1, n) scanf("%d", &a[i]);
  fp(i, 1, n) scanf("%d", &b[i]);
  int h = 1, t = 0, u, k;
  fp(i, 1, n) if (ck(i)) q[++t] = i;
  while (h <= t) {
    u = q[h++];
    k = (b[u] - a[u]) / (b[las[u]] + b[nxt[u]]);
    res += k, b[u] -= k * (b[las[u]] + b[nxt[u]]);
    //		printf("%d %d\n",u,b[u]);
    if (ck(las[u]))
      q[++t] = las[u];
    if (ck(nxt[u]))
      q[++t] = nxt[u];
    if (ck(u))
      q[++t] = u;
  }
  fp(i, 1, n) if (a[i] != b[i]) return puts("-1"), 0;
  return printf("%lld\n", res), 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
typedef long long ll;
const int N = 5e5 + 5, M = 5e7 + 5;
int a[N], b[N], las[N], nxt[N], n;
ll res;
inline bool ck(R int x) { return (b[x] - a[x]) >= b[las[x]] + b[nxt[x]]; }
int q[M];
int main() {
  scanf("%d", &n);
  fp(i, 1, n) las[i] = i - 1, nxt[i] = i + 1;
  las[1] = n, nxt[n] = 1;
  fp(i, 1, n) scanf("%d", &a[i]);
  fp(i, 1, n) scanf("%d", &b[i]);
  int h = 1, t = 0, u, k;
  fp(i, 1, n) if (ck(i)) q[++t] = i;
  while (h <= t) {
    u = q[h++];
    k = (b[u] - a[u]) / (b[las[u]] + b[nxt[u]]);
    res += k, b[u] -= k * (b[las[u]] + b[nxt[u]]);
    //		printf("%d %d\n",u,b[u]);
    if (ck(las[u]))
      q[++t] = las[u];
    if (ck(nxt[u]))
      q[++t] = nxt[u];
    if (ck(u))
      q[++t] = u;
  }
  fp(i, 1, n) if (a[i] != b[i]) return puts("-1"), 0;
  return printf("%lld\n", res), 0;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
const int NMAX = 1e5 + 5;

int n, a[NMAX], b[NMAX];
ll ans = 0;

pair<int, int> p;
bool possible = true;
priority_queue<pair<int, int>> valeurs;

int main() {
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  a[0] = a[n];
  a[n + 1] = a[1];

  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (b[i] != a[i])
      valeurs.push(make_pair(b[i], i));
  }

  b[0] = b[n];
  b[n + 1] = b[1];

  while (!valeurs.empty() && possible) {

    p = valeurs.top();
    valeurs.pop();

    int id = p.second;
    // cout << (int)valeurs.size() << '\n';
    // cout << id << ' ' << b[id] << ' ';
    if (b[id] - (b[id - 1] + b[id + 1]) < a[id])
      possible = false;
    else {

      int rep = (b[id] - a[id]) / (b[id - 1] + b[id + 1]);

      ans += rep;
      b[id] -= (b[id - 1] + b[id + 1]) * rep;

      if (id == n)
        b[0] = b[id];
      if (id == 1)
        b[n + 1] = b[id];

      if (b[id] != a[id])
        valeurs.push(make_pair(b[id], id));
    }
    // cout << b[id] << ' ' << a[id] << '\n';
  }
  /*for(int i=1;i<=n;i++) cout << b[i] << ' ';
  cout << '\n';*/

  if (possible)
    cout << ans << '\n';
  else
    cout << -1 << '\n';

  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
const int NMAX = 2 * 1e5 + 5;

int n, a[NMAX], b[NMAX];
ll ans = 0;

pair<int, int> p;
bool possible = true;
priority_queue<pair<int, int>> valeurs;

int main() {
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  a[0] = a[n];
  a[n + 1] = a[1];

  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (b[i] != a[i])
      valeurs.push(make_pair(b[i], i));
  }

  b[0] = b[n];
  b[n + 1] = b[1];

  while (!valeurs.empty() && possible) {

    p = valeurs.top();
    valeurs.pop();

    int id = p.second;
    // cout << (int)valeurs.size() << '\n';
    // cout << id << ' ' << b[id] << ' ';
    if (b[id] - (b[id - 1] + b[id + 1]) < a[id])
      possible = false;
    else {

      int rep = (b[id] - a[id]) / (b[id - 1] + b[id + 1]);

      ans += rep;
      b[id] -= (b[id - 1] + b[id + 1]) * rep;

      if (id == n)
        b[0] = b[id];
      if (id == 1)
        b[n + 1] = b[id];

      if (b[id] != a[id])
        valeurs.push(make_pair(b[id], id));
    }
    // cout << b[id] << ' ' << a[id] << '\n';
  }
  /*for(int i=1;i<=n;i++) cout << b[i] << ' ';
  cout << '\n';*/

  if (possible)
    cout << ans << '\n';
  else
    cout << -1 << '\n';

  return 0;
}
Problem p02942 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
const int N = 101;
int a[N][N];
int r[N];    // r[x] = row of element x
int p[N][N]; // p[x][y] = row of element y in column x

int next(int row) {
  int col = 0;
  while (a[row][col] != -1)
    ++col;
  return col;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      int x;
      scanf("%d", &x);
      r[x - 1] = i;
      a[i][j] = -1;
    }

  for (int i = 0; i < n * m; ++i) {
    int row = r[i];
    int col1 = i % m;
    int col2 = next(row);
    int val = i / m;
    if (col1 == col2) {
      a[row][col1] = val;
      p[col1][val] = row;
    } else
      while (val != -1) {
        p[col1][val] = row;
        std::swap(val, a[row][col1]);
        a[row][col2] = val;
        if (val != -1)
          std::swap(row, p[col2][val]);
      }
  }

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      int val = a[i][j] * m;
      for (int k = 0; k < m; ++k)
        if (r[val + k] == i) {
          a[i][j] = val + k;
          r[val + k] = -1;
          break;
        }
    }

  for (int i = 0; i < n; ++i, puts(""))
    for (int j = 0; j < m; ++j)
      printf("%d ", a[i][j] + 1);

  for (int i = 0; i < m; ++i)
    for (int j = 1; j < n; ++j)
      for (int k = 0; k < n - j; ++k)
        if (a[k][i] > a[k + 1][i])
          std::swap(a[k][i], a[k + 1][i]);

  for (int i = 0; i < n; ++i, puts(""))
    for (int j = 0; j < m; ++j)
      printf("%d ", a[i][j] + 1);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
const int N = 101;
int a[N][N];
int r[N * N]; // r[x] = row of element x
int p[N][N];  // p[x][y] = row of element y in column x

int next(int row) {
  int col = 0;
  while (a[row][col] != -1)
    ++col;
  return col;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      int x;
      scanf("%d", &x);
      r[x - 1] = i;
      a[i][j] = -1;
    }

  for (int i = 0; i < n * m; ++i) {
    int row = r[i];
    int col1 = i % m;
    int col2 = next(row);
    int val = i / m;
    if (col1 == col2) {
      a[row][col1] = val;
      p[col1][val] = row;
    } else
      while (val != -1) {
        p[col1][val] = row;
        std::swap(val, a[row][col1]);
        a[row][col2] = val;
        if (val != -1)
          std::swap(row, p[col2][val]);
      }
  }

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      int val = a[i][j] * m;
      for (int k = 0; k < m; ++k)
        if (r[val + k] == i) {
          a[i][j] = val + k;
          r[val + k] = -1;
          break;
        }
    }

  for (int i = 0; i < n; ++i, puts(""))
    for (int j = 0; j < m; ++j)
      printf("%d ", a[i][j] + 1);

  for (int i = 0; i < m; ++i)
    for (int j = 1; j < n; ++j)
      for (int k = 0; k < n - j; ++k)
        if (a[k][i] > a[k + 1][i])
          std::swap(a[k][i], a[k + 1][i]);

  for (int i = 0; i < n; ++i, puts(""))
    for (int j = 0; j < m; ++j)
      printf("%d ", a[i][j] + 1);
}
Problem p02942 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PP pair<int, pair<int, int>>
const int MAX = 510000;
const int MOD = 998244353;
const int INV2 = (MOD + 1) / 2;

signed main() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  pair<int, int> ichi[n * m + 1];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      ichi[a[i][j]] = make_pair(i, j);
    }
  int ten[n][m];
  for (int i = 1; i <= n * m; i++) {
    ten[(i - 1) / m][(i - 1) % m] = ichi[i].first;
    int i0 = a[ichi[i].first][(i - 1) % m], j = ichi[i].second,
        i1 = ichi[i].first;
    if (j == (i - 1) % m)
      continue;
    swap(a[ichi[i].first][(i - 1) % m], a[ichi[i].first][ichi[i].second]);
    while ((i0 - 1) / m < (i - 1) / m) {
      swap(a[ten[(i0 - 1) / m][j]][j], a[ten[(i0 - 1) / m][j]][(i - 1) % m]);
      i1 = ten[(i0 - 1) / m][j];
      i0 = a[ten[(i0 - 1) / m][j]][j];
    }
    // cerr<<i0<<' '<<i1<<endl;
    ichi[i0] = make_pair(i1, j);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cout << a[i][j] << ' ';
    cout << endl;
  }
  int b[m][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      b[j][i] = a[i][j];
  for (int i = 0; i < m; i++)
    sort(b[i], b[i] + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cout << b[j][i] << ' ';
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PP pair<int, pair<int, int>>
const int MAX = 510000;
const int MOD = 998244353;
const int INV2 = (MOD + 1) / 2;

signed main() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  pair<int, int> ichi[n * m + 1];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      ichi[a[i][j]] = make_pair(i, j);
    }
  int ten[n][m];
  for (int i = 1; i <= n * m; i++) {
    ten[(i - 1) / m][(i - 1) % m] = ichi[i].first;
    int i0 = a[ichi[i].first][(i - 1) % m], j = ichi[i].second,
        i1 = ichi[i].first;
    if (j == (i - 1) % m)
      continue;
    swap(a[ichi[i].first][(i - 1) % m], a[ichi[i].first][ichi[i].second]);
    while ((i0 - 1) / m < (i - 1) / m) {
      swap(a[ten[(i0 - 1) / m][j]][j], a[ten[(i0 - 1) / m][j]][(i - 1) % m]);
      i1 = ten[(i0 - 1) / m][j];
      swap(ten[(i0 - 1) / m][j], ten[(i0 - 1) / m][(i - 1) % m]);

      i0 = a[ten[(i0 - 1) / m][(i - 1) % m]][j];
    }
    // cerr<<i0<<' '<<i1<<endl;
    ichi[i0] = make_pair(i1, j);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cout << a[i][j] << ' ';
    cout << endl;
  }
  int b[m][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      b[j][i] = a[i][j];
  for (int i = 0; i < m; i++)
    sort(b[i], b[i] + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cout << b[j][i] << ' ';
    cout << endl;
  }
}
Problem p02943 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int n, k, st = 0, ed;
  cin >> n >> k >> s;
  if (k > 30 || (1 << (k - 1)) >= n) {
    cout << string(n, *min_element(s.begin(), s.end())) << "\n";
    return 0;
  }
  string S = s;
  reverse(S.begin(), S.end());
  string T = s + S;
  for (int i = 1; i < n; i++)
    if (T.substr(i, n) < T.substr(st, n))
      st = i;
  for (ed = st; ed - st + 1 < n && T[st] == T[ed]; ed++)
    ;
  for (int i = 1; i <= ((ed - st) << (k - 1)); i++)
    cout << T[st], n--;
  while (n--)
    cout << T[ed++];
  cout << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int n, k, st = 0, ed;
  cin >> n >> k >> s;
  if (k > 30 || (1 << (k - 1)) >= n) {
    cout << string(n, *min_element(s.begin(), s.end())) << "\n";
    return 0;
  }
  string S = s;
  reverse(S.begin(), S.end());
  string T = s + S;
  for (int i = 1; i < n; i++)
    if (T.substr(i, n) < T.substr(st, n))
      st = i;
  for (ed = st; ed - st + 1 < n && T[st] == T[ed]; ed++)
    ;
  for (int i = 1; i <= ((ed - st) << (k - 1)) && n; i++)
    cout << T[st], n--;
  while (n--)
    cout << T[ed++];
  cout << "\n";
  return 0;
}
Problem p02943 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
string s, ss, s2, t, ans, ff;
vector<pii> v;
int n, k, i, z, l;
char mn = 127;
int main() {
  freopen("1", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k >> s;
  ss = s;
  reverse(ss.begin(), ss.end());
  ss = s + ss;
  for (char c : s)
    mn = min(mn, c);
  for (i = 0; i < n; ++i)
    if (s[i] == mn)
      v.push_back(pii(++z, i));
    else
      z = 0;
  sort(v.begin(), v.end());
  if (k > 15 || (v.back().first << k - 1) >= n) {
    for (i = 0; i < n; ++i)
      cout << mn;
    return 0;
  }
  if (z) {
    if ((z << k) >= n) {
      for (i = 0; i < n; ++i)
        cout << mn;
      return 0;
    }
    l = n - (z << k);
    ff = string(n - l, mn) + ss.substr(n + z, l);
  } else
    ff = s;
  l = n - (v.back().first << k - 1);
  for (pii u : v)
    if (u.first == v.back().first) {
      t = ss.substr(n * 2 - 1 - u.second - l, l);
      reverse(t.begin(), t.end());
      if (s2.empty() || t < s2)
        s2 = t;
    }
  ans = min(string(n - l, mn) + s2, min(s, ff));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
string s, ss, s2, t, ans, ff;
vector<pii> v;
int n, k, i, z, l;
char mn = 127;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k >> s;
  ss = s;
  reverse(ss.begin(), ss.end());
  ss = s + ss;
  for (char c : s)
    mn = min(mn, c);
  for (i = 0; i < n; ++i)
    if (s[i] == mn)
      v.push_back(pii(++z, i));
    else
      z = 0;
  sort(v.begin(), v.end());
  if (k > 15 || (v.back().first << k - 1) >= n) {
    for (i = 0; i < n; ++i)
      cout << mn;
    return 0;
  }
  if (z) {
    if ((z << k) >= n) {
      for (i = 0; i < n; ++i)
        cout << mn;
      return 0;
    }
    l = n - (z << k);
    ff = string(n - l, mn) + ss.substr(n + z, l);
  } else
    ff = s;
  l = n - (v.back().first << k - 1);
  for (pii u : v)
    if (u.first == v.back().first) {
      t = ss.substr(n * 2 - 1 - u.second - l, l);
      reverse(t.begin(), t.end());
      if (s2.empty() || t < s2)
        s2 = t;
    }
  ans = min(string(n - l, mn) + s2, min(s, ff));
  cout << ans << endl;
  return 0;
}
Problem p02943 (C++)

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

using namespace std;

char S[5555];
int n, m, k;

int main() {
  int i, j;

  scanf("%d%d%s", &n, &k, S);

  for (i = 0; i < n; i++) {
    S[n + i] = S[n - i - 1];
  }

  for (i = 0, m = 0; i <= n; i++) {
    for (j = 0; j < n; j++) {
      if (S[i + j] != S[m + j]) {
        if (S[i + j] < S[m + j])
          m = i;
        break;
      }
    }
  }

  for (i = 0; S[m + i] == S[m] && i < n; i++)
    ;
  m += i;

  for (; --k && i < n; i <<= 1)
    ;
  i = min(n, i);

  S[m + n - i] = '\0';

  for (; i--;)
    printf("%c", S[m - 1]);
  printf("%s\n", S + m);

  return 0;
}

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

using namespace std;

char S[10101];
int n, m, k;

int main() {
  int i, j;

  scanf("%d%d%s", &n, &k, S);

  for (i = 0; i < n; i++) {
    S[n + i] = S[n - i - 1];
  }

  for (i = 0, m = 0; i <= n; i++) {
    for (j = 0; j < n; j++) {
      if (S[i + j] != S[m + j]) {
        if (S[i + j] < S[m + j])
          m = i;
        break;
      }
    }
  }

  for (i = 0; S[m + i] == S[m] && i < n; i++)
    ;
  m += i;

  for (; --k && i < n; i <<= 1)
    ;
  i = min(n, i);

  S[m + n - i] = '\0';

  for (; i--;)
    printf("%c", S[m - 1]);
  printf("%s\n", S + m);

  return 0;
}
Problem p02943 (C++)

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

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

  string s;
  cin >> s;
  s.resize(2 * s.size());
  for (int i = 0; i < n; i++) {
    s[2 * n - 1 - i] = s[i];
  }

  string mi, m;
  mi.resize(1e6);
  m.resize(1e6);
  for (int i = 0; i < 1e6; i++) {
    mi[i] = 'z';
    m[i] = 'z';
  }

  if (k == 1) {
    for (int i = 0; i <= n; i++) {
      mi = min(mi, s.substr(i, n));
    }

    cout << mi << endl;
  }

  else {
    reverse(s.begin(), s.end());
    for (int i = 0; i <= n; i++) {
      mi = min(mi, s.substr(i, n));
    }

    reverse(mi.begin(), mi.end());
    // cout << mi << endl;

    for (int i = k - 1; i >= max(2, i - 30); i--) {
      mi.resize(2 * n);
      for (int i = 0; i < n; i++) {
        mi[2 * n - 1 - i] = mi[i];
      }
      int pos;
      for (int j = n; j < 2 * n; j++) {
        if (mi[j] != mi[n])
          break;
        pos = j;
      }
      pos++;

      mi = mi.substr(pos - n, n);
      // cout << mi << endl;
    }

    mi.resize(2 * n);
    for (int i = 0; i < n; i++) {
      mi[2 * n - 1 - i] = mi[i];
    }

    for (int i = 0; i <= n; i++) {
      m = min(m, mi.substr(i, n));
    }

    cout << m << endl;
  }
}


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

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

  string s;
  cin >> s;
  s.resize(2 * s.size());
  for (int i = 0; i < n; i++) {
    s[2 * n - 1 - i] = s[i];
  }

  string mi, m;
  mi.resize(1e6);
  m.resize(1e6);
  for (int i = 0; i < 1e6; i++) {
    mi[i] = 'z';
    m[i] = 'z';
  }

  if (k == 1) {
    for (int i = 0; i <= n; i++) {
      mi = min(mi, s.substr(i, n));
    }

    cout << mi << endl;
  }

  else {
    reverse(s.begin(), s.end());
    for (int i = 0; i <= n; i++) {
      mi = min(mi, s.substr(i, n));
    }

    reverse(mi.begin(), mi.end());
    // cout << mi << endl;

    for (int i = k - 1; i >= max(2, k - 30); i--) {
      mi.resize(2 * n);
      for (int i = 0; i < n; i++) {
        mi[2 * n - 1 - i] = mi[i];
      }
      int pos;
      for (int j = n; j < 2 * n; j++) {
        if (mi[j] != mi[n])
          break;
        pos = j;
      }
      pos++;

      mi = mi.substr(pos - n, n);
      // cout << mi << endl;
    }

    mi.resize(2 * n);
    for (int i = 0; i < n; i++) {
      mi[2 * n - 1 - i] = mi[i];
    }

    for (int i = 0; i <= n; i++) {
      m = min(m, mi.substr(i, n));
    }

    cout << m << endl;
  }
}
Problem p02943 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
char s[maxn], res[maxn];
char c;
int n, k, len[maxn], mx;
bool check(int p) {
  for (int i = 1; i <= n; i++)
    if (s[i + p - 1] > res[i])
      return false;
    else if (s[i + p - 1] < res[i])
      return true;
  return false;
}
int main() {
  c = 'z' + 1;
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    c = min(c, s[i]), s[n * 2 - i + 1] = s[i];
  for (int i = 1; i <= n; i++)
    res[i] = 'z' + 1;
  if (k >= 15) {
    for (int i = 1; i <= n; i++)
      printf("%c", c);
    printf("\n");
    return 0;
  }
  if (k == 1) {
    for (int i = 1; i <= n + 1; i++)
      if (check(i)) {
        for (int j = 1; j <= n; j++)
          res[j] = s[i + j - 1];
      }
    printf("%s", res + 1);
    return 0;
  }
  for (int i = 1; i <= 2 * n; i++)
    len[i] = s[i] == c ? len[i - 1] + 1 : 0, mx = max(mx, len[i]);
  k--;
  if ((mx << k) >= n) {
    for (int i = 1; i <= n; i++)
      printf("%c", c);
    printf("\n");
    return 0;
  }
  int now = mx << k;
  for (int i = 1; i <= n + mx; i++)
    if (len[i] == mx && check(i + 1))
      for (int j = 1; j <= n; j++)
        res[j] = s[i + j];
  for (int i = 1; i <= now; i++)
    printf("%c", c);
  for (int i = now + 1; i <= n; i++)
    printf("%c", res[i - now]);
  printf("\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
char s[maxn], res[maxn];
char c;
int n, k, len[maxn], mx;
bool check(int p) {
  for (int i = 1; i <= n; i++)
    if (s[i + p - 1] > res[i])
      return false;
    else if (s[i + p - 1] < res[i])
      return true;
  return false;
}
int main() {
  c = 'z' + 1;
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    c = min(c, s[i]), s[n * 2 - i + 1] = s[i];
  for (int i = 1; i <= n; i++)
    res[i] = 'z' + 1;
  if (k >= 15) {
    for (int i = 1; i <= n; i++)
      printf("%c", c);
    printf("\n");
    return 0;
  }
  if (k == 1) {
    for (int i = 1; i <= n + 1; i++)
      if (check(i)) {
        for (int j = 1; j <= n; j++)
          res[j] = s[i + j - 1];
      }
    printf("%s", res + 1);
    return 0;
  }
  for (int i = 1; i <= 2 * n; i++)
    len[i] = s[i] == c ? len[i - 1] + 1 : 0, mx = max(mx, len[i]);
  k--;
  if ((mx << k) >= n) {
    for (int i = 1; i <= n; i++)
      printf("%c", c);
    printf("\n");
    return 0;
  }
  int now = mx << k;
  for (int i = 1; i <= n + mx; i++)
    if (len[i] == mx && check(i + 1))
      for (int j = 1; j <= n; j++)
        res[j] = s[i + j];
  for (int i = 1; i <= now; i++)
    printf("%c", c);
  for (int i = now + 1; i <= n; i++)
    printf("%c", res[i - now]);
  printf("\n");
}
Problem p02943 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 5005;
char s[N], t[N], ans[N], mn, mx;
int n, k;
bool cmp(char *s, char *t) {
  fp(i, 1, n) if (s[i] != t[i]) return s[i] < t[i];
  return false;
}
void solve(char *s, int k) {
  R int now = n, len = 0;
  while (now && s[now] == mn)
    --now, ++len;
  while (k && len < n)
    len <<= 1, --k;
  if (len >= n) {
    fp(i, 1, n) ans[i] = mn;
    printf("%s\n", ans + 1);
    exit(0);
  }
  fp(i, 1, len) t[i] = mn;
  fp(i, len + 1, n) t[i] = s[now--];
  if (cmp(t, ans))
    fp(i, 1, n) ans[i] = t[i];
}
int main() {
  scanf("%d%d%s", &n, &k, s + 1);
  mn = 'z', mx = 'a';
  fp(i, 1, n) cmin(mn, s[i]), cmax(mx, s[i]), ans[i] = 'z',
                                              s[(n << 1) - i + 1] = s[i];
  if (mn == mx)
    return printf("%s\n", s + 1), 0;
  if (s[n] == mn)
    solve(s, k);
  fp(i, n, n << 1) if (s[i] == mn) solve(s + i - n, k - 1);
  printf("%s\n", ans + 1);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 10005;
char s[N], t[N], ans[N], mn, mx;
int n, k;
bool cmp(char *s, char *t) {
  fp(i, 1, n) if (s[i] != t[i]) return s[i] < t[i];
  return false;
}
void solve(char *s, int k) {
  R int now = n, len = 0;
  while (now && s[now] == mn)
    --now, ++len;
  while (k && len < n)
    len <<= 1, --k;
  if (len >= n) {
    fp(i, 1, n) ans[i] = mn;
    printf("%s\n", ans + 1);
    exit(0);
  }
  fp(i, 1, len) t[i] = mn;
  fp(i, len + 1, n) t[i] = s[now--];
  if (cmp(t, ans))
    fp(i, 1, n) ans[i] = t[i];
}
int main() {
  scanf("%d%d%s", &n, &k, s + 1);
  mn = 'z', mx = 'a';
  fp(i, 1, n) cmin(mn, s[i]), cmax(mx, s[i]), ans[i] = 'z',
                                              s[(n << 1) - i + 1] = s[i];
  if (mn == mx)
    return printf("%s\n", s + 1), 0;
  if (s[n] == mn)
    solve(s, k);
  fp(i, n, n << 1) if (s[i] == mn) solve(s + i - n, k - 1);
  printf("%s\n", ans + 1);
  return 0;
}
Problem p02943 (C++)

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

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  string ans = S;

  string T(S.rbegin(), S.rend());
  string U = S + T;
  char a = *min_element(S.begin(), S.end());

  if (K == 1) {
    for (int i = 0; i <= N; i++) {
      ans = min(ans, string(U.begin() + i, U.begin() + i + N));
    }
    cout << ans << '\n';
    return 0;
  } else {
    for (int i = 0; i <= N; i++) {
      string cur = string(U.begin() + i, U.begin() + i + N);
      int cnt = 0;
      while (!cur.empty() && cur.back() == a) {
        cur.pop_back();
        cnt++;
      }
      for (int z = 0; cnt < N && z < K - 1; z++) {
        cnt = min(cnt * 2, N);
      }
      string cnd(cnt, a);
      cnd += string(cur.rbegin(), cur.rend());
      ans = min(ans, string(cnd.begin(), cnd.begin() + N));
    }
    cout << ans << '\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);
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  string ans = S;

  string T(S.rbegin(), S.rend());
  string U = S + T;
  char a = *min_element(S.begin(), S.end());

  if (K == 1) {
    for (int i = 0; i <= N; i++) {
      ans = min(ans, string(U.begin() + i, U.begin() + i + N));
    }
    cout << ans << '\n';
    return 0;
  } else {
    for (int i = 0; i <= N; i++) {
      string cur = string(U.begin() + i, U.begin() + i + N);
      int cnt = 0;
      while (!cur.empty() && cur.back() == a) {
        cur.pop_back();
        cnt++;
      }
      if (cnt == 0)
        continue;
      for (int z = 0; cnt < N && z < K - 1; z++) {
        cnt = min(cnt * 2, N);
      }
      string cnd(cnt, a);
      cnd += string(cur.rbegin(), cur.rend());
      ans = min(ans, string(cnd.begin(), cnd.begin() + N));
    }
    cout << ans << '\n';
  }

  return 0;
}