8255: 262144 Revisited

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

Bessie likes downloading games to play on her cell phone, even though she does find the small touch screen rather cumbersome to use with her large hooves.

She is particularly intrigued by the current game she is playing. The game starts with a sequence of NN positive integers a1,a2,…,aNa1,a2,…,aN (2≤N≤262,1442≤N≤262,144), each in the range 1…1061…106. In one move, Bessie can take two adjacent numbers and replace them with a single number equal to one greater than the maximum of the two (e.g., she might replace an adjacent pair (5,7)(5,7) with an 88). The game ends after N−1N−1 moves, at which point only a single number remains. The goal is to minimize this final number.

Bessie knows that this game is too easy for you. So your job is not just to play the game optimally on aa, but for every contiguous subsequence of aa.

Output the sum of the minimum possible final numbers over all N(N+1)2N(N+1)2 contiguous subsequences of aa.

INPUT FORMAT (input arrives from the terminal / stdin):

First line contains NN. The next line contains NN space-separated integers denoting the input sequence.

OUTPUT FORMAT (print output to the terminal / stdout):

A single line containing the sum.

SAMPLE INPUT:

6
1 3 1 2 1 10

SAMPLE OUTPUT:

115

There are 6⋅72=216⋅72=21 contiguous subsequences in total. For example, the minimum possible final number for the contiguous subsequence [1,3,1,2,1][1,3,1,2,1] is 55, which can be obtained via the following sequence of operations:

original    -> [1,3,1,2,1]
combine 1&3 -> [4,1,2,1]
combine 2&1 -> [4,1,3]
combine 1&3 -> [4,4]
combine 4&4 -> [5]

Here are the minimum possible final numbers for each contiguous subsequence:

final(1:1) = 1
final(1:2) = 4
final(1:3) = 5
final(1:4) = 5
final(1:5) = 5
final(1:6) = 11
final(2:2) = 3
final(2:3) = 4
final(2:4) = 4
final(2:5) = 5
final(2:6) = 11
final(3:3) = 1
final(3:4) = 3
final(3:5) = 4
final(3:6) = 11
final(4:4) = 2
final(4:5) = 3
final(4:6) = 11
final(5:5) = 1
final(5:6) = 11
final(6:6) = 10

SCORING:

  • Test cases 2-3 satisfy N≤300N≤300.
  • Test cases 4-5 satisfy N≤3000N≤3000.
  • In test cases 6-8, all values are at most 4040.
  • In test cases 9-11, the input sequence is non-decreasing.
  • Test cases 12-23 satisfy no additional constraints.