Cod sursa(job #2509558)

Utilizator Sebi_MafteiMaftei Sebastioan Sebi_Maftei Data 14 decembrie 2019 13:12:43
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    int x, y, p, s, M, n, st, dr, i;
    fin >> n;
    fin >> x;
    s = M = x;
    if (s < 0)
    {
        s = 0;
        p = 2;
    }
    for (i = 2; i <= n; i++)
    {
        fin >> y;
        s += y;
        if (s > M)
        {
            M = s;
            dr = i;
            st = p;
        }
        if (s < 0)
        {
            s = 0;
            p = i + 1;
        }
    }
    fout << M << " " << st << " " << dr;
    return 0;
}