DMOPC '18 Contest 3 P4 - Bob and English Class

View as PDF

Submit solution


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, K, 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 N zones labelled 1,2,,N. There are N1 roads so that one can travel from any zone to any other. The ith road connects zones ai and bi and has distance di.

The ith student lives in zone zi. 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 K2 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?

Constraints

K is even
1ziN
1ai,biN
1di1000

Subtask 1 [10%]

ai=i,bi=i+1 for all i=1,2,,N1
2K200000
2N200000

Subtask 2 [20%]

2K10
2N200000

Subtask 3 [30%]

2K70
2N70

Subtask 4 [40%]

2K200000
2N200000

Input Specification

The first line contains two space-separated integers, K and N.
The next line contains K space-separated integers, z1,z2,,zK.
The next N1 lines each contain three space-separated integers, ai,bi,di, the description of ith road.

Output Specification

Output a single integer, the maximum possible total.

Sample Input 1

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

Sample Output 1

Copy
7

Explanation for Sample 1

The pairs of students (1,5),(2,6),(3,7),(4,8) give costs 0,0,7,0. The total is 7 which is the maximum possible.

Sample Input 2

Copy
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

Sample Output 2

Copy
36

Explanation for Sample 2

The pairs of students (1,8),(2,7),(3,6),(4,5) give costs 12,13,10,1. The total is 36 which is the maximum possible.

Sample Input 3

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

Sample Output 3

Copy
20

Explanation for Sample 3

The pairs of students (1,6),(2,7),(3,8),(4,9),(5,10) give costs 4,4,4,4,4. The total is 20 which is the maximum possible.


Comments

There are no comments at the moment.