Max answer

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 \le n \le 5 \times 10 ^5n(1≤n≤5×105).

Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−105≤ai​≤105).

Output

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

样例输入复制

5
1 2 3 4 5

样例输出复制

36

用单调栈判断以每个值为最小值的最大左边界和右边界。后对对每个值分成负数和正数讨 论取可行区间内的最小或最大值,方法为求前缀和和后缀和,然后用线段树求区间最值。

线段树维护的时候,左区间维护后缀和,右区间维护前缀和,找的时候,在i值左边找后缀,在i值右边找前缀,然后交叉的部分就是满足的区间。

代码:

 //I-线段树+单调栈
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
const int inf=0x3f3f3f3f; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int a[maxn],l[maxn],r[maxn];
ll pre[maxn],beh[maxn],maxl[maxn<<],minl[maxn<<],maxr[maxn<<],minr[maxn<<]; void pushup(int rt)
{
maxl[rt]=max(maxl[rt<<],maxl[rt<<|]);
minl[rt]=min(minl[rt<<],minl[rt<<|]);
maxr[rt]=max(maxr[rt<<],maxr[rt<<|]);
minr[rt]=min(minr[rt<<],minr[rt<<|]);
} void build(int l,int r,int rt)
{
if(l==r){
maxl[rt]=minl[rt]=beh[l];
maxr[rt]=minr[rt]=pre[l];
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} ll query(int op,int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
if (op==) return maxl[rt];
else if(op==) return minl[rt];
else if(op==) return maxr[rt];
else if(op==) return minr[rt];
} int m=(l+r)>>;
ll ret;
if(op==||op==){
ret=-inf;
if(L<=m) ret=max(ret,query(op,L,R,lson));
if(R> m) ret=max(ret,query(op,L,R,rson));
}
else if(op==||op==){
ret=inf;
if(L<=m) ret=min(ret,query(op,L,R,lson));
if(R> m) ret=min(ret,query(op,L,R,rson));
}
return ret;
} deque<int> deq;//因为是双端队列,所以插的时候要插到头上才能实现栈的功能 int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
pre[i]=pre[i-]+a[i];
}
for(int i=n;i>=;i--){
beh[i]=beh[i+]+a[i];
}
build(,n,);
for(int i=;i<=n;i++){
while(deq.size()&&a[deq.front()]>=a[i]) deq.pop_front();
if(deq.empty()) l[i]=;
else l[i]=deq.front()+;
deq.push_front(i);
}
deq.clear();
for(int i=n;i>=;i--){
while(deq.size()&&a[deq.front()]>=a[i]) deq.pop_front();
if(deq.empty()) r[i]=n;
else r[i]=deq.front()-;
deq.push_front(i);
}
ll maxx=-inf,ret;
for(int i=;i<=n;i++){
if(a[i]>=){
ret=query(,l[i],i,,n,);
ret+=query(,i,r[i],,n,);
ret=ret-beh[i]-pre[i]+a[i];
ret*=a[i];
// cout<<query(1,l[i],i,1,n,1)<<" "<<query(3,i,r[i],1,n,1)<<endl;
}
else{
ret=query(,l[i],i,,n,);
ret+=query(,i,r[i],,n,);
ret=ret-beh[i]-pre[i]+a[i];
ret*=a[i];
}
maxx=max(maxx,ret);
}
printf("%lld\n",maxx);
}

最新文章

  1. My97DatePicker日期插件
  2. check_env函数解析
  3. Delphi中线程类TThread实现多线程编程1---构造、析构……
  4. Head First 设计模式 --4 工厂模式 抽象工厂模式
  5. Java Socket发送与接收HTTP消息简单实现
  6. DB天气app冲刺第四天
  7. CodeForces 605B Lazy Student
  8. python定时利用QQ邮件发送天气预报
  9. npm vue ivew vue-cli3
  10. leetcode — single-number-ii
  11. 《Java》第三周学习总结 20175301
  12. 小程序 模态对话框自定义组件(modal)
  13. Linux中设置别名
  14. 前端工程化-webpack(打包JS)(二)
  15. Java高级面试题及答案
  16. [UE4]添加射击的准心
  17. 【BZOJ1502】【NOI2005】月下柠檬树
  18. (转)Inno Setup入门(八)——有选择性的安装文件
  19. html 线条重叠变粗
  20. HDU 3037 组合数、lucas,逆元

热门文章

  1. java之mybatis整合spring
  2. java之mybatis之查询及分页
  3. C# 截取字符串方法总结
  4. 自己用JQueryUI封装了几个系统常用对话框
  5. Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)
  6. VLC架构及流程分析
  7. 安装nginx + nginx-gridfs + mongodb
  8. Android笔记(七十五) Android中的图片压缩
  9. IDA7.0安装findcrypt插件
  10. mysql where 1