Pagini recente » Cod sursa (job #3172585) | Cod sursa (job #2784337) | Cod sursa (job #3170233) | Cod sursa (job #120957) | Cod sursa (job #1726618)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <math.h>
using namespace std;
#define ll long long
#define llu long long unsigned
#define pb push_back
#define mp make_pair
string problemName = "operatii";
string inFile = problemName+".in";
string outFile = problemName+".out";
ifstream fin(inFile.c_str());
ofstream fout(outFile.c_str());
int main(){
int n,i,mn,x,mx,y,ds;
llu sol;
char type;
fin>>n;
mn = 1e9;
mx = 0;
sol = 0;
ds = 0;
y = 0;
for(i = 1;i <= n;i++){
fin>>x;
if(x == 0){
sol += mx;
mn = 1e9;
mx = 0;
ds = 0;
y = 0;
type = 'n';
}else{
x -= ds;
if(y == 0){
y = x;
}
if(x > y){
if(type == 'a'){
mn = min(x, mn);
mx = max(x, mx);
}else if(type == 'd'){
ds += mn;
sol += mx;
x -= mn;
mn = x;
mx = x;
type = 'n';
}else{
type = 'a';
mn = min(x, mn);
mx = max(x, mx);
}
}else if(x < y){
if(type == 'd'){
mn = min(x, mn);
mx = max(x, mx);
}else if(type == 'a'){
ds += mn;
sol += mx;
x -= mn;
mn = x;
mx = x;
type = 'n';
}else{
type = 'd';
mn = min(x, mn);
mx = max(x, mx);
}
}else{
mn = min(x, mn);
mx = max(x, mx);
}
y = x;
}
}
sol += mx;
fout<<sol;
return 0;
}