CF1083A.The Fair Nut and the Best Path

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city uu and go by a simple path to city vv. He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex.

A filling station is located in every city. Because of strange law, Nut can buy only wiw_i liters of gasoline in the ii-th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.

He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.

公平的坚果将前往树之国旅行,该国有 nn 座城市。这个国家的大部分土地被森林覆盖。此外,当地的公路系统构成一棵树(即无环的连通图)。坚果希望在城市 uu 租一辆车,并沿一条简单路径前往城市 vv。他尚未确定具体路径,现在是时候决定了。注意:所选路径可以仅包含一个顶点。

每座城市都设有一座加油站。由于一项奇特的法律,坚果在第 ii 座城市最多只能购买 wiw_i 升汽油。我们可以假设他拥有无限的资金。每条道路都有一个长度,一旦坚果驶过该道路,其汽油量就会减少该长度值。显然,坚果不能选择一条会导致他在途中耗尽汽油的路径。他可以在访问到的每一座城市购买汽油,包括起点和终点城市。

他还希望找出在路径终点时所能拥有的最大汽油量。请帮助他计算该值。

输入格式

The first line contains a single integer nn (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5) — the number of cities.

The second line contains nn integers w1,w2,…,wnw_1, w_2, \ldots, w_n (0≤wi≤1090 \leq w_{i} \leq 10^9) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n−1n - 1 lines describes road and contains three integers uu, vv, cc (1≤u,v≤n1 \leq u, v \leq n, 1≤c≤1091 \leq c \leq 10^9, u≠vu \ne v), where uu and vv — cities that are connected by this road and cc — its length.

It is guaranteed that graph of road connectivity is a tree.

第一行包含一个整数 nn(1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5)——城市的数量。

第二行包含 nn 个整数 w1,w2,…,wnw_1, w_2, \ldots, w_n(0≤wi≤1090 \leq w_{i} \leq 10^9)——Nut 在各城市最多可购买的汽油升数。

接下来的 n−1n - 1 行每行描述一条道路,包含三个整数 uu、vv、cc(1≤u,v≤n1 \leq u, v \leq n,1≤c≤1091 \leq c \leq 10^9,u≠vu \ne v),其中 uu 和 vv 是该道路所连接的两个城市,cc 是该道路的长度。

保证道路连通性构成的图是一棵树。

输出格式

Print one number — the maximum amount of gasoline that he can have at the end of the path.

输出一个数字——他在路径终点所能拥有的汽油最大量。

输入输出样例

  • 输入#1

    3
    1 3 3
    1 2 2
    1 3 2

    输出#1

    3
  • 输入#2

    5
    6 3 2 5 0
    1 2 10
    2 3 3
    2 4 1
    1 5 1

    输出#2

    7

说明/提示

The optimal way in the first example is 2→1→32 \to 1 \to 3.

The optimal way in the second example is 2→42 \to 4.

第一个样例的最优路径是 2→1→32 \to 1 \to 3。

第二个样例的最优路径是 2→42 \to 4。

输入解题思路,AI测评打分。不知道怎么写?

首页