A regional telecom operator runs n relay stations, numbered 0 to n-1, wired together by m direct communication links. Before the network goes live, engineers must assign each station a distinct priority level, using every integer from 1 to n exactly once. The signal strength carried on a link is defined as the sum of the priority levels of the two stations it connects. Determine the assignment of priority levels that maximizes the total signal strength summed across every link.
Print a single integer: the maximum possible total signal strength across all links, over every valid assignment of priority levels 1..n to the stations.
Example 1
Input
5 4 0 1 0 2 0 3 3 4
Expected
29
Explanation
Station 0 has degree 3 (links to 1, 2, and 3), so it should receive the highest priority, 5. Station 3 has degree 2 (links to 0 and 4), so it receives the next-highest priority, 4. The remaining priorities {1,2,3} go to stations 1, 2, and 4 (each degree 1) in any order, since each appears in exactly one link. With station0=5, station3=4, station1=3, station2=2, station4=1, the links contribute 5+3=8, 5+2=7, 5+4=9, and 4+1=5, for a total of 8+7+9+5=29, which is optimal.
Example 2
Input
4 4 0 1 1 2 2 0 2 3
Expected
23
Explanation
Station 2 has the highest degree (3, touching stations 0, 1, and 3), so it gets the top priority, 4. Stations 0 and 1 each have degree 2 (both touch each other and station 2), so they split the next two priorities, 3 and 2, in either order. Station 3, with degree 1, gets the lowest priority, 1. With station2=4, station0=3, station1=2, station3=1, the links contribute 3+2=5 (0-1), 2+4=6 (1-2), 4+3=7 (2-0), and 4+1=5 (2-3), totaling 23, which is optimal.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →