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;
template <class TFlow>
struct TMaxFlowCalcer {
  struct TEdge {
    int From;
    int To;
    TFlow Capacity;
    TFlow Flow;
    int Back;
    TEdge(int from, int to, TFlow capacity, TFlow flow, int back)
        : From(from), To(to), Capacity(capacity), Flow(flow), Back(back) {}
  };
  vector<vector<TEdge> > Edges;

 private:
  vector<int> Used;
  int Source;
  int Sink;
  static const TFlow MAX_FLOW_VALUE;
  TFlow MinFlowValue;
  TFlow Dfs(int v, TFlow maxFlowGain) {
    Used[v] = 1;
    if (v == Sink) {
      return maxFlowGain;
    }
    for (int i = 0; i < Edges[v].size(); ++i) {
      TEdge& e = Edges[v][i];
      if (!Used[e.To] && e.Capacity - e.Flow >= MinFlowValue) {
        TFlow flow = Dfs(e.To, min(e.Capacity - e.Flow, maxFlowGain));
        if (flow > 0) {
          e.Flow += flow;
          Edges[e.To][e.Back].Flow -= flow;
          return flow;
        }
      }
    }
    return 0;
  }

 public:
  TFlow TotalFlow;
  void Initialize(int size) { Edges.assign(size, vector<TEdge>()); }
  void AddEdge(int from, int to, int capacity) {
    TEdge forward(from, to, capacity, 0, Edges[to].size());
    TEdge back(to, from, 0, 0, Edges[from].size());
    Edges[from].push_back(forward);
    Edges[to].push_back(back);
  }
  void CalcMaxFlow(int source, int sink) {
    TotalFlow = 0;
    Source = source;
    Sink = sink;
    const int N = Edges.size();
    for (MinFlowValue = MAX_FLOW_VALUE; MinFlowValue > 0; MinFlowValue /= 2) {
      TFlow flowGain;
      Used.assign(N, 0);
      while ((flowGain = Dfs(source, MAX_FLOW_VALUE)) > 0) {
        Used.assign(N, 0);
        TotalFlow += flowGain;
      }
    }
  }
};
template <class TFlow>
const TFlow TMaxFlowCalcer<TFlow>::MAX_FLOW_VALUE =
    numeric_limits<TFlow>::max();
int main() {
  int n;
  cin >> n;
  vector<vector<int> > a(n, vector<int>());
  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    int size;
    cin >> size;
    a[i].resize(size);
    for (int j = 0; j < size; ++j) {
      cin >> a[i][j];
      --a[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }
  vector<int> groupByNumber(n);
  {
    TMaxFlowCalcer<int> biCalcer;
    biCalcer.Initialize(2 * n + 2);
    int source = 2 * n;
    int sink = 2 * n + 1;
    for (int i = 0; i < n; ++i) {
      biCalcer.AddEdge(source, i, 1);
      biCalcer.AddEdge(n + i, sink, 1);
      for (int j = 0; j < a[i].size(); ++j) {
        biCalcer.AddEdge(i, n + a[i][j], 1);
      }
    }
    biCalcer.CalcMaxFlow(source, sink);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < biCalcer.Edges[i].size(); ++j) {
        TMaxFlowCalcer<int>::TEdge& e = biCalcer.Edges[i][j];
        if (e.Flow > 0) {
          groupByNumber[e.To - n] = i;
        }
      }
    }
  }
  {
    TMaxFlowCalcer<int> calcer;
    calcer.Initialize(n + 2);
    int source = n;
    int sink = n + 1;
    int bestPossible = 0;
    for (int i = 0; i < n; ++i) {
      if (w[i] > 0) {
        calcer.AddEdge(i, sink, w[i]);
      } else {
        calcer.AddEdge(source, i, -w[i]);
        bestPossible += -w[i];
      }
      for (int j = 0; j < a[i].size(); ++j) {
        if (groupByNumber[a[i][j]] != i) {
          calcer.AddEdge(i, groupByNumber[a[i][j]], 1000 * 1000 * 1000);
        }
      }
    }
    calcer.CalcMaxFlow(source, sink);
    cout << calcer.TotalFlow - bestPossible << endl;
  }
  return 0;
}