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;
const int inf = int(1e9);
struct edge {
  int v, f, c, next;
} e[100000];
int first[500], ff, n, S, T, C;
int color[500], cc, mate[500], w[500];
bool a[500][500];
bool dfs(int u) {
  if (color[u] == cc) return 0;
  color[u] = cc;
  for (int i = 0; i < n; ++i)
    if (a[u][i]) {
      if (mate[i] == -1 || dfs(mate[i])) {
        mate[i] = u;
        return 1;
      }
    }
  return 0;
}
void addEdge(int u, int v, int c) {
  e[ff].v = v, e[ff].c = c, e[ff].f = 0, e[ff].next = first[u], first[u] = ff++;
  e[ff].v = u, e[ff].c = 0, e[ff].f = 0, e[ff].next = first[v], first[v] = ff++;
}
int dfsFlow(int u, int flow) {
  if (u == T) return flow;
  if (color[u] == cc) return 0;
  color[u] = cc;
  for (int i = first[u]; i != -1; i = e[i].next)
    if (e[i].c - e[i].f >= C) {
      int x = dfsFlow(e[i].v, min(flow, e[i].c - e[i].f));
      if (x > 0) {
        e[i].f += x;
        e[i ^ 1].f -= x;
        return x;
      }
    }
  return 0;
}
int maxFlow() {
  int res = 0;
  C = (1 << 20);
  while (C) {
    int x = 0;
    do {
      ++cc;
      x = dfsFlow(S, inf);
      res += x;
    } while (x >= C);
    C >>= 1;
  }
  return res;
}
int main() {
  scanf("%d", &n);
  int i, j, k, x;
  for (i = 0; i < n; ++i) {
    scanf("%d", &k);
    for (j = 0; j < k; ++j) {
      scanf("%d", &x);
      a[i][x - 1] = 1;
    }
  }
  memset(mate, -1, sizeof(mate));
  for (i = 0; i < n; ++i) {
    ++cc;
    dfs(i);
  }
  memset(first, -1, sizeof(first));
  for (i = 0; i < n; ++i) {
    int u = mate[i];
    for (j = 0; j < n; ++j)
      if (i != j && a[u][j]) {
        int v = mate[j];
        addEdge(u, v, inf);
      }
  }
  S = n;
  T = n + 1;
  int wsum = 0;
  for (i = 0; i < n; ++i) {
    scanf("%d", &w[i]);
    if (w[i] < 0) {
      wsum -= w[i];
      addEdge(S, i, -w[i]);
    } else {
      addEdge(i, T, w[i]);
    }
  }
  int mf = maxFlow();
  cout << mf - wsum << endl;
  return 0;
}