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 long long INF = 1e9 + 7;
const int N = 6e2 + 7;
const int MOD = 1e9 + 7;
const double Pi = acos(-1.);
const double EPS = 1e-7;
int et, cur[N], dis[N], que[N], head[N];
struct Edge {
  long long v;
  int s, t, next;
} e[N * N];
void addEdge(int s, int t, long long v) {
  e[et].s = s, e[et].t = t, e[et].v = v, e[et].next = head[s], head[s] = et++;
  e[et].s = t, e[et].t = s, e[et].v = 0, e[et].next = head[t], head[t] = et++;
}
bool bfs(int S, int T, int n) {
  int qh = 0, qt = 0;
  for (int i = 0; i < n; ++i) dis[i] = -1;
  dis[S] = 0, que[qt++] = S;
  while (qh < qt) {
    for (int i = head[que[qh++]]; ~i; i = e[i].next) {
      if (e[i].v && !~dis[e[i].t]) {
        dis[e[i].t] = 1 + dis[e[i].s];
        if (e[i].t == T) return true;
        que[qt++] = e[i].t;
      }
    }
  }
  return false;
}
long long dinic(int S, int T, int n) {
  int u, qt;
  long long maxFlow = 0;
  while (bfs(S, T, n)) {
    for (int i = 0; i < n; ++i) cur[i] = head[i];
    u = S, qt = 0;
    while (~cur[S]) {
      if (u != T && ~cur[u] && e[cur[u]].v && dis[e[cur[u]].t] == 1 + dis[u]) {
        que[qt++] = cur[u];
        u = e[cur[u]].t;
      } else if (u == T) {
        long long tmp = 1LL + (INF << 1);
        for (int i = qt - 1; i >= 0; --i) tmp = min(tmp, e[que[i]].v);
        for (int i = qt - 1; i >= 0; --i) {
          e[que[i]].v -= tmp;
          e[que[i] ^ 1].v += tmp;
          if (!e[que[i]].v) qt = i;
        }
        u = e[que[qt]].s;
        maxFlow += tmp;
      } else {
        while (u != S && !~cur[u]) u = e[que[--qt]].s;
        cur[u] = e[cur[u]].next;
      }
    }
  }
  return maxFlow;
}
int n;
int main() {
  long long sum = 0;
  int S, T;
  scanf("%d", &n);
  S = 0, T = 1 + (n << 1);
  memset(head, -1, sizeof(head));
  for (int i = 1; i <= n; ++i) {
    int m;
    scanf("%d", &m);
    while (m--) {
      int x;
      scanf("%d", &x);
      addEdge(i, x + n, INF << 1);
    }
  }
  for (int i = 1; i <= n; ++i) {
    int pri;
    scanf("%d", &pri);
    sum += pri;
    addEdge(S, i, INF - pri);
  }
  for (int i = 1; i <= n; ++i) {
    addEdge(n + i, T, INF);
  }
  long long ans = sum + dinic(S, T, T + 1) - n * INF;
  printf("%I64d", ans);
  return 0;
}