Prefix Suffix Operations
You are given two arrays
and , each of size . You can perform the following types of operations on array .
- Type : Select any prefix of and increment all its elements by .
- Type : Select any suffix of and increment all its elements by .
Your task is to transform the array into array using the minimum number of operations. If it is impossible to do so, output .
For an array having elements:
- A prefix of the array is a subarray starting at index .
- A suffix of the array is a subarray ending at index .
Input Format
- First line will contain , number of test cases. Then the test cases follow.
- The first line of each test case contains a single integer , the size of arrays and .
- The next line contains space-separated integers, where the integer denotes .
- The next line contains space-separated integers, where the integer denotes .
Output Format
For each test case, print a single line containing one integer ― minimum operations required to convert into . Print if it is impossible to convert into .
Constraints
- Sum of over all test cases does not exceed .
Sample Input 1
3
5
2 3 5 1 2
4 3 6 2 3
4
0 0 0 0
1 2 2 1
3
1 2 3
1 2 2
Sample Output 1
3
2
-1
Explanation
Test Case : Given . It can be converted to array in operations:
- Operation : Perform a type operation. Choose the prefix of length and increment the elements by . Thus, the updated array is .
- Operation : Perform a type operation. Choose the prefix of length and increment the elements by . Thus, the updated array is .
- Operation : Perform a type operation. Choose the suffix of length and increment the elements by . Thus, the updated array is .
It can be proven that we cannot convert the array into array in less than operations.
Test Case : Given . It can be converted to array in operations:
- Operation : Perform a type operation. Choose the prefix of length and increment the elements by . Thus, the updated array is .
- Operation : Perform a type operation. Choose the suffix of length and increment the elements by . Thus, the updated array is .
It can be proven that we cannot convert the array into array in less than operations.
Test Case : Given . The value of will always be greater than . Hence, it is impossible to convert into using any number of operations
Comments
Post a Comment