题目

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 neighboringand 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:

Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. 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 ≤ n, ui ≠ 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个点,n-1条边,现在让你任选一个点当做起点,去掉这个点,然后和这个点连接的所有的点的权值都加一,然后所有和那个点相连的点的点的点权也要加一,有点绕,慢慢理解,1->2->3,就是说去掉1后2和3的点权都要加一,然后问你最大集合中的最小点权。

根据这个易知这是个无根树。
然后接下来是来一遍预处理。求出各个点中与其相邻Max,Max-1的个数,包含当前节点。接下来设所有点中Max的个数为sum1, Max-1的个数为sum2;

(1)当答案为Max时,这时得有sum1==1,且当前点的子节点包含了全部的值为Max-1的节点。

(2)当答案为Max+1时,这时候要有一个节点的子节点包含了所有的值Max的节点。

(3)剩下的就是答案为Max+2;

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
const int inf = 0x3f3f3f3f;
const int maxn = 4e5+;
using namespace std;
int a[maxn];
vector<int >G[maxn];
int n;
int solve(){
int x=,y=;
int maxval=-inf;
for(int i=;i<=n;i++){
if(a[i]>maxval)
maxval=a[i];
}
for(int i=;i<=n;i++){
if(a[i]==maxval)
x++;
if(a[i]==maxval-)
y++;
}
int xx=x;
int yy=y;
bool flag1=true;
bool flag2=true;
for(int i=;i<=n;i++){
bool flag3=true;
if(a[i]==maxval)
x--;
else if(a[i]==maxval-)
y--;
for(int j=;j<(int )G[i].size();j++){
if(a[G[i][j]]==maxval){
x--;
flag3=false;
}
if(a[G[i][j]]==maxval-)
y--;
}
if(x==){
flag1=false;
if(y==&&flag3)
flag2=false;
}
x=xx,y=yy;
}
if(!flag1){
if(flag2==)
return maxval;
else
return maxval+;
}
return maxval+;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<n-;i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
int ans=solve();
printf("%d\n",ans);
return ;
}

codeforce 796C - Bank Hacking(无根树+思维)的更多相关文章

  1. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  2. CodeForces - 796C Bank Hacking

    思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...

  3. 【模拟】CF 796C Bank Hacking

    题目大意 洛谷链接 给定一棵带点权树,选出一个最佳的根节点,使得根节点的点权不变,它的儿子点权加1,其余点点权加2,并使最大点权最小,输出这个最小的最大点权. 其他见链接(懒). PS:原题面很不好总 ...

  4. C. Bank Hacking 解析(思維)

    Codeforce 796 C. Bank Hacking 解析(思維) 今天我們來看看CF796C 題目連結 題目 略,請直接看原題. 前言 @copyright petjelinux 版權所有 觀 ...

  5. CF796C Bank Hacking 思维

    Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search f ...

  6. Bank Hacking CodeForces - 796C

    题目 题意: 一条笨狗要去黑银行,银行有n个,它们之间用n-1条边连接.可以选择任意一个银行开始黑,但是后面每一次黑的银行都要求与已经黑过的银行直接相连.每个银行初始有一个防御值,每一个银行被黑后,与 ...

  7. 【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)

    [题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...

  8. 【codeforces 796C】Bank Hacking

    [题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...

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

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

随机推荐

  1. Java实现 蓝桥杯 算法提高 抽卡游戏

    试题 算法提高 抽卡游戏 某个抽卡游戏卡池抽出限定卡的概率为p,该游戏有一个"井"的机制,抽满k次卡后直接送这张限定卡.试求获得这张限定卡需要的期望抽卡次数.输入为一行,用空格隔开 ...

  2. Java实现 LeetCode 590 N叉树的后序遍历(遍历树,迭代法)

    590. N叉树的后序遍历 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? ...

  3. Java实现 LeetCode 394 字符串解码

    394. 字符串解码 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k ...

  4. Java实现 LeetCode 35 搜索插入位置

    35. 搜索插入位置 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1, ...

  5. Android Button的四种点击事件

    bta1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.m ...

  6. C++拷贝构造函数被调用的时机

    拷贝构造函数调用的几种情况: 当用类的一个对象去初始化该类的另一个对象(或引用)时系统自动调用拷贝构造函数实现拷贝赋值. 若函数的形参为类对象,调用函数时,实参赋值给形参,系统自动调用拷贝构造函数.( ...

  7. MAC/VMware配置双机调试简述

    Configuration 注:建议提前备份所有修改内容,可能会导致无法开机. 我的测试环境: server: windows 10 + windbg client: windows 7 Server ...

  8. 1.vue的基础认识

    vue 1.基于MvvM MVC--MVVM,是MVC的改进版      MVVM主要是将视图的状态和行为抽象化,把视图和业务逻辑分开      M:模型--存放状态的容器,是以数据为中心的      ...

  9. 开发小白可以一年涨薪10w?这份java文档功不可没,学透你也可以

    靠这份文档,跳槽涨薪10K 金九银十的时候我分享了一份面试文档给我的兄弟,没想到这哥们2个月之内斩获数个BAT的offer, 最后选择了一个他最想要去的公司,既然有这么好的效果,我就打算把这份文档分享 ...

  10. NASH:基于丰富网络态射和爬山算法的神经网络架构搜索 | ICLR 2018

    论文提出NASH方法来进行神经网络结构搜索,核心思想与之前的EAS方法类似,使用网络态射来生成一系列效果一致且继承权重的复杂子网,本文的网络态射更丰富,而且仅需要简单的爬山算法辅助就可以完成搜索,耗时 ...