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 long long MOD = 1e9 + 7;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 1e5 + 5;
int m[MAXN];
vector<int> num[MAXN];
const int MX = 666;
int match[MX];
bool vis[MX];
struct Edge {
  int v, nxt;
} E[MAXN];
int Head[MAXN], tot;
void add(int x, int y) {
  E[tot++] = {y, Head[x]};
  Head[x] = tot - 1;
}
bool DFS(int u) {
  for (int i = Head[u]; ~i; i = E[i].nxt) {
    int v = E[i].v;
    if (!vis[v]) {
      vis[v] = 1;
      if (match[v] == -1 || DFS(match[v])) {
        match[v] = u;
        match[u] = v;
        return 1;
      }
    }
  }
  return 0;
}
int BM(int n) {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int u = 1; u <= n; u++) {
    memset(vis, 0, sizeof(vis));
    if (DFS(u)) res++;
  }
  return res;
}
const int inf = 0x3f3f3f3f;
const int MXE = 4 * MX * MX;
struct MaxFlow {
  struct Edge {
    int v, w, nxt;
  } edge[MXE];
  int tot, num, s, t;
  int head[MX];
  void init() {
    memset(head, -1, sizeof(head));
    tot = 0;
  }
  void add(int u, int v, int w) {
    edge[tot].v = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
    edge[tot].v = u;
    edge[tot].w = 0;
    edge[tot].nxt = head[v];
    head[v] = tot++;
  }
  int d[MX], vis[MX], gap[MX];
  void bfs() {
    memset(d, 0, sizeof(d));
    memset(gap, 0, sizeof(gap));
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(t);
    vis[t] = 1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = head[u]; ~i; i = edge[i].nxt) {
        int v = edge[i].v;
        if (!vis[v]) {
          d[v] = d[u] + 1;
          gap[d[v]]++;
          q.push(v);
          vis[v] = 1;
        }
      }
    }
  }
  int last[MX];
  int dfs(int u, int f) {
    if (u == t) return f;
    int sap = 0;
    for (int i = last[u]; ~i; i = edge[i].nxt) {
      int v = edge[i].v;
      if (edge[i].w > 0 && d[u] == d[v] + 1) {
        last[u] = i;
        int tmp = dfs(v, min(f - sap, edge[i].w));
        edge[i].w -= tmp;
        edge[i ^ 1].w += tmp;
        sap += tmp;
        if (sap == f) return sap;
      }
    }
    if (d[s] >= num) return sap;
    if (!(--gap[d[u]])) d[s] = num;
    ++gap[++d[u]];
    last[u] = head[u];
    return sap;
  }
  int solve(int st, int ed, int n) {
    int flow = 0;
    num = n;
    s = st;
    t = ed;
    bfs();
    memcpy(last, head, sizeof(head));
    while (d[s] < num) flow += dfs(s, inf);
    return flow;
  }
} F;
int v[MX];
int main() {
  int n;
  cin >> n;
  memset(Head, -1, sizeof(Head));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m[i]);
    for (int j = 1; j <= m[i]; j++) {
      int x;
      scanf("%d", &x);
      num[i].push_back(x);
      add(i, x + n);
      add(x + n, i);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]), v[i] = -v[i];
  BM(n);
  F.init();
  int tot = 0;
  int st = 0, en = n + n + 10;
  for (int i = 1; i <= n; i++) {
    for (int j : num[i]) {
      F.add(match[i] - n, j, INF);
    }
    if (v[i] > 0)
      F.add(st, match[i] - n, v[i]), tot += v[i];
    else
      F.add(match[i] - n, en, -v[i]);
  }
  int ret = F.solve(st, en, en + 5);
  printf("%d\n", -(tot - ret));
  return 0;
}