MAGCARI

Electricity

Dec 21st, 2022
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 21 December 2022 [21:15]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int val,idx;
  11.     bool operator < (const A&o) const{
  12.         return val>o.val;
  13.     }
  14. };
  15. priority_queue<A > heap;
  16. int dp[500010];
  17. int main(){
  18.     int n,k;
  19.     scanf("%d %d",&n,&k);
  20.     for(int i=1;i<=n;i++){
  21.         scanf("%d",&dp[i]);
  22.         if(heap.empty()){
  23.             heap.push({dp[i],i});
  24.             continue;
  25.         }
  26.         // i <-> heap.top().idx
  27.         while(!heap.empty() && i-heap.top().idx>k)
  28.             heap.pop();
  29.         dp[i]+=heap.top().val;
  30.         heap.push({dp[i],i});
  31.     }
  32.     printf("%d\n",dp[n]);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment