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 kInf = 0x3f3f3f3f;
const int kMaxN = 300;
int n, w[kMaxN], mat[kMaxN];
vector<int> num[kMaxN];
bool vis[kMaxN];
namespace flow {
const int kMaxV = kMaxN + 2, kMaxE = (kMaxN + kMaxN * kMaxN) * 2;
int cnt, begin[kMaxV], end[kMaxE], next[kMaxE], cap[kMaxE];
int s, t, label[kMaxV], start[kMaxV], q[kMaxV], qh, qt, stack[kMaxV],
    edge[kMaxV], stop;
int maxflow;
void Init(int s_, int t_) {
  cnt = 0;
  memset(begin, -1, sizeof(int) * (n + 2));
  s = s_, t = t_;
}
void AddEdge(int u, int v, int c) {
  next[cnt] = begin[u];
  begin[u] = cnt;
  end[cnt] = v;
  cap[cnt++] = c;
}
void AddPair(int u, int v, int c) {
  AddEdge(u, v, c);
  AddEdge(v, u, 0);
}
bool Relabel() {
  qh = qt = 0;
  memset(label, -1, sizeof(label));
  for (label[s] = 0, start[s] = begin[s], q[qt++] = s; qh < qt;) {
    int u = q[qh++];
    for (int now = begin[u], v; now != -1; now = next[now])
      if (cap[now] && label[v = end[now]] < 0) {
        label[v] = label[u] + 1;
        start[v] = begin[v];
        q[qt++] = v;
      }
  }
  return (label[t] != -1);
}
void DoAug() {
  stop = 0;
  stack[stop++] = s;
  while (stop) {
    int u = stack[stop - 1];
    if (u != t) {
      int &now = start[u], v;
      for (; now != -1; now = next[now])
        if (cap[now] && label[v = end[now]] == label[u] + 1) break;
      if (now >= 0) {
        stack[stop] = v;
        edge[stop++] = now;
      } else {
        --stop;
        label[u] = -1;
      }
    } else {
      int flow = kInf;
      for (int i = stop - 1; i >= 1; --i) flow = min(flow, cap[edge[i]]);
      for (int i = stop - 1; i >= 1; --i) {
        cap[edge[i]] -= flow, cap[((edge[i]) ^ 1)] += flow;
        if (!cap[edge[i]]) stop = i;
      }
      maxflow += flow;
    }
  }
}
int Maxflow() {
  maxflow = 0;
  while (Relabel()) DoAug();
  return maxflow;
}
}  // namespace flow
bool Dfs(int u) {
  for (int i = 0, v; i < int(num[u].size()); ++i)
    if (!vis[v = num[u][i]]) {
      vis[v] = true;
      if (mat[v] == -1 || Dfs(mat[v])) {
        mat[v] = u;
        return true;
      }
    }
  return false;
}
int main() {
  scanf("%d", &n);
  for (int i = 0, x; i < n; ++i) {
    scanf("%d", &x);
    for (int y; x--;) {
      scanf("%d", &y);
      num[i].push_back(y - 1);
    }
  }
  memset(mat, -1, sizeof(mat));
  for (int i = 0; i < n; ++i) {
    memset(vis, false, sizeof(vis));
    Dfs(i);
  }
  for (int i = 0; i < n; ++i) scanf("%d", &w[i]);
  flow::Init(n, n + 1);
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (w[i] < 0) {
      ans += -w[i];
      flow::AddPair(n, i, -w[i]);
    } else if (w[i] > 0)
      flow::AddPair(i, n + 1, w[i]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < int(num[i].size()); ++j)
      if (mat[num[i][j]] != i) flow::AddPair(i, mat[num[i][j]], kInf);
  ans -= flow::Maxflow();
  printf("%d\n", -ans);
  return 0;
}