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>
const int oo = 0x3f3f3f3f;
using std::vector;
const int MAXN = 305;
namespace Dinic {
const int MAXV = 1e3 + 5, MAXE = 2e6 + 5;
struct Edge {
  int u, v;
  int rsd;
  int next;
  Edge() {}
  Edge(int u_, int v_, int c_, int next_)
      : u(u_), v(v_), rsd(c_), next(next_) {}
};
int n, m, s, t;
int head[MAXV];
Edge edge[MAXE];
int dis[MAXV];
int cur[MAXV];
inline void init(int size) {
  n = size + 2;
  s = size;
  t = size + 1;
  m = 0;
  memset(head, -1, n * sizeof(*head));
}
inline void addEdge(int u, int v, int c) {
  edge[m] = Edge(u, v, c, head[u]);
  head[u] = m++;
  edge[m] = Edge(v, u, 0, head[v]);
  head[v] = m++;
}
bool bfs() {
  static int q[MAXV];
  int front = 0, rear = 0;
  memset(dis, -1, n * sizeof(*dis));
  dis[s] = 0;
  q[rear++] = s;
  while (front != rear) {
    int u = q[front++];
    for (int i = head[u]; i >= 0; i = edge[i].next) {
      const Edge &e = edge[i];
      if (dis[e.v] < 0 && e.rsd > 0) {
        dis[e.v] = dis[e.u] + 1;
        q[rear++] = e.v;
      }
    }
  }
  return dis[t] >= 0;
}
int dfs(int u, int rsd) {
  if (u == t || rsd == 0) return rsd;
  int res = 0;
  for (int &i = cur[u]; i >= 0; i = edge[i].next) {
    Edge &e = edge[i];
    if (dis[u] + 1 == dis[e.v] && e.rsd > 0) {
      int f = dfs(e.v, std::min(e.rsd, rsd));
      if (f > 0) {
        e.rsd -= f;
        edge[i ^ 1].rsd += f;
        res += f;
        rsd -= f;
        if (rsd == 0) return res;
      }
    }
  }
  return res;
}
int exec() {
  int flow = 0;
  while (bfs()) {
    memcpy(cur, head, n * sizeof(*cur));
    flow += dfs(s, +oo);
  }
  return flow;
}
}  // namespace Dinic
int n;
vector<int> set[MAXN];
int weight[MAXN];
void input() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int m;
    scanf("%d", &m);
    for (int j = 0; j < m; ++j) {
      int first;
      scanf("%d", &first);
      set[i].push_back(first - 1);
    }
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &weight[i]);
  }
}
void solve() {
  for (int i = 0; i < n; ++i) {
    weight[i] = -weight[i];
  }
  using namespace Dinic;
  init(2 * ::n);
  for (int i = 0; i < ::n; ++i) {
    addEdge(s, i, 1);
    addEdge(::n + i, t, 1);
    for (size_t j = 0; j < set[i].size(); ++j) {
      addEdge(i, ::n + set[i][j], 1);
    }
  }
  assert(exec() == ::n);
  static int match[MAXN];
  for (int i = 0; i < ::n; ++i) {
    for (int j = head[i]; j; j = edge[j].next) {
      if (edge[j].rsd == 0) {
        match[edge[j].v - ::n] = i;
        break;
      }
    }
  }
  int ans = 0;
  init(::n);
  for (int i = 0; i < ::n; ++i) {
    if (weight[i] >= 0) {
      ans += weight[i];
      addEdge(s, i, weight[i]);
    } else {
      addEdge(i, t, -weight[i]);
    }
    for (int j = 0; j < set[i].size(); ++j) {
      addEdge(i, match[set[i][j]], +oo);
    }
  }
  ans -= exec();
  ans = -ans;
  printf("%d\n", ans);
}
int main() {
  input();
  solve();
}