Submission #1258756


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

const int DIM = 2e5 + 5;

vector<int> stk;
int arr[DIM], lef[DIM], rig[DIM];

int main( void ) {
    
    int n;
    cin >> n;
    
    for( int i = 1; i <= n; i ++ )
        cin >> arr[i];
    
    stk.assign( 1, 0 );
    for( int i = 1; i <= n; i ++ ) {
        while( arr[i] <= arr[stk.back()] )
            stk.pop_back();
        
        lef[i] = stk.back() + 1;
        stk.push_back( i );
    }
    
    stk.assign( 1, n + 1 );
    for( int i = n; i >= 1; i -- ) {
        while( arr[i] <= arr[stk.back()] )
            stk.pop_back();
        
        rig[i] = stk.back() - 1;
        stk.push_back( i );
    }
    
    long long ans = 0;
    for( int i = 1; i <= n; i ++ )
        ans += 1LL * arr[i] * ( i - lef[i] + 1 ) * ( rig[i] - i + 1 );
    
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task B - Minimum Sum
User EmanuelNrx
Language C++14 (GCC 5.4.1)
Score 400
Code Size 886 Byte
Status AC
Exec Time 82 ms
Memory 3448 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 78 ms 3444 KB
corner1 AC 79 ms 3448 KB
corner2 AC 1 ms 256 KB
corner3 AC 80 ms 3064 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 80 ms 2560 KB
maxrand1 AC 82 ms 2560 KB
maxrand2 AC 80 ms 2560 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 1 ms 256 KB