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>
const long long lim = 3e8 + 5;
const long long inf = 0x3f3f3f3f;
long long n, m, s, t, ans, head[10010], to[200010], nxt[200010], w[200010], cnt,
    cur[200010];
void add(const long long& x, const long long& y, const long long& z) {
  to[cnt] = y;
  w[cnt] = z;
  nxt[cnt] = head[x];
  head[x] = cnt++;
}
long long d[10010], que[10010], *hd, *tl;
bool bfs() {
  d[t] = 1;
  hd = tl = que;
  *tl++ = t;
  while (tl != hd) {
    long long x = *hd++;
    for (long long i = head[x]; ~i; i = nxt[i]) {
      if (w[i ^ 1] && !d[to[i]]) {
        *tl++ = to[i];
        d[to[i]] = d[x] + 1;
      }
    }
  }
  return d[s];
}
long long dfs(const long long& x, const long long& low) {
  if (x == t) {
    ans -= low;
    return low;
  }
  for (long long& i = cur[x]; ~i; i = nxt[i])
    if (w[i] && d[to[i]] + 1 == d[x]) {
      long long res = dfs(to[i], w[i] < low ? w[i] : low);
      if (res) {
        w[i] -= res;
        w[i ^ 1] += res;
        return res;
      }
    }
  return 0;
}
signed main() {
  memset(head, -1, sizeof(head));
  scanf("%lld", &n);
  t = n + n + 1, s = 0;
  for (long long m, x, i = 1; i <= n; i++) {
    scanf("%lld", &m);
    while (m--) {
      scanf("%lld", &x);
      add(i, x + n, inf);
      add(x + n, i, 0);
    }
  }
  for (long long i = 1, x; i <= n; i++)
    scanf("%lld", &x), add(s, i, lim - x), add(i, s, 0), ans += lim - x;
  for (long long i = 1; i <= n; i++) add(i + n, t, lim), add(t, i + n, 0);
  while (bfs()) {
    for (long long i = s; i <= t; i++) cur[i] = head[i];
    while (dfs(s, 0x7fffffff))
      ;
    memset(d, 0, sizeof(d));
  }
  printf("%lld", ans < 0 ? 0 : -ans);
  return 0;
}