Cod sursa(job #3245108)

Utilizator Iancu007Sandea Iancu-Ioan Iancu007 Data 27 septembrie 2024 15:55:50
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>

using namespace std;
ifstream cin("ssm.in");
ofstream cout("ssm.out");
long long s[6000005];
int main()
{
    int n;
    cin>>n;
    long long a;
    int minpoz=0;
    int ri=1, rj=1;
    long long rs=-10000000000;
    bool ok=0;
    long long minn=10000000000;
    for (int i=1; i<=n; i++)
    {
        cin>>a;
        if (!ok && a<minn)
        {
            minn=a;
            ri=rj=i;
        }
        s[i]=s[i-1]+a;
        if (s[i]<s[minpoz])
            minpoz=i;
        if (s[i]-s[minpoz]>rs && minpoz!=i)
        {
            ri=minpoz+1;
            rj=i;
            rs=s[i]-s[minpoz];
            ok=1;
        }
    }
    if (!ok)
        cout<<minn<<' '<<ri<<' '<<rj;
    else
        cout<<rs<<' '<<ri<<' '<<rj;
    return 0;
}