Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

void solve() {}
int a[200009], b[200009];
bool vis[200009];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  priority_queue<pii> q;
  ll ans = 0;
  rep(i, n) {
    int x;
    cin >> x;
    b[i] = x;
    if (b[i] == a[i])
      vis[i] = 1;
    else
      q.push(mp(x, i));
  }
  while (!q.empty()) {
    pii z = q.top();
    q.pop();
    int i = z.snd, j = (i + n - 1) % n, k = (i + 1) % n;
    if (z.fst - b[j] - b[k] < a[i]) {
      cout << -1;
      return 0;
    }
    int lst = a[i];
    if ((!vis[j] || !vis[k]) && sz(q))
      lst = max(lst, q.top().fst - 1);
    int d = (z.fst - lst + b[j] + b[k] - 1) / (b[j] + b[k]);
    d = max(1, d);
    ans += d;
    b[i] -= d * (b[j] + b[k]);
    if (b[i] < a[i]) {
      cout << -1;
      return 0;
    } else if (b[i] == a[i])
      vis[i] = 1;
    else
      q.push(mp(b[i], i));
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

void solve() {}
int a[200009], b[200009];
bool vis[200009];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  priority_queue<pii> q;
  ll ans = 0;
  rep(i, n) {
    int x;
    cin >> x;
    b[i] = x;
    if (b[i] == a[i])
      vis[i] = 1;
    else
      q.push(mp(x, i));
  }
  while (!q.empty()) {
    pii z = q.top();
    q.pop();
    int i = z.snd, j = (i + n - 1) % n, k = (i + 1) % n;
    if (z.fst - b[j] - b[k] < a[i]) {
      cout << -1;
      return 0;
    }
    int lst = a[i];
    if (!vis[j])
      lst = max(lst, b[j] - 1);
    if (!vis[k])
      lst = max(lst, b[k] - 1);
    int d = (z.fst - lst + b[j] + b[k] - 1) / (b[j] + b[k]);
    d = max(1, d);
    ans += d;
    b[i] -= d * (b[j] + b[k]);
    if (b[i] < a[i]) {
      cout << -1;
      return 0;
    } else if (b[i] == a[i])
      vis[i] = 1;
    else
      q.push(mp(b[i], i));
  }
  cout << ans;
  return 0;
}