Build the Tallest Tower

Time limit1sMemory limit128 MB

Summary
Select and order bricks with strictly increasing area and weight from bottom to top to maximize total height, then output the chosen brick indices top to bottom.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Greedy
Solved
No attempts yet

Problem

You are given rectangular bricks whose bases are squares. Bricks cannot be rotated, so each brick must use its given base as the base. Build a tower by stacking bricks one at a time from bottom to top, and maximize the sum of the heights of the bricks used.

The tower must satisfy all of the following conditions:

  1. A brick cannot be rotated; a side face cannot be used as its base.
  2. No two bricks have the same base area, and no two bricks have the same weight.
  3. Two or more bricks may have the same height.
  4. A brick with a larger base area cannot be placed on a brick with a smaller base area.
  5. A heavier brick cannot be placed on a lighter brick.

Input

The first line contains the number of bricks N. N is at most 100. Each of the next N lines contains three positive integers describing one brick: its base area, height, and weight, in that order. Bricks are numbered from 1 to N in input order. Every area, height, and weight is a positive integer not greater than 10,000.

Output

Print the number of bricks used in the tower on the first line. Then print the brick numbers from the top brick to the bottom brick, one number per line.

Examples1

  1. Example 1

    Input
    5
    25 3 4
    4 4 6
    9 2 3
    16 2 5
    1 5 2
    Expected output
    3
    5
    3
    1