Cod sursa(job #2657402)

Utilizator BeilandArnoldArnold Beiland BeilandArnold Data 10 octombrie 2020 14:28:49
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

    int n;
    fin >> n;

    int s;
    fin >> s;

    int elozoMax = s;
    int elozoMaxStart = 0;

    int globMax = s;
    int globMaxStart = 0;
    int globMaxEnd = 0;

    for (int i = 1; i < n; ++i) {
        fin >> s;

        if (s > elozoMax + s) {
            elozoMax = s;
            elozoMaxStart = i;
        }
        else {
            elozoMax += s;
        }

        if (elozoMax > globMax) {
            globMax = elozoMax;
            globMaxStart = elozoMaxStart;
            globMaxEnd = i;
        }
    }

    fout << globMax << ' '
        << globMaxStart+1 << ' '
        << globMaxEnd+1 << endl;

    return 0;
}