Cod sursa(job #3149423)

Utilizator sorinturdaSorin Turda sorinturda Data 8 septembrie 2023 15:19:00
Problema Subsecventa de suma maxima Scor 95
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <stdio.h>
#include <stdlib.h>


int main() {
    FILE *f = fopen("ssm.in","r");
    int n,x, L=0,Lmax=0, s=0, smax=0, u;
    fscanf(f,"%d",&n);
    for(int i=0; i<n; i++) {
        fscanf(f,"%d",&x);
        if(s < 0)
            s = x, L=1;
        else{
            s += x;
            L++;
        }
        if(smax < s){
            smax = s;
            Lmax = L;
            u = i+1;
        }
        else
         if(smax == s){
             if(L < Lmax){
                Lmax = L;
                u = i+1;
             }
         }


    }
    fclose(f);
    f = fopen("ssm.out","w");
    fprintf(f,"%d %d %d",smax, u-Lmax+1, u);
    fclose(f);
    return 0;
}