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 = 303;
struct Set {
  int size;
  int A[MAXN];
  bool included[MAXN];
  void read() {
    scanf("%d", &size);
    for (int i = 0; i < size; i++) {
      scanf("%d", A + i);
      included[A[i]] = true;
    }
  }
  int& operator[](const int& i) { return A[i]; }
  bool contain(int x) { return included[x]; }
};
int n;
Set s[MAXN];
int represent[MAXN];
bool vis[MAXN];
int choice[MAXN];
bool dfs(int u) {
  for (int i = 0; i < s[u].size; i++) {
    int v = s[u][i];
    if (vis[v]) continue;
    vis[v] = true;
    if (!choice[v] || dfs(choice[v])) {
      choice[v] = u;
      represent[u] = v;
      return true;
    }
  }
  return false;
}
void match() {
  for (int i = 1; i <= n; i++) {
    fill(vis, vis + 1 + n, false);
    assert(dfs(i));
  }
}
const int MAXV = MAXN + 2;
const int MAXE = MAXN * MAXN + MAXN;
struct Edge {
  Edge* next;
  int dest, cap;
};
int S, T;
Edge memory[MAXE * 2];
Edge* info[MAXV];
Edge* curMemory = memory;
void insert(int u, int v, int c) {
  curMemory->next = info[u];
  curMemory->dest = v;
  curMemory->cap = c;
  info[u] = curMemory++;
  curMemory->next = info[v];
  curMemory->dest = u;
  curMemory->cap = 0;
  info[v] = curMemory++;
}
Edge* oppositeEdge(const Edge* const x) { return memory + ((x - memory) ^ 1); }
int lev[MAXV];
bool bfs() {
  queue<int> que;
  fill(lev, lev + n + 2, -1);
  que.push(S);
  lev[S] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (Edge* pt = info[u]; pt; pt = pt->next) {
      int v = pt->dest;
      if (lev[v] == -1 && pt->cap) {
        que.push(v);
        lev[v] = lev[u] + 1;
      }
    }
  }
  return lev[T] != -1;
}
Edge* pre[MAXV];
int dfs(int u, int flow) {
  if (u == T) return flow;
  int rest = flow;
  for (Edge*& pt = pre[u]; pt; pt = pt->next) {
    int v = pt->dest;
    if (pt->cap && lev[u] + 1 == lev[v]) {
      int temp = dfs(v, min(pt->cap, rest));
      if (temp) {
        rest -= temp;
        pt->cap -= temp;
        oppositeEdge(pt)->cap += temp;
        if (!rest) return flow;
      }
    }
  }
  return flow - rest;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) s[i].read();
  match();
  S = 0, T = n + 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x > 0)
      insert(i, T, x);
    else {
      insert(S, i, -x);
      ans += x;
    }
  }
  for (int i = 1; i <= n; i++) {
    int x = represent[i];
    for (int j = 1; j <= n; j++)
      if (i != j && s[j].contain(x)) insert(j, i, 0x3f3f3f3f);
  }
  while (bfs()) {
    copy(info, info + n + 2, pre);
    ans += dfs(S, 0x3f3f3f3f);
  }
  printf("%d\n", ans);
  return 0;
}