QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#22352. Cheering up the Cows

Statistics

Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths that currently provide a way to visit each of his $N$ $(5 \le N \le 10,000)$ pastures (conveniently numbered $1,\ldots,N$). Each and every pasture is home to one cow. FJ plans to remove as many of the $P$ $(N-1 \le P \le 100,000)$ paths as possible while keeping the pastures connected. You must determine which $N$-1 paths to keep.

Bidirectional path j connects pastures $S_{j}$ and $E_{j}$ $(1 \le S_{j} \le N; 1 \le E_{j} \le N; S_{j} \ne E_{j})$ and requires $L_{j}$ $(0 \le L_{j} \le 1,000)$ time to traverse. No pair of pastures is directly connected by more than one path.

The cows are sad that their transportation system is being reduced. You must visit each cow at least once every day to cheer her up. Every time you visit pasture $i$ (even if you're just traveling through), you must talk to the cow for time $C_{i}$ $(1 \le C_{i} \le 1,000)$.

You will spend each night in the same pasture (which you will choose) until the cows have recovered from their sadness. You will end up talking to the cow in the sleeping pasture at least in the morning when you wake up and in the evening after you have returned to sleep.

Assuming that Farmer John follows your suggestions of which paths to keep and you pick the optimal pasture to sleep in, determine the minimal amount of time it will take you to visit each cow at least once in a day.

For your first 10 submissions, you will be provided with the results of running your program on a part of the actual test data.

Input

  • Line 1: Two space-separated integers: $N$ and $P$
  • Lines $2,\ldots,N+1$: Line $i+1$ contains a single integer: $C_{i}$
  • Lines $N+2,\ldots,N+P+1$: Line $N$+j+1 contains three space-separated integers: $S_{j}$, $E_{j}$, and $L_{j}$

Output

  • Line 1: A single integer, the total time it takes to visit all the cows (including the two visits to the cow in your sleeping-pasture)

Examples

Input 1

5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6
4 5 12

Output 1

176
              +-(15)-+
             /        \
            /          \
     1-(5)-2-(5)-3-(6)--5
            \   /(17)  /
         (12)\ /      /(12)
              4------+

Keep these paths:

     1-(5)-2-(5)-3      5
            \          /
         (12)\        /(12)
             *4------+

Wake up in pasture 4 and visit pastures in the order 4, 5, 4, 2, 3, 2, 1, 2, 4 yielding a total time of 176 before going back to sleep.

Scoring

POINTS: 300

Problem credits: David Benjamin and Jacob Steinhardt, 2008

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.