题目大意:给出一个数组,每次操作可以删除一个数,得到这个数左右两个数的最小值的权值(如果左右两侧不全,权值为0),问最大得到多少?
首先,如果是左右两个数均不比中间的数小(有一侧大于,一侧相等也可以),那么可以直接取中间的数,权值加上左右两侧的最小值,这样操作完成后,得到的数组,递增,递减,先增后减,最大的两个数是紧挨着的,一定取不到,能取到的是除去最大的两个数的其他所有的和
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define LL __int64
LL p[600000] , top ;
int main()
{
LL i , x , n , ans ;
top = -1 ; ans = 0 ;
scanf("%I64d", &n);
while(n--)
{
scanf("%I64d", &x);
while( top >= 1 && p[top] <= p[top-1] && p[top] <= x )
{
ans += min( p[top-1],x );
top-- ;
}
p[++top] = x ;
}
sort(p,p+(top+1));
for(i = 0 ; i < top-1 ; i++)
ans += p[i] ;
printf("%I64d\n", ans);
return 0;
}