Cod sursa(job #3198496)

Utilizator vlad231Gheorghica Istrate David vlad231 Data 29 ianuarie 2024 17:22:02
Problema Subsecventa de suma maxima Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");
vector<int> v;
int n,x,S[7000011],poz;
int main() {
    fin>>n;
    for(int i=0;i<n;i++){
fin>>x;
v.push_back(x);
    }
    S[0]=v[0];
    for(int i=1;i<n;i++){

        if(S[i-1]+v[i]>=v[i])
            S[i]+=S[i-1]+v[i];
        else{
            S[i]=v[i-1];
        }
    }
    int max=INT_MIN;
    for(int i=0;i<n;i++){
        if(max<S[i])
            max=S[i];
        poz=i;
    }
    int pozs=poz;
    fout<<max<<" ";
    while(max!=0){
        max-=v[poz];
        poz--;

    }
    fout<<poz+2<<" "<<pozs+1;
    return 0;
}
/*
 *
 *
 */