Submission #1358492


Source Code Expand

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

#define ll long long
#define ff first
#define ss second
typedef pair<int,int> ii;


int main()
{
    int n;
    cin>>n;

    stack<ii> pile;

    ll t[n];
    ll minLeft[n];
    ll minRight[n];

    for(int i=0;i<n;i++)
    {
        cin>>t[i];
        minRight[i]=i;

        while(!pile.empty() && pile.top().ff>=t[i])
        {
            minRight[pile.top().ss]=i;
            pile.pop();
        }

        if(pile.empty())
            minLeft[i]=-1;
        else
            minLeft[i]=pile.top().ss;
        pile.push(ii(t[i],i));
    }

    while(!pile.empty())
    {
        minRight[pile.top().ss]=n;
        pile.pop();
    }
    ll answer=0;

    for(int i=0;i<n;i++)
    {
        ll nb= (minRight[i]-i)*(i-minLeft[i]);
        answer+=nb*t[i];
    }

    cout<<answer;

}

Submission Info

Submission Time
Task B - Minimum Sum
User MohamedAmine
Language C++14 (GCC 5.4.1)
Score 400
Code Size 888 Byte
Status AC
Exec Time 64 ms
Memory 6528 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 64 ms 6528 KB
corner1 AC 62 ms 4992 KB
corner2 AC 1 ms 256 KB
corner3 AC 64 ms 5532 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 64 ms 4992 KB
maxrand1 AC 64 ms 4992 KB
maxrand2 AC 64 ms 4992 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 1 ms 256 KB