Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
typedef pair<int, int> P;
int A[200010];
int B[200010];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  priority_queue<P> pq;
  rep(i, N) {
    int L = (i + N - 1) % N;
    int M = (i + N) % N;
    int R = (i + N + 1) % N;
    pq.push({B[M] - B[L] - B[R], i});
  }
  int cnt = 0;
  while (pq.size()) {
    P num = pq.top();
    pq.pop();
    // cout << num.first << endl;
    if (num.first < 0)
      break;
    int i = num.second;
    int L = (i + N - 1) % N;
    int M = (i + N) % N;
    int R = (i + N + 1) % N;
    if (B[L] + B[R] < B[M] && B[M] > A[M]) {
      int times = (B[M] - A[M]) / (B[L] + B[R]);
      B[M] -= (B[L] + B[R]) * times;
      cnt += times;
      // rep(i, N) cout << B[i] << " ";
      // cout << endl;
      REP(j, -1, 1) {
        int i2 = num.second + j + N * 2;
        int L1 = (i2 + N - 1) % N;
        int M1 = (i2 + N) % N;
        int R1 = (i2 + N + 1) % N;
        pq.push({B[M1] - B[L1] - B[R1], M1});
      }
    }
  }
  rep(i, N) {
    if (B[i] != A[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
typedef pair<int, int> P;
int A[200010];
int B[200010];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  priority_queue<P> pq;
  rep(i, N) {
    int L = (i + N - 1) % N;
    int M = (i + N) % N;
    int R = (i + N + 1) % N;
    pq.push({B[M] - B[L] - B[R], i});
  }
  int cnt = 0;
  while (pq.size()) {
    P num = pq.top();
    pq.pop();
    // cout << num.first << endl;
    if (num.first < 0)
      break;
    int i = num.second;
    int L = (i + N - 1) % N;
    int M = (i + N) % N;
    int R = (i + N + 1) % N;
    if (B[L] + B[R] < B[M] && B[M] > A[M]) {
      int times = (B[M] - A[M]) / (B[L] + B[R]);
      if (times <= 0)
        break;
      B[M] -= (B[L] + B[R]) * times;
      cnt += times;
      // rep(i, N) cout << B[i] << " ";
      // cout << endl;
      REP(j, -1, 1) {
        int i2 = num.second + j + N * 2;
        int L1 = (i2 + N - 1) % N;
        int M1 = (i2 + N) % N;
        int R1 = (i2 + N + 1) % N;
        pq.push({B[M1] - B[L1] - B[R1], M1});
      }
    }
  }
  rep(i, N) {
    if (B[i] != A[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << cnt << endl;

  return 0;
}