Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define P pair<int, int>
#define V vector<int>
#define M map<int, int>
#define itn int
#define tin int
#define tni int
bool dbg = false;
int N, A[200010], B[200010];
int ans;
bool search() {
  bool flag = true;
  FOR(i, 0, N) {
    if (A[i] != B[i]) {
      flag = false;
    }
  }
  return flag;
}

void solve() {
  priority_queue<P> que;
  // set<P> S;
  FOR(i, 0, N) {
    if (A[i] != B[i]) {
      // S.insert(make_pair(B[i], i));
      que.push(make_pair(B[i], i));
    }
  }
  // while (!S.empty()) {
  while (!que.empty()) {
    // auto it = S.end();
    auto x = que.top();
    que.pop();
    // it--;
    // S.erase(it);
    // int t = (*it).second;
    int t = x.second;
    int &b = B[t];
    int &a = B[(t + N - 1) % N];
    int &c = B[(t + 1) % N];
    if (b > a + c) {
      int n = b - A[t];
      n = n / (a + c);
      ans += n;
      b -= n * (a + c);
      if (b != A[t]) {
        // S.insert(make_pair(B[t], t));
        que.push(make_pair(B[t], t));
      }
    } else {
      cout << -1 << endl;
      return;
    }
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> N;
  FOR(i, 0, N)
  cin >> A[i];
  FOR(i, 0, N)
  cin >> B[i];
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define P pair<int, int>
#define V vector<int>
#define M map<int, int>
#define itn int
#define tin int
#define tni int
bool dbg = false;
int N, A[200010], B[200010];
int ans;
bool search() {
  bool flag = true;
  FOR(i, 0, N) {
    if (A[i] != B[i]) {
      flag = false;
    }
  }
  return flag;
}

void solve() {
  priority_queue<P> que;
  // set<P> S;
  FOR(i, 0, N) {
    if (A[i] != B[i]) {
      // S.insert(make_pair(B[i], i));
      que.push(make_pair(B[i], i));
    }
  }
  // while (!S.empty()) {
  while (!que.empty()) {
    // auto it = S.end();
    auto x = que.top();
    que.pop();
    // it--;
    // S.erase(it);
    // int t = (*it).second;
    int t = x.second;
    int &b = B[t];
    int &a = B[(t + N - 1) % N];
    int &c = B[(t + 1) % N];
    if (b > a + c) {
      int n = b - A[t];
      n = n / (a + c);
      if (n == 0) {
        cout << -1 << endl;
        return;
      }
      ans += n;
      b -= n * (a + c);
      if (b != A[t]) {
        // S.insert(make_pair(B[t], t));
        que.push(make_pair(B[t], t));
      }
    } else {
      cout << -1 << endl;
      return;
    }
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> N;
  FOR(i, 0, N)
  cin >> A[i];
  FOR(i, 0, N)
  cin >> B[i];
  solve();
  return 0;
}