Cod sursa(job #3275069)

Utilizator maxtraAlex Deonise maxtra Data 9 februarie 2025 10:29:36
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
using namespace std;

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

int main(void) {
    int sc_i = 0, st = 1, st_max = 1, dr_max = 1, s_max = INT_MIN;
    /*
    sc_i = suma curentă până la elementul i
    st = începutul secvenței curente
    st_max, dr_max = începutul și sfârșitul celei mai bune secvențe găsite
    s_max = suma maximă găsită, inițializată cu cel mai mic număr posibil (INT_MIN
    */
    
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> x;
        
        if(x > s_max + x) {
            s = x;
            st = i;
           
        }
        else {
             s_max += x;
        }
        
        if (sc_i > s_max) {
            s_max = sc_i;
            st_max = st;
            dr_max = i;
        }
    }
    
    fout << s_max << " " << st_max << " " << dr_max;
    
    return 0;
}