Optimal Edge Weights
Practice
3 (6 votes)
Algorithms
Depth first search
Graphs
Greedy algorithms
Medium
Sorting
Trees
Problem
25% Success 1005 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There is a state (territory) which is a tree rooted at node 1. All the cities (numbered from 1 to \(N+1\)) in this state are connected via bidirectional roads. You have to add toll tax on each road. There are N roads which connect the cities of the state. You have to assign toll taxes on the roads so as to maximize the function Toll described below:

for(i=1;i<=number of cities;i++)
{
    for(j=i+1;j<=number of cities;j++)
    {
        toll+=(toll required to pay to travel from i to j)
     } 
}

You have to maximize the toll tax. Assign the roads by toll taxes from the given array A(using each value exactly once). Find the maximum toll obtained.

Input Format

First line contains N and an integer whose value is always 2.

Then N roads follow containing 2 integers u and v, denoting the cities between which the road is.

Next line contains N space separated values denoting elements of array A.

Output Format

Print the maximum toll which can be obtained.

Input Constraints

\(1 \le N \le 2 * 10^5 \)
\(1 \le A[i] \le 1000\)
\(1 \le u,v \le N+1\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
7 votes
Tags:
AlgorithmsDepth First SearchGraphsStrongly connected component
Points:30
10 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium
Points:30
8 votes
Tags:
AlgorithmsCombinatoricsDepth First SearchGraphsMedium