Cod sursa(job #2668844)

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

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

int n;
int suma;
int summax;
int x;
int ind1, ind2;
int ind1max, ind2max;
int nrmax=-2000000009;
int indnrmax;

int main()
{
    f >> n;
    ind1 = 1;
    ind2 = 1;
    for (int i=1;i<=n;i++) {
        f >> x;
        if (x > nrmax) {
            nrmax = x;
            indnrmax = i;
        }
        ind2=i;
        if (suma<0) {
            suma = 0;
            suma += x;
            ind1 = i;
            if (suma > summax) {
                summax = suma;
                ind1max = ind1;
                ind2max = ind2;
            }
        }
        else {
            suma += x;
            if (suma > summax) {
                summax = suma;
                ind1max = ind1;
                ind2max = ind2;
            }
        }
    }
    if (nrmax < 0) {
        g << nrmax << " " << indnrmax << " " << indnrmax;
    }
    else {
        g << summax << " " << ind1max << " " << ind2max;
    }
    return 0;
}