Cod sursa(job #1042384)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 26 noiembrie 2013 22:48:35
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream input("ssm.in");
ofstream output("ssm.out");

int N , x;

int S = 0;
int poz1 , poz2;

int SMAX = -1 * ( 1 << 20);
int poz1_max , poz2_max;


int main()
{
    input >> N;
    poz1 = 1;
    poz2 = 0;


    for (int i = 1; i <= N; i++)
    {
        input >> x;
        S += x;
        if (S > SMAX)
        {
            SMAX = S;
            poz1_max = poz1;
            poz2_max = i;
        }
        else if (S == SMAX)
        {
            if (poz1 < poz1_max)
            {
                poz1_max = poz1;
                poz2_max = i;
            }
            else if (poz1 == poz1_max && i - poz1 + 1 < poz2_max - poz1_max + 1)
            {
                poz1_max = poz1;
                poz2_max = i;
            }
        }
        if (S < 0)
        {
            S = 0;
            poz1 = i + 1;
        }
        else if (S < x)
        {
            poz1 = i;
            S = x;
        }
    }
    output << SMAX << " " << poz1_max << " " << poz2_max;
    input.close();
    output.close();
    return 0;
}