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;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
inline unsigned long long rnd() {
  return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const long long N = 605, M = 200005, oo = 1e9, inf = 1e18;
long long di[M], nxt[M], ed[M];
long long son[N], nedge, cur[N], deep[N];
inline void aedge(long long a, long long b, long long c) {
  nxt[++nedge] = son[a];
  son[a] = nedge;
  ed[nedge] = b;
  di[nedge] = c;
}
inline void insert(long long a, long long b, long long c) {
  aedge(a, b, c);
  aedge(b, a, 0);
}
inline bool bfs(long long s, long long t) {
  for (long long i = 0; i < N; i++) deep[i] = inf;
  for (long long i = 0; i < N; i++) cur[i] = son[i];
  deep[s] = 0;
  queue<long long> q;
  q.push(s);
  while (!q.empty()) {
    long long now = q.front();
    q.pop();
    for (long long tmp = son[now]; tmp; tmp = nxt[tmp])
      if (deep[ed[tmp]] >= inf && di[tmp]) {
        deep[ed[tmp]] = deep[now] + 1, q.push(ed[tmp]);
      }
  }
  return deep[t] < inf;
}
long long dfs(long long now, long long t, long long limit) {
  if (!limit || now == t) return limit;
  long long flow = 0, f;
  for (long long tmp = cur[now]; tmp; tmp = nxt[tmp]) {
    cur[now] = tmp;
    if (deep[ed[tmp]] == deep[now] + 1 &&
        (f = dfs(ed[tmp], t, min(limit, di[tmp])))) {
      flow += f;
      limit -= f;
      di[tmp] -= f;
      di[((tmp - 1) ^ 1) + 1] += f;
      if (!limit) break;
    }
  }
  return flow;
}
inline long long dinic(long long s, long long t) {
  long long ans = 0;
  while (bfs(s, t)) {
    ans += dfs(s, t, inf);
  }
  return ans;
}
signed main() {
  long long n = read(), s = 2 * n + 1, t = 2 * n + 2;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    long long m = read();
    while (m--) insert(i, n + read(), inf);
  }
  long long sum = 0;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    long long jj = -read();
    insert(s, i, oo + jj);
    insert(n + i, t, oo);
    sum += oo + jj;
  }
  writeln(-(sum - dinic(s, t)));
}