Cod sursa(job #2171861)

Utilizator zanugMatyas Gergely zanug Data 15 martie 2018 13:53:24
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <climits>

#define ll long long

using namespace std;

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

const int NLIM = 6e6+10;

int n, x[NLIM];
int s;

int main()
{
    ios::sync_with_stdio(false);

    fin >> n;
    for(int i = 1; i <= n; ++i)
    {
        fin >> x[i];
    }

    int miniidx = 1;
    int maxi = -INT_MAX;
    int first = 1;
    int last = 1;

    for(int i = 1; i <= n; ++i)
    {
        s += x[i];

        if(s < x[i])
        {
            s = x[i];
            miniidx = i;
        }

        if(s > maxi)
        {
            maxi = s;
            last = i;
            first = miniidx;
        }
    }

    fout << maxi << " " << first << " " << last;

    return 0;
}