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;
namespace red {
inline long long read() {
  long long x = 0;
  char ch, f = 1;
  for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') f = 0, ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return f ? x : -x;
}
const long long N = 1e5 + 10, inf = 0x3f3f3f3f;
long long n, st, ed, ret, sum;
long long head[N], cnt = 1;
struct point {
  long long nxt, to, val;
  point() {}
  point(const long long &nxt, const long long &to, const long long &val)
      : nxt(nxt), to(to), val(val) {}
} a[N << 1];
inline void link(long long x, long long y, long long z) {
  a[++cnt] = (point){head[x], y, z};
  head[x] = cnt;
  a[++cnt] = (point){head[y], x, 0};
  head[y] = cnt;
}
long long dis[N], cur[N];
queue<long long> q;
inline bool bfs() {
  for (long long i = 1; i <= ed; ++i) dis[i] = 0, cur[i] = head[i];
  q.push(st);
  dis[st] = 1;
  while (!q.empty()) {
    long long now = q.front();
    q.pop();
    for (long long i = head[now]; i; i = a[i].nxt) {
      long long t = a[i].to;
      if (!dis[t] && a[i].val) {
        dis[t] = dis[now] + 1;
        q.push(t);
      }
    }
  }
  return dis[ed];
}
inline long long dfs(long long now, long long c) {
  if (now == ed || !c) return c;
  long long ret = c, f;
  for (long long i = cur[now]; i; i = a[i].nxt) {
    cur[now] = i;
    long long t = a[i].to;
    if (dis[t] == dis[now] + 1) {
      f = dfs(t, min(a[i].val, ret));
      ret -= f;
      a[i].val -= f;
      a[i ^ 1].val += f;
      if (!ret) return c;
    }
  }
  if (ret == c) dis[now] = 0;
  return c - ret;
}
inline void dinic() {
  while (bfs()) ret += dfs(st, inf);
}
inline void main() {
  n = read();
  st = 2 * n + 1, ed = 2 * n + 2;
  for (long long s, x, i = 1; i <= n; ++i) {
    s = read();
    for (long long j = 1; j <= s; ++j) {
      x = read();
      link(i, x + n, inf);
    }
  }
  for (long long x, i = 1; i <= n; ++i) {
    x = read();
    link(st, i, inf - x);
    link(i + n, ed, inf);
    sum += inf - x;
  }
  dinic();
  ret = ret - sum;
  printf("%lld\n", ret);
}
}  // namespace red
signed main() {
  red::main();
  return 0;
}