3
7
2016
0

[Codeforces Round #337] Vika and Squares


B. Vika and Squares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar containsai liters of paint of color i.

Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 123 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.

Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has.

The second line of the input contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.

Output

The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above.

Examples
input
5
2 4 2 3 3
output
12
input
3
5 5 5
output
15
input
6
10 10 10 1 10 10
output
11
Note

In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.

In the second sample Vika can start to paint using any color.

In the third sample Vika should start painting using color number 5.

题目大意:有$n$罐颜料,按$1\sim n$编号,第$i$罐里有$a_i$个单位的颜料,现在从第$x$罐开始,第$i$次操作使用第$\left(x+i-1\right)\bmod n$罐中一个单位的颜料,反复执行操作直到碰到一个空的颜料罐。求最多可以用掉多少单位颜料。


先所有颜料罐都用掉$\min_\limits{i\in\left[1,n\right]}a_i$单位,然后扫一遍,找出最长连续不为0的一段都用掉1单位。注意跨越边界的情况。

代码在此。

Category: 题解 | Tags: Codeforces | Read Count: 758

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com