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. Yarn调度器负载模拟器——Yarn Scheduler Load Simulator (SLS)

    一.概述: Yarn调度器有很多实现,如Fifo, Capacity和Fair schedulers等.与其同一时候,正在进行一些优化措施来提高调度器在不同负载和工作场景下的性能.每一个调度器都有自己 ...

  2. 基础才是重中之重~用好configSections让配置信息更规范

    对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...

  3. XHTML中button加入超链接以及使插入图片与屏幕一样大

    1.button加入超链接 (1)假设是在本页跳转到新页面.用 <span style="font-size:18px;"><input type="b ...

  4. java8--面向对象 下(疯狂java讲义3) 复习笔记

    1.如果一个包装类和一个基本类型比较大小,或者是两个基本类型进行比较大小,直接用==就好: 如果是两个包装类进行比较大小,那么使用equals(),返回值是true,false,或者使用Xxx.com ...

  5. FLTK 简介

          FLTK,如同其名字所表达的:The Fast Light Tool Kit,一个轻量级的GUI开发库.但这轻量级并不代表功能的羸弱,相反,FLTK在具有基本的GUI功能之外,还拥有一些特 ...

  6. HTML canvas

    什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...

  7. Kafka使用kclient三种使用方法

    kclient提供了三种使用方法,对于每一种方法,按照下面的步骤可快速构建Kafka生产者和消费者程序. 前置步骤1) 下载源代码后在项目根目录执行如下命令安装打包文件到你的Maven本地库. mvn ...

  8. Oracle10G各版本下载以及补丁地址

    Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise/Standard Edition for Microsoft Windows (32-bit ...

  9. codeforces 691B B. s-palindrome(水题)

    题目链接: B. s-palindrome 题意: 问给定的字符串是否是镜面对称; 思路: 直接看哪些字母是镜面对称的就行: AC代码: //#include <bits/stdc++.h> ...

  10. Python机器视觉编程常用数据结构与示例

    本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slicing).加(adding).乘(multi ...