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 = 305;
const int maxm = 2 * maxn + maxn * maxn;
const int sups = 303, supt = 304;
const int inf = 0x6ffffff;
int n;
int price[maxn];
int st[maxn][maxn], nst[maxn];
int mch[2 * maxn];
bool vis[2 * maxn];
int first[maxn], elen;
struct edge {
  int nxt, f, t, c;
} e[maxm];
int dis[maxn], gap[maxn];
bool subMatch(int s) {
  vis[s] = true;
  for (int j = 0; j < nst[s]; j++) {
    int t = st[s][j] + n;
    if (vis[t] || mch[t] == s) continue;
    if (mch[t] == 0 || (!vis[mch[t]] && subMatch(mch[t]))) {
      mch[t] = s;
      mch[s] = t;
      return true;
    }
  }
  return false;
}
void Match() {
  for (int i = 1; i <= n; i++) {
    if (mch[i] == 0) {
      memset(vis, false, sizeof(vis));
      subMatch(i);
    }
  }
}
void addedge(int f, int t, int c) {
  e[elen].nxt = first[f];
  e[elen].f = f;
  e[elen].t = t;
  e[elen].c = c;
  first[f] = elen++;
}
void build() {
  for (int i = 1; i <= n; i++) {
    if (price[i] >= 0) {
      addedge(sups, i, price[i]);
      addedge(i, sups, 0);
    } else {
      addedge(i, supt, -price[i]);
      addedge(supt, i, 0);
    }
    for (int j = 0; j < nst[i]; j++) {
      int t = mch[st[i][j] + n];
      addedge(i, t, inf);
      addedge(t, i, 0);
    }
  }
}
int dfs(int s, int flow) {
  if (s == supt) return flow;
  int mindis = n;
  int tflow = flow, sub;
  for (int p = first[s]; p != -1; p = e[p].nxt) {
    int t = e[p].t;
    if (e[p].c > 0) {
      if (dis[t] + 1 == dis[s]) {
        sub = dfs(t, min(tflow, e[p].c));
        e[p].c -= sub;
        e[p ^ 1].c += sub;
        tflow -= sub;
        if (dis[sups] > n) return flow - tflow;
        if (tflow <= 0) break;
      }
      mindis = min(mindis, dis[t]);
    }
  }
  if (flow == tflow) {
    --gap[dis[s]];
    if (gap[dis[s]] == 0)
      dis[sups] = n + 1;
    else {
      dis[s] = mindis + 1;
      ++gap[dis[s]];
    }
  }
  return flow - tflow;
}
int maxflow() {
  int flow = 0;
  gap[0] = n + 2;
  while (dis[sups] <= n) {
    flow += dfs(sups, inf);
  }
  return flow;
}
int main() {
  int ans = 0;
  memset(first, -1, sizeof(first));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", nst + i);
    for (int j = 0; j < nst[i]; j++) {
      scanf("%d", st[i] + j);
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", price + i);
    price[i] *= -1;
    if (price[i] >= 0) ans += price[i];
  }
  Match();
  build();
  ans = maxflow() - ans;
  printf("%d\n", ans);
  return 0;
}