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;
namespace fastIO1 {
inline char read() {
  static const int IN_LEN = 1000000;
  static char buf[IN_LEN], *s, *t;
  return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),
          (s == t ? -1 : *s++) : *s++);
}
template <class T>
inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = read(); !isdigit(c); c = read()) {
    if (c == '-') iosig = true;
    if (c == -1) return;
  }
  for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig) x = -x;
}
inline char readc(char &c) {
  for (c = read(); !isalpha(c) && !isdigit(c); c = read())
    if (c == -1) return 0;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {
  if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
  *ooh++ = c;
}
template <class T>
inline void print(T x) {
  static int buf[30], cnt;
  if (x == 0)
    print('0');
  else {
    if (x < 0) print('-'), x = -x;
    for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
    while (cnt) print((char)buf[cnt--]);
  }
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
}  // namespace fastIO1
namespace fastIO2 {
template <class T>
inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
    if (c == '-') iosig = true;
    if (c == -1) return;
  }
  for (x = 0; isdigit(c); c = getchar()) x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig) x = -x;
}
}  // namespace fastIO2
using namespace fastIO1;
vector<int> G[(1010)];
int n, m, V, vis[(1010)], Tim, S, T, E = 1, con[(1010)], ans;
int fi[(1010)], ne[((1010) * (1010) + 10)], b[((1010) * (1010) + 10)],
    c[((1010) * (1010) + 10)], d[(1010)], vh[(1010)];
void add(int x, int y, int z) {
  ne[++E] = fi[x], fi[x] = E, b[E] = y, c[E] = z;
  ne[++E] = fi[y], fi[y] = E, b[E] = x, c[E] = 0;
}
void dist() {
  memset(d, 0, sizeof(d));
  queue<int> Q;
  Q.push(T), d[T] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = fi[u]; i; i = ne[i]) {
      int v = b[i];
      if (d[v] || v == T) continue;
      d[v] = d[u] + 1, Q.push(v);
    }
  }
}
int dfs(int u, int Flow) {
  if (u == T || Flow == 0) return Flow;
  int res = Flow, mindist = V - 1;
  for (int i = fi[u]; i; i = ne[i])
    if (c[i]) {
      int v = b[i], nega = i ^ 1;
      if (d[u] == d[v] + 1) {
        int x = dfs(v, min(res, c[i]));
        res -= x, c[i] -= x, c[nega] += x;
        if (d[S] == V) return Flow - res;
        if (!res) break;
      }
      mindist = min(d[v], mindist);
    }
  if (Flow == res) {
    vh[d[u]]--;
    if (!vh[d[u]])
      d[S] = V;
    else
      d[u] = mindist + 1, vh[d[u]]++;
  }
  return Flow - res;
}
int ISAP() {
  dist();
  int ans = 0;
  for (int i = 1; i <= V; i++) vh[d[i]]++;
  while (d[S] < V) ans += dfs(S, (0x7f7f7f7f));
  return ans;
}
bool dfs(int u) {
  if (vis[u] == Tim) return 0;
  vis[u] = Tim;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (vis[v] == Tim) continue;
    if (!con[v] || dfs(con[v])) {
      con[v] = u;
      return 1;
    }
  }
  return 0;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    int cnt;
    read(cnt);
    while (cnt--) {
      int x;
      read(x);
      G[i].push_back(x + n);
    }
  }
  for (int i = 1; i <= n; i++) Tim++, dfs(i);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < G[i].size(); j++) add(i, con[G[i][j]], (0x7f7f7f7f));
  S = n + 1, T = n + 2, V = n + 2;
  for (int i = 1; i <= n; i++) {
    int W;
    read(W), W = -W;
    if (W > 0)
      add(S, i, W), ans += W;
    else
      add(i, T, -W);
  }
  printf("%d\n", ISAP() - ans);
}