Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. 

A new idea Bill has recently developed assigns a non-negative integer value to each day of human life. 

Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day. 

Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.

Input

The first line of the input contains n - the number of days of Bill's life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 10 6 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill's life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill's life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

Sample Input

6
3 1 6 4 5 2

Sample Output

60
3 5

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<cmath> const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll a[maxn];
ll sum[maxn];
ll L[maxn];
ll L2[maxn];
int main()
{
int n;ll mn=0x3f3f3f3f;
while(cin>>n)
{
for(int t=1;t<=n;t++)
{
scanf("%lld",&a[t]);
mn=min(mn,a[t]);
}
memset(sum,0,sizeof(sum));
for(int t=1;t<=n;t++)
{
sum[t]=sum[t-1]+a[t];
}
stack<int> S1,S2;
while(!S1.empty())
{
S1.pop();
}
while(!S2.empty())
{
S2.pop();
} for(ll t=1;t<=n;t++)
{
while(S1.size() && a[S1.top()] >= a[t]) S1.pop();
if(S1.empty()) L[t] = 0;
else L[t] = S1.top();
S1.push(t);
}
for(ll t=n;t>=1;t--)
{
while(S2.size() && a[S2.top()] >= a[t]) S2.pop();
if(S2.empty()) L2[t] = n+1;
else L2[t] = S2.top();
S2.push(t);
}
ll maxnn=n*mn;//注意这个初始化,很细节
int j=1,k=n;
for(int t=1;t<=n;t++)
{
if((sum[L2[t]-1]-sum[L[t]])*a[t]>maxnn)
{
maxnn=(sum[L2[t]-1]-sum[L[t]])*a[t];
j=L[t]+1;
k=L2[t]-1;
}
}
cout<<maxnn<<endl;
cout<<j<<" "<<k<<endl;
}
return 0;
}

Feel Good(两遍单调栈维护区间+前缀和)的更多相关文章

  1. 【bzoj5089】最大连续子段和 分块+单调栈维护凸包

    题目描述 给出一个长度为 n 的序列,要求支持如下两种操作: A  l  r  x :将 [l,r] 区间内的所有数加上 x : Q  l  r : 询问 [l,r] 区间的最大连续子段和. 其中,一 ...

  2. bzoj1007: [HNOI2008]水平可见直线 单调栈维护凸壳

    在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例如,对于直线:L1:y=x; L2:y=-x; L3 ...

  3. [CSP-S模拟测试]:A(单调栈维护凸包+二分答案)

    题目传送门(内部题150) 输入格式 第一行两个整数$N,Q$. 接下来的$N$行,每行两个整数$a_i,b_i$. 接下来的$Q$行,每行一个整数$x$. 输出格式 对于每个询问,输出一行一个整数表 ...

  4. LOJ #2769 -「ROI 2017 Day 1」前往大都会(单调栈维护斜率优化)

    LOJ 题面传送门 orz 斜率优化-- 模拟赛时被这题送走了,所以来写篇题解( 首先这个最短路的求法是 trivial 的,直接一遍 dijkstra 即可( 重点在于怎样求第二问.注意到这个第二问 ...

  5. CF1137E Train Car Selection(单调栈维护凸函数)

    首先本题的关键是一次性加0操作只有第一个0是有用的.然后对于1 k操作,其实就是把之前的所有数删除.对于其他的情况,维护一次函数的和,将(i,a[i])看成平面上的一个点,用单调栈维护一下. #inc ...

  6. 翻转长方形 (不知名oj中一道个人私题)--单调栈维护最大子矩形

    怎么分析这道题呢? 首先 ,我们注意到一点: 不管怎么操作,任意一个2*2方格中的 "#"个数的奇偶性是不变的. 所以,如果一个2*2方格中有奇数个"#",这个 ...

  7. Lost My Music:倍增实现可持久化单调栈维护凸包

    题目就是求树上每个节点的所有祖先中(ci-cj)/(dj-di)的最小值. 那么就是(ci-cj)/(di-dj)的最大值了. 对于每一个点,它的(ci,di)都是二维坐标系里的一个点 要求的就是祖先 ...

  8. 【单调栈】【前缀和】【二分查找】8.28题解-long

    long 题目描述 AP神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M的矩形空地.空地中每个格子都有自己的海拔高度.AP想让他的宫殿的平均海拔在海平面之上(假设海平面的高度是0,平均数都会 ...

  9. Q - Queue HDU - 5493(树状树组维护区间前缀和 + 二分找预留空位)

    Q - Queue HDU - 5493 Problem Description NNN people numbered from 1 to NNN are waiting in a bank for ...

随机推荐

  1. Base -快捷键|通配符|特殊符号|输出(正确与错误的保存)

    curl + a   移动光标到行首. curl +e    移动光标到行尾. curl +k    剪切光标所在位置到行末的字符. curl+u    剪切光标所在位置到行首的字符. curl +y ...

  2. 关于getchar的一些思考

    这个问题是有一段代码引起的: 代码1: #include<iostream> using namespace std; int main() { char t; t=getchar(); ...

  3. 在Ubuntu安装Tomcat7.0及开机自动运行

    在Ubuntu安装Tomcat7.0及开机自动运行 1.安装装Tomcat7.0 一般都是绿色版的,下载一个tomcat7.0解开到指定的目录上即可 然后进入tomcat目录的bin文件夹,执行 su ...

  4. web大文件上传控件-设置附加参数-Xproer.HttpUploader6

    自定义附加字段在up6.js中定义,也可以不用定义: 注意: 1.附加字段必须是字符串类型. 2.如果附加字段的值包含中文,在上传前必须使用encodeURIComponent进行编码.     在引 ...

  5. 编译boost,去掉不使用的组件

    说明:下面内容仅针对Linux环境(boost官网为:http://www.boost.org/,可从这里下载它的源代码包,这里要求下载.tar.gz包,而非.7z..zip或bz2包). 在当前目录 ...

  6. 打造一套UI与后台并重.net通用权限管理系统

    一.前言 从进行到软件开发这个行业现在已经有几年了,在整理出这个套开发框架之前自己做了不少重复造轮子的事.每次有新的项目总是要耗费不少时间在UI.权限和系统通用模块上面,自己累得要死,老板还骂没效率. ...

  7. Linux:SSH免密码登录

    1.使用包管理器安装openssh: 本人的系统是Arch Linux,因此安装命令为:sudo pacman -S openssh 2.使用ssh-keygen命令创建公钥: #ssh-keygen ...

  8. canvas基本绘制图形

    canvas H5新增的元素,提供了强大的图形的绘制,变换,图片,视频的处理等等.需要使用JavaScript脚本操作 浏览器支持 大多数的现代浏览器都可以支持:IE8以下的浏览器不支持 画布 可支持 ...

  9. Setting property 'source' to 'org.eclipse.jst.jee.server:web' did not find a matching property原因

    这个问题困扰了好久,虽然只是tomcat的一个警告,但强迫症让我总觉得不舒服,搜索了好多文章才找到知乎上一篇处理的最好的.另外:能找谷哥,尽量不要度娘,太浪费时间. 具体操作如下: 默认情况下,ser ...

  10. 快速下载android源码

    众所周知的原因,android源码被墙了,还好国内有不少镜像,这里使用清华提供的镜像. 以下内容转自: https://wiki.tuna.tsinghua.edu.cn/MirrorUsage/an ...