Cod sursa(job #3229745)

Utilizator gabriel.9619Gabriel Stefan Tita gabriel.9619 Data 17 mai 2024 11:53:56
Problema Subsecventa de suma maxima Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
#include <climits>
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");
int main()
{
    int n, i, s=0, x, smax=-INT_MAX, st, stmax, dr, drmax;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>x;
        if(s+x>=0)
        {
            s+=x;
            dr=i;
        }
        else
        {
            st=dr=i;
            s=x;
        }
        if(s>smax)
        {
            smax=s;
            drmax=dr;
            stmax=st;
        }
        else
        if(s==smax)
        {
            if(st<stmax)
            {
                stmax=st;
                drmax=dr;
            }
            else
            if(st==stmax)
            {
                if(drmax-stmax>dr-st)
                {
                    drmax=dr;
                    stmax=st;
                }
            }
        }
    }
    fout<<smax<<" "<<stmax<<" "<<drmax;
}