题干:

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 wiwi 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.

Input

The first line contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of cities.

The second line contains nn integers w1,w2,…,wnw1,w2,…,wn (0≤wi≤1090≤wi≤109) — 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≤u,v≤n, 1≤c≤1091≤c≤109, u≠vu≠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.

Output

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

Examples

Input

3
1 3 3
1 2 2
1 3 2

Output

3

Input

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

Output

7

Note

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

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

题目大意:

每个点有权值,每个边也有权值,你可以任选一条路径使得 路径上的点的点权和减去边权和 最大,答案也可以只为一个点,输出这个最大值。

解题报告:

树形dp,遍历每个点的时候维护最大值和次大值,返回那个最大值然后答案可以从它本身,他和他的一棵子树,他和他的两棵子树,分别转移过来。(注意因为题干中权值肯定是正数,所以可以直接tmp1和tmp2都赋值为0,这样就不用更新三次maxx了。。)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 3e5 + 5;
typedef pair<int,ll> PIL;
vector<PIL> vv[MAX];
ll a[MAX];
ll dp[MAX];
ll maxx = -123123123123;
ll dfs(int cur,int rt) {int up = vv[cur].size();ll res=a[cur],tmp1=-123123123123,tmp2=-123123123123;for(int i = 0; i<up; i++) {PIL v = vv[cur][i];if(v.first == rt) continue;ll tmp = dfs(v.first,cur) - v.second;if(tmp >= tmp1) {tmp2 = tmp1;tmp1 = tmp;}else if(tmp >= tmp2) {tmp2 = tmp;}res = max(res,tmp + a[cur]);}maxx = max(maxx,tmp1+tmp2+a[cur]);maxx = max(maxx,tmp1+a[cur]);maxx = max(maxx,a[cur]);return res;
}
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);for(int u,v,i = 1; i<=n-1; i++) {ll c;scanf("%d%d%lld",&u,&v,&c);vv[u].pb(pm(v,c));vv[v].pb(pm(u,c)); }dfs(1,0);printf("%lld\n",maxx);return 0 ;
}
/*
2
2 2
1 2 1
*/

【CodeForces - 1084D】The Fair Nut and the Best Path (树形dp)相关推荐

  1. CodeForces 1084D The Fair Nut and the Best Path(树形dp)

    题目描述 The Fair Nut is going to travel to the Tree Country, in which there are n cities. Most of the l ...

  2. A. The Fair Nut and the Best Path(无根树dp详解)

    https://codeforces.com/problemset/problem/1083/A 题意:到达一个点得到这个点的价值,经过一个边花费这个边的价值,求得到的最大价值 思路:之前碰过一些无根 ...

  3. Codeforces 1084A - The Fair Nut and Elevator

    Codeforces 1084A - The Fair Nut and Elevator 题解链接 https://lucien.ink 题目链接 https://codeforces.com/con ...

  4. CF 1083 A. The Fair Nut and the Best Path

    A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...

  5. CF1083A The Fair Nut and the Best Path

    CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...

  6. Codeforces 1088E Ehab and a component choosing problem(树形DP)

    Codeforces 1088E Ehab and a component choosing problem(树形DP) 题意 给一棵树,要求从中选一些联通分量,使得平均联通分量重量总和最大.如果有多 ...

  7. CodeForces 1084A The Fair Nut and Elevator 题解

    A. The Fair Nut and Elevator time limit per test : 1 second memory limit per test : 256 megabytes in ...

  8. CodeForces - 1084A The Fair Nut and Elevator 数学

    题目 The Fair Nut lives in n story house. ai people live on the i-th floor of the house. Every person ...

  9. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

    链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为 ...

最新文章

  1. Linux系统查看系统是32位还是64位方法总结 in 创新实训
  2. 微型计算机应用重点,微型计算机原理以及的应用考试重点.doc
  3. 如何设计一个良好的接口?
  4. 实现输入一个字符串,分别将大写字母、小写字母、数字、其他字符存到列表里面并且输出列表
  5. 初识 JSP---(Servlet映射 / ServletRequest / get与post区别 / 解决乱码)
  6. C#EF中,使用类似于SQL中的% 模糊查询
  7. mysql innodb4大特征_MYSQL中InnoDB特性浅谈
  8. python itertools模块_Python标准库:itertools模块
  9. 在网页中放入贴纸插画是怎样的体验?这样的UI素材,你还不收藏!
  10. kali 运行java_kali linux运行java程序
  11. PHP实现菱形与杨辉三角形【php趣味案例】
  12. 修改卡巴斯基注册表,无限试用
  13. docker配置aria2
  14. 北京大学计算机学院王锐,王锐-北京航空航天大学计算机学院
  15. 联想微型计算机B3O5,联想B3一体电脑
  16. sever串口wifi拓展板_基于串口调试助手的WIFI模块调试-FPGA简单联网(点灯)...
  17. python plot画柱状图_Python绘制柱状图
  18. Micheal Nielsen's神经网络学习之二
  19. 山中无甲子,寒尽不知年
  20. 经典的开源免费网络游戏:planeshift

热门文章

  1. 公司间交易学习笔记---概述
  2. 【小技巧】【牛客网】【JAVA】在线输入输出练习
  3. [Leedcode][第215题][JAVA][数组中的第K个最大元素][快排][优先队列]
  4. 易语言执行linux脚本,初识易语言到编写SHELL生成器
  5. 站怎么点都是一样_老鼠被卡在轮胎里,像是被点了穴道一样:这可怎么办才好?...
  6. javaweb开发的准备工作——配置篇
  7. java file 堵塞_单元测试最终在Java 6中阻塞
  8. 1003 我要通过! (20 分)
  9. php 判斷數據庫表是否存在,phpl判斷mysq數據庫中的某個數據表是否存在的方法總結...
  10. java延迟覆盖_高效Java第九条覆盖equals时总要覆盖hashCode