Pagini recente » Cod sursa (job #1079135) | Cod sursa (job #1323474) | Cod sursa (job #2947762) | Cod sursa (job #2809820) | Cod sursa (job #3259736)
#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;
}