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 MAXN = 300;
int S, T;
int v[MAXN + 10];
const int INF = 0x3f3f3f3f;
int n;
struct et {
  int t;
  et* n;
  et() {}
  et(int _t, et* _n) : t(_t), n(_n) {}
} * e[MAXN + 10], store[MAXN * MAXN + 10], *loc = store;
struct et2 {
  int t, u;
  et2 *n, *p;
  et2() {}
  et2(int _t, int _u, et2* _n) : t(_t), u(_u), n(_n) {}
} * e2[MAXN + 10], store2[MAXN * MAXN + 10 + MAXN * 2], *loc2 = store2;
void add(int x, int y, int z) {
  e2[x] = new (loc2++) et2(y, z, e2[x]);
  e2[y] = new (loc2++) et2(x, 0, e2[y]);
  e2[x]->p = e2[y];
  e2[y]->p = e2[x];
}
int d[MAXN + 10];
bool build() {
  static int q[MAXN + 10];
  int head = 0, tail = 0;
  q[0] = S;
  memset(d, 0, sizeof(d));
  d[S] = 1;
  while (head <= tail) {
    int now = q[head++];
    for (et2* i = e2[now]; i; i = i->n) {
      if (!d[i->t] && i->u) {
        d[i->t] = d[now] + 1;
        q[++tail] = i->t;
        if (i->t == T) return true;
      }
    }
  }
  return false;
}
int flow(int x, int left) {
  if (x == T || !left) return left;
  int res = 0;
  for (et2*& i = e2[x]; i; i = i->n)
    if (d[i->t] == d[x] + 1) {
      int got = flow(i->t, min(left, i->u));
      i->u -= got;
      i->p->u += got;
      res += got;
      left -= got;
      if (!left) return res;
    }
  return res;
}
int maxFlow() {
  static et2* eb[MAXN + 10];
  memcpy(eb, e2, sizeof(eb));
  int res = 0;
  while (build()) {
    res += flow(S, INF);
    memcpy(e2, eb, sizeof(eb));
  }
  return res;
}
int goes[MAXN + 10];
bool been[MAXN + 10];
bool find(int x) {
  for (et* i = e[x]; i; i = i->n)
    if (!been[i->t]) {
      been[i->t] = true;
      if (!goes[i->t] || find(goes[i->t])) {
        goes[i->t] = x;
        return true;
      }
    }
  return false;
}
void perfectMatch() {
  for (int i = 1; i <= n; i++) {
    memset(been, false, sizeof(been));
    assert(find(i));
  }
}
int main() {
  scanf("%d", &n);
  S = 1, T = ((n) + 1) + 1;
  for (int i = 1; i <= n; i++) {
    int cnt;
    scanf("%d", &cnt);
    for (int j = 1; j <= cnt; j++) {
      int x;
      scanf("%d", &x);
      e[i] = new (loc++) et(x, e[i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }
  perfectMatch();
  for (int i = 1; i <= n; i++) {
    for (et* j = e[i]; j; j = j->n)
      if (goes[j->t] != i) {
        add(((i) + 1), ((goes[j->t]) + 1), INF);
      }
  }
  int posSum = 0;
  for (int i = 1; i <= n; i++) {
    v[i] = -v[i];
    if (v[i] > 0) {
      add(S, ((i) + 1), v[i]);
      posSum += v[i];
    } else {
      add(((i) + 1), T, -v[i]);
    }
  }
  cout << -(posSum - maxFlow()) << endl;
  return 0;
}