题库练习 Trains and Statistic
← 上一题 下一题 →

A10426 | Trains and Statistic

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Vasya commutes by train every day. There are $n$ train stations in the city, and at the $i$ -th station it's possible to buy only tickets to stations from $i+1$ to $a_{i}$ inclusive. No tickets are sold at the last station.

Let $ρ_{i,j}$ be the minimum number of tickets one needs to buy in order to get from stations $i$ to station $j$ . As Vasya is fond of different useless statistic he asks you to compute the sum of all values $ρ_{i,j}$ among all pairs $1<=i<j<=n$ .

输入格式

The first line of the input contains a single integer $n$ ( $2<=n<=100000$ ) — the number of stations.

The second line contains $n-1$ integer $a_{i}$ ( $i+1<=a_{i}<=n$ ), the $i$ -th of them means that at the $i$ -th station one may buy tickets to each station from $i+1$ to $a_{i}$ inclusive.

输出格式

Print the sum of $ρ_{i,j}$ among all pairs of $1<=i<j<=n$ .

输入输出样例

输入 #1
4
4 4 4
输出 #1
6
输入 #2
5
2 3 5 5
输出 #2
17
C++ 编辑器
输入
输出