Cod sursa(job #2076953)

Utilizator gruhtenZinnenberg Gruhten gruhten Data 27 noiembrie 2017 15:05:55
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include<stack>
#define dim 1000004
using namespace std;

stack <int> v;
int x[dim];

int main()
{
    ifstream f("concurs.in");
    ofstream g("concurs.out");
    long long aria,Amax=0;
    int i,j,N,y;

    f>>N;
    for(i=1;i<=N;++i)
      f>>x[i];

    for(i=1;i<=N;++i)
    {
        if(v.empty() or x[i]>x[v.top()])
        {
            v.push(i);
        }
        else if(x[i]<x[v.top()])  //avem un element de val. mai mica
        {
            while(!v.empty() and x[i]<x[v.top()])
            {
                y=v.top();
                v.pop();
                aria=(i-y)*x[y];
                if(Amax<aria)
                   Amax=aria;
            }
        }
    }

    while(!v.empty() )
    {
                y=v.top();
                v.pop();
                aria=(i-y)*x[y];
                if(Amax<aria)
                   Amax=aria;
    }

g<<Amax<<'\n';

    return 0;
}