Submission #9639257


Source Code Expand

// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <bits/stdc++.h>
#define re register
using namespace std;
typedef long long ll;

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=200000+10;

int n,a[N];
int L[N],R[N];
int sta[N],top=0;

int main() {
    n=read();
    for (re int i=1;i<=n;++i) a[i]=read();
    a[0]=0,sta[top=1]=0;
    for (re int i=1;i<=n;++i) {
        while (top&&a[i]<a[sta[top]]) --top;
        L[i]=sta[top],sta[++top]=i;
    }
    a[n+1]=0,sta[top=1]=n+1;
    for (re int i=n;i;--i) {
        while (top&&a[i]<a[sta[top]]) --top;
        R[i]=sta[top],sta[++top]=i;
    }
    ll ans=0;
    for (re int i=1;i<=n;++i) ans+=1ll*a[i]*(i-L[i])*(R[i]-i);
    printf("%lld\n",ans);
    return 0;
}

Submission Info

Submission Time
Task B - Minimum Sum
User M_sea
Language C++14 (GCC 5.4.1)
Score 400
Code Size 958 Byte
Status AC
Exec Time 17 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 14 ms 3328 KB
corner1 AC 14 ms 3328 KB
corner2 AC 1 ms 256 KB
corner3 AC 15 ms 2944 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 17 ms 2560 KB
maxrand1 AC 17 ms 2560 KB
maxrand2 AC 17 ms 2560 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 1 ms 256 KB