#K63867. Checkpoint Transition Time

    ID: 31849 Type: Default 1000ms 256MiB

Checkpoint Transition Time

Checkpoint Transition Time

You are given a process consisting of N stages and a matrix \(T\) of dimensions \(N \times N\). The element \(T_{ij}\) represents the time taken to move from stage \(i\) to stage \(j\). Additionally, you are provided with M checkpoints, which specify the stages that a product must pass through sequentially. Your task is to compute the total transition time of the product as it moves through these checkpoints in order. Formally, if the checkpoints are \(c_1, c_2, \ldots, c_M\), the total time is given by:

\(\text{Total Time} = \sum_{i=1}^{M-1} T_{c_i, c_{i+1}}\)

Note that the indices in the checklist are 1-indexed. Ensure that your solution correctly converts these indices when accessing the matrix.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two integers, N and M, representing the number of stages and the number of checkpoints respectively.
  • The next N lines each contain N integers. The j-th integer on the i-th line represents \(T_{ij}\), the time to transition from stage i to stage j.
  • The final line contains M space-separated integers representing the sequence of checkpoints (1-indexed).

outputFormat

Output a single integer to standard output (stdout) representing the total time taken to transition through the given checkpoints.

## sample
5 3
0 10 20 30 40
10 0 15 25 35
20 15 0 17 27
30 25 17 0 20
40 35 27 20 0
1 3 5
47