Cod sursa(job #2428158)

Utilizator Ionut28Porumb Palincas Ionut Ionut28 Data 4 iunie 2019 06:08:50
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");
const int nmax = 6000005;
int v[nmax], n, x, y, maxim = -1,copie, minim = 10000000, lg;
long long int s;
bool ok;
int main()
{
    fin >> n;
    for(int i = 1; i <= n; ++i)
        fin >> v[i];
    ok = true;
    for(int i = 1; i <= n; ++i)
    {
        s += v[i];
        if(ok)
        {
                x = i;
                ok = false;
        }
        if(s > maxim)
        {
            maxim = s;
            y = i;
            copie = x;

        }
        else if(s == maxim)
        {
            if(i - x < minim)
            {
                minim = i - x;
                y = i;
                copie = x;
            }
        }
        if(s < 0)
        {
            ok = true;
            s = 0;
        }
    }
    fout << maxim << " " << copie << " " << y << "\n";
    return 0;
}