Cod sursa(job #2408217)

Utilizator KonnayDinu Marius Valentin Konnay Data 17 aprilie 2019 18:48:35
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <stack>

using namespace std;

ifstream in ("strabunica.in");
ofstream out ("strabunica.out");

int a[200010];
int dr[200010];
int st[200010];

stack <int>s;

int main()
{
    int n;
    in>>n;
    for (int i=1;i<=n;i++){
        in>>a[i];
    }
    for (int i=1;i<=n;i++){
        while (!s.empty()&&a[s.top()]>a[i]){
            int now=s.top();
            s.pop();
            dr[now]=i;
        }
        s.push(i);
    }
    while (!s.empty()){
        int now=s.top();
        s.pop();
        dr[now]=n+1;
    }

    for (int i=n;i>=1;i--){
        while (!s.empty()&&a[s.top()]>a[i]){
            int now=s.top();
            s.pop();
            st[now]=i;
        }
        s.push(i);
    }
    while (!s.empty()){
        int now=s.top();
        s.pop();
        st[now]=0;
    }
    long long MAX=0;
    for (int i=1;i<=n;i++){
        MAX = max(MAX,1LL*a[i]*(dr[i]-st[i]-1));
    }
    out<<MAX;
    return 0;
}