Cod sursa(job #2711167)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 23 februarie 2021 18:45:47
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.01 kb
#include <bits/stdc++.h>

using namespace std;

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

int a[6000005];

int main()
{
    int n,i,s = 0,smax = -1,start = 1,finish = 1,finmax,startmax,j,sret,maxim = 2147483647,pozmax,last_one;
    in >> n;
    for (i = 1; i <= n; i++)
    {
        in >> a[i];
        if (a[i] >= maxim)
        {
            maxim = a[i];
            pozmax = i;
        }
    }
    if (maxim >= 0)
    {
        cout << 1;
        for (i = 1; i <= n; i++)
        {
            if (a[i] >= 0)
            {
                s += a[i];
                if (s > smax)
                {
                    smax = s;
                    startmax = start;
                    finmax = i;
                }
                finish++;
                if (a[i] == 0)
                    last_one = i - 1;
            }
            else
            {
                j = i;
                sret = s;
                while (i <= n and s > 0)
                {
                    s += a[i];
                    i++;
                    if (s > smax)
                    {
                        smax = s;
                        startmax = start;
                        finmax = i;
                    }
                }
                if (i == n + 1)
                {
                    out << s << " " << start << " " << n;
                    return 0;
                }
                else
                {
                    if (s > smax)
                    {
                        smax = s;
                        startmax = start;
                        finmax = j;
                        start = finish = i;
                        s = 0;
                        i--;

                    }
                }
            }
        }
        if (a[finmax] == 0)
            finmax = last_one;
        out << smax << " " << startmax << " " << finmax;
    }
    else
        out << a[pozmax] << " " << pozmax << " " << pozmax;
    return 0;
}