Блог пользователя siriexo

Автор siriexo, история, 4 часа назад, По-английски

I've been working on a tree problem and would like some feedback on the problem itself. The intended solution is around $$$2100$$$, with a plausible range of roughly $$$2200$$$--$$$2400$$$. The main ingredients are LCA observations, inclusion-exclusion, small-to-large merging, and Li Chao trees. I would especially like to know whether there is a significantly simpler solution that I have missed.

Problem

You are given a tree with $$$N$$$ vertices.

Each vertex $$$v$$$ has an integer value $$$P_v$$$, and $$$P$$$ is a permutation of $$$1,2,\ldots,N$$$.

The tree is rooted at the unique vertex $$$r$$$ satisfying

$$$ P_r=1. $$$

It is guaranteed that for every parent-child edge,

$$$ P_{\operatorname{parent}(v)} \lt P_v. $$$

You must delete exactly two edges.

For two vertices $$$u,v$$$ that remain in the same connected component, define

$$$ B(u,v)=\min_{x\in\operatorname{path}(u,v)}P_x, $$$

where the path is the path in the original tree.

The score of the resulting forest is

$$$ \sum_{\substack{u \lt v\\u,v\text{ remain connected}}}B(u,v). $$$

Find the maximum possible score.

You do not have to output the two deleted edges.

Input

The first line contains an integer $$$N$$$ ($$$3\le N\le2\cdot10^5$$$).

The second line contains $$$N$$$ integers $$$P_1,P_2,\ldots,P_N$$$ — a permutation of $$$1,2,\ldots,N$$$.

The next $$$N-1$$$ lines contain two integers $$$u_i,v_i$$$ ($$$1\le u_i,v_i\le N$$$, $$$u_i\ne v_i$$$), describing an edge between vertices $$$u_i$$$ and $$$v_i$$$.

The given graph is a tree.

It is guaranteed that if the tree is rooted at the unique vertex $$$r$$$ satisfying $$$P_r=1$$$, then for every non-root vertex $$$v$$$,

$$$ P_{\operatorname{parent}(v)} \lt P_v. $$$

Output

Print one integer — the maximum possible score after deleting exactly two edges.

Examples

Example 1

Input

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

Output

10

Example 2

Input

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

Output

15

Example 3

Input

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

Output

19

Solution
Code

Discussion

I am mainly looking for feedback on the difficulty and whether the problem has any significantly easier unintended solution.

My current estimate is around 2100, with a plausible range of 2200-2400.

I would probably place it around D/E in a combined Div. 1 + Div. 2 contest.

In particular, I would be interested in:

  1. Whether the problem is actually around the claimed difficulty.
  2. Whether there are substantially simpler approaches.
  3. Whether the constraints are appropriate.
  4. Whether the statement has any ambiguity or missing edge cases.
  5. Whether the problem feels suitable for a Codeforces round.

If someone finds a significantly simpler solution, that would be especially useful feedback.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
3 часа назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

If this showed up as problem D in a Div 1+2 round I think I would ragequit the contest...

  • »
    »
    3 часа назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +3 Проголосовать: не нравится

    I'd say the problem is at least 2600 in difficulty. It's a nice problem, although maybe a bit standard for a codeforces round.