Cod sursa(job #2345069)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 15 februarie 2019 21:14:55
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>

using namespace std;

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

const int NMAX = 6000005;

int N;

int best = -2000000000, val;
int dp;
int lg;
int lf, rg;

int main()
{
    fin >> N;

    for( int i = 1; i <= N; ++i )
    {
      fin >> val;

      if( dp > 0 )
      {
        dp += val;
        lg++;
      }
      else
      {
        dp = val;
        lg = 1;
      }

      if( dp > best )
      {
        best = dp;

        rg = i;
        lf = i - lg + 1;
      }
    }

    fout << best << ' ' << lf << ' ' << rg << '\n';

    return 0;
}