Cod sursa(job #1885652)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 20 februarie 2017 10:44:00
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
# include <bits/stdc++.h>
# define maxn 100005
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define pb push_back
# define mp make_pair
//# define int ll
using namespace std;

int n,k,x,ans;

int32_t main(){_
    freopen("deque.in","r",stdin);
    freopen("deque.out","w",stdout);
 	deque<pair<int,int>> q;
 	cin >> n >> k;
 	for(int i = 1;i<=n;i++)
 	{
 		cin >> x;
 		while(!q.empty() && q.back().first > x)
 			q.pop_back();
 		q.push_back(mp(x,i));
 		while(!q.empty() && q.front().second + k - 1 < i)
 			q.pop_front();
 		if(i >= k)
 			ans += q.front().first;
 		//cout << q.front().first << '\n';
 	}
 	rc(ans);
}