Pagini recente » Rating Ana-Maria Dobrescu (anadobrescu) | Cod sursa (job #1897116) | Cod sursa (job #1717178) | Cod sursa (job #1060794) | Cod sursa (job #2909030)
#include <iostream>
#include <fstream>
using namespace std;
int max_neg = -25001, positive, maxx, countt, act, n, k, v[50001], leftt, rightt;
int main() {
ifstream fin("secv2.in");
ofstream fout("secv2.out");
fin >> n >> k;
for (int i = 1; i <= n; ++i) {
fin >> v[i];
if (v[i] > -1) {
++positive;
}
if (v[i] > max_neg && v[i] < 0) {
max_neg = v[i];
}
}
if (positive > 0) {
for (int i = 1; i <= n; ++i) {
act = act + v[i];
++countt;
if (maxx < act && countt >= k) {
maxx = act;
rightt = i;
}
if (act < 0) {
act = 0;
countt = 0;
}
}
int i = rightt, summ = 0;
while (summ < maxx) {
summ = summ + v[i];
leftt = i;
--i;
}
fout << leftt << ' ' << rightt << ' ' << maxx;
} else {
fout << max_neg;
}
return 0;
}