#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include<bits/stdc++.h>
//#pragma GCC target("popcnt")
using namespace std;
#define int long long
#define X first
#define Y second
int n;
int ar[500005];
int uf[500005];
int gmx[500005];
//vector<int>e[500005];
int fin(int x){
if(uf[x]==x){return x;}
uf[x]=fin(uf[x]);
return uf[x];
}
void mrg(int a,int b){
int pa=fin(a);int pb=fin(b);
if(pa==pb){return;}
uf[pa]=pb;
gmx[pb]=max(gmx[pb],gmx[pa]);
}
pair<int,int>e[500005];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>ar[i];
uf[i]=i;
gmx[i]=ar[i];
}
for(int i=1;i<=n-1;i++){
cin>>e[i].X>>e[i].Y;
}
int ans=0;
for(int t=1;t<=n-1;t++){
int gp=fin(1);
for(int i=1;i<=n;i++){
if(gmx[fin(i)]<gmx[gp]){
gp=fin(i);
}
}
int gp2=0;
for(int i=1;i<=n-1;i++){
int a=e[i].X;int b=e[i].Y;
if(fin(a)==gp&&fin(b)==gp){continue;}
if(fin(a)!=gp&&fin(b)!=gp){continue;}
if(fin(b)==gp){swap(a,b);}
if(gp2==0||gmx[gp2]>gmx[fin(b)]){
gp2=fin(b);
}
}
ans+=gmx[gp]+gmx[gp2];
mrg(gp,gp2);
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1078 ms |
5136 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
7 ms |
340 KB |
Output is correct |
7 |
Correct |
7 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
7 ms |
316 KB |
Output is correct |
10 |
Correct |
7 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
1078 ms |
5136 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |