This page is still under construction.

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

The Enemy of My Enemy is My Friend

Time limit8sMemory limit512 MB

Summary
Given a country adjacency graph, pick a maximum-weight set of countries including our own, where no two chosen countries are adjacent or share a common neighbor.
Level

Medium7 of 10

Topics
Graph, Brute force, Backtracking, Greedy
Solved
No attempts yet

Problem

The year is XXXX. It is an age of war.

Conflicts between neighboring countries over land and resources break out everywhere, and the future of the world is very uncertain. Amid this, one country decides to survive the turbulent age by forming military alliances with various countries. A military alliance must satisfy the following conditions.

  • It cannot ally with a country that neighbors its own country.
  • It cannot ally with a country that neighbors an allied country.

However, each country has a different military strength. It may be more advantageous to ally with one very strong country than with several weak ones. Here we want to find a way to form a military alliance that maximizes the sum of the military strengths of the countries in the alliance.

Input

The input consists of multiple datasets. The format of each dataset is as follows.

N
A1 B1 C1 D1,1 ... D1,C1
A2 B2 C2 D2,1 ... D2,C2
...
AN BN CN DN,1 ... DN,CN

In each dataset, the first line gives the number of countries N (1 ≤ N ≤ 40), and lines 2 through N+1 give the details of the countries. The details of a country are the country name Ai, the military strength Bi, the number of neighboring countries Ci, and the list of neighboring countries Di,1 ... Di,Ci. Our own country is the first country A1.

All country names Ai are distinct and consist of 1 to 16 uppercase or lowercase letters. The military strength Bi is an integer from 0 to 1000. Each country name Di,j in the neighbor list matches one of A1 through AN. The neighbor list of a country never contains the country itself, and the same country name never appears twice in it. Inputs in which the neighbor relation is not symmetric do not occur.

The end of the input is indicated by a line consisting of only 0.

Output

For each test case, output on one line the sum of military strengths when an alliance is formed to maximize the sum of military strengths including our own country.

Examples1

  1. Example 1

    Input
    7
    INTERCAL 10 3 Chef Piet COW
    Chef 7 3 INTERCAL Piet COW
    Piet 6 2 INTERCAL Chef
    COW 7 2 INTERCAL Chef
    J 6 1 A
    A 12 1 J
    Grass 0 0
    7
    Qin 105 4 Zhao Wei Han Chu
    Zhao 81 4 Qin Wei Qi Yan
    Wei 70 5 Qin Zhao Han Qi Chu
    Han 45 3 Qin Wei Chu
    Qi 79 4 Zhao Wei Chu Yan
    Chu 102 4 Qin Wei Han Qi
    Yan 53 2 Zhao Qi
    14
    Magadha 98 8 Macedonia Kalinga Surashtra Ashmaka Kantala Andhra Gangaridai Kamapura
    Macedonia 79 2 Magadha Surashtra
    Kalinga 51 2 Magadha Andhra
    Surashtra 13 3 Magadha Macedonia Ashmaka
    Ashmaka 11 3 Magadha Surashtra Kantala
    Kantala 12 3 Magadha Ashmaka Andhra
    Andhra 14 3 Magadha Kantala Kalinga
    Cholas 15 3 Cheras Pandyas Anuradhapuras
    Cheras 8 2 Cholas Pandyas
    Pandyas 10 3 Cholas Cheras Anuradhapuras
    Anuradhapuras 7 2 Cholas Pandyas
    Gangaridai 20 3 Magadha Kamapura Arakan
    Kamapura 18 2 Magadha Gangaridai
    Arakan 7 1 Gangaridai
    0
    
    Expected output
    22
    184
    120