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;
vector<int> mp[100010];
int n, m, v[10010], lnk[100010];
inline bool Find(int x, int t) {
  for (int i = 0; i < mp[x].size(); i++)
    if (v[mp[x][i]] != t) {
      int to = mp[x][i];
      v[to] = t;
      if (!lnk[to] || Find(lnk[to], t)) {
        lnk[to] = x;
        return (1);
      }
    }
  return (0);
}
inline void Match() {
  for (int i = 1; i <= n; i++) Find(i, i);
}
struct Edge {
  int to, old, c;
} e[10010];
int S, T, tot, l[10010], pre[10010], q[10010], ans;
inline int Dfs(int x, int f) {
  int flow = f;
  if (x == T || !f) return (f);
  for (int i = l[x]; i && f; i = e[i].old)
    if (pre[e[i].to] == pre[x] + 1 && e[i].c) {
      int a = Dfs(e[i].to, min(f, e[i].c));
      e[i].c -= a, e[i ^ 1].c += a, f -= a;
    }
  return (flow - f);
}
inline bool Bfs() {
  int h = 1, t = 1;
  q[1] = S;
  memset(pre, -1, sizeof(pre));
  pre[S] = 0;
  do {
    int nw = q[h];
    for (int i = l[nw]; i; i = e[i].old)
      if (pre[e[i].to] == -1 && e[i].c)
        pre[e[i].to] = pre[nw] + 1, q[++t] = e[i].to;
    h++;
  } while (h <= t);
  return (pre[T] != -1);
}
inline void Add(int x, int y, int z) {
  e[++tot].to = y;
  e[tot].old = l[x], l[x] = tot, e[tot].c = z;
  e[++tot].to = x, e[tot].old = l[y], l[y] = tot, e[tot].c = 0;
}
int main() {
  scanf("%d%", &n);
  for (int i = 1; i <= n; i++) {
    int K;
    scanf("%d", &K);
    for (int j = 1; j <= K; j++) {
      int x;
      scanf("%d", &x);
      mp[i].push_back(x + n);
      mp[x + n].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  Match();
  S = 301, T = 302, tot = 1;
  for (int i = 1; i <= n; i++)
    if (v[i] < 0)
      Add(i, T, -v[i]), ans -= v[i];
    else
      Add(S, i, v[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < mp[i].size(); j++)
      if (lnk[mp[i][j]] != i) Add(lnk[mp[i][j]], i, 1000000000);
  }
  while (Bfs()) ans -= Dfs(S, 1000000000);
  printf("%d\n", -ans);
}