Changyoung and the Bus
InterviewTime limit2sMemory limit512 MB
Given a list of buses in the order they are ridden and a matrix of transfer fares, print the sum of the fares between consecutive buses.
- Level
Easy2 of 10
- Topics
- Implementation, Array, Matrix, Simulation
- Solved
- No attempts yet
Problem
Changyoung takes the bus to work every day.
The city where Changyoung lives has N buses, and transferring from bus S to bus E costs AS,E won.
The M buses Changyoung uses to get to work are given in the order he transfers between them, and the transfer fares between all pairs of buses are given.
Find the total transfer fare Changyoung must pay for one trip to work. The bus fare for the first ride is excluded.
Input
The first line gives the number of buses in the city, N, and the number of buses Changyoung uses, M.
The second line gives the numbers of the M buses Changyoung uses, in order. Bus numbers are distinct.
Starting from the third line, N lines give information about transfer fares between buses. The E-th number on the S-th line is AS,E. AS,E may differ from AE,S.
Output
Print the total transfer fare required when riding the given M buses in order, transferring between them.
Constraints
- 2 ≤ N ≤ 100
- 1 ≤ M ≤ N
- 1 ≤ AS,E ≤ 1,000 (1 ≤ S, E ≤ N)
- Bus numbers are natural numbers between 1 and N.
- Changyoung does not use the same bus more than once.