Pagini recente » Cod sursa (job #2826183) | Monitorul de evaluare | Istoria paginii teoria-jocurilor/notiuni | Cod sursa (job #55419) | Cod sursa (job #1154705)
#include <cstdio>
#include <deque>
using namespace std;
struct punct
{
int x, poz;
};
deque <punct> q;
int main()
{
int n, k, x, sum=0;
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%d %d", &n, &k);
for (int i = 1; i < k; ++i)
{
scanf("%d", &x);
punct aux;
aux.x = x;
aux.poz = i;
if (!q.empty())
while ( x < q.back().x )
{
q.pop_back();
if (q.empty()) break;
}
q.push_back(aux);
}
for (int i = k; i <= n; ++i)
{
scanf("%d", &x);
punct aux;
aux.x = x;
aux.poz = i;
if (!q.empty())
while ( x < q.back().x )
{
q.pop_back();
if (q.empty()) break;
}
q.push_back(aux);
if (q.front().poz == i-k) q.pop_front();
sum += q.front().x;
}
printf("%d\n", sum);
return 0;
}