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 = 310;
const int INF = 0x3f3f3f3f;
int n, en, ans, depth[maxn], result[maxn];
bool use[maxn];
queue<int> que;
struct edge {
  int e, f;
  edge *next, *op;
} * v[maxn], *v2[maxn], ed[maxn * maxn << 1];
void add_edge(int s, int e) {
  en++;
  ed[en].next = v[s];
  v[s] = ed + en;
  v[s]->e = e;
}
void add_edge(int s, int e, int f) {
  en++;
  ed[en].next = v2[s];
  v2[s] = ed + en;
  v2[s]->e = e;
  v2[s]->f = f;
  en++;
  ed[en].next = v2[e];
  v2[e] = ed + en;
  v2[e]->e = s;
  v2[e]->f = 0;
  v2[s]->op = v2[e];
  v2[e]->op = v2[s];
}
bool dfs(int now) {
  for (edge *e = v[now]; e; e = e->next)
    if (!use[e->e]) {
      use[e->e] = true;
      if (!result[e->e] || dfs(result[e->e])) {
        result[e->e] = now;
        return true;
      }
    }
  return false;
}
bool bfs() {
  memset(depth, 0, sizeof(depth));
  que.push(0);
  depth[0] = 1;
  while (que.size()) {
    int now = que.front();
    que.pop();
    for (edge *e = v2[now]; e; e = e->next)
      if (e->f && !depth[e->e]) {
        depth[e->e] = depth[now] + 1;
        que.push(e->e);
      }
  }
  return depth[n + 1] != 0;
}
int dfs(int now, int cur_flow) {
  if (now == n + 1) return cur_flow;
  int rest = cur_flow;
  for (edge *e = v2[now]; e; e = e->next)
    if (depth[e->e] == depth[now] + 1 && e->f && rest) {
      int new_flow = dfs(e->e, min(e->f, rest));
      e->f -= new_flow;
      e->op->f += new_flow;
      rest -= new_flow;
    }
  if (cur_flow == rest) depth[now] = 0;
  return cur_flow - rest;
}
void dinic() {
  while (bfs()) ans += dfs(0, INF);
  printf("%d\n", ans);
}
int main() {
  scanf("%d", &n);
  for (int a = 1; a <= n; a++) {
    int m;
    scanf("%d", &m);
    for (int b = 1; b <= m; b++) {
      int v;
      scanf("%d", &v);
      add_edge(a, v);
    }
  }
  for (int a = 1; a <= n; a++) {
    memset(use, false, sizeof(use));
    dfs(a);
  }
  for (int a = 1; a <= n; a++) {
    int v;
    scanf("%d", &v);
    if (v < 0)
      ans += v, add_edge(0, a, -v);
    else
      add_edge(a, n + 1, v);
  }
  for (int a = 1; a <= n; a++)
    for (edge *e = v[a]; e; e = e->next)
      if (result[e->e] != a) add_edge(a, result[e->e], INF);
  dinic();
  return 0;
}