Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o set
const ll MOD = 1e9 + 7;
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  priority_queue<P> pq;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    cin >> b[i];
    if (a[i] != b[i])
      pq.push({b[i], i});
  }
  ll ans = 0;
  while (!pq.empty()) {
    P d = pq.top();
    pq.pop();
    int del = b[d.se] - a[d.se];
    if (del % (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]) == 0) {
      ans += del / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
      b[d.se] = a[d.se];
      continue;
    }
    int cnt = b[d.se] / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
    b[d.se] -= cnt * (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
    if (a[d.se] < b[d.se])
      pq.push({b[d.se], d.se});
    if (a[d.se] > b[d.se]) {
      cout << -1 << endl;
      return 0;
    }
    ans += cnt;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o set
const ll MOD = 1e9 + 7;
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  priority_queue<P> pq;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    cin >> b[i];
    if (a[i] != b[i])
      pq.push({b[i], i});
  }
  ll ans = 0;
  while (!pq.empty()) {
    P d = pq.top();
    pq.pop();
    int del = b[d.se] - a[d.se];
    if (del % (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]) == 0) {
      ans += del / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
      b[d.se] = a[d.se];
      continue;
    }
    if (b[d.se] < (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n])) {
      cout << -1 << endl;
      return 0;
    }
    int cnt = b[d.se] / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
    b[d.se] -= cnt * (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
    if (a[d.se] < b[d.se])
      pq.push({b[d.se], d.se});
    if (a[d.se] > b[d.se]) {
      cout << -1 << endl;
      return 0;
    }
    ans += cnt;
  }
  cout << ans << endl;
}