Language: C++

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

// const ll MOD = 998244353;

//ll solver(vector<ll>A,vector<ll>B){
//    int N = (int)A.size();
//    ll res = 0;
//    while(true){
//        ll max1 = -LINF,max2 = -LINF;
//        ll id1 = -1,id2 = -1;
//        for(int i = 0; i < N;i++){
//            if(A[i] == B[i]) continue;
//            if(B[i] > max1){
//                id2 = id1;
//                id1 = i;
//
//                max2 = max1;
//                max1 = B[i];
//            }else if(B[i] > max2){
//                id2 = i;
//                max2 = B[i];
//            }
//        }
//        if(max1 == -LINF) break;
//        ll S = B[(id1-1+N)%N] + B[(id1+1+N)%N];
//        if(max1 == max2){
//            res++;
//            B[id1] -= S;
//        }else{
//            ll C = ((max1-max(A[id1],max2))+S-1)/S;
//            res+=C;
//            B[id1] -= S*C;
//        }
//
//        if(B[id1] < A[id1]) return -1;
//    }
//    return res;
//}

ll solver2(vector<ll>A,vector<ll>B){
    int N = (int)A.size();
    ll res = 0;
    
    priority_queue<pll> pq;
    for(int i = 0; i < N;i++){
        pq.push(pll(B[i],i));
    }
    while(pq.size()){
        ll max1,id1; tie(max1,id1) = pq.top(); pq.pop();
        
        ll max2 = -LINF,id2 = -1;
        if(pq.size()) tie(max2,id2) = pq.top();pq.pop();
        
        ll S = B[(id1-1+N)%N] + B[(id1+1+N)%N];
        if(max1 == max2){
            res++;
            B[id1] -= S;
        }else{
            ll C = ((max1-max(A[id1],max2))+S-1)/S;
            res+=C;
            B[id1] -= S*C;
        }
        if(B[id1] < A[id1]) return -1;
        if(B[id1] == A[id1]) continue;
        pq.push(pll(B[id1],id1));
    }
    return res;
}

template<class Type>
Type solve(Type res = Type()){
    ll N; cin >> N;
    vector<ll> A(N),B(N);

    for(auto& in:A) cin >> in;
    for(auto& in:B) cin >> in;
    return solver2(A,B);
}

void test(){
    random_device rnd;
    mt19937_64 mt;
    uniform_int_distribution<> rand(1, INF);
    mt.seed(rnd());
    for(int _ = 0; _ < 1000; _++){
        ll N = 5;
        vector<ll> A(N,1);
        for(int i = 0; i < N;i++){
            A[i] = rand(mt);
        }
        auto B = A;
        bool ng = false;
        for(int i = 1; i <= 1000; i++){
            ll j = rand(mt)%N;

            B[j] += B[(j-1+N)%N] + B[(j+1)%N];
            if(B[j] > INF){
                break;
            }

            ll ret = solver(A,B);
            if(i != ret){
                ng = true;
                cout << A << endl;
                cout << B << endl;
                break;
            }
        }
        if(ng){
            cout << "NG" << " " << ng  << endl;
            break;
        }
        cout << "ok " << endl;
        cout << A << endl;
    }
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    // test();
    // solve(0);
    cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}


Compiler output:
a.cc: In function 'void test()':
a.cc:124:22: error: 'solver' was not declared in this scope; did you mean 'solver2'?
  124 |             ll ret = solver(A,B);
      |                      ^~~~~~
      |                      solver2