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 = 500;
int head[maxn], cnt = 1, ans, clk, S, T, n, a[maxn], vis[maxn], p[maxn];
vector<int> v[maxn];
struct edge {
  int to, next, w;
} e[maxn * maxn * 2];
namespace Dinic {
int d[maxn];
void link(int u, int v, int w) {
  e[++cnt] = (edge){v, head[u], w};
  head[u] = cnt;
  e[++cnt] = (edge){u, head[v], 0};
  head[v] = cnt;
}
bool bfs() {
  for (int i = S; i <= T; i++) d[i] = -1;
  queue<int> q;
  q.push(S);
  d[S] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = e[i].next)
      if (e[i].w && d[e[i].to] < 0) d[e[i].to] = d[x] + 1, q.push(e[i].to);
  }
  return d[T] > 0;
}
int dfs(int x, int f) {
  if (f == 0 || x == T) return f;
  int w, used = 0;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].w && d[e[i].to] == d[x] + 1) {
      w = dfs(e[i].to, min(e[i].w, f - used));
      used += w, e[i].w -= w, e[i ^ 1].w += w;
      if (used == f) return used;
    }
  if (!used) d[x] = -1;
  return used;
}
int main() {
  int flow = 0;
  while (bfs()) flow += dfs(S, (1ll << 30));
  return flow;
}
}  // namespace Dinic
using namespace Dinic;
bool match(int x) {
  for (int i = 0, j = v[x].size(); i < j; i++) {
    if (vis[v[x][i]] == clk) continue;
    vis[v[x][i]] = clk;
    if (!p[v[x][i]] || match(p[v[x][i]])) {
      p[v[x][i]] = x;
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int x, y, i = 1; i <= n; i++) {
    scanf("%d", &y);
    for (int j = 1; j <= y; j++) scanf("%d", &x), v[i].push_back(x);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) ++clk, match(i);
  S = 0, T = n + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0, k = v[i].size(); j < k; j++)
      link(i, p[v[i][j]], (1ll << 30));
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0)
      link(S, i, -a[i]), ans -= a[i];
    else
      link(i, T, a[i]);
  }
  ans -= Dinic::main();
  printf("%d", -ans);
  return 0;
}