Pagini recente » Cod sursa (job #2087731) | Cod sursa (job #3194084) | Cod sursa (job #28786) | Cod sursa (job #2517679) | Cod sursa (job #2025490)
#include <fstream>
using namespace std;
ifstream cin("deque.in");
ofstream cout("deque.out");
long long NR[5000500];
class Deque{
struct node{
int nr;
node *prev = NULL;
node *next = NULL;
};
int size = 0;
node *pos_front = NULL;
node *pos_back = NULL;
public:
bool empty(){
if (size == 0){
return 1;
}
return 0;
};
void push_front(int val){
size ++;
node *now = new node();
now -> nr = val;
if (pos_front != NULL){
now -> prev = pos_front;
pos_front -> next = now;
}
pos_front = now;
if (pos_back == NULL){
pos_back = now;
}
};
void push_back(int val){
size ++;
node *now = new node();
now -> nr = val;
if (pos_back != NULL){
now -> next = pos_back;
pos_back -> prev = now;
}
pos_front = now;
if (pos_front == NULL){
pos_front = now;
}
};
void pop_front(){
if (pos_front == NULL){
return;
}
size --;
if (pos_front -> prev != NULL){
pos_front = pos_front -> prev;
delete pos_front -> next;
pos_front -> next = NULL;
}
else{
delete pos_front;
pos_front = NULL;
pos_back = NULL;
}
};
void pop_back(){
if (pos_back == NULL){
return;
}
size --;
if (pos_back -> next != NULL){
pos_back = pos_back -> next;
delete pos_back -> prev;
pos_back -> prev = NULL;
}
else{
delete pos_back;
pos_front = NULL;
pos_back = NULL;
}
};
int front(){
return pos_front -> nr;
};
int back(){
return pos_back -> nr;
};
} D;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long cont = 0;
int n, k;
cin>>n>>k;
for (int i=1; i<=n; i++){
cin>>NR[i];
}
int full = n - k + 1;
for (int i=1; i<=k; i++){
if (!D.empty() && NR[i] > NR[D.front()] && i > full){
continue;
}
while(!D.empty() && NR[D.front()] > NR[i]){
D.pop_front();
}
D.push_front(i);
}
cont += NR[D.back()];
for (int i=k+1; i<=n; i++){
while(!D.empty() && NR[D.front()] > NR[i]){
D.pop_front();
}
if (!D.empty() && D.back() <= i - k){
D.pop_back();
}
D.push_front(i);
cont += NR[D.back()];
}
cout<<cont;
return 0;
}