Cod sursa(job #2668717)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 5 noiembrie 2020 11:05:23
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;

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

int nr;
int maxsum = -2000000009;
int n;
int ind1, ind2;
int summ;
int nrpoz;
int nrmax=-2000000009,poznrmax;
int ind1max, ind2max;

int main()
{
    f >> n;
    ind1 = 1;
    ind2 = 1;
    for (int i=1;i<=n;i++) {
        f >> nr;
        if (nr > nrmax) {
            nrmax = nr;
            poznrmax = i;
        }
        summ += nr;
        ind2 = i;
        if (summ < 0) {
            ind1 = i+1;
            summ = 0;
        }
        else {
            if (summ > maxsum) {
                maxsum = summ;
                ind1max = ind1;
                ind2max = ind2;
            }
        }
    }
    if (nrmax < 0) {
        g << nrmax << " " << poznrmax << " " << poznrmax;
    }
    else {
        g << maxsum << " " << ind1max << " " << ind2max;
    }
    return 0;
}