#include <bits/stdc++.h>
using namespace std;
const int inf(1010101010);
struct flow {
  int to[2 * 10010], c[2 * 10010];
  int n, ne;
  vector<vector<int> > adj;
  flow(int n) : n(n), ne(0), adj(n) {}
  void ae(int a, int b, int cap, int r = 1) {
    to[ne] = b, c[ne] = cap, adj[a].push_back(ne++);
    if (r) ae(b, a, 0, 0);
  }
  int q[10010], qle, qd, pre[10010];
  void bfs(int s) {
    q[0] = s, qd = 0, qle = 1;
    while (qle - qd) {
      int x(q[qd++]);
      for (__typeof((adj[x]).end()) it((adj[x]).begin());
           it != ((adj[x]).end()); ++it) {
        int e(*it), y(to[e]);
        if (c[e])
          if (pre[y] == -1) pre[y] = e, q[qle++] = y;
      }
    }
  }
  int max_flow(int s, int t) {
    for (int tot(0);;) {
      fill(pre, pre + n, -1);
      bfs(s);
      if (pre[t] == -1) return tot;
      int w(inf);
      for (int x(t), e(pre[x]); x - s; x = to[e ^ 1], e = pre[x])
        w = min(w, c[e]);
      tot += w;
      for (int x(t), e(pre[x]); x - s; x = to[e ^ 1], e = pre[x])
        c[e] -= w, c[e ^ 1] += w;
    }
  }
};
int n;
flow matching(605);
int which_set[305];
flow graph2(305);
int main() {
  cin >> n;
  {
    int s(0), t(1 + n + n);
    for (int i(0); i < (n); i++) {
      int ne;
      cin >> ne;
      while (ne--) {
        int x;
        cin >> x;
        x--;
        matching.ae(1 + i, 1 + n + x, 1);
      }
    }
    for (int i(0); i < (n); i++) matching.ae(s, 1 + i, 1);
    for (int i(0); i < (n); i++) matching.ae(1 + n + i, t, 1);
    matching.max_flow(s, t);
    for (int i(0); i < (n); i++)
      for (__typeof((matching.adj[1 + i]).end()) it(
               (matching.adj[1 + i]).begin());
           it != ((matching.adj[1 + i]).end()); ++it) {
        int e(*it), y(matching.to[e]);
        if (y == 0) continue;
        int item(y - (1 + n));
        if (matching.c[e] == 0) which_set[item] = i;
      }
  }
  {
    int s(0), t(1 + n);
    for (int i(0); i < (n); i++)
      for (__typeof((matching.adj[1 + i]).end()) it(
               (matching.adj[1 + i]).begin());
           it != ((matching.adj[1 + i]).end()); ++it) {
        int e(*it), y(matching.to[e]);
        if (y == 0) continue;
        int item(y - (1 + n));
        if (matching.c[e]) graph2.ae(1 + i, 1 + which_set[item], inf);
      }
    int tot(0);
    for (int i(0); i < (n); i++) {
      int v;
      cin >> v;
      if (v > 0)
        graph2.ae(1 + i, t, v);
      else
        graph2.ae(s, 1 + i, -v), tot += -v;
    }
    printf("%d\n", graph2.max_flow(s, t) - tot);
  }
}
