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 chkmin(T& x, T y) {
  return x > y ? x = y, true : false;
}
template <class T>
inline bool chkmax(T& x, T y) {
  return x < y ? x = y, true : false;
}
inline int read(void) {
  int x, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
template <typename T>
struct Graph {
  enum { maxn = 305, maxm = 200000 };
  struct Edge {
    int to, nxt;
    T w, cap, flow;
  } v[maxm];
  int h[maxn], cnt;
  Graph() { memset(h, -1, sizeof(h)); }
  void add1(int x, int y, T w, T cap) {
    v[cnt] = Edge{y, h[x], w, cap, 0};
    h[x] = cnt++;
  }
  void add(int x, int y, T w, T cap) {
    add1(x, y, w, cap);
    add1(y, x, -w, 0);
  }
};
Graph<int> G;
int Ss, Se;
static int f[305];
int dfs(int x, int re) {
  if (x == Se || re <= 0) return re;
  int s1 = 0, t;
  for (int i = G.h[x]; i != -1; i = G.v[i].nxt) {
    int to = G.v[i].to;
    if (f[to] != f[x] + 1) continue;
    t = dfs(to, min(re, G.v[i].cap - G.v[i].flow));
    G.v[i].flow += t;
    G.v[i ^ 1].flow -= t;
    s1 += t;
    re -= t;
    if (re == 0) break;
  }
  return s1;
}
bool bfs(int x) {
  queue<int> Q;
  memset(f, -1, sizeof(f));
  Q.push(x);
  f[x] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = G.h[u]; i != -1; i = G.v[i].nxt) {
      if (G.v[i].flow >= G.v[i].cap) continue;
      int to = G.v[i].to;
      if (f[to] != -1) continue;
      f[to] = f[u] + 1;
      Q.push(to);
    }
  }
  return f[Se] != -1;
}
int dinic() {
  int ans = 0;
  while (bfs(Ss)) ans += dfs(Ss, 1000000000);
  return ans;
}
bool vi[305];
int n, lk[305], v, x, ans, w;
vector<int> V[305];
bool dfs(int x) {
  vi[x] = 1;
  for (int i = 0; i <= V[x].size() - 1; ++i) {
    int to = V[x][i];
    if (!lk[to] || (!vi[lk[to]] && dfs(lk[to]))) {
      lk[to] = x;
      return 1;
    }
  }
  return 0;
}
int main(void) {
  n = read();
  for (int i = 1; i <= n; ++i) {
    v = read();
    while (v--) {
      x = read();
      V[i].push_back(x);
    }
  }
  for (int i = 1; i <= n; ++i) {
    memset(vi, 0, sizeof(vi));
    dfs(i);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= V[i].size() - 1; ++j)
      G.add(i, lk[V[i][j]], 0, 1000000000);
  Ss = n + 1;
  Se = n + 2;
  for (int i = 1; i <= n; ++i) {
    w = -read();
    if (w < 0)
      G.add(i, Se, 0, -w);
    else
      G.add(Ss, i, 0, w), ans += w;
  }
  printf("%d\n", -(ans - dinic()));
  return 0;
}