Pagini recente » Cod sursa (job #37619) | Cod sursa (job #2568262) | Cod sursa (job #483778) | Cod sursa (job #906625) | Cod sursa (job #2294664)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("secv2.in");
ofstream cout("secv2.out");
const int DIM = 50001;
int nr[DIM], s[DIM], d[DIM];
int n, k;
int main()
{
cin >> n >> k;
int st = 1, dr = 1, maxi = -DIM * 25000, ind1, ind2, imaxi;
for (int i = 1; i <= n; ++i)
{
cin >> nr[i];
if (i <= k)
{
s[i] = s[i - 1] + nr[i];
}
else
{
s[i] = s[i - 1] + nr[i];
s[i] -= nr[i - k];
}
if (i >= k)
{
if (s[i] > maxi)
{
maxi = s[i];
imaxi = i - k + 1;
}
}
}
int i1 = imaxi, stemp = 0;
for (int i = imaxi - 1; i >= 1; --i)
{
stemp += nr[i];
if (maxi + stemp > maxi)
{
maxi = maxi + stemp;
i1 = i;
}
}
stemp = 0;
int i2 = imaxi + k - 1;
for (int i = i2 + 1; i <= n; ++i)
{
stemp += nr[i];
if (maxi + stemp > maxi)
{
maxi = maxi + stemp;
i2 = i;
}
}
cout << i1 << ' ' << i2 << ' ' << maxi << endl;
getchar();
getchar();
return 0;
}