This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
long long n, a[100001], p[100001], sz[100001], sum = 0;
pair<int, pair<int, int>> edge[100001];
int findparent(int x)
{
if(x == p[x]) return x;
else return p[x] = findparent(p[x]);
}
int main()
{
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> a[i];
p[i] = i;
sz[i] = a[i];
}
for(int i = 1; i < n; ++i)
{
cin >> edge[i].second.first >> edge[i].second.second;
edge[i].first = max(a[edge[i].second.first], a[edge[i].second.second]);
}
sort(edge + 1, edge + n);
for(int i = 1; i < n; ++i)
{
long long a = findparent(edge[i].second.first), b = findparent(edge[i].second.second);
sum = sum + sz[a] + sz[b];
if(a != b)
{
p[a] = b;
sz[b] = max(sz[a], sz[b]);
}
}
cout << sum;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |