Cod sursa(job #2768044)

Utilizator ililogIlinca ililog Data 9 august 2021 10:46:58
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;

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

int n;
int v[6000001];
int stmax, drmax, summax = INT_MIN;

int main() {
    
    fin >> n;
    for (int i = 1; i<=n; i++) {
        fin >> v[i];
    }
    
    int sumcurr = 0;
    int st = 1, dr = 0;
    for (int i = 1; i<=n; i++) {
        sumcurr += v[i];
        
        if (sumcurr > summax) {
            summax = sumcurr;
            stmax = st;
            drmax = i;
        } else if (sumcurr == summax) {
            if (st < stmax) {
                stmax = st;
                drmax = i;
            } 
        }
        
        if (sumcurr < 0) {
            sumcurr = 0;
            st = i+1;
        }
    }
    
    fout << summax << " " << stmax << " " << drmax;
    
    return 0;
}