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;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return f * s;
}
struct edge {
  int to, lt, f;
} e[310 * 310 * 4];
queue<int> que;
int dep[310], cnt = 1, in[310], s, t;
void add(int x, int y, int f) {
  e[++cnt].to = y;
  e[cnt].lt = in[x];
  e[cnt].f = f;
  in[x] = cnt;
  e[++cnt].to = x;
  e[cnt].lt = in[y];
  e[cnt].f = 0;
  in[y] = cnt;
}
bool bfs() {
  while (!que.empty()) que.pop();
  memset(dep, 0, sizeof(dep));
  que.push(s);
  dep[s] = 1;
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    for (int i = in[x]; i; i = e[i].lt)
      if (e[i].f && !dep[e[i].to]) {
        que.push(e[i].to), dep[e[i].to] = dep[x] + 1;
        if (e[i].to == t) return 1;
      }
  }
  return 0;
}
int dfs(int x, int flow) {
  if (x == t) return flow;
  int cur = flow;
  for (int i = in[x]; i; i = e[i].lt)
    if (e[i].f && dep[e[i].to] == dep[x] + 1) {
      int tmp = dfs(e[i].to, min(e[i].f, cur));
      e[i].f -= tmp;
      e[i ^ 1].f += tmp;
      cur -= tmp;
      if (!cur) return flow;
    }
  dep[x] = -1;
  return flow - cur;
}
int dinic() {
  int ans = 0;
  while (bfs()) ans += dfs(s, 2002122500);
  return ans;
}
int k[310], edg[310][310], p[310], a[310], n;
bool vis[310];
bool match(int x) {
  for (int i = 1; i <= k[x]; i++)
    if (!vis[edg[x][i]]) {
      vis[edg[x][i]] = 1;
      if (!p[edg[x][i]] || match(p[edg[x][i]])) return p[edg[x][i]] = x, true;
    }
  return false;
}
void Match() {
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof(vis));
    match(i);
  }
}
void build() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= k[i]; j++)
      if (p[edg[i][j]] != i) add(i, p[edg[i][j]], 2002122500);
}
int main() {
  s = 310 - 3;
  t = s + 1;
  n = read();
  for (int i = 1; i <= n; i++) {
    k[i] = read();
    for (int j = 1; j <= k[i]; j++) edg[i][j] = read();
  }
  int ful = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = -read();
    if (a[i] >= 0)
      add(s, i, a[i]), ful += a[i];
    else
      add(i, t, -a[i]);
  }
  Match();
  build();
  printf("%d\n", dinic() - ful);
  return 0;
}