#P5647. ygg发神威

    ID: 4571 远端评测题 2000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树形结构线段树二分平衡树O2优化枚举,暴力排序

ygg发神威

Description

There are nn computers in ygg's computer room, and all of them are in use. To reduce the cost of computers in the room, the ii-th computer is used by aia_i newbies at the same time. Each computer has an "online multi-user communication platform" installed. Through this platform, every computer is directly or indirectly connected to all other computers. Since the platform is still in the testing stage, if there is more than one different way to transmit a message from one computer to any other computer, various strange problems will occur. Two message transmission methods are considered different if and only if the set of directly connected links used during transmission is different, or the set of computers passed through is different. Of course, a message transmission will never pass through the same link more than once. To avoid this, the platform links are specially designed so that the transmission route between any two computers is unique. Also, to prevent a computer from being overloaded, each computer can be connected to at most pp computers via platform links.

Originally, two computers connected by a platform link could transmit data to each other. But ygg found that this would allow newbies using the two computers to send messages to each other, causing large-scale exam cheating worshipping ygg. So he showed his power and cut off half of all links. Specifically, he changed each original bidirectional link between two computers into a unidirectional link. That is, between two computers originally connected by one bidirectional link, only one computer can send messages to the other, and the other cannot send any message back.

At every moment, the newbies in the computer room have some messages to transmit. If computer ii can directly or indirectly reach computer jj through the platform links, then any of the aia_i newbies using computer ii can send messages to the aja_j newbies using computer jj. Clearly, messages between newbies using the same computer can be delivered offline and do not need the platform, so they are not counted as online messages.

In fact, the newbies already know the administrator password of the platform, so they can change the directions of the links. However, no matter how they try, they can no longer restore the original bidirectional state. The newbies of course hope to send as many messages as possible, so they want to know: when the computers are connected only by directed links, what is the maximum number of messages that can be sent through the platform at any moment.

To simplify the problem, assume that all newbies can send online messages at the same time, and each newbie can send messages to multiple people simultaneously.

Brief statement: Given a tree with nodes numbered 1n1\sim n, node weights aia_i, and maximum node degree pp. After changing every edge of the tree into a directed edge, find the maximum value of the following expression:

$$\sum_{i=1}^{n}\sum_{j=1}^{n}a_ia_j\left[i\rightarrow j\right]$$

[ij]\left[i\rightarrow j\right] is defined as follows: if the node numbered ii can reach the node numbered jj through directed edges, then it equals 11; otherwise it equals 00, and [ii]=0\left[i\rightarrow i\right]=0.

Input Format

The input has (n+1)(n+1) lines.

The first line contains two positive integers nn and pp, representing the number of computers in the computer room and the maximum load limit of all computers.
The second line contains nn positive integers. The ii-th integer aia_i is the number of newbies using the ii-th computer.
The next (n1)(n-1) lines each contain two integers ui,viu_i,v_i, indicating that there was originally a bidirectional link of the "online multi-user communication platform" between computer uiu_i and computer viv_i.

Output Format

Output one line with one integer, representing the maximum number of messages that can be sent at a moment.

4 2
1 2 3 4
1 2
3 2
4 1

35

Hint

Sample Explanation

When the number of messages that can be sent is maximized, the directions of message transmission are as follows:

At the moment when this connection method sends the most messages: the 11 newbie using computer 11 sends one message to each of 55 newbies, for a total of 1×5=51\times5=5 messages;
the 22 newbies using computer 22 each send one message to each of 33 newbies, for a total of 2×3=62\times3=6 messages;
the 33 newbies using computer 33 cannot send any online messages, so no messages are sent from this computer;
the 44 newbies using computer 44 each send one message to each of 66 newbies, for a total of 4×6=244\times6=24 messages.
Therefore, at a certain moment, at most 5+6+24=355+6+24=35 online messages can be sent.
You can verify by enumeration that there is no directed connection method that can make the number of online messages sent at a moment reach 3636 or more.

Constraints and Notes

For 10%10\% of the data, n10n\le10.
For another 10%10\% of the data, n=p+1n=p+1.
For another 10%10\% of the data, p=2p=2.
For another 10%10\% of the data, p20p\le20.
For another 10%10\% of the data, p40p\le40.

For all data, 2n1052\le n\le10^5, 2p502\le p\le50, 1ai1001\le a_i\le100, 1ui,vin1\le u_i,v_i\le n. It is guaranteed that the given bidirectional links make the message transmission route between any two computers unique. It is guaranteed that at least one computer originally has exactly pp platform links, and no computer originally has more than pp platform links.

Translated by ChatGPT 5