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;
int d[305], ml[305];
int S, T, len = 1, ans, E[305][305], a[305];
int n, head[305], mr[305], vis[305], cnt;
struct way {
  int num, next, cap;
} G[305 * 305];
void link(int x, int y, int z) {
  G[++len] = (way){y, head[x], z}, head[x] = len;
  G[++len] = (way){x, head[y], 0}, head[y] = len;
}
bool bfs() {
  int i, x;
  queue<int> Q;
  for (i = 1; i <= T; i++) d[i] = 0;
  d[S] = 1, Q.push(S);
  while (!Q.empty()) {
    x = Q.front(), Q.pop();
    for (i = head[x]; i; i = G[i].next)
      if (G[i].cap && !d[G[i].num]) {
        d[G[i].num] = d[x] + 1;
        Q.push(G[i].num);
      }
  }
  return d[T] > 0;
}
int dfs(int t, int mini) {
  int i, ans = 0, res;
  if (t == T || !mini) return mini;
  for (i = head[t]; i; i = G[i].next)
    if (G[i].cap && d[G[i].num] == d[t] + 1) {
      res = dfs(G[i].num, min(mini, G[i].cap));
      ans += res, mini -= res, G[i].cap -= res, G[i ^ 1].cap += res;
      if (!mini) return ans;
    }
  d[t] = (1 << 30);
  return ans;
}
bool find(int t) {
  int i, q;
  for (i = 1; i <= n; i++) {
    if (!E[t][i] || vis[i] == cnt) continue;
    q = mr[i], mr[i] = t, vis[i] = cnt;
    if (!q || find(q)) return 1;
    mr[i] = q;
  }
  return 0;
}
int main() {
  int i, j, x;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &len);
    for (j = 1; j <= len; j++) {
      scanf("%d", &x);
      E[i][x] = 1;
    }
  }
  len = 1, S = n + 1, T = S + 1;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]), a[i] = -a[i];
    if (a[i] < 0)
      link(i, T, -a[i]);
    else
      ans += a[i], link(S, i, a[i]);
  }
  for (i = 1; i <= n; i++) ++cnt, find(i);
  for (i = 1; i <= n; i++) ml[mr[i]] = i;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (E[i][j] && j != ml[i]) link(i, mr[j], (1 << 30));
  while (bfs()) ans -= dfs(S, (1 << 30));
  cout << -ans;
  return 0;
}