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 N = 310, inf = INT_MAX / 3;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') ch = getchar(), f = -1;
  while (ch >= '0' & ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * f;
}
struct node {
  int to, nxt, flow;
} edge[N * N * 2 + N * 4];
int head[N * 2], gcnt, deep[N * 2], cur[N * 2], s, t, n, ans, val[N], table[N];
vector<int> v[N];
queue<int> q;
void add(int x, int y, int z) {
  edge[++gcnt] = (node){y, head[x], z};
  head[x] = gcnt;
}
bool bfs() {
  int i, j, x, y;
  memset(deep, 0, sizeof(deep));
  memcpy(cur, head, sizeof(head));
  q.push(s);
  deep[s] = 1;
  while (!q.empty()) {
    x = q.front(), q.pop();
    for (i = head[x]; i != -1; i = edge[i].nxt) {
      y = edge[i].to;
      if (deep[y] || !edge[i].flow) continue;
      deep[y] = deep[x] + 1, q.push(y);
    }
  }
  return deep[t];
}
int dfs(int x, int flow) {
  int nflow, y, used = 0, i;
  if (flow == 0 || x == t) return flow;
  for (i = cur[x]; i != -1; i = edge[i].nxt, cur[x] = i) {
    y = edge[i].to;
    if (!edge[i].flow || deep[y] != deep[x] + 1) continue;
    if (nflow = dfs(y, min(edge[i].flow, flow - used))) {
      used += nflow, edge[i].flow -= nflow, edge[i ^ 1].flow += nflow;
      if (used == flow) break;
    }
  }
  if (used == 0) deep[x] = -1;
  return used;
}
void Dinic() {
  while (bfs()) ans += dfs(s, inf);
}
int main() {
  memset(head, -1, sizeof(head)), gcnt = -1;
  int i, j, x, y, z, sum = 0;
  n = read();
  s = n * 2 + 1, t = s + 1;
  for (i = 1; i <= n; i++) {
    y = read();
    v[i].resize(y);
    for (j = 0; j < v[i].size(); j++)
      v[i][j] = read(), add(i, v[i][j] + n, 1), add(v[i][j] + n, i, 0);
  }
  for (i = 1; i <= n; i++) val[i] = -read();
  for (i = 1; i <= n; i++)
    add(s, i, 1), add(i, s, 0), add(i + n, t, 1), add(t, i + n, 0);
  Dinic();
  for (i = 1; i <= n; i++) {
    for (j = head[i]; j != -1; j = edge[j].nxt)
      if (edge[j].flow == 0 && edge[j].to != s) {
        table[edge[j].to - n] = i;
        break;
      }
  }
  memset(head, -1, sizeof(head)), gcnt = -1;
  s = n + 1, t = s + 1, ans = 0;
  for (i = 1; i <= n; i++) {
    if (val[i] > 0)
      add(s, i, val[i]), add(i, s, 0), sum += val[i];
    else
      add(i, t, -val[i]), add(t, i, 0);
    for (j = 0; j < v[i].size(); j++)
      add(i, table[v[i][j]], inf), add(table[v[i][j]], i, 0);
  }
  Dinic();
  cout << min(0, -sum + ans);
  return ~~(0 - 0);
}