#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector<int> credit;
vector<vector<int>> graph;
vector<bool> visited;
pair<long long, long long> dfs(int u, int parent) {
    visited[u] = true;
    long long notTaken = 0, taken = credit[u];
    for (int v : graph[u]) {
        if (v == parent) continue;
        if (!visited[v]) {
            auto child = dfs(v, u);
            notTaken += max(child.first, child.second);
            taken += child.first;
        }
    }
    return {notTaken, taken};
}
int main(){
    cin >> n;
    credit.resize(n + 1);
    graph.resize(n + 1);
    visited.assign(n + 1, false);
    for (int i = 1; i <= n; i++){
        cin >> credit[i];
    }
    for (int i = 1; i <= n; i++){
        int Ai;
        cin >> Ai;
        for (int j = 0; j < Ai; j++){
            int v;
            cin >> v;
            graph[i].push_back(v);
            graph[v].push_back(i);
        }
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++){
        if (!visited[i]){
            auto res = dfs(i, -1);
            ans += max(res.first, res.second);
        }
    }
    cout<<ans<<endl;
    return 0;
}
