Speedrun
InterviewTime limit6sMemory limit1024 MB
Given N levels where each level points to a next level, find the minimum total time (sum of level numbers) to complete levels until entering a completed one.
Problem
The game has levels numbered from 1 to . A player can start on any level from 1 to . For each level , the level follows it. The player wins as soon as they enter a level that is already completed. Completing every level is not required. Bajtosia can complete every level, and completing level takes exactly minutes. Write a program that reads the game description, computes the minimum number of minutes Bajtosia needs to win, and prints it.
Input
The first line contains one natural number (), the number of levels. The second line contains integers () separated by single spaces. The -th number means that level follows level .
Output
Print one integer, the minimum number of minutes Bajtosia needs to win.