Pagini recente » Cod sursa (job #2987096) | Cod sursa (job #2498646) | Cod sursa (job #1112371) | Cod sursa (job #145028) | Cod sursa (job #3183173)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("secv2.in");
ofstream fout("secv2.out");
int main()
{
int n, v[50002], k, suma_max=0;
fin>>n>>k;
for (int i=1; i<=n; i++){
fin>>v[i];
suma_max += v[i];
}
int poz1=1, poz2=n, poz1max=1, poz2max=n, suma;
// 0 -6 2 1 4 -1 3 -5
// idee: stergem ultimul/primul element depinde care e mai mic
// while (poz2-poz1 >= k){
// int suma=0;
// for (int i=poz1; i<=poz2; i++){
// suma += v[i];
// }
// if (suma > suma_max){
// suma_max = suma;
// poz1max = poz1;
// poz2max = poz2;
// }
// if (v[poz1] > v[poz2])
// poz2--;
// else
// poz1++;
// }
for (int i=2; i<=n; i++){
for (int j=i+k; j<=n; j++){
suma = 0;
for (int k=i; k<=j; k++){
suma+=v[k];
}
if (suma > suma_max){
suma_max = suma;
poz1max = i;
poz2max = j;
} else if (suma == suma){
if (poz1max > i){
suma_max = suma;
poz1max = i;
poz2max = j;
} else if ((poz1max == i) && (poz2max-poz1max > j-i)){
suma_max = suma;
poz1max = i;
poz2max = j;
}
}
}
}
fout<<poz1max<<" "<<poz2max<<" "<<suma_max;
return 0;
}