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 long long int BILLION = 1e9, INF = 1e18;
struct maxflow {
  struct edge {
    long long int u, v, cap, flow;
    long long int rem() { return cap - flow; }
  };
  long long int n;
  vector<long long int> *adj;
  vector<edge> e;
  maxflow(long long int n) : n(n) {
    adj = new vector<long long int>[n];
    e.clear();
  }
  void addEdge(long long int u, long long int v, long long int cap) {
    adj[u].push_back(((long long int)(e).size()));
    e.push_back((edge){u, v, cap, 0});
    adj[v].push_back(((long long int)(e).size()));
    e.push_back((edge){v, u, 0, 0});
  }
  long long int *dist, *pre, *cap;
  long long int flow(long long int s, long long int t) {
    dist = new long long int[n];
    pre = new long long int[n];
    cap = new long long int[n];
    long long int res = 0;
    for (;;) {
      fill(dist, dist + n, (long long int)INF);
      dist[s] = 0;
      fill(pre, pre + n, -1);
      fill(cap, cap + n, 0);
      cap[s] = INF;
      queue<long long int> q;
      q.push(s);
      while (!q.empty()) {
        long long int u = q.front();
        q.pop();
        if (u == t) break;
        for (long long int i = 0;
             i < (long long int)(((long long int)(adj[u]).size())); i++) {
          edge E = e[adj[u][i]];
          if (E.rem() > 0 && dist[E.v] == INF) {
            dist[E.v] = dist[u] + 1;
            pre[E.v] = adj[u][i];
            cap[E.v] = min(cap[u], E.rem());
            q.push(E.v);
          }
        }
      }
      if (pre[t] == -1) break;
      res += cap[t];
      for (long long int v = t; v != s;) {
        long long int u = e[pre[v]].u;
        e[pre[v]].flow += cap[t];
        e[pre[v] ^ 1].flow -= cap[t];
        v = u;
      }
    }
    return res;
  }
};
int n;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  maxflow mf(2 * n + 2);
  for (int i = 0; i < (int)(n); i++) {
    int k;
    cin >> k;
    while (k--) {
      int j;
      cin >> j;
      j--;
      mf.addEdge(i, n + j, INF);
    }
  }
  int S = 2 * n, T = 2 * n + 1;
  long long int sum = 0;
  for (int i = 0; i < (int)(n); i++) {
    int x;
    cin >> x;
    mf.addEdge(S, i, BILLION - x);
    mf.addEdge(n + i, T, BILLION);
    sum += x;
  }
  auto res = mf.flow(S, T);
  res -= n * BILLION;
  res += sum;
  cout << res << endl;
  return 0;
}