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>
const int inf = 1000000000;
const int large = 300000001;
int n, src, sink;
int deg[605], adj[605][605];
int res[605][605];
int ql, qr, que[605];
int pred[605], aug[605];
int visited[605] = {0}, visid = 0;
inline int left(int v) { return v; }
inline int right(int v) { return v + n; }
inline void append(int v, int u, int c) {
  adj[v][deg[v]++] = u;
  if (u != sink && v != src) adj[u][deg[u]++] = v;
  res[v][u] = c;
  res[u][v] = 0;
}
inline int min(int a, int b) { return a < b ? a : b; }
inline bool bfs() {
  ++visid;
  ql = qr = 0;
  que[qr++] = src;
  visited[src] = visid;
  aug[src] = inf;
  while (ql < qr) {
    int v = que[ql++];
    if (v == sink) return 1;
    for (int i = 0; i < deg[v]; i++) {
      int u = adj[v][i];
      if (!res[v][u]) continue;
      if (visited[u] == visid) continue;
      aug[u] = min(aug[v], res[v][u]);
      pred[u] = v;
      que[qr++] = u;
      visited[u] = visid;
    }
  }
  return 0;
}
inline void augment() {
  int v = sink, a = aug[sink];
  while (v != src) {
    res[v][pred[v]] += a;
    res[pred[v]][v] -= a;
    v = pred[v];
  }
}
inline long long mincut() {
  long long f = 0;
  while (bfs()) {
    f += aug[sink];
    augment();
  }
  return f;
}
int main(void) {
  while (scanf("%d", &n) == 1) {
    src = 0;
    sink = 2 * n + 1;
    memset(deg, 0, sizeof(deg));
    for (int i = 1; i <= n; i++) {
      int d, j;
      scanf("%d", &d);
      while (d--) {
        scanf("%d", &j);
        append(left(i), right(j), inf);
      }
    }
    long long costsum = 0;
    for (int i = 1; i <= n; i++) {
      int c;
      scanf("%d", &c);
      costsum += c;
      append(src, left(i), large - c);
      append(right(i), sink, large);
    }
    int sol = costsum + mincut() - (long long)n * large;
    printf("%d\n", sol);
  }
  return 0;
}