#P6615. 草莓 Strawberry
草莓 Strawberry
Description
Given a tree , both vertices and edges have weights.
A simple path is a path that does not pass through any vertex more than once. It can be proved that the simple path between two nodes and is unique; we denote it by . The length of a path is the sum of the weights of all edges on it.
You have two sets and .
At the beginning, you must choose one node as the current node and add it to the set . Then you may perform any number of operations. One operation is as follows:
Suppose your current node is . You may choose a node such that , then update your current node to , add node to the set , and add the path to the set .
After you finish all operations, the profit you get is: the minimum of (the sum of vertex weights of all nodes in ) (the sum of lengths of all paths in ). If is empty, we consider the value of the second term to be .
Note that both and are sets, not multisets. This means that even if you add the same node to multiple times, it is counted only once when computing the vertex-weight sum.
Find the maximum possible profit.
Input Format
The first line contains an integer .
The next line contains integers , representing the vertex weights of these nodes.
The next lines each contain three integers , indicating that there is an edge of weight between and .
Output Format
Output one integer, the maximum profit.
6
1 4 2 8 5 7
1 2 3
4 1 2
1 5 1
2 3 4
3 6 1
198
Hint
This problem uses bundled testdata.
| Subtask ID | Special Property | Score | |
|---|---|---|---|
| 1 | 5 | ||
| 2 | 13 | ||
| 3 | 8 | ||
| 4 | The tree is a chain. | 13 | |
| 5 | 1 | ||
| 6 | 60 |
For all data, it is guaranteed that , , and .
Sample #1 Explanation
The graph given in the first sample is as follows.
Translated by ChatGPT 5

京公网安备 11011102002149号