Cod sursa(job #2763400)

Utilizator gabimoiseMoise Gabriel gabimoise Data 13 iulie 2021 17:32:12
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>

using namespace std;

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

struct elem {
    int value;
    int index;
};

elem best[6000001];
int arr[6000050], i, n, maxSum, minIndex, x, r;

int main()
{
    fin >> n;
    best[0].index = 0;
    fin >> best[0].value;
    maxSum = best[0].value; minIndex = n + 1; r = n + 1;
    for (i = 1; i < n; i++)
    {
        fin >> x;
        if (best[i-1].value > 0) { best[i].value = best[i-1].value + x; best[i].index = best[i-1].index; }
        else { best[i].value = x; best[i].index = i; }
        if (best[i].value > maxSum) { maxSum = best[i].value; minIndex = best[i].index; r = i; }
        else if (best[i].value == maxSum && best[i].index < minIndex) { minIndex = best[i].index; r = i; }
    }
    fout << maxSum << " " << minIndex + 1 << " " << r + 1 << endl;
    return 0;
}