CodeForces-610B-Vika and Squares

来源:互联网 发布:网络店铺运营 编辑:程序博客网 时间:2024/06/16 02:41

Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai 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 1, 2, 3 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.
题意:给定每种颜色的瓶子数,每种颜色的编号依次为1,2…n,选定一种颜色的瓶子为起点进行排列,瓶子的颜色编号必须为连续的,如果瓶子颜色编号为最后一个,那么下一个为编号1。求最长能排多少个。
题解:找到瓶子数最少的颜色,数量为min,那么最少能排min*n个,然后找到最长的大于min的最长序列maxlen,则最多能排min*n+maxlen个。注意要用64位整形。

#include<iostream>#include<cstdio>using namespace std;int a[200005];int main(){    long long n;    cin >> n;    long long min = 1000000000;    for (int i = 0; i < n; i++)    {        cin >> a[i];        if (min > a[i])            min = a[i];    }    int num1, num2;    num1 = 0; num2 = n - 1;    while (num1<n&&a[num1] > min)        num1++;    while (num2>=0&&a[num2] > min)        num2--;    long long maxlen = num1 + n - 1 - num2;                     //最后一个min到第一个min的长度    int len = 0;    for (int j = num1 + 1; j < num2; j++)    {        if (a[j] > min)            len++;        else            len = 0;        if (maxlen < len)            maxlen = len;    }    maxlen = maxlen + n*min;    printf("%lld\n", maxlen);    return 0;}
0 0