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;
int n, m;
int k;
bool debug = false;
vector<int> v[305];
int w[305], cnt, vis[305], p[305];
class DinicMaxFlow {
 private:
  int cnt;
  int Head[305 * 305 * 2];
  int Next[305 * 305 * 2];
  int W[305 * 305 * 2];
  int V[305 * 305 * 2];
  int Depth[305 * 305 * 2];

 public:
  int s, t;
  void init() {
    cnt = -1;
    memset(Head, -1, sizeof(Head));
    memset(Next, -1, sizeof(Next));
  }
  void _Add(int u, int v, int w) {
    cnt++;
    Next[cnt] = Head[u];
    Head[u] = cnt;
    V[cnt] = v;
    W[cnt] = w;
  }
  void Add_Edge(int u, int v, int w) {
    _Add(u, v, w);
    _Add(v, u, 0);
  }
  int dfs(int u, int flow) {
    if (u == t || flow == 0) return flow;
    for (int i = Head[u]; i != -1; i = Next[i]) {
      if ((Depth[V[i]] == Depth[u] + 1) && (W[i] != 0)) {
        int di = dfs(V[i], min(flow, W[i]));
        if (di > 0) {
          W[i] -= di;
          W[i ^ 1] += di;
          return di;
        }
      }
    }
    return 0;
  }
  int bfs() {
    queue<int> Q;
    memset(Depth, 0, sizeof(Depth));
    Depth[s] = 1;
    Q.push(s);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (int i = Head[u]; i != -1; i = Next[i])
        if ((Depth[V[i]] == 0) && (W[i] > 0)) {
          Depth[V[i]] = Depth[u] + 1;
          Q.push(V[i]);
        }
    }
    return Depth[t] > 0;
  }
  int Dinic() {
    int Ans = 0;
    while (bfs()) {
      while (int d = dfs(s, 1 << 30)) {
        Ans += d;
      }
    }
    return Ans;
  }
};
bool match(int x) {
  for (auto j : v[x]) {
    if (vis[j] == cnt) continue;
    vis[j] = cnt;
    if (p[j] == 0 || match(p[j])) {
      p[j] = x;
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1, y, x; i <= n; i++) {
    scanf("%d", &y);
    for (int j = 0; j < y; j++) {
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", w + i);
  for (int i = 1; i <= n; i++) {
    cnt++;
    match(i);
  }
  DinicMaxFlow model;
  model.init();
  model.s = 0;
  model.t = n + 1;
  for (int i = 1; i <= n; i++) {
    for (auto j : v[i]) {
      model.Add_Edge(i, p[j], 1 << 30);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (w[i] < 0) {
      ans += -w[i];
      model.Add_Edge(model.s, i, -w[i]);
    } else {
      model.Add_Edge(i, model.t, w[i]);
    }
  }
  ans -= model.Dinic();
  printf("%d\n", -ans);
  return 0;
}