Pagini recente » Cod sursa (job #267807) | Cod sursa (job #3254306) | Cod sursa (job #1492859) | Cod sursa (job #2986761) | Cod sursa (job #2180326)
#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;
ll 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();
}
St.push(x);
}
cout << rs + (St.empty() ? 0 : St.top());
return 0;
}