Cod sursa(job #2573590)

Utilizator DariusDCDarius Capolna DariusDC Data 5 martie 2020 18:16:51
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 6000005;
int n;
int dp[nmax], pos[nmax];

int main()
{
    fin >> n;
    int x;
    fin >> x;
    dp[1] = x;
    pos[1] = 1;
    int maxim = -2e9, p;
    for (int i = 2; i <= n; i++)
    {
        int x;
        fin >> x;
        if (dp[i-1] + x > x)
            dp[i] = dp[i-1] + x, pos[i] = pos[i-1];
        else
            dp[i] = x, pos[i] = i;
        if (dp[i] > maxim)
            maxim = dp[i], p = i;
    }
    fout << maxim << " " << pos[p]  << " " << p;
    return 0;
}