Problem p02941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<ll, ll> PL;
typedef vector<PL> vpl;
typedef vector<vector<PL>> vvpl;
typedef vector<vvpl> vvvpl;
typedef pair<ll, PL> TL;
typedef vector<TL> vtl;
typedef vector<vtl> vvtl;
typedef long double ld;
typedef vector<ld> vld;
const ll LINF = 1e17;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for (ll i = (a); i < b; i++)
#define RFOR(i, a, b) for (ll i = (a); i > b; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define FORMAP(it, m) for (auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define epb emplace_back
#define ALL(X) (X).begin(), (X).end()

template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

bool p_comp_fs(const PL p1, const PL p2) { return p1.first < p2.first; };
bool p_comp_fg(const PL p1, const PL p2) { return p1.first > p2.first; };
bool p_comp_ss(const PL p1, const PL p2) { return p1.second < p2.second; };
bool p_comp_sg(const PL p1, const PL p2) { return p1.second > p2.second; };
template <typename T> vector<T> uniquen(vector<T> vec /* copy */) {
  sort(ALL(vec));
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

inline ll popcnt(ll x) { return __builtin_popcountll((unsigned long long)x); };
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// friend bool operator<(const tpl& lhs, const tpl& rhs){
//     return std::tie(lhs.l, lhs.r) < std::tie(rhs.l, rhs.r);
// }

bool bit(ll st, ll b) { return ((st >> b) & 1) == 1; }

int main() {
  ifstream file("../../../../../in.txt");
  if (file.is_open())
    cin.rdbuf(file.rdbuf());
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vl A(N);
  rep(i, N) cin >> A[i];
  vl B(N);
  rep(i, N) cin >> B[i];
  set<PL, greater<PL>> s;
  rep(i, N) {
    if (B[i] != A[i])
      s.insert({B[i], i});
  }

  if (s.empty()) {
    cout << 0 << endstr;
    return 0;
  }

  ll cnt = 0;
  while (!s.empty()) {
    ll idx = s.begin()->ss;
    ll l = (idx - 1 + N) % N, r = (idx + 1 + N) % N;
    if (s.size() < 2) {
      ll dif = B[idx] - A[idx], div = B[l] + B[r];
      if (dif % div != 0) {
        cout << -1 << endstr;
        return 0;
      } else {
        cnt += dif / div;
        cout << cnt << endstr;
        return 0;
      }
    } else {
      auto nmx = next(s.begin())->ff;
      if (A[idx] >= nmx) {
        ll dif = B[idx] - A[idx], div = B[l] + B[r];
        if (dif % div != 0) {
          cout << -1 << endstr;
          return 0;
        } else {
          cnt += dif / div;
          B[idx] = A[idx];
          s.erase(s.begin());
        }
      } else {
        ll dif = B[idx] - nmx, div = B[l] + B[r];
        ll add = (dif + div - 1) / div;
        cnt += add;
        B[idx] -= add * div;
        if (B[idx] < A[idx]) {
          cout << -1 << endstr;
          return 0;
        }
        s.erase(s.begin());
        if (B[idx] != A[idx]) {
          s.insert({B[idx], idx});
        }
      }
    }
  }
  cout << -1 << endstr;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<ll, ll> PL;
typedef vector<PL> vpl;
typedef vector<vector<PL>> vvpl;
typedef vector<vvpl> vvvpl;
typedef pair<ll, PL> TL;
typedef vector<TL> vtl;
typedef vector<vtl> vvtl;
typedef long double ld;
typedef vector<ld> vld;
const ll LINF = 1e17;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for (ll i = (a); i < b; i++)
#define RFOR(i, a, b) for (ll i = (a); i > b; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define FORMAP(it, m) for (auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define epb emplace_back
#define ALL(X) (X).begin(), (X).end()

template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

bool p_comp_fs(const PL p1, const PL p2) { return p1.first < p2.first; };
bool p_comp_fg(const PL p1, const PL p2) { return p1.first > p2.first; };
bool p_comp_ss(const PL p1, const PL p2) { return p1.second < p2.second; };
bool p_comp_sg(const PL p1, const PL p2) { return p1.second > p2.second; };
template <typename T> vector<T> uniquen(vector<T> vec /* copy */) {
  sort(ALL(vec));
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

inline ll popcnt(ll x) { return __builtin_popcountll((unsigned long long)x); };
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// friend bool operator<(const tpl& lhs, const tpl& rhs){
//     return std::tie(lhs.l, lhs.r) < std::tie(rhs.l, rhs.r);
// }

bool bit(ll st, ll b) { return ((st >> b) & 1) == 1; }

int main() {
  ifstream file("../../../../../in.txt");
  if (file.is_open())
    cin.rdbuf(file.rdbuf());
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vl A(N);
  rep(i, N) cin >> A[i];
  vl B(N);
  rep(i, N) cin >> B[i];
  set<PL, greater<PL>> s;
  rep(i, N) {
    if (B[i] != A[i])
      s.insert({B[i], i});
  }

  if (s.empty()) {
    cout << 0 << endstr;
    return 0;
  }

  ll cnt = 0;
  while (!s.empty()) {
    ll idx = s.begin()->ss;
    ll l = (idx - 1 + N) % N, r = (idx + 1 + N) % N;
    if (s.size() < 2) {
      ll dif = B[idx] - A[idx], div = B[l] + B[r];
      if (dif % div != 0) {
        cout << -1 << endstr;
        return 0;
      } else {
        cnt += dif / div;
        cout << cnt << endstr;
        return 0;
      }
    } else {
      ll to = max(B[l] + B[r], A[idx]);
      ll dif = B[idx] - to;
      ll div = B[l] + B[r];
      ll add = (dif + div - 1) / div;
      if (add <= 0) {
        cout << -1 << endstr;
        return 0;
      }
      cnt += add;
      B[idx] -= add * div;
      if (B[idx] < A[idx]) {
        cout << -1 << endstr;
        return 0;
      }
      s.erase(s.begin());
      if (B[idx] != A[idx]) {
        s.insert({B[idx], idx});
      }
    }
  }
  cout << -1 << endstr;
  return 0;
}