Pagini recente » Profil ionanghelina | Cod sursa (job #355550) | Cod sursa (job #2937701) | Cod sursa (job #1702920) | Cod sursa (job #3148775)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 5e6;
deque<int> q;
queue<int> coada;
int main() {
FILE *fin, *fout;
fin = fopen("deque.in", "r");
fout = fopen("deque.out", "w");
int n, k;
fscanf(fin, "%d%d", &n, &k);
long long ans = 0;
for(int i = 0; i < k; i++) {
int x;
fscanf(fin, "%d", &x);
coada.push(x);
while(!q.empty() && x < q.back())
q.pop_back();
q.push_back(x);
}
//cout << q.front() << endl;
ans += q.front();
for(int i = k; i < n; i++) {
//cout << "HATZ" << ' ' << q.front() << endl;
if(q.front() == coada.front())
q.pop_front();
coada.pop();
int x;
fscanf(fin, "%d", &x);
coada.push(x);
while(!q.empty() && x < q.back())
q.pop_back();
q.push_back(x);
//cout << q.front() << endl;
ans += q.front();
}
fprintf(fout, "%lld", ans);
fclose(fin);
fclose(fout);
return 0;
}