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 = 305;
bool c[MAXN][MAXN];
bool u[MAXN];
int n;
int Link[MAXN];
bool Find(int p) {
  for (int i = 1; i <= n; i++)
    if (!u[i] && c[p][i]) {
      u[i] = true;
      if (!Link[i] || Find(Link[i])) {
        Link[i] = p;
        return true;
      }
    }
  return false;
}
struct Edge {
  int num, next, cap, rev;
} e[MAXN * MAXN * 2];
int en = 0, start[MAXN];
inline void Add_edge(int p, int q, int c) {
  en++;
  e[en].num = q;
  e[en].next = start[p];
  start[p] = en;
  e[en].cap = c;
  e[en].rev = en + 1;
  en++;
  e[en].num = p;
  e[en].next = start[q];
  start[q] = en;
  e[en].cap = 0;
  e[en].rev = en - 1;
}
int flow = 0, aug;
bool flag;
int gap[MAXN], gapv[MAXN];
void auge(int p) {
  if (p == n) {
    flow += aug;
    flag = true;
    return;
  }
  int i, mingap = n - 1, augt = aug;
  for (i = start[p]; i != 0; i = e[i].next)
    if (e[i].cap > 0) {
      if (gap[p] == gap[e[i].num] + 1) {
        if (e[i].cap < aug) aug = e[i].cap;
        auge(e[i].num);
        if (gap[1] >= n) return;
        if (flag) break;
        aug = augt;
      }
      if (gap[e[i].num] < mingap) mingap = gap[e[i].num];
    }
  if (flag) {
    e[i].cap -= aug;
    e[e[i].rev].cap += aug;
  } else {
    gapv[gap[p]]--;
    if (gapv[gap[p]] == 0) gap[1] = n;
    gap[p] = mingap + 1;
    gapv[gap[p]]++;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    while (t--) {
      int p;
      scanf("%d", &p);
      c[i][p] = true;
    }
  }
  for (int i = 1; i <= n; i++) {
    memset(u, 0, sizeof(u));
    Find(i);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (c[i][j] && Link[j] != i) Add_edge(i + 1, Link[j] + 1, 1000000000);
  int s = 0;
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    if (t < 0) s -= t;
    if (t < 0)
      Add_edge(1, i + 1, -t);
    else
      Add_edge(i + 1, n + 2, t);
  }
  memset(gap, 0, sizeof(gap));
  memset(gapv, 0, sizeof(gapv));
  n += 2;
  gapv[0] = n;
  while (gap[1] < n) {
    aug = 1000000000;
    flag = false;
    auge(1);
  }
  printf("%d\n", -(s - flow));
  return 0;
}