Under construction

Still being built. Things may change

Clique Graph Shortest Path Sum

Time limit2sMemory limit512 MB

Summary
A graph is given as a union of K cliques on overlapping vertex lists; sum the shortest path lengths over all vertex pairs.
Level

Medium6 of 10

Topics
Graph, BFS, Shortest path
Solved
No attempts yet

Problem

An undirected, unweighted, connected graph GG has NN vertices numbered 0 through N−1N-1.

The edges of GG are fixed by two arrays, VV and sizessizes. VV holds vertex numbers, and the same number may appear in it more than once. sizessizes holds the lengths of the blocks that VV is cut into, starting from the front.

Define S[i]S[i] as the sum of the first ii elements of sizessizes. For example, if sizes={10,20,30}sizes = \{10, 20, 30\}, then S[0]=0S[0] = 0, S[1]=10S[1] = 10, S[2]=30S[2] = 30 and S[3]=60S[3] = 60.

For every ii with 0≤i<K0 \le i < K, take every pair (j,k)(j, k) that satisfies S[i]≤j<k<S[i+1]S[i] \le j < k < S[i+1]. Each such pair gives one edge between vertex V[j]V[j] and vertex V[k]V[k]. In other words, the vertices V[S[i]]V[S[i]] through V[S[i+1]−1]V[S[i+1]-1] form a complete graph. GG has no other edges.

Given NN, VV and sizessizes, write a program that computes the shortest path length for every pair of two distinct vertices and prints the sum. The graph given in the input is always connected.

Input

The first line contains the number of vertices NN (2≤N≤25002 \le N \le 2500), the size MM of VV (1≤M≤50001 \le M \le 5000) and the size KK of sizessizes (1≤K≤25001 \le K \le 2500), separated by spaces.

The second line contains the MM elements of VV in order. The third line contains the KK elements of sizessizes in order.

Every element of VV is between 0 and N−1N-1. Every element of sizessizes is an integer between 2 and NN, and their sum is at most MM. For every ii with 0≤i<K0 \le i < K, the values V[S[i]],V[S[i]+1],…,V[S[i+1]−1]V[S[i]], V[S[i]+1], \dots, V[S[i+1]-1] are distinct.

Output

Print on the first line the sum of the shortest path lengths over all pairs of two distinct vertices. Each pair is counted once. The value can exceed the range of a 32-bit integer.

Examples2

  1. Example 1

    Input
    4 5 2
    0 1 2 0 3
    3 2
    
    Expected output
    8
    
  2. Example 2

    Input
    5 7 2
    0 1 2 3 1 2 4
    4 3
    
    Expected output
    12