Cod sursa(job #2848069)

Utilizator Luca_CristianZamfir Luca-Cristian Luca_Cristian Data 12 februarie 2022 09:44:08
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("ssm.in");
ofstream fout("ssm.out");

#define INF -99999999

int main()
{
    int n, x, i;


    fin >> n;

    int suma = 0, sum_max = 0, st = 1, dr = 0, st_rez = 1, dr_rez = 0, pozitive = 0;
    int e_maxim = INF, poz_max;
    for(i = 1; i <= n; i++)
    {
        fin >> x;

        suma += x;
        dr++;

        if(suma < 0)
        {
            suma = 0;
            st = i + 1;
        }
        if(suma > sum_max)
        {
           sum_max = suma;
           dr = i;
           st_rez = st;
           dr_rez = dr;
        }

        if(x > 0)
            pozitive++;
        if(x > e_maxim)
        {
            e_maxim = x;
            poz_max = i;
        }

    }

    if(pozitive > 0)
        fout << sum_max << " " << st_rez << " " << dr_rez;
    else
        fout << e_maxim << " " << poz_max << " " << poz_max;


    return 0;
}