Pagini recente » Cod sursa (job #2624679) | Cod sursa (job #2033655)
#include <fstream>
#include <deque>
using namespace std;
ifstream fin ("deque.in");
ofstream cout("deque.out");
deque<int> q;
int a[5000005];
int main()
{
int n, k, x;
long long s = 0;
fin >> n >> k;
for(int i = 1; i <= n; ++i) fin >> a[i];
///prelucrez primele k elemente
for(int i = 1; i <= k; ++i)
{
x = a[i];
/// scot din queue toate elem >= cu x;
while(!q.empty() && x <= a[q.back()])
q.pop_back();
q.push_back(i);
}
s += a[q.front()];
for(int i = k + 1; i <= n; ++i)
{
x = a[i];
while(!q.empty() && x <= a[q.back()])
q.pop_back();
q.push_back(i);
/// verific daca s-a invechit un elem
if(i-q.front() >= k) q.pop_front();
s += a[q.front()];
}
cout << s;
return 0;
}