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;
bool has[305][305];
int ll[305], lr[305], n, w[305];
bool vis[305];
bool dfs(int u) {
  if (vis[u]) return false;
  vis[u] = true;
  for (int v = 0; v < n; v++)
    if (has[u][v] && (lr[v] == -1 || dfs(lr[v])))
      return ll[u] = v, lr[v] = u, true;
  return false;
}
struct Maxflow {
  struct Edge {
    int t, c;
    Edge *n, *r;
    Edge(int _t, int _c, Edge* _n) : t(_t), c(_c), n(_n) {}
  };
  vector<Edge*> E;
  int addV() {
    E.push_back(NULL);
    return E.size() - 1;
  }
  Edge* makeEdge(int s, int t, int c) { return E[s] = new Edge(t, c, E[s]); }
  void addEdge(int s, int t, int c) {
    Edge *e1 = makeEdge(s, t, c), *e2 = makeEdge(t, s, 0);
    e1->r = e2, e2->r = e1;
  }
  int calcMaxFlow(int vs, int vt, int INF = ~0U >> 2) {
    int nV = E.size(), ttf = 0;
    vector<int> am(nV, 0), h(nV, 0), cnt(nV + 1, 0);
    vector<Edge*> pe(nV, (Edge*)(0)), cur(nV, (Edge*)(0));
    cnt[0] = nV;
    int u = vs;
    Edge* e;
    am[u] = INF;
    while (h[vs] < nV) {
      for (e = cur[u]; e; e = e->n)
        if (e->c > 0 && h[u] == h[e->t] + 1) break;
      if (e) {
        int v = e->t;
        cur[u] = pe[v] = e;
        am[v] = min(am[u], e->c);
        u = v;
        if (u == vt) {
          int by = am[u];
          while (u != vs) {
            pe[u]->c -= by;
            pe[u]->r->c += by;
            u = pe[u]->r->t;
          }
          ttf += by;
          am[u] = INF;
        }
      } else {
        if (!--cnt[h[u]]) break;
        h[u] = nV;
        for (e = E[u]; e; e = e->n)
          if (e->c > 0 && h[e->t] + 1 < h[u]) {
            h[u] = h[e->t] + 1;
            cur[u] = e;
          }
        ++cnt[h[u]];
        if (u != vs) u = pe[u]->r->t;
      }
    }
    return ttf;
  }
};
int main() {
  scanf("%d", &n);
  int c, x;
  for (int i = 0; i < n; i++) {
    scanf("%d", &c);
    for (int j = 0; j < c; j++) {
      scanf("%d", &x);
      has[i][--x] = true;
    }
  }
  for (int i = 0; i < n; i++) scanf("%d", &w[i]);
  memset(ll, -1, sizeof ll);
  memset(lr, -1, sizeof lr);
  for (int i = 0; i < n; i++) {
    memset(vis, false, sizeof vis);
    if (!dfs(i)) {
      cout << "OH NO!" << endl;
      return 0;
    }
  }
  Maxflow sol;
  for (int i = 0; i < n; i++) sol.addV();
  int vs = sol.addV(), vt = sol.addV();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    w[i] = -w[i];
    if (w[i] >= 0) {
      ans += w[i];
      sol.addEdge(vs, i, w[i]);
    } else
      sol.addEdge(i, vt, -w[i]);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (has[i][j]) sol.addEdge(i, lr[j], INT_MAX);
  cout << sol.calcMaxFlow(vs, vt, INT_MAX) - ans << endl;
}