Cod sursa(job #3268740)

Utilizator jumaracosminJumara Cosmin-Mihai jumaracosmin Data 16 ianuarie 2025 22:20:08
Problema Subsecventa de suma maxima Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

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

ll sum, sum_max;
int x, st, dr, n, st_max, dr_max, lg, lg_max;

int main()
{
    fin >> n >> x;
    sum = sum_max = x;
    st = dr = st_max = dr_max = lg = lg_max = 1;
    for(int i = 2; i <= n; ++i)
    {
        fin >> x;

        if(sum + x > 0)
            sum += x, lg++, dr = i;
        else
            sum = x, lg = 1, st = i, dr = i;

        if(sum > sum_max)
            sum_max = sum, st_max = dr - lg + 1, dr_max = dr, lg_max = lg;

        else if(sum == sum_max)
            if(st < st_max)
                st_max = dr - lg + 1, dr_max = dr, lg_max = lg;

            else if(st == st_max && lg < lg_max)
                dr_max = dr, lg_max = lg;

    }
    fout << sum_max << " " << st_max << " " << dr_max;
    return 0;
}