【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 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
*/

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440621.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python不同曲线设置标签_【图像分类】基于Pascal VOC2012增强数据的多标签图像分类实战...

接着上一次的多标签分类综述&#xff0c;本文主要以Pascal VOC2012增强数据集进行多标签图像分类训练&#xff0c;详细介绍增强数据集制作、训练以及指标计算过程&#xff0c;并通过代码进行详细阐述&#xff0c;希望能为大家提供一定的帮助&#xff01;作者&编辑 | 郭冰洋…

表字段注释放在哪_橱柜台面6种材质优缺点大解剖,我来测评,告诉你哪一种适合你!...

很多人装修厨房&#xff0c;只知道橱柜要好用&#xff0c;厨房才好用。但在做橱柜的时候&#xff0c;除了橱柜的选择&#xff0c;很多装修新手还会遇到一些其他疑问&#xff1a;① 橱柜的台面用什么材质的比较好&#xff1f;② 台面的挡水条到底要不要做&#xff1f;③ 橱柜台面…

【Kattis - triangle 】Sierpiński Circumference(数学,求位数,取对数或Java)

题干&#xff1a; Polish mathematician Wacław Sierpiński (1882-1969) described the 2D geometric figure known as the Sierpiński triangle as part of his work on set theory in 1915. The triangle, which is really an infinite collection of points, can be con…

米莱狄的机器人是_王者荣耀2.22更新:米莱狄机器人化身超级兵,狂铁将成T1级战士...

王者荣耀体验服在刚刚迎来了一次小更新&#xff0c;主要针对两位海都英雄的调整&#xff0c;这也是米莱狄和狂铁自上回加强后获得的第二波BUFF&#xff0c;一起来看看这次调整能否让这两位冷门英雄成为上分首选吧&#xff01;狂铁在上回的调整中移除了大招护盾伤害和汲取能量的…

如何云服务器销售,如何云服务器销售

如何云服务器销售 内容精选换一换云耀云服务器(Halo Elastic Cloud Server&#xff0c;HECS)是可以快速搭建简单应用的新一代云服务器&#xff0c;具备独立、完整的操作系统和网络功能。提供快速地应用部署和简易的管理能力&#xff0c;适用于网站搭建、开发环境等低负载应用场…

中原标准时间对时_GPS对时系统(时间同步系统)应用电子政务

GPS对时系统&#xff08;时间同步系统&#xff09;应用电子政务GPS对时系统&#xff08;时间同步系统&#xff09;应用电子政务摘要&#xff1a;随着电子政务的不断发展&#xff0c;许多省份都建立了自己的政务网络&#xff0c;使用的网络设备和服务器日益增多&#xff0c;这些…

【牛客 - 272C】Balls(概率dp,结论,思维)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/272/C 来源&#xff1a;牛客网 有一个盒子&#xff0c;里面有一个黑球和一个白球。每次随机取出盒子中的一个球&#xff0c;并将两个与取出球同色的球放进盒子&#xff08;就是随机一种颜色将其个数…

邮件发送类_10 分钟实现 Spring Boot 发生邮件功能

基础知识什么是SMTP&#xff1f;什么是IMAP&#xff1f;什么是POP3&#xff1f;IMAP和POP3协议有什么不同呢&#xff1f;进阶知识什么是JavaMailSender和JavaMailSenderImpl&#xff1f;如何通过JavaMailSenderImpl发送邮件&#xff1f;为什么JavaMailSenderImpl 能够开箱即用 …

mess系统可以读取opc服务器,C3. Messages

数据字段!Time Data Data Data Data Data Data Data Data!Time Data Data Data Data Data Data Data Data!Time Data Data Data Data Data Data Data Data!Time Data Data Data Data Data Data Data Data……………………….以下是数据文件的示例。Fig. C 2.11 Sample Configur…

【牛客 - 272D】Where are you(Tarjan求桥)

题干&#xff1a; 小p和他的朋友约定好去游乐场游玩&#xff0c;但是他们到了游乐场后却互相找不到对方了。 游乐场可以看做是一张n个点&#xff0c;m条道路的图&#xff0c;每条道路有边权wi&#xff0c;表示第一次经过该道路时的花费&#xff08;第二次及以后经过时花费为…

sharding分表后主键_Sharding-JDBC 使用入门和基本配置

一、什么是Sharding-JDBCSharding-JDBC定位为轻量级Java框架&#xff0c;在Java的JDBC层提供的额外服务。它使用客户端直连数据库&#xff0c;以jar包形式提供服务&#xff0c;无需额外部署和依赖&#xff0c;可理解为增强版的JDBC驱动&#xff0c;完全兼容JDBC和各种ORM框架。…

操作表格_Excel表格基础操作-新手入门级

今日新学了几个基础操作&#xff0c;觉得很有意思&#xff0c;分享给大家。不过真心觉得&#xff0c;有些操作当时学会了&#xff0c;长时间不用&#xff0c;转眼就忘了&#xff1b;另一方面&#xff0c;如果购买相关课程&#xff0c;学着也没什么意思&#xff0c;我前段时间买…

dmsetup remove_all 这命令干啥的_分一个小知识,服务器上的一个解压与压缩文件的命令....

在服务器上对文件做解压操作,是一件非常常见的操作,如安装软件就有很多是要用的到这个操作的.去官网下载一个压缩包,然后解压,编译,再安装.当然网上是能找到相应的命令操作.但是每次都要去找命令,就算记住了,不知道为什么要这样去使用,那么如果不经常使用那还是会忘记的.所以要…

倍福 在 vs 里 编程 是怎么做到的_截图里的文字要改,字体怎么做到一模一样?...

大家是不是碰到过这么一个尴尬的情况&#xff1a;一个截图里有一个错别字&#xff0c;或者其中一小段文字要修改&#xff0c;可是苦于找不到源文件&#xff0c;又没办法找出和原来文字一模一样的字体。每次遇到这样的情况你都是怎么处理的呢&#xff1f;火箭君今天给大家介绍一…

*【POJ - 3659】Cell Phone Network (树形dp,最小支配集)

题干&#xff1a; Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so the…

【POJ - 2632】Crashing Robots(模拟)

题干&#xff1a; In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occup…

pandownload用户未登录_Pandownload再度复活,下载速度飙升到10MB/s以上

PanDownload再度复活版&#xff0c;此版镜像服务器由新城旧梦维护卢本伟修改版&#xff0c;基本都是维持在10M/S的下载速度&#xff0c;如果你的宽带够大&#xff0c;下载速度会更快&#xff0c;无需安装即可免费使用&#xff0c;但是需要登陆才能下载。(Pandownload卢本伟修改…

momentjs转换格式_Moment.js+Vue过滤器的使用,各种时间格式转换为YYYY-MM-DD HH:mm:ss格式...

前言这篇文章将Moment.js与vue过滤器连用。如果不会过滤器的朋友&#xff0c;可以先看这篇文章vue过滤器一、Moment.js是什么&#xff1f;Moment.js是JavaScript 日期处理类库。使用场景&#xff1a;vue项目中经常需要将时间戳转换为各种时间格式再显示。二、使用步骤1.安装这里…

【HDU - 1943】Ball bearings(几何问题)

题干&#xff1a; The Swedish company SKF makes ball bearings. As explained by Britannica Online, a ball bearing is “one of the two types of rolling, or anti friction, bearings (the other is the roller bearing). Its function is to connect two machine mem…

mysql显示修改密码_MySQL修改密码

第一种方式&#xff1a;最简单的方法就是借助第三方工具Navicat for MySQL来修改&#xff0c;方法如下&#xff1a;1、登录mysql到指定库&#xff0c;如&#xff1a;登录到test库。2、然后点击上方“用户”按钮。3、选择要更改的用户名&#xff0c;然后点击上方的“编辑用户”按…