Cod sursa(job #2915197)

Utilizator alin.gabrielAlin Gabriel Arhip alin.gabriel Data 22 iulie 2022 01:32:58
Problema Operatii Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <fstream>
#include <string>
using namespace std;

long long findMax(int *v, int n) {
    int lmax = 0;
    long long gmax = 0;

    for (int i = 1; i < n; i++) {
        if (v[i] == 0) {
            gmax += lmax;
            lmax = 0;
        } else if (lmax <= v[i]) {
            lmax = v[i];
        } else {
            gmax += lmax - v[i];
            lmax = v[i];
        }
    }

    gmax += lmax;

    return gmax;
}

int main() {
    ifstream fin("operatii.in");
    ofstream fout("operatii.out");
    
    int n;
    fin >> n;

    int v[n];
    for (int i = 0; i < n; i++)
        fin >> v[i];
 
    fout << findMax(v, n);

    return 0;
}