Cod sursa(job #3252778)

Utilizator Arunn_AzyzAnichitoaie Arun Arunn_Azyz Data 30 octombrie 2024 23:51:37
Problema Subsecventa de suma maxima Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fi("ssm.in");
ofstream fo("ssm.out");
int v[100005],sp[100005];
int main()
{
   int N;
   fi>>N;
   for(int i=1;i<=N;i++){
    fi>>v[i];
   }
   sp[1]=v[1];
   for(int j=2;j<=N;j++){
    sp[j]=sp[j-1]+v[j];
   }
   int smax=-200000,st=1,dr=N;
   for(int i=1;i<=N;i++){
    for(int j=i;j<=N;j++){
        int S=sp[j]-sp[i-1];
        if(S>smax){
            smax=S;
            st=i;
            dr=j;
        }
    }
   }
   fo<<smax<<" "<<st<<" "<<dr;
}