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>
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fll;
long long getint() {
  long long ret = 0;
  bool f = 0;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f ^= 1;
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return f ? -ret : ret;
}
using namespace std;
const int MAXN = 600;
namespace ISAP {
const int MAXM = MAXN * MAXN;
struct edge {
  int v, c;
  edge *nxt, *other;
  edge() {}
  edge(int _v, int _c, edge *_nxt, edge *_other) {
    v = _v;
    c = _c;
    nxt = _nxt;
    other = _other;
  }
} E[2 * MAXM + 10], *cnte = E, *fir[MAXN + 10];
void addedge(int u, int v, int c) {
  *cnte = edge(v, c, fir[u], cnte + 1);
  fir[u] = cnte++;
  *cnte = edge(u, 0, fir[v], cnte - 1);
  fir[v] = cnte++;
}
int dis[MAXN + 10], num[MAXN + 10], myq[MAXN + 10];
bool vis[MAXN + 10];
int s, t;
int aug(int u, int augc) {
  if (u == t) return augc;
  int stream, augs = 0, mindis = MAXN + 1, v;
  for (edge *p = fir[u]; p; p = p->nxt)
    if (p->c > 0) {
      v = p->v;
      if (dis[v] + 1 == dis[u]) {
        stream = min(p->c, augc - augs);
        stream = aug(v, stream);
        p->c -= stream;
        p->other->c += stream;
        augs += stream;
        if (dis[s] > MAXN || augs == augc) return augs;
      }
      mindis = min(mindis, dis[v]);
    }
  if (!augs) {
    if (!(--num[dis[u]])) dis[s] = MAXN + 1;
    ++num[dis[u] = mindis + 1];
  }
  return augs;
}
void prepare() {
  memset(dis, 0x3f, sizeof dis);
  int l = 0, r = 0;
  myq[++r] = t;
  vis[t] = 1;
  dis[t] = 0;
  num[0] = 1;
  while (l < r) {
    int u = myq[++l], v;
    for (edge *p = fir[u]; p; p = p->nxt)
      if (!vis[v = p->v]) {
        vis[v] = 1;
        ++num[dis[v] = dis[u] + 1];
        myq[++r] = v;
      }
  }
}
int work(int _s, int _t) {
  s = _s;
  t = _t;
  prepare();
  int ret = 0;
  while (dis[s] <= MAXN) ret += aug(s, INF);
  return ret;
}
}  // namespace ISAP
using ISAP::addedge;
using ISAP::work;
int val[MAXN + 10];
int main() {
  int n = getint();
  for (int i = 1; i <= n; ++i) {
    int m = getint();
    while (m--) {
      int j = getint();
      addedge(i, j + n, INF);
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; ++i) sum += max(0, val[i] = getint());
  int s = 2 * n + 1, t = 2 * n + 2;
  int w = 0;
  for (int i = 1; i <= n; ++i) {
    addedge(s, i, sum - val[i]);
    addedge(i + n, t, sum);
    w += sum - val[i];
  }
  printf("%d\n", work(s, t) - w);
}