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;
struct edge {
  int b, l, c;
} g[91005 << 1];
int gap[305], cur[305], h[305], t, dis[305], n, pre[305];
int graph[305][305], link[305], vis[305];
bool hungry(int u) {
  for (int i = 1; i <= n; i++)
    if (vis[i] != t && graph[u][i]) {
      vis[i] = t;
      if (!link[i] || hungry(link[i])) {
        link[i] = u;
        return 1;
      }
    }
  return 0;
}
int arc(int i) { return i + (i & 1 ? 1 : -1); }
void addedge(int a, int b, int c) {
  g[++t] = (edge){b, h[a], c};
  h[a] = t;
  g[++t] = (edge){a, h[b], 0};
  h[b] = t;
}
void bfs(int u) {
  int i;
  queue<int> q;
  for (i = 0; i <= n; i++) dis[i] = n, gap[i] = 0;
  gap[dis[u] = 0] = 1;
  q.push(u);
  while (q.size()) {
    u = q.front();
    q.pop();
    for (i = h[u]; i; i = g[i].l)
      if (dis[g[i].b] >= n) {
        gap[dis[g[i].b] = dis[u] + 1]++;
        q.push(g[i].b);
      }
  }
}
int sap(int x, int y) {
  int i, d, u = x, flow = 0;
  bfs(y);
  for (i = 1; i <= n; i++) cur[i] = h[i];
  while (dis[x] < n) {
    for (i = cur[u]; i; i = g[i].l)
      if (dis[u] == dis[g[i].b] + 1 && g[i].c) break;
    if (i) {
      cur[u] = i;
      pre[u = g[i].b] = i;
      if (u == y) {
        for (i = pre[y], d = 1e9; i; i = pre[g[arc(i)].b]) d = min(d, g[i].c);
        for (flow += d, u = x, i = pre[y]; i; i = pre[g[arc(i)].b]) {
          g[i].c -= d;
          g[arc(i)].c += d;
        }
      }
    } else {
      for (i = cur[u] = h[u], d = n; i; i = g[i].l)
        if (g[i].c) d = min(d, dis[g[i].b] + 1);
      if (!--gap[dis[u]]) break;
      gap[dis[u] = d]++;
      if (u != x) u = g[arc(pre[u])].b;
    }
  }
  return flow;
}
int main() {
  int i, j, s;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &j);
    while (j--) {
      scanf("%d", &s);
      graph[i][s] = 1;
    }
  }
  for (i = t = 1; i <= n; i++, t++) hungry(i);
  for (i = 1, t = 0; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (graph[i][j]) addedge(i, link[j], 1e9);
  for (i = 1, s = 0; i <= n; i++) {
    scanf("%d", &j);
    if (!j) continue;
    if (j > 0)
      addedge(i, n + 2, j);
    else {
      s -= j;
      addedge(n + 1, i, -j);
    }
  }
  n += 2;
  printf("%d", sap(n - 1, n) - s);
  return 0;
}