Cod sursa(job #2563986)

Utilizator vladm98Munteanu Vlad vladm98 Data 1 martie 2020 16:43:00
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <algorithm>

using namespace std;

int main()
{
    ifstream fin ("ssm.in");
    ofstream fout ("ssm.out");
    int n, bestSum = -2000000000, bestLeft = -1, bestRight = -1, currentSum = 0, currentLeft = 1;
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        int currentValue;
        fin >> currentValue;
        if (currentSum >= 0) {
            currentSum += currentValue;
        } else {
            currentSum = currentValue;
            currentLeft = i;
        }
        if (bestSum < currentSum or bestLeft == -1) {
            bestSum = currentSum;
            bestLeft = currentLeft;
            bestRight = i;
        } else if (bestSum == currentSum and currentLeft < bestLeft) {
            bestLeft = currentLeft;
            bestRight = i;
        } else if (bestSum == currentSum and currentLeft == bestLeft and bestRight < i) {
            bestRight = i;
        }
    }
    fout << bestSum << ' ' << bestLeft << ' ' << bestRight;
    return 0;
}