#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); using ll = long long; int n; cin >> n; vector a(n), b(n); for (auto&& e : a) cin >> e; for (auto&& e : b) cin >> e; auto d = accumulate(begin(a), end(a), 0LL) - accumulate(begin(b), end(b), 0LL); if (n == 2) { if (d) { cout << "-1\n"; } else { cout << abs(a[0] - b[0]) << '\n'; } exit(0); } if (d < 0 or d % (n - 2)) { cout << "-1\n"; exit(0); } ll num = d / (n - 2), sum = 0; for (int i = 0; i < n; ++i) { ll delta = b[i] - a[i]; if ((num + delta) & 1) { cout << "-1\n"; exit(0); } ll x = (num + delta) / 2; if (x < 0 or x > num) { cout << "-1\n"; exit(0); } sum += x; } cout << num << '\n'; DEBUG(sum); }