Cod sursa(job #3204521)

Utilizator Bogdan345Marius Mihalache Bogdan345 Data 16 februarie 2024 22:12:50
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include<fstream>
#include <vector>
using namespace std;
ifstream cin("ssm.in");
ofstream cout("ssm.out");
vector<int>v;
int main(){
int n;
cin>>n;
v.resize(n+1);
for(int i=1;i<=n;i++){
    cin>>v[i];
}
long long s=0;
int iI=1,jI=-1,iF=-1,jF=-1;
int maxx=-1;
for(int i=1;i<=n;i++){
    s+=v[i];
    jI=i;
    if(s>=0){
        if(maxx<s){
            iF=iI;
            jF=jI;
            maxx=s;
        }
    }else{
        s=0;
        iI=i+1;
    }
}
if(jF!=-1){
cout<<maxx<<" "<<iF<<" "<<jF;
}else{
    maxx=-1e9;
    for(int i=1;i<=n;i++){
        if(maxx<v[i]){
            maxx=v[i];
            iF=i;
            jF=i;
        }
    }
    cout<<maxx<<" "<<iF<<" "<<jF;
}
}