Pagini recente » Cod sursa (job #3196261) | Cod sursa (job #1200547) | Profil Fetita_Jucausa | Cod sursa (job #1737673) | Cod sursa (job #2047232)
#include <bits/stdc++.h>
using namespace std;
const int nMax = 5000005;
int n, k, a[nMax], ans;
deque <int> q;
void Read() {
ifstream fin("deque.in");
fin >> n >> k;
for (int i = 1; i <= n; i++)
fin >> a[i];
fin.close();
}
void Solve() {
for (int i = 1; i <= k; i++) {
int x = a[i];
while (!q.empty() && x <= a[q.back()])
q.pop_back();
q.push_back(i);
}
ans += a[q.front()];
for (int i = k + 1; i <= n; i++) {
int x = a[i];
while (!q.empty() && x <= a[q.back()])
q.pop_back();
q.push_back(i);
if (i - q.front() >= k)
q.pop_front();
ans += a[q.front()];
}
}
void Write() {
ofstream fout("deque.out");
fout << ans << "\n";
fout.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}