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 = 3e2 + 20;
const int maxm = 1e5 + 20;
template <int maxn, int maxm>
struct Flow {
  static const long long inf = 1e18;
  int from[maxm * 2], to[maxm * 2], head[maxn], prv[maxm * 2], id, d[maxn],
      pt[maxn];
  long long cap[maxm * 2];
  Flow() {
    memset(from, 0, sizeof from);
    memset(to, 0, sizeof to);
    memset(cap, 0, sizeof cap);
    memset(d, 0, sizeof d);
    memset(head, -1, sizeof head);
    id = 0;
  }
  void add_edge(int v, int u, long long c) {
    prv[id] = head[v], head[v] = id, from[id] = v, to[id] = u, cap[id] = c,
    id++;
    prv[id] = head[u], head[u] = id, from[id] = u, to[id] = v, cap[id] = 0,
    id++;
  }
  bool bfs(int S, int T) {
    queue<int> q;
    memset(d, 63, sizeof d);
    d[S] = 0;
    q.push(S);
    memcpy(pt, head, sizeof head);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int e = head[v]; e != -1; e = prv[e])
        if (cap[e] && d[to[e]] > d[v] + 1) {
          d[to[e]] = d[v] + 1;
          q.push(to[e]);
        }
    }
    return (d[T] < 1e9);
  }
  long long dfs(int v, int T, long long flow = inf) {
    if (v == T || !flow) return flow;
    long long res = 0;
    for (; pt[v] != -1; pt[v] = prv[pt[v]]) {
      int e = pt[v], u = to[e];
      if (cap[e] && d[u] == d[v] + 1) {
        long long x = dfs(u, T, min(flow, cap[e]));
        flow -= x, cap[e] -= x, cap[e ^ 1] += x;
        res += x;
      }
      if (!flow) break;
    }
    return res;
  }
  long long maxFlow(int S, int T) {
    long long res = 0;
    while (bfs(S, T)) res += dfs(S, T);
    return res;
  }
};
Flow<maxn * 2, maxn * maxn + 20> G;
Flow<maxn, maxn * maxn + 20> H;
bool A[maxn][maxn];
int match[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int S = 2 * n, T = S + 1;
  for (int i = 0; i < n; i++) {
    int d;
    cin >> d;
    while (d--) {
      int v;
      cin >> v;
      v--;
      A[i][v] = 1;
      G.add_edge(i, v + n, 1);
    }
    G.add_edge(S, i, 1);
  }
  for (int i = 0; i < n; i++) G.add_edge(i + n, T, 1);
  assert(G.maxFlow(S, T) == n);
  for (int i = 0; i < G.id; i += 2)
    if (G.from[i] != S && G.to[i] != T && !G.cap[i])
      match[G.from[i]] = G.to[i] - n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (i != j && A[i][match[j]]) H.add_edge(i, j, 1e9);
  S = n, T = S + 1;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x *= -1;
    if (x >= 0)
      H.add_edge(S, i, x), sum += x;
    else
      H.add_edge(i, T, -x);
  }
  cout << -sum + H.maxFlow(S, T) << endl;
}