Cod sursa(job #2583484)

Utilizator alexboat10759Alex Mateescu alexboat10759 Data 18 martie 2020 13:07:19
Problema Subsecventa de suma maxima Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>

using namespace std;

int a[100002],sum[100002],best[100002];

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

    int n,poz=0,k,maxx=-2000000000,minn=2000000000,poz1,poz2;
    fin>>n;
    for(int i=1; i<=n; i++)
    {
        fin>>a[i];
    }
    sum[0] = 0;
    for (int i = 1; i <= n; i++)
        sum[i] = a[i] + sum[i-1];
    minn = sum[0];
    for (int i = 1; i <= n; i++)
    {
        best[i] = sum[i] - minn;
        if (minn > sum[i]){
            minn = sum[i];
            poz1=i+1;
        }
        if (maxx < best[i]){
            maxx = best[i];
            poz2=i;
        }
    }
    fout<<maxx<<" "<<poz1<<" "<<poz2;
    return 0;
}