This page is still under construction.

Parts of this page are still being built. What you see may change.

Joy of the Cylinder Game

Time limit1sMemory limit128 MB

Summary
Choose one cell in every row of the cylindrical grid within the step limit so the total is largest, and print the smallest best path.
Level

Medium6 of 10

Topics
Dynamic programming, Sliding window
Solved
No attempts yet

Problem

The cylinder game is new and has not been published anywhere yet. Its inventor wants a program that finds the best play before releasing the game.

The board is a cylinder. It is divided into NN rows numbered 11 through NN starting at the top, and each row is divided into MM cells numbered 11 through MM. Every cell holds one number. Because the board is a cylinder, cell 11 and cell MM of the same row are adjacent.

A cell is given by two numbers XX and YY, where XX is the row number and YY is the cell number inside that row. The distance between cells (X1,Y1)(X_1, Y_1) and (X2,Y2)(X_2, Y_2) is

∣X1−X2∣+min⁡(∣Y1−Y2∣,M−∣Y1−Y2∣)|X_1 - X_2| + \min(|Y_1 - Y_2|, M - |Y_1 - Y_2|)

The game starts by picking any cell of the first row. From the current cell you then move to any cell of the next row whose distance from the current cell is at most KK, and you repeat that move until you reach the last row. Your score is the sum of the numbers in the cells you picked, and the goal is to make that score as large as possible.

Input

The input holds one or more test cases. The first line contains a single integer TT, the number of test cases (1≤T≤1001 \le T \le 100). The specifications of the TT test cases follow.

Each case takes N+1N + 1 lines. The first line of a case contains three integers NN, MM and KK: the number of rows, the number of cells in each row, and the largest distance allowed in one move (1≤N,M,K≤10001 \le N, M, K \le 1000).

Each of the remaining NN lines contains MM numbers. The jj-th number on the ii-th line is the number in cell (i,j)(i, j). Every given number has absolute value at most 10001000.

Output

For each test case print one line. Start with the maximum score you can get, then a single space, then NN numbers separated by single spaces, where the ii-th number is the index of the cell you picked in row ii.

If several selections reach the same maximum score, print the lexicographically smallest one. A selection XX is lexicographically smaller than a selection YY if XX has the smaller number at the first position where the two differ.

Notes

The row numbers of two consecutive rows always differ by 11, so a move from cell yy of row ii to cell y′y' of row i+1i + 1 is allowed exactly when min⁡(∣y−y′∣,M−∣y−y′∣)≤K−1\min(|y - y'|, M - |y - y'|) \le K - 1. With K=1K = 1 the cell number never changes.

Exactly one cell is picked in every row, so the printed list always holds NN numbers.

Examples1

  1. Example 1

    Input
    1
    11 12 3
    5 -2 -5 0 3 10 0 0 0 0 0 0
    1 0 -5 7 9 5 0 0 0 0 0 0
    2 5 3 3 1 0 0 0 0 0 0 0
    9 7 5 1 2 3 0 0 0 0 0 0
    8 8 7 -4 5 5 0 0 0 0 0 0
    11 2 4 -1 5 9 0 0 0 0 0 0
    10 20 15 21 3 -5 0 0 0 0 0 0
    1 2 0 2 1 0 0 0 0 0 0 0
    3 2 1 3 -2 0 0 0 0 0 0 0
    5 6 12 10 11 -9 0 0 0 0 0 0
    0 -1 -2 5 7 3 0 0 0 0 0 0
    
    Expected output
    94 6 4 2 1 1 1 2 2 1 3 5