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 = 305;
const int INF = (int)1e9;
vector<int> g[maxN];
int n;
bool used[maxN];
int mt[maxN];
int par[maxN];
int we[maxN];
bool kuhn(int v) {
  if (used[v]) return false;
  used[v] = true;
  for (int to : g[v]) {
    if (mt[to] == 0 || kuhn(mt[to])) {
      mt[to] = v;
      return true;
    }
  }
  return false;
}
void init() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 1; j <= k; j++) {
      int x;
      cin >> x;
      g[i].push_back(x);
    }
  }
  for (int i = 1; i <= n; i++) cin >> we[i];
  for (int i = 1; i <= n; i++) {
    memset(used, 0, sizeof used);
    kuhn(i);
  }
  for (int i = 1; i <= n; i++) {
    par[mt[i]] = i;
  }
}
struct edge {
  int to, id, cap, flow;
  edge(int _to, int _id, int _cap, int _flow) {
    to = _to;
    id = _id;
    cap = _cap;
    flow = _flow;
  }
};
vector<edge> edges[2 * maxN];
void addEdge(int from, int to, int cap) {
  edges[from].emplace_back(to, edges[to].size(), cap, 0);
  edges[to].emplace_back(from, edges[from].size() - 1, cap, cap);
}
int s, t;
int sum = 0;
void build() {
  s = 0;
  t = 2 * n + 1;
  sum = 0;
  for (int i = 1; i <= n; i++) {
    if (we[i] > 0) {
      addEdge(i, t, we[i]);
    } else {
      sum += we[i];
      addEdge(s, i, -we[i]);
    }
    for (int to : g[i]) {
      addEdge(i, to + n, INF);
    }
    addEdge(par[i] + n, i, INF);
  }
}
int d[2 * maxN];
int minV = 1 << 22;
void bfs() {
  memset(d, -1, sizeof d);
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto& ed : edges[v]) {
      if (d[ed.to] == -1 && ed.flow + minV <= ed.cap) {
        d[ed.to] = d[v] + 1;
        q.push(ed.to);
      }
    }
  }
}
int ptr[2 * maxN];
int dfs(int from, int lim) {
  if (lim == 0) return 0;
  if (from == t) return lim;
  for (; ptr[from] < edges[from].size(); ptr[from]++) {
    auto& ed = edges[from][ptr[from]];
    int to = ed.to;
    if (d[to] != d[from] + 1) continue;
    int add = dfs(to, min(lim, ed.cap - ed.flow));
    if (add != 0) {
      ed.flow += add;
      edges[to][ed.id].flow -= add;
      return add;
    }
  }
  return 0;
}
int goFlow() {
  int flow = 0;
  while (true) {
    bfs();
    if (d[t] == -1) {
      return flow;
    }
    while (true) {
      memset(ptr, 0, sizeof ptr);
      int add = dfs(s, 2 * INF);
      if (add == 0) {
        break;
      }
      flow += add;
    }
  }
}
int findFlow() {
  int total = 0;
  while (minV > 0) {
    total += goFlow();
    minV /= 2;
  }
  return total;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  build();
  cout << sum + findFlow();
  return 0;
}