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 INF = 2000000005;
const int INFW = 1000000005;
struct Edge {
  int src, dst;
  int weight;
  Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}
int maximumFlow(const vector<vector<Edge> > &g, int s, int t) {
  int n = g.size();
  vector<vector<int> > flow(n, vector<int>(n)), capacity(n, vector<int>(n));
  for (int u = 0; u < (int)(n); ++u)
    for (__typeof((g[u]).begin()) e = (g[u]).begin(); e != (g[u]).end(); ++e)
      capacity[e->src][e->dst] += e->weight;
  int total = 0;
  while (1) {
    queue<int> Q;
    Q.push(s);
    vector<int> prev(n, -1);
    prev[s] = s;
    while (!Q.empty() && prev[t] < 0) {
      int u = Q.front();
      Q.pop();
      for (__typeof((g[u]).begin()) e = (g[u]).begin(); e != (g[u]).end(); ++e)
        if (prev[e->dst] < 0 && (capacity[u][e->dst] - flow[u][e->dst]) > 0) {
          prev[e->dst] = u;
          Q.push(e->dst);
        }
    }
    if (prev[t] < 0) return total;
    int inc = INF;
    for (int j = t; prev[j] != j; j = prev[j])
      inc = min(inc, (capacity[prev[j]][j] - flow[prev[j]][j]));
    for (int j = t; prev[j] != j; j = prev[j])
      flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
    total += inc;
  }
}
void add_edge(vector<vector<Edge> > &g, int i, int j, int cap) {
  g[i].push_back(Edge(i, j, cap));
  g[j].push_back(Edge(j, i, 0));
}
int main() {
  int n;
  cin >> n;
  vector<vector<Edge> > g(n * 2 + 2, vector<Edge>());
  for (int i = 0; i < (int)(n); ++i) {
    int m;
    cin >> m;
    while (m--) {
      int a;
      cin >> a;
      a--;
      add_edge(g, i, n + a, INFW);
    }
  }
  int wsum = 0;
  for (int i = 0; i < (int)(n); ++i) {
    int w;
    cin >> w;
    wsum += w;
    add_edge(g, n * 2, i, INFW - w);
    add_edge(g, n + i, n * 2 + 1, INFW);
  }
  int minCut = maximumFlow(g, n * 2, n * 2 + 1);
  cout << wsum + minCut - n * INFW << endl;
  return 0;
}