欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1324F.Maximum White Subtree
Maximum White Subtree
You are given a tree consisting of vertices. A tree is a connected undirected graph with edges. Each vertex of this tree has a color assigned to it ( if the vertex is white and if the vertex is black).
You have to solve the following problem for each vertex : what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex ? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains white vertices and black vertices, you have to maximize .
Input
The first line of the input contains one integer () — the number of vertices in the tree.
The second line of the input contains integers (), where is the color of the -th vertex.
Each of the next lines describes an edge of the tree. Edge is denoted by two integers and , the labels of vertices it connects ).
It is guaranteed that the given edges form a tree.
Output
Print integers , where is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex .
Note
The first example is shown below:

The black vertices have bold borders.
In the second example, the best subtree for vertices and are vertices and correspondingly. And the best subtree for the vertex is the subtree consisting of vertices and .
Samples
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9
2 2 2 2 2 1 1 0 2
4
0 0 1 0
1 2
1 3
1 4
0 -1 1 -1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |