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 N = 666;
const int inf = 1e9;
struct edge {
  int pr;
  int to;
  int cap;
} e[111111];
int tot = 1, la[N];
void clr() {
  tot = 1;
  memset(la, 0, sizeof(la));
}
void adde(int x, int y, int z) {
  e[++tot].pr = la[x];
  la[x] = tot;
  e[tot].to = y;
  e[tot].cap = z;
}
void addf(int x, int y, int z = inf) {
  adde(x, y, z);
  adde(y, x, 0);
}
int d[N];
int bfs(int s, int t) {
  queue<int> Q;
  int i, x, y;
  memset(d, 0, sizeof(d));
  d[s] = 1;
  Q.push(s);
  while (!Q.empty()) {
    x = Q.front();
    Q.pop();
    for (i = la[x]; i; i = e[i].pr) {
      if (!e[i].cap) continue;
      y = e[i].to;
      if (!d[y]) {
        d[y] = d[x] + 1;
        Q.push(y);
      }
    }
  }
  return d[t];
}
int dfs(int s, int t, int f) {
  if (s == t) return f;
  int i, x, y, r = 0;
  for (i = la[s]; i; i = e[i].pr) {
    if (!e[i].cap || !f) continue;
    y = e[i].to;
    if (d[y] != d[s] + 1) continue;
    x = dfs(y, t, min(f, e[i].cap));
    if (x) {
      f -= x;
      r += x;
      e[i].cap -= x;
      e[i ^ 1].cap += x;
    }
  }
  if (!r) d[s] = 0;
  return r;
}
int maxflow(int s, int t) {
  int x, r = 0;
  while (bfs(s, t)) {
    x = dfs(s, t, inf);
    if (x)
      r += x;
    else
      break;
  }
  return r;
}
int vis[N], w[N];
bool hungary(int x) {
  int i, y;
  for (i = la[x]; i; i = e[i].pr) {
    y = e[i].to;
    if (!vis[y]) {
      vis[y] = 1;
      if (!w[y] || hungary(w[y])) {
        w[y] = x;
        return true;
      }
    }
  }
  return false;
}
int n, a[N][N];
int main() {
  int i, j, o, s, t, ans;
  scanf("%d", &n);
  clr();
  for (i = 1; i <= n; i = i + 1) {
    scanf("%d", &o);
    while (o--) {
      scanf("%d", &j);
      a[i][j] = 1;
      adde(i, j, 0);
    }
  }
  for (i = 1; i <= n; i = i + 1) {
    memset(vis, 0, sizeof(vis));
    hungary(i);
  }
  clr();
  s = 0, t = n + 1;
  for (i = 1; i <= n; i = i + 1)
    for (j = 1; j <= n; j = j + 1)
      if (a[i][j]) addf(i, w[j]);
  ans = 0;
  for (i = 1; i <= n; i = i + 1) {
    scanf("%d", &o);
    if (o > 0)
      addf(i, t, o);
    else {
      ans += o;
      addf(s, i, -o);
    }
  }
  printf("%d", ans + maxflow(s, t));
  return 0;
}