Cod sursa(job #2180326)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 20 martie 2018 19:53:18
Problema Operatii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.55 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;

ll 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();
        }

        St.push(x);
    }

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

	return 0;
}