Pagini recente » Cod sursa (job #3271911) | Cod sursa (job #1078370) | Cod sursa (job #1078407) | Cod sursa (job #2714310)
#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;
}