Strange Graph

아직 제출이 없습니다시간 제한7초메모리 제한1024 MB

문제

You are given a two-dimensional array of integers of size N×KN \times K, AA == A_0,0A\_{0,0}, A_0,1A\_{0,1}, \dots, A_0,K1A\_{0,K-1}, A_1,0A\_{1,0}, \dots, A_N1,K1A\_{N-1,K-1} and also arrays of integers of size MM, UU == U_0U\_{0}, \dots, U_M1U\_{M-1} and VV == V_0V\_0, \dots, V_M1V\_{M-1}.

Jimin made a cute weighted undirected graph GG, which is a complete graph with the weight of an edge connecting vertices uu and vv is A_u,(v,mod,K)A_v,(u,mod,K)\left\lvert A\_{u, (v \\, \bmod \\, K)} - A\_{v, (u \\, \bmod \\, K)} \right\rvert. Eunsoo then found the minimum spanning tree of GG.

However, Jongyoung brutally deleted edges of GG connecting U_iU\_i and V_iV\_i for 0iM10 \le i \le M-1. Note that GG may not be connected after deleting the edges.

Now, to help poor Jimin and Eunsoo, you should find the minimum spanning forest of GG. A minimum spanning forest is a union of the minimum spanning trees of its connected components.

입력

The first line contains three space-separated integers, NN, KK, and MM.

Each of the following NN lines contains KK space-separated integers, A_i,0,A\_{i,0}, ,\dots, A_i,K1A\_{i,K-1}. (0iN1)(0 \le i \le N-1)

Each of the following MM lines contains two space-separated integers, U_iU\_i and V_iV\_i. (0iM1)(0 \le i \le M-1)

출력

Output the sum of the weight of edges in the minimum spanning forest of GG.

제한

  • 1NK300,0001 \le NK \le 300\\,000
  • 1KN1 \le K \le N
  • 0Mmin(N(N1)2, 300,000)0 \le M \le \min\left(\frac{N(N-1)}{2},\ 300\\,000\right)
  • 109A_i,j109-10^9 \le A\_{i,j} \le 10^9 (0iN1,0jK1)(0 \le i \le N-1, 0 \le j \le K-1)
  • 0U_i<V_iN10 \le U\_i < V\_i \le N - 1 (0iM1)(0 \le i \le M-1)
  • (U_i,(U\_i, V_i)(U_j,V\_i) \neq (U\_j, V_j)V\_j) (0i<jM1)(0 \le i < j \le M-1)