Under construction

Still being built. Things may change

Tourism business

Time limit5sMemory limit1024 MB

Summary
Given a weighted tree and two city sets A and B with populations, find the maximum of (C_X + C_Y) times the path distance over all X in A and Y in B.
Level

Hard8 of 10

Topics
Tree, Divide and conquer, Dynamic programming
Solved
No attempts yet

Problem

Octorabbit Kingdom has NN cities connected by N−1N-1 roads. From any city, you can reach any other city using only roads, so the country forms a tree. Each road has a positive integer length. The distance between two cities is the sum of the road lengths on the simple path that connects them.

For a new tourism project, the country picks two cities XX and YY and makes them tourism sister cities. Once paired, people from each city travel to visit the other. Let D(X,Y)D(X,Y) be the distance between XX and YY, and let CXC_X and CYC_Y be the populations living in XX and YY. The transport fee income is (CX+CY)×D(X,Y)(C_X+C_Y) \times D(X,Y).

The country is going through upheaval, so city populations keep changing, and the cities that want to join vary from situation to situation. The country needs to find the right pairs for many different situations.

There are QQ sister city plans. Each plan gives a set AA of candidate cities for XX and a set BB of candidate cities for YY, along with the population CuC_u of each candidate city. No city is in both AA and BB.

For each plan, find the maximum transport fee income that can be obtained by choosing XX and YY.

Input

The first line contains NN and QQ, separated by a space. (1≤N≤300 0001 \leq N \leq 300\ 000, 1≤Q≤100 0001 \leq Q \leq 100\ 000)

The next N−1N-1 lines each contain uu, vv, and dd, separated by spaces. The ii-th road connects city uu and city vv, and has length dd. (1≤u,v≤N1 \leq u,v \leq N, 1≤d≤301 \leq d \leq 30)

Then the QQ plans follow in this format.

  • The first line contains NAN_A and NBN_B, separated by a space. NAN_A is the size of AA, and NBN_B is the size of BB. (1≤NA,NB,NA+NB≤N1 \leq N_A,N_B, N_A+N_B \leq N)
  • The next NAN_A lines each contain uu and pp, separated by a space. City uu belongs to set AA and has a population of pp, that is, Cu=pC_u=p. (1≤u≤N1 \leq u \leq N, 1≤p≤3×1061 \leq p \leq 3 \times 10^6)
  • The next NBN_B lines each contain vv and qq, separated by a space. City vv belongs to set BB and has a population of qq, that is, Cv=qC_v=q. (1≤v≤N1 \leq v \leq N, 1≤q≤3×1061 \leq q \leq 3 \times 10^6)

The sum ∑i=1Q(NA+NB)\sum_{i=1}^{Q} (N_A+N_B) is at most 200 000200\ 000.

Output

Print the maximum transport fee income for each sister city plan on its own line, in order.

Hint

When X=3X=3 and Y=7Y=7, the transport fee income is (5+1)×17=102(5+1) \times 17=102, which is optimal. X=1X=1 and Y=5Y=5 is also optimal.

Examples2

  1. Example 1

    Input
    3 3
    1 2 1
    2 3 1
    1 2
    1 1
    2 2
    3 3
    1 2
    2 2
    1 1
    3 3
    1 2
    3 3
    1 1
    2 2
    
    Expected output
    8
    5
    8
    
  2. Example 2

    Input
    7 1
    1 2 10
    3 4 1
    4 5 1
    2 4 6
    4 6 6
    6 7 10
    3 3
    1 1
    2 3
    3 5
    5 5
    6 3
    7 1
    
    Expected output
    102