Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("deque.in");
- ofstream fout("deque.out");
- const int NMAX=5000005;
- deque<int>Q;
- int n,k,a[NMAX];
- long long sol;
- int main()
- {
- fin>>n>>k;
- for(int i=1;i<=k;i++)
- {
- int x;
- fin>>a[i];
- x=a[i];
- while(!Q.empty() && a[Q.back()]>=x)
- Q.pop_back();
- Q.push_back(i);
- }
- sol+=(a[Q.front()]);
- for(int i=k+1;i<=n;i++)
- {
- int x;
- fin>>a[i];
- x=a[i];
- while(!Q.empty() && a[Q.back()]>=x)
- Q.pop_back();
- Q.push_back(i);
- if(i-Q.front()>=k)
- Q.pop_front();
- sol+=a[Q.front()];
- }
- fout<<sol<<"\n";
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment