This page is still under construction.

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

Knowledge for the Masses

Time limit1sMemory limit512 MB

Summary
Each row's racks keep their order and can shift left or right at cost 1 per rack; find the cheapest passage position and all positions attaining it.
Level

Hard8 of 10

Topics
Greedy, Prefix sum, Sorting, Array
Solved
No attempts yet

Problem

You are in a library equipped with bookracks that move on rails. There are many parallel rails, so the bookracks are organized in several rows, as shown below.

The bookracks in the library. At the moment there is no passage to the librarian.

To borrow a book you must reach the librarian, who is hiding on the far side of the bookracks. Your task is to move the racks along the rails so that a passage forms.

  • Each rack has an integer width and can be placed at any integer point along its rail. (A rack is not fixed at a non-integer position and could accidentally roll either way.)
  • The racks in a single row need not be contiguous: there may be arbitrary integer-sized empty space between two successive racks.
  • Within a row, racks may not overlap and cannot pass through one another, so their left-to-right order never changes.

A passage forms at position kk if, in every row, there is no bookrack inside the interval (k,k+1)(k, k + 1).

Passages formed in the library: position 88 (left figure) and position 99 (right figure). Moving the racks marked with arrows, both are attained at cost 33.

Moving a rack takes effort: shifting it in either direction costs 11, and this cost does not depend on the shift distance (which can be explained by the well-known fact that static friction is considerably higher than kinetic friction). You are here to borrow a book, not to work out, so you would like to form a passage (at any position) with as little effort as possible.

Input

In each row, a value ai,j>0a_{i,j} > 0 denotes a bookrack of width ai,ja_{i,j}, while ai,j=0a_{i,j} = 0 denotes a single unit of empty space.

  • The first line contains the number of test cases ZZ (Z≤15Z \le 15). Then ZZ test cases follow.
  • The first line of each test case contains two space-separated integers RR and LL (1≤R1 \le R, 1≤L≤1061 \le L \le 10^6): the number of rows and the common width of every row.
  • Then RR row descriptions follow. Each starts with an integer nin_i, followed by nin_i integers ai,1,ai,2,…,ai,nia_{i,1}, a_{i,2}, \dots, a_{i,n_i} separated by single spaces.

For every row ii, ∑jai,j\sum_j a_{i,j} equals LL minus the number of ai,ja_{i,j} that are equal to 00. Moreover, n1+n2+⋯+nR≤2×107n_1 + n_2 + \dots + n_R \le 2 \times 10^7. Each row description contains at least one 00, so forming a passage is always possible.

Output

For each test case, output two lines.

  • The first line contains the minimum cost of making a passage.
  • The second line contains, in increasing order and separated by spaces, all positions at which a minimum-cost passage can be formed.

Examples4

  1. Example 1

    Input
    1
    4 10
    8 1 2 1 0 1 2 0 1
    7 2 2 2 1 0 1 0
    6 1 3 2 0 2 1
    7 2 1 2 0 2 1 0
    
    Expected output
    3
    8 9
    
  2. Example 2

    Input
    1
    1 2
    2 1 0
    
    Expected output
    0
    1
    
  3. Example 3

    Input
    1
    2 3
    2 2 0
    2 2 0
    
    Expected output
    0
    2
    
  4. Example 4

    Input
    1
    2 4
    3 2 1 0
    3 0 2 1
    
    Expected output
    2
    0 2 3