Cod sursa(job #3195928)

Utilizator AndreiMLCChesauan Andrei AndreiMLC Data 22 ianuarie 2024 09:51:38
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("ssm.in");
ofstream g("ssm.out");

int n;
const int nmax = 6000005;
int v[nmax];
long cost[nmax];
int cost_min=INT_MAX;
int cost_max=INT_MIN;
int ind_start;
int ind_stop;
int sum;

int main()
{
    f >> n;
    for(int i = 1;i<=n;i++)
    {
        f >> v[i];
        sum+=v[i];
        cost[i] = sum - cost[ind_start];
        if(cost_min > cost[i] && v[i] > 0)
        {
            ind_start = i;
            cost_min = cost[i];
        }
        if(cost_max < cost[i])
        {
            ind_stop = i;
            cost_max = cost[i];
        }
    }
    g << cost_max << ' ' << ind_start << ' ' << ind_stop;
    return 0;
}