Submission #807346

#TimeUsernameProblemLanguageResultExecution timeMemory
807346Tunglam07Sjekira (COCI20_sjekira)C++17
110 / 110
84 ms4704 KiB
#include<bits/stdc++.h>
using namespace std;
 
long long n, a[100001], parent[100001], ans[100001], sum = 0;
pair<int, pair<int, int>> connect[100001];
 
int findparent(int id)
{
	if(id == parent[id])	
	{
		return id;
	}
	else	
	{
		return parent[id] = findparent(parent[id]);
	}
}
 
int main()
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		cin >> a[i];
		parent[i] = i;
		ans[i] = a[i];
	}
	for(int i = 1; i < n; ++i)
	{
		cin >> connect[i].second.first >> connect[i].second.second;
		connect[i].first = max(a[connect[i].second.first], a[connect[i].second.second]);
	}
	sort(connect + 1, connect + n);
	for(int i = 1; i < n; ++i)
	{
		long long x = findparent(connect[i].second.first), y = findparent(connect[i].second.second);
		sum = sum + ans[x] + ans[y];
		if(x != y)
		{
			parent[x] = y;
			ans[y] = max(ans[x], ans[y]);
		}
	}
	cout << sum;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...