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 + 10;
const int maxm = maxn * maxn;
const int INF = 0x3f3f3f3f;
int connect[maxn][maxn], edgecnt[maxn];
int price[maxn], vis[maxn * 2], match[maxn * 2];
int n;
bool extend(int u) {
  vis[u] = true;
  for (int i = 1; i <= edgecnt[u]; i++) {
    int v = connect[u][i] + n;
    if (vis[v] || v == match[u]) continue;
    vis[v] = true;
    if (match[v] == 0 || extend(match[v])) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}
void Match() {
  memset(match, 0, sizeof(match));
  for (int i = 1; i <= n; i++) {
    if (match[i]) continue;
    memset(vis, 0, sizeof(vis));
    extend(i);
  }
  for (int i = 1; i <= 2 * n; i++) {
  }
}
int S, T;
struct Edge {
  Edge *n, *o;
  int v, c, w;
  void init(int _v, int _c, int _w) {
    v = _v;
    c = _c;
    w = _w;
  }
};
Edge *E[maxn * 2], Er[maxn * maxn * 2];
int Es = 0;
void addedge(int u, int v, int c, int w) {
  static Edge *P1, *P2;
  P1 = &Er[++Es];
  P2 = &Er[++Es];
  P1->init(v, c, w);
  P1->n = E[u];
  P1->o = P2;
  E[u] = P1;
  P2->init(u, 0, -w);
  P2->n = E[v];
  P2->o = P1;
  E[v] = P2;
}
int H[maxn * 2];
bool V[maxn * 2];
bool BFS() {
  static int Q[maxn * 20], bot, u, v;
  memset(H, INF, sizeof(H));
  memset(V, true, sizeof(V));
  H[Q[bot = 1] = S] = 0;
  V[S] = false;
  for (int top = 1; top <= bot; top++) {
    u = Q[top];
    for (Edge *P = E[u]; P; P = P->n)
      if (P->c && H[P->v] > H[u] + 1) {
        H[P->v] = H[u] + 1;
        if (V[P->v]) {
          Q[++bot] = P->v;
          V[P->v] = false;
        }
      }
    V[u] = true;
  }
  return H[T] < INF;
}
int extend(int u, int lefts) {
  if (u == T) {
    return lefts;
  }
  V[u] = false;
  int ret = 0, t;
  for (Edge *P = E[u]; P && ret < lefts; P = P->n) {
    if (P->c && V[P->v] && H[u] + 1 == H[P->v]) {
      t = extend(P->v, min(lefts - ret, P->c));
      ret += t;
      P->c -= t;
      P->o->c += t;
    }
  }
  V[u] = true;
  if (ret < lefts) H[u] = INF;
  return ret;
}
int maxflow() {
  int ret = 0, cnt = 0;
  while (BFS()) {
    ret += extend(S, INF);
  }
  return ret;
}
void build() {
  memset(E, 0, sizeof(E));
  for (int i = 1; i <= n; i++) {
    if (price[i] >= 0) {
      addedge(S, i, price[i], 1);
    } else {
      addedge(i, T, -price[i], 1);
    }
    for (int j = 1; j <= edgecnt[i]; j++) {
      int t = match[connect[i][j] + n];
      addedge(i, t, INF, 1);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &edgecnt[i]);
    for (int j = 1; j <= edgecnt[i]; j++) {
      scanf("%d", &connect[i][j]);
    }
  }
  S = 0;
  T = 2 * n + 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", price + i);
    price[i] *= -1;
    if (price[i] >= 0) ans += price[i];
  }
  Match();
  build();
  ans = maxflow() - ans;
  cout << ans << endl;
  return 0;
}