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 n, m, x, ed, S, T, V;
int l[650], v[650], g[650];
int qd[200000], qn[200000];
long long int qc[200000];
long long int cost, ret;
void bd(int x, int y, long long int c) {
  qd[++ed] = y, qn[ed] = l[x], l[x] = ed, qc[ed] = c;
  if (ed & 1 ^ 1) bd(y, x, 0);
}
long long int ISAP(int x, long long int y) {
  int mn = V - 1;
  long long int F = 0, R = y, t;
  if (x == T) return y;
  for (int i = l[x]; i; i = qn[i]) {
    if (qc[i] > 0) {
      if (v[x] == v[qd[i]] + 1) {
        t = ISAP(qd[i], min(qc[i], R));
        qc[i] -= t;
        qc[i ^ 1] += t;
        R -= t;
        F += t;
        if (v[S] >= V) return F;
        if (!R) break;
      }
      mn = min(mn, v[qd[i]]);
    }
  }
  if (!F) {
    g[v[x]]--;
    if (!g[v[x]]) v[S] = V;
    v[x] = mn + 1;
    g[v[x]]++;
  }
  return F;
}
int main() {
  scanf("%d", &n);
  S = 0, T = 2 * n + 1;
  V = T + 1;
  ed = 1;
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &m);
    while (m--) {
      scanf("%d", &x);
      bd(n + i, x, (1LL << 50));
    }
    bd(i, T, (1LL << 40));
  }
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &x);
    bd(S, n + i, (1LL << 40) - x);
    ret += (1LL << 40) - x;
  }
  g[0] = V;
  while (v[S] < V) ret -= ISAP(S, (1LL << 50));
  printf("%d\n", -(int)ret);
  return 0;
}