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;
struct MaxMatch {
  struct Edge {
    int ed, next;
  } edge[100010];
  int head[512], matchx[512], matchy[512], nEdge;
  bool vis[512];
  void init() {
    memset(head, -1, sizeof(head));
    nEdge = 0;
  }
  void addEdge(int a, int b) {
    edge[nEdge].ed = b;
    edge[nEdge].next = head[a];
    head[a] = nEdge++;
  }
  bool find(int x) {
    int i, y;
    for (i = head[x]; ~i; i = edge[i].next) {
      if (!vis[y = edge[i].ed]) {
        vis[y] = true;
        if (matchy[y] < 0 || find(matchy[y])) {
          matchx[x] = y;
          matchy[y] = x;
          return true;
        }
      }
    }
    return false;
  }
  int maxMatch(int n) {
    int i, ret = 0;
    memset(matchy, -1, sizeof(matchy));
    for (i = 0; i < n; ++i) {
      memset(vis, 0, sizeof(vis));
      if (find(i)) ++ret;
    }
    return ret;
  }
} match;
struct MaxFlow {
  struct Edge {
    int ed, flow, next;
  } edge[100010];
  int head[512], nEdge;
  int d[512], pre[512], path[512], his[512], cur[512], vh[512];
  void init() {
    memset(head, -1, sizeof(head));
    nEdge = 0;
  }
  void addEdge(int a, int b, int flow) {
    edge[nEdge].ed = b;
    edge[nEdge].flow = flow;
    edge[nEdge].next = head[a];
    head[a] = nEdge++;
    edge[nEdge].ed = a;
    edge[nEdge].flow = 0;
    edge[nEdge].next = head[b];
    head[b] = nEdge++;
  }
  int sap(int v, int src, int dst) {
    int x, y, i, m, nowFlow = 0x3F3F3F3F, totalFlow = 0;
    bool flag;
    memset(d, 0, sizeof(d));
    memset(vh, 0, sizeof(vh));
    memcpy(cur, head, sizeof(cur));
    for (vh[0] = v, x = src; d[src] < v;) {
      his[x] = nowFlow;
      flag = false;
      for (i = cur[x]; ~i; i = edge[i].next) {
        if (edge[i].flow && d[x] == d[y = edge[i].ed] + 1) {
          flag = true;
          nowFlow = min(nowFlow, edge[i].flow);
          pre[y] = x;
          path[y] = cur[x] = i;
          if ((x = y) == dst) {
            for (totalFlow += nowFlow; x != src; x = pre[x]) {
              edge[path[x]].flow -= nowFlow;
              edge[path[x] ^ 1].flow += nowFlow;
            }
            nowFlow = 0x3F3F3F3F;
          }
          break;
        }
      }
      if (!flag) {
        for (m = v - 1, i = cur[x] = head[x]; ~i; i = edge[i].next) {
          if (edge[i].flow && d[y = edge[i].ed] < m) {
            m = d[y];
            cur[x] = i;
          }
        }
        if (!(--vh[d[x]])) break;
        ++vh[d[x] = m + 1];
        if (x != src) nowFlow = his[x = pre[x]];
      }
    }
    return totalFlow;
  }
} flow;
int val[512];
int main() {
  int n, m, a, i, j, v, src, dst, sum;
  while (~scanf("%d", &n)) {
    match.init();
    flow.init();
    for (i = 0; i < n; ++i) {
      scanf("%d", &m);
      while (m--) {
        scanf("%d", &a);
        match.addEdge(i, a - 1);
      }
    }
    for (sum = i = 0; i < n; ++i) {
      scanf("%d", &val[i]);
      if (val[i] < 0) sum += val[i];
    }
    match.maxMatch(n);
    v = n + 2;
    src = n;
    dst = n + 1;
    for (i = 0; i < n; ++i) {
      if (val[i] < 0)
        flow.addEdge(src, i, -val[i]);
      else
        flow.addEdge(i, dst, val[i]);
      for (j = match.head[i]; ~j; j = match.edge[j].next) {
        if (match.edge[j].ed != match.matchx[i])
          flow.addEdge(i, match.matchy[match.edge[j].ed], 0x3F3F3F3F);
      }
    }
    printf("%d\n", sum + flow.sap(v, src, dst));
  }
  return 0;
}