LCC '24 Contest 3 S4 - Split

View as PDF

Submit solution

Points: 10
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Split is a new mobile game that has recently exploded in popularity. In each level of Split, you are given two rows of non-negative integers that can be represented as arrays A and B of size M. We label the first index of both arrays 1 and the last index M. The objective of Split is to beat each level by turning array A into array B.

In a split operation, you can split any index A_i in A by redistributing its value to adjacent indices and setting A_i to 0. For the case A_1 and A_M, you may only redistribute its value to A_2 and A_{M-1}, respectively. Note that only integer values may be distributed. The sum of values across all indices in A and B shall remain the same across all split operations, N.

For example, in one operation, you can redistribute A_1 in A = [1,2,3] to A_2, resulting in the array [0,3,3]. You can also redistribute A_2 from the same array A to create one of [3,0,3], [2,0,4], or [1,0,5]. Finally, you can redistribute A_M=A_3 from the same array A to A_{M-1}=A_2, resulting in [1,5,0].

Unfortunately, the game is bugged and contains levels that are impossible to beat. The creator of Split knows that if the level can be solved, there is always a sequence of split operations that takes at most 3M moves. Thus, determine if it is possible to transform array A into array B. If it is possible, determine a sequence of split operations that transforms array A into array B, taking at most 3M moves.

Constraints

1 \leq N \leq 10^9

2 \leq M \leq 10^5

0 \leq A_i, B_i \leq N

\sum\limits_{i=1}^M A_i, B_i = N

Input Specification

The first line will contain 2 integers N and M.

The next line will contain M space seperated integers A_i.

The third and final line will contain M space seperated integers B_i.

Output Specification

The first line of output should be -1 if the level of split is impossible, that is, there is no sequence of split operations that transforms array A into array B. Otherwise, output a non-negative integer K \ (K \le 3M) indicating the number of split operations.

Each of the next K lines should contain 3 space-separated integers i \ (1 \le i \le M), l_i, and r_i, indicating the index to split, the value redistributed to the left, and the value redistributed to the right, respectively. Given the nature of the operation, l_i + r_i should equal the value of A_i before redistribution.

The number of moves does not have to be minimized and any valid sequence of operations such that K \leq 3M are accepted.

Sample Input 1

15 6
2 5 1 0 2 5
8 1 1 0 2 3

Sample Output 1

7
6 5 0
5 4 3
4 2 2
3 3 0
2 6 2
3 1 1
4 1 0

Explanation For Sample Input 1

After each split operation, A is as follows:

2 5 1 0 7 0
2 5 1 4 0 3
2 5 3 0 2 3
2 8 0 0 2 3
8 0 2 0 2 3
8 1 0 1 2 3
8 1 1 0 2 3

There may be multiple solutions. Output any valid one.

Sample Input 2

3 2
1 2
2 1

Sample Output 2

-1

Comments

There are no comments at the moment.