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. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  2. 824. Goat Latin山羊拉丁文

    [抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...

  3. jsoup 的简单应用

    导入相关jar包 package jsoup.zr.com.utils; import java.io.IOException; import java.util.List; import org.j ...

  4. Mybatis中resultType和resultMap

    一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...

  5. css 样式文字溢出显示省略号

    在table中使用溢出样式,table样式要设置为”table-layout: fixed“,即<table style="table-layout: fixed;"> ...

  6. 利用osmosis导出osm城市数据

    转载(未测试) 方法核心就是利用osmosis的导出指定功能,即是从大范围导出小范围的基本用例. 我们只需要知道我们所需要提取的城市的经纬度范围, 例如广州市的经纬度范围是北纬22.26~23.56度 ...

  7. Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法

    Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/ ...

  8. oracle数据库单表查询

    今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...

  9. System.Web.UI.Page事件执行顺序

    #region OnPreInit 第一步(显式重写,文章下面有隐式重写) protected override void OnPreInit(EventArgs e) { //检查 IsPostBa ...

  10. HYSBZ 1036 树的统计Count (水题树链剖分)

    题意:中文题. 析:就是直接维护一个最大值和一个和,用线段树维护即可,这个题很简单,但是我卡了一晚上,就是在定位的时候,位置直接反过来了,但是样例全过了...真是... 代码如下: #pragma c ...