Language: C++

/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>

#define F first
#define S second
#define endl '\n'
#define deb(x) cout<<#x<<' '<<x<<endl;
#define pb push_back

const long long MAXN = 1e6 + 1;
const long long ZB = 1e18;
const long long MOD = 1e9 + 7;
using namespace std;

typedef pair<int, int> pii;


long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

void fail() {
    cout << -1;
    exit(0);
}



#define ll long long

ll a[MAXN];
ll  b[MAXN];


int n(int i, int k) {
    if (i == k) return 1;
    return i + 1;
}
int p(int i, int k) {
    if (i == 1) return k;
    return i - 1;
}
main(){
    int n = readInt();
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
    }
    queue<int> que;
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
        if (b[i] < a[i]) fail();
        que.push(i);
    }

    ll ans = 0;

    while (que.size()) {
        int i = que.front(); que.pop();
        ll near = b[next(i, n)] + b[prev(i, n)];
        ll u = (b[i] - a[i]) / near;
        if (u <= 0) continue;
        ans += u;
        b[i] %= near;
        que.push(p(i, n));
        que.push(n(i, n));
    }

    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i]) fail();
    }

    cout << ans << endl;

    return 0;
}



Compiler output:
a.cc:83:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   83 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:99:25: error: no matching function for call to 'next(int&, int&)'
   99 |         ll near = b[next(i, n)] + b[prev(i, n)];
      |                     ~~~~^~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/vector:62,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidate: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h: In substitution of 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type) [with _InputIterator = int]':
a.cc:99:25:   required from here
   99 |         ll near = b[next(i, n)] + b[prev(i, n)];
      |                     ~~~~^~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: error: no type named 'difference_type' in 'struct std::iterator_traits<int>'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:99:41: error: no matching function for call to 'prev(int&, int&)'
   99 |         ll near = b[next(i, n)] + b[prev(i, n)];
      |                                     ~~~~^~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidate: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h: In substitution of 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type) [with _BidirectionalIterator = int]':
a.cc:99:41:   required from here
   99 |         ll near = b[next(i, n)] + b[prev(i, n)];
      |                                     ~~~~^~~~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: error: no type named 'difference_type' in 'struct std::iterator_traits<int>'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:105:19: error: 'n' cannot be used as a function
  105 |         que.push(n(i, n));
      |                  ~^~~~~~