#P10949. 四叶草魔杖

四叶草魔杖

Problem Description

Freda, the guardian of the wand, merged four weapons. Then, at the top of the wand, a four-leaf clover slowly grew, and its four leaves shimmered with faint seven-colored light.

Rainbow, the guardian of the holy sword, took out a disk. On the disk, there are NN gems, numbered from 00 to N1N-1. The energy of the ii-th gem is AiA_i.

If Ai>0A_i > 0, it means the energy of this gem is too high, and it needs to transfer AiA_i units of energy to other gems. If Ai<0A_i < 0, it means the energy of this gem is too low, and it needs to obtain Ai-A_i units of energy from other gems.

It is guaranteed that Ai=0\sum A_i = 0.

Only when all gems have the same energy can you insert the Clover Wand into the center of the disk and open the passage to the supernatural world.

However, energy can be transferred only between MM pairs of gems. For the ii-th pair, no matter how much energy is transferred, it costs TiT_i.

The explorers want to know the minimum total cost needed to make the energy of all gems equal.

Input Format

The first line contains two integers N,MN, M.

The second line contains NN integers AiA_i.

The next MM lines each contain three integers pi,qi,Tip_i, q_i, T_i, meaning that transferring energy between the gems numbered pip_i and qiq_i costs TiT_i.

The testdata guarantees that each pair pi,qip_i, q_i appears at most once.

Output Format

Output one integer representing the answer. If there is no solution, output Impossible.

3 3
50 -20 -30
0 1 10
1 2 20
0 2 100
30

Hint

2N162 \le N \le 16,
0MN(N1)20 \le M \le \dfrac{N(N-1)}{2},
0pi,qi<N0 \le p_i,q_i < N,
1000Ai1000-1000 \le A_i \le 1000,
0Ti10000 \le T_i \le 1000

Translated by ChatGPT 5