#include <bits/stdc++.h>
using namespace std;
class Edge {
 public:
  int from, to, flow;
  Edge(int _from, int _to, int _flow) : from(_from), to(_to), flow(_flow) {}
};
class MaxFlow {
 public:
  vector<Edge> edges;
  vector<vector<int>> adia;
  int n, s, d;
  MaxFlow(int _n, int _s, int _d) {
    s = _s, d = _d;
    n = _n;
    adia = vector<vector<int>>(n);
  }
  void addEdge(Edge x) {
    adia[x.from].push_back(edges.size());
    edges.push_back(x);
    adia[x.to].push_back(edges.size());
    edges.push_back({x.to, x.from, 0});
  }
  vector<int> father;
  bool bfs() {
    father = vector<int>(n, -1);
    father[s] = -2;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int x(q.front());
      q.pop();
      for (auto i : adia[x]) {
        if (edges[i].flow && father[edges[i].to] == -1) {
          father[edges[i].to] = i;
          q.push(edges[i].to);
        }
      }
    }
    return father[d] != -1;
  }
  int Flow() {
    int ans(0);
    while (bfs()) {
      int fmax(1e9);
      for (int i(father[d]); i != -2; i = father[edges[i].from])
        fmax = min(fmax, edges[i].flow);
      for (int i(father[d]); i != -2; i = father[edges[i].from])
        edges[i].flow -= fmax, edges[i ^ 1].flow += fmax;
      ans += fmax;
    }
    return ans;
  }
};
class Cuplaj {
 public:
  vector<vector<int>> adiast;
  vector<int> cuplatst, cuplatdr;
  vector<bool> tried;
  int n;
  Cuplaj(int _n, int _m) {
    n = _n;
    adiast = vector<vector<int>>(n + 1);
    cuplatst = vector<int>(n + 1);
    cuplatdr = vector<int>(_m + 1);
    tried = vector<bool>(n + 1);
  }
  void addEdge(int x, int y) { adiast[x].push_back(y); }
  vector<int> mk_cuplaj() {
    bool ok(1);
    while (ok) {
      ok = 0;
      tried = vector<bool>(n + 1);
      for (int i(1); i <= n; i++)
        if (!tried[i] && !cuplatst[i] && cuplaj(i)) ok = 1;
    }
    return cuplatst;
  }
  bool cuplaj(int nod) {
    tried[nod] = 1;
    for (auto i : adiast[nod]) {
      if (!cuplatdr[i]) {
        cuplatdr[i] = nod;
        cuplatst[nod] = i;
        return true;
      }
    }
    for (auto i : adiast[nod]) {
      if (!tried[cuplatdr[i]] && cuplaj(cuplatdr[i])) {
        cuplatdr[i] = nod;
        cuplatst[nod] = i;
        return true;
      }
    }
    return false;
  }
};
vector<int> sets[310];
int price[310];
int main() {
  int n, m;
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  Cuplaj cuplaj = Cuplaj(n + 1, n + 1);
  for (int i(1); i <= n; i++) {
    cin >> m;
    sets[i].resize(m);
    for (auto& j : sets[i]) {
      cin >> j;
      cuplaj.addEdge(i, j);
    }
  }
  for (int i(1); i <= n; i++) cin >> price[i];
  vector<int> rep = cuplaj.mk_cuplaj(), where(n + 1);
  for (int i(1); i <= n; i++) where[rep[i]] = i;
  MaxFlow maxflow(n + 2, 0, n + 1);
  int ans(0);
  for (int i(1); i <= n; i++) {
    if (price[i] >= 0) {
      maxflow.addEdge({0, i, 0});
      maxflow.addEdge({i, n + 1, price[i]});
    } else {
      ans += price[i];
      maxflow.addEdge({0, i, -price[i]});
      maxflow.addEdge({i, n + 1, 0});
    }
  }
  for (int i(1); i <= n; i++) {
    for (auto j : sets[i])
      if (j != rep[i]) maxflow.addEdge({i, where[j], (int)1e9});
  }
  cout << ans + maxflow.Flow();
  return 0;
}
