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>
int n, dep[1005], sp, s, t;
long long sum, p, ans;
std::vector<int> a[1005], f[1005], g[1005];
std::vector<long long> b[1005];
std::queue<int> q;
void add(int x, int y, long long v) {
  a[x].push_back(y);
  a[y].push_back(x);
  b[x].push_back(v);
  b[y].push_back(0);
  f[x].push_back(a[y].size() - 1);
  f[y].push_back(a[x].size() - 1);
  g[x].push_back(1);
  g[y].push_back(0);
}
long long min(long long x, long long y) { return x > y ? y : x; }
int bfs(int s, int t) {
  for (int i = 1; i <= sp; i++) dep[i] = 0;
  dep[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < a[v].size(); i++)
      if (!dep[a[v][i]] && b[v][i]) {
        dep[a[v][i]] = dep[v] + 1;
        q.push(a[v][i]);
      }
  }
  return dep[t] != 0;
}
long long dfs(int v, long long now) {
  if (v == t || !now) return now;
  long long flow = 0;
  for (int i = 0; i < a[v].size(); i++)
    if (dep[v] + 1 == dep[a[v][i]] && b[v][i]) {
      long long k = dfs(a[v][i], min(b[v][i], now));
      flow = flow + k;
      now = now - k;
      b[v][i] = b[v][i] - k;
      b[a[v][i]][f[v][i]] = b[a[v][i]][f[v][i]] + k;
    }
  return flow;
}
long long dinic() {
  long long ans = 0;
  while (bfs(s, t)) ans = ans + dfs(s, 1000000000000000000);
  return ans;
}
int main() {
  scanf("%d", &n);
  s = n * 2 + 1, t = n * 2 + 2, sp = n * 2 + 2;
  for (int i = 1; i <= n; i++) {
    int k, x;
    scanf("%d", &k);
    for (int j = 1; j <= k; j++) {
      int x;
      scanf("%d", &x);
      add(i, n + x, 2000000000000000);
    }
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    add(s, i, 1000000000000000 - x);
    sum = sum + 1000000000000000 - x;
  }
  for (int i = 1; i <= n; i++) add(n + i, t, 1000000000000000);
  p = dinic();
  ans = min((long long)0, -(sum - p));
  printf("%lld\n", ans);
  return 0;
}