Cod sursa(job #2714310)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 1 martie 2021 17:53:03
Problema Operatii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.51 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("operatii.in");
ofstream fout("operatii.out");

stack<int> st;

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

    long long rez = 0;
    st.push(0);
    for(int i = 1; i <= n; ++i)
    {
        int val;
        fin >> val;

        if(st.top() < val)
            rez += val - st.top(), st.pop();
        while(!st.empty() && st.top() <= val)
            st.pop();
        st.push(val);
    }
    fout << rez << '\n';
    return 0;
}