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;
const int MAXN = 1010;
struct Edge {
  int s, e, nx;
  long long c;
} E[1000000];
int head[MAXN], cnt;
void add(int s, int e, long long c) {
  E[cnt].s = s;
  E[cnt].e = e;
  E[cnt].c = c;
  E[cnt].nx = head[s];
  head[s] = cnt++;
  E[cnt].s = e;
  E[cnt].e = s;
  E[cnt].c = 0;
  E[cnt].nx = head[e];
  head[e] = cnt++;
}
int flow_d[MAXN], flow_num[MAXN];
int flow_Stack[MAXN], flow_Top;
int flow_H[MAXN];
int flow_Q[MAXN];
long long isap(int N, int s, int t) {
  for (int i = 0; i <= N + 1; i++) flow_H[i] = head[i];
  memset(flow_d, -1, sizeof(flow_d));
  memset(flow_num, 0, sizeof(flow_num));
  flow_d[t] = 0;
  flow_num[0] = 1;
  int l = 0, r = 0;
  flow_Q[r++] = t;
  int x;
  while (l < r) {
    x = flow_Q[l++];
    for (int i = head[x]; ~i; i = E[i].nx)
      if (E[i ^ 1].c && flow_d[E[i].e] == -1) {
        flow_d[E[i].e] = flow_d[x] + 1;
        ++flow_num[flow_d[x] + 1];
        flow_Q[r++] = E[i].e;
      }
  }
  long long ans = 0;
  flow_Top = 0;
  while (flow_d[s] < N) {
    if (!flow_Top)
      x = s;
    else
      x = E[flow_Stack[flow_Top - 1]].e;
    if (x == t) {
      long long p = E[flow_Stack[0]].c;
      for (int i = 1; i < flow_Top; i++) p = min(p, E[flow_Stack[i]].c);
      ans += p;
      for (int i = flow_Top - 1; i >= 0; i--) {
        E[flow_Stack[i]].c -= p;
        if (E[flow_Stack[i]].c == 0) flow_Top = i;
        E[flow_Stack[i] ^ 1].c += p;
      }
    } else {
      bool ok = 0;
      for (int i = flow_H[x]; ~i; i = E[i].nx)
        if (E[i].c > 0 && flow_d[E[i].e] == flow_d[x] - 1) {
          flow_Stack[flow_Top++] = i;
          flow_H[x] = i;
          ok = 1;
          break;
        }
      if (ok) continue;
      if (--flow_num[flow_d[x]] == 0) return ans;
      int mi = N;
      for (int i = head[x]; i != -1; i = E[i].nx)
        if (E[i].c > 0 && mi > flow_d[E[i].e]) {
          mi = flow_d[E[i].e];
          flow_H[x] = i;
        }
      flow_d[x] = mi + 1;
      ++flow_num[flow_d[x]];
      if (x != s) --flow_Top;
    }
  }
  return ans;
}
const long long M = 1LL << 50;
int n;
bool can[MAXN][MAXN];
int main() {
  while (~scanf("%d", &n)) {
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(can, 0, sizeof(can));
    int s = 0, t = 2 * n + 1;
    for (int i = 1; i <= n; i++) {
      int m;
      scanf("%d", &m);
      for (int j = 1; j <= m; j++) {
        int x;
        scanf("%d", &x);
        can[i][x] = 1;
      }
    }
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      int p;
      scanf("%d", &p);
      add(s, i, -p + M);
      sum -= p;
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (can[i][j]) {
          add(i, j + n, M * 100000);
        }
    for (int i = 1; i <= n; i++) add(i + n, t, M);
    long long ans = isap(t + 1, s, t);
    printf("%I64d\n", ans - sum - n * M);
  }
}