Golden Landmarks

면접 대비

시간 제한1초메모리 제한2048 MB

요약
이름과 격자 좌표가 주어진 n개의 랜드마크를 정해진 순서로 방문할 때, 연속한 두 지점 사이의 맨해튼 거리를 모두 더해 출력한다.
난이도

쉬움10점 중 2점

유형
구현, 해시맵, 수학, 배열
정답자
아직 제출이 없습니다

문제

The city of Golden has many landmarks worth visiting. Before arriving in Golden, you made a list of landmarks you want to visit and the order in which you will visit them. To understand where these landmarks are located, you create a map of Golden and mark each landmark on it. You determine the xx and yy coordinates of each landmark and plot out your journey for the day. Now, you want to determine the total walking distance required to visit all landmarks in the given order. You will start at the first landmark and finish your walk at the last landmark.

Since Golden is laid out on a grid, the walking distance between two landmarks at coordinates (x_1,y_1)(x\_1, y\_1) and (x_2,y_2)(x\_2, y\_2) is ∣x_1−x_2∣+∣y_1−y_2∣|x\_1 - x\_2| + |y\_1 - y\_2|.

입력

The first line of input contains an integer nn (2≤n≤1052 \leq n \leq 10^5) representing the number of landmarks.

The next nn lines each contain a string ss (1≤∣s∣≤251 \leq |s| \leq 25), the name of a landmark, followed by two integers xx and yy (−104≤x,y≤104-10^4 \leq x, y \leq 10^4) representing the landmark's coordinates. Each landmark name consists only of uppercase and lowercase Latin letters and is unique.

The final line contains a space-separated sequence of nn landmark names, specifying the exact order in which you will visit them. Each landmark appears exactly once in this sequence.

출력

Output a single integer, the total walking distance required to visit all landmarks in the specified order.

예제2

  1. 예제 1

    입력
    2
    CTLM 1 1
    MinesMarket 2 2
    MinesMarket CTLM
    
    예상 출력
    2
    
  2. 예제 2

    입력
    5
    ClearCreek 0 0
    MinesM 20 10
    SouthTable -20 -10
    WoodysPizza 10 10
    ColoradoSchoolOfMines 20 -10
    ColoradoSchoolOfMines ClearCreek WoodysPizza SouthTable  MinesM
    
    예상 출력
    160