import sys
from collections import deque
def main():
n = int(sys.stdin.readline())
c = []
for _ in range(n):
ci = int(sys.stdin.readline())
c.append(ci)
# 构建邻接表,使用集合避免重复边
adj = [set() for _ in range(n)]
for i in range(n):
parts = list(map(int, sys.stdin.readline().split()))
ai = parts[0]
courses = parts[1:]
for course in courses:
j = course - 1 # 转换为0-based
adj[i].add(j)
adj[j].add(i)
# 转换为列表以便后续处理
adj = [list(s) for s in adj]
visited = [False] * n
total = 0
for i in range(n):
if not visited[i]:
# BFS找到连通分量
q = deque()
q.append(i)
visited[i] = True
component = []
while q:
u = q.popleft()
component.append(u)
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
k = len(component)
if k == 1:
total += c[component[0]]
elif k == 2:
total += max(c[component[0]], c[component[1]])
else:
# 寻找中心节点
center = None
for u in component:
if len(adj[u]) == k - 1:
# 检查其他节点是否度数均为1
valid = True
for v in component:
if v == u:
continue
if len(adj[v]) != 1:
valid = False
break
if valid:
center = u
break
if center is not None:
sum_leaves = sum(c[v] for v in component if v != center)
total += max(c[center], sum_leaves)
else:
# 根据题目条件,这不可能发生
pass
print(total)
if __name__ == "__main__":
main()