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;
template <typename T>
inline void read(T &t) {
  t = 0;
  char c = getchar();
  long long f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    t = (t << 3) + (t << 1) + c - '0';
    c = getchar();
  }
  t *= f;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
long long n, st, ed;
struct edge {
  long long v, flw, nxt;
} e[100005 << 1];
long long toop = 1, head[605];
void add_edge(long long u, long long v, long long w) {
  e[++toop] = edge{v, w, head[u]}, head[u] = toop;
  e[++toop] = edge{u, 0, head[v]}, head[v] = toop;
}
long long dep[605], cur[605];
inline bool BFS(long long ss, long long tt) {
  memset(dep, -1, sizeof(dep)), memcpy(cur, head, sizeof(cur));
  queue<long long> q;
  while (!q.empty()) q.pop();
  q.push(ss), dep[ss] = 1;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (register long long i = head[u]; i; i = e[i].nxt) {
      long long v = e[i].v, flw = e[i].flw;
      if (flw && dep[v] == -1) dep[v] = dep[u] + 1, q.push(v);
    }
  }
  return ~dep[tt];
}
inline long long dfs(long long u, long long tt, long long flow) {
  if (u == tt) return flow;
  long long res = 0;
  for (register long long &i = cur[u]; i; i = e[i].nxt) {
    long long v = e[i].v, flw = e[i].flw, tmp;
    if (dep[v] == dep[u] + 1 && flw) {
      tmp = dfs(v, tt, min(flow, flw)), res += tmp, flow -= tmp,
      e[i].flw -= tmp, e[i ^ 1].flw += tmp;
      if (!flow) break;
    }
  }
  if (!res) dep[u] = -1;
  return res;
}
inline long long Maxflow(long long ss, long long tt) {
  long long ans = 0;
  while (BFS(ss, tt)) ans += dfs(ss, tt, 0x7f7f7f7f);
  return ans;
}
signed main() {
  read(n), st = 2 * n + 1, ed = st + 1;
  for (register long long i = 1; i <= n; ++i) {
    long long k, x;
    read(k);
    while (k-- > 0) read(x), add_edge(i, n + x, 0x7f7f7f7f);
  }
  long long ans = 0;
  for (register long long i = 1, x; i <= n; ++i)
    read(x), ans += 0x7f7f7f7f - x, add_edge(st, i, 0x7f7f7f7f - x),
        add_edge(n + i, ed, 0x7f7f7f7f);
  write(min(Maxflow(st, ed) - ans, 0ll)), putchar('\n');
  return 0;
}