Advertisement
SuitNdtie

electricity

May 11th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4.  
  5. int arr[500010];
  6. int dp[500010];
  7.  
  8. int n,k;
  9. int min(int a,int b){
  10.     return (a < b ? a : b);
  11. }
  12. /*
  13. int cal(int I){
  14.     if(I == n)return arr[I];
  15.     if(dp[I])return dp[I];
  16.     int ans = 1e9+1;
  17.     for(int i = I + 1 ; i <= I + k && i <= n ; i ++){
  18.         ans = min(ans,cal(i));
  19.     }
  20.     return dp[I] = ans + arr[I];
  21. }*/
  22. int main()
  23. {
  24.     scanf("%d %d",&n,&k);
  25.     for(int i = 1 ; i <= n ; i ++){
  26.         scanf("%d",&arr[i]);
  27.     }
  28.    
  29. //  printf("%d",cal(1));
  30.     priority_queue<pair<int,int> ,vector<pair<int,int> > ,greater<pair<int,int> > > pq;
  31.     dp[n] = arr[n];
  32.     for(int I = n - 1; I >= 1; I --){
  33.         while(!pq.empty() && pq.top().second > I + k)pq.pop();
  34.         pq.push({dp[I+1],I+1});
  35.         dp[I] = pq.top().first + arr[I];
  36.     }
  37.     printf("%d",dp[1]);
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement