var fs = require('fs');

let inputFile = fs.readFileSync('input.txt', 'utf-8');

let myArray = inputFile.split('\n').map((line) => line.split(' '));

const TOTAL_SIGNUP_TIME = parseInt(myArray[0][2]);

let libraryScores = [];
let libraryBooksScore = [];
let maxLibraries = [];

for (let i = 2; i < myArray.length; i += 2) {
  const libraryBooks = myArray[i + 1];

  const daysToScan = TOTAL_SIGNUP_TIME - parseInt(myArray[i][1]);
  const maxBooks = parseInt(myArray[i][2]) * daysToScan;

  let libraryBook = [];

  const libraryScore = libraryBooks
    .map((bookIndex) => parseInt(myArray[1][bookIndex]))
    .sort()
    .reverse()
    .slice(0, maxBooks)
    .map((bookValue) => {
      myArray[1].forEach((v, i) => {
        if (
          v.toString() === bookValue.toString() &&
          libraryBooks.includes(i.toString())
        ) {
          libraryBook.push(i);
        }
      });
      return bookValue;
    })
    .reduce((result, item) => result + item, 0);

  libraryScores.push(libraryScore);
  libraryBooksScore.push(libraryBook);
}

let copyLibraryScores = [...libraryScores];
copyLibraryScores = copyLibraryScores.sort().reverse();
copyLibraryScores.forEach((score) => {
  libraryScores.forEach((s, libraryIndex) => {
    if (s.toString() === score.toString()) {
      maxLibraries.push(libraryIndex);
    }
  });
});

let totalNumberofLibraries = 0;
let totalSignupTime = TOTAL_SIGNUP_TIME;

while (totalSignupTime > 0 && totalNumberofLibraries < maxLibraries.length) {
  totalSignupTime -= parseInt(
    myArray[maxLibraries[totalNumberofLibraries] * 2 + 2][1]
  );
  totalNumberofLibraries++;
}

let booksToSendScanning = [];
let totalSignupYet = TOTAL_SIGNUP_TIME;
for (let j = 0; j < maxLibraries.length; j++) {
  totalSignupYet -= myArray[maxLibraries[j] * 2 + 2][1];
  const maxCurrentBooks =
    (TOTAL_SIGNUP_TIME - totalSignupYet) * myArray[maxLibraries[j] * 2 + 2][2];
  booksToSendScanning.push([
    ...libraryBooksScore[maxLibraries[j]].slice(0, maxCurrentBooks),
  ]);
}

console.log(`${totalNumberofLibraries}`);
for (let k = 0; k < booksToSendScanning.length; k++) {
  console.log(`${maxLibraries[k]} ${booksToSendScanning[k].length}`);
  console.log(`${booksToSendScanning[k].join(' ')}`);
}
