No.1120 Strange Teacher
Problem Statement
$N$ students are taking Professor yuki's computer science class.
Yesterday, the final exam was held, and the student numbered $i$ scored $A_i$ points.
However, the professor was not satisfied with the exam results. Therefore, the professor wants to make the student numbered $i$ have $B_i$ points by performing the following operation some number of times.
- Choose $1$ student, add $1$ point to that student, and subtract $1$ point from every other student. (Some students' scores may become negative as a result of this operation.)
Input
$N$ $A_1$ $A_2$ $\cdots$ $A_N$ $B_1$ $B_2$ $\cdots$ $B_N$
The $1$st line contains an integer $N$.
The $2$nd line contains integers $A_1, A_2, ..., A_N$ separated by spaces.
The $3$rd line contains integers $B_1, B_2, ..., B_N$ separated by spaces.
Output
If the professor's goal cannot be achieved, output -1.
If the professor's goal can be achieved, output the minimum number of operations as an integer.
Constraints
- $2 \leq N \leq 100 \ 000$
- $1 \leq A_i \leq 1 \ 000 \ 000$
- $1 \leq B_i \leq 1 \ 000 \ 000$
- All inputs are integers.
Sample
Sample 1
Input
4 100 100 99 100 99 99 100 99
Output
1
If the professor adds $1$ point to the $3$rd student and subtracts $1$ point from everyone else, the scores become $99, 99, 100, 99$ points in order starting from student $1$, as the professor wishes.
Sample 2
Input
4 90 90 90 90 80 60 100 60
Output
30
For example, performing the operations in the following order gives the result the professor wishes.
-
Perform the following operation $10$ times.
- Add $1$ point to the $1$st student and subtract $1$ point from everyone else.
-
Perform the following operation $20$ times.
- Add $1$ point to the $3$rd student and subtract $1$ point from everyone else.
Sample 3
Input
3 20 80 90 82 56 47
Output
-1
There is no way to perform the operations that gives the result the professor wishes.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
e869120
maspy