Pagini recente » Cod sursa (job #1580932) | Cod sursa (job #2697094) | Cod sursa (job #1283492) | Cod sursa (job #1453018) | Cod sursa (job #3346260)
#include <bits/stdc++.h>
#define ll long long
int n,k;
int main()
{
std::ifstream fin("deque.in");
std::ofstream fout("deque.out");
fin >> n >> k;
ll ans=0;
std::deque<std::pair<int,int> > d;
for(int i=1; i<=k; i++){
int x;
fin >> x;
while(!d.empty() && d.back().first>=x){
d.pop_back();
}
d.push_back(std::make_pair(x,i));
}
ans+=(1LL*d.front().first);
// std::cout << d.front().first << "\n";
for(int i=k+1; i<=n; i++){
int x;
fin >> x;
while(!d.empty() && d.back().first>=x){
d.pop_back();
}
d.push_back(std::make_pair(x,i));
if(d.front().second==i-k)
d.pop_front();
ans+=(1LL*d.front().first);
// std::cout << d.front().first << "\n";
}
fout << ans << "\n";
return 0;
}