Cod sursa(job #2180318)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 20 martie 2018 19:46:52
Problema Operatii Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair< int , int > PII;

int n, rs;
stack < int > St;


int main(){
    ifstream cin("operatii.in");
    ofstream cout("operatii.out");

    cin >> n;
    for (int i = 1, x; i <= n; i++){
        cin >> x;

        if (St.size() && x < St.top()){
            rs += St.top() - x;
            while (St.size() && St.top() > x) St.pop();
        }

        if (St.empty() || x >= St.top()){
            St.push(x);
            continue;
        }
    }

    cout << rs + (St.empty() ? 0 : St.top());

	return 0;
}