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 <class T>
inline bool checkMax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMax(T &a, const Args... arg) {
  checkMax(a, max(arg...));
}
template <class T>
inline bool checkMin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMin(T &a, const Args... arg) {
  checkMin(a, min(arg...));
}
const int INF = 0x3f3f3f3f;
const long long llINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 305;
const int LIM = 3e8 + 5;
int n, tot, S, T;
namespace NetWorkFlow {
const int MAXN = 605, MAXM = 2e5 + 5;
int dep[MAXN];
long long maxflow;
queue<int> q;
struct Graph {
  int len, head[MAXN], to[MAXM], nxt[MAXM], flow[MAXM], cur[MAXM];
  Graph() {
    memset(head, -1, sizeof(head));
    len = 1;
  }
  void add_edge(int u, int v, int f) {
    to[++len] = v;
    nxt[len] = head[u];
    flow[len] = f;
    head[u] = len;
    to[++len] = u;
    nxt[len] = head[v];
    flow[len] = 0;
    head[v] = len;
  }
} G;
bool bfs() {
  for (int i = 1; i <= tot; i++) dep[i] = -1, G.cur[i] = G.head[i];
  dep[S] = 0;
  q.push(S);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = G.head[u]; i != -1; i = G.nxt[i]) {
      int v = G.to[i];
      if (dep[v] == -1 && G.flow[i]) {
        dep[v] = dep[u] + 1;
        q.push(v);
      }
    }
  }
  return dep[T] != -1;
}
int dfs(int u, int flow) {
  if (u == T) {
    maxflow += flow;
    return flow;
  }
  int used = 0, low;
  for (int &i = G.cur[u]; i != -1; i = G.nxt[i]) {
    int v = G.to[i];
    if (dep[v] == dep[u] + 1 && G.flow[i]) {
      if (low = dfs(v, min(G.flow[i], flow - used))) {
        used += low;
        G.flow[i] -= low;
        G.flow[i ^ 1] += low;
        if (used == flow) break;
      }
    }
  }
  return used;
}
long long Dinic() {
  while (bfs()) dfs(S, INF);
  return maxflow;
}
}  // namespace NetWorkFlow
using NetWorkFlow::G;
int main() {
  scanf("%d", &n);
  tot = n + n;
  S = ++tot;
  T = ++tot;
  for (int i = 1, m, x; i <= n; i++) {
    scanf("%d", &m);
    G.add_edge(i + n, T, LIM);
    while (m--) scanf("%d", &x), G.add_edge(i, x + n, INF);
  }
  long long ans = 0;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    G.add_edge(S, i, LIM - x);
    ans += LIM - x;
  }
  ans -= NetWorkFlow::Dinic();
  printf("%lld\n", min(0ll, -ans));
  return 0;
}