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 Data, Weight, Next;
};
int Get() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && (c != '-'))
    ;
  bool Flag = (c == '-');
  if (Flag) c = getchar();
  int X = c - 48;
  while (c = getchar(), c >= '0' && c <= '9') X = X * 10 + c - 48;
  return Flag ? -X : X;
}
const int inf = 1000000000;
vector<int> List[300];
int Pair[300];
bool Visited[300];
int G[302];
Edge E[180000];
bool Find(int X) {
  if (X == -1) return true;
  if (Visited[X]) return false;
  Visited[X] = true;
  for (int i = 0; i < List[X].size(); i++)
    if (Find(Pair[List[X][i]])) {
      Pair[List[X][i]] = X;
      return true;
    }
  return false;
}
void Add(int Number, int X, int Y, int W) {
  E[Number * 2] = (Edge){Y, W, G[X]};
  G[X] = Number * 2;
  E[Number * 2 + 1] = (Edge){X, 0, G[Y]};
  G[Y] = Number * 2 + 1;
}
int MaxFlow(int S, int T, int N) {
  static int Dist[302], Count[303], Start[302], Pre[302];
  memset(Dist, 0, sizeof(Dist));
  memset(Count, 0, sizeof(Count));
  Count[0] = N;
  for (int i = 0; i < N; i++) Start[i] = G[i];
  int Ans = 0, Flow = inf, V = S;
  while (Dist[S] < N) {
    int P = Start[V];
    while (P != -1) {
      if (E[P].Weight && Dist[V] == Dist[E[P].Data] + 1) break;
      P = E[P].Next;
    }
    if (P != -1) {
      Start[V] = P;
      V = E[P].Data;
      Pre[V] = P;
      Flow = min(Flow, E[P].Weight);
      if (V == T) {
        Ans += Flow;
        while (V != S) {
          E[Pre[V]].Weight -= Flow;
          E[Pre[V] ^ 1].Weight += Flow;
          V = E[Pre[V] ^ 1].Data;
        }
        Flow = inf;
      }
    } else {
      int Min = N, P = G[V];
      while (P != -1) {
        if (E[P].Weight && Dist[E[P].Data] + 1 < Min) {
          Min = Dist[E[P].Data] + 1;
          Start[V] = P;
        }
        P = E[P].Next;
      }
      if (!--Count[Dist[V]]) break;
      Count[Dist[V] = Min]++;
      if (V != S) V = E[Pre[V] ^ 1].Data;
    }
  }
  return Ans;
}
int main() {
  int N = Get();
  for (int i = 0; i < N; i++) {
    int M = Get();
    for (int j = 0; j < M; j++) List[i].push_back(Get() - 1);
  }
  memset(Pair, -1, sizeof(Pair));
  for (int i = 0; i < N; i++) {
    memset(Visited, 0, sizeof(Visited));
    Find(i);
  }
  int Count = 0;
  memset(G, -1, sizeof(G));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < List[i].size(); j++)
      Add(Count++, i, Pair[List[i][j]], inf);
  int Ans = 0, S = N, T = N + 1;
  for (int i = 0; i < N; i++) {
    int V = Get();
    if (V < 0) Ans += V;
    if (V < 0) Add(Count++, S, i, -V);
    if (V > 0) Add(Count++, i, T, V);
  }
  printf("%d\n", MaxFlow(S, T, N + 2) + Ans);
  return 0;
}