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 long long N = 3e2 + 5;
const long long BIGinf = 1ll << 60;
const long long inf = 1ll << 30;
inline long long read() {
  register long long s = 0;
  register char c = getchar(), lc = '+';
  while (c < '0' || '9' < c) lc = c, c = getchar();
  while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar();
  return lc == '-' ? -s : s;
}
void write(register long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x < 10)
    putchar(x + '0');
  else {
    write(x / 10);
    putchar(x % 10 + '0');
  }
}
inline void print(const register long long x, const register char c = '\n') {
  write(x);
  putchar(c);
}
namespace Dinic {
const long long N = 1e3 + 5;
const long long M = N * N + 5;
const long long inf = 1ll << 60;
long long n, S, T, cur[N], head[N], cnt = -1, deep[N];
struct edge {
  long long nxt, to, v;
} e[2 * M];
struct Queue {
  long long now, dis;
} q[N];
void add_edge(long long u, long long v, long long w) {
  e[++cnt].to = v;
  e[cnt].v = w;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}
void add(long long u, long long v, long long w) {
  add_edge(u, v, w);
  add_edge(v, u, 0);
}
bool bfs() {
  for (long long i = 1; i <= n; i++) cur[i] = head[i];
  memset(deep, 0, sizeof(deep));
  long long h = 1, t = 1;
  q[1].now = S;
  q[1].dis = 0;
  while (h <= t) {
    long long now = q[h].now, dis = q[h++].dis;
    for (long long i = head[now]; ~i; i = e[i].nxt)
      if (e[i].v) {
        long long to = e[i].to;
        if ((!deep[to]) && to != S) {
          deep[to] = dis + 1;
          q[++t].dis = dis + 1;
          q[t].now = to;
        }
      }
  }
  return deep[T];
}
long long dfs(long long now, long long flow) {
  if (!flow) return 0;
  if (now == T) return flow;
  long long ans = 0, to, v, tmp;
  for (long long i = cur[now]; ~i; i = e[i].nxt) {
    cur[now] = i;
    if (to = e[i].to, v = e[i].v, deep[now] < deep[to])
      if (tmp = dfs(to, min(flow, v))) {
        ans += tmp;
        flow -= tmp;
        e[i].v -= tmp;
        e[i ^ 1].v += tmp;
        if (!flow) return ans;
      }
  }
  cur[now] = -1;
  return ans;
}
long long work() {
  if (S == T) return 0;
  long long ans = 0;
  while (bfs()) ans += dfs(S, BIGinf);
  return ans;
}
void send(long long _n, long long _S, long long _T) {
  memset(head, -1, sizeof(head));
  n = _n;
  S = _S;
  T = _T;
}
}  // namespace Dinic
long long SET[N], NUM[N];
signed main() {
  long long n = read(), S = 1, T = 2, cnt = 2, v, sum = 0;
  for (long long i = 1; i <= n; i++) SET[i] = ++cnt;
  for (long long i = 1; i <= n; i++) NUM[i] = ++cnt;
  Dinic::send(cnt, S, T);
  for (long long i = 1; i <= n; i++) {
    long long m = read();
    for (long long j = 1; j <= m; j++) Dinic::add(SET[i], NUM[read()], inf);
  }
  for (long long i = 1; i <= n; i++)
    Dinic::add(S, SET[i], inf - (v = read())), sum += v;
  for (long long i = 1; i <= n; i++) Dinic::add(NUM[i], T, inf);
  print(sum + (Dinic::work() - inf * n));
  return 0;
}