Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << "-1" << endl;                                                        \
  return 0
#define MM << " " <<
#define Endl endl

signed main() {
  int N;
  int A[214514], B[214514];
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) { cin >> B[i]; }
  bool end[214514] = {};
  priority_queue<pair<int, int>> P;
  REP(i, N) {
    if (A[i] > B[i]) {
      GOODBYE;
    } else if (A[i] == B[i]) {
      end[i] = true;
    } else {
      P.push({B[i], i});
    }
  }
  int Ans = 0;
  while (!P.empty()) {
    pair<int, int> t = P.top();
    P.pop();
    int s = (P.top().first);
    int j = t.second;
    int K = (B[(j + 1) % N] + B[(j - 1 + N) % N]);

    int kai = (int)(1 + (t.first - s) / K);
    if (end[(j + 1) % N] && end[(j - 1 + N) % N]) {
      kai = (int)((t.first - A[j]) / K);
    }
    t.first -= K * (kai);
    if (t.first == B[j]) {
      GOODBYE;
    }
    B[j] = t.first;
    if (A[j] > B[j]) {
      GOODBYE;
    } else if (A[j] != B[j]) {
      P.push({B[j], j});
    } else {
      end[j] = true;
    }
    Ans += kai;
    // REP(i,N){cout << B[i];if(end[i]==true)cout << "x";cout << " ";}cout <<
    // endl;
  }
  cout << Ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << "-1" << endl;                                                        \
  return 0
#define MM << " " <<
#define Endl endl

signed main() {
  int N;
  int A[214514], B[214514];
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) { cin >> B[i]; }
  bool end[214514] = {};
  priority_queue<pair<int, int>> P;
  REP(i, N) {
    if (A[i] > B[i]) {
      GOODBYE;
    } else if (A[i] == B[i]) {
      end[i] = true;
    } else {
      P.push({B[i], i});
    }
  }
  int Ans = 0;
  while (!P.empty()) {
    pair<int, int> t = P.top();
    P.pop();
    int s = (P.top().first);
    int j = t.second;
    int K = (B[(j + 1) % N] + B[(j - 1 + N) % N]);

    int kai = (int)((t.first - A[j]) / K);
    t.first -= K * (kai);
    if (t.first == B[j]) {
      GOODBYE;
    }
    B[j] = t.first;
    if (A[j] > B[j]) {
      GOODBYE;
    } else if (A[j] != B[j]) {
      P.push({B[j], j});
    } else {
      end[j] = true;
    }
    Ans += kai;
    // REP(i,N){cout << B[i];if(end[i]==true)cout << "x";cout << " ";}cout <<
    // endl;
  }
  cout << Ans << endl;
  return 0;
}