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;
const int inf = ~0u >> 3;
const int N = 1005, M = 400000;
namespace FL {
struct edge {
  int t, f;
  edge *n, *d;
} eb[M * 2], *ec, *e[N];
inline void add_edge(int s, int t, int f) {
  *ec = (edge){t, f, e[s], ec + 1};
  e[s] = ec++;
  *ec = (edge){s, 0, e[t], ec - 1};
  e[t] = ec++;
}
int d[N], n, s, t, q[N];
bool modlabel() {
  int x, y, l(0), r(0);
  fill(d, d + 1 + n, -1);
  d[q[++r] = s] = 0;
  while (l != r)
    for (edge *i = e[x = q[++l]]; i; i = i->n)
      if (i->f && d[y = i->t] == -1) d[q[++r] = y] = d[x] + 1;
  return d[t] >= 0;
}
int aug(int x, int f) {
  if (x == t) return f;
  int r(f), y;
  for (edge *i = e[x]; i; i = i->n)
    if (d[x] + 1 == d[y = i->t] && i->f)
      if (int de = aug(y, min(r, i->f)))
        if (!(i->f -= de, i->d->f += de, r -= de)) break;
  return f - r;
}
void reset(int _n, int _s, int _t) {
  n = _n, s = _s, t = _t;
  memset(e, 0, sizeof(e[0]) * (n + 2));
  ec = eb;
}
int solve() {
  int r(0), d;
  while (modlabel())
    while (d = aug(s, inf)) r += d;
  return r;
}
}  // namespace FL
namespace MT {
int n, s, t;
void solve(int res[]) {
  if (FL::solve() != n) throw;
  for (int u = 1; u <= n; ++u) {
    res[u] = 0;
    for (FL::edge *i = FL::e[u]; !res[u] && i; i = i->n)
      if (!i->f && i->t != s) res[u] = i->t - n;
    if (res[u] == 0) throw;
  }
}
void init(int _n) {
  n = _n;
  s = n * 2 + 1;
  t = s + 1;
  FL::reset(t, s, t);
  for (int i = 1; i <= n; ++i) FL::add_edge(s, i, 1);
  for (int i = 1; i <= n; ++i) FL::add_edge(n + i, t, 1);
}
void link(int u, int v) { FL::add_edge(u, v + n, 1); }
}  // namespace MT
namespace SP {
int n, s, t;
void init(int _n) {
  n = _n;
  s = n + 1;
  t = s + 1;
  FL::reset(t, s, t);
}
inline void link(int u, int v) { FL::add_edge(u, v, inf); }
namespace DET {
using namespace FL;
int vis[N];
void dfs(int x) {
  vis[x] = 1;
  for (edge *i = e[x]; i; i = i->n)
    if (i->f && !vis[i->t]) dfs(i->t);
}
}  // namespace DET
int solve(int w[]) {
  for (int i = 1; i <= n; ++i)
    if (w[i] < 0)
      FL::add_edge(s, i, -w[i]);
    else
      FL::add_edge(i, t, w[i]);
  FL::solve();
  int res(0);
  DET::dfs(DET::s);
  for (int i = 1; i <= n; ++i)
    if (DET::vis[i]) res += w[i];
  return res;
}
}  // namespace SP
namespace PSR {
int n, weight[N], match[N], setid[N];
vector<int> ct[N];
void solve() {
  scanf("%d", &n);
  MT::init(n);
  for (int i = 1; i <= n; ++i) {
    int z, a;
    scanf("%d", &z);
    while (z--) {
      scanf("%d", &a);
      ct[i].push_back(a);
      MT::link(i, a);
    }
  }
  MT::solve(match);
  for (int i = 1; i <= n; ++i) setid[match[i]] = i;
  SP::init(n);
  for (int i = 1; i <= n; ++i)
    for (int u : ct[i])
      if (u != match[i]) SP::link(i, setid[u]);
  for (int i = 1; i <= n; ++i) scanf("%d", weight + i);
  int nw = SP::solve(weight);
  printf("%d\n", nw);
}
}  // namespace PSR
int main() {
  PSR::solve();
  return 0;
}