#include <bits/stdc++.h>
const int N = 305, M = N * N * 2, oo = 100000000;
int n, m, x, S, T, l = 1, ans, p[N], f[N], d[N], son[N], ed[M], data[M],
                   next[M];
bool b[N], a[N][N];
bool find(int x) {
  for (int i = 1; i <= n; i++)
    if (!b[i] && a[x][i]) {
      b[i] = 1;
      if (!p[i] || find(p[i])) {
        p[i] = x;
        return 1;
      }
    }
  return 0;
}
bool build() {
  memset(d, -1, sizeof(d));
  int h = 0, t = 1;
  f[1] = S;
  d[S] = 0;
  while (h < t) {
    int i = f[++h];
    for (int p = son[i]; p; p = next[p])
      if (data[p]) {
        int j = ed[p];
        if (d[j] >= 0) continue;
        d[j] = d[i] + 1;
        f[++t] = j;
        if (j == T) return 1;
      }
  }
  return 0;
}
inline int min(int a, int b) { return a < b ? a : b; }
int dinic(int i, int low) {
  if (i == T) return low;
  int w = 0, res;
  for (int p = son[i]; p && w < low; p = next[p]) {
    int j = ed[p];
    if (data[p] && d[j] == d[i] + 1 && (res = dinic(j, min(data[p], low - w))))
      data[p] -= res, data[p ^ 1] += res, w += res;
  }
  if (!w) d[i] = -1;
  return w;
}
void add(int x, int y, int z) {
  ed[++l] = y, data[l] = z, next[l] = son[x], son[x] = l;
  ed[++l] = x, data[l] = 0, next[l] = son[y], son[y] = l;
}
int main() {
  scanf("%d", &n), T = n + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m);
    while (m--) scanf("%d", &x), a[i][x] = 1;
  }
  for (int i = 1; i <= n; i++) memset(b, 0, sizeof(b)), find(i);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x < 0)
      ans += x, add(S, i, -x);
    else
      add(i, T, x);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j] && i - p[j]) add(i, p[j], oo);
  while (build()) ans += dinic(S, oo);
  printf("%d\n", ans);
  return 0;
}
