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;
struct edge {
  int pre, e, r;
  long long c;
  bool v;
};
edge E[100000];
int last = 0, V[1000], m, n, s, t;
int h[1000], vh[1000];
void insert(int u, int v, long long w) {
  E[last].e = v;
  E[last].c = w;
  E[last].r = last + 1;
  E[last].pre = V[u];
  E[last].v = false;
  V[u] = last++;
  E[last].e = u;
  E[last].c = 0;
  E[last].r = last - 1;
  E[last].pre = V[v];
  E[last].v = true;
  V[v] = last++;
}
long long AugPath(int u, long long mf) {
  if (u == t) return mf;
  long long l = mf;
  int minh = n - 1;
  for (int i = V[u]; i != -1; i = E[i].pre)
    if (E[i].c) {
      if (h[E[i].e] + 1 == h[u]) {
        long long d = l < E[i].c ? l : E[i].c, f;
        if (d) d = AugPath(E[i].e, d);
        E[i].c -= d;
        E[E[i].r].c += d;
        l -= d;
        if (!l || h[s] >= n) return mf - l;
      }
      if (h[E[i].e] < minh) minh = h[E[i].e];
    }
  if (!(mf - l)) {
    vh[h[u]]--;
    if (!vh[h[u]]) h[s] = n;
    h[u] = ++minh;
    vh[h[u]]++;
  }
  return mf - l;
}
int main() {
  int nn;
  cin >> nn;
  s = 0;
  t = 2 * nn + 1;
  n = 2 * nn + 2;
  for (int i = 0; i < n; ++i) V[i] = -1;
  last = 0;
  for (int i = nn + 1; i <= 2 * nn; ++i) insert(i, t, 0x7f7f7f7f7f7fll);
  for (int m, a, i = 0; i < nn; ++i) {
    cin >> m;
    for (int j = 0; j < m; ++j) {
      cin >> a;
      insert(i + 1, nn + a, 0x7f7f7f7f7f7fll);
    }
  }
  long long tot = 0;
  for (int w, i = 0; i < nn; ++i) {
    cin >> w;
    tot += w;
    insert(s, i + 1, 0x7f7f7f7f7f7fll - w);
  }
  memset(h, 0, sizeof(h));
  memset(vh, 0, sizeof(vh));
  vh[0] = n;
  long long flow = 0;
  while (h[s] < n) flow += AugPath(s, 0x7f7f7f7f7f7fll * 2ll);
  cout << tot + flow - nn * 0x7f7f7f7f7f7fll << endl;
  return 0;
}