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;
class Edge {
 public:
  int from, to, flow;
  Edge(int _from, int _to, int _flow) : from(_from), to(_to), flow(_flow) {}
};
class MaxFlow {
 public:
  vector<Edge> edges;
  vector<vector<int>> adia;
  int n, s, d;
  MaxFlow(int _n, int _s, int _d) {
    s = _s, d = _d;
    n = _n;
    adia = vector<vector<int>>(n);
  }
  void addEdge(Edge x) {
    adia[x.from].push_back(edges.size());
    edges.push_back(x);
    adia[x.to].push_back(edges.size());
    edges.push_back({x.to, x.from, 0});
  }
  vector<int> father;
  bool bfs() {
    father = vector<int>(n, -1);
    father[s] = -2;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int x(q.front());
      q.pop();
      for (auto i : adia[x]) {
        if (edges[i].flow && father[edges[i].to] == -1) {
          father[edges[i].to] = i;
          q.push(edges[i].to);
        }
      }
    }
    return father[d] != -1;
  }
  int Flow() {
    int ans(0);
    while (bfs()) {
      int fmax(1e9);
      for (int i(father[d]); i != -2; i = father[edges[i].from])
        fmax = min(fmax, edges[i].flow);
      for (int i(father[d]); i != -2; i = father[edges[i].from])
        edges[i].flow -= fmax, edges[i ^ 1].flow += fmax;
      ans += fmax;
    }
    return ans;
  }
};
class Cuplaj {
 public:
  vector<vector<int>> adiast;
  vector<int> cuplatst, cuplatdr;
  vector<bool> tried;
  int n;
  Cuplaj(int _n, int _m) {
    n = _n;
    adiast = vector<vector<int>>(n + 1);
    cuplatst = vector<int>(n + 1);
    cuplatdr = vector<int>(_m + 1);
    tried = vector<bool>(n + 1);
  }
  void addEdge(int x, int y) { adiast[x].push_back(y); }
  vector<int> mk_cuplaj() {
    bool ok(1);
    while (ok) {
      ok = 0;
      tried = vector<bool>(n + 1);
      for (int i(1); i <= n; i++)
        if (!tried[i] && !cuplatst[i] && cuplaj(i)) ok = 1;
    }
    return cuplatst;
  }
  bool cuplaj(int nod) {
    tried[nod] = 1;
    for (auto i : adiast[nod]) {
      if (!cuplatdr[i]) {
        cuplatdr[i] = nod;
        cuplatst[nod] = i;
        return true;
      }
    }
    for (auto i : adiast[nod]) {
      if (!tried[cuplatdr[i]] && cuplaj(cuplatdr[i])) {
        cuplatdr[i] = nod;
        cuplatst[nod] = i;
        return true;
      }
    }
    return false;
  }
};
vector<int> sets[310];
int price[310];
int main() {
  int n, m;
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  Cuplaj cuplaj = Cuplaj(n + 1, n + 1);
  for (int i(1); i <= n; i++) {
    cin >> m;
    sets[i].resize(m);
    for (auto& j : sets[i]) {
      cin >> j;
      cuplaj.addEdge(i, j);
    }
  }
  for (int i(1); i <= n; i++) cin >> price[i];
  vector<int> rep = cuplaj.mk_cuplaj(), where(n + 1);
  for (int i(1); i <= n; i++) where[rep[i]] = i;
  MaxFlow maxflow(n + 2, 0, n + 1);
  int ans(0);
  for (int i(1); i <= n; i++) {
    if (price[i] >= 0) {
      maxflow.addEdge({0, i, 0});
      maxflow.addEdge({i, n + 1, price[i]});
    } else {
      ans += price[i];
      maxflow.addEdge({0, i, -price[i]});
      maxflow.addEdge({i, n + 1, 0});
    }
  }
  for (int i(1); i <= n; i++) {
    for (auto j : sets[i])
      if (j != rep[i]) maxflow.addEdge({i, where[j], (int)1e9});
  }
  cout << ans + maxflow.Flow();
  return 0;
}