RoAlgo PreOJI 2024 VII | partsum

This was the problem page during the contest. Access the current page here.
Time limit: 0.4s Memory limit: 64MB Input: partsum.in Output: partsum.out

Task

From a matrix matmat of size N×NN \times N, three matrices v1v_1, v2v_2, v3v_3 are constructed with the following properties:
For each cell in the matrix (i,j)(i, j), where 1i,jN1 \leq i, j \leq N:

  • 0v1(i,j)<i0 \leq v_{1(i, j)} < i;
  • 0v2(i,j)<j0 \leq v_{2(i, j)} < j;
  • v3(i,j)v_{3(i, j)} is the sum of the submatrix with the top-left corner at (iv1(i,j),jv2(i,j))(i-v_{1(i, j)}, j-v_{2(i, j)}) and the bottom-right corner at (i,j)(i, j) from the matrix matmat.

You are given NN, KK, and the matrices v1v_1, v2v_2, v3v_3. You need to display a way to change exactly KK values in the matrix matmat, such that the sum of the values in v3v_3 is minimized. Additionally, you need to display the minimum sum. You can choose to keep the element in cell (i,j)(i, j) in the matrix matmat unchanged, in which case you will display (i,j,element)(i, j, element) of the matrix matmat at cell (i,j)(i, j).

Input data

The first line of the input file partsum.in contains two integers, NN and KK. The next NN lines will contain NN values each, representing the values in v1v_1. The following NN lines will contain NN values each, representing the values in v2v_2. The next NN lines will contain NN values each, representing the values in v3v_3.

Note! In the tests, there are no empty lines between the matrices v1v_1, v2v_2, and v3v_3. They have been added only for clarification in the example!

Output data

The first KK lines of the output file partsum.out will contain three values lini,coli,xilin_i, col_i, x_i, representing that the value in cell (lini,coli)(lin_i, col_i) will change to xix_i. On the (K+1)(K+1)-th line, the minimum sum formed will be displayed.

Note! If at least one value among linilin_i or colicol_i is not within the interval [1,N][1, N], the verdict will be Wrong Answer. Additionally, if at least one value xix_i is not within the interval [0,3×106][0, 3 \times 10^6], the verdict will be Wrong Answer.

Constraints and clarifications

  • 1N6001 \leq N \leq 600;
  • 1KN21 \leq K \leq N^2;
  • There may be multiple correct solutions, any of them will be accepted.
  • If the KK changes do not lead to the sum of the values in v3v_3 being the sum displayed on the (K+1)(K+1)-th line, you will receive 00 points!
  • It is guaranteed that the matrices v1v_1, v2v_2, and v3v_3 originate from a matrix matmat.
  • 0v3(i,j)10120 \leq v_{3(i, j)} \leq 10^{12}.
  • It is guaranteed that the elements of the matrix matmat are natural numbers.
# Points Constraints
1 13 v1(i,j)=v2(i,j)=0v_{1(i, j)} = v_{2(i, j)} = 0, 1i,jN1 \leq i, j \leq N
2 15 K=1K = 1
3 29 1N501 \leq N \leq 50
4 43 No additional constraints

Example 1

partsum.in

2 1
0 0
0 0

0 1
0 0

2 5
3 3

partsum.out

1 1 0
9

Explanation

The matrix matmat from which v1v_1, v2v_2 and v3v_3 are derived is:

2 3
3 3

If we change the value 22 to 00 in cell (1,1)(1, 1), the sums on row 11 will become 0 30 \ 3, and the sum of the values in v3v_3 becomes 0+3+3+3=90 + 3 + 3 + 3 = 9. This is the minimum sum that can be formed if we change at most one element.

Log in or sign up to be able to send submissions!