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>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx")
using namespace std;
inline char gc() {
  static char buf[1 << 16], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin);
    if (p2 == p1) return EOF;
  }
  return *p1++;
}
template <class t>
inline t read(t &x) {
  char c = gc();
  bool f = 0;
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = gc();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
  if (f) x = -x;
  return x;
}
template <class t>
inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const long long N = 6e4 + 5, M = 2e5 + 5;
long long en = 1, h[N], cur[N], d[N], n, m, ans;
struct edge {
  long long n, v, w;
} e[M << 1];
void add(long long x, long long y, long long z) {
  e[++en] = (edge){h[x], y, z};
  h[x] = en;
}
bool bfs(long long s, long long aim) {
  memset(d, 0, sizeof d);
  memcpy(cur, h, sizeof cur);
  queue<long long> q;
  q.push(s);
  d[s] = 1;
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    for (long long i = h[x]; i; i = e[i].n) {
      long long y = e[i].v;
      if (!d[y] && e[i].w) {
        d[y] = d[x] + 1;
        if (y == aim) return 1;
        q.push(y);
      }
    }
  }
  return 0;
}
long long dfs(long long x, long long flow, long long aim) {
  if (x == aim) return flow;
  long long rest = flow;
  for (long long &i = cur[x]; i && rest; i = e[i].n) {
    long long y = e[i].v;
    if (d[y] == d[x] + 1 && e[i].w) {
      long long tp = dfs(y, min(rest, e[i].w), aim);
      rest -= tp;
      e[i].w -= tp;
      e[i ^ 1].w += tp;
    }
  }
  return flow - rest;
}
long long dinic(long long s, long long t) {
  long long res = 0;
  while (bfs(s, t)) res += dfs(s, 1000000000000, t);
  return res;
}
void exadd(long long x, long long y, long long f) {
  add(x, y, f);
  add(y, x, 0);
}
signed main() {
  read(n);
  for (long long i = 1, m, x; i <= n; i++) {
    read(m);
    while (m--) exadd(i, read(x) + n, 100000000);
  }
  for (long long i = 1, x; i <= n; i++) {
    read(x);
    exadd(0, i, 100000000 - x);
    ans += 100000000 - x;
  }
  for (long long i = 1; i <= n; i++) exadd(i + n, n * 2 + 1, 100000000);
  write(min(dinic(0, n * 2 + 1) - ans, 0ll));
}