Cod sursa(job #1617905)

Utilizator Balescu_OvidiuBalescu Ovidiu-Gheorghe Balescu_Ovidiu Data 27 februarie 2016 17:05:55
Problema Subsecventa de suma maxima Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <stdio.h>
#include <stdlib.h>
 
int main(){
    unsigned long n,new_i,max_i=0,max_j=0;
    FILE*f=fopen("ssm.in","r");
    FILE*g=fopen("ssm.out","w");
    fscanf(f,"%lu",&n);
    long s,sum,best;
	fscanf(f,"%ld",&s);
	sum=best=s;
    for(unsigned long i=1;i<n;i++){
        fscanf(f,"%ld",&s);
		if(sum>0)
			sum+=s;
		else{
			sum=s;
			new_i=i;
		}
		if(best<sum){
			best=sum;
			max_i=new_i;
			max_j=i;
		}
    }
    fprintf(g,"%ld %lu %lu",best,max_i+1,max_j+1);
    fclose(f);
    fclose(g);
    return 0;
}