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. Java小日历

    自己写的一个小小日历.执行程序是柯自己主动定位到当前年月日,当点击下个月button是会定位到下个月的这一天,就是说天数不会变.当在一个月中点击某一天时,以下的时间也会随时变化. import jav ...

  2. (testng多个class文件执行时混乱,不是等一个class内的所有methods执行完再去执行下一个class内的内容)问题的解决

    问题描述如下: We use TestNG and Selenium WebDriver to test our web application. Now our problem is that we ...

  3. A new session could not be created. (Original error: Requested a new session but one was in progress) )错误解决办法

    z在desiredCapabilities里新增这俩居然fix了问题,原因暂时不得而知: capabilities.setCapability("unicodeKeyboard", ...

  4. 我遇到的错误curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused

    今天我用curl命令,无论如何都是出现: curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused 找了很久,不知道 ...

  5. 小程序多级下拉菜单demo

    小程序多级下拉菜单demo - CSDN博客 https://blog.csdn.net/github_39371177/article/details/80251211

  6. FAT和FAT32文件系统的原理

    [转自] http://www.sjhf.net/Article/sjhfdoc/200404/1.html 一.硬盘的物理结构:     硬盘存储数据是根据电.磁转换原理实现的.硬盘由一个或几个表面 ...

  7. react native 之页面布局

     第一章 flexbox 布局 1.flexDirection:'row', 水平 flexDirection:'column',垂直 需要在父元素上设置这种属性才能实现flex. flex:1 会撑 ...

  8. HDU2476 String painter —— 区间DP

    题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others)    Me ...

  9. 通过mysqldumpslow来分析日志

    通过mysqldumpslow来分析日志. 将mysql加入到全局变量中!!! sudo vim /etc/profile # 添加Mysql export PATH=$PATH:/usr/local ...

  10. java -- 虚拟机和内存

    从大方向来分:栈内存,堆内存,方法区,本地方法栈,程序计数器 java从存储数据的角度来分: 寄存器(register):最快的存储区,由编译器根据需求进行分配,不由认为控制. 堆栈(statck): ...