Description

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 areneighboring 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.

题意:我们可以攻击比自己能力小的,攻击成功后,与它相邻的或者间接相邻防御的会+1,问全部攻击成功最小要多少攻击力

题意:因为是求最小值,想到二分,我们把最小防御和最大防御求出来。接下来就是判断能不能符合条件了,我们根据模拟发现,除了相邻的或者间接相邻的,其他都是+2,我们就先将所有防御与中间值比较,大的就return 0,否则就+2

对于每一个+2的点,如果大于中间值,记录个数,再次遍历,到了大于中间值的点,那么说明可以作为开始点,与它相邻的点-1等于中间值,减去个数,如果最后等于0,说明可行

 #include<bits/stdc++.h>
using namespace std;
const int len=;
long long maxn=-;
long long minn=(<<)-;
long long x[len];
long long y[len];
long long n,m;
vector<int>q[len];
int solve(long long w)
{
int cnt=;
for(int i=;i<=n;i++)
{
y[i]=x[i];
//if(x[i]>w) return 0;
}
for(int i=;i<=n;i++)
{
if(x[i]>w) return ;
y[i]+=;
if(y[i]>w)
{
cnt++;
}
}
if(cnt==) return ;
if(n==) return ;
for(int i=;i<=n;i++)
{
int ans=cnt;
if(y[i]>w)
{
ans--;//大于x的点作为起点,相邻的点为+1
}
for(int j=;j<q[i].size();j++)//遍历相邻的点
{
int pos=q[i][j];
if(pos==i) continue;
if(y[pos]-==w)
{
ans--;
}
}
if(ans==) return ;
}
return ;
} long long answer;
int main()
{
scanf("%lld",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&x[i]);
maxn=max(x[i],maxn);
minn=min(x[i],minn);
}
for(int i=;i<=n-;i++)
{
long long u,v;
scanf("%lld%lld",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
}
if(n==)
{
printf("%lld\n",x[]);
return ;
}
long long ans;
maxn=maxn+;
while(minn<=maxn)
{
long long mid=(maxn+minn)/;
if(solve(mid))
{
ans=mid;
maxn=mid-;
}
else
{
minn=mid+;
}
}
printf("%lld\n", ans);
return ;
}

Codeforces Round #408 (Div. 2) C的更多相关文章

  1. Codeforces Round #408 (Div. 2)(A.水,B,模拟)

    A. Buying A House time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  2. Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)

    题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...

  3. Codeforces Round #408 (Div. 2) C. Bank Hacking

    http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...

  4. Codeforces Round #408 (Div. 2) A B C 模拟 模拟 set

    A. Buying A House time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  6. Codeforces Round #408 (Div. 2) B

    Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, n ...

  7. Codeforces Round #408 (Div. 2)

    C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...

  8. Codeforces Round #408 (Div. 2) 题解【ABCDE】

    A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...

  9. Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)

    传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...

  10. Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)

    传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...

随机推荐

  1. js弹出QQ对话框在线交谈

    <div style="position:absolute; top:110px; right:220px; z-index:2;"> <a target=&qu ...

  2. redis07-----Redis持久化配置

    Redis持久化配置 持久化: 即把数据存储于断电后不会丢失的设备中,通常是硬盘. 常见的持久化方式: 主从:通过从服务器保存和持久化,如mongoDB的replication sets配置. 淘宝是 ...

  3. 总结 <stdlib.h>头文件 在算法中可能会用到的一些函数

    头文件<stdlib.>具有一定的总结性. 它定义了类型.宏和各种函数,这些函数用于:内存管理.排序和查找.整形运算.字符串到数字的转换.伪随机数序列.与环境的接口.把多字节字符串和字符转 ...

  4. MYSQL进阶学习笔记三:MySQL流程控制语句!(视频序号:进阶_7-10)

    知识点四:MySQL流程控制语句(7-10) 选择语句: (IF ELSE ELSE IF CASE 分支)IFNULL函数 IF语法: 语法规则: IF search_condition THEN ...

  5. 一步一步学Silverlight 2系列(13):数据与通信之WebRequest

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. hdu 4463 Outlets(最小生成树)

    题意:n个点修路,要求总长度最小,但是有两个点p.q必须相连 思路:完全图,prim算法的效率取决于节点数,适用于稠密图.用prim求解. p.q间距离设为0即可,最后输出时加上p.q间的距离 pri ...

  7. 各种java生成word解决方案的优缺点对比

    解决方案 优点 缺点 Jacob 功能强大 直接调用VBA接口,程序异常复杂:服务器必须是:windows系统+安装Office:服务器端自动化com接口容易产生死进程造成服务器宕机 Apache P ...

  8. codeforces 696B B. Puzzles(树形dp+概率)

    题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. [Java]手动编译Java

    1.安装JDK 2.编写 Example.java 程序放到C 盘 public class Example { public static void main(string[] args) { sy ...

  10. Map集合的几种遍历方式

    Map<String ,String> map=new HashMap<String,String>(); map.put("1","value1 ...