您的位置:首页 > 其它

CodeForces 610B - Vika and Squares

2016-07-12 20:35 344 查看
题目:

B - Vika and Squares

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

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 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 ≤ 10e9), 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个颜料罐,里面有a[i]升颜料,1<=i<=n;颜料位置固定。画一张1X1的纸要一升颜料。从某一个罐子开始,依次向后使用第i个罐子中的一升颜料来画一张纸,到达第n个罐子时,跳回第一个罐子,当遇到罐子为空时停止。选择一个合适的位置,使得能画最多的纸,输出这个最大的数。

思路:

刚开始理解为规律题,(尴尬),找到最小的颜料是min升,然后标记这个min第一次出现的位置和最后出现的位置,再根据所谓的规律,然后就可以解出,测试也过了。。。果断WA了

中间还出了小插曲,错用swap函数了,将数组的值都更改了,死性不改,将我的错误思路代码改了后再交,还是WA。

正确思路:

考虑到短板是最少颜料的罐子,那么,必定可以画大于等于min*n张纸,接下来,可以将数组的值减去min,之后便是求最长的不为空的颜料罐子有多少个,相加即可。

#include <iostream>
#include <algorithm>
#include <cstring>
const int maxn = 200000+10;
long long arr[maxn];

using namespace std;

int main(){
int n;
while(cin>>n){
memset(arr,0,sizeof(arr));

cin>>arr[0];

long long low, temp;

low = arr[0];

for(int i=1; i<n; i++){
cin>>arr[i];
if(arr[i] < low)
low = arr[i];
}

for(int i=0; i<n; i++)
arr[i] -= low;

int i=0;
int len=0;
while(i<n){
int j=i;
int cnt = 0;
while(arr[j]>0){
cnt++;
j++;
if(j==i-1) break;
if(j==n) j=0;

}
if(len<cnt) len = cnt;
if(i>j) break;
else i = j+1;
}

long long ans = low*n + len;
cout<<ans<<endl;
}
return 0;
}


注意数据的范围,最多20W个罐子,每个罐子最多10e9升。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: