Pagini recente » Cod sursa (job #496141) | Cod sursa (job #279303) | Cod sursa (job #2243618) | Cod sursa (job #557470) | Cod sursa (job #45480)
Cod sursa(job #45480)
#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 = 0;
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;
}