intkruskal(int n, vector<edge> v){ int ans = 0; int edgeCount = 0; for (int i = 0; i <= n; i++) par[i] = i; sort(v.begin(), v.end(), cmp);
for (int i = 0; i != v.size(); i++) { int p1 = find(v[i].u); int p2 = find(v[i].v); if (p1 != p2) { par[p1] = p2; ans += v[i].w; edgeCount++; if (edgeCount == n) break; } }
return ans; }
intmain(){ int n; cin >> n; vector<edge> v; for (int i = 1; i <= n; i++) { int a; cin >> a; edge e1(0, i, a); edge e2(i, 0, a); v.push_back(e1); v.push_back(e2); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int a; cin >> a; if (a != 0) { edge e(i, j, a); v.push_back(e); } } } int res = kruskal(n, v); cout << res << endl; return0; }