Pagini recente » Cod sursa (job #2459670) | Cod sursa (job #3236745) | Cod sursa (job #3161447) | Cod sursa (job #7160) | Cod sursa (job #3162555)
#include <fstream>
#include <climits>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
int v[5000001],dq[5000001],n,k,i;
long long s;
int main()
{
cin>>n>>k;
v[0]=INT_MIN;
int st=0,dr=0;
for(i=1;i<=n;i++)
{
cin>>v[i];
while(i-dq[st]+1>k) ///elimin de la inceput deoarece lungimea secventei e mai mare decta k
st++;
while(st<=dr&&v[i]<=v[dq[dr]])
dr--;
dq[++dr]=i;
if(i>=k)
s+=v[dq[st]];
}
cout<<s;
return 0;
}