MAGCARI

Electricity

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