比赛 20160415x 评测结果 WWWWWWEAWW
题目名称 游戏内测 最终得分 10
用户昵称 dydxh 运行时间 2.856 s
代码语言 C++ 内存使用 20.72 MiB
提交时间 2016-04-15 16:28:51
显示代码纯文本
/*
Problem:Gamebeta;
Language:c++;
by dydxh;
Date:2016.04.15;
*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<utility>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<ctime>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define ull unsigned long long
using namespace std;
const int oo=2000000000;
const int maxn=500005;
int n,Len,Leftt=oo,Rightt,Ans;
int C[maxn],Linkk[maxn],Limit[maxn];
int Maxc[maxn],Tor[maxn],L[maxn],R[maxn];
struct edge{
	int y,next;
}e[maxn<<1];
inline int read(){
	int x=0;bool flag=false;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-') flag=true;ch=getchar();}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return flag?-x:x;
}
void Insert(int a,int b){
	e[++Len].next=Linkk[a],Linkk[a]=Len,e[Len].y=b;
	e[++Len].next=Linkk[b],Linkk[b]=Len,e[Len].y=a;
}
int Tim=0;
bool Dfs(int x){
	if(Limit[x]<Tim) return false;
	for(int i=L[x];i<=R[x];i++)
		if(Dfs(Tor[i])==false)
			return false;
	return true;
}
bool Check(int Fence){
	for(int i=1;i<=n;i++){
		Limit[i]=Fence-C[i];
		if(Limit[i]<=0) return false;
	}
	Tim=0;
	if((n-1)*2>Limit[1]) return false;
	if(Dfs(1)) return true;
	return false;
}
bool mycmp(int x,int y){
	return Maxc[x]>Maxc[y];
}
void Get_Son(int x,int Fa){
	Maxc[x]=C[x];
	for(int i=Linkk[x];i;i=e[i].next){
		int Tn=e[i].y;
		if(Tn==Fa) continue;
		Get_Son(Tn,x);
		if(Maxc[x]<Maxc[Tn]) Maxc[x]=Maxc[Tn];
	}
	L[x]=Tor[0]+1;
	for(int i=Linkk[x];i;i=e[i].next){
		int Tn=e[i].y;
		if(Tn==Fa) continue;
		Tor[++Tor[0]]=Tn;
	}
	R[x]=Tor[0];
	if(L[x]<R[x]) sort(Tor+L[x],Tor+R[x]+1,mycmp);
}
void Prepare(){
	n=read();
	for(int i=1;i<=n;i++){
		C[i]=read();
		if(Leftt>C[i]) Leftt=C[i];
		if(Rightt<C[i]) Rightt=C[i];
	}
	Leftt+=((n-1)<<1)-1,Rightt+=((n-1)<<1)+1;
	for(int i=1;i<n;i++) Insert(read(),read());
	Get_Son(1,0);
}
int main(){
	//freopen("cc.in","r",stdin);
	freopen("gamebeta.in","r",stdin);
	freopen("gamebeta.out","w",stdout);
	Prepare();
	while(Leftt+1<Rightt){
		int Mid=(Leftt+Rightt)>>1;
		if(Check(Mid)) Rightt=Ans=Mid;
		else Leftt=Mid;
	}
	cout<<Ans<<endl;
	//cout<<"Time has passed:"<<1.0*clock()/1000<<"s!"<<endl;
    return 0;
}