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 = 605;
const int MAXM = 200005;
const long long INF = 100000000000000LL;
const int M = 1000000000;
int n, tot, fst[MAXN], S, T, q[MAXN], h[MAXN];
long long sum = 0;
struct Tedge {
  int y, pre, op;
  long long f;
} g[MAXM];
int get() {
  int f = 0, v = 0;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') break;
  if (ch == '-')
    f = 1;
  else
    v = ch - 48;
  while (isdigit(ch = getchar())) v = v * 10 + ch - 48;
  if (f == 1) return -v;
  return v;
}
void add(int x, int y, long long f) {
  g[++tot].pre = fst[x];
  fst[x] = tot;
  g[tot].y = y;
  g[tot].f = f;
  g[tot].op = tot + 1;
  g[++tot].pre = fst[y];
  fst[y] = tot;
  g[tot].y = x;
  g[tot].f = 0;
  g[tot].op = tot - 1;
}
bool bfs() {
  memset(h, 0, sizeof(h));
  q[1] = S;
  h[S] = 1;
  int f = 1, r = 2;
  while (f != r) {
    for (int i = fst[q[f]]; i != 0; i = g[i].pre)
      if (g[i].f > 0 && !h[g[i].y]) h[g[i].y] = h[q[f]] + 1, q[r++] = g[i].y;
    f++;
  }
  return h[T] != 0;
}
long long dfs(int u, long long flow) {
  if (u == T) return flow;
  long long tmp = flow;
  for (int i = fst[u]; i != 0; i = g[i].pre)
    if (g[i].f > 0 && h[g[i].y] == h[u] + 1) {
      long long d = min(tmp, g[i].f);
      d = dfs(g[i].y, d);
      tmp -= d, g[i].f -= d, g[g[i].op].f += d;
      if (tmp == 0) break;
    }
  if (tmp == flow) h[u] = 0;
  return flow - tmp;
}
long long dinic() {
  long long res = 0;
  while (bfs()) res += dfs(S, INF);
  return res;
}
int main() {
  n = get();
  S = 0;
  T = (n << 1) + 1;
  tot = 0;
  memset(fst, 0, sizeof(fst));
  int x;
  for (int i = 1; i <= n; i++)
    for (int t = get(); t > 0; t--) x = get(), add(i, n + x, INF);
  for (int i = 1; i <= n; i++)
    x = get(), add(S, i, M - x), add(n + i, T, M), sum += (M - x);
  printf("%d\n", dinic() - sum);
  return 0;
}