C. Bank Hacking
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. The strength of bank x is less than or equal to the strength of Inzane's computer.

Determine the minimum strength of the computer Inzane needs to hack all the banks.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.

Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

Examples
input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
output
5
input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
output
93
input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
output
8
Note

In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:

  • Initially, strengths of the banks are [1, 2, 3, 4, 5].
  • He hacks bank 5, then strengths of the banks become [1, 2, 4, 5,  - ].
  • He hacks bank 4, then strengths of the banks become [1, 3, 5,  - ,  - ].
  • He hacks bank 3, then strengths of the banks become [2, 4,  - ,  - ,  - ].
  • He hacks bank 2, then strengths of the banks become [3,  - ,  - ,  - ,  - ].
  • He completes his goal by hacking bank 1.

In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93.

题意:
有n家银行,每个银行都有自己的权值,当我们攻击一个银行时,跟他距离为1和2的银行权值都会+1。

只有在我们本身权值大于银行权值时才可以攻击,求本身至少需要多少权值。

可以将所有的银行关系看成一棵树,则与根节点距离为一的银行最终会+1,其余的+2.

由此我们就有maxn,maxn+1,maxn+2三种可能的结果。

当最大值maxn只有一个时,若所有maxn-1距离maxn都为1,则本身需要maxn,否则需要maxn+1。

当最大值大于一个时,若存在一点,其本身和距离为1的点包含了所有maxn,则只需要maxn+1,

否则maxn+2。

附AC代码:

 #include<bits/stdc++.h>
using namespace std; const int N=; vector<int> mp[N];
int a[N]; int main(){
int n,x,y,u,ans;
int maxn=-;
cin>>n;
for(int i=;i<=n;i++){
mp[i].clear();
cin>>a[i];
maxn=max(a[i],maxn);
}
for(int i=;i<n;i++){
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
int ma=,mb=;
for(int i=;i<=n;i++){
if(a[i]==maxn)
ma++,u=i;
if(a[i]==maxn-)
mb++;
}
if(ma==){
int cont=;
for(int i=;i<mp[u].size();i++){
int v=mp[u][i];
if(a[v]==maxn-)
cont++;
}
if(cont==mb)
ans=maxn;
else
ans=maxn+;
cout<<ans<<endl;
}
else{
bool flag=false;
for(int i=;i<=n;i++){
int cont=;
if(a[i]==maxn)
cont++;
for(int j=;j<mp[i].size();j++){
int v=mp[i][j];
if(a[v]==maxn)
cont++;
}
if(cont==ma)
flag=true;
}
if(flag){
cout<<maxn+<<endl;
}
else{
cout<<maxn+<<endl;
}
}
return ;
}

最新文章

  1. jquery插件的用法之cookie 插件
  2. 两个js文件之间函数互调问题
  3. NFC读写实例
  4. hdu4833 Best Financing(DP)
  5. apt-get的常用用法
  6. WPF编程学习——布局
  7. Oracle EBS-SQL (INV-11):检查子库存会计信息.sql
  8. QT:“下载速度柱状图”的模拟实现——思路真好,会动脑筋,连我都有了启发(这个思路好像是通用的)
  9. layer弹窗插件实战用法小结1—— layer.alert()
  10. 教你用python写:HDU刷题神器
  11. 开始连载啦~每周2更共11堂iKcamp课|基于Koa2搭建Node.js实战项目教学(含视频)| 课程大纲介绍
  12. 一种使用 sprintf 导致死机的情况
  13. jquery瀑布流排列样式代码
  14. MyBatis-进阶2
  15. Jenkins的配置从节点中默认没有Launch agent via Java Web Start,该如何配置使用
  16. lanmp中环境变量的更改方法
  17. Linux之用户组、文件权限详解
  18. 在Storm的Toplogy中设置多数据源Spout
  19. django中ModelForm save方法 以及快速生成空表单或包含数据的表单 包含错误信息
  20. cocos2d-x 发布win32 release版本后找不到msvcr110.dll

热门文章

  1. CentOSyum操作
  2. Gson解析数组和list容器
  3. 使用Python与数据库交互
  4. HDU 5384 Danganronpa (2015年多校比赛第8场)
  5. bzoj-2251 外星联络
  6. storm是怎样保证at least once语义的
  7. NOTE:rfc5766-turn-server
  8. Django中如何实现数据库路由?
  9. Xmpp学习之Asmack取经-asmack入门(一)
  10. TortoiseSVN使用笔记