Problem: The Hexadecimal virus loves playing with number sets — intersecting them, uniting them. One beautiful day she was surprised to find out that Scuzzy, her spherical pet cat, united all sets in one and ate the result! Something had to be done quickly and Hexadecimal rushed to the market.

The market has n sets of numbers on sale. The virus wants to buy the following collection of sets: the number of sets in the collection should be exactly the same as the number of numbers in the union of all bought sets. Moreover, Hexadecimal wants to buy the cheapest suitable collection of set.

Yet nothing's so easy! As Mainframe is a kingdom of pure rivalry markets, we know that the union of any k sets contains no less than k distinct numbers (for every positive integer k).

Help the virus choose the suitable collection of sets. The collection can be empty.

Input

The first line contains the only number n (1 ≤ n ≤ 300) — the number of sets available in the market.

Next n lines describe the goods: first we are given mi (1 ≤ mi ≤ n) — the number of distinct numbers in the i-th set, then follow mi numbers — the set's elements. We know that the set's elements are distinct positive integers and they do not exceed n.

The last line contains n integers whose absolute values do not exceed 106 — the price of each set.

Output

Print a single number — the minimum price the virus will have to pay for such a collection of k sets that union of the collection's sets would have exactly k distinct numbers (<image>).

Examples

Input

3
1 1
2 2 3
1 3
10 20 -3


Output

-3


Input

5
2 1 2
2 2 3
2 3 4
2 4 5
2 5 1
1 -1 1 -1 1


Output

0


Input

5
2 1 2
2 2 3
2 3 4
2 4 5
2 5 1
-1 1 -1 1 -1


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
long long n, s, t, head[700], nxt[1000000], len[1000000], to[1000000], cnt = -1;
long long cur[1000000];
void add(long long a, long long b, long long l, bool _rev = false) {
  nxt[++cnt] = head[a];
  to[cnt] = b;
  len[cnt] = l;
  head[a] = cnt;
  if (!_rev) add(b, a, 0, true);
}
long long lev[700];
bool bfs() {
  for (long long i = s; i <= t; i++) {
    lev[i] = 2000000000;
    cur[i] = head[i];
  }
  lev[s] = 0;
  queue<long long> q;
  q.push(s);
  while (!q.empty()) {
    long long now = q.front();
    q.pop();
    if (now == t) return true;
    for (long long i = head[now]; i != -1; i = nxt[i]) {
      if (lev[to[i]] == 2000000000 && len[i]) {
        lev[to[i]] = lev[now] + 1;
        q.push(to[i]);
      }
    }
  }
  return false;
}
long long dfs(long long now, long long flow) {
  if (now == t) return flow;
  long long use = 0;
  for (long long i = cur[now]; i != -1 && flow; i = nxt[i]) {
    cur[now] = i;
    if (lev[to[i]] == lev[now] + 1 && len[i]) {
      long long val = dfs(to[i], min(flow, len[i]));
      if (!val) lev[to[i]] = 2000000000;
      flow -= val;
      use += val;
      len[i] -= val;
      len[i ^ 1] += val;
    }
  }
  return use;
}
signed main() {
  memset(head, -1, sizeof(head));
  scanf("%lld", &n);
  s = 0;
  t = 2 * n + 1;
  for (long long i = 1; i <= n; i++) {
    long long num;
    scanf("%lld", &num);
    for (long long j = 1; j <= num; j++) {
      long long x;
      scanf("%lld", &x);
      add(i, n + x, 2000000000);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long x;
    scanf("%lld", &x);
    add(s, i, 500000000 - x);
    ans += 500000000 - x;
  }
  for (long long i = 1; i <= n; i++) {
    add(n + i, t, 500000000);
  }
  while (bfs()) {
    ans -= dfs(s, 2000000000);
  }
  printf("%lld\n", min(-ans, 0ll));
  return 0;
}