#arc129e. [arc129_e]Yet Another Minimization

[arc129_e]Yet Another Minimization

Problem Statement

Snuke is making an integer sequence of length NN: x=(x1,x2,cdots,xN)x=(x_1,x_2,\\cdots,x_N). For each ii (1leqileqN1 \\leq i \\leq N), there are MM candidates for the value of xix_i: the kk-th of them is Ai,kA_{i,k}. Choosing Ai,kA_{i,k} incurs a cost of Ci,kC_{i,k}.

Additionally, after deciding xx, a cost of xixjtimesWi,j|x_i-x_j| \\times W_{i,j} is incurred for each i,ji,j (1leqi<jleqN1 \\leq i < j \\leq N).

Find the minimum possible total cost incurred.

Constraints

  • 2leqNleq502 \\leq N \\leq 50
  • 2leqMleq52 \\leq M \\leq 5
  • $1 \\leq A_{i,1} < A_{i,2} < \\cdots < A_{i,M} \\leq 10^6$
  • 1leqCi,kleq10151 \\leq C_{i,k} \\leq 10^{15}
  • 1leqWi,jleq1061 \\leq W_{i,j} \\leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM A1,1A_{1,1} C1,1C_{1,1} A1,2A_{1,2} C1,2C_{1,2} vdots\\vdots A1,MA_{1,M} C1,MC_{1,M} A2,1A_{2,1} C2,1C_{2,1} A2,2A_{2,2} C2,2C_{2,2} vdots\\vdots A2,MA_{2,M} C2,MC_{2,M} vdots\\vdots AN,1A_{N,1} CN,1C_{N,1} AN,2A_{N,2} CN,2C_{N,2} vdots\\vdots AN,MA_{N,M} CN,MC_{N,M} W1,2W_{1,2} W1,3W_{1,3} cdots\\cdots W1,N1W_{1,N-1} W1,NW_{1,N} W2,3W_{2,3} W2,4W_{2,4} cdots\\cdots W2,NW_{2,N} vdots\\vdots WN1,NW_{N-1,N}

Output

Print the answer.


Sample Input 1

3 2
1 1
5 2
2 3
9 4
7 2
8 2
1 5
3

Sample Output 1

28

An optimal choice is x=(5,9,7)x=(5,9,7). The individual costs incurred here are as follows.

  • Choosing A1,2A_{1,2} for x1x_1 incurs a cost of C1,2=2C_{1,2}=2.
  • Choosing A2,2A_{2,2} for x2x_2 incurs a cost of C2,2=4C_{2,2}=4.
  • Choosing A3,1A_{3,1} for x3x_3 incurs a cost of C3,1=2C_{3,1}=2..
  • For (i,j)=(1,2)(i,j)=(1,2), a cost of xixjtimesWi,j=4|x_i-x_j| \\times W_{i,j}=4 is incurred.
  • For (i,j)=(1,3)(i,j)=(1,3), a cost of xixjtimesWi,j=10|x_i-x_j| \\times W_{i,j}=10 is incurred.
  • For (i,j)=(2,3)(i,j)=(2,3), a cost of xixjtimesWi,j=6|x_i-x_j| \\times W_{i,j}=6 is incurred.

Sample Input 2

10 3
19 2517
38 785
43 3611
3 681
20 758
45 4745
6 913
7 2212
22 536
4 685
27 148
36 2283
25 3304
36 1855
43 2747
11 1976
32 4973
43 3964
3 4242
16 4750
50 24
4 4231
22 1526
31 2152
15 2888
28 2249
49 2208
31 3127
40 3221
47 4671
24 6 16 47 42 50 35 43 47
29 18 28 24 27 25 33 12
5 43 20 9 39 46 30
40 24 34 5 30 21
50 6 21 36 5
50 16 13 13
2 40 15
25 48
20

Sample Output 2

27790

Sample Input 3

2 2
1 1
10 10
1 1
10 10
100

Sample Output 3

2