Cod sursa(job #2844795)

Utilizator DooMeDCristian Alexutan DooMeD Data 5 februarie 2022 14:50:39
Problema Subsecventa de suma maxima Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nmax = 6e6;

ll v[nmax+5];
ll best[nmax+5];
int st[nmax+5];

int main () {
    ifstream f ("ssm.in");
    ofstream g ("ssm.out");

    int n; f >> n;
    for(int i=1; i<=n; i++) f >> v[i];
    ll ans = -1e18;
    int l, r;
    for(int i=1; i<=n; i++) {
        if(v[i] > best[i-1]+v[i]) {
            st[i] = i;
            best[i] = v[i];
        }
        else {
            st[i] = st[i-1];
            best[i] = best[i-1] + v[i];
        }
        if(best[i] > ans) {
            ans = best[i];
            l = st[i];
            r = i;
        }
    }
    g << ans << " " << l << " " << r;
    return 0;
}