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;
inline long long Readll() {
  long long ret = 0;
  int f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (ch >= 0 && (ch < '0' || ch > '9'));
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret * f;
}
void open() {
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
}
void close() {
  fclose(stdin);
  fclose(stdout);
}
const int MAXN = 333;
const int MAXM = 101010;
const int inf = 1e9;
int match[MAXN];
int ans;
int n;
struct bi_graph {
  int tot;
  int to[MAXM];
  int nxt[MAXM];
  int hd[MAXN];
  bool vis[MAXN];
  inline void add(const int &x, const int &y) {
    tot++;
    to[tot] = y;
    nxt[tot] = hd[x];
    hd[x] = tot;
  }
  bool find(int p) {
    for (int i = hd[p]; i; i = nxt[i])
      if (vis[to[i]] == false) {
        vis[to[i]] = true;
        if (match[to[i]] == 0 || find(match[to[i]])) {
          match[to[i]] = p;
          return true;
        }
      }
    return false;
  }
  void work() {
    for (int i = (1), _END_ = (n); i <= _END_; i++) {
      memset(vis, 0, sizeof(vis));
      find(i);
    }
  }
} B;
struct flow_graph {
  int src, snk;
  int tot;
  int to[MAXM];
  int nxt[MAXM];
  int fl[MAXM];
  int hd[MAXN];
  int que[MAXN + 1];
  bool in[MAXN];
  int dis[MAXN];
  inline void clear() {
    tot = -1;
    memset(hd, -1, sizeof(hd));
  }
  inline void add(const int &x, const int &y, const int &FL) {
    tot++;
    to[tot] = y;
    fl[tot] = FL;
    nxt[tot] = hd[x];
    hd[x] = tot;
    tot++;
    to[tot] = x;
    fl[tot] = 0;
    nxt[tot] = hd[y];
    hd[y] = tot;
  }
  inline int Nxt(int &T) {
    int ret = T;
    if (T == MAXN)
      T = 0;
    else
      T++;
    return ret;
  }
  bool SPFA() {
    int L = 0, R = 0;
    for (int i = (src), _END_ = (snk); i <= _END_; i++) dis[i] = inf;
    que[Nxt(R)] = src;
    dis[src] = 0;
    int now;
    while (L != R) {
      now = que[Nxt(L)];
      in[now] = false;
      for (int i = hd[now]; i != -1; i = nxt[i])
        if (fl[i]) {
          if (dis[to[i]] > dis[now] + 1) {
            dis[to[i]] = dis[now] + 1;
            if (!in[to[i]]) {
              in[to[i]] = true;
              que[Nxt(R)] = to[i];
            }
          }
        }
    }
    return dis[snk] < inf;
  }
  int dfs(int p, int flow) {
    if (p == snk) return flow;
    int ret = 0, tmp;
    for (int i = hd[p]; i != -1; i = nxt[i])
      if (fl[i] && dis[p] + 1 == dis[to[i]]) {
        tmp = dfs(to[i], min(flow, fl[i]));
        fl[i] -= tmp;
        fl[i ^ 1] += tmp;
        flow -= tmp;
        ret += tmp;
        if (flow == 0) break;
      }
    return ret;
  }
  int maxflow() {
    int ret = 0;
    while (SPFA()) ret += dfs(src, inf);
    return ret;
  }
} F;
vector<int> son[MAXN];
void init() {
  scanf("%d", &n);
  int sz;
  int x;
  for (int i = (1), _END_ = (n); i <= _END_; i++) {
    scanf("%d", &sz);
    while (sz) {
      sz--;
      scanf("%d", &x);
      son[i].push_back(x);
      B.add(i, x);
    }
  }
}
void build() {
  int x;
  F.clear();
  F.src = 0;
  F.snk = n + 1;
  for (int i = (1), _END_ = (n); i <= _END_; i++) {
    scanf("%d", &x);
    x = -x;
    if (x > 0) {
      ans += x;
      F.add(F.src, i, x);
    } else
      F.add(i, F.snk, -x);
    for (int j = (0), _END_ = (son[i].size()); j < _END_; j++)
      if (match[son[i][j]] != i) {
        F.add(i, match[son[i][j]], inf);
      }
  }
}
int main() {
  int _ = 0;
  init();
  B.work();
  build();
  printf("%d\n", F.maxflow() - ans);
  close();
  return 0;
}