Problem Statement There are N cells numbered from 1 to N in a row. Initially, M cells…
- User
Problem Statement There are N cells numbered from 1 to N in a row. Initially, M cells contain stones, and cell X i contains A i stones (1≤i≤M).
You can perform the following operation any number of times (possibly zero):
If cell i ( 1≤i≤N−1) contains a stone, move one stone from cell i to cell i+1. Find the minimum number of operations required to reach a state where each of the N cells contains exactly one stone. If it is impossible, print −1.
Constraints 2≤N≤2×10 9
1≤M≤2×10 5
M≤N 1≤X i ≤N (1≤i≤M) X i
=X j
(1≤i<j≤M) 1≤A i ≤2×10 9
(1≤i≤M) All input values are integers. Input The input is given from Standard Input in the following format:
N M X 1
X 2
… X M
A 1
A 2
… A M
Output Print the answer.
Sample Input 1 Copy 5 2 1 4 3 2 Sample Output 1 Copy 4 You can reach a state where each of the five cells contains exactly one stone with four operations as follows:
Move one stone from cell 1 to cell 2. Move one stone from cell 2 to cell 3. Move one stone from cell 4 to cell 5. Move one stone from cell 1 to cell 2. It is impossible to achieve the goal in three or fewer operations. Therefore, print 4.
Sample Input 2 Copy 10 3 1 4 8 4 2 4 Sample Output 2 Copy -1 No matter how you perform the operations, you cannot reach a state where all ten cells contain exactly one stone. Therefore, print −1. kodu c++da yazsana
- Assistant