제출 #647741

#제출 시각아이디문제언어결과실행 시간메모리
647741sadorn666Sjekira (COCI20_sjekira)C++14
110 / 110
45 ms5068 KiB
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define filetest freopen("debug.INP","r",stdin);freopen("debug.OUT","w",stdout);
#define file freopen("debug.INP","r",stdin);freopen("debug.OUT","w",stdout);
#define pii pair<int,int>
#define ll long long
const int N=1e5+6;
int p[N], d[N],s[N];
vector <pii> e;
bool cmp(const pii& a, const pii& b) {
    int x = max(d[a.first], d[a.second]);
    int y = max(d[b.first], d[b.second]);
    return x<y;
}
int fset(int v)
{
    if (v== p[v]) return v;
    return p[v]=fset(p[v]);
}
ll temp=0;
void uset(int a,int b)
{
    //int b=a-1;
    //if (a>b) swap(a,b);
    a = fset(a);
    b = fset(b);
    if (a!=b)
    {
        temp+=s[a]+s[b];
        s[b]=max(s[b],s[a]);
        p[a]=b;
    }
}
int main(){
    fast
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> d[i];
        s[i]=d[i];
        p[i]=i;
    }
    n--;
    while (n--)
    {
        int x,y;
        cin >> x >> y;
        e.push_back(pii(x,y));
    }
    sort(e.begin(), e.end(), cmp);
    for (auto i : e)
    {
        uset(i.first,i.second);
    }
    cout << temp<< " ";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...