Pagini recente » Cod sursa (job #2111760) | Cod sursa (job #2356757) | Cod sursa (job #1077331) | Cod sursa (job #660050) | Cod sursa (job #2180318)
#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;
int 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();
}
if (St.empty() || x >= St.top()){
St.push(x);
continue;
}
}
cout << rs + (St.empty() ? 0 : St.top());
return 0;
}