Pagini recente » Cod sursa (job #1648648) | Cod sursa (job #1860294) | Cod sursa (job #2117718) | Cod sursa (job #1318995) | Cod sursa (job #3355626)
//#include "header.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ft(n) for(ll i=1; i<=n; i++)
string file = "deque";
ifstream f(file + ".in");
ofstream g(file + ".out");
deque<ll> d, pos;
vector<ll> mins;
int main()
{
ll n, k, x, s=0;
f >> n >> k;
mins.resize(n + 5);
ft(n)
{
f >> x;
while (d.size() && x <= d.back())
d.pop_back(), pos.pop_back();
d.push_back(x), pos.push_back(i);
if (pos.front() <= i - k) d.pop_front(), pos.pop_front();
mins[i] = d.front();
}
for (ll i = k; i <= n; i++)
s += mins[i];
g << s;
return 0;
}