Codeforces Round #337 (Div. 2) B. 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 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 123and 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.

Sample test(s)
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.



题意:给你一堆油漆,然后选择一个油漆开始涂,一个油漆一个油漆的涂,就是涂过4后面涂5,涂过n后面可以涂1,一直涂到不能继续就停止,问最多能涂多少块

思路:选择一个颜料最少的油漆,然后同时减去,然后从头向后扫看最长有值序列的长度,需要注意的是:最后涂完能返回1开始涂

总结:其实题目不难,做的时候总是想着怎么选取,冷静了一会才想到,还是太水了= =

ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
#define INF 0x7fffffff
#define MAXN 200010
#define mem(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define LL __int64
using namespace std;
LL a[MAXN];
int main()
{
    int t,i;
    int n,bz;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
        LL mi=INF;//这里定义成int,然后死活过不了
        int k;
        for(i=n;i>=1;i--)
        {
            if(a[i]<mi)
            {
                mi=a[i];
            }
        }
        for(i=1;i<=n;i++)
        a[i]-=mi;
        i=1;
        int len=0;
        int M=0;
        while(i<=n)
        {
        	if(a[i])
        	len++;
        	else
        	{
        		M=max(M,len);
        		len=0;
        	}
        	i++;
        }
        if(a[n])
        {
        	//printf("len=%d\n",len);
        	i=1;
        	while(a[i])
        	{
        		len++;
        		i++;
        	}
        	//printf("len=%d\n",len);
        	M=max(M,len);
        }
        LL ans=mi*n+M;
        printf("%I64d\n",ans);
    } 
    return 0;
}


你可能感兴趣的:(Codeforces Round #337 (Div. 2) B. Vika and Squares (技巧乱搞))