Cod sursa(job #2611160)

Utilizator natrovanCeval Marius natrovan Data 6 mai 2020 15:10:57
Problema Subsecventa de suma maxima Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
///T5
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");

long long x, s, smax, n, poz, v[1000000], l , lmax;
int main()
{
    fin >>n; fin >> v[1]; poz = 1;
    s = smax = v[1]; l = lmax = 1;
    for(int i = 2; i <= n; i++){
        fin >> v[i];
        if(v[i]>0){
            if(s<0){
                s = v[i];
                l = 1;
            } else {
                s+= v[i];
                l++;
            }
        } else {
            s += v[i]; l++;
        }
        if(s > smax){
            smax = s;
            poz  = i;
            lmax = l;
        }
    }

    int i = poz - lmax + 1;
    fout << smax << ' ' << i << ' ' << poz;

    fin.close(); fout.close();
    return 0;
}