Cod sursa(job #1633626)

Utilizator AnduB08Boldisor Andu AnduB08 Data 6 martie 2016 12:33:14
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int v[6000010],n,st,dr,i,s,ss,stt,drr,ok;
int main()
{
    f>>n;
    for(i=1;i<=n;++i)
        f>>v[i];
    for(i=1;i<=n;++i)
    {
        if(v[i]>=0)
        {
            ok=1;
            break;
        }
    }
    if(ok==0)
    {
    st=1;
    dr=1;
    ss=-2000000000;
    for(i=1;i<=n;++i)
    {
        if(s<0)
        {
            s=0;
            st=i;
            dr=i;
        }
            s=s+v[i];
            if(ss<s)
            {
                ss=s;
                stt=st;
                drr=dr;
            }
    }
    g<<ss<<" "<<stt<<" "<<drr;
    }
    else
    {

    st=1;
    dr=1;
    ss=-2000000000;
    for(i=1;i<=n;++i)
    {
        if(s<0)
        {
            s=v[i];
            st=i;
        }
        else
        {
            s=s+v[i];
            if(ss<s)
            {
                ss=s;
                stt=st;
                dr=i;
            }
        }
    }
    g<<ss<<" "<<stt<<" "<<dr;
    }
    return 0;
}