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;
struct edge {
  int from, to;
  int flow, cap;
};
struct DinicFlow {
  vector<edge> edges;
  vector<int> dist, now;
  vector<vector<int> > adj;
  int n, src, snk;
  const int inf = 1e9 + 7;
  DinicFlow() {}
  DinicFlow(int nn) {
    n = nn;
    dist.resize(n);
    now.resize(n);
    adj.resize(n);
  }
  void addEdge(int from, int to, int cap) {
    adj[from].push_back(edges.size());
    edges.push_back({from, to, 0, cap});
    adj[to].push_back(edges.size());
    edges.push_back({to, from, 0, 0});
  }
  bool bfs() {
    for (int i = 0; i < n; ++i) {
      dist[i] = inf;
      now[i] = (int)adj[i].size() - 1;
    }
    queue<int> Q;
    Q.push(src), dist[src] = 0;
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (int w : adj[v]) {
        edge& e = edges[w];
        if (dist[e.to] > dist[v] + 1 && e.cap - e.flow > 0) {
          dist[e.to] = dist[v] + 1;
          Q.push(e.to);
        }
      }
    }
    return dist[snk] != inf;
  }
  int dfs(int u, int flow) {
    if (u == snk || !flow) return flow;
    for (int& w = now[u]; w >= 0; --w) {
      edge& e = edges[adj[u][w]];
      if (dist[e.to] == dist[u] + 1 && e.cap > e.flow) {
        int fl = dfs(e.to, min(flow, e.cap - e.flow));
        if (fl > 0) {
          e.flow += fl;
          edges[adj[u][w] ^ 1].flow -= fl;
          return fl;
        }
      }
    }
    return 0;
  }
  int getFlow(int source, int sink) {
    src = source, snk = sink;
    int totFlow = 0;
    int flowLimit = std::numeric_limits<int>::max() >> 2;
    while (bfs()) {
      while (int f = dfs(src, flowLimit)) {
        totFlow += f;
      }
    }
    return totFlow;
  }
};
int main() {
  int n;
  cin >> n;
  vector<vector<int> > sets(n);
  for (int i = 0; i < n; ++i) {
    int m;
    cin >> m;
    for (int j = 0; j < m; ++j) {
      int x;
      cin >> x;
      sets[i].push_back(x - 1);
    }
  }
  vector<int> cost(n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
    cost[i] *= -1;
    ans += max(0, cost[i]);
  }
  DinicFlow df(2 * n + 2);
  for (int i = 0; i < n; ++i) {
    df.addEdge(0, i + 1, 1);
    df.addEdge(i + 1 + n, 2 * n + 1, 1);
    for (int x : sets[i]) df.addEdge(i + 1, x + 1 + n, 1);
  }
  int fl = df.getFlow(0, 2 * n + 1);
  assert(fl == n);
  cerr << "matching = " << fl << "\n";
  vector<int> myMatch(n);
  vector<int> myInvMatch(n);
  for (edge e : df.edges) {
    if (e.flow > 0 && e.from != 0 && e.to != 2 * n + 1) {
      myMatch[e.from - 1] = e.to - 1 - n;
      myInvMatch[e.to - 1 - n] = e.from - 1;
    }
  }
  DinicFlow df1(n + 2);
  for (int i = 0; i < n; ++i) {
    for (int x : sets[i])
      if (x != myMatch[i]) {
        df1.addEdge(i + 1, myInvMatch[x] + 1, 100000000);
      }
  }
  for (int i = 0; i < n; ++i) {
    if (cost[i] > 0)
      df1.addEdge(0, i + 1, cost[i]);
    else
      df1.addEdge(i + 1, n + 1, -cost[i]);
  }
  ans -= df1.getFlow(0, n + 1);
  cout << -ans << "\n";
  return 0;
}