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 TAT>
inline void read(TAT &a) {
  static char cc;
  static bool f;
  while ((cc = getchar()) != '-' && (cc < '0' || cc > '9'))
    ;
  if (cc == '-')
    f = 1, a = 0;
  else
    f = 0, a = cc - '0';
  while ((cc = getchar()) >= '0' && cc <= '9') a = a * 10 + cc - '0';
  if (f) a = -a;
}
template <class TAT>
inline void write(TAT a) {
  static char cc[27];
  static int ct;
  if (a == 0) {
    putchar('0');
    return;
  }
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  ct = 0;
  while (a) cc[++ct] = a % 10 + '0', a /= 10;
  while (ct) putchar(cc[ct--]);
}
template <class TAT>
inline void Ckmin(TAT &a, const TAT &b) {
  if (a > b) a = b;
}
template <class TAT>
inline void Ckmax(TAT &a, const TAT &b) {
  if (a < b) a = b;
}
void begin() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void end() {
  fclose(stdin);
  fclose(stdout);
}
const int maxn = 305;
int n;
int Val[maxn];
int Set[maxn][maxn], sz[maxn] = {0};
int Link[maxn] = {0};
bool ever[maxn];
int denote[maxn], dist[maxn], cur[maxn];
struct edge {
  int to, next, c;
  edge() {}
  edge(int _t, int _n, int _c) : to(_t), next(_n), c(_c) {}
} e[maxn * maxn * 5];
int head[maxn], et = -1;
int S = 0, T;
void Addedge(int fr, int to, int c) {
  e[++et] = edge(to, head[fr], c), head[fr] = et;
  e[++et] = edge(fr, head[to], 0), head[to] = et;
}
void Init() {
  read(n);
  memset(head, -1, sizeof(head));
  et = -1;
  for (int i = 1; i <= n; ++i) {
    read(sz[i]);
    for (int j = 1; j <= sz[i]; ++j) {
      read(Set[i][j]);
    }
  }
  for (int i = 1; i <= n; ++i) {
    read(Val[i]);
    Val[i] *= -1;
  }
  S = 0;
  T = n + 2;
}
bool Dfs(int p) {
  int to;
  for (int i = 1; i <= sz[p]; ++i) {
    to = Set[p][i];
    if (ever[to] == 0) {
      ever[to] = 1;
      if (Link[to] == 0 || Dfs(Link[to]) == 1) {
        Link[to] = p;
        return 1;
      }
    }
  }
  return 0;
}
void Prepare() {
  for (int i = 1; i <= n; ++i) {
    memset(ever, 0, sizeof(ever));
    Dfs(i);
  }
  for (int i = 1; i <= n; ++i) {
    if (Link[i] != 0) {
      denote[i] = Link[i];
    } else {
      denote[i] = n + 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (Val[i] > 0) {
      Addedge(S, i, Val[i]);
    } else if (Val[i] < 0) {
      Addedge(i, T, -Val[i]);
    }
  }
  Addedge(n + 1, T, 1e9);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= sz[i]; ++j) {
      if (denote[Set[i][j]] == i) continue;
      Addedge(i, denote[Set[i][j]], 1e9);
    }
  }
}
void Bfs() {
  static int line[maxn];
  int h = 0, t = 0, u;
  memset(dist, 0x3f, sizeof(dist));
  dist[T] = 0;
  line[h++] = T;
  while (h != t) {
    u = line[t++];
    for (int i = head[u]; i != -1; i = e[i].next) {
      if (e[i ^ 1].c > 0 && dist[e[i].to] > dist[u] + 1) {
        dist[e[i].to] = dist[u] + 1;
        line[h++] = e[i].to;
      }
    }
  }
}
int Dinic(int p, int res) {
  int flow = 0, ff;
  if (p == T || res == 0) {
    return res;
  }
  for (int &i = cur[p]; i != -1; i = e[i].next) {
    if (dist[p] - 1 == dist[e[i].to] && e[i].c > 0) {
      ff = Dinic(e[i].to, min(res, e[i].c));
      res -= ff;
      flow += ff;
      e[i].c -= ff;
      e[i ^ 1].c += ff;
      if (res == 0) return flow;
    }
  }
  return flow;
}
int Maxflow() {
  int FLOW = 0;
  while (1) {
    Bfs();
    if (dist[S] > 1e9) break;
    memcpy(cur, head, sizeof(head));
    FLOW += Dinic(S, 1e9);
  }
  return FLOW;
}
void Solve() {
  int Sum = 0;
  for (int i = 1; i <= n; ++i) {
    if (Val[i] > 0) {
      Sum += Val[i];
    }
  }
  write(-(Sum - Maxflow()));
  putchar('\n');
}
int main() {
  Init();
  Prepare();
  Solve();
  return 0;
}