Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval.

Now she is planning to find the max value of the intervals in her array. Can you help her?

Input

First line contains an integer n(1≤n≤5×10^5).

Second line contains nn integers represent the array a(−10^5≤ai​≤10^5).

Output

One line contains an integer represent the answer of the array.

样例输入

5
1 2 3 4 5

样例输出

36
题解:
枚举每个元素作为最小值时的最优答案,然后取最大值。首先根据单调队列的思想,求出每个元素作为最小值时能够管辖的范围。
如果这个元素是正数或零,那么就是它所管辖的区间的值的和乘上该元素,因为他是正数,所以它所管辖的区间内的值均为正数。
如果这个元素是负数,那我们只需要找到
从它到它能影响到的范围的最右边前缀和的最小值-从它左边第一个元素到它能影响到的最左边的左边第一个元素这个区间前缀和的最大值,然后乘上该元素。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
int n,ri[maxn],le[maxn],st[maxn];
ll ans=-1e18,a[maxn],sum[maxn],mi[maxn*],ma[maxn*];
void build(int L,int R,int rt)
{
if(L==R)
{
mi[rt]=ma[rt]=sum[L];
return ;
}
int mid=(R+L)/;
build(L,mid,*rt);
build(mid+,R,*rt+);
mi[rt]=min(mi[*rt],mi[*rt+]);
ma[rt]=max(ma[*rt],ma[*rt+]);
}
ll qmi(int L,int R,int rt,int l,int r)
{
if(l<=L && R<=r) return mi[rt];
int mid=(L+R)/;
ll ret=1e18;
if(l<=mid) ret=min(ret,qmi(L,mid,*rt,l,r));
if(r>=mid+) ret=min(ret,qmi(mid+,R,*rt+,l,r));
return ret;
}
ll qma(int L,int R,int rt,int l,int r)
{
if(l<=L && R<=r) return ma[rt];
int mid=(L+R)/;
ll ret=-1e18;
if(l<=mid) ret=max(ret,qma(L,mid,*rt,l,r));
if(r>=mid+) ret=max(ret,qma(mid+,R,*rt+,l,r));
return ret;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-]+a[i];
}
build(,n,);
for(int i=;i<=n;i++)
{
int cnt=i-;
while(cnt>= && a[cnt]>=a[i])
{
cnt=le[cnt];
}
le[i]=cnt;
}
for(int i=;i<=n;i++) le[i]++;
for(int i=n;i>=;i--)
{
int cnt=i+;
while(cnt<=n && a[cnt]>=a[i])
{
cnt=ri[cnt];
}
ri[i]=cnt;
}
for(int i=;i<=n;i++) ri[i]--;
/*int top=0;
for(int i=1;i<=n;i++)
{
while(top && a[st[top]]>a[i])
{
ri[st[top]]=i-1;
top--;
}
st[++top]=i;
}
while(top) ri[st[top--]]=n;
for(int i=n;i>=1;i--)
{
while(top && a[st[top]]>a[i])
{
le[st[top]]=i+1;
top--;
}
st[++top]=i;
}
while(top) le[st[top--]]=1;*/
for(int i=;i<=n;i++)
{
if(a[i]>=) ans=max(ans,a[i]*(sum[ri[i]]-sum[le[i]-]));
else
{
if(le[i]==)
{
if(i==) ans=max(ans,a[i]*a[i]);
else ans=max(ans,a[i]*(qmi(,n,,i,ri[i])-max(0ll,qma(,n,,le[i],i-))));
}
else
{
ans=max(ans,a[i]*(qmi(,n,,i,ri[i])-qma(,n,,le[i]-,i-)));
}
}
}
printf("%lld\n",ans);
return ;
}

Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)的更多相关文章

  1. 2019The Preliminary Contest for ICPC China Nanchang National Invitational

    The Preliminary Contest for ICPC China Nanchang National Invitational 题目一览表 考察知识点 I. Max answer 单调栈+ ...

  2. 计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛

    Max answer Alice has a magic array. She suggests that the value of a interval is equal to the sum of ...

  3. The Preliminary Contest for ICPC China Nanchang National Invitational

    目录 Contest Info Solutions A. PERFECT NUMBER PROBLEM D. Match Stick Game G. tsy's number H. Coloring ...

  4. The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

    打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...

  5. 2019 The Preliminary Contest for ICPC China Nanchang National Invitational(A 、H 、I 、K 、M)

    A. PERFECT NUMBER PROBLEM 题目链接:https://nanti.jisuanke.com/t/38220 题意: 输出前五个完美数 分析: 签到.直接百度完美数输出即可 #i ...

  6. The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...

  7. The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

    题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...

  8. The Preliminary Contest for ICPC China Nanchang National Invitational I题

    Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...

  9. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

随机推荐

  1. js学习笔记 -- 函数

    js函数有类似javaMethod用法 Math.max.apply( Math.max.call( Array map,reduce,filter,sort , , , , , , , , ]; v ...

  2. 数据库mysql基础语言--各模式的含义

    1. 欢迎信息 欢迎来到 MySQL 镜像.命令以 ; 或 g 结束.你的 MySQL 连接编号为 2.服务器版本:5.1.47-社区 MySQL 社区服务器(GPL) 版权(C)2000.2010, ...

  3. ubuntu下安装wireshark(以及配置非root)

    https://jingyan.baidu.com/article/c74d60009d992f0f6a595de6.html Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网 ...

  4. 性能测试工具LoadRunner30-LR之监控Tomcat

    步骤: 1.通过LR去访问tomcat监控页(安装tomcat的过程可以百度一下) 2.然后通过关联取监控数据 3.通过lr_user_data_point()添加数据到图表中去 double ato ...

  5. React.js 小书 Lesson6 - 使用 JSX 描述 UI 信息

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson6 转载请注明出处,保留原文链接和作者信息. 这一节我们通过一个简单的例子讲解 React.j ...

  6. Jersey前后端交互初体验

    一 get请求 前端 基本的GET请求 $.ajax({ type : "get", url : "../rest/api/account/delete", d ...

  7. Windows Store 应用中获取程序集版本号的方法

    本文为个人博客备份文章,原文地址: http://validvoid.net/windows-store-app-get-assembly-version/ WinRT 中对反射做了很多限制,假设 W ...

  8. IIS设置问题

    1.解决IIS7.5中部署WCF时,访问.svc文件的404错误问题  如果你直接在IIS 7中配置WCF,访问.svc文件时会出现404错误.解决方法,以管理员身份进入命令行模式,运行:" ...

  9. 报表XtraReport创建是实现

    1.创建XtraReport报表程序 一般设计这个程序是分着的,为了方便我就把他们合到一起 首先创建一个Winform Application 在form1中放一个button,右键程序,添加新项 如 ...

  10. iOS 警告收集快速消除

    1.ld: warning: directory not found for option 去掉警告的方法 工程老是提示ld: warning: directory not found for opt ...