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;
inline int read() {
  register int x(0), f(1);
  register char c(getchar());
  while (c > '9' || c < '0') f = c == '-' ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
  return f * x;
}
const int N = 1001000;
int INF = 1e7;
int n, cnt = 1, s, t, h[N], m;
struct edge {
  int next, to, f;
} e[N << 1];
void add(int u, int v, int f) {
  e[++cnt] = (edge){h[u], v, f}, h[u] = cnt;
  e[++cnt] = (edge){h[v], u, 0}, h[v] = cnt;
}
queue<int> q;
int dep[N];
bool bfs() {
  memset(dep, 0, sizeof(dep));
  while (!q.empty()) q.pop();
  q.push(s), dep[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = h[u], v; v = e[i].to, i; i = e[i].next) {
      if (dep[v] || !e[i].f) continue;
      dep[v] = dep[u] + 1;
      q.push(v);
    }
  }
  return dep[t];
}
int dfs(int u, int flow) {
  if (u == t) return flow;
  int res = 0;
  for (int i = h[u], v; v = e[i].to, i; i = e[i].next) {
    if (dep[v] != dep[u] + 1 || !e[i].f) continue;
    int k = dfs(v, min(flow - res, e[i].f));
    e[i].f -= k;
    e[i ^ 1].f += k;
    res += k;
  }
  if (res == 0) dep[u] = 0;
  return res;
}
int sum;
void dinic() {
  int ans, maxf = 0;
  while (bfs())
    while ((ans = dfs(s, INF))) maxf += ans;
  cout << maxf - sum << endl;
}
struct node {
  int u, v, w;
} a[N];
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    int T = read();
    while (T--) add(i, read() + n, INF);
  }
  s = n + n + 1, t = s + 1;
  for (int i = 1; i <= n; ++i) {
    int x = read();
    add(s, i, INF - x);
    add(i + n, t, INF);
    sum += INF - x;
  }
  INF = 1e9 + 7;
  dinic();
  return 0;
}