Problem:
Dungeon 2

　

Bob is playing a game called "Dungeon 2" which is the sequel to the popular "Dungeon" released last year. The game is played on a map consisting of $N$ rooms and $N-1$ roads connecting them. The roads allow bidirectional traffic and the player can start his tour from any room and reach any other room by way of multiple of roads. A point is printed in each of the rooms.

Bob tries to accumulate the highest score by visiting the rooms by cleverly routing his character "Tora-Tora." He can add the point printed in a room to his score only when he reaches the room for the first time. He can also add the points on the starting and ending rooms of Tora-Tora’s journey. The score is reduced by one each time Tora-Tore passes a road. Tora-Tora can start from any room and end his journey in any room.

Given the map information, make a program to work out the maximum possible score Bob can gain.

Input

The input is given in the following format.

$N$
$p_1$
$p_2$
$...$
$p_N$
$s_1$ $t_1$
$s_2$ $t_2$
$...$
$s_{N-1}$ $t_{N-1}$

The first line provides the number of rooms $N$ ($1 \leq N \leq 100,000$). Each of the subsequent $N$ lines provides the point of the $i$-th room $p_i$ ($-100 \leq p_i \leq 100$) in integers. Each of the subsequent lines following these provides the information on the road directly connecting two rooms, where $s_i$ and $t_i$ ($1 \leq s_i < t_i \leq N$) represent the numbers of the two rooms connected by it. Not a single pair of rooms are connected by more than one road.

Output

Output the maximum possible score Bob can gain.

Sample Input 1

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

Sample Output 1

10

For example, the score is maximized by routing the rooms in the following sequence: $6 \rightarrow 5 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 1$.

Sample Input 2

4
5
0
1
1
1 2
2 3
2 4

Sample Output 2

5

The score is maximized if Tora-Tora stats his journey from room 1 and ends in the same room.

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int n; scanf("%d", &n);
	std::vector<int> p(n);
	std::vector<std::vector<int>> g(n);
	for(int i = 0; i < n; i++) scanf("%d", &p[i]);
	for(int i = 0; i < n - 1; i++) {
		int a, b; scanf("%d%d", &a, &b);

		g[a - 1].push_back(b - 1);
		g[b - 1].push_back(a - 1);
	}

	// t = 0: 戻らない
	// t = 1: 戻る
	std::vector<std::map<int, std::vector<std::vector<int>>>> dp(n);
	auto dfs = [&](auto&& dfs, int v, int par, int t, bool f) -> std::vector<int> {
		if(dp[v].count(par)) return dp[v][par][t];

		auto X = std::vector<int>(1, p[v]);
		auto Y = std::vector<int>(1, p[v]);
		if(f or par == -1) {
			for(auto e: g[v]) {
				if(e == par) continue;
				auto A = dfs(dfs, e, v, 0, f);
				auto B = dfs(dfs, e, v, 1, f);
				
				if(B[0] - 2 > 0) Y[0] += B[0] - 2;
			}
			for(auto e: g[v]) {
				if(e == par) continue;
				auto A = dfs(dfs, e, v, 0, f);
				auto B = dfs(dfs, e, v, 1, f);
				
				if(B[0] - 2 > 0) X.push_back(p[v] - 1 + A[0] + (Y[0] - (B[0] - 2) - p[v]));
				else X.push_back(p[v] - 1 + A[0] + (Y[0] - p[v]));
			}
			sort(begin(X), end(X));
			reverse(begin(X), end(X));
			X.resize(2);
		} else {
			auto PA = dfs(dfs, par, v, 0, f);
			auto PB = dfs(dfs, par, v, 1, f);
			auto AA = dfs(dfs, v, -1, 0, f);
			auto AB = dfs(dfs, v, -1, 1, f);

			Y[0] = AB[0];
			if(PB[0] - 2 > 0) Y[0] -= PB[0] - 2;
			if(p[v] - 1 + PA[0] + (Y[0] - p[v]) == AA[0]) {
				if(AA.size() >= 1) X[0] = AA[1] - (PB[0] - 2 > 0 ? PB[0] - 2 : 0);
			} else {
				X[0] = AA[0] - (PB[0] - 2 > 0 ? PB[0] - 2 : 0);
			}
			X[0] = std::max(X[0], p[v]);
		}
		dp[v][par].resize(2);
		dp[v][par][0] = X;
		dp[v][par][1] = Y;
	
		return dp[v][par][t];
	};
	dfs(dfs, 0, -1, 0, 1);
	dfs(dfs, 0, -1, 0, 0);

	int ans = -(1 << 30);
	for(int v = 0; v < n; v++) {
		auto X = dfs(dfs, v, -1, 0, 0);
		auto Y = dfs(dfs, v, -1, 1, 0);

		ans = std::max({ans, X[0], Y[0]});
	}
	printf("%d\n", ans);
	return 0;
}