#P15831. [JOI Open 2015] Election Campaign

[JOI Open 2015] Election Campaign

Description

In the Republic of JOI, there are NN cities numbered from 1 to NN. The cities are connected with N1N-1 roads. People in the Republic of JOI are travelling between cities using these roads. They can pass each of the roads in both directions. They can travel between any two cities by passing through one or several roads.

Mr. IOI is a candidate for the president of the Republic of JOI. Of course, to become the president, he must perform the campaign for the presidential election. His secretary made MM plans for the election campaign. In the ii-th plan, Mr. IOI will travel from the city AiA_i to the city BiB_i passing through minimum number of roads, and make a public speech in each of the cities on the way (including the city AiA_i and the city BiB_i). Because his secretary is brilliant, it is known that Mr. IOI will get CiC_i votes if the ii-th plan is performed. It is possible to perform several plans.

However, people in the Republic of JOI are impatient. If Mr. IOI makes public speeches more than once in the same city, he will lose the support from people in the Republic of JOI.

Because Mr. IOI would like to become the president, he wants to get as many votes as possible. Since you are a superprogrammer in the Republic of JOI, he asked you to write a program which calculates the maximum number of votes Mr. IOI can get in the presidential election under the assumption that he will not make public speeches more than once in the same city.

Task

Given the number NN of cities in the Republic of JOI, the information on the roads, the number MM of plans for the election campaign, and information of each plan, write a program which calculates the maximum number of votes Mr. IOI can get in the presidential election.

Input Format

Read the following data from the standard input.

  • The first line of input contains an integer NN, the number of cities in the Republic of JOI.
  • The ii-th line (1iN11 \leq i \leq N-1) of the following N1N-1 lines contains two space separated integers Xi,YiX_i, Y_i. This means the ii-th road connects the city XiX_i and the city YiY_i.
  • The following one line contains an integer MM, the number of plans for the election campaign.
  • The ii-th line (1iM1 \leq i \leq M) of the following MM lines contains three space separated integers Ai,Bi,CiA_i, B_i, C_i. This means, in the ii-th plan, Mr. IOI will travel from the city AiA_i to the city BiB_i passing through minimum number of roads, and he will get CiC_i votes if this plan is performed.

Output Format

The output should consist of one line, and contain one integer which denotes the maximum number of votes Mr. IOI can get in the presidential election.

7
3 4
6 5
2 7
1 5
7 5
4 5
5
4 3 10
5 6 5
2 6 9
7 2 2
1 3 8
19
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
5
7 5 4
5 8 9
4 3 9
1 3 3
2 8 11
18
10
10 6
2 7
1 9
9 8
3 8
6 4
7 8
5 4
4 8
7
1 3 1
4 10 1
2 8 1
5 3 1
3 7 1
8 5 1
1 9 1
3
20
17 10
11 4
8 3
3 16
1 14
15 18
5 4
6 18
10 18
19 4
16 7
2 13
4 12
12 20
9 20
18 13
20 14
14 7
13 7
15
19 9 2341
13 8 6974
8 3 3339
15 17 6515
10 13 4370
1 7 8376
18 2 9272
6 7 4595
1 20 505
10 9 308
6 19 8937
2 15 5072
5 4 4217
2 4 4170
19 12 8204
29191

Hint

Sample 1 Explanation

In this sample input, the optimal strategy is to perform the plan 1 and the plan 3 for the election campaign.

Sample 2 Explanation

This sample input satisfies the constraints of the subtask 3.

Sample 3 Explanation

This sample input satisfies the constraints of the subtask 4.

Constraints

All input data satisfy the following conditions.

  • 2N1000002 \leq N \leq 100\,000.
  • 1XiN1 \leq X_i \leq N.
  • 1YiN1 \leq Y_i \leq N.
  • XiYiX_i \ne Y_i (1iN11 \leq i \leq N-1).
  • People can travel between any two cities by passing through one or several roads.
  • 1M1000001 \leq M \leq 100\,000.
  • 1AiN1 \leq A_i \leq N.
  • 1BiN1 \leq B_i \leq N.
  • AiBiA_i \ne B_i (1iM1 \leq i \leq M).
  • 1Ci100001 \leq C_i \leq 10\,000.

Subtask

Subtask 1 [10 points]

  • M15M \leq 15.

Subtask 2 [5 points]

  • Xi=iX_i = i (1iN11 \leq i \leq N-1).
  • Yi=i+1Y_i = i+1 (1iN11 \leq i \leq N-1).
  • Ci=1C_i = 1 (1iM1 \leq i \leq M).

Subtask 3 [5 points]

  • Xi=iX_i = i (1iN11 \leq i \leq N-1).
  • Yi=i+1Y_i = i+1 (1iN11 \leq i \leq N-1).

Subtask 4 [30 points]

  • Ci=1C_i = 1 (1iM1 \leq i \leq M).

Subtask 5 [10 points]

  • N1000N \leq 1\,000.
  • M1000M \leq 1\,000.

Subtask 6 [40 points]

There are no additional constraints.