#P18107. [COCI 2026/2027 #2] 敌人 / Neprijatelji
[COCI 2026/2027 #2] 敌人 / Neprijatelji
Problem Description
A kingdom has cities, labelled to , connected by bidirectional roads in such a way that every city can be reached from every other city. City is the capital. We say that city is subordinate to city if every path from the capital to city passes through city . In particular, every city is subordinate to itself.
One knight lives in each city, and the knight from city has reputation , which may be negative. The distance between two cities is the number of roads on the path between them. Knights from two different cities are enemies if the distance between their cities is less than .

A sequence of events will take place in the kingdom, each in one of the following two forms:
1 x y– the reputation of the knight from city becomes ,2 x– a feast is held in city , to which some knights from the cities subordinate to city are invited. No two of the invited knights may be enemies. What is the largest possible sum of reputations of the invited knights?
It is allowed to invite no knights to a feast, in which case the sum of reputations is . Feasts do not change the reputations of the knights.
Input Format
The first line contains positive integers and (, ), the number of cities and the number of events.
The second line contains integers (), the initial reputations of the knights.
Each of the following lines contains two positive integers and (, ), denoting that cities and are connected by a road.
Each of the following lines contains the description of one event in one of the forms given above. It holds that and .
There is at least one event of the form 2 x.
Output Format
For each event of the form 2 x, in the order in which they are given, output in a separate line the largest possible sum of reputations of the invited knights.
4 2
5 1 1 5
1 2
2 3
3 4
2 1
2 2
10
5
5 7
-3 4 6 -2 -1
1 2
1 3
1 4
4 5
2 1
1 3 -5
2 1
1 2 -1
2 1
1 5 7
2 1
6
4
0
7
8 8
3 2 4 5 1 2 6 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
2 1
2 2
1 4 -1
2 2
2 1
1 6 10
2 3
2 1
11
5
2
9
10
12
Hint
Explanation of example 1
The cities are connected in the order . The distance between cities and is , so the knights from these cities are not enemies and both can be invited to the feast in city . Cities , and are subordinate to city , and every two knights from these cities are enemies, so only the knight from city is invited to the feast in city .
Explanation of example 2
All the feasts are held in the capital. The distance between cities and is , so the two of them cannot both be invited to the first feast, and it is best to invite only the knight from city . After his reputation becomes , it is best to invite only the knight from city . At the third feast, all knights have negative reputation, so nobody is invited. Only the knight from city is invited to the fourth feast.
Explanation of example 3
The knights from cities and are invited to the first feast, and the knight from city to the second one, for which only the knights from cities , and can be considered. After his reputation becomes , the knight from city is invited to the feast in city , and the knights from cities and , whose distance is , to the feast in city . After the reputation of the knight from city becomes , only he is invited to the feast in city , and the knights from cities and to the feast in city .
Scoring
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 18 | . |
| 2 | 36 | Cities and are connected by a road for each . |
| 3 | 37 | . |
| 4 | 19 | No additional constraints. |