Cod sursa(job #2611069)

Utilizator natrovanCeval Marius natrovan Data 6 mai 2020 12:05:50
Problema Subsecventa de suma maxima Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 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++;}

            if(s > smax){
                    smax = s;
                    poz  = i;
                    lmax = l;
            }
        } else
        {
            s += v[i];
            l++;
        }

    }


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

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