Cod sursa(job #2742946)

Utilizator LucaMuresanMuresan Luca Valentin LucaMuresan Data 22 aprilie 2021 12:59:18
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

ifstream in ( "ssm.in" ) ;

ofstream out ( "ssm.out" ) ;

int n, i, x, maxim, S, st, stmax, drmax ;

int main()
{
    in >> n ;
    in >> x ;
    S = x ;
    maxim = x ;
    stmax = 1 ;
    drmax = 1 ;
    for ( i = 2 ; i <= n ; i ++ )
    {
        in >> x ;
        if ( S > 0 )
        {
            S = S + x ;
        }
        else
        {
            S = x ;
            st = i ;
        }
        if ( S > maxim )
        {
            maxim = S ;
            stmax = st ;
            drmax = i ;
        }
        if ( S == maxim )
        {
            if ( i - st < drmax - stmax )
            {
                stmax = st ;
                drmax = i ;
            }
        }
    }
    out << maxim << " " << stmax << " " << drmax  ;
    return 0;
}