#include <bits/stdc++.h>
using namespace std;
vector<int> mp[100010];
int n, m, v[10010], lnk[100010];
inline bool Find(int x, int t) {
  for (int i = 0; i < mp[x].size(); i++)
    if (v[mp[x][i]] != t) {
      int to = mp[x][i];
      v[to] = t;
      if (!lnk[to] || Find(lnk[to], t)) {
        lnk[to] = x;
        return (1);
      }
    }
  return (0);
}
inline void Match() {
  for (int i = 1; i <= n; i++) Find(i, i);
}
struct Edge {
  int to, old, c;
} e[10010];
int S, T, tot, l[10010], pre[10010], q[10010], ans;
inline int Dfs(int x, int f) {
  int flow = f;
  if (x == T || !f) return (f);
  for (int i = l[x]; i && f; i = e[i].old)
    if (pre[e[i].to] == pre[x] + 1 && e[i].c) {
      int a = Dfs(e[i].to, min(f, e[i].c));
      e[i].c -= a, e[i ^ 1].c += a, f -= a;
    }
  return (flow - f);
}
inline bool Bfs() {
  int h = 1, t = 1;
  q[1] = S;
  memset(pre, -1, sizeof(pre));
  pre[S] = 0;
  do {
    int nw = q[h];
    for (int i = l[nw]; i; i = e[i].old)
      if (pre[e[i].to] == -1 && e[i].c)
        pre[e[i].to] = pre[nw] + 1, q[++t] = e[i].to;
    h++;
  } while (h <= t);
  return (pre[T] != -1);
}
inline void Add(int x, int y, int z) {
  e[++tot].to = y;
  e[tot].old = l[x], l[x] = tot, e[tot].c = z;
  e[++tot].to = x, e[tot].old = l[y], l[y] = tot, e[tot].c = 0;
}
int main() {
  scanf("%d%", &n);
  for (int i = 1; i <= n; i++) {
    int K;
    scanf("%d", &K);
    for (int j = 1; j <= K; j++) {
      int x;
      scanf("%d", &x);
      mp[i].push_back(x + n);
      mp[x + n].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  Match();
  S = 301, T = 302, tot = 1;
  for (int i = 1; i <= n; i++)
    if (v[i] < 0)
      Add(i, T, -v[i]), ans -= v[i];
    else
      Add(S, i, v[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < mp[i].size(); j++)
      if (lnk[mp[i][j]] != i) Add(lnk[mp[i][j]], i, 1000000000);
  }
  while (Bfs()) ans -= Dfs(S, 1000000000);
  printf("%d\n", -ans);
}
