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 INF = 1e9 + 5;
const int Maxn = 305;
const int S = 0;
const int T = 301;
int n, m;
vector<int> ss[Maxn];
int value[Maxn];
int match[Maxn << 1];
bool used[Maxn << 1];
vector<int> adj1[Maxn << 1];
vector<int> adj2[Maxn];
int dist[Maxn];
int capa[Maxn][Maxn];
int ans = 0;
bool dfs1(int now) {
  used[now] = true;
  for (auto v : adj1[now]) {
    int w = match[v];
    if (w == 0 || !used[w] && dfs1(w)) {
      match[now] = v;
      match[v] = now;
      return true;
    }
  }
  return false;
}
void perfect() {
  memset(match, 0, sizeof match);
  for (int i = 1; i <= n; i++) {
    if (match[i] == 0) {
      memset(used, 0, sizeof used);
      dfs1(i);
    }
  }
}
bool bfs() {
  memset(dist, -1, sizeof dist);
  queue<int> q;
  int x;
  q.push(S);
  dist[S] = 0;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (auto v : adj2[x]) {
      if (dist[v] == -1 && capa[x][v] > 0) {
        q.push(v);
        dist[v] = dist[x] + 1;
      }
    }
  }
  return dist[T] > 0;
}
int dfs(int now, int f) {
  if (f == 0 || now == T) return f;
  int w = 0, used = 0;
  for (auto v : adj2[now]) {
    if (dist[v] == dist[now] + 1 && capa[now][v] > 0) {
      w = dfs(v, min(f - used, capa[now][v]));
      if (w != 0) {
        capa[now][v] -= w;
        capa[v][now] += w;
        used += w;
      }
    }
  }
  if (used == 0) dist[now] = -1;
  return used;
}
void build() {
  memset(capa, 0, sizeof capa);
  for (int i = 1; i <= n; i++) {
    if (value[i] < 0) {
      adj2[S].push_back(i);
      adj2[i].push_back(S);
      capa[S][i] += -value[i];
    } else {
      adj2[i].push_back(T);
      adj2[T].push_back(i);
      capa[i][T] += value[i];
    }
    for (auto v : adj1[i]) {
      adj2[i].push_back(match[v]);
      adj2[match[v]].push_back(i);
      capa[i][match[v]] += INF;
    }
  }
}
void calc() {
  int flow = 0;
  while (bfs()) {
    flow += dfs(S, INF);
  }
  ans -= flow;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m);
    ss[i].resize(m);
    for (int j = 0; j < m; j++) {
      scanf("%d", ss[i].begin() + j);
      adj1[i].push_back(ss[i][j] + Maxn);
      adj1[ss[i][j] + Maxn].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", value + i);
  for (int i = 1; i <= n; i++) {
    if (value[i] < 0) ans -= value[i];
  }
  perfect();
  build();
  calc();
  printf("%d\n", -ans);
  return 0;
}