【CodeForces 610B】Vika and Squares

Vika and Squares

Description

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 Input

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

Hint

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~n的不同的颜料,第i种颜料有a[i]个,现在给出一个宽为1,由1*1的方块组成的长度无限的纸,方块从左到右标记为1,2,3……已知1个颜料只能够涂一个方块,现在从第1个开始从左到右填涂。
要求:第一个方块可以是任意颜料,如果是标号为x的颜料,那么下一个颜料为x+1,如果x=n,则下一个x为1,直到没有满足条件的颜料用来填涂时停止。
试求可以填涂的最大长度。

【输入】
第一行:一个整数n (1<=n<=200,000)
第二行:n个整数 a[1],a[2],……a[n]  (1<=a[i]<=10^9)

【输出】
一个整数,表示可能的最大长度。

【分析】
由于第一个可以是任意颜料,根据填涂规则,前n个为1~n的一个排列,令min为a[i]中的最小值,则前n*min个即为第一个排列重复min次,对每一个a[i]都减去min后,那么问题的关键就是寻找新的a[i]数列中的最长连续非0子列的长度len,这样的同时前面排列的顺序也确定了,答案即为n*min+len。注意由于第n个颜料后面是第1个颜料,即连续非0子列可能是从尾端连续到首端,例如1,0,0,2,3的len为3,而不是2,。简单的操作方法就是定义一个长度为2*n的数组存放a[i],满足a[i]=a[n+i],即两个相同的首尾相连的数列a[i]。

【AC代码】
<span style="font-size:14px;">#include<stdio.h>
int main()
{
	long long n,i,j,count=0,min,len=0;
	int a[400100]={0};//2*n长度 
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		a[n+i]=a[i];
	}
	min=a[1];
	for(i=1;i<=n;i++)
	{
		if(min>a[i])
		min=a[i];
	}//查找最小值 
	for(i=1;i<=2*n;i++)
	{
		a[i]=a[i]-min;
	}//减去最小值 
	for(i=1;i<=2*n;i++)
	{
		if(a[i]!=0)
		{
			for(j=i;;j++)
			{
				if(a[j]==0)
				break;
			}
			if(len<j-i)
			{
				len=j-i;
			}
			i=j-1;
		}
	}
	count=min*n+len;
	printf("%lld",count);
	return 0;
}</span>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值