Problem p02941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

struct Edge {
  int from, to;
  ll w;
  bool operator<(const Edge &rhs) const {
    return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
  }
};

int mod_pow(int x, int n, int mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = 1LL * x * x % mod;
    n >>= 1;
  }
  return res;
}

typedef vector<vector<Edge>> Graph;
template <unsigned mod> struct RollingHash {
  vector<unsigned> hashed, power;
  unsigned base = 10007;

  inline unsigned mul(unsigned a, unsigned b) const {
    return (1LL * a * b) % mod;
  }

  RollingHash(const string &s) {
    int sz = (int)s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = mul(power[i], base);
      hashed[i + 1] = mul(hashed[i], base) + s[i];
      if (hashed[i + 1] >= mod)
        hashed[i + 1] -= mod;
    }
  }

  unsigned get(int l, int r) const {
    unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
    if (ret >= mod)
      ret -= mod;
    return ret;
  }

  unsigned connect(unsigned h1, unsigned h2, ll h2len) const {
    unsigned ret = mul(h1, mod_pow(base, h2len, mod)) + h2;
    if (ret >= mod)
      ret -= mod;
    return ret;
  }

  unsigned powstr(unsigned h2, int h2len, ll ts) const {
    if (ts == 0)
      return 0;

    unsigned han = powstr(h2, h2len, ts / 2);

    unsigned ret = connect(han, han, h2len * (ts / 2));
    if (ts % 2)
      ret = connect(ret, h2, h2len);

    return ret;
  }

  /*unsigned powstr(unsigned h2, int h2len, ll ts) const {
          unsigned ret = 0;

          REP(i, ts)
                  ret = connect(ret, h2, h2len);

          return ret;
  }*/

  int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
    int len = min(r1 - l1, r2 - l2);
    int low = -1, high = len + 1;
    while (high - low > 1) {
      int mid = (low + high) / 2;
      if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
        low = mid;
      else
        high = mid;
    }
    return (low);
  }
};

using RH = RollingHash<1000000007>;

#define issel(i)                                                               \
  (b[(i + n) % n] - b[(i - 1 + n) % n] - b[(i + 1 + n) % n] >= a[(i + n) % n])

signed main(void) {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);

  REP(i, n)
  scanf("%lld", &a[i]);
  REP(i, n)
  scanf("%lld", &b[i]);

  set<int> cand;

  REP(i, n) {
    if (issel(i))
      cand.insert(i);
  }
  ll ans = 0;

  while (cand.size()) {
    int cur = *cand.begin();
    cand.erase(cand.begin());
    ans += b[cur] / (b[(cur - 1 + n) % n] + b[(cur + 1 + n) % n]);
    b[cur] %= b[(cur - 1 + n) % n] + b[(cur + 1 + n) % n];
    if (issel(cur - 1))
      cand.insert((cur - 1 + n) % n);
    if (issel(cur + 1))
      cand.insert((cur + 1 + n) % n);
  }

  REP(i, n)
  if (a[i] != b[i]) {
    cout << -1 << endl;
    return 0;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

struct Edge {
  int from, to;
  ll w;
  bool operator<(const Edge &rhs) const {
    return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
  }
};

int mod_pow(int x, int n, int mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = 1LL * x * x % mod;
    n >>= 1;
  }
  return res;
}

typedef vector<vector<Edge>> Graph;
template <unsigned mod> struct RollingHash {
  vector<unsigned> hashed, power;
  unsigned base = 10007;

  inline unsigned mul(unsigned a, unsigned b) const {
    return (1LL * a * b) % mod;
  }

  RollingHash(const string &s) {
    int sz = (int)s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = mul(power[i], base);
      hashed[i + 1] = mul(hashed[i], base) + s[i];
      if (hashed[i + 1] >= mod)
        hashed[i + 1] -= mod;
    }
  }

  unsigned get(int l, int r) const {
    unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
    if (ret >= mod)
      ret -= mod;
    return ret;
  }

  unsigned connect(unsigned h1, unsigned h2, ll h2len) const {
    unsigned ret = mul(h1, mod_pow(base, h2len, mod)) + h2;
    if (ret >= mod)
      ret -= mod;
    return ret;
  }

  unsigned powstr(unsigned h2, int h2len, ll ts) const {
    if (ts == 0)
      return 0;

    unsigned han = powstr(h2, h2len, ts / 2);

    unsigned ret = connect(han, han, h2len * (ts / 2));
    if (ts % 2)
      ret = connect(ret, h2, h2len);

    return ret;
  }

  /*unsigned powstr(unsigned h2, int h2len, ll ts) const {
          unsigned ret = 0;

          REP(i, ts)
                  ret = connect(ret, h2, h2len);

          return ret;
  }*/

  int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
    int len = min(r1 - l1, r2 - l2);
    int low = -1, high = len + 1;
    while (high - low > 1) {
      int mid = (low + high) / 2;
      if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
        low = mid;
      else
        high = mid;
    }
    return (low);
  }
};

using RH = RollingHash<1000000007>;

#define issel(i)                                                               \
  (b[(i + n) % n] - b[(i - 1 + n) % n] - b[(i + 1 + n) % n] >= a[(i + n) % n])

signed main(void) {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);

  REP(i, n)
  scanf("%lld", &a[i]);
  REP(i, n)
  scanf("%lld", &b[i]);

  set<int> cand;

  REP(i, n) {
    if (issel(i))
      cand.insert(i);
  }
  ll ans = 0;

  while (cand.size()) {
    int cur = *cand.begin();
    cand.erase(cand.begin());
    ll sum = b[(cur - 1 + n) % n] + b[(cur + 1 + n) % n];
    ll t = (b[cur] - a[cur]) / sum;
    ans += t;
    b[cur] -= t * sum;
    if (issel(cur - 1))
      cand.insert((cur - 1 + n) % n);
    if (issel(cur + 1))
      cand.insert((cur + 1 + n) % n);
  }

  REP(i, n)
  if (a[i] != b[i]) {
    cout << -1 << endl;
    return 0;
  }

  cout << ans << endl;

  return 0;
}