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;
int N, F, T;
struct edge {
  int f, t, cap, fl;
  edge(int _f = 0, int _t = 0, int _cap = 0) {
    f = _f;
    t = _t;
    cap = _cap;
    fl = 0;
  }
  int rc() { return cap - fl; }
};
vector<edge> U;
vector<int> fo[2000];
void init(int how) {
  U.clear();
  for (int i = 0; i < 2000; ++i) fo[i].clear();
  N = how + 2;
  F = N - 2;
  T = N - 1;
}
void add_edge(int f, int t, int cap, int rcap = 0) {
  U.push_back(edge(f, t, cap));
  U.push_back(edge(t, f, rcap));
  fo[f].push_back(U.size() - 2);
  fo[t].push_back(U.size() - 1);
}
int Q[2000], lev[2000], L, R;
bool bfs() {
  memset(lev, -1, sizeof lev);
  L = R = 0;
  Q[R++] = F;
  lev[F] = 0;
  while (L < R) {
    int V = Q[L++];
    for (int i = 0; i < fo[V].size(); ++i) {
      int to = U[fo[V][i]].t;
      if (lev[to] == -1 && U[fo[V][i]].rc() > 0) {
        Q[R++] = to;
        lev[to] = lev[V] + 1;
      }
    }
  }
  return (lev[T] != -1);
}
int pos[2000];
int dfs(int vr, int htp = 1000000) {
  if (!htp || vr == T) return htp;
  for (; pos[vr] < fo[vr].size(); ++pos[vr]) {
    int i = pos[vr];
    edge& E = U[fo[vr][i]];
    int cur;
    if (E.rc() > 0 && lev[vr] + 1 == lev[E.t] &&
        (cur = dfs(E.t, min(htp, E.rc()))) > 0) {
      E.fl += cur;
      U[fo[vr][i] ^ 1].fl -= cur;
      return cur;
    }
  }
  return 0;
}
int max_flow() {
  int FLOW = 0, cur;
  while (bfs()) {
    memset(pos, 0, sizeof pos);
    while ((cur = dfs(F))) FLOW += cur;
  }
  return FLOW;
}
map<int, int> RL;
int n, pr[300];
vector<int> S[300];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int Q;
    scanf("%d", &Q);
    S[i].resize(Q);
    while (Q-- > 0) {
      scanf("%d", &S[i][Q]);
      --S[i][Q];
    }
  }
  for (int i = 0; i < n; ++i) scanf("%d", &pr[i]);
  init(2 * n);
  for (int i = 0; i < n; ++i) {
    add_edge(F, i, 1);
    add_edge(i + n, T, 1);
    for (int j = 0; j < S[i].size(); ++j) add_edge(i, S[i][j] + n, 1);
  }
  max_flow();
  for (int i = 0; i < U.size(); ++i)
    if (U[i].fl == 1 && U[i].f != F && U[i].t != T) RL[U[i].t - n] = U[i].f;
  init(n);
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (pr[i] >= 0)
      add_edge(i, T, pr[i]);
    else {
      add_edge(F, i, -pr[i]);
      ans -= pr[i];
    }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < S[i].size(); ++j) add_edge(i, RL[S[i][j]], 1000000000);
  printf("%d\n", max_flow() - ans);
  return 0;
}