Cod sursa(job #1145313)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 18 martie 2014 09:07:16
Problema Subsecventa de suma maxima Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include    <iostream>
#include    <fstream>

using namespace std;

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

short int N, v[6000005];

void read()
{
    int a,b, max = -7000000,k,x,y;
    fin >> N;
    a = x = 1; //Begin from x and set x to 1
    b = y = 1; //End in y and set y to 1
    fin >> k; //Reading one value
    v[0] = k; //Setting the first element from v to value that has readed.
    max = v[0]; //The maximum will be the first element readed.
    for(int i = 1; i < N; i++)
    {
        fin >> k; //Reading one value
        if(v[i-1] + k < k)
        {//If the new sum is lower than the current element
            v[i] = k;
            a = i + 1;
            b = i + 1;
        }
        else
        {
            v[i] = v[i-1] + k;
            b = i + 1;
        }
        if(v[i] > max)
        {
            max = v[i];
            x = a;
            y = b;
        }
    }
    fout << max << " " << x << " " << y;
}

int main()
{
    read();
    return 0;
}