Monday, February 21, 2022

Tangling Tree

 You are given an undirected tree of 

N nodes. A positive weight Wi is assigned to the node i for all 1iN.

For all 1jN1, the j-th edge connects the nodes uj and vj, and has a restriction value of Rj.

An array A1,A2,,AN consisting of N non-negative integers is called valid if:

  • For all 1jN1, the condition Auj+AvjRj holds.

The profit of a valid array A is defined as

profit(A)=i=1NAiWi

Find the maximum possible value of profit(A) for some valid array A.

Input Format

  • The first line of input contains a single integer T - the number of test cases. The description of T test cases follow.
  • The first line of each test case contains a single integer N - the number of nodes in the tree.
  • The second line of each test case contains N integers W1,W2,,WN denoting the weights assigned to every node.
  • The next N1 lines contain three space-separated integers each, with the j-th line containing ujvj, and Rj.

Output Format

For each test case, output the maximum possible value of profit(A).

Constraints

  • 1T105
  • 3N105
  • 1Wi109
  • 1uj,vjN and ujvj
  • 1Rj109
  • It is guaranteed that Nmaxi=1NWimaxj=1N1Rj1018.
  • It is guaranteed that the input forms a valid tree for all test cases.
  • The sum of N over all test cases do not exceed 105

Sample Input 1 

3
3
6 9 4
1 2 2
2 3 1
6
1 2 2 6 100 100
1 2 9
2 3 17
2 4 3
3 5 1
3 6 4
3
120734269 1000000000 1
1 2 300000000
2 3 300000000

Sample Output 1 

16
527
300000000000000000

Explanation

Test case-1: All valid arrays A are:

  • A=[0,0,0]profit(A)=06+09+04=0
  • A=[0,0,1]profit(A)=06+09+14=4
  • A=[0,1,0]profit(A)=06+19+04=9
  • A=[1,0,1]profit(A)=16+09+14=10
  • A=[1,1,0]profit(A)=16+19+04=15
  • A=[2,0,1]profit(A)=26+09+14=16

The maximum profit among all valid arrays A is 16, which is the answer.

Test case-2: The optimal A is [9,0,0,3,1,4].

Test case-3: The optimal A is [0,300000000,0].

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...