Niwango created a playlist of N songs. The title and the duration of the i-th song are s_i and t_i seconds, respectively. It is guaranteed that s_1,\ldots,s_N are all distinct.
Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.) However, he fell asleep during his work, and he woke up after all the songs were played. According to his record, it turned out that he fell asleep at the very end of the song titled X.
Find the duration of time when some song was played while Niwango was asleep.
Constraints
1 \leq N \leq 50
s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.
s_1,\ldots,s_N are distinct.
There exists an integer i such that s_i = X.
1 \leq t_i \leq 1000
t_i is an integer.
Input
Input is given from Standard Input in the following format:
We are given N songs with title and duration t_i seconds. The playlist is played in order, no pauses. Niwango fell asleep at the very end of song titled X. That means he fell asleep exactly at the moment when that song finished. He woke up after all songs were played. We need to find the total duration of time when some song was played while he was asleep. That is, the total time from when he fell asleep (the end of song X) to the end of the last song (when all songs are played). So we need to sum the durations of all songs that come after the song with title X.
But careful: "the duration of time when some song was played while Niwango was asleep." Since he fell asleep at the very end of song X, he was awake during song X (including its entire duration). Then after that, from the start of the next song until the end of the last song, he was asleep. There's no pause, so the asleep period begins at the end of song X, which coincides with the start of the next song (if any). So the asleep duration is the sum of durations of all songs after X. That seems straightforward.
Check example 1: songs: dwango 2, sixth 5, prelims 25. X = dwango. So after dwango, we have sixth 5 + prelims 25 = 30. Output 30. Yes.
Example 2: only one song abcde 1000, X=abcde. So after that song there are none, asleep duration 0. Output 0. Yes.
Example 3: They output 6348. We can verify quickly: We need to find X = pmuduhznoaqu. Find its position: from list: 1: ypnxn 279, 2: kgjgwx 464, 3: qquhuwq 327, 4: rxing 549, 5: pmuduhznoaqu 832, ... So songs after index 5 are from index 6 to 15. Sum durations of songs 6 to 15. Let's compute sum: we have song6: dagktgdarveusju 595? Actually check list order:
So solution: read N, then list of songs, find index i where s_i == X, then sum t_{i+1} ... t_N. Print sum.
Constraints are small (N <= 50) so trivial.
Implementation: Use a list of (title, duration). Or better: as we read input, we can store titles and durations in arrays. Then after reading X, we can find position. Since strings distinct, we can also use a dictionary mapping title to index. But simplest: read all pairs into two lists: titles, times. Then iterate to find index of X. Then compute sum of times from index+1 to end.
Edge cases: if X is the last song, sum is 0.
Potential pitfalls: Input format: N on first line, then N lines each with string and integer, then X on its own line. There may be extra whitespace but standard.
Thus answer.
We'll produce code in Python. We'll read N, loop for _ in range(N): read line, split, title = parts[0], t = int(parts[1]), store in list. Then read X (maybe using input().strip()). Then find index: for i, (title, t) in enumerate(songs): if title == X: idx = i; break. Then sum = sum(t for (_, t) in songs[idx+1:]). Print sum.
Complexity O(N).
But ensure to treat as int.
Let's write. ```python
import sys
def main():
data = sys.stdin.read().strip().splitlines()
if not data:
return
N = int(data[0].strip())
songs = []
for i in range(1, N+1):
parts = data[i].split()
title = parts[0]
t = int(parts[1])
songs.append((title, t))
X = data[N+1].strip() if N+1 < len(data) else ''
code
# Find index of the song with title X
idx = -1
for i, (title, _) in enumerate(songs):
if title == X:
idx = i
break
# Sum durations after X
ans = sum(t for _, t in songs[idx+1:])
print(ans)