Pagini recente » Cod sursa (job #1834797) | Cod sursa (job #2262552) | Cod sursa (job #1794179) | Cod sursa (job #1664776) | Cod sursa (job #1370313)
#include <fstream>
#include <iostream>
#include <vector>
#include <limits>
#include <cassert>
int main()
{
std::ifstream fin("secv2.in");
std::ofstream fout("secv2.out");
int N, K;
fin >> N >> K;
std::vector<int> v(N);
for (auto &i : v)
fin >> i;
int start_idx = 0, end_idx = 0;
int current_max = v[0];
int max = std::numeric_limits<int>::min();
int max_start = start_idx, max_end = end_idx;
assert(K > 1);
for (auto i = 1; i < N; ++i) {
while (end_idx - start_idx >= K - 1 && v[start_idx] <= 0) {
current_max -= v[start_idx];
start_idx++;
}
current_max += v[i];
++end_idx;
if (current_max >= max && end_idx - start_idx >= K - 1) {
max = current_max;
max_start = start_idx;
max_end = end_idx;
}
}
fout << max_start + 1 << " " << max_end + 1 << " " << max << "\n";
return 0;
}