Submission #1690845


Source Code Expand

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

const int maxn=2e5+10;
int n,a[maxn],stk[maxn],top,l[maxn],r[maxn];
long long ans;

int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>a[i];
	for(int i=1;i<=n;++i){
		while(top&&a[stk[top-1]]>a[i])
			r[stk[--top]]=i-1;
		if(!top)
			l[i]=1;
		else
			l[i]=stk[top-1]+1;
		stk[top++]=i;
	}
	for(int i=0;i<top;++i)
		r[stk[i]]=n;
	for(int i=1;i<=n;++i)
		ans+=(long long)(r[i]-i+1)*(i-l[i]+1)*a[i];
	cout<<ans<<endl;
	return 0;
}

Submission Info

Submission Time
Task B - Minimum Sum
User guo
Language C++14 (GCC 5.4.1)
Score 400
Code Size 603 Byte
Status AC
Exec Time 23 ms
Memory 3328 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 13
Set Name Test Cases
Sample example0, example1, example2
All corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2
Case Name Status Exec Time Memory
corner0 AC 20 ms 3328 KB
corner1 AC 20 ms 2560 KB
corner2 AC 1 ms 256 KB
corner3 AC 21 ms 2944 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 23 ms 2560 KB
maxrand1 AC 23 ms 2560 KB
maxrand2 AC 23 ms 2560 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 1 ms 256 KB