Cod sursa(job #2800709)

Utilizator danutbodbodnariuc danut danutbod Data 13 noiembrie 2021 19:25:32
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
//var II programare dinamica sum[i] = Max(sum[i-1] + a[i], a[i]).
#include<fstream>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int n,i,maxi,x,sum,pi,pf,p;
int main()
{
    f>>n>>x;
    sum=x;p=1;maxi=-2000000000;
    for(i=2;i<=n;i++)
    {
        f>>x;
        if(sum>=0)sum+=x;
        else{ sum=x; p=i; }
        if(sum>maxi){maxi=sum;pi=p;pf=i;}
    }
    g<<maxi<<" "<<pi<<" "<<pf;
    return 0;
}
//
////var I  S[i] - Min(S[j]), j < i.
//#include<fstream>
//using namespace std;
//ifstream f("ssm.in");
//ofstream g("ssm.out");
//int n,i,maxi,mini,x,sum,pi,pf,p;
//int main()
//{
//    f>>n;
//    maxi=-2000000000;mini=0;p=1;
//    for(i=1;i<=n;i++)
//    {
//        f>>x;
//        sum=sum+x;
//        if(sum-mini>maxi){ maxi=sum-mini; pi=p; pf=i; }
//        if(sum<mini){ mini=sum; p=i+1; }
//    }
//    g<<maxi<<" "<<pi<<" "<<pf;
//    return 0;
//}