## DMOPC '18 Contest 3 P4 - Bob and English Class

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Java 2.5s
Memory limit: 256M

Author:
Problem types

Bob's English class has been given a project which must be done in pairs. The class size, , is even. Each pair needs to meet outside class to do the project.

The city which Bob and his classmates live in is a tree. It has zones labelled . There are roads so that one can travel from any zone to any other. The road connects zones and and has distance .

The student lives in zone . Bob defines the cost of a pair to be the distance between the zones of the two students in the pair. Bob is wondering what the total of the costs is. Since the pairs haven't been assigned yet, he wants to know the worst-case. That is, what is the maximum possible total of the costs?

is even

for all

#### Input Specification

The first line contains two space-separated integers, and .
The next line contains space-separated integers, .
The next lines each contain three space-separated integers, , the description of road.

#### Output Specification

Output a single integer, the maximum possible total.

8 4
2 2 2 2 1 2 2 2
1 2 7
1 3 3
1 4 1

7

#### Explanation for Sample 1

The pairs of students give costs . The total is which is the maximum possible.

8 8
1 2 3 4 5 6 7 8
1 4 2
2 4 7
3 4 7
4 5 1
5 6 2
6 7 3
7 8 4

36

#### Explanation for Sample 2

The pairs of students give costs . The total is which is the maximum possible.

#### Sample Input 3

10 5
1 1 1 1 1 5 5 5 5 5
1 2 1
2 3 1
3 4 1
4 5 1

20

#### Explanation for Sample 3

The pairs of students give costs . The total is which is the maximum possible.