Under construction

Still being built. Things may change

Cookbook Composition

Interview

Time limit2sMemory limit2048 MB

Summary
Given each recipe as a dependency graph of timed steps, sort the recipes by the ratio of serial time to parallel critical-path time.
Level

Medium4 of 10

Topics
Dynamic programming, Topological sort, Sorting
Solved
No attempts yet

Problem

The world-famous chef Gordon Oliver is composing a new cookbook called "Becoming A Perfect Chef". He has a list of recipes to publish in it. Each recipe is a list of steps. A step can depend on earlier steps, so it cannot start until all of its dependencies have finished. Each step also has an expected time.

Gordon knows that, as an expert chef, he can multitask and do as many tasks simultaneously as needed. A beginner can do only one task at a time, so a beginner must execute the tasks sequentially. He wants to order the recipes by accessibility, with the lowest beginner timeexpert time\frac{\text{beginner time}}{\text{expert time}} ratio first.

For example, take the first sample case. For the oven dish, an expert can prepare the tomatoes, eggplants, and sauce at the same time. The sauce takes the longest, 5, so these steps finish after time 5. Arranging (1) and baking (30) follow, for a total of 5+1+30=365+1+30=36. A beginner needs 2+2+5+1+30=402+2+5+1+30=40. The accessibility ratio of the oven dish is 40/36≈1.1140/36\approx 1.11. The ice cream ratio is 1, because a beginner and an expert both need 5+5+5+240=2555+5+5+240=255. So the ice cream comes before the oven dish.

Input

The first line has an integer nn (2≤n≤5002 \leq n \leq 500), the number of recipes.

Then, for every recipe:

One line with the recipe name and an integer ss (1≤s≤501 \leq s \leq 50), the number of steps.

ss lines follow, one per step. Each line has the step name, an integer tt (1≤t≤1061 \leq t \leq 10^6) for the step duration, an integer dd (0≤d≤490 \leq d \leq 49) for the number of dependencies, and then the names of those dd steps. A step appears only after all the steps it depends on have been listed.

Recipe and step names consist of at most 10 English lowercase letters (a-z). Recipe names are unique, and step names are unique within each recipe.

Output

Output the recipe names in the cookbook, ordered by accessibility. If there are multiple valid solutions, you may output any one of them.

Examples3

  1. Example 1

    Input
    2
    ovendish 5
    tomatoes 2 0
    eggplants 2 0
    sauce 5 0
    arrange 1 3 tomatoes eggplants sauce
    bake 30 1 arrange
    icecream 4
    mix 5 0
    heat 5 1 mix
    churn 5 1 heat
    freeze 240 1 churn
    
    Expected output
    icecream
    ovendish
    
  2. Example 2

    Input
    2
    recipea 4
    stepa 5 0
    stepb 5 1 stepa
    stepc 2 0
    stepd 2 1 stepc
    recipeb 4
    stepa 1 0
    stepb 2 1 stepa
    stepc 2 1 stepa
    stepd 1 2 stepb stepc
    
    Expected output
    recipea
    recipeb
    
  3. Example 3

    Input
    2
    recipea 2
    stepa 2 0
    stepb 2 1 stepa
    recipeb 2
    stepa 5 0
    stepb 5 1 stepa
    
    Expected output
    recipeb
    recipea