Codeforces Round #337 (Div. 2) B. Vika and squares water problem

Source: Internet
Author: User

B. Vika and Squares

Vika have n jars with paints of distinct colors. All the jars is numbered from 1 to n and the i-th jar contains ai< /c12> liters of paint of color I.

Vika also have an infinitely long rectangular piece of paper of Width 1, consisting of Squar Es of Size 1x1. Squares is Numbered 1, 2,  3and so on. Vika decided that she'll start painting squares one by one from left to right and starting from the square Number  1 and some arbitrary color. If The square is painted in Color  x , then the next square would be painted in COLOR&N Bsp x + 1. In case Of  x = n , next square was painted in Color 1. If there is no more paint of the color Vika wants to use now, then she stops.

Square is all 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 being painted, if Vika chooses right color to paint the fi RST square.

Input

The first line of the input contains a single integer n (1≤ n ≤200)-the Number of Jars W ITH colors Vika has.

The second line of the input contains a sequence of integers a1, a2, ..., a N (1≤ ai ≤109), where ai am equal to the number O F 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 fol Lows 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 are to start painting using color 4. Then the squares is 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.

Test Instructions : give you a sequence, you can start from any starting point, from doing to the right, has been circulating, each left a point, weights-1, ask you to walk a few steps

puzzle: Sweep to find the smallest value, for the last section of the road, we can be the middle of a value, or both ends of the value, traverse Select one of the largest is good

//Meek///#include <bits/stdc++.h>#include <cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<iostream>#include<bitset>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>using namespacestd;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairtypedefLong Longll;Const intN =201000;Const intM =1000001;Const intINF =0x3f3f3f3f;Const intMOD =1000000007;Const DoubleEPS =0.000001; ll A[n],n;intMain () {scanf ("%i64d",&N); ll ans=inf; for(intI=1; i<=n;i++) {scanf ("%i64d",&A[i]); Ans=min (ans,a[i]);}  for(intI=1; i<=n;i++) A[i]-=ans; ans.*=N; ll mm=0, mn=0;  for(intI=1; i<=n;i++) {        if(A[i]) mm++; Else{mn=Max (MM,MN); MM=0; } MN=Max (MM,MN); } MN=Max (MM,MN); MM=0;  for(intI=1; i<=n;i++)if(A[i]) mm++; Else  Break;  for(inti=n;i>=1; i--)if(A[i]) mm++; Else  Break; mn=Max (MM,MN); cout<<ans+mn<<Endl; return 0;}
Code

Codeforces Round #337 (Div. 2) B. Vika and squares water problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.