#include <iostream>
#include <fstream>
#define NMAX 5000005
using namespace std;
int v[NMAX], dq[NMAX];
int main()
{
ifstream f("deque.in");
ofstream g("deque.out");
int frt, bck, n, k, i, sum = 0, j;
f >> n >> k;
for(i = 1; i <= n; ++i)
f >> v[i];
frt = 1;
bck = 0;
for(i = 1; i <= n; ++i)
{
while(frt <= bck && v[i] <= v[dq[bck]])
bck --;
dq[++ bck] = i;
for(j = frt; j <= bck; j ++)
cout << v[dq[j]] << " ";
cout << endl;
if(dq[frt] == i - k)
frt ++;
if(i >= k)
sum += v[dq[frt]];
}
g << sum;
return 0;
}