Joy of the Cylinder Game
Time limit1sMemory limit128 MB
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 rows numbered through starting at the top, and each row is divided into cells numbered through . Every cell holds one number. Because the board is a cylinder, cell and cell of the same row are adjacent.
A cell is given by two numbers and , where is the row number and is the cell number inside that row. The distance between cells and is
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 , 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 , the number of test cases (). The specifications of the test cases follow.
Each case takes lines. The first line of a case contains three integers , and : the number of rows, the number of cells in each row, and the largest distance allowed in one move ().
Each of the remaining lines contains numbers. The -th number on the -th line is the number in cell . Every given number has absolute value at most .
Output
For each test case print one line. Start with the maximum score you can get, then a single space, then numbers separated by single spaces, where the -th number is the index of the cell you picked in row .
If several selections reach the same maximum score, print the lexicographically smallest one. A selection is lexicographically smaller than a selection if has the smaller number at the first position where the two differ.
Notes
The row numbers of two consecutive rows always differ by , so a move from cell of row to cell of row is allowed exactly when . With the cell number never changes.
Exactly one cell is picked in every row, so the printed list always holds numbers.