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 code[1000];
int lvl[1000], f[1000], cadj[1000], n;
int adj[1000], nxt[1000000], to[1000000];
long long cap[1000000];
bool bfs(int S, int T) {
  for (int i = 0; i < T + 1; i++) lvl[i] = -1;
  int pi = 0, pf = 0;
  f[pf++] = S;
  lvl[S] = 0;
  while (pi < pf) {
    int v = f[pi++];
    for (int i = adj[v]; i != -1; i = nxt[i]) {
      if (cap[i] && lvl[to[i]] == -1) {
        lvl[to[i]] = lvl[v] + 1;
        f[pf++] = to[i];
      }
    }
  }
  return lvl[T] != -1;
}
long long dfs(int v, int T, long long flow) {
  if (v == T) return flow;
  long long foi = 0LL;
  for (int &i = cadj[v]; i != -1 && (flow - foi); i = nxt[i]) {
    if (cap[i] && lvl[to[i]] == lvl[v] + 1) {
      long long f = dfs(to[i], T, min(flow - foi, cap[i]));
      if (f) {
        cap[i] -= f, cap[i ^ 1] += f;
        foi += f;
      }
    }
  }
  return foi;
}
long long fluxo(int S, int T, int m) {
  n = m;
  long long tmp, flow = 0;
  while (bfs(S, T)) {
    for (int i = 0; i < T + 1; i++) cadj[i] = adj[i];
    while ((tmp = dfs(S, T, 100000000000000LL))) flow += tmp;
  }
  return flow;
}
int cnt = 0;
void are(int a, int b, long long cp, bool eh = true) {
  to[cnt] = b, cap[cnt] = cp, nxt[cnt] = adj[a], adj[a] = cnt;
  cnt++;
  if (eh) are(b, a, 0, false);
}
int v1[1000000], v2[1000000];
int main() {
  int m;
  scanf("%d", &m);
  int S = 2 * m, T = 2 * m + 1;
  cnt = 0;
  for (int i = 0; i < T + 1; i++) adj[i] = -1;
  for (int i = 0; i < m; i++) {
    int qtd, v;
    scanf("%d", &qtd);
    for (int j = 0; j < qtd; j++) {
      scanf("%d", &v);
      --v;
      are(i, v + m, 1);
    }
  }
  for (int i = 0; i < m; i++) are(S, i, 1), are(i + m, T, 1);
  long long val = fluxo(S, T, T + 1);
  for (int i = 0; i < m; i++) {
    for (int j = adj[i]; j != -1; j = nxt[j]) {
      if (to[j] != S && cap[j] == 0) {
        code[to[j]] = i;
        break;
      }
    }
  }
  int k = 0;
  for (int i = 0; i < m; i++) {
    for (int j = adj[i]; j != -1; j = nxt[j]) {
      if (to[j] != S && cap[j] == 1) v1[k] = i, v2[k] = code[to[j]], k++;
    }
  }
  long long sum = 0;
  cnt = 0;
  S = m, T = m + 1;
  for (int i = 0; i < T + 1; i++) adj[i] = -1;
  for (int i = 0; i < k; i++) are(v2[i], v1[i], 100000000000000LL);
  for (int i = 0; i < m; i++) {
    int cus;
    scanf("%d", &cus);
    if (cus < 0)
      are(i, T, -cus), sum += cus;
    else
      are(S, i, cus);
  }
  cout << sum + fluxo(S, T, T + 1) << endl;
  return 0;
}