Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

Description

The Queen of Byteland is very loved by her people. In order to show her their love, the Bytelanders have decided to conquer a new country which will be named according to the queen's name. This new country contains N towns. The towns are connected by bidirectional roads and there is exactly ONE path between any two towns, walking on the country's roads. For each town, the profit it brings to the owner is known. Although the Bytelanders love their queen very much, they don't want to conquer all the N towns for her. They will be satisfied with a non-empty subset of these towns, with the following 2 properties: there exists a path from every town in the subset to every other town in the subset walking only through towns in the subset and the profit of the subset is maximum. The profit of a subset of the N towns is equal to the sum of the profits of the towns which belong to the subset. Your task is to find the maximum profit the Bytelanders may get.

Input

The first line of input will contain the number of towns N (1<=N<=16 000). The second line will contain N integers: the profits for each town, from 1 to N. Each profit is an integer number between -1000 and 1000. The next N-1 lines describe the roads: each line contains 2integer numbers a and b, separated by blanks, denoting two different towns between which there exists a road.

Output

The output should contain one integer number: the maximum profit the Bytelanders may get.

Sample Input

5
-1 1 3 1 -1
4 1
1 3
1 2
4 5

Sample Output

4

Author : Mugurel Ionut Andreica
Resource : SSU::Online Contester Fall Contest #2
Date : Fall 2002
 
 
 
 
/*/
题意:
有N个村庄,每个村庄有一个权值,有n-1条路,将村庄连起来,然后选取这些路中的一个联通图,权值最大。 整个图都被联通,找其中权值最大的子联通块。 树状DP。 代码风格学了某个学长的写了个结构体,真刺激。。
AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n"
#define bigfor(x) for(LL qq=1;qq<= T ;qq++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int MX = 16666; struct Treedp {
struct Edge {
int v,nxt;
} E[MX<<1]; int Head[MX],erear;
bool vis[MX];
int dp[MX];
int INF=-1e9-1e5; void init() {
erear=0;
memset(E,0);
memset(vis,0);
memset(Head,-1);
} void add(int u,int v) {
E[erear].v=v;
E[erear].nxt=Head[u];
Head[u]=erear++;
} int run(int u) {
vis[u]=1;
for(int i=Head[u]; ~i; i=E[i].nxt) {
int v=E[i].v;
if(!vis[v]) {
dp[u]+=max(0,run(v));
}
}
return dp[u];
} void print(int n) {
for(int i=1; i<=n; i++)
cout<<dp[i]<<" ";
puts("");
}
}; Treedp tdp; int main() {
int n,l,r;
scanf("%d",&n);
tdp.init();
for(int i=1; i<=n; i++) {
scanf("%d",&tdp.dp[i]);
}
for(int i=1; i<n; i++) {
scanf("%d%d",&l,&r);
tdp.add(l,r);
tdp.add(r,l);
}
int maxx=-1e9-1000000;
tdp.run(1);
for(int i=1; i<=n; i++) {
maxx=max(maxx,tdp.dp[i]);
}
// tdp.print(n);
printf("%d\n",maxx);
return 0;
}

  

 

最新文章

  1. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
  2. JAVA实现 springMVC方式的微信接入、实现消息自动回复
  3. iOS——CALayer的shadow无效问题
  4. 51Node 1483----化学变换(暴力枚举)
  5. NoSuchMethodException问题总结
  6. oracle 日志学习(转载)
  7. jQuery获取鼠标移动方向
  8. UTF-8 GBK UTF8 GB2312 之间的区别和关系
  9. 个人总结-Alpha阶段
  10. 原生js写的flybird小游戏
  11. ☆ [WC2006] 水管局长 「LCT动态维护最小生成树」
  12. empty和isset区别
  13. Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)
  14. go标准库的学习-runtime
  15. .io域名在申请SSL证书时被坑
  16. [spark 快速大数据分析读书笔记] 第一章 导论
  17. 如何判断Map中的key或value是什么类型
  18. TestNG入门到...
  19. wepy - 使用vsCode编辑器安装插件
  20. .htaccess技巧: URL重写(Rewrite)与重定向(Redirect)

热门文章

  1. iptables下state的4种形式
  2. jquery.query.js 插件(示例及简单应用)
  3. SQL索引及视图常用语法
  4. hdu 1698:Just a Hook(线段树,区间更新)
  5. 配置ogg异构oracle-mysql 双向同步注意事项
  6. 使用Jmeter进行http接口性能测试
  7. ARM伪指令,王明学learn
  8. orientation和gravity的区别
  9. supervisor(三)xml_rpc
  10. 【转】最近搞Hadoop集群迁移踩的坑杂记