Under construction

Still being built. Things may change

War

Time limit2sMemory limit128 MB

Summary
Given a forest of vassal relations and per-country conquest costs, find the minimum days needed to conquer or force surrender of at least M countries using tree knapsack DP.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, Recursion
Solved
No attempts yet

Problem

Country W is at war with N countries. Through intelligence, it learned that if at least M of those countries are conquered or forced to surrender, every remaining country will surrender as well.

The time needed to directly conquer each country may be different. Because all military power must be focused on the country currently being attacked, only one country can be fought at any moment. The goal is to reach the required number of countries as quickly as possible.

There may be vassal relationships between countries. If country A is a vassal of country B, then directly conquering country B also makes country A surrender automatically. These relationships are hierarchical, so conquering a country makes all of its descendant vassals surrender as well.

If a country is a vassal, it has exactly one direct superior. A country may have any number of direct vassals.

Given N, M, the direct conquest time of each country, and the vassal relationships, find the minimum number of days needed to conquer or force the surrender of at least M countries.

Input

The first line contains the number of countries N (1 <= N <= 200) and the required number M (0 <= M <= N).

Each of the next N lines describes one country. A line contains the country name, the number of days needed to directly conquer that country, followed by zero or more names of its direct vassals. A country name is a string of uppercase and lowercase English letters with length from 1 to 100. The names after the conquest time are exactly the direct vassals of the country on that line.

Output

Print the minimum number of days needed to conquer or force the surrender of at least M countries.

Hint

Directly conquering CountryB also makes its vassal CountryA surrender, so 2 countries are secured.

Examples1

  1. Example 1

    Input
    3 2
    CountryA 10
    CountryB 20 CountryA
    CountryC 15
    
    Expected output
    20