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 MAX = 305, MAX2 = 50000, MAX3 = 610;
const long long oo = 1e14;
int head[MAX3], to[MAX3 * MAX3], nxt[MAX3 * MAX3], ent;
int mat[MAX3], vist[MAX3], i, n, m;
inline void add(int v, int u) {
  to[ent] = u, nxt[ent] = head[v], head[v] = ent, ent++;
  to[ent] = v, nxt[ent] = head[u], head[u] = ent, ent++;
}
bool findTree(int i) {
  vist[i] = 1;
  for (int j = head[i]; ~j; j = nxt[j]) {
    int u = to[j];
    if (!vist[u]) {
      vist[u] = 1;
      if (!mat[u] || findTree(mat[u])) {
        mat[u] = i, mat[i] = u;
        return true;
      }
    }
  }
  return false;
}
void hung() {
  for (int i = 1; i <= n; i++) mat[i] = 0;
  for (i = 1; i <= n; i++) {
    if (!mat[i]) {
      for (int i = 1; i <= n; i++) vist[i] = 0;
      findTree(i);
    }
  }
}
struct edge {
  int to;
  int nxt;
  long long C;
  edge() {}
  edge(int a, int b, long long c) : to(a), nxt(b), C(c) {}
};
int headr[MAX], entr;
edge edgesr[MAX2];
int headg[MAX];
edge edges[MAX2];
int addr[MAX];
int level[MAX];
int s, t;
void add2(int a, int b, long long c) {
  edgesr[entr].nxt = headr[a], edgesr[entr].to = b, edgesr[entr].C = c,
  headr[a] = entr++;
  edgesr[entr].nxt = headr[b], edgesr[entr].to = a, edgesr[entr].C = 0,
  headr[b] = entr++;
}
bool bfs() {
  for (int i = 1; i <= n; i++) level[i] = -1;
  queue<int> q;
  level[s] = 0, q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = headr[v]; ~i; i = edgesr[i].nxt) {
      int u = edgesr[i].to;
      if (edgesr[i].C && level[u] == -1) {
        level[u] = level[v] + 1;
        q.push(u);
      }
    }
  }
  return level[t] != -1;
}
long long dfs(int v, long long cf) {
  if (v == t) {
    return cf;
  }
  for (; ~addr[v]; addr[v] = edgesr[addr[v]].nxt) {
    int i = addr[v], u = edgesr[i].to;
    if (edgesr[i].C && level[u] == level[v] + 1) {
      long long deta = dfs(u, std::min(cf, edgesr[i].C));
      if (deta) {
        edgesr[i ^ 1].C += deta;
        edgesr[i].C -= deta;
        return deta;
      }
    }
  }
  return 0;
}
long long dinic() {
  memset(headr, -1, sizeof(headr));
  entr = 0;
  for (int v = 1; v <= n; v++) {
    for (int i = headg[v]; ~i; i = edges[i].nxt)
      add2(v, edges[i].to, edges[i].C);
  }
  long long f = 0;
  while (bfs()) {
    for (int i = 1; i <= n; i++) addr[i] = headr[i];
    long long tmp;
    while ((tmp = dfs(s, LLONG_MAX))) {
      f += tmp;
    }
  }
  return f;
}
void add(int a, int b, long long c) {
  edges[ent].nxt = headg[a], edges[ent].to = b, edges[ent].C = c,
  headg[a] = ent, ent++;
}
void printG() {
  for (int i = 1; i <= n; i++) {
    printf("v = %d -> ", i);
    for (int j = headg[i]; ~j; j = edges[j].nxt) {
      printf("(%d, %lld) ", edges[j].to, edges[j].C);
    }
    printf("\n");
  }
}
long long w[MAX];
int main() {
  memset(head, -1, sizeof(head)), ent = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int m;
    scanf("%d", &m);
    for (int j = 0; j < m; j++) {
      int ve;
      scanf("%d", &ve);
      add(i, ve + n);
    }
  }
  int tmpn = n;
  n = n * 2;
  hung();
  n = tmpn;
  memset(headg, -1, sizeof(headg));
  ent = 0;
  for (int i = 1; i <= n; i++) {
    for (int a = head[i]; ~a; a = nxt[a]) {
      int u = to[a];
      if (u != mat[i]) {
        int j = mat[u];
        add(i + 1, j + 1, oo);
      }
    }
  }
  long long tt = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &w[i]);
    w[i] = -w[i];
    if (w[i] > 0)
      add(1, i + 1, w[i]);
    else
      add(i + 1, n + 2, -w[i]);
    if (w[i] > 0) tt += w[i];
  }
  n = n + 2;
  s = 1, t = n;
  printf("%lld\n", -(tt - dinic()));
  return 0;
}