Cod sursa(job #2990009)

Utilizator rosca.crisRosca Cristian rosca.cris Data 7 martie 2023 13:23:51
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("ssm.in");
ofstream fout("ssm.out");

int main(){
    int lenght;
    fin >> lenght;
    int nums[lenght + 1]; nums[0] = 0;
    for(int i = 1; i <= lenght; ++i){
        int num;
        fin >> num;
        nums[i] = nums[i - 1] + num;
    }
    int max_sum = 0;
    int max_str, max_end;
    for(int i = 0; i < lenght; ++i){
        //cout << i << '\n';
        for(int l = 1; l <= lenght; ++l){
            nums[l] -= nums[i];
            //cout << "--|" << l << ' ' << nums[l] << '\n';
            if(nums[l] > max_sum){
                max_sum = nums[l];
                max_str = i + 1;
                max_end = l;
            }else if(nums[l] == max_sum){
                if(max_str == i){
                    if(l - i < max_end - (max_str - 1)){
                        max_end = l;
                    }
                }
            }
        }
    }
    fout << max_sum << ' ' << max_str << ' ' << max_end;
}