Problem:
The Enemy of My Enemy is My Friend
敵の敵は味方
English text is not available in this practice contest.
時は XXXX 年。戦乱の世である。
土地や資源を巡って、隣り合った国同士の衝突がそこかしこで起こり、世の中の先行きは非常に不透明であった。そんな中とある国が、様々な国と軍事同盟を結んでこの乱世を生き延びようと考えた。軍事同盟は次の条件をみたすように行うことが出来る。
自国の隣国とは同盟を結ぶことは出来ない。
同盟をした国の隣国とは同盟を結ぶことは出来ない。
ただし、国ごとにその軍事的な強さは異なっている。軍事的に強くない複数の国と同盟を結ぶより、軍事的に非常に強い一国と同盟を結んだほうが有利であることも有り得る。ここでは、同盟に含まれる国の軍事的強さの和が最大になるような軍事同盟の結び方を考えたい。
Input
入力は複数のデータセットからなる．各データセットの形式は次の通りである。
N
A
1
B
1
C
1
D
1,1
...
D
1,C
1
A
2
B
2
C
2
D
2,1
...
D
2,C
2
...
A
N
B
N
C
N
D
N,1
...
D
N,C
N
各データセットでは、1行目は国の数 N (1 ≤ N ≤ 40)が与えられ、2〜N+1行目には国の詳細が与えられる。国の詳細では、国名 A
i
、軍事的強さ B
i
、隣接している国の数 C
i
、隣接している国のリスト D
i,1
... D
i,C
i
が与えられる。自国は1つ目の国 A
1
である。
国名 A
i
は全て異なり、それぞれ1文字以上16文字以下の大文字あるいは小文字アルファベットからなる。軍事的強さ B
i
は0以上1000以下の整数で与えられる。隣接している国のリストにある国名 D
i,j
は A
1
から A
N
のうちいずれかと一致する。また、ある国の隣接国リストにその国自身が含まれることはなく、同じ国名が2回以上重複して含まれることもない。さらに、隣接関係が対称でない場合は入力に存在しない。
入力の終了は0のみからなる行で表される。
Output
各テストケースについて、自国を含めた軍事的強さの和が最大になるような同盟を考えたとき、その強さを求め、1行で出力せよ。
Sample 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
Output for Sample Input
22
184
120

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;


map<string, int>mp;
int num = 0;

int getnum(string st) {
	if (mp.find(st) == mp.end()) {
		mp[st] = num++;
	}
	return mp[st];
}



struct country {
	int num;
	int power;
	vector<int>connects;
};
int N;
vector<int>rests;
void getans(int now, const vector<country>&cs,vector<int>&oks,int n_power ,int&ans) {
	if (now == N-1) {
		ans = max(ans, n_power);
		return;
	}
	else if (rests[now] + n_power < ans) {
		return;
	}
	if (oks[cs[now].num]) {
		vector<int>nextoks(oks);
		for (auto con : cs[now].connects) {
			nextoks[con] = false;
		}
		getans(now + 1, cs, nextoks, n_power + cs[now].power, ans);
	}
	getans(now + 1, cs, oks, n_power, ans);
}

int main() {
	while (1) {cin >> N;
	if (!N)break;
	mp.clear();
	num = 0;
	rests.clear();
		vector<vector<int>>connects(N);
		vector<int>powers(N);
		for (int i = 0; i < N; ++i) {
			string name;
			int B, C; cin >> name>> B >> C;
			int from = getnum(name);
			powers[from] = B;
			for (int j = 0; j < C; ++j) {
				string d; cin >> d;
				int to = getnum(d);
				connects[from].push_back(to);
			}
		}
		vector<country>cs;
		for (int i = 1; i < N; ++i) {
			cs.push_back(country{ i,powers[i],connects[i] });
		}
		sort(cs.begin(), cs.end(), [](const country&l, const country&r) {
			return l.power > r.power;
		});

		vector<int>oks(N,1);
		for (auto con : connects[0]) {
			oks[con] = false;
		}
		rests=vector<int>(N);

		for (int i = N -2; i >= 0; --i) {
			rests[i] = rests[i + 1] + cs[i].power;
		}
		int ans = 0;
		
		getans(0, cs, oks, 0, ans);
		cout <<powers[0]+ ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3408
final_rank: 1

code_snippet: #include <bits/stdc++.h>
      
#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort( (x).begin(),(x).end() )
#define REVERSE(x) reverse( (x).begin(),(x).end() )
#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x,n) for( int yui = 0; yui < n; yui++ ){ cout << x[yui] << " "; } cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;
     
const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9+7;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> inline T sq( T a ){ return a * a; }

ll in(){ long long int x; scanf( "%lld" , &x ); return x; }
char yuyushiki[1000010]; string stin(){ scanf( "%s" , yuyushiki ); return yuyushiki; }

// head

int n;

int a[64];

map<string,int> ma;

vector<string> sG[64];
vector<int> G[64];

ll ms[64];

int ans;

vi v;

int hstar( int p , ll mask ){
  int res = 0;
  FOR( i , p , n ){
    if( !( mask & PW(v[i]) ) ){
      res += a[v[i]];
    }
  }
  return res;
}

void dfs( int p , int cur, ll mask ){
  // cout << cur << " " << mask << endl;
  chmax( ans , cur );
  if( cur + hstar( p , mask ) <= ans ){
    return;
  }
  FOR( i , p , n ){
    if( !( mask & PW(v[i]) ) ){
      dfs( i+1 , cur + a[v[i]], mask | ms[v[i]] );
    }
  }
}

int cmp( int i , int j ){
  return a[i] > a[j];
}

int main(){

  while( 1 ){
    n = in();
    if( n == 0 ){
      break;
    }
    ma.clear();
    ans = 0;
    v.clear();
    REP( i , n ){
      sG[i].clear();
      G[i].clear();
      ms[i] = 0;
    }
    REP( i , n ){
      string s = stin();
      ma[s] = i;
      a[i] = in();
      int m = in();
      REP( j , m ){
        string t = stin();
        sG[i].pb( t );
      }
    }
    REP( i , n ){
      YYS( w , sG[i] ){
        G[i].pb( ma[w] );
        ms[i] = ms[i] | PW( ma[w] );
      }
      ms[i] = ms[i] | PW(i);
    }
    REP( i , n ){
      v.pb( i );
    }
    sort( ALL(v), cmp );
    dfs( 0 , a[0] , ms[0] );
    printf( "%d\n" , ans );
  }

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3692
final_rank: 2

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// #ifndef ONLINE_JUDGE
// #define _GLIBCXX_DEBUG // 配列外参照のエラー
// #endif

// スタンダードライブラリ
#include <bits/stdc++.h>
using namespace std;

// 型関連
using ll = long long;
using lint = long long;
using pll = pair<ll, ll>;
using plll = tuple<ll, ll, ll>;
using pii = pair<int, int>;
using piii = tuple<ll, ll, ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;

template <class T> using prique = priority_queue<T>;

// 型定数
constexpr ll mod = 998244353;
constexpr ll MOD = 1000000007;
int INF32 = 2e9;
ll INF64 = 9e18;
#define endl '\n';

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/////////////////////////// print関連
template <typename T> void print(vector<T> A);
void print();
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail);
void print(string S);
template <typename T> void print(vector<vector<T>> &F);

inline void Yes(bool f);

///////////////////ここから//////////////////////
bool solve() {
    int N;
    cin >> N;
    if (N == 0) {
        return false;
    }
    int n = 0;
    map<string, int> name2n;
    vvi E(N, vi(N));
    vvi G(N);

    vi Power(N);
    for (int i = 0; i < N; i++) {
        string S;
        cin >> S;
        if (name2n.count(S) == 0) {
            name2n[S] = n;
            n++;
        }
        int u = name2n[S];
        int B, C;
        cin >> B >> C;
        Power[u] = B;
        for (int j = 0; j < C; j++) {
            string T;
            cin >> T;
            if (name2n.count(T) == 0) {
                name2n[T] = n;
                n++;
            }
            int v = name2n[T];
            E[u][v] = 1;
            G[u].emplace_back(v);
        }
    }
    // print(E);

    int N1 = N / 2;
    int N2 = N - N1;
    vl dp(1 << N2,-INF32);
    vi lis;

    for (int S1 = 0; S1 < (1 << N1); S1++) {
        if ((S1 & 1) == 0) {
            continue;
        }
        lis.clear();

        ll W = 0;
        bool ok = true;
        for (int i = 0; i < N1; i++) {
            if (S1 & (1 << i)) {
                for (auto v : lis) {
                    if (E[i][v]) {
                        ok = false;
                        goto go1;
                    }
                }
                lis.emplace_back(i);
                W += Power[i];
            }
        }

        if (ok == false) {
        go1:;
            continue;
        }

        int S2 = 0;
        for (int v : lis) {
            for (int i = 0; i < N2; i++) {
                if (E[v][i + N1]) {
                    S2 |= (1 << i);
                }
            }
        }
        S2 ^= ((1 << N2) - 1);
        dp[S2] = max(dp[S2], W);
    }
    for (int S2 = (1 << N2) - 1; S2 >= 0; S2--) {
        for (int i = 0; i < N2; i++) {
            if (S2 & (1 << i)) {
                dp[S2 - (1 << i)] = max(dp[S2 - (1 << i)], dp[S2]);
            }
        }
    }

    ll ans = 0;

    for (int S2 = 0; S2 < (1 << N2); S2++) {

        lis.clear();
        ll W = 0;
        bool ok = true;
        for (int i = 0; i < N2; i++) {
            if (S2 & (1 << i)) {
                for (auto v : lis) {
                    if (E[v + N1][i + N1]) {
                        ok = false;
                        goto go2;
                    }
                }
                lis.emplace_back(i);
                W += Power[i + N1];
            }
        }

        if (ok == false) {
        go2:;
            continue;
        }
        ans = max(ans, W + dp[S2]);
        // print(11,W);
    }
    print(ans);

    return true;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    while (solve())
        ;
}

////////////////////print/////////////////////////
// 1次元ベクトルを出力する
template <typename T> void print(vector<T> A) {
    if (A.size() == 0) {
        return;
    }
    for (size_t i = 0; i < A.size() - 1; i++) {
        cout << A[i] << ' ';
    }
    cout << A[A.size() - 1] << endl;
    return;
}

// 2次元ベクトルを出力する
template <typename T> void print(vector<vector<T>> &F) {
    for (size_t i = 0; i < F.size(); i++) {
        for (size_t j = 0; j < F[i].size(); j++) {
            cout << F[i][j];
            if (j < F[i].size() - 1) {
                cout << ' ';
            }
        }
        cout << endl;
    }
}

// 空白行を出力するprint
void print() { cout << endl; }

// 数値・文字列を空白区切りで出力する. print(a,b)など
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}

void print(string S) { cout << S << endl; }

//////////////出力関連

inline void Yes(bool f) {
    if (f) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 5460
memory_kb: 19916
final_rank: 18

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var) do { cout << #var << " :\n"; view(var); } while(0)
template<typename T>void view(const T& e) {cout << e;}
template<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << "{" << p.first << ", " << p.second << "}";}
template<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << " ";} cout << endl;}
template<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << " ";} cout << endl;}

int N;

void solve(){
    vc<string> A(N);
    vi B(N);
    vv<string> D(N);
    rep(i, N){
        cin >> A[i] >> B[i];
        int c; cin >> c;
        D[i].resize(c);
        rep(j, c) cin >> D[i][j];
    }
    if (N == 1){
        cout << B[0] << endl;
        return;
    }
    unordered_map<string, int> toid;
    rep(i, N) toid[A[i]] = i;
    vl no(N, 0);
    rep(i, N){
        for (auto d : D[i]) no[i] += (1ll << toid[d]);
    }
    int P = N / 2, Q = N - P;
    vi maxi(1 << Q, -inf);
    rep(cmb, 1 << P) if (cmb & 1){
        ll ng = 0;
        int sm = 0;
        rep(i, P) if (cmb >> i & 1){
            ng |= no[i];
            sm += B[i];
        }
        if ((ng & cmb) != 0) continue;
        ng >>= P;
        ll ok = ((1 << Q) - 1) ^ ng;
        maxi[ok] = max(maxi[ok], sm);
    }

    // for (ll cmb = (1 << Q) - 1; cmb >= 0; cmb--){
    //     rep(i, Q) if (cmb >> i & 1) maxi[cmb ^ (1 << i)] = max(maxi[cmb], maxi[cmb ^ (1 << i)]);
    // }
    for (int i = 0; i < Q; i++){
        for (int S = 0; S < 1 << Q; S++){
            if (~S & (1 << i)){
                maxi[S] = max(maxi[S], maxi[S ^ (1 << i)]);
            }
        }
    }

    int ans = 0;
    rep(cmb, 1 << Q){
        ll ncmb = cmb << P;
        ll ng = 0;
        int sm = 0;
        srep(i, P, N) if (ncmb >> i & 1){
            ng |= no[i];
            sm += B[i];
        }
        if ((ng & ncmb) != 0) continue;

        ans = max(ans, sm + maxi[cmb]);
    }
    cout << ans << endl;
}

int main(){
    while (true){
        cin >> N;
        if (N == 0) break;
        solve();
    }
}
accuracy: 1
time_ms: 4860
memory_kb: 7852
final_rank: 6

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var) do { cout << #var << " :\n"; view(var); } while(0)
template<typename T>void view(const T& e) {cout << e;}
template<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << "{" << p.first << ", " << p.second << "}";}
template<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << " ";} cout << endl;}
template<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << " ";} cout << endl;}

int N;

void solve(){
    vc<string> A(N);
    vi B(N);
    vv<string> D(N);
    rep(i, N){
        cin >> A[i] >> B[i];
        int c; cin >> c;
        D[i].resize(c);
        rep(j, c) cin >> D[i][j];
    }
    if (N == 1){
        cout << B[0] << endl;
        return;
    }
    unordered_map<string, int> toid;
    rep(i, N) toid[A[i]] = i;
    vl no(N, 0);
    rep(i, N){
        for (auto d : D[i]) no[i] += (1ll << toid[d]);
    }
    int P = N / 2, Q = N - P;
    vi maxi(1 << Q, -inf);
    rep(cmb, 1 << P) if (cmb & 1){
        ll ng = 0;
        int sm = 0;
        rep(i, P) if (cmb >> i & 1){
            ng |= no[i];
            sm += B[i];
        }
        if ((ng & cmb) != 0) continue;
        ng >>= P;
        ll ok = ((1 << Q) - 1) ^ ng;
        maxi[ok] = max(maxi[ok], sm);
    }

    for (ll cmb = (1 << Q) - 1; cmb >= 0; cmb--){
        rep(i, Q) if (cmb >> i & 1) maxi[cmb ^ (1 << i)] = max(maxi[cmb], maxi[cmb ^ (1 << i)]);
    }


    int ans = 0;
    rep(cmb, 1 << Q){
        ll ncmb = cmb << P;
        ll ng = 0;
        int sm = 0;
        srep(i, P, N) if (ncmb >> i & 1){
            ng |= no[i];
            sm += B[i];
        }
        if ((ng & ncmb) != 0) continue;

        ans = max(ans, sm + maxi[cmb]);
    }
    cout << ans << endl;
}

int main(){
    while (true){
        cin >> N;
        if (N == 0) break;
        solve();
    }
}
accuracy: 1
time_ms: 7010
memory_kb: 7928
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<(n);i++)
#define REP2(i, n) for (ll i=0;i<(n);i++)
#define REP3(i, a, n) for (ll i=a;i<(n);i++)
#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=(n)-1;i>=0;i--)
#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)
#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)
#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

ll intpow(ll a,ll b){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
        }
        a *= a;
        b /= 2;
    }
    return ans;
}
ll modpow(ll a,ll b,ll c){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
            ans %= c;
        }
        a *= a;
        a %= c;
        b /= 2;
    }
    return ans;
}

template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}

void print(){cout << "\n";}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
    os << "[";
    for (size_t i = 0; i < vec.size(); ++i) {
        os << vec[i];
        if (i + 1 < vec.size()) os << ", ";
    }
    os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {
    os << "[";
    for (size_t j = 0; j < a.size(); ++j) {
        os << "(" << a[j].first << ", " << a[j].second << ")";
		if (j + 1 < a.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {
	os << "[";
    for (size_t i = 0; i < mat.size(); ++i) {
		os << "[";
        for (size_t j = 0; j < mat[i].size(); ++j) {
            os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")";
			if (j + 1 < mat[i].size()) os << ", ";
        }
		os << "]";
		if (i + 1 < mat.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template <typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& [key, val] : m) {
        if (!first) os << ", ";
        os << key << ": " << val;
        first = false;
    }
    os << "}";
    return os;
}

template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}

void write(){cout << "\n";}
template<class T, class... Ts>
void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(pll x){cout << x.first << ' ' << x.second << '\n';}
void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}}
void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}

template <typename T>
T sum(const std::vector<T>& v) {
    return std::accumulate(v.begin(), v.end(), T(0));
}

template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }




int main(){
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
    while(1){
        LL(n);
        if(n == 0){break;}
        vs name(n);
        vll x(n);
        vvs neighbor(n);
        map<string,int> mp;
        rep(i,n){
            STR(s);
            mp[s] = i;
            LL(z);
            name[i] = s;
            x[i] = z;
            LL(cnt);
            rep(j,cnt){
                STR(t);
                neighbor[i].push_back(t);
            }
        }
        vvll edge(n);
        rep(i,n){
            for(string t:neighbor[i]){
                edge[i].push_back(mp[t]);
            }
        }
        ll m = n / 2;
        vll dp(1<<m,-1LL<<60);
        rep(i,(1<<m)){
            if(((i >> 0) & 1) == 0){continue;}
            ll temp = 0;
            ll c = 0;
            rep(j,m){
                if(((i >> j) & 1) == 1){
                    temp += x[j];
                    for(ll to:edge[j]){
                        if(0 <= to && to < m && ((i >> to) & 1) == 1){
                            c = 1;
                            break;
                        }
                    }
                }
            }
            if(c){continue;}
            dp[i] = temp;
        }

        rep(i,m){
            rep(j,(1<<m)){
                if(((j >> i) & 1) == 0){
                    chmax(dp[j | (1 << i)],dp[j]);
                }
            }
        }

        ll ans = 0;
        ll r = n - m;
        rep(i,(1<<r)){
            ll temp = 0;
            ll c = 0;
            rep(j,r){
                if(((i >> j) & 1) == 1){
                    temp += x[j+m];
                    for(ll to:edge[j+m]){
                        if(m <= to && ((i >> (to - m)) & 1) == 1){
                            c = 1;
                            break;
                        }
                    }
                }
            }
            if(c){continue;}
            ll v = (1 << m) - 1;
            rep(j,r){
                if(((i >> j) & 1) == 1){
                    for(ll to:edge[j+m]){
                        if(to < m && ((v >> to) & 1) == 1){
                            v ^= (1 << to);
                        }
                    }
                }
            }
            chmax(ans,temp + dp[v]);
        }
        write(ans);
    }
}
accuracy: 1
time_ms: 7330
memory_kb: 11836
final_rank: 17

code_snippet: /**
	author:  shobonvip
	created: 2025.04.24 18:44:48
**/

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

void solve(int n) {
	vector<string>name(n);
	vector lst(n,vector<string>(0));
	vector<int> b(n);

	rep(i,0,n){
		cin>>name[i];
		cin>>b[i];
		int c;cin>>c;
		rep(j,0,c){
			string t;cin>>t;
			lst[i].push_back(t);
		}
	}

	map<string,int> mp;
	rep(i,0,n){
		mp[name[i]]=i;
	}

	vector<ll> edge(n);
	rep(i,0,n){
		for(string &j:lst[i]){
			edge[i]|=1LL<<mp[j];
		}
	}

	if(n<=0){
		int ans=0;
		rep(i,0,1<<n){
			if(!(i&1))continue;
			bool mode=1;
			int tmp=0;
			rep(j,0,n){
				if(i>>j&1){
					tmp+=b[j];
					if((edge[j]&i)>0){
						mode=0;
						break;
					}
				}
			}
			if(mode){
				chmax(ans,tmp);
			}
		}
		cout<<ans<<'\n';
	}

	int m=(n+1)/2;
	vector<int> d(1<<m,(int)-1e9);
	rep(i,0,1<<m){
		if(!(i&1))continue;
		bool mode=1;
		int tmp=0;
		rep(j,0,m){
			if(i>>j&1){
				tmp+=b[j];
				if((edge[j]&i)>0){
					mode=0;
					break;
				}
			}
		}
		if(mode){
			chmax(d[i],tmp);
		}
	}

	rep(i,0,m){
		int mask=1<<i;
		rep(j,0,1<<m){
			if(mask&j)continue;
			chmax(d[mask|j],d[j]);
		}
	}

	int ans=0;
	rep(i,0,1<<(n-m)){
		bool mode=1;
		int tmp=0;
		ll sihai=0;
		rep(j,0,n-m){
			if(i>>j&1){
				tmp+=b[j+m];
				if(((edge[j+m]>>m)&i)>0){
					mode=0;
					break;
				}
				sihai|=edge[j+m];
			}
		}
		if(!mode)continue;
		sihai&=(1LL<<m)-1;
		sihai^=(1LL<<m)-1;
		chmax(ans,tmp+d[sihai]);
	}
	cout<<ans<<'\n';
	
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	while(true){
		int n; cin >> n;
		if(n==0)break;
		solve(n);
	}
}
accuracy: 1
time_ms: 2130
memory_kb: 7624
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll ILL=2167167167167167167;
const int INF=100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
bool yneos(bool a,bool upp=false){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;}
template<class T> void vec_out(vector<T> &p,int ty=0){
    if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
    else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}
template<class T> T square(T a){return a * a;}

int N;

void solve();
// CITRUS CURIO CITY / FREDERIC
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (cin >> N, N) solve();
}

void solve(){
    vector<string> A(N);
    vector<int> B(N);
    vector<vector<string>> D(N);
    rep(i, 0, N){
        cin >> A[i] >> B[i];
        int c;
        cin >> c;
        D[i].resize(c);
        for (auto &a : D[i]) cin >> a;
    }
    map<string, int> m;
    rep(i, 0, N) m[A[i]] = i;
    vector G(N, vector<int>(N));
    rep(i, 0, N) for (auto x : D[i]) G[i][m[x]] = 1;
    auto f = [&](int l, int r) -> vector<int> {
        vector<int> ban(1 << (r - l));
        rep(i, l, r) rep(j, l, r) if (G[i][j]){
            ban[(1 << (i - l)) +  (1 << (j - l))] = 1;
        }
        rep(i, 0, r - l) rep(j, 0, 1 << (r - l)) if (j & (1 << i)){
            if (ban[j - (1 << i)]) ban[j] = 1;
        }
        // vec_out(ban);
        rep(i, 0, 1 << (r - l)){
            if (ban[i] == 1) ban[i] = -INF;
            else{
                rep(k, 0, r - l) if (i & (1 << k)) ban[i] += B[l + k];
            }
        }
        if (l == 0) return ban;
        rep(i, 0, r - l) rep(j, 0, 1 << (r - l)) if (j & (1 << i)){
            chmax(ban[j], ban[j - (1 << i)]);
        }
        // vec_out(ban);
        return ban;
    };
    int mid = N / 2;
    auto X = f(0, mid);
    auto Y = f(mid, N);
    int ans = 0;
    vector<int> cov(1 << mid);
    rep(i, 0, mid) rep(j, mid, N) if (G[i][j]) cov[1 << i] += (1 << (j - mid));
    rep(i, 0, mid) rep(j, 0, 1 << mid) if (j & (1 << i)) cov[j] = (cov[j] | cov[j - (1 << i)]);
    rep(i, 0, 1 << mid) if (i & 1) {
        if (chmax(ans, X[i] + Y[(1 << (N - mid)) - 1 - cov[i]])){
            // cout << i << " " << cov << endl;
        }
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 4520
memory_kb: 15748
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector<string> A(N);
    vector<ll> B(N);
    vector<vector<string>> C(N);
    map<string,int> mp;
    rep(i,0,N) {
        cin >> A[i] >> B[i];
        mp[A[i]] = i;
        int c;
        cin >> c;
        rep(j,0,c) {
            string s;
            cin >> s;
            C[i].push_back(s);
        }
    }
    vector<ll> G(N,0);
    rep(i,0,N) {
        for (string s : C[i]) {
            G[i] |= (1LL << mp[s]);
        }
    }
    int M = (N + 1) / 2, K = N - M;
    vector<ll> LC(1<<M,0), RC(1<<K,0), L(1<<M,-INF), R(1<<K,-INF);
    rep(i,0,1<<M) {
        ll X = 0, COUNT = 0;
        rep(j,0,M) {
            if ((i>>j)&1) {
                X |= G[j];
                COUNT += B[j];
            }
        }
        if ((X & i) == 0) {
            LC[i] = X;
            L[i] = COUNT;
        }
    }
    rep(i,0,1<<K) {
        ll X = 0, COUNT = 0;
        rep(j,0,K) {
            if ((i>>j)&1) {
                X |= G[j+M];
                COUNT += B[j+M];
            }
        }
        if ((X & ((ll)i<<M)) == 0) {
            RC[i] = X;
            R[i] = COUNT;
        }
    }
    rep(i,0,K) {
        rep(j,0,1<<K) {
            chmax(R[j|(1<<i)],R[j]);
        }
    }
    ll ANS = -INF;
    rep(i,0,1<<M) {
        if ((i&1)==0) continue;
        if (L[i] == -INF) continue;
        ll Comp = (1LL<<N) - 1 - LC[i];
        Comp >>= M;
        chmax(ANS,L[i]+R[Comp]);
    }
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 6060
memory_kb: 36140
final_rank: 20

code_snippet: #include <bits/stdc++.h>




namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa


namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
using namespace zawa;

void input(int n, std::vector<std::vector<bool>>& g, std::vector<int>& b) {
    std::vector<std::string> a(n);
    std::vector adj(n, std::vector<std::string>{});
    for (int i{} ; i < n ; i++) {
        std::cin >> a[i] >> b[i];
        int deg; std::cin >> deg;
        adj[i].resize(deg);
        for (auto& s : adj[i]) std::cin >> s;
    }
    for (int i{} ; i < n ; i++) {
        for (int j{} ; j < (int)adj[i].size() ; j++) {
            for (int k{} ; k < n ; k++) {
                if (a[k] == adj[i][j]) {
                    g[i][k] = true;
                    break;
                }
            }
        }
    }
}

bool solve() {
    int n; std::cin >> n;
    if (n == 0) return false;
    std::vector g(n, std::vector<bool>(n));
    std::vector<int> b(n);
    input(n, g, b);
    int p{n / 2}, q{n - p};
    std::vector<int> sL(1 << p);
    std::vector<bool> canL(1 << p);
    {
        std::vector<int> ng(p);
        for (int i{} ; i < p ; i++) {
            for (int j{i + 1} ; j < p ; j++) {
                if (g[i][j]) ng[i] |= (1 << j);
            }
        }
        for (int bit{} ; bit < (1 << p) ; bit++) {
            bool ok{true};
            for (int i{} ; i < p ; i++) {
                if (bit & (1 << i)) {
                    ok &= (bit & ng[i]) == 0;
                    sL[bit] += b[i];
                }
            }
            if (ok) canL[bit] = true;
        }
    }
    std::vector<int> sR(1 << q);
    std::vector<bool> canR(1 << q);
    {
        std::vector<int> ng(q);
        for (int i{} ; i < q ; i++) {
            for (int j{i + 1} ; j < q ; j++) {
                if (g[p + i][p + j]) ng[i] |= (1 << j);
            }
        }
        for (int bit{} ; bit < (1 << q) ; bit++) {
            bool ok{true};
            for (int i{} ; i < q ; i++) {
                if (bit & (1 << i)) {
                    ok &= (bit & ng[i]) == 0;
                    sR[bit] += b[p + i];
                }
            }
            if (ok) canR[bit] = true;
        }
    }
    std::vector<int> R(1 << q);
    for (int bit{} ; bit < (1 << q) ; bit++) {
        if (canR[bit]) R[bit] = sR[bit];
    }
    for (int i{} ; i < q ; i++) {
        for (int bit{} ; bit < (1 << q) ; bit++) {
            if (!(bit & (1 << i))) {
                int tar{bit | (1 << i)};
                R[tar] = std::max(R[tar], R[bit]);
            }
        }
    } 
    std::vector<int> ngLR(p);
    for (int i{} ; i < p ; i++) {
        for (int j{} ; j < q ; j++) {
            if (g[i][p + j]) {
                ngLR[i] |= (1 << j);
            }
        }
    }
    int ans{};
    for (int bit{} ; bit < (1 << p) ; bit++) {
        // 未来の自分へ、このコードを使う時はこの行を消すこと
        if (!(bit & (1 << 0))) continue;
        if (!canL[bit]) continue;
        int ok{};
        for (int i{} ; i < p ; i++) if (bit & (1 << i)) {
            ok |= ngLR[i];
        }
        ok = ((1 << q) - 1) ^ ok;
        int L{sL[bit]};
        int val{L + R[ok]};
        ans = std::max(ans, val);
    }
    std::cout << ans <<'\n';
    return true;
}

int main() {
    SetFastIO();

    for (int i{} ; solve() ; i++) {
    }
}
accuracy: 1
time_ms: 6270
memory_kb: 15796
final_rank: 15

code_snippet: #include <bits/stdc++.h>




namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa


namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
using namespace zawa;

void input(int n, std::vector<std::vector<bool>>& g, std::vector<int>& b) {
    std::vector<std::string> a(n);
    std::vector adj(n, std::vector<std::string>{});
    for (int i{} ; i < n ; i++) {
        std::cin >> a[i] >> b[i];
        int deg; std::cin >> deg;
        adj[i].resize(deg);
        for (auto& s : adj[i]) std::cin >> s;
    }
    for (int i{} ; i < n ; i++) {
        for (int j{} ; j < (int)adj[i].size() ; j++) {
            for (int k{} ; k < n ; k++) {
                if (a[k] == adj[i][j]) {
                    g[i][k] = true;
                    break;
                }
            }
        }
    }
}

bool solve() {
    int n; std::cin >> n;
    if (n == 0) return false;
    std::vector g(n, std::vector<bool>(n));
    std::vector<int> b(n);
    input(n, g, b);
    int p{n / 2}, q{n - p};
    std::vector<int> sL(1 << p);
    std::vector<bool> canL(1 << p);
    {
        std::vector<int> ng(p);
        for (int i{} ; i < p ; i++) {
            for (int j{i + 1} ; j < p ; j++) {
                if (g[i][j]) ng[i] |= (1 << j);
            }
        }
        for (int bit{} ; bit < (1 << p) ; bit++) {
            bool ok{true};
            for (int i{} ; i < p ; i++) {
                if (bit & (1 << i)) {
                    ok &= (bit & ng[i]) == 0;
                    sL[bit] += b[i];
                }
            }
            if (ok) canL[bit] = true;
        }
    }
    std::vector<int> sR(1 << q);
    std::vector<bool> canR(1 << q);
    {
        std::vector<int> ng(q);
        for (int i{} ; i < q ; i++) {
            for (int j{i + 1} ; j < q ; j++) {
                if (g[p + i][p + j]) ng[i] |= (1 << j);
            }
        }
        for (int bit{} ; bit < (1 << q) ; bit++) {
            bool ok{true};
            for (int i{} ; i < q ; i++) {
                if (bit & (1 << i)) {
                    ok &= (bit & ng[i]) == 0;
                    sR[bit] += b[p + i];
                }
            }
            if (ok) canR[bit] = true;
        }
    }
    std::vector<int> R(1 << q);
    for (int bit{} ; bit < (1 << q) ; bit++) {
        if (canR[bit]) R[bit] = sR[bit];
    }
    for (int i{} ; i < q ; i++) {
        for (int bit{} ; bit < (1 << q) ; bit++) {
            if (!(bit & (1 << i))) {
                int tar{bit | (1 << i)};
                R[tar] = std::max(R[tar], R[bit]);
            }
        }
    } 
    std::vector<int> ngLR(p);
    for (int i{} ; i < p ; i++) {
        for (int j{} ; j < q ; j++) {
            if (g[i][p + j]) {
                ngLR[i] |= (1 << j);
            }
        }
    }
    int ans{};
    for (int bit{} ; bit < (1 << p) ; bit++) {
        if (!(bit & (1 << 0))) continue;
        if (!canL[bit]) continue;
        int ok{};
        for (int i{} ; i < p ; i++) if (bit & (1 << i)) {
            ok |= ngLR[i];
        }
        ok = ((1 << q) - 1) ^ ok;
        int L{sL[bit]};
        int val{L + R[ok]};
        ans = std::max(ans, val);
    }
    std::cout << ans <<'\n';
    return true;
}

int main() {
    SetFastIO();

    for (int i{} ; solve() ; i++) {
    }
}
accuracy: 1
time_ms: 6270
memory_kb: 15808
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	while (true) {
		int N;
		cin >> N;
		if (N == 0) {
			break;
		}
		vector<int> W(N);
		vector<string> S(N);
		vector<vector<string> > T(N);
		for (int i = 0; i < N; i++) {
			int c;
			cin >> S[i] >> W[i] >> c;
			T[i].resize(c);
			for (int j = 0; j < c; j++) {
				cin >> T[i][j];
			}
		}
		vector<uint64_t> G(N);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < T[i].size(); j++) {
				int pos = find(S.begin(), S.end(), T[i][j]) - S.begin();
				G[i] |= uint64_t(1) << pos;
			}
		}
		int m = N / 2;
		vector<int> dp(1 << (N - m));
		for (int i = 0; i < (1 << (N - m)); i++) {
			bool valid = true;
			int weight = 0;
			for (int j = 0; j < N - m; j++) {
				if ((i >> j) & 1) {
					if (((G[j + m] >> m) & i) != 0) {
						valid = false;
					}
					weight += W[j + m];
				}
			}
			if (valid) {
				dp[i] = weight;
			}
		}
		for (int i = 0; i < N - m; i++) {
			for (int j = 0; j < (1 << (N - m)); j++) {
				if ((j >> i) & 1) {
					dp[j] = max(dp[j], dp[j ^ (1 << i)]);
				}
			}
		}
		int ans = 0;
		for (int i = 1; i < (1 << m); i += 2) {
			bool valid = true;
			int adj = 0;
			int weight = 0;
			for (int j = 0; j < m; j++) {
				if ((i >> j) & 1) {
					if ((G[j] & i) != 0) {
						valid = false;
					}
					adj |= (G[j] >> m);
					weight += W[j];
				}
			}
			if (valid) {
				ans = max(ans, weight + dp[((1 << (N - m)) - 1) - adj]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 4630
memory_kb: 7664
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

mt19937 engine(42);

template<class T>
bool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}

bool is_end = false;

ll calc_cc(vector<ll> &connected, vector<ll> &weight, vector<vector<ll>> &graph, bool has_root = false)
{
    ll N = weight.size();
    ll ret = 0;
    
    for (int loop = 0; loop < 100000; ++loop)
    {
        shuffle(connected.begin(), connected.end(), engine);
        
        vector<bool> skip(N, false);
        ll tmp = 0;
        
        if (has_root)
        {
            tmp += weight[0];
            skip[0] = true;
            for (int j = 0; j < N; ++j)
            {
                if (graph[0][j]) {skip[j] = true;}
            }
        }
        
        for (auto v : connected)
        {
            if (skip[v]) {continue;}
            
            tmp += weight[v];
            for (int j = 0; j < N; ++j)
            {
                if (graph[v][j]) {skip[j] = true;}
            }
        }
        
        chmax(ret, tmp);
    }
    
    return ret;
}

void calc()
{
    ll N; cin >> N;
    if (N == 0)
    {
        is_end = true;
        return;
    }
    
    vector<vector<string>> adj(N);
    map<string, ll> idx;
    vector<ll> weight(N);
    ll num = 0;
    for (int i = 0; i < N; ++i)
    {
        string S; cin >> S;
        idx[S] = num;
        num++;
        ll W, C; cin >> W >> C;
        weight[i] = W;
        for (int j = 0; j < C; ++j)
        {
            string T; cin >> T;
            adj[i].emplace_back(T);
        }
    }
    
    vector<vector<ll>> G(N, vector<ll>(N, 0));
    for (int i = 0; i < N; ++i)
    {
        for (auto s : adj[i])
        {
            ll j = idx[s];
            G[i][j] = G[j][i] = 1;
        }
    }
    
    vector<bool> used(N, false);
    ll res = 0;
    
    for (int i = 0; i < N; ++i)
    {
        if (used[i]) {continue;}
        
        vector<ll> connected;
        
        queue<ll> que;
        que.push(i);
        used[i] = true;
        
        while (!que.empty())
        {
            ll v = que.front();
            que.pop();
            connected.emplace_back(v);
            
            for (int j = 0; j < N; ++j)
            {
                if (G[v][j] && !used[j])
                {
                    que.push(j);
                    used[j] = true;
                }
            }
        }
        
        res += calc_cc(connected, weight, G, i == 0);
    }
    
    cout << res << endl;
    
    return;
}

int main() {
    
    while (!is_end)
    {
        calc();
    }
    
    return 0;
}
accuracy: 1
time_ms: 6430
memory_kb: 3616
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll=vector<ll>;
using vvll=vector<vll>;
using vd =vector<double>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}


ll MW(vvll &G,vll& W){
    ll N=G.size();
    ll N1=N/2;
    ll N2=N-N1;
    vll V11(N1),V12(N1),V22(N2);
    rep(i,N){
        for(auto j:G[i]){
            if(i<N1&&j<N1)V11[i]|=(1<<j);
            if(i<N1&&j>=N1)V12[i]|=(1<<(j-N1));
            if(i>=N1&&j>=N1)V22[i-N1]|=(1<<(j-N1));
        }
    }
    vll DP(1<<N2);
    //N2側内で求める
    rep(bit,(1<<N2)){
        ll res=0;
        ll n=0;
        rep(i,N2){
            if(bit&(1<<i)){
                res+=W[N1+i];
                n|=V22[i];
            }
        }
        if(!(n&bit)){
            chmax(DP[bit],res);
        }
        rep(i,N2){
            if(bit&(1<<i)){

            }
            else{
                chmax(DP[bit|(1<<i)],DP[bit]);
            }
        }
    }
    ll an=0;
    //N1内で求めて, 結合
    rep(bit,(1<<N1)){
        //if(!(bit&1))continue;
        ll res=0;
        ll n1=0,n2=0;
        rep(i,N1){
            if(bit&(1<<i)){
                res+=W[i];
                n1|=V11[i];
                n2|=V12[i];
            }
        }
        if(!(n1&bit)){
            chmax(an,res+DP[n2^((1<<N2)-1)]);
        }
    }
    return an;
}

int main() {
    while(1){
        ll N;
        cin>>N;
        
        if(N==0)return 0;
        vector<string> A(N);
        vll B(N);
        vector<vector<ll>> C(N);
        vector<vector<string>> D(N);
        map<string,ll> M; 
        rep(i,N){
            cin>>A[i]>>B[i];
            ll c;
            cin>>c;
            rep(j,c){
                string S;
                cin>>S;
                D[i].push_back(S);
            }
            M[A[i]]=i;
        }
        
        rep(i,N){
            ll c=D[i].size();
            rep(j,c){
                C[i].push_back(M[D[i][j]]);
            }
        }
        ll b=B[0];
        B[0]=1e10;
        //cout<<1<<endl;
        ll an=MW(C,B);
        cout<<an-B[0]+b<<endl;
        
    }
    
}
accuracy: 1
time_ms: 7980
memory_kb: 11804
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

void solve(int n) {
  string as[40];
  int bs[40], cs[40];
  // vector< string > as(n);
  // vector< int > bs(n);
  // vector< int > cs(n);
  vector< vector< string > > dss(n);

  map< string, int > to_node;
  for (int i = 0; i < n; i++) {
    cin >> as[i];
    to_node[as[i]] = i;

    cin >> bs[i] >> cs[i];
    dss[i].resize(cs[i]);
    for (auto &d: dss[i]) {
      cin >> d;
    }
  }

  int g[40][40] = {};
  // auto g = vector(n, vector(n, 0));
  for (int v = 0; v < n; v++) {
    for (const auto &d: dss[v]) {
      int u = to_node[d];
      g[v][u] = 1;
    }
  }

  pair pre(0, n / 2), post(n / 2, n); // [first, second)

  bitset< (1 << 20) > pre_err;
  // int pre_err[1 << 20] = {};
  for (int u = pre.first; u < pre.second; u++) {
    for (int v = u + 1; v < pre.second; v++) {
      if (g[u][v] == 0) continue;
      pre_err[(1 << u) | (1 << v)] = 1;
    }
  }
  for (int bit = 0; bit < 1 << (pre.second - pre.first); bit++) {
    if (not pre_err[bit]) continue;
    for (int i = 0; i < (pre.second - pre.first); i++) {
      pre_err[bit | (1 << i)] = 1;
    }
  }

  bitset< (1 << 20) > post_err;
  // int post_err[1 << 20] = {};
  for (int u = post.first; u < post.second; u++) {
    for (int v = u + 1; v < post.second; v++) {
      if (g[u][v] == 0) continue;
      post_err[(1 << (u - post.first)) | (1 << (v - post.first))] = 1;
    }
  }
  for (int bit = 0; bit < 1 << (post.second - post.first); bit++) {
    if (not post_err[bit]) continue;
    for (int i = 0; i < (post.second - post.first); i++) {
      post_err[bit | (1 << i)] = 1;
    }
  }

  int dp[1 << 20] = {};
  // vector< int > dp(1 << (post.second - post.first), 0);
  { // precalc
    int size = post.second - post.first;
    for (int bit = 0; bit < (1 << size); bit++) {

      // check edge is not exist
      // bool edge_exist = false;
      // for (int u = 0; u < size; u++) {
      //   if ((bit & (1 << u)) == 0) continue;
      //   for (int v = u + 1; v < size; v++) {
      //     if ((bit & (1 << v)) == 0) continue;
      //     if (g[u + post.first][v + post.first] == 0) continue;
      //     edge_exist = true;
      //     break;
      //   }
      // }
      // if (edge_exist) continue;
      if (post_err[bit]) continue;

      for (int v = 0; v < size; v++) {
        if ((bit & (1 << v)) == 0) continue;
        dp[bit] += bs[v + post.first];
      }
    }
    
    for (int bit = 0; bit < (1 << size); bit++) {
      for (int i = 0; i < size; i++) {
        dp[bit | (1 << i)] = max(dp[bit], dp[bit | (1 << i)]);
      }
    }

  } // precalc end

  int ans = 0;
  int size = pre.second - pre.first;
  for (int bit = 0; bit < (1 << size); bit++) {
    if ((bit & 1) == 0) continue; // my country

    // bool edge_exist = false;
    // for (int u = 0; u < size; u++) {
    //   if ((bit & (1 << u)) == 0) continue;
    //   for (int v = u + 1; v < size; v++) {
    //     if ((bit & (1 << v)) == 0) continue;
    //     if (g[u][v] == 0) continue;
    //     edge_exist = true;
    //     break;
    //   }
    // }
    // if (edge_exist) continue;
    if (pre_err[bit]) continue;

    int subset = 0;
    for (int v = post.first; v < post.second; v++) {
      bool contain_subset = true;
      for (int u = pre.first; u < pre.second; u++) {
        if ((bit & (1 << u)) == 0) continue;
        if (g[u][v] == 0) continue;
        contain_subset = false;
        break;
      }
      if (contain_subset) subset |= (1 << (v - post.first));
    }

    int sum = 0;
    for (int v = pre.first; v < pre.second; v++) {
      if ((bit & (1 << v)) == 0) continue;
      sum += bs[v];
    }

    sum += dp[subset];
    ans = max(ans, sum);
  }

  cout << ans << endl;
}

int main() {
  int n;
  while (cin >> n, n) {
    solve(n);
  }
}
accuracy: 1
time_ms: 7240
memory_kb: 8028
final_rank: 14

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

template <class T>
bool chmin(T &a, const T b) {
    if (b < a) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int n;

int solve() {
    map<string, int> mp;
    vector<int> b(n);
    vector<vector<string>> d(n, vector<string>(0));
    rep(i,n,0) {
        string a; cin >> a;
        mp[a] = i;
        cin >> b[i];
        int c; cin >> c;
        rep(j,c,0) {
            string s; cin >> s;
            d[i].emplace_back(move(s));
        }
    }
    vector<vector<int>> da(n, vector<int>(0));
    rep(i,n,0) {
        for(auto &j : d[i]) {
            da[i].emplace_back(mp[j]);
        }
    }
    int m1 = n/2;
    vector<int> dp1(1<<m1,-1);
    dp1[0] = 0;
    rep(i,1<<m1,1) {
        int k = 0;
        while(((i>>k)&1) == 0) {
            ++k;
        }
        bool flg = false;
        for(auto &j : da[k]) {
            if(j < m1 && (i>>j)&1) flg = true;
        }
        if(!flg) dp1[i] = dp1[i^(1<<k)] + b[k];
    }
    rep(i,1<<m1,0) if(i%2 == 0) dp1[i] = -1;
    rep(i,1<<m1,0) {
        rep(j,m1,0) {
            if((i>>j)&1) chmax(dp1[i], dp1[i^(1<<j)]);
        }
    }
    int m2 = n - m1;
    vector<int> dp2(1<<m2,-1);
    vector<int> mask(1<<m2, 0);
    dp2[0] = 0;
    rep(i,1<<m2,1) {
        int k = 0;
        while(((i>>k)&1) == 0) {
            ++k;
        }
        mask[i] = mask[i^(1<<k)];
        bool flg = false;
        for(auto &j : da[k+m1]) {
            if(m1 <= j && (i>>(j-m1))&1) flg = true;
            if(m1 > j) mask[i] |= 1<<j;
        }
        if(!flg) dp2[i] = dp2[i^(1<<k)] + b[k+m1];
    }
    int ans = 0;
    // vdeb(dp1);
    // vdeb(dp2);
    // vdeb(mask);
    const int kk = (1<<m1)-1;
    rep(i,1<<m2,0) {
        int j = mask[i]^kk;
        if(dp2[i] != -1 && dp1[j] != -1) {
            chmax(ans, dp2[i] + dp1[j]);
        }
    }
    return ans;
}

int main() {
    vector<int> ans;
    while(1) {
        cin >> n;
        if(n == 0) break;
        ans.emplace_back(solve());
    }
    for(auto &i : ans) cout << i << endl;
}
accuracy: 1
time_ms: 3870
memory_kb: 15700
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <typename T>
T weighted_maximum_independent_set(const std::vector<std::vector<int>>& G, const std::vector<T>& w) {
    const int n = G.size();
    const int n1 = n / 2;
    const int n2 = n - n1;
    std::vector<T> neighbors11(n1), neighbors12(n1), neighbors22(n2);
    for (int i = 0; i < n; ++i) {
        for (int j : G[i]) {
            if (i < n1 && j < n1) neighbors11[i] |= 1 << j;
            if (i < n1 && j >= n1) neighbors12[i] |= 1 << (j - n1);
            if (i >= n1 && j >= n1) neighbors22[i - n1] |= 1 << (j - n1);
        }
    }
    std::vector<int> dp(1 << n2);
    for (int S = 0; S < 1 << n2; ++S) {
        T s = 0;
        int neighbor = 0;
        for (int i = 0; i < n2; ++i) {
            if (S >> i & 1) {
                s += w[n1 + i];
                neighbor |= neighbors22[i];
            }
        }
        if (!(neighbor & S)) {
            dp[S] = std::max(dp[S], s);
        }
        for (int i = 0; i < n2; ++i) {
            if (!(S >> i & 1)) {
                dp[S | (1 << i)] = std::max(dp[S | (1 << i)], dp[S]);
            }
        }
    }
    T ans = std::numeric_limits<T>::min();
    for (int S = 0; S < 1 << n1; ++S) {
        if (!(S & 1)) continue;
        int s = 0;
        int neighbor1 = 0;
        int neighbor2 = 0;
        for (int i = 0; i < n1; ++i) {
            if (S >> i & 1) {
                s += w[i];
                neighbor1 |= neighbors11[i];
                neighbor2 |= neighbors12[i];
            }
        }
        if (!(neighbor1 & S)) {
            ans = std::max(ans, s + dp[neighbor2 ^ ((1 << n2) - 1)]);
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    while (true) {
        int N;
        cin >> N;
        if (N == 0) break;
        vector<string> A(N);
        vector<int> B(N);
        vector<vector<string>> D(N);
        rep(i, 0, N) {
            int C;
            cin >> A[i] >> B[i] >> C;
            rep(_, 0, C) {
                string s;
                cin >> s;
                D[i].push_back(s);
            }
        }
        if (N == 1) {
            cout << B[0] << endl;
            continue;
        }
        map<string, int> idx;
        rep(i, 0, N) idx[A[i]] = i;
        vector<vector<int>> G(N);
        rep(i,0,N) {
            for (auto& s : D[i]) {
                G[i].push_back(idx[s]);
            }
        }
        cout << weighted_maximum_independent_set(G, B) << "\n";
    }
}
accuracy: 1
time_ms: 5760
memory_kb: 7720
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    while (true) {
        int N;
        cin >> N;
        if (N == 0) break;
        vector<string> A(N);
        vector<int> B(N);
        vector<vector<string>> D(N);
        rep(i,0,N) {
            int C;
            cin >> A[i] >> B[i] >> C;
            rep(_,0,C) {
                string s;
                cin >> s;
                D[i].push_back(s);
            }
        }
        if (N == 1) {
            cout << B[0] << endl;
            continue;
        }
        map<string, int> idx;
        rep(i,0,N) idx[A[i]] = i;
        const int N1 = N/2;
        const int N2 = N-N1;
        vector<ll> neighbors11(N1), neighbors12(N1), neighbors22(N2);
        rep(i,0,N) {
            for (auto& s : D[i]) {
                int j = idx[s];
                if (i < N1 && j < N1) neighbors11[i] |= 1<<j;
                if (i < N1 && j >= N1) neighbors12[i] |= 1<<(j-N1);
                if (i >= N1 && j >= N1) neighbors22[i-N1] |= 1<<(j-N1);
            }
        }
        vector<int> dp(1<<N2);
        rep(S,0,1<<N2) {
            int s = 0;
            int neighbor = 0;
            rep(i,0,N2) {
                if (S>>i&1) {
                    s += B[N1+i];
                    neighbor |= neighbors22[i];
                }
            }
            if (!(neighbor & S)) {
                chmax(dp[S], s);
            }
            rep(i,0,N2) {
                if (!(S>>i&1)) {
                    chmax(dp[S|(1<<i)], dp[S]);
                }
            }
        }
        int ans = 0;
        rep(S,0,1<<N1) {
            if (!(S & 1)) continue;
            int s = 0;
            int neighbor1 = 0;
            int neighbor2 = 0;
            rep(i,0,N1) {
                if (S>>i&1) {
                    s += B[i];
                    neighbor1 |= neighbors11[i];
                    neighbor2 |= neighbors12[i];
                }
            }
            if (!(neighbor1 & S)) {
                chmax(ans, s + dp[neighbor2 ^ ((1<<N2)-1)]);
            }
        }
        cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 7000
memory_kb: 7608
final_rank: 12

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

/* macro */

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define Bit(n) (1LL << (n))

/* macro end */

/* template */

namespace ebi {

#ifdef LOCAL
#define debug(...)                                                      \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {
    return os << pa.first << " " << pa.second;
}

template <typename T1, typename T2>
std::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {
    return os >> pa.x >> pa.y;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
    for (std::size_t i = 0; i < vec.size(); i++)
        os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}

template <typename T>
std::istream &operator>>(std::istream &os, std::vector<T> &vec) {
    for (T &e : vec) std::cin >> e;
    return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {
    if (opt) {
        os << opt.value();
    } else {
        os << "invalid value";
    }
    return os;
}

using size_t = std::size_t;
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;

template <class T>
void fill(std::vector<T> &v) {
    for (T &e : v) std::cin >> e;
}

template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b) {
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}

template <class T>
T lcm(T a, T b) {
    a /= gcd(a, b);
    return a * b;
}

template <class T>
T Pow(T x, i64 n) {
    T res = 1;
    while (n > 0) {
        if (n & 1) res = res * x;
        x = x * x;
        n >>= 1;
    }
    return res;
}

template <class T>
T mod_pow(T x, i64 n, i64 mod) {
    T res = 1;
    while (n > 0) {
        if (n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

template <class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}

template <class T>
struct Edge {
    int to;
    T cost;
    Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}
};

template <class T>
struct Graph : std::vector<std::vector<Edge<T>>> {
    using std::vector<std::vector<Edge<T>>>::vector;
    void add_edge(int u, int v, T w, bool directed = false) {
        (*this)[u].emplace_back(v, w);
        if (directed) return;
        (*this)[v].emplace_back(u, w);
    }
};

struct graph : std::vector<std::vector<int>> {
    using std::vector<std::vector<int>>::vector;
    void add_edge(int u, int v, bool directed = false) {
        (*this)[u].emplace_back(v);
        if (directed) return;
        (*this)[v].emplace_back(u);
    }
};

constexpr i64 LNF = std::numeric_limits<i64>::max() / 4;

constexpr int INF = std::numeric_limits<int>::max() / 2;

const std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};
const std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};

}  // namespace ebi

namespace ebi {

void main_() {
    int n;
    while (std::cin >> n, n != 0) {
        std::map<std::string, int> map;
        std::vector<int> powers(n);
        std::vector table(n, std::vector<std::string>());
        rep(i, 0, n) {
            std::string s;
            std::cin >> s >> powers[i];
            map[s] = i;
            int c;
            std::cin >> c;
            while (c--) {
                std::string t;
                std::cin >> t;
                table[i].emplace_back(t);
            }
        }
        if (n == 1) {
            std::cout << powers.back() << '\n';
            continue;
        }
        const int sz = n / 2;
        graph front(n), back(n);
        rep(i, 0, n) {
            for (auto s : table[i]) {
                assert(map.find(s) != map.end());
                int idx = map[s];
                if (idx < sz) {
                    front[i].emplace_back(idx);
                } else {
                    back[i].emplace_back(idx);
                }
            }
        }
        std::vector<int> dp(1 << sz, -INF);
        {
            std::vector<int> a(sz, 0);
            rep(i, 0, sz) {
                for (auto v : front[i]) {
                    a[i] |= 1 << v;
                }
            }
            rep(bit, 0, 1 << sz) {
                int sum = 0;
                int ret = 0;
                rep(i, 0, sz) {
                    if (bit & (1 << i)) {
                        sum += powers[i];
                        ret |= a[i];
                        chmax(dp[bit], dp[bit ^ (1 << i)]);
                    }
                }
                if ((bit & ret) == 0 && (bit & 1)) chmax(dp[bit], sum);
            }
        }
        int ans = 0;
        {
            const int m = n - sz;
            const int mask = (1 << sz) - 1;
            std::vector<int> a(m, 0);
            std::vector<int> b(m, 0);
            rep(i, sz, n) {
                for (auto v : back[i]) {
                    a[i - sz] |= 1 << (v - sz);
                }
                for (auto v : front[i]) {
                    b[i - sz] |= 1 << v;
                }
            }
            rep(bit, 0, 1 << m) {
                int sum = 0;
                int ret = 0;
                int cobit = 0;
                rep(i, 0, m) {
                    if (bit & (1 << i)) {
                        sum += powers[i + sz];
                        ret |= a[i];
                        cobit |= b[i];
                    }
                }
                if ((bit & ret) == 0) {
                    cobit = mask ^ cobit;
                    chmax(ans, dp[cobit] + sum);
                }
            }
        }
        std::cout << ans << '\n';
    }
}

}  // namespace ebi

int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    // std::cin >> t;
    while (t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 5130
memory_kb: 7708
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
using namespace std;

struct Solver{
    const int n;
    vector<vector<int>> G;
    vector<int> b;
    vector<int> useable;
    vector<int> lim;
    int tot = 0;
    int ans = 0;
    Solver(int n): n(n), G(vector<vector<int>>(n)), b(vector<int>(n)),
    useable(vector<int>(n, 0)), lim(vector<int>(n+1, 0)){
        using tup = tuple<string, int, int, vector<string>>;
        vector<tup> lines;
        tup l0;
        for(int i = 0; i < n; i++){
            string a1;
            int b1, c1;
            cin >> a1 >> b1 >> c1;
            vector<string> d1(c1);
            for(auto &it: d1) cin >> it;
            if(i > 0) lines.emplace_back(a1, b1, c1, d1);
            else l0 = {a1, b1, c1, d1};
        }
        sort(lines.begin(), lines.end(), [](const auto &l, const auto &r){
            return get<2>(l) < get<2>(r);
        });
        lines.emplace_back(l0);
        reverse(lines.begin(), lines.end());
        map<string, int> order;
        for(int i = 0; i < n; i++) order[get<0>(lines[i])] = i;
        for(int i = 0; i < n; i++){
            // cout << get<2>(lines[i]) << " ";
            b[i] = get<1>(lines[i]);
            for(auto &it: get<3>(lines[i])){
                G[i].emplace_back(order[it]);
            }
        }
        for(int i = n-1; i >= 0; i--) lim[i] = lim[i+1]+b[i];
        //cout << endl;
    }

    void f(int phase){
        ans = max(ans, tot);
        if(phase == n){
            return;
        }
        if(useable[phase] > 0){
            f(phase+1);
            return;
        }
        if(tot+lim[phase] > ans && G[phase].size() > 0 && phase > 0){
            f(phase+1);
        }
        // if(tot+lim[phase] < ans) return;
        int mx = 0;
        for(auto &it: G[phase]){
            // if(it < phase) continue;
            useable[it]++;
            mx = max(mx, b[it]);
        }
        tot += b[phase];
        f(phase+1);
        tot -= b[phase];
        for(auto &it: G[phase]){
            // if(it < phase) continue;
            useable[it]--;
        }
        return;
    }

    void solve(){
        f(0);
        cout << ans << '\n';
    }
};

int main(){
    while(true){
        int n; cin >> n;
        if(n == 0) break;
        Solver sol(n);
        sol.solve();
    }
    return 0;
}
accuracy: 1
time_ms: 3190
memory_kb: 3692
final_rank: 4