Berries for the Lemurs
InterviewTime limit2sMemory limit1024 MB
Given n lemurs with love levels a_i, find the sum over each i of a_i times the number of earlier lemurs j whose reach j+a_j extends past i.
- Level
Medium4 of 10
- Topics
- Prefix sum, Array
- Solved
- No attempts yet
Statement
Lemurs are cute animals that live on the island of Madagascar. Their leader is an energetic and charming lemur who calls himself King Julian. One of his royal duties is handing out berries to all of his subjects.
To hand out the berries, King Julian lines up all lemurs of his tribe in a row. Besides its number in this row, each lemur has a non-negative integer , its level of love for King Julian. The number of berries each lemur receives depends on how much he and his neighbors in the row love their leader.
Before giving berries to lemur number , Julian counts the lemurs with numbers such that . He then multiplies this count by , and the result is the number of berries lemur receives.
Find the total number of berries Julian has to hand out.
Input
The first line contains one integer , the number of subjects of King Julian. The next line contains integers (), the lemurs' levels of love for their leader.
Output
Print one integer, the total number of berries King Julian has to hand out.
Hint
The answer for the sample is computed by the formula .