#include using namespace std; int main() { ios::sync_with_stdio(false); int N; { cin >> N; } vector A(N); { for (int i = 0; i < N; ++i) cin >> A[i]; } vector B(N); { for (int i = 0; i < N; ++i) cin >> B[i]; } if (N == 2) { if (A[0] - B[0] == B[1] - A[0]) { cout << abs(A[0] - B[0]) << endl; } else { cout << -1 << endl; } return 0; } int dSum = 0; { for (int i = 0; i < N; ++i) dSum += A[i] - B[i]; } if (dSum < 0 || dSum % (N - 2)) { cout << -1 << endl; return 0; } vector dord(N); { iota(dord.begin(), dord.end(), 0); sort(dord.begin(), dord.end(), [&](int i, int j) { return B[i] - A[i] > B[j] - A[j]; }); } int dGlobal = 0; int opCnt = dSum / (N - 2); for (int i : dord) { int d = B[i] - (A[i] - dGlobal); int q = (opCnt + d) / 2; if (opCnt + d % 2 == 1 || q < 0 || opCnt < q) { cout << -1 << endl; return 0; } opCnt -= q; dGlobal += q; } assert(opCnt == 0); cout << (dSum / (N - 2)) << endl; }