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;
const int maxn = 305;
int n, m;
inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  return sum;
}
const int Maxn = 605, Maxm = Maxn * Maxn;
const long long Inf = 1ll << 60, zero = 5000000;
long long ans;
struct edge {
  int to, next;
  long long cap;
} e[Maxm];
int h[Maxn], tot, s, t;
inline void add(int u, int v, long long w) {
  e[++tot] = (edge){v, h[u], w};
  h[u] = tot;
  e[++tot] = (edge){u, h[v], 0};
  h[v] = tot;
}
bool vis[Maxn];
int l, r, que[Maxn], d[Maxn], cur[Maxn];
inline bool bfs() {
  l = 0;
  r = 1;
  que[r] = s;
  vis[s] = true;
  do {
    int u = que[++l];
    for (int i = h[u], v; ~i; i = e[i].next)
      if (e[i].cap && !vis[v = e[i].to]) {
        vis[v] = true;
        d[v] = d[u] + 1;
        que[++r] = v;
      }
  } while (l < r);
  return vis[t];
}
long long dfs(int u, long long a) {
  if (u == t || a == 0) return a;
  long long f, flow = 0;
  for (int &i = cur[u], v; ~i; i = e[i].next) {
    if (d[u] + 1 == d[v = e[i].to] && (f = dfs(v, min(a, e[i].cap)))) {
      e[i].cap -= f;
      e[i ^ 1].cap += f;
      flow += f;
      a -= f;
      if (a == 0) break;
    }
  }
  return flow;
}
int main() {
  memset(h, -1, sizeof(h));
  tot = -1;
  n = gi();
  s = n * 2 + 1;
  t = s + 1;
  for (int t, i = 1; i <= n; ++i) {
    t = gi();
    while (t--) add(i, gi() + n, Inf);
  }
  for (int i = 1; i <= n; ++i) add(i + n, t, zero);
  for (int p, i = 1; i <= n; ++i) {
    scanf("%d", &p);
    add(s, i, zero - p);
    ans -= zero - p;
  }
  while (bfs()) {
    memset(vis, 0, sizeof(bool) * (t + 1));
    memcpy(cur, h, sizeof(int) * (t + 1));
    ans += dfs(s, Inf);
  }
  printf("%lld\n", ans);
  return 0;
}