Cod sursa(job #3259736)

Utilizator 0021592Grecu rares 0021592 Data 27 noiembrie 2024 18:03:22
Problema Operatii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include <stack>
#define int long long
using namespace std;
ifstream in("operatii.in");
ofstream out("operatii.out");
int n, i, j, x, wh, op;
int aux;
stack <int> st;
int32_t main()
{
    in >> n;
    for (i = 1; i <= n; i++)
    {
        in >> x;
        if (x == 0)
        {
            while(st.size())
                st.pop();
            continue;
        }
        if (!st.size())
        {
            st.push(x);
            continue;
        }
        while(st.size() && st.top() > x)
        {
            ///sa zicem ca 2, 4, vrem o operatie
            aux = st.top();
            st.pop();
            if (st.empty())
                wh = 0;
            else
                wh = st.top();
            op += aux-wh;
        }
        if (st.empty())
            st.push(x);
        else if (st.top() != x)
            st.push(x);
    }
    while(st.size())
    {
        ///sa zicem ca 2, 4, vrem o operatie
        aux = st.top();
        st.pop();
        if (st.empty())
            wh = 0;
        else
            wh = st.top();
        op += aux-wh;
    }
    out << op;
    return 0;
}