Cod sursa(job #877325)

Utilizator TeodoraTanaseTeodora Tanase TeodoraTanase Data 12 februarie 2013 19:36:05
Problema Subsecventa de suma maxima Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>

#define NMAX 6000001
#define INF 0x03f3f3f3f3f3f

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 + a[i] >= 0)
        {
            s += a[i];
            ++ lg;

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

    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;
}