#include <bits/stdc++.h>
using namespace std;
struct edge {
  int b, l, c;
} g[91005 << 1];
int gap[305], cur[305], h[305], t, dis[305], n, pre[305];
int graph[305][305], link[305], vis[305];
bool hungry(int u) {
  for (int i = 1; i <= n; i++)
    if (vis[i] != t && graph[u][i]) {
      vis[i] = t;
      if (!link[i] || hungry(link[i])) {
        link[i] = u;
        return 1;
      }
    }
  return 0;
}
int arc(int i) { return i + (i & 1 ? 1 : -1); }
void addedge(int a, int b, int c) {
  g[++t] = (edge){b, h[a], c};
  h[a] = t;
  g[++t] = (edge){a, h[b], 0};
  h[b] = t;
}
void bfs(int u) {
  int i;
  queue<int> q;
  for (i = 0; i <= n; i++) dis[i] = n, gap[i] = 0;
  gap[dis[u] = 0] = 1;
  q.push(u);
  while (q.size()) {
    u = q.front();
    q.pop();
    for (i = h[u]; i; i = g[i].l)
      if (dis[g[i].b] >= n) {
        gap[dis[g[i].b] = dis[u] + 1]++;
        q.push(g[i].b);
      }
  }
}
int sap(int x, int y) {
  int i, d, u = x, flow = 0;
  bfs(y);
  for (i = 1; i <= n; i++) cur[i] = h[i];
  while (dis[x] < n) {
    for (i = cur[u]; i; i = g[i].l)
      if (dis[u] == dis[g[i].b] + 1 && g[i].c) break;
    if (i) {
      cur[u] = i;
      pre[u = g[i].b] = i;
      if (u == y) {
        for (i = pre[y], d = 1e9; i; i = pre[g[arc(i)].b]) d = min(d, g[i].c);
        for (flow += d, u = x, i = pre[y]; i; i = pre[g[arc(i)].b]) {
          g[i].c -= d;
          g[arc(i)].c += d;
        }
      }
    } else {
      for (i = cur[u] = h[u], d = n; i; i = g[i].l)
        if (g[i].c) d = min(d, dis[g[i].b] + 1);
      if (!--gap[dis[u]]) break;
      gap[dis[u] = d]++;
      if (u != x) u = g[arc(pre[u])].b;
    }
  }
  return flow;
}
int main() {
  int i, j, s;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &j);
    while (j--) {
      scanf("%d", &s);
      graph[i][s] = 1;
    }
  }
  for (i = t = 1; i <= n; i++, t++) hungry(i);
  for (i = 1, t = 0; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (graph[i][j]) addedge(i, link[j], 1e9);
  for (i = 1, s = 0; i <= n; i++) {
    scanf("%d", &j);
    if (!j) continue;
    if (j > 0)
      addedge(i, n + 2, j);
    else {
      s -= j;
      addedge(n + 1, i, -j);
    }
  }
  n += 2;
  printf("%d", sap(n - 1, n) - s);
  return 0;
}
