Cod sursa(job #2922361)

Utilizator BeilandArnoldArnold Beiland BeilandArnold Data 8 septembrie 2022 01:46:48
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 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;
}