codeforces 610B

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 ≤ 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

52 4 2 3 3

Output

12

Input

35 5 5

Output

15

Input

610 10 10 1 10 10

Output

11

要涂油漆,有T(需要输入)种油漆,每种编号1,2,3,4....,没涂完一次,下一种油漆序号要比第一种大,如果到最大,那下一种就是1,输入各种油漆的数量,问你最多能涂多少油漆
 1 #include <stdio.h>
 2 #include <iostream>
 3 using namespace std;
 4 #define Maxn 210000
 5 #define INF 1500000000
 6
 7 struct Node
 8 {
 9     long long int num;
10 }N[Maxn];
11
12 int main()
13 {
14     long long int T;
15     scanf("%lld",&T);
16     long long int min = INF;
17     long long int flag_big;
18     long long int flag_small;
19     long long int flag;
20     long long int bigger;
21     bool Q = false;
22     for(int i = 1; i <= T; i++)
23     {
24         scanf("%lld",&N[i].num);
25         if (min > N[i].num)
26         {
27             min = N[i].num;
28             flag = i;
29         }
30     }
31     if (T == 1)
32     {
33         printf("%lld\n",N[1].num);//我在这里错了十发- -
34         return 0;
35     }
36     flag_small = flag;
37     for(int i = 1; i <= T; i++)
38     {
39         if (min == N[i].num && flag < i)
40         {
41             flag = i;
42             Q = true;
43         }
44     }
45     flag_big = flag;
46     long long int biggest;
47     bigger = T - flag_big + flag_small -1;
48     long long int count = 0;
49     long long int cnt = 0;
50     if (Q)
51     {
52         for(int i = flag_small; i <= T; i++)
53         {
54             if (N[i].num == min)
55             {
56                 count = i - flag_small-1;
57                 flag_small = i;
58             }
59             if (count > cnt)
60             {
61                 cnt = count;
62             }
63         }
64         biggest = max(cnt,bigger);
65         //long long Fuck;
66         //Fuck = T*min;
67         printf("%lld\n",T*min + biggest);
68     }
69     else
70     {
71         printf("%lld\n",T*min + T-1);
72     }
73 }
时间: 2024-10-31 02:39:31

codeforces 610B的相关文章

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Round #408 (Div. 2) B

Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x?=?i. The probl

Codeforces 617 E. XOR and Favorite Number

题目链接:http://codeforces.com/problemset/problem/617/E 一看这种区间查询的题目,考虑一下莫队. 如何${O(1)}$的修改和查询呢? 令${f(i,j)}$表示区间${\left [ l,r \right ]}$内数字的异或和. 那么:${f(l,r)=f(1,r)~~xor~~f(1,l-1)=k}$ 记一下前缀异或和即可维护. 1 #include<iostream> 2 #include<cstdio> 3 #include&l

CodeForces - 601A The Two Routes

http://codeforces.com/problemset/problem/601/A 这道题没想过来, 有点脑筋急转弯的感觉了 本质上就是找最短路径 但是卡在不能重复走同一个点 ---->>> 这是来坑人的 因为这是一个完全图(不是被road 连接  就是被rail连接 ) 所以一定有一条直接连1 和 n的路径 那么只用找没有连 1 和 n 的路径的 那个图的最短路即可 然后这个dijkstra写的是O(V^2)的写法 以后尽量用优先队列的写法O(ElogV) 1 #includ