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;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
  if (fu) v = -v;
}
const int INF = 1e9 + 10;
bool c[1010][1010], v[5555];
int nxt[500010], q[500010], S, T, ans, h[500010], g[500010], d[500010],
    hed[500010], cap[500010], too[500010], nedge, n, m, x, y, a[666];
void ae(int x, int y, int w) {
  nxt[++nedge] = hed[x];
  hed[x] = nedge;
  too[nedge] = y;
  cap[nedge] = w;
}
void add(int x, int y, int w) { ae(x, y, w), ae(y, x, 0); }
bool build() {
  int he = 0, ta = 1;
  q[1] = S;
  for (int i = S; i <= T; ++i) d[i] = -1;
  d[S] = 0;
  while (he != ta) {
    int x = q[++he];
    for (int i = hed[x]; i; i = nxt[i]) {
      int y = too[i];
      if (!cap[i] || d[y] != -1) continue;
      d[y] = d[x] + 1;
      q[++ta] = y;
      if (y == T) return 1;
    }
  }
  return 0;
}
int fnd(int x, int flo) {
  if (x == T) return flo;
  int ret, w = 0;
  for (int i = hed[x]; i && w < flo; i = nxt[i]) {
    int y = too[i];
    if (d[y] == d[x] + 1 && cap[i] && (ret = fnd(y, min(flo - w, cap[i]))))
      w += ret, cap[i] -= ret, cap[i ^ 1] += ret;
  }
  d[x] = -1;
  return w;
}
int dinic() {
  int ans = 0;
  while (build()) {
    while (1) {
      int flo = fnd(S, INF);
      if (!flo) break;
      ans += flo;
    }
  }
  return ans;
}
bool ck(int x) {
  for (int y = 1; y <= n; ++y) {
    if (!c[x][y] || v[y]) continue;
    v[y] = 1;
    if (h[y] == -1 || ck(h[y])) {
      h[y] = x;
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  S = 0;
  T = n + n + 1;
  nedge = 1;
  memset(h, -1, sizeof(h));
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &x);
    while (x--) {
      getint(y);
      c[i][y] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) v[j] = 0;
    ck(i);
  }
  for (int i = 1; i <= n; ++i) g[h[i]] = i;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j)
      if (c[i][j] && j != g[i]) {
        add(i, h[j], INF);
      }
  }
  for (int i = 1; i <= n; ++i) {
    getint(a[i]);
    a[i] = -a[i];
    if (a[i] >= 0)
      add(S, i, a[i]), ans += a[i];
    else
      add(i, T, -a[i]);
  }
  ans -= dinic();
  cout << -ans << endl;
  return 0;
}