Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double EPS = 1e-10;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n + 2), b(n + 2);
  FOR(i, 1, n + 1) { cin >> a[i]; }
  bool flag = true;
  set<P> st;
  FOR(i, 1, n + 1) {
    cin >> b[i];
    if (b[i] < a[i]) {
      flag = false;
    }
    if (b[i] != a[i])
      st.insert(P(b[i], i));
  }
  if (!flag) {
    cout << -1 << endl;
    return 0;
  }
  b[0] = b[n], b[n + 1] = b[1];
  ll ans = 0;
  while (true) {
    bool flag = false;
    FOR(i, 1, n + 1) {
      if (a[i] != b[i])
        flag = true;
    }
    if (!flag)
      break;
    auto itr = st.end();
    itr--;
    st.erase(itr);
    auto [num, ind] = *itr;
    ll diff = b[ind - 1] + b[ind + 1];
    if (b[ind] <= diff) {
      cout << -1 << endl;
      return 0;
    }
    if ((b[ind] - a[ind]) % diff) {
      ans += b[ind] / diff;
      b[ind] %= diff;
      if (b[ind] < a[ind]) {
        cout << -1 << endl;
        return 0;
      }
      st.insert(P(b[ind], ind));
    } else {
      ans += (b[ind] - a[ind]) / diff;
      b[ind] = a[ind];
    }
    b[0] = b[n], b[n + 1] = b[1];
    // cout<<b<<endl;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double EPS = 1e-10;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n + 2), b(n + 2);
  FOR(i, 1, n + 1) { cin >> a[i]; }
  bool flag = true;
  set<P> st;
  FOR(i, 1, n + 1) {
    cin >> b[i];
    if (b[i] < a[i]) {
      flag = false;
    }
    if (b[i] != a[i])
      st.insert(P(b[i], i));
  }
  if (!flag) {
    cout << -1 << endl;
    return 0;
  }
  b[0] = b[n], b[n + 1] = b[1];
  ll ans = 0;
  while (!st.empty()) {
    auto itr = st.end();
    itr--;
    st.erase(itr);
    auto [num, ind] = *itr;
    ll diff = b[ind - 1] + b[ind + 1];
    if (b[ind] <= diff) {
      cout << -1 << endl;
      return 0;
    }
    if ((b[ind] - a[ind]) % diff) {
      ans += b[ind] / diff;
      b[ind] %= diff;
      if (b[ind] < a[ind]) {
        cout << -1 << endl;
        return 0;
      }
      st.insert(P(b[ind], ind));
    } else {
      ans += (b[ind] - a[ind]) / diff;
      b[ind] = a[ind];
    }
    b[0] = b[n], b[n + 1] = b[1];
    // cout<<b<<endl;
  }
  cout << ans << endl;
}