Cod sursa(job #877338)

Utilizator TeodoraTanaseTeodora Tanase TeodoraTanase Data 12 februarie 2013 19:40:37
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>

#define NMAX 6000001
#define INF 0x03f3f3f3f

using namespace std;

int n;
int smax = -INF;
int lgmax;
int pozmax;
int a[NMAX];

void read()
{
    freopen("ssm.in", "r", stdin);

    scanf("%d\n", &n);
    for(int i = 0; i < n; ++ i)
        scanf("%d ", &a[i]);
}

void solve()
{
    int s = a[0];
    int lg = 1;

    for(int i = 1; i < n; ++ i)
    {
        if(s >= 0)
        {
            s += a[i];
            ++ lg;
        }
        else
        {
            lg = 1;
            s = a[i];
        }

        if(s > smax)
        {
            smax = s;
            lgmax = lg;
            pozmax = i + 1;
        }
    }

    if(s > smax)
    {
        smax = s;
        pozmax = n;
    }
}

int main()
{
    read();
    solve();

    freopen("ssm.out", "w", stdout);
    printf("%d %d %d\n", smax, pozmax - lgmax + 1, pozmax);

    return 0;
}