题目描述

The Fair Nut is going to travel to the Tree Country, in which there are n 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 u and go by a simple path to city v. 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 wi liters of gasoline in the i-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 n (1≤n≤3⋅105) — the number of cities.

The second line contains n integers w1,w2,…,wn (0≤wi≤109) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n−1 lines describes road and contains three integers u, v, c (1≤u,v≤n, 1≤c≤109, u≠v), where u and v — cities that are connected by this road and c — 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→3.

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

题意:

就是类似于树的直径,看一下最大的路径权值,每经过一个点,这个路径的权值就会加上这个点的权重,同样的经过一条边,这个路径的权值就会减去这个边的权重,求这个树上的最大路径权值

思路:

以前咱们在求树的直径的时候就是开了两个数组 f1 和 f2 分别代表了以这个点为根节点的最大路径和次大路径,然后树的直径就是所有点的f1 + f2 取一个max,现在咱们同样也可以开两个数组,一个是最大一个是次大,这个最大的权值不就是f1 + f2 + w[i]吗,这个w[i]代表的是这个点的权重,比较容易想到,但是实现起来当时自己写的时候出了点问题,借鉴的别人的代码写的这个树形dp,发现这个代码比较清晰易懂,现在咱们就来看一下这个代码吧!

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 3e5+10,M = N*2;
typedef long long LL;
int h[N],e[M],ne[M],idx;
LL a[N],w[M];
LL f1[N],f2[N];
void add(int a,int b,int c) {e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx++;
}
LL ans;
void dfs(int u,int fa) {LL t1=0,t2=0;//t1代表的是最大,t2代表的是次大for(int i=h[u]; i!=-1; i=ne[i]) {int j = e[i];if(j == fa) continue;dfs(j,u);if(f1[j]-w[i] >= t1) {t2 = t1;t1 = f1[j] - w[i];} else if(f1[j]-w[i] > t2) t2 = f1[j]-w[i];}f1[u] = t1 + a[u];//最后发现这个次大数组好像没啥用,开了一个临时变量就能完成他的功能了ans = max(ans,f1[u]+t2);//这个就相当于树的直径了
}
int main() {int n;while(~scanf("%d",&n)) {memset(h,-1,sizeof h);idx=0;ans = 0;memset(f1,0,sizeof f1);memset(f2,0,sizeof f2);for(int i=1; i<=n; i++) scanf("%lld",&a[i]);for(int i=1; i<n; i++) {int a,b,c;scanf("%d%d%d",&a,&b,&c);add(a,b,c);add(b,a,c);}dfs(1,-1);//其实这个地方从哪个点开始都一样cout<<ans<<endl;}return 0;
}

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

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

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

  2. Codeforces 1084A - The Fair Nut and Elevator

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

  3. 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 题意: 在一棵树内找一条路径,使得从起点 ...

  4. CF1083A The Fair Nut and the Best Path

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

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

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

  6. 【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 nn cities. Most of the l ...

  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. oracle imp dmp
  2. 山东省青岛市黄海学院计算机考试,2017年3月山东计算机等级考试考点联系方式...
  3. BugKuCTF 加密 这不是摩斯密码
  4. HBase 0.94.21 zookeeper-3.4.6 分布式安装
  5. JQuery AJAX 加载 HTML代码“lt”形式的。怎么解析成形式,并且把img解析成图片输出到浏览器中。...
  6. 哪里可以学3D次世代角色建模?具体学什么东西?
  7. 不同分辨率图片匹配_超实用的图像超分辨率重建方法及应用介绍
  8. 数据库工作笔记018---Windows下mysql安装_服务无法启动没有报告解决
  9. 嵌入式操作系统内核原理和开发(中断)
  10. TFS小记(3):建立团队项目
  11. 基于springboot的高校档案系统
  12. SQL Server【获取当前时间】
  13. 计算机设备自动关机,怎样设置电脑自动关机时间,电脑设置自动关机时间-
  14. postgres关键字、常量和数据类型
  15. LocalDate 获取英文星期
  16. 程序员代码面试指南刷题--第五章.翻转字符串(1)
  17. The Shawshank Redemption-10
  18. 计算机上的be无法正常启动,应用程序无法正常启动0xc0000005的三种解决方法
  19. 中乾山东最专业众筹系统
  20. 如何使用纯JS过掉淘宝滑块

热门文章

  1. 最好的爱情,是彼此觉得高攀了对方
  2. opencv [c++] OpenCV实现Halcon相关算子算法
  3. while循环和for循环
  4. 攻防世界Web新shou区-command_execution
  5. html可以编写爬虫吗,JavaScript能写爬虫吗?
  6. 美团小程序框架 mpvue mvvm框架互通
  7. Understanding Camera exposure
  8. SMC - 状态机代码生成工具
  9. # 什么是Tproxy透明代理
  10. Dell G3电脑配置(增加ssd,ubuntu双系统等)