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>
inline int rd() {
  int x = 0, p = 1;
  char a = getchar();
  while ((a < 48 || a > 57) && a != '-') a = getchar();
  if (a == '-') p = -p, a = getchar();
  while (a > 47 && a < 58) x = (x << 1) + (x << 3) + (a & 15), a = getchar();
  return x * p;
}
template <typename T>
inline T min(T x, T y) {
  return x < y ? x : y;
}
const int N = 200002;
struct Edge {
  int to, next;
  long long flow;
} edge[N << 1];
int head[N], cnt = -1;
int n, s, t;
long long ans;
inline void add(int f, int t, long long w) {
  edge[++cnt].next = head[f];
  edge[cnt].to = t;
  edge[cnt].flow = w;
  head[f] = cnt;
}
inline void ins(int u, int v, long long f) { add(u, v, f), add(v, u, 0); }
int h[N], gap[N];
long long e[N];
int vis[N];
struct cmp {
  bool operator()(const int &x, const int &y) const { return h[x] < h[y]; }
};
std::priority_queue<int, std::vector<int>, cmp> q;
inline void bfs() {
  std::queue<int> q;
  for (int i = 1; i <= t; i++) h[i] = (2100000000);
  h[t] = 0, q.push(t);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (edge[i ^ 1].flow && h[v] > h[u] + 1) h[v] = h[u] + 1, q.push(v);
    }
  }
}
inline void push(int u) {
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (edge[i].flow && h[u] == h[v] + 1) {
      long long f = min(e[u], edge[i].flow);
      edge[i].flow -= f, edge[i ^ 1].flow += f;
      e[u] -= f, e[v] += f;
      if (v != s && v != t && !vis[v]) q.push(v), vis[v] = 1;
    }
    if (!e[u]) break;
  }
}
inline void relable(int u) {
  h[u] = (2100000000);
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (edge[i].flow && h[u] > h[v] + 1) h[u] = h[v] + 1;
  }
}
inline long long HLPP() {
  bfs();
  if (h[s] == (2100000000)) return 0;
  h[s] = t;
  for (int i = 1; i <= t; i++)
    if (h[i] != (2100000000)) gap[h[i]]++;
  for (int i = head[s]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    long long f = 0;
    if ((f = edge[i].flow)) {
      edge[i].flow -= f, edge[i ^ 1].flow += f;
      e[s] -= f, e[v] += f;
      if (v != s && v != t && !vis[v]) q.push(v), vis[v] = 1;
    }
  }
  while (!q.empty()) {
    int u = q.top();
    q.pop();
    vis[u] = 0, push(u);
    if (e[u]) {
      gap[h[u]]--;
      if (!gap[h[u]])
        for (int v = 1; v <= t; v++)
          if (v != s && v != t && h[v] < t + 1 && h[u] < h[v]) h[v] = t + 1;
      relable(u), gap[h[u]]++;
      q.push(u), vis[u] = 1;
    }
  }
  return e[t];
}
int main() {
  memset(head, -1, sizeof head);
  n = rd();
  s = n + n + 1, t = s + 1;
  for (int i = 1; i <= n; i++) {
    int x = rd(), v;
    while (x--) {
      v = rd();
      ins(i, v + n, (210000000000ll));
    }
    ins(i + n, t, (500000000));
  }
  for (int i = 1; i <= n; i++) {
    int x = rd();
    ins(s, i, -x + (500000000));
    ans += (-x + (500000000));
  }
  printf("%lld\n", min(-(ans - HLPP()), 0ll));
  return 0;
}