#P1194. 买礼物

买礼物

Description

It is Mingming’s birthday again. Mingming wants to buy BB kinds of items, and coincidentally, each of these BB items is priced at AA yuan.

However, the shop owner says there is a promotion:

If you have bought item II, then when you buy item JJ, you only need to pay KI,JK_{I,J} yuan. Moreover, KI,JK_{I,J} happens to equal KJ,IK_{J,I}.

Now Mingming wants to know the minimum amount of money he has to spend.

Input Format

The first line contains two integers, A,BA,B.

Then there are BB lines, each containing BB numbers. The II-th line and JJ-th number is KI,JK_{I,J}.

We guarantee KI,J=KJ,IK_{I,J}=K_{J,I} and KI,I=0K_{I,I}=0.

In particular, if KI,J=0K_{I,J}=0, it means there is no discount between these two items.

Note that KI,JK_{I,J} may be greater than AA.

Output Format

Output a single integer, the minimum total amount of money required.

1 1
0

1
3 3
0 2 4
2 0 2
4 2 0

7

Hint

Explanation for Sample 22.

Buy item 22 first, spending 3 yuan. Then, due to the promotion, buying items 11 and 33 each costs 2 yuan, for a total of 7 yuan.

(When multiple “discounts” apply at the same time, clever Mingming will of course not choose to pay 4 yuan for the remaining one, but choose to pay 2 yuan.)

Constraints

For 30%30\% of the testdata, 1B101\le B\le 10.

For 100%100\% of the testdata, 1B500,0A,KI,J10001\le B\le500,0\le A,K_{I,J}\le1000.

Added one more set of testdata on 2018.7.25.

Translated by ChatGPT 5