Cod sursa(job #1839357)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 2 ianuarie 2017 20:18:24
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(0);
#define setnow clock_t tStart=clock();
#define time (double)(clock() - tStart)/CLOCKS_PER_SEC;
#define setin(x) ifstream cin(x);
#define setout(x) ofstream cout(x);
typedef long long ll;
typedef long long int lli;
typedef pair < int, int> dbl;
const int maxInt = 1e9*2;
const lli maxLong = 1e18*2;



int main(){
           // setnow;
            ios;
            setin("deque.in");
            setout("deque.out");
            lli n, k, ans = 0;
            cin >> n >> k;
            deque < pair < int , int > > deq;
            for(int i = 1; i <= n; i++){

                        int x;
                        cin >> x;
                        while(!deq.empty() && x <= deq.back().first )
                                deq.pop_back();
                        deq.push_back(make_pair(x, i));
                        if(deq.front().second <= i - k)
                                deq.pop_front();
                        if(i >= k)
                                ans += deq.front().first;
            }
            return cout << ans, 0;
           // cout << fixed << time;
	//return 0;
}