MAGCARI

Electricity

Dec 12th, 2022
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. /*
  2.     Task    : Electricity
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 12 December 2022 [21:45]
  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.     cin.tie(0)->sync_with_stdio(0);
  19.     cin.exceptions(cin.failbit);
  20.     int n,k;
  21.     cin >> n >> k;
  22.     for(int i=1;i<=n;i++){
  23.         cin >> dp[i];
  24.         if(heap.empty()){
  25.             heap.push({dp[i],1});
  26.             continue;
  27.         }
  28.         while(!heap.empty() && i-heap.top().idx > k)    heap.pop();
  29.         dp[i]+=heap.top().val;
  30.         heap.push({dp[i],i});
  31.     }
  32.     cout << dp[n];
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment