Language: C++

#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int64_t min_val = INT64_MAX;

#if 0
int64_t solve(const vector<int64_t> & as, vector<int64_t> & bs, int n, int64_t c);

int64_t decrease(const vector<int64_t> & as, vector<int64_t> & bs, int n, int64_t c, int i, int64_t d)
{
	int64_t nc = bs[i] / d;
	bs[i] -= (nc * d);
	int64_t ans = solve(as, bs, n, c + nc);
	bs[i] += (nc * d);
	return ans;
}

int64_t solve(const vector<int64_t> & as, vector<int64_t> & bs, int n, int64_t c)
{
	if (c >= min_val) return INT64_MAX;

	bool has_diff = false;
	FOR_LT(i, 0, n) {
		if (as[i] != bs[i]) {
			has_diff = true;
			break;
		}
	}
	if (!has_diff) {
		min_val = c;
		return c;
	}

	int64_t ans = INT64_MAX;

	if (bs[n - 1] + bs[1] < bs[0]) {
		int64_t d = bs[n - 1] + bs[1];
		ans = min(ans, decrease(as, bs, n, c, 0, d));
	}
	FOR_LE(i, 1, n - 2) {
		if (bs[i - 1] + bs[i + 1] < bs[i]) {
			int64_t d = bs[i - 1] + bs[i + 1];
			ans = min(ans, decrease(as, bs, n, c, i, d));
		}
	}
	if (bs[n - 2] + bs[0] < bs[n - 1]) {
		int64_t d = bs[n - 2] + bs[0];
		ans = min(ans, decrease(as, bs, n, c, n - 1, d));
	}

	return ans;
}
#endif

void update(const vector<int64_t>& as, const vector<int64_t> & bs, int n, int i, queue<int>& move_idx)
{
	if (i < 0) i += n;
	if (n <= i) i -= n;

	if (as[i] == bs[i]) return;

	if (i == 0) {
		if (bs[n - 1] + bs[1] < bs[0]) move_idx.push(0);
	}
	else if (i == n - 1) {
		if (bs[n - 2] + bs[0] < bs[n - 1]) move_idx.push(n - 1);
	}
	else {
		if (bs[i - 1] + bs[i + 1] < bs[i]) move_idx.push(i);
	}
}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n; cin >> n;

	vector<int64_t> as(n);
	for (auto& a : as) {
		cin >> a;
	}

	vector<int64_t> bs(n);
	for (auto& b : bs) {
		cin >> b;
	}

	int ce = 0;
	FOR_LT(i, 0, n) {
		if (as[i] == bs[i]) ce++;
		if (bs[i] < as[i]) {
			cout << -1 << endl;
			return;
		}
	}

	queue<int> move_idx;

	if (bs[n - 1] + bs[1] < bs[0]) {
		move_idx.push(0);
	}
	FOR_LT(i, 1, n - 1) {
		if (bs[i - 1] + bs[i + 1] < bs[i]) {
			move_idx.push(i);
		}
	}
	if (bs[n - 2] + bs[0] < bs[n - 1]) {
		move_idx.push(n - 1);
	}

	int64_t ans = 0;

	while (ce != n) {
		if (move_idx.empty()) {
			cout << -1 << endl;
			return 0;
		}
		int i = move_idx.front();
		move_idx.pop();

		int64_t d = 0;
		if (i == 0) {
			d = bs[n - 1] + bs[1];
		}
		else if (i == n - 1) {
			d = bs[n - 2] + bs[0];
		}
		else {
			d = bs[i - 1] + bs[i + 1];
		}

		if ((bs[i] - as[i]) % d == 0) {
			ans += (bs[i] - as[i]) / d;
			bs[i] = as[i];
		}
		else {
			int64_t c = bs[i] / d;
			if (bs[i] - c * d < as[i]) {
				continue;
			}
			ans += c;
			bs[i] -= c * d;
		}
		if (bs[i] == as[i]) {
			ce++;
		}

		update(as, bs, n, i + 1, move_idx);
		update(as, bs, n, i - 1, move_idx);
	}

	cout << ans << endl;

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:123:25: error: return-statement with no value, in function returning 'int' [-fpermissive]
  123 |                         return;
      |                         ^~~~~~