Cod sursa(job #2616575)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 18 mai 2020 22:42:11
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
using namespace std;

ifstream cin( "ssm.in" );
ofstream cout( "ssm.out" );

int read()
{
    int semn = 1;
    char x;
    cin.get( x );
    while( isspace( x ) )
        cin.get( x );

    if( x == '-' ){
        semn = -1;
        cin.get( x );
    }
    long long nr = 0;
    while( isdigit( x ) ){
        nr = nr * 10 + x - '0';
        cin.get( x );
    }
    return semn * nr;
}

int main()
{
    int n, x, ant, i, stmax, drmax, st;
    long long s, maxx;
    n = read();
    x = read();
    s = x;
    ant = x;
    maxx = -2000000000;
    stmax = 0;
    drmax = n - 1;
    st = 0;
    for( i = 1; i < n; i++ ){
        x = read();
        s = max( s + x, (long long)x );
        if( s == x )
            if( ant == 0 )
                st = i - 1;
            else
                st = i;
        //cout << st << ' ';
        if( maxx < ( s ) ){
            maxx = s, stmax = st, drmax = i;
        }
        ant = x;
    }
    cout << maxx << ' ' << stmax + 1 << ' ' << drmax + 1;
    return 0;
}