Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using pii = pair<int,int>;
- int dp[500001];
- priority_queue <pii,vector<pii>,greater<pii>> pq;
- void del(int i,int k){
- while(pq.size() > 0 and i-pq.top().second > k) {
- pq.pop();
- }
- }
- int main(){
- int n,k;
- scanf("%d%d",&n,&k);
- int ar[n+1];
- for(int i=1;i<=n;i++) scanf("%d",&ar[i]);
- dp[1] = ar[1];
- pq.push({dp[1],1});
- for(int i=2;i<=n;i++){
- del(i,k);
- dp[i] = ar[i] + pq.top().first;
- pq.push({dp[i],i});
- }
- printf("%d",dp[n]);
- return 0;
- }
Advertisement
RAW Paste Data
Copied
Advertisement