Pagini recente » Cod sursa (job #2917550) | Cod sursa (job #2724084) | Cod sursa (job #1493131) | Cod sursa (job #1170275) | Cod sursa (job #1072850)
#include <fstream>
using namespace std;
#define DEBUG true
#if DEBUG
#include <iostream>
#define INFILE "/Users/biordach/Documents/Xcode Projects/training/training/deque.in"
#define __OUT cout
#else
#define INFILE "deque.in"
#define OUTFILE "deque.out"
ofstream __OUT(OUTFILE);
#endif
void readInput();
void solve();
void printOutput();
struct node{
int value, position;
node *prev, *next;
}*first, *last;
int main(int argc, const char * argv[])
{
readInput();
// solve();
// printOutput();
#if DEBUG == false
__OUT.close();
#endif
return 0;
}
node* createNode(int value, int pos){
node* q = new node;
q->prev = q->next = NULL;
q->value = value;
q->position = pos;
return q;
}
void deleteList(){
node* q = first;
while(q){
node* next = q->next;
delete q;
q = next;
}
first = last = NULL;
}
void addToList(int x, int pos){
node* newNode = createNode(x, pos);
if(first != NULL && first -> value >= x){
deleteList();
}
if(last == NULL){
first = last = newNode;
return;
}
while(last -> value >= x){
node* prev = last->prev;
delete last;
last = prev;
}
last->next = newNode;
newNode->prev = last;
last = newNode;
}
void readInput(){
int n, k;
long long sum = 0;
ifstream in(INFILE);
in>>n>>k;
int x;
for(int i=0;i<k;i++){
in>>x;
addToList(x, i);
}
sum += first->value;
for(int i=k; i < n; i++){
in>>x;
addToList(x, i);
if(i - first->position == k){
node* next = first->next;
delete first;
first = next;
}
sum += first->value;
}
in.close();
__OUT<<sum<<'\n';
}
void solve(){
}
void printOutput(){
}