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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 2e5 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int A[N], B[N], n;
int q[60 * N], cur, sz;

inline int prv(int x) { return (x - 1 + n) % n; }

inline int nxt(int x) { return (x + 1) % n; }

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", A + i);
  for (int i = 0; i < n; i++)
    scanf("%d", B + i);
  for (int i = 0; i < n; i++) {
    if (B[i] > B[prv(i)] + B[nxt(i)] && B[i] > A[i])
      q[sz++] = i;
  }
  ll sol = 0;
  for (; cur < sz;) {
    int i = q[cur++];
    sol += (ll)(B[i] - A[i]) / (B[prv(i)] + B[nxt(i)]);
    B[i] = B[i] -
           ((B[i] - A[i]) / (B[prv(i)] + B[nxt(i)])) * (B[prv(i)] + B[nxt(i)]);

    if (B[prv(i)] > A[prv(i)] && B[prv(i)] > B[prv(prv(i))] + B[i])
      q[sz++] = prv(i);
    if (B[nxt(i)] > A[nxt(i)] && B[nxt(i)] > B[nxt(nxt(i))] + B[i])
      q[sz++] = nxt(i);
    if (B[i] > A[i] && B[i] > B[prv(i)] + B[nxt(i)])
      q[sz++] = i;
  }
  bool ans = 1;
  for (int i = 0; i < n; i++)
    ans &= (A[i] == B[i]);
  printf("%lld\n", ans ? sol : -1);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 2e5 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int A[N], B[N], n;
int q[60 * N], cur, sz;

inline int prv(int x) { return (x - 1 + n) % n; }

inline int nxt(int x) { return (x + 1) % n; }

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", A + i);
  for (int i = 0; i < n; i++)
    scanf("%d", B + i);
  for (int i = 0; i < n; i++) {
    if (B[i] > B[prv(i)] + B[nxt(i)] && B[i] > A[i])
      q[sz++] = i;
  }
  ll sol = 0;
  for (; cur < sz;) {
    int i = q[cur++];
    int potez =
        (ll)(B[i] - A[i] + B[prv(i)] + B[nxt(i)] - 1) / (B[prv(i)] + B[nxt(i)]);
    if (B[i] <= potez * (B[prv(i)] + B[nxt(i)]))
      potez--;
    sol += potez;
    B[i] = B[i] - potez * (B[prv(i)] + B[nxt(i)]);
    if (B[prv(i)] > A[prv(i)] && B[prv(i)] > B[prv(prv(i))] + B[i])
      q[sz++] = prv(i);
    if (B[nxt(i)] > A[nxt(i)] && B[nxt(i)] > B[nxt(nxt(i))] + B[i])
      q[sz++] = nxt(i);
    if (B[i] > A[i] && B[i] > B[prv(i)] + B[nxt(i)])
      q[sz++] = i;
  }
  bool ans = 1;
  for (int i = 0; i < n; i++)
    ans &= (A[i] == B[i]);
  printf("%lld\n", ans ? sol : -1);
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

long solve(int n, vector<long> &a, vector<long> &b) {
  long ret = 0;
  while (1) {
    bool f = false;
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i])
        break;
      if (i == n - 1)
        return ret;
    }
    for (int i = 0; i < n; i++) {
      long t = b[i % n] + b[(i + 2) % n];
      if (b[(i + 1) % n] > t) {
        long k = (b[(i + 1) % n] - a[(i + 1) % n]) / t;
        ret += k;
        b[(i + 1) % n] -= t * k;
        f = true;
      }
    }
    if (!f)
      return -1;
  }
}

int main() {
  int n;
  cin >> n;
  vector<long> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] < a[i]) {
      puts("-1");
      return 0;
    }
  }
  cout << solve(n, a, b) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

long solve(int n, vector<long> &a, vector<long> &b) {
  long ret = 0;
  while (1) {
    bool f = false;
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i])
        break;
      if (i == n - 1)
        return ret;
    }
    for (int i = 0; i < n; i++) {
      long t = b[i % n] + b[(i + 2) % n];
      if (b[(i + 1) % n] > t) {
        long k = (b[(i + 1) % n] - a[(i + 1) % n]) / t;
        ret += k;
        b[(i + 1) % n] -= t * k;
        if (k)
          f = true;
      }
    }
    if (!f)
      return -1;
  }
}

int main() {
  int n;
  cin >> n;
  vector<long> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] < a[i]) {
      puts("-1");
      return 0;
    }
  }
  cout << solve(n, a, b) << endl;
  return 0;
}
Problem p02941 (C++)

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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

struct N {
  int i;
  ll v;
};

bool operator<(const N &l, const N &r) { return l.v < r.v; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  priority_queue<N> q;
  for (int i = 0; i < n; i++) {
    if (b[i] > a[i])
      q.push(N{i, b[i]});
    else if (b[i] < a[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  ll ans = 0;
  while (q.size()) {
    auto now = q.top();
    q.pop();
    ll side = b[(now.i + n - 1) % n] + b[(now.i + 1) % n];
    if (side == 0 || now.v - side < a[now.i]) {
      cout << -1 << endl;
      return 0;
    }
    ll sidemax = max(b[(now.i + n - 1) % n], b[(now.i + 1) % n]);
    ll skip = (sidemax + side - 1) / side;
    b[now.i] -= skip * side;
    if (b[now.i] > a[now.i]) {
      q.push(N{now.i, b[now.i]});
    }
    ans += skip;
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

struct N {
  int i;
  ll v;
};

bool operator<(const N &l, const N &r) { return l.v < r.v; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  priority_queue<N> q;
  for (int i = 0; i < n; i++) {
    if (b[i] > a[i])
      q.push(N{i, b[i]});
    else if (b[i] < a[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  ll ans = 0;
  while (q.size()) {
    auto now = q.top();
    q.pop();
    ll side = b[(now.i + n - 1) % n] + b[(now.i + 1) % n];
    if (side == 0 || now.v - side < a[now.i]) {
      cout << -1 << endl;
      return 0;
    }
    ll skip = (now.v - side) / side;
    if (skip == 0)
      skip++;
    skip = min(skip, (now.v - a[now.i] + side - 1) / side);
    b[now.i] -= skip * side;
    if (b[now.i] > a[now.i]) {
      q.push(N{now.i, b[now.i]});
    }
    ans += skip;
  }
  cout << ans << endl;

  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>
#define st first
#define nd second

using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e5 + 10;

int a[N], b[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  a[0] = a[n];
  priority_queue<pii> pq;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] != a[i])
      pq.push({b[i], i % n});
  }
  b[0] = b[n];
  ll ans = 0;
  while (!pq.empty()) {
    pii p = pq.top();
    pq.pop();
    int x = (p.nd + 1) % n, y = (p.nd - 1 + n) % n;
    if (b[x] + b[y] >= b[p.nd])
      return !printf("-1");
    int sum = b[x] + b[y];
    int ma = max(a[p.nd], sum);
    int k = (b[p.nd] - ma + sum - 1) / sum;
    b[p.nd] -= sum * k;
    if (b[p.nd] < a[p.nd])
      return !printf("-1");
    ans += k;
    if (b[p.nd] != a[p.nd])
      pq.push({b[p.nd], p.nd});
  }
  printf("%lld", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>
#define st first
#define nd second

using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 2e5 + 10;

int a[N], b[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  a[0] = a[n];
  priority_queue<pii> pq;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] != a[i])
      pq.push({b[i], i % n});
  }
  b[0] = b[n];
  ll ans = 0;
  while (!pq.empty()) {
    pii p = pq.top();
    pq.pop();
    int x = (p.nd + 1) % n, y = (p.nd - 1 + n) % n;
    if (b[x] + b[y] >= b[p.nd])
      return !printf("-1");
    int sum = b[x] + b[y];
    int ma = max(a[p.nd], sum);
    int k = (b[p.nd] - ma + sum - 1) / sum;
    b[p.nd] -= sum * k;
    if (b[p.nd] < a[p.nd])
      return !printf("-1");
    ans += k;
    if (b[p.nd] != a[p.nd])
      pq.push({b[p.nd], p.nd});
  }
  printf("%lld", ans);
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <utility>
#define llint long long

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint a[200005], b[200005];
priority_queue<P> Q;

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

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

  for (int i = 0; i < n; i++) {
    Q.push(make_pair(b[i], i));
  }

  llint ans = 0;
  while (Q.size()) {
    llint id = Q.top().second;
    // cout << id << endl;
    // for(int i = 0; i < n; i++) cout << b[i] << " "; cout << endl;
    Q.pop();
    if (b[id] == a[id])
      continue;
    if (b[id] < a[id]) {
      cout << -1 << endl;
      return 0;
    }
    llint d = b[(id + 1) % n] + b[(id - 1 + n) % n],
          m = max(a[id], max(b[(id + 1) % n], b[(id - 1 + n) % n]));
    llint c = (b[id] - m + d - 1) / d;
    ans += c;
    b[id] -= c * d;
    Q.push(make_pair(b[id], id));
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <utility>
#define llint long long

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint a[200005], b[200005];
priority_queue<P> Q;

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

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

  for (int i = 0; i < n; i++) {
    Q.push(make_pair(b[i], i));
  }

  llint ans = 0;
  while (Q.size()) {
    llint id = Q.top().second;
    // cout << id << endl;
    // for(int i = 0; i < n; i++) cout << b[i] << " "; cout << endl;
    Q.pop();
    if (b[id] == a[id])
      continue;
    if (b[id] < a[id]) {
      cout << -1 << endl;
      return 0;
    }
    llint d = b[(id + 1) % n] + b[(id - 1 + n) % n],
          m = max(a[id], max(b[(id + 1) % n], b[(id - 1 + n) % n]));
    llint c = (b[id] - m + d - 1) / d;
    ans += c;
    b[id] -= c * d;
    Q.push(make_pair(b[id], id));

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

  cout << ans << endl;

  return 0;
}
Problem p02941 (C++)

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

using namespace std;

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll A[200005];
ll B[200005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) { cin >> B[i]; }
  priority_queue<pll, vector<pll>> q;
  REP(i, N) { q.push(pll(B[i], i)); }

  ll ans = 0;
  while (!q.empty()) {
    pll t = q.top();
    q.pop();
    if (B[t.second] <= A[t.second])
      continue;
    ans++;
    ll s = B[(t.second + 1) % N] + B[(N + t.second - 1) % N];
    B[t.second] = std::max(0LL, t.first - s);
    if (B[t.second] == 0)
      break;
    q.push(pll(B[t.second], t.second));
  }

  REP(i, N) {
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll A[200005];
ll B[200005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) { cin >> B[i]; }
  priority_queue<pll, vector<pll>> q;
  REP(i, N) { q.push(pll(B[i], i)); }

  ll ans = 0;
  while (!q.empty()) {
    pll t = q.top();
    q.pop();
    if (B[t.second] <= A[t.second])
      continue;
    ll left = B[(t.second - 1 + N) % N];
    ll right = B[(t.second + 1) % N];
    ll s = left + right;
    ll n = std::min((t.first - std::max(left, right) + s - 1) / s,
                    (t.first - A[t.second] + s - 1) / s);
    ans += n;
    B[t.second] = t.first - n * s;
    if (B[t.second] <= 0)
      break;
    q.push(pll(B[t.second], t.second));
  }

  REP(i, N) {
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <queue>

using namespace std;

int a[200000];
int b[200000];

int main() {
  int n, i;
  long long ans = 0;
  priority_queue<pair<int, int>> q;

  scanf("%d", &n);

  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n; i++)
    scanf("%d", &b[i]);

  for (i = 0; i < n; i++)
    q.push(make_pair(b[i], i));

  while (!q.empty()) {
    int x = q.top().second, l, r, c;

    q.pop();

    if (b[x] < a[x]) {
      puts("-1");
      return 0;
    } else if (b[x] == a[x]) {
      continue;
    }

    l = (x - 1 + n) % n;
    r = (x + 1) % n;

    if (b[x] <= b[l] + b[r]) {
      puts("-1");
      return 0;
    }

    c = (b[x] - a[x]) / (b[l] + b[r]);
    ans += c;
    b[x] -= (b[l] + b[r]) * c;

    q.push(make_pair(b[x], x));
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <cstdio>
#include <queue>

using namespace std;

int a[200000];
int b[200000];

int main() {
  int n, i;
  long long ans = 0;
  priority_queue<pair<int, int>> q;

  scanf("%d", &n);

  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n; i++)
    scanf("%d", &b[i]);

  for (i = 0; i < n; i++)
    q.push(make_pair(b[i], i));

  while (!q.empty()) {
    int x = q.top().second, l, r, c;

    q.pop();

    if (b[x] < a[x]) {
      puts("-1");
      return 0;
    } else if (b[x] == a[x]) {
      continue;
    }

    l = (x - 1 + n) % n;
    r = (x + 1) % n;

    if (b[x] <= b[l] + b[r]) {
      puts("-1");
      return 0;
    }

    c = (b[x] - a[x]) / (b[l] + b[r]);

    if (c == 0) {
      puts("-1");
      return 0;
    }

    ans += c;
    b[x] -= (b[l] + b[r]) * c;

    q.push(make_pair(b[x], x));
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p02941 (C++)

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

int n, a[200010], b[200010];
int norm(int x) {
  x = ((x % n) + n) % n;
  return x ? x : n;
}
int &A(int i) { return a[norm(i)]; }
int &B(int i) { return b[norm(i)]; }
bool f(int i) { return B(i) >= B(i - 1) + B(i + 1) + A(i); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]);

  priority_queue<pii> q;
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (f(i))
      q.push({B(i), i});
  while (!q.empty()) {
    int i = q.top().second;
    q.pop();
    int k = (B(i) - A(i)) / (B(i - 1) + B(i + 1));
    ans += k;
    B(i) -= k * (B(i - 1) + B(i + 1));
    if (f(i - 1))
      q.push({B(i - 1), i - 1});
    if (f(i + 1))
      q.push({B(i + 1), i + 1});
  }
  for (int i = 1; i <= n; i++)
    if (A(i) != B(i)) {
      puts("-1");
      return 0;
    }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int n, a[200010], b[200010];
int norm(int x) {
  x = ((x % n) + n) % n;
  return x ? x : n;
}
int &A(int i) { return a[norm(i)]; }
int &B(int i) { return b[norm(i)]; }
bool f(int i) { return B(i) >= B(i - 1) + B(i + 1) + A(i); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]);

  priority_queue<pii> q;
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (f(i))
      q.push({B(i), i});
  while (!q.empty()) {
    int i = q.top().second;
    q.pop();
    int k = (B(i) - A(i)) / (B(i - 1) + B(i + 1));
    if (!k) {
      puts("-1");
      return 0;
    }
    ans += k;
    B(i) -= k * (B(i - 1) + B(i + 1));
    if (f(i - 1))
      q.push({B(i - 1), i - 1});
    if (f(i + 1))
      q.push({B(i + 1), i + 1});
  }
  for (int i = 1; i <= n; i++)
    if (A(i) != B(i)) {
      puts("-1");
      return 0;
    }
  printf("%lld", ans);
  return 0;
}
Problem p02941 (C++)

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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  long long a[n], b[n];
  multiset<pair<long long, int>> memo;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    if (a[i] != b[i]) {
      memo.insert(make_pair(b[i], i));
    }
  }
  long long ans = 0LL;
  while (memo.size() > 0) {
    for (auto i = memo.end();; --i) {
      if (i == memo.end()) {
        --i;
      }
      auto p = *i;
      int l = (p.second - 1 + n) % n, r = (p.second + 1) % n;
      if (b[p.second] > b[l] + b[r]) {
        memo.erase(i);
        b[p.second] -= a[p.second];
        ans += b[p.second] / (b[l] + b[r]);
        b[p.second] %= (b[l] + b[r]);
        b[p.second] += a[p.second];
        if (a[p.second] != b[p.second]) {
          memo.insert(make_pair(b[p.second], p.second));
        }
        break;
      }
      if (i == memo.begin()) {
        cout << "-1\n";
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  long long a[n], b[n];
  multiset<pair<long long, int>> memo;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    if (a[i] != b[i]) {
      memo.insert(make_pair(b[i], i));
    }
  }
  long long ans = 0LL;
  while (memo.size() > 0) {
    auto i = memo.end();
    --i;
    auto p = *i;
    int l = (p.second - 1 + n) % n, r = (p.second + 1) % n;
    memo.erase(i);
    b[p.second] -= a[p.second];
    if (b[p.second] / (b[l] + b[r]) == 0) {
      cout << "-1\n";
      return 0;
    }
    ans += b[p.second] / (b[l] + b[r]);
    b[p.second] %= (b[l] + b[r]);
    b[p.second] += a[p.second];
    if (a[p.second] != b[p.second]) {
      memo.insert(make_pair(b[p.second], p.second));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02941 (C++)

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

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

  int n, a[100100], b[100100];
  cin >> 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 ans = 0;
  while (!s.empty()) {
    int i = s.rbegin()->second;
    s.erase(*s.rbegin());
    if (b[i] <= a[i])
      continue;

    int sum = b[(i + 1) % n] + b[(i + n - 1) % n];
    int op = (b[i] - a[i]) / sum;
    if (op) {
      ans += op;
      b[i] -= op * sum;
      s.insert({b[i], i});
    } else {
      cout << -1 << endl;
      return 0;
    }
  }

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

  cout << ans << endl;
}


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

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

  int n, a[200100], b[200100];
  cin >> 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 ans = 0;
  while (!s.empty()) {
    int i = s.rbegin()->second;
    s.erase(*s.rbegin());
    if (b[i] <= a[i])
      continue;

    int sum = b[(i + 1) % n] + b[(i + n - 1) % n];
    int op = (b[i] - a[i]) / sum;
    if (op) {
      ans += op;
      b[i] -= op * sum;
      s.insert({b[i], i});
    } else {
      cout << -1 << endl;
      return 0;
    }
  }

  for (int i = 0; i < n; i++)
    if (a[i] != b[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;
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ll long long
typedef pair<ll, ll> pll;
typedef vector<pll> vi;
#define out(x) return cout << x, 0;
#define precision(x, d) cout << fixed << setprecision(d) << x
#define coutif(a, b, c) cout << (a ? b : c)

int main() {
  ll n, m, j, k = 0, ans = 0, t = 0, x, y, i;
  cin >> n;
  ll a[n], b[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++)
    cin >> b[i];
  for (j = 0; j < 1000; j++) {
    for (i = 0; i < n; i++) {
      x = (i + n - 1) % n, y = (i + 1) % n;
      k = b[x] + b[y];
      if (k < b[i]) {
        t = (b[i] - a[i]) / k;
        b[i] -= k * t;
        ans += t;
      }
    }
  }
  for (i = 0; i < n; i++)
    if (a[i] != b[i])
      out(-1);
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ll long long
typedef pair<ll, ll> pll;
typedef vector<pll> vi;
#define out(x) return cout << x, 0;
#define precision(x, d) cout << fixed << setprecision(d) << x
#define coutif(a, b, c) cout << (a ? b : c)

int main() {
  ll n, m, j, k = 0, ans = 0, t = 0, x, y, i;
  cin >> n;
  ll a[n], b[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++)
    cin >> b[i];
  for (j = 0; j < 100; j++) {
    for (i = 0; i < n; i++) {
      x = (i + n - 1) % n, y = (i + 1) % n;
      k = b[x] + b[y];
      if (k < b[i]) {
        t = (b[i] - a[i]) / k;
        b[i] -= k * t;
        ans += t;
      }
    }
  }
  for (i = 0; i < n; i++)
    if (a[i] != b[i])
      out(-1);
  cout << ans;
}
Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1e9 + 7;
const int BASE = 27;
const int N = 100001;
const ll oo = 1e18;
const double EPS = 1e-9;

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

priority_queue<pii> q;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", b + i);
    q.push(mp(b[i], i));
  }
  ll ans = 0;
  while (q.size()) {
    pii f = q.top();
    q.pop();
    if (b[f.y] == a[f.y])
      continue;
    if (a[f.y] > b[f.y]) {
      puts("-1");
      return 0;
    }
    int nxt_idx = (f.y + 1) % n;
    int prv_idx = (f.y - 1 + n) % n;
    int rem = b[nxt_idx] + b[prv_idx];
    int g = (b[f.y] - a[f.y]) / rem;
    int newVal = f.x - g * rem;
    b[f.y] = newVal;
    ans += g;
    if (!g && newVal != a[f.y]) {
      puts("-1");
      return 0;
    }
    q.push(make_pair(newVal, f.y));
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1e9 + 7;
const int BASE = 27;
const int N = 200001;
const ll oo = 1e18;
const double EPS = 1e-9;

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

priority_queue<pii> q;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", b + i);
    q.push(mp(b[i], i));
  }
  ll ans = 0;
  while (q.size()) {
    pii f = q.top();
    q.pop();
    if (b[f.y] == a[f.y])
      continue;
    if (a[f.y] > b[f.y]) {
      puts("-1");
      return 0;
    }
    int nxt_idx = (f.y + 1) % n;
    int prv_idx = (f.y - 1 + n) % n;
    int rem = b[nxt_idx] + b[prv_idx];
    int g = (b[f.y] - a[f.y]) / rem;
    int newVal = f.x - g * rem;
    b[f.y] = newVal;
    ans += g;
    if (!g && newVal != a[f.y]) {
      puts("-1");
      return 0;
    }
    q.push(make_pair(newVal, f.y));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02941 (C++)

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

int main() {
  using namespace std;

  unsigned long N;
  cin >> N;

  vector<unsigned long> A(N), B(N);
  copy_n(istream_iterator<unsigned long>(cin), N, A.begin());
  priority_queue<pair<unsigned long, unsigned long>> pq;
  for (unsigned long i = 0; i < N; ++i) {
    cin >> B[i];
    if (B[i] != A[i])
      pq.emplace(B[i], i);
  }

  unsigned long ans{0};
  while (!pq.empty()) {
    auto t = pq.top().second;
    pq.pop();
    auto bf = (t + N - 1) % N, nx = (t + 1) % N;
    if (B[t] < B[bf] + B[nx] + A[t])
      return 0 & puts("-1");
    auto tmp = (B[t] - A[t]) / (B[bf] + B[nx]);
    if (!pq.empty())
      tmp = min(tmp,
                (B[t] - pq.top().first + B[bf] + B[nx] - 1) / (B[bf], B[nx]));
    ans += tmp;
    if ((B[t] -= tmp * (B[bf] + B[nx])) > A[t])
      pq.emplace(B[t], t);
  }

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

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

int main() {
  using namespace std;

  unsigned long N;
  cin >> N;

  vector<unsigned long> A(N), B(N);
  copy_n(istream_iterator<unsigned long>(cin), N, A.begin());
  priority_queue<pair<unsigned long, unsigned long>> pq;
  for (unsigned long i = 0; i < N; ++i) {
    cin >> B[i];
    if (B[i] != A[i])
      pq.emplace(B[i], i);
  }

  unsigned long ans{0};
  while (!pq.empty()) {
    auto t = pq.top().second;
    pq.pop();
    auto bf = (t + N - 1) % N, nx = (t + 1) % N;
    if (B[t] < B[bf] + B[nx] + A[t])
      return 0 & puts("-1");
    auto tmp = (B[t] - A[t]) / (B[bf] + B[nx]);
    ans += tmp;
    if ((B[t] -= tmp * (B[bf] + B[nx])) > A[t])
      pq.emplace(B[t], t);
  }

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

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

using namespace std;

using ll = long long;
mt19937 mt(736);

void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n;

  cin >> n;

  vector<int> a(n), b(n);

  for (auto &it : a)
    cin >> it;

  priority_queue<pair<int, int>> pq;

  for (size_t i = 0; i < b.size(); i++) {
    cin >> b[i];

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

      return;
    }

    pq.emplace(b[i], i);
  }

  ll cnt = 0;

  while (!pq.empty()) {
    int wh = pq.top().second;

    pq.pop();

    assert(b[wh] >= a[wh]);

    if (b[wh] > a[wh]) {
      auto sub = b[wh == 0 ? n - 1 : wh - 1] + b[wh + 1 == n ? 0 : wh + 1];

      if ((b[wh] - a[wh]) % sub == 0) {
        cnt += (b[wh] - a[wh]) / sub;
        b[wh] = a[wh];
      } else {
        cnt += b[wh] / sub;
        b[wh] %= sub;
      }

      if (b[wh] < a[wh]) {
        cout << -1 << endl;

        return;
      }

      pq.emplace(b[wh], wh);
    }
  }

  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << fixed;

#ifdef LOCAL
  ifstream fin("../input.txt");

  solve(fin);

  cout << setprecision(4);
  cout << "clock: " << clock() / (double)CLOCKS_PER_SEC << endl;
#else
  solve();
#endif

  return 0;
}


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

using namespace std;

using ll = long long;
mt19937 mt(736);

void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n;

  cin >> n;

  vector<int> a(n), b(n);

  for (auto &it : a)
    cin >> it;

  priority_queue<pair<int, int>> pq;

  for (size_t i = 0; i < b.size(); i++) {
    cin >> b[i];

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

      return;
    }

    pq.emplace(b[i], i);
  }

  ll cnt = 0;

  while (!pq.empty()) {
    int wh = pq.top().second;

    pq.pop();

    assert(b[wh] >= a[wh]);

    if (b[wh] > a[wh]) {
      auto sub = b[wh == 0 ? n - 1 : wh - 1] + b[wh + 1 == n ? 0 : wh + 1];

      if (sub > b[wh]) {
        cout << -1 << endl;

        return;
      }

      if ((b[wh] - a[wh]) % sub == 0) {
        cnt += (b[wh] - a[wh]) / sub;
        b[wh] = a[wh];
      } else {
        cnt += b[wh] / sub;
        b[wh] %= sub;
      }

      if (b[wh] < a[wh]) {
        cout << -1 << endl;

        return;
      }

      pq.emplace(b[wh], wh);
    }
  }

  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << fixed;

#ifdef LOCAL
  ifstream fin("../input.txt");

  solve(fin);

  cout << setprecision(4);
  cout << "clock: " << clock() / (double)CLOCKS_PER_SEC << endl;
#else
  solve();
#endif

  return 0;
}
Problem p02941 (C++)

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

long long n;
vector<long long> a, b;
priority_queue<pair<int, int>> pq;

long long solve();

int main() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long cnt = 0;
  for (int i = 0; i < n; ++i)
    if (b[i] != a[i])
      pq.push({b[i] - a[i], i});
  while (!pq.empty()) {
    pair<int, int> now = pq.top();
    pq.pop();
    if (now.first == 0)
      break;
    ++cnt;
    b[now.second] -= b[(now.second + 1) % n];
    b[now.second] -= b[(now.second - 1 + n) % n];
    if (b[now.second] - a[now.second] < 0)
      return -1;
    if (b[now.second] == a[now.second])
      continue;
    pq.push({b[now.second] - a[now.second], now.second});
  }
  return cnt;
}


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

long long n;
vector<long long> a, b;
priority_queue<pair<int, int>> pq;

long long solve();

int main() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long cnt = 0;
  for (int i = 0; i < n; ++i)
    if (b[i] != a[i])
      pq.push({b[i] - a[i], i});
  while (!pq.empty()) {
    pair<int, int> now = pq.top();
    pq.pop();
    if (now.first == 0)
      break;
    long long mnum = b[(now.second + 1) % n] + b[(now.second - 1 + n) % n],
              nowc;
    nowc = (b[now.second] - a[now.second]) / mnum;
    cnt += nowc;
    b[now.second] -= nowc * mnum;
    if (nowc <= 0)
      return -1;
    if (b[now.second] == a[now.second])
      continue;
    pq.push({b[now.second] - a[now.second], now.second});
  }
  return cnt;
}
Problem p02941 (C++)

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

#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fr, to) for (int i = fr; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = 2e5;
int n;
num a[nsz + 5], b[nsz + 5], ans;
priority_queue<pair<num, int>> q;

int inline pre(int id) { return id != 1 ? id - 1 : n; }

int inline nxt(int id) { return id != n ? id + 1 : 1; }

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  cont(i, n) { cin >> a[i]; }
  cont(i, n) {
    cin >> b[i];
    if (a[i] != b[i]) {
      q.push(make_pair(b[i], i));
    }
  }
  for (; !q.empty();) {
    int id = q.top().second;
    num cur = q.top().first;
    q.pop();
    num del = b[pre(id)] + b[nxt(id)], cnt = (b[id] - a[id]) / del;
    ans += cnt;
    b[id] = cur - cnt * del;
    if (a[id] > b[id]) {
      ans = -1;
      break;
    } else if (a[id] != b[id]) {
      q.push(make_pair(b[id], id));
    }
  }
  cout << ans << ln;
}


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

#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fr, to) for (int i = fr; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = 2e5;
int n;
num a[nsz + 5], b[nsz + 5], ans;
priority_queue<pair<num, int>> q;

int inline pre(int id) { return id != 1 ? id - 1 : n; }

int inline nxt(int id) { return id != n ? id + 1 : 1; }

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  cont(i, n) { cin >> a[i]; }
  cont(i, n) {
    cin >> b[i];
    if (a[i] != b[i]) {
      q.push(make_pair(b[i], i));
    }
  }
  for (; !q.empty();) {
    int id = q.top().second;
    num cur = q.top().first;
    q.pop();
    num del = b[pre(id)] + b[nxt(id)], cnt = max(1LL, (b[id] - a[id]) / del);
    ans += cnt;
    b[id] = cur - cnt * del;
    if (a[id] > b[id]) {
      ans = -1;
      break;
    } else if (a[id] != b[id]) {
      q.push(make_pair(b[id], id));
    }
  }
  cout << ans << ln;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define va first
#define vb second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int n, arr[202020], brr[202020];
ll ans;

priority_queue<pii> pq;

inline int pv(int x) {
  if (x == 0)
    return n - 1;
  return --x;
}
inline int nx(int x) {
  if (x == n - 1)
    return 0;
  return ++x;
}

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

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < n; i++)
    cin >> brr[i];

  for (int i = 0; i < n; i++)
    pq.push(pii(brr[i], i));

  while (!pq.empty()) {
    pii tmp = pq.top();
    pq.pop();

    // cout << tmp.va << ' ' << tmp.vb << '\n';
    int v = tmp.va, i = tmp.vb;
    if (v < arr[i] + brr[pv(i)] + brr[nx(i)]) {
      if (brr[i] == arr[i] && brr[pv(i)] == arr[pv(i)] &&
          brr[nx(i)] == arr[nx(i)])
        continue;
      break;
    }

    ans += (ll)(brr[i] - arr[i]) / (brr[pv(i)] + brr[nx(i)]);
    brr[i] = (brr[i] - arr[i]) % (brr[pv(i)] + brr[nx(i)]) + arr[i];
    pq.push(pii(brr[i], i));
  }

  for (int i = 0; i < n; i++) {
    if (arr[i] == brr[i])
      continue;

    cout << -1;
    return 0;
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define va first
#define vb second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int n, arr[202020], brr[202020];
ll ans;

priority_queue<pii> pq;

inline int pv(int x) {
  if (x == 0)
    return n - 1;
  return --x;
}
inline int nx(int x) {
  if (x == n - 1)
    return 0;
  return ++x;
}

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

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < n; i++)
    cin >> brr[i];

  for (int i = 0; i < n; i++)
    pq.push(pii(brr[i], i));

  while (!pq.empty()) {
    pii tmp = pq.top();
    pq.pop();

    // cout << tmp.va << ' ' << tmp.vb << '\n';
    int v = tmp.va, i = tmp.vb;
    if (v < arr[i] + brr[pv(i)] + brr[nx(i)]) {
      if (brr[i] == arr[i] && brr[pv(i)] == arr[pv(i)] &&
          brr[nx(i)] == arr[nx(i)])
        continue;
      break;
    }

    ans += (ll)(brr[i] - arr[i]) / (brr[pv(i)] + brr[nx(i)]);
    brr[i] = (brr[i] - arr[i]) % (brr[pv(i)] + brr[nx(i)]) + arr[i];
    if (brr[i] < brr[nx(i)] || brr[i] < brr[pv(i)])
      pq.push(pii(brr[i], i));
  }

  for (int i = 0; i < n; i++) {
    if (arr[i] == brr[i])
      continue;

    cout << -1;
    return 0;
  }

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

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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "{" << p.first << ", " << p.second << "}";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

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

  int n;
  ll ans = 0;
  cin >> n;
  vector<int> a(n), b(n);
  priority_queue<P> que;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i])
      que.push(P(b[i], i));
  }

  while (!que.empty()) {
    int v, m, l, r, s, k;
    tie(v, m) = que.top();
    que.pop();
    l = (m - 1 + n) % n;
    r = (m + 1) % n;
    s = b[l] + b[r];
    k = (b[m] - a[m]) / s;

    if (b[m] <= s)
      continue;

    b[m] -= k * s;
    if (b[m] != a[m] && a[m] < s)
      que.push(P(b[m], m));
    ans += k;
  }

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

  cout << ans << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "{" << p.first << ", " << p.second << "}";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

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

  int n;
  ll ans = 0;
  cin >> n;
  vector<int> a(n), b(n);
  priority_queue<P> que;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] != b[i])
      que.push(P(b[i], i));
  }

  while (!que.empty()) {
    int v, m, l, r, s, k;
    tie(v, m) = que.top();
    que.pop();
    l = (m - 1 + n) % n;
    r = (m + 1) % n;
    s = b[l] + b[r];
    k = (b[m] - a[m]) / s;

    if (b[m] <= s)
      continue;

    b[m] -= k * s;
    if (b[m] > a[m] && k != 0)
      que.push(P(b[m], m));
    ans += k;
  }

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

  cout << ans << endl;

  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n), b(n);
        for
          rep(i, n) cin >> b[i];
        for
          rep(i, n) cin >> a[i];
        vector<bool> ju(n, true);
        priority_queue<pair<long long, int>> da;
        long long ans = 0;
        for
          rep(i, n) {
            da.push(make_pair(a[i], i));
            ju[i] = bool(a[i] == b[i]);
          }
        while (!da.empty()) {
          int x = da.top().second;
          da.pop();
          if (!ju[x]) {
            long long y = a[(x + 1) % n] + a[(x - 1 + n) % n];
            if ((a[x] - b[x]) % y == 0) {
              ans += (a[x] - b[x]) / y;
              a[x] = b[x];
              ju[x] = true;
            } else {
              ans += a[x] / y;
              a[x] %= y;
              if (a[x] < b[x]) {
                ans = -1;
                break;
              }
              da.push(make_pair(a[x], x));
            }
          }
        }
        cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n), b(n);
        for
          rep(i, n) cin >> b[i];
        for
          rep(i, n) cin >> a[i];
        vector<bool> ju(n, true);
        priority_queue<pair<long long, int>> da;
        long long ans = 0;
        for
          rep(i, n) {
            da.push(make_pair(a[i], i));
            ju[i] = bool(a[i] == b[i]);
          }
        while (!da.empty()) {
          int x = da.top().second;
          da.pop();
          if (!ju[x]) {
            long long y = a[(x + 1) % n] + a[(x - 1 + n) % n];
            if (a[x] <= y) {
              ans = -1;
              break;
            }
            if ((a[x] - b[x]) % y == 0) {
              ans += (a[x] - b[x]) / y;
              a[x] = b[x];
              ju[x] = true;
            } else {
              ans += a[x] / y;
              a[x] %= y;
              if (a[x] < b[x]) {
                ans = -1;
                break;
              }
              da.push(make_pair(a[x], x));
            }
          }
        }
        cout << ans << endl;
}
Problem p02941 (C++)

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

using namespace std;

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

  vector<long> a(n), b(n);
  for (auto &x : a)
    cin >> x;
  for (auto &x : b)
    cin >> x;

  long c = 0, r = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == b[i])
      c++;

  set<long> s;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i] && b[i] > b[(i - 1 + n) % n] + b[(i + 1) % n])
      s.insert(i);

  while (!s.empty()) {
    long i = *s.begin();
    s.erase(i);
    long ai = (i - 1 + n) % n, bi = i % n, ci = (i + 1) % n;

    if (a[i] == b[i])
      continue;
    if (b[bi] <= b[ai] + b[ci])
      continue;

    r += (b[i] - a[i]) / (b[ai] + b[ci]);
    b[i] = a[i] + ((b[i] - a[i]) % (b[ai] + b[ci]));

    if (b[ai] > b[bi] + b[(ai - 1 + n) % n])
      s.insert(ai);
    if (b[ci] > b[bi] + b[(ci + 1 + n) % n])
      s.insert(ci);

    if (a[i] == b[i]) {
      c++;
      continue;
    }
    if (b[bi] > b[ai] + b[ci])
      s.insert(bi);
  }

  cout << (c == n ? r : -1) << endl;
}


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

using namespace std;

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

  vector<long> a(n), b(n);
  for (auto &x : a)
    cin >> x;
  for (auto &x : b)
    cin >> x;

  long c = 0, r = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == b[i])
      c++;

  set<long> s;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i] && b[i] > b[(i - 1 + n) % n] + b[(i + 1) % n])
      s.insert(i);

  while (!s.empty()) {
    long i = *s.begin();
    s.erase(i);
    long ai = (i - 1 + n) % n, bi = i % n, ci = (i + 1) % n;

    if (a[i] == b[i])
      continue;
    if (b[bi] <= b[ai] + b[ci])
      continue;
    if ((b[i] - a[i]) / (b[ai] + b[ci]) == 0)
      continue;

    r += (b[i] - a[i]) / (b[ai] + b[ci]);
    b[i] = a[i] + ((b[i] - a[i]) % (b[ai] + b[ci]));

    if (b[ai] > b[bi] + b[(ai - 1 + n) % n])
      s.insert(ai);
    if (b[ci] > b[bi] + b[(ci + 1 + n) % n])
      s.insert(ci);

    if (a[i] == b[i]) {
      c++;
      continue;
    }
    if (b[bi] > b[ai] + b[ci])
      s.insert(bi);
  }

  cout << (c == n ? r : -1) << endl;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll a[200010], b[200010];
priority_queue<pair<ll, ll>> que;
int main() {
  ll n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    b[i] = x;
    if (a[i] != b[i]) {
      que.push(make_pair(x, i));
      cnt++;
    }
  }
  ll ans = 0;
  while (cnt != 0) {
    pair<ll, ll> kari = que.top();
    que.pop();
    ll nowval = kari.first;
    ll nowiti = kari.second;
    ll l = nowiti - 1, r = nowiti + 1;
    if (l < 0) {
      l = n - 1;
    }
    if (r == n) {
      r = 0;
    }
    ll lval = b[l], rval = b[r];
    ll herasu = (nowval - a[nowiti]) / (lval + rval);
    ans += herasu;
    nowval -= herasu * (lval + rval);
    b[nowiti] = nowval;
    if (nowval < a[nowiti]) {
      cout << "-1";
      return 0;
    }
    if (nowval == a[nowiti]) {
      cnt--;
    } else {
      que.push(make_pair(nowval, nowiti));
    }
  }
  cout << ans;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll a[200010], b[200010];
priority_queue<pair<ll, ll>> que;
int main() {
  ll n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    b[i] = x;
    if (a[i] != b[i]) {
      que.push(make_pair(x, i));
      cnt++;
    }
  }
  ll ans = 0;
  while (cnt != 0) {
    pair<ll, ll> kari = que.top();
    que.pop();
    ll nowval = kari.first;
    ll nowiti = kari.second;
    ll l = nowiti - 1, r = nowiti + 1;
    if (l < 0) {
      l = n - 1;
    }
    if (r == n) {
      r = 0;
    }
    ll lval = b[l], rval = b[r];
    ans++;
    nowval -= (lval + rval);
    ll herasu = (nowval - a[nowiti]) / (lval + rval);
    ans += herasu;
    nowval -= herasu * (lval + rval);
    b[nowiti] = nowval;
    if (nowval < a[nowiti]) {
      cout << "-1";
      return 0;
    }
    if (nowval == a[nowiti]) {
      cnt--;
    } else {
      que.push(make_pair(nowval, nowiti));
    }
  }
  cout << ans;
  // your code goes here
  return 0;
}
Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll a[200010], b[200010];
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  priority_queue<pair<ll, ll>> ch;
  for (ll i = 0; i < n; i++) {
    cin >> b[i];
    ch.push(make_pair(b[i], i));
  }
  ll ans = 0;
  while (!ch.empty()) {
    ll val = ch.top().first;
    ll i = ch.top().second;
    ch.pop();
    ll l = i - 1;
    if (l < 0) {
      l = n - 1;
    }
    ll r = i + 1;
    if (r == n) {
      r = 0;
    }
    if (val < a[i]) {
      cout << -1;
      return 0;
    }
    if (val == a[i]) {
      continue;
    }
    val = val - b[l] - b[r];
    ans++;
    ch.push(make_pair(val, i));
    b[i] = val;
  }
  cout << ans;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll a[200010], b[200010];
int main() {
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  priority_queue<pair<ll, ll>> ch;
  for (ll i = 0; i < n; i++) {
    cin >> b[i];
    ch.push(make_pair(b[i], i));
  }
  ll ans = 0;
  while (!ch.empty()) {
    ll val = ch.top().first;
    ll i = ch.top().second;
    ch.pop();
    ll l = i - 1;
    if (l < 0) {
      l = n - 1;
    }
    ll r = i + 1;
    if (r == n) {
      r = 0;
    }
    if (val < a[i]) {
      cout << -1;
      return 0;
    }
    if (val == a[i]) {
      continue;
    }
    ll m = ((val)-a[i]) / (b[l] + b[r]);
    if (m == 0) {
      cout << -1;
      return 0;
    }
    ans += m;
    val = val - m * (b[l] + b[r]);
    ch.push(make_pair(val, i));
    b[i] = val;
  }
  cout << ans;
  // your code goes here
  return 0;
}