B. Vika and Squares

题目连接:

http://www.codeforces.com/contest/610/problem/B

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

5

2 4 2 3 3

Sample Output

12

Hint

题意

你有n种颜色,然后每种颜色有ai个,你需要依次涂色。

比如第一个你涂x,那么下一个就得涂x+1,然后x+2.....

问你最多能涂多少个格子

题解:

贪心一下,我们肯定从最小的后面一格开始走。

这样可以得到答案,是 n * min + tmp

tmp是多少呢?tmp是最小值的两个之间的差值中最大的那个

证明略 hhh

代码

#include<bits/stdc++.h>
using namespace std; int Min = 1e9+5,ans=0;
int a[200005];
vector<int>P;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(Min>=a[i])
{
Min = a[i];
ans = i;
}
}
int first = 0;
for(int i=1;i<=n;i++)
{
if(a[i]==Min)
P.push_back(i);
}
P.push_back(P[0]+n);
long long tmp = 0;
for(int i=1;i<P.size();i++)
tmp = max(tmp,1LL*P[i]-1LL*P[i-1]-1LL);
printf("%lld\n",1LL*n*Min+1LL*tmp);
}

Codeforces Round #337 (Div. 2) B. Vika and Squares 贪心的更多相关文章

  1. Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

    B. Vika and Squares   Vika has n jars with paints of distinct colors. All the jars are numbered from ...

  2. 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 ...

  3. Codeforces Round #337 (Div. 2) 610B Vika and Squares(脑洞)

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  6. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  7. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  8. Codeforces Round #337 (Div. 2)

    水 A - Pasha and Stick #include <bits/stdc++.h> using namespace std; typedef long long ll; cons ...

  9. Codeforces Round #337 (Div. 2)B

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. [转]linux之partprobe命令

    转自:http://www.7edown.com/edu/article/soft_48647_1.html linux上,在安装系统之后,可否创建分区并且在不重新启动机器的情况下系统能够识别这些分区 ...

  2. failback 和failover

    dubbo 和motan都有在注册中心中都有这个概念 Failover 失效转移通俗地说,即当A无法为客户服务时,系统能够自动地切换,使B能够及时地顶上继续为客户提供服务,且客户感觉不到这个为他提供服 ...

  3. java学习随笔--- 捣蛋vector

    最近比较有时间啦,有时间搞下java,个人觉得学这门语言语法太多啦,不一一去学习啦,心血来潮,挂了个struct2的源代码,一入深似海啊,看得我天花缭乱,从最简单的开始吧 public static ...

  4. Java基础 —— HTML

    HTML:超文本标记语言(HyperText Markup Language) --> 最基础的网页语言 基本结构: <html> //HTML文件的开始 <head> ...

  5. Ngix 移动端与Pc端 反向代理判断

    如神马搜索和百度(http://www.baidu.com),当用桌面浏览器和移动浏览器访问的结果是不一样的.其中的手段可能有两种: 转载Ngix反向代理判断 服务端直接判断UA输出不同的界面,JAV ...

  6. android 开发必用的开源库

    LogReport:  https://github.com/wenmingvs/LogReport,   崩溃日志上传框架 wcl-permission-demo:Android 6.0 - 动态权 ...

  7. openstack neutron网络主机节点网口配置 liberty版本之前的

  8. SQL Server 索引 之 书签查找 <第十一篇>

    一.书签查找的概念 书签可以帮助SQL Server快速从非聚集索引条目导向到对应的行,其实这东西几句话我就能说明白. 如果表有聚集索引(区段结构),那么书签就是从非聚集索引找到聚集索引后,利用聚集索 ...

  9. 通过Microsoft Azure服务设计网络架构的经验分享(转)

    原文:http://www.infoq.com/cn/articles/azure-networking-tips 本文从产品设计和架构角度分享了 Microsoft Azure 网络服务方面的使用经 ...

  10. Swift 可选值(Optional Values)介绍

    Optional的定义 Optional也是Objective-C没有的数据类型,是苹果引入到Swift语言中的全新类型,它的特点就和它的名字一样:可以有值,也可以没有值,当它没有值时,就是nil.此 ...