Cod sursa(job #45477)

Utilizator chermanCorina Herman cherman Data 1 aprilie 2007 15:58:18
Problema Secventa 2 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <iostream>

using namespace std;

ifstream in("secv2.in");
ofstream out("secv2.out");

int n, a[50000], k, ktmp=0;
int seqStart = 0, seqEnd =-1;

void read()
{
    in >> n >> k;
    for ( int i = 0; i < n; ++i )
        in >> a[i];
}

int maxSubSum3()
    {
        int maxSum = -65535;
        int thisSum = 0;

        for( int i = 0, j = 0; j < n; j++ )
        {
            //if(j-i>=k)
            //{
            thisSum += a[ j ];

            if( thisSum > maxSum )
            {
                maxSum = thisSum;
                seqStart = i;
                seqEnd   = j;
            }
            else if( thisSum < 0 )
            {
                i = j + 1;
                //thisSum = 0;
            }
            //}
        }

        return maxSum;
    }

int main()
{
    read();
    cout<<seqStart+1<<" "<<seqEnd+1<<" " <<maxSubSum3();
    out<<seqStart+1<<" "<<seqEnd+1<<" " <<maxSubSum3();
	return 0;
}