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;
int n, p[510], ans, belong[510], to[510];
vector<int> c[510];
bool bj[510];
inline int getint() {
  int w = 0, q = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') q = 1, c = getchar();
  while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
  return q ? -w : w;
}
struct DINIC {
  int deep[510], dl[510], head, tail, N;
  bool bj[510];
  vector<int> a[510], val[510], ba[510];
  void link(int x, int y, int z) {
    a[x].push_back(y), val[x].push_back(z);
    a[y].push_back(x), val[y].push_back(0);
    ba[x].push_back(a[y].size() - 1);
    ba[y].push_back(a[x].size() - 1);
  }
  void bfs() {
    int x, w, v;
    head = 0, tail = 1, dl[1] = 0;
    memset(bj, 0, sizeof(bj));
    bj[0] = 1;
    deep[0] = 0;
    do {
      x = dl[++head], w = a[x].size();
      for (int i = 0; i < w; i++) {
        v = a[x][i];
        if (val[x][i] <= 0 || bj[v]) continue;
        deep[v] = deep[x] + 1, bj[v] = 1;
        dl[++tail] = v;
      }
    } while (head != tail);
  }
  int dfs(int x, int low) {
    int inc = 0, va;
    int w = a[x].size(), v;
    if (x == N) return low;
    for (int i = 0; i < w; i++) {
      v = a[x][i];
      if (deep[v] == deep[x] + 1 && val[x][i] &&
          (va = dfs(v, min(low, val[x][i])))) {
        val[x][i] -= va;
        val[v][ba[x][i]] += va;
        inc += va;
        return va;
      }
    }
    if (!inc) deep[x] = -1;
    return 0;
  }
  int maxflow() {
    int sum = 0;
    while (1) {
      bfs();
      if (!bj[N]) break;
      sum += dfs(0, 0x7fffffff);
    }
    return sum;
  }
} G;
void init() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int k = getint();
    for (int j = 1; j <= k; j++) {
      int x = getint();
      c[i].push_back(x);
    }
  }
  for (int i = 1; i <= n; i++) {
    p[i] = getint();
    p[i] *= -1;
    if (p[i] > 0) ans += p[i];
  }
}
bool find(int x) {
  int w = c[x].size();
  for (int i = 0; i < w; i++) {
    int v = c[x][i];
    if (bj[v]) continue;
    bj[v] = 1;
    if (!belong[v] || find(belong[v])) {
      belong[v] = x;
      return 1;
    }
  }
  return 0;
}
void make_g(int x) {
  if (p[x] >= 0)
    G.link(0, x, p[x]);
  else
    G.link(x, G.N, p[x] * -1);
  int w = c[x].size();
  for (int i = 0; i < w; i++) {
    int v = c[x][i];
    if (v == to[x]) continue;
    G.link(x, belong[v], 0x7fffffff);
  }
}
int main() {
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) bj[j] = 0;
    find(i);
  }
  G.N = n + 1;
  for (int i = 1; i <= n; i++) to[belong[i]] = i;
  for (int i = 1; i <= n; i++) make_g(i);
  ans -= G.maxflow();
  cout << ans * -1;
  return 0;
}