Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename T> void uin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}

template <typename T> void uax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}

#define int long long
#define ghost signed
#define left left228
#define right right228
#define complex complex228
#define count count228
#define sin sin228
#define list list228

const int N = 200 * 1000 + 228;

int a[N], b[N];

ghost main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  set<pair<int, int>> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      s.insert({b[i], i});
    }
  }
  int cnt = 0;
  while (!s.empty()) {
    pair<int, int> p = *s.rbegin();
    s.erase(p);
    int i = p.second;
    b[i] -= (b[(i + n - 1) % n] + b[(i + 1) % n]);
    ++cnt;
    if (b[i] <= 0) {
      cout << -1 << '\n';
      return 0;
    }
    if (a[i] != b[i])
      s.insert({b[i], i});
  }
  cout << cnt << '\n';
  return 0;
} // kek ;
// Ого! Кажетсья это $#@!


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename T> void uin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}

template <typename T> void uax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}

#define int long long
#define ghost signed
#define left left228
#define right right228
#define complex complex228
#define count count228
#define sin sin228
#define list list228

const int N = 200 * 1000 + 228;

int a[N], b[N];

ghost main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  set<pair<int, int>> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      s.insert({b[i], i});
    }
  }
  int cnt = 0;
  while (!s.empty()) {
    pair<int, int> p = *s.rbegin();
    s.erase(p);
    int i = p.second;
    int bn = b[(i + 1) % n];
    int bp = b[(i - 1 + n) % n];
    int kek =
        min((b[i] - a[i]) / (bn + bp), (b[i] - max(bn, bp)) / (bn + bp) + 1);
    b[i] -= (bn + bp) * kek;
    if (kek == 0) {
      cout << -1 << '\n';
      return 0;
    }
    cnt += kek;
    if (b[i] <= 0) {
      cout << -1 << '\n';
      return 0;
    }
    if (a[i] != b[i])
      s.insert({b[i], i});
  }
  cout << cnt << '\n';
  return 0;
} // kek ;
// Ого! Кажетсья это $#@!