Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<long long>;
using ld = long double;

#define unordered_map __fast_unordered_map
template <class Key, class Value, class Hash = std::hash<Key>>
using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>;

template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);

  int n;
  cin >> n;

  vi a(n), b(n);
  for (int &x : a) {
    cin >> x;
  }
  for (int &x : b) {
    cin >> x;
  }

  multiset<pii> b_set;
  for (int i = 0; i < n; ++i) {
    if (b[i] > a[i]) {
      b_set.emplace(b[i], i);
    }
  }

  ll num_ops = 0;
  while (!b_set.empty()) {
    int val, loc;
    tie(val, loc) = *b_set.rbegin();
    debug(val, loc, a, b);
    b_set.erase(prev(b_set.end()));
    assert(val > a[loc]);

    const ll x = b[(loc + n - 1) % n];
    const ll y = b[(loc + 1) % n];
    const ll diff = x + y;
    if (diff > b[loc]) {
      cout << "-1\n";
      return 0;
    }

    const int bound = max<ll>({a[loc], x - diff, y - diff});
    const int max_steps = (b[loc] - bound) / diff;
    num_ops += max_steps;
    b[loc] -= max_steps * diff;
    if (b[loc] > a[loc]) {
      b_set.emplace(b[loc], loc);
    }
  }

  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      cout << "-1\n";
      return 0;
    }
  }

  cout << num_ops << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<long long>;
using ld = long double;

#define unordered_map __fast_unordered_map
template <class Key, class Value, class Hash = std::hash<Key>>
using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>;

template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);

  int n;
  cin >> n;

  vi a(n), b(n);
  for (int &x : a) {
    cin >> x;
  }
  for (int &x : b) {
    cin >> x;
  }

  multiset<pii> b_set;
  for (int i = 0; i < n; ++i) {
    if (b[i] > a[i]) {
      b_set.emplace(b[i], i);
    }
  }

  ll num_ops = 0;
  while (!b_set.empty()) {
    int val, loc;
    tie(val, loc) = *b_set.rbegin();
    debug(val, loc, a, b);
    b_set.erase(prev(b_set.end()));
    assert(val > a[loc]);

    const ll x = b[(loc + n - 1) % n];
    const ll y = b[(loc + 1) % n];
    const ll diff = x + y;
    if (diff > b[loc] - a[loc]) {
      cout << "-1\n";
      return 0;
    }

    const int bound = max<ll>({a[loc], x - diff, y - diff});
    const int max_steps = (b[loc] - bound) / diff;
    num_ops += max_steps;
    b[loc] -= max_steps * diff;
    if (b[loc] > a[loc]) {
      b_set.emplace(b[loc], loc);
    }
  }

  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      cout << "-1\n";
      return 0;
    }
  }

  cout << num_ops << "\n";
}