796C - Bank Hacking(思维+STL)

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

Copy

5
1 2 3 4 5
1 2
2 3
3 4
4 5

output

Copy

5

input

Copy

7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6

output

Copy

93

input

Copy

5
1 2 7 6 7
1 5
5 3
3 4
2 4

output

Copy

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条线,所以这是一棵树。

然后每个银行都有它的防御力,黑掉这个银行所需要的电脑攻击力必须大于等于银行的防御力才行。

然后每次攻击任意一个银行的时候,与他连接的银行的防御力+1,然后这些与它相邻的银行,的相邻的银行防御力也+1;

问攻击掉所有银行所必须的电脑攻击力的最小值;

第一次选择一个银行进行攻击,然后以后每一次选择的银行必须与被攻击过的银行相邻。。

这是一颗树,所以每个银行防御力最多加2,想一下树的性质在纸上画一下就能推出来。

然后就好办了。最后的结果一定是mx,mx+1,mx+2;这三个值的其中一个。

mx是这些点的最大权值,。

1.结果是mx的情况:

必须满足mx值的只有一个,而且它是第一个攻击的银行,然后次大点都在它的旁边。

(因为所有点除了第一次攻击的银行防御力不增加,它周围的银行防御力加1,其他银行的防御力都是加2)

2.结果是mx+1的情况;

必须满足mx全都在第一个点和它周围的这些点中,这样所有银行防御力都不会超过mx+1;

3.剩余的所有情况都是mx+2;

#include<bits/stdc++.h>
using namespace std;
#define M 300000+100
vector<int> mp[M];
int a[M];
int main()
{
    int n,mx=-2000000000,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        mx=max(a[i],mx);
    }
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d %d",&u,&v);
        mp[u].push_back(v);
        mp[v].push_back(u);
    }
    int maxn=0,maxnn=0;
    for(int i = 1;i <= n; i++)
    {
        if(a[i] == mx)maxn++,u=i;
        if(a[i] == mx-1) maxnn++;
    }
    if(maxn==1)
    {
        int k=0;
        for(int i=0;i<mp[u].size(); i++)
        {
            if(a[mp[u][i]]==mx-1)
                k++;
        }
        if(k==maxnn)
        {
            printf("%d\n",mx);
            return 0;
        }
    }
    int q;
    for(int j=1;j<=n;j++)
    {
        q=0;
        for(int i=0;i<mp[j].size(); i++)
        {
            if(a[mp[j][i]]==mx)
                q++;
        }
        if(a[j]==mx)
            q++;
        if(q==maxn)
        {
            printf("%d\n",mx+1);
            return 0;
        }
    }
    printf("%d\n",mx+2);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值