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;
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& _p) {
  return os << "(" << _p.first << "," << _p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& _V) {
  bool f = true;
  os << "[";
  for (auto v : _V) {
    os << (f ? "" : ",") << v;
    f = false;
  }
  return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& _S) {
  bool f = true;
  os << "(";
  for (auto s : _S) {
    os << (f ? "" : ",") << s;
    f = false;
  }
  return os << ")";
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& _M) {
  return os << set<pair<T, U>>(_M.begin(), _M.end());
}
const signed long long INF = 1000000100;
const long double EPS = 1e-9;
namespace Matching {
const int MAXN = 50500;
int L, R;
vector<int> G[MAXN];
int A[MAXN];
int B[MAXN];
int TS[MAXN];
int ts;
void init(int _L, int _R) {
  L = _L;
  R = _R;
  for (int(i) = (0); (i) < (L); (i)++) {
    A[i] = -1;
    TS[i] = 0;
    G[i].clear();
  }
  for (int(i) = (0); (i) < (R); (i)++) B[i] = -1;
}
void add_edge(int a, int b) { G[a].push_back(b); }
bool dfs(int v) {
  TS[v] = ts;
  for (int u : G[v])
    if (B[u] == -1 or (TS[B[u]] != ts and dfs(B[u]))) {
      A[v] = u;
      B[u] = v;
      return true;
    }
  return false;
}
int run() {
  for (int(i) = (0); (i) < (L); (i)++) random_shuffle(G[i].begin(), G[i].end());
  ts = 0;
  int result = 0;
  int improvement = -1;
  while (improvement != 0) {
    improvement = 0;
    ++ts;
    for (int(i) = (0); (i) < (L); (i)++)
      if (A[i] == -1 and TS[i] != ts and dfs(i)) ++improvement;
    result += improvement;
  }
  return result;
}
};  // namespace Matching
namespace MaxFlow {
struct edge {
  int u, p;
  signed long long f, c;
  edge(int _u, signed long long _f, signed long long _c, int _p)
      : u(_u), p(_p), f(_f), c(_c) {}
};
const int MAXE = 30100;
const int MAXV = 5050;
int D[MAXV];
bool V[MAXV];
int L[MAXV];
vector<edge> G[MAXV];
int E, S, T;
void clear() {
  for (int(i) = (0); (i) < (MAXV); (i)++) G[i].clear();
  E = 0;
}
void add_edge(int a, int b, signed long long f) {
  ++E;
  if (a == b) return;
  G[a].push_back(edge(b, 0, f, int(G[b].size())));
  G[b].push_back(edge(a, 0, 0, int(G[a].size()) - 1));
}
bool bfs() {
  memset(V, 0, sizeof(V));
  queue<int> Q;
  Q.push(S);
  D[S] = 0;
  V[S] = true;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (edge& e : G[v])
      if (!V[e.u] and e.c - e.f > 0) {
        V[e.u] = true;
        D[e.u] = D[v] + 1;
        Q.push(e.u);
      }
  }
  return V[T];
}
signed long long dfs(int v, signed long long f) {
  if (v == T or f == 0) return f;
  signed long long r = 0;
  for (int& e_id = L[v]; e_id < int(G[v].size()); ++e_id) {
    edge& e = G[v][e_id];
    if (D[v] + 1 != D[e.u] or e.c == e.f) {
      continue;
    }
    signed long long c = dfs(e.u, min(f - r, e.c - e.f));
    e.f += c;
    G[e.u][e.p].f -= c;
    r += c;
    if (r == f) break;
  }
  return r;
}
signed long long run() {
  signed long long result = 0;
  while (bfs()) {
    memset(L, 0, sizeof(L));
    result += dfs(S, 10000000000000000LL);
  }
  return result;
}
}  // namespace MaxFlow
const int MAXN = 333;
int N;
vector<int> A[MAXN];
int W[MAXN];
void read_data() {
  scanf("%d", &N);
  for (int(i) = (1); (i) <= (N); (i)++) {
    int t;
    scanf("%d", &t);
    for (int(j) = (1); (j) <= (t); (j)++) {
      int a;
      scanf("%d", &a);
      A[i].push_back(a);
    }
  }
  for (int(i) = (1); (i) <= (N); (i)++) scanf("%d", W + i);
}
void solve() {
  Matching::init(N + 1, N + 1);
  for (int(i) = (1); (i) <= (N); (i)++)
    for (int a : A[i]) Matching::add_edge(i, a);
  int matching = Matching::run();
  int S = N + 1;
  int T = N + 2;
  MaxFlow::S = S;
  MaxFlow::T = T;
  for (int(v) = (1); (v) <= (N); (v)++)
    for (int u : Matching::G[v])
      if (u != Matching::A[v]) {
        MaxFlow::add_edge(v, Matching::B[u], INF);
      }
  int c = 0;
  for (int(v) = (1); (v) <= (N); (v)++) {
    if (W[v] < 0) {
      MaxFlow::add_edge(S, v, -W[v]);
      c += -W[v];
    } else {
      MaxFlow::add_edge(v, T, W[v]);
    }
  }
  int flow = MaxFlow::run();
  int result = -c + flow;
  cout << result << endl;
}
int main() {
  read_data();
  solve();
}