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 = 300;
const int INF = 0x3f3f3f3f;
const long long int LLF = 0x3f3f3f3f3f3f3f3fll;
template <class T>
void Read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = 0;
  while (c < '0' || '9' < c) {
    if (c == '-') flag = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x + (x << 2) << 1) + c - '0';
    c = getchar();
  }
  if (flag) x = -x;
}
namespace ISAP {
const int MAXP = MAXN * 2;
const int MAXE = MAXN * MAXN * 4;
struct node {
  int v;
  long long int c;
  node *nxt, *bck;
} * adj[MAXP + 10], Edges[MAXE * 2 + 10], *New;
inline void addedges(int u, int v, long long int c) {
  node *p = ++New;
  p->v = v;
  p->c = c;
  p->bck = New + 1;
  p->nxt = adj[u];
  adj[u] = p;
  p = ++New;
  p->v = u;
  p->c = 0;
  p->bck = New - 1;
  p->nxt = adj[v];
  adj[v] = p;
}
int S, T, P;
int d[MAXP + 10], vd[MAXP + 10];
inline void init(int s, int t, int p) {
  S = s, T = t, P = p;
  memset(adj, 0, sizeof(adj));
  New = Edges;
}
long long int aug(int x, long long int augco) {
  if (x == T) return augco;
  long long int delta, augc = augco;
  int Dmin = P - 1;
  for (node *p = adj[x]; p != NULL; p = p->nxt)
    if (p->c) {
      if (d[x] == d[p->v] + 1) {
        delta = min(augc, p->c);
        delta = aug(p->v, delta);
        p->c -= delta;
        p->bck->c += delta;
        augc -= delta;
        if (d[S] >= P) return augco - augc;
        if (!augc) break;
      }
      Dmin = min(Dmin, d[p->v]);
    }
  if (augco == augc) {
    if (!--vd[d[x]]) d[S] = P;
    ++vd[d[x] = Dmin + 1];
  }
  return augco - augc;
}
inline long long int isap() {
  memset(vd, 0, sizeof(vd));
  memset(d, 0, sizeof(d));
  vd[0] = P;
  long long int ret = 0;
  while (d[S] < P) ret += aug(S, LLF);
  return ret;
}
}  // namespace ISAP
int n;
int main() {
  Read(n);
  ISAP::init(n * 2 + 1, n * 2 + 2, n * 2 + 2);
  int a, b;
  for (int i = 1; i <= n; ++i) {
    Read(a);
    while (a--) {
      Read(b);
      ISAP::addedges(i, n + b, INF << 1);
    }
  }
  long long int sum = 0;
  for (int i = 1; i <= n; ++i) {
    Read(a);
    ISAP::addedges(ISAP::S, i, INF - a);
    ISAP::addedges(n + i, ISAP::T, INF);
    sum += INF - a;
  }
  sum = min(0ll, -(sum - ISAP::isap()));
  printf("%I64d\n", sum);
}