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>
namespace IO {
char buf[1000000], *p1, *p2;
inline char getc() {
  if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 10, stdin);
  return p1 == p2 ? EOF : *(p1++);
}
template <typename tp>
inline void r(tp &n) {
  n = 0;
  char c = getc();
  int f = 1;
  while (!isdigit(c) && c != '-') c = getc();
  if (c == '-') c = getc(), f = -1;
  while (isdigit(c)) n = n * 10 + c - 48, c = getc();
  n *= f;
}
template <typename tp>
inline void w(tp n) {
  if (n < 0) putchar('-'), n = -n;
  if (n / 10) w(n / 10);
  putchar(n % 10 + 48);
}
};  // namespace IO
using namespace IO;
const int N = 6e2 + 5, M = 2e3 + 5, INF = 1e9, MAX = 1e8;
using namespace std;
int n, S, T, num = 1;
int d[N], v[N];
int head[N], nt[M << 1], to[M << 1], cap[M << 1];
long long ans;
bool BFS() {
  for (int i = S; i <= T; ++i) d[i] = 0;
  queue<int> q;
  q.push(S);
  d[S] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = nt[i]) {
      if (!cap[i] || d[to[i]]) continue;
      d[to[i]] = d[u] + 1;
      q.push(to[i]);
      if (to[i] == T) return true;
    }
  }
  return false;
}
int dinic(int p, int flow) {
  if (p == T) return flow;
  int res = flow;
  for (int i = head[p]; i && res; i = nt[i]) {
    if (!cap[i] || d[to[i]] != d[p] + 1) continue;
    int k = dinic(to[i], min(cap[i], res));
    if (!k) d[to[i]] = 0;
    cap[i] -= k;
    cap[i ^ 1] += k;
    res -= k;
  }
  return flow - res;
}
void Add(int x, int y, int z) {
  ++num;
  nt[num] = head[x];
  head[x] = num;
  to[num] = y;
  cap[num] = z;
  ++num;
  nt[num] = head[y];
  head[y] = num;
  to[num] = x;
  cap[num] = 0;
}
int main() {
  r(n);
  S = 0, T = n + n + 1;
  for (int i = 1, k, x; i <= n; ++i) {
    r(k);
    while (k--) r(x), Add(i, x + n, INF);
  }
  for (int i = 1; i <= n; ++i)
    r(v[i]), Add(S, i, MAX - v[i]), Add(i + n, T, MAX), ans += MAX - v[i];
  int flow;
  while (BFS())
    while (flow = dinic(S, INF)) ans -= flow;
  w(min(-ans, 0ll));
  return 0;
}