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;
vector<int> v[305];
int lk[305], vis[305], S, T, x[305];
int h[305 * 305 << 1], to[305 * 305 << 1], ne[305 * 305 << 1],
    en = 0, fl[305 * 305 << 1];
void add(int a, int b, int c) {
  to[en] = b;
  ne[en] = h[a];
  fl[en] = c;
  h[a] = en++;
  to[en] = a;
  ne[en] = h[b];
  fl[en] = 0;
  h[b] = en++;
}
bool dfs(int o) {
  for (int i = 0; i < (int)v[o].size(); ++i)
    if (!vis[v[o][i]]) {
      vis[v[o][i]] = 1;
      if ((!lk[v[o][i]]) || (dfs(lk[v[o][i]]))) {
        lk[v[o][i]] = o;
        return true;
      }
    }
  return false;
}
queue<int> q;
int dis[305];
bool tell() {
  memset(dis, -1, sizeof dis);
  dis[S] = 0;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = h[x]; i >= 0; i = ne[i])
      if (dis[to[i]] == -1 && fl[i] > 0) {
        dis[to[i]] = dis[x] + 1;
        q.push(to[i]);
      }
  }
  return dis[T] != -1;
}
int zeng(int k, int now) {
  if (k == T) return now;
  int ret = 0;
  for (int i = h[k]; i >= 0; i = ne[i])
    if (dis[to[i]] == dis[k] + 1 && fl[i] > 0) {
      int tmp = zeng(to[i], min(now - ret, fl[i]));
      fl[i] -= tmp;
      fl[i ^ 1] += tmp;
      ret += tmp;
    }
  if (!ret) dis[k] = -1;
  return ret;
}
int dinic() {
  int tmp, ret = 0;
  while (tell()) {
    while ((tmp = zeng(S, 0x3f3f3f3f))) ret += tmp;
  }
  return ret;
}
int main() {
  memset(h, -1, sizeof h);
  int n;
  scanf("%d", &n);
  S = n + 1;
  T = n + 2;
  for (int i = 1; i <= n; ++i) {
    int y;
    scanf("%d", &x[i]);
    for (int j = 1; j <= x[i]; ++j) {
      scanf("%d", &y);
      v[i].push_back(y);
    }
  }
  for (int i = 1; i <= n; ++i) {
    memset(vis, 0, sizeof vis);
    dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < (int)v[i].size(); ++j) {
      if (lk[v[i][j]] != i) {
        add(i, lk[v[i][j]], 0x3f3f3f3f);
      }
    }
    int w;
    scanf("%d", &w);
    if (w >= 0)
      add(i, T, w);
    else
      add(S, i, -w), ans -= w;
  }
  printf("%d\n", -(ans - dinic()));
}