Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- using namespace std;
- int arr[500010];
- int dp[500010];
- int n,k;
- int min(int a,int b){
- return (a < b ? a : b);
- }
- /*
- int cal(int I){
- if(I == n)return arr[I];
- if(dp[I])return dp[I];
- int ans = 1e9+1;
- for(int i = I + 1 ; i <= I + k && i <= n ; i ++){
- ans = min(ans,cal(i));
- }
- return dp[I] = ans + arr[I];
- }*/
- int main()
- {
- scanf("%d %d",&n,&k);
- for(int i = 1 ; i <= n ; i ++){
- scanf("%d",&arr[i]);
- }
- // printf("%d",cal(1));
- priority_queue<pair<int,int> ,vector<pair<int,int> > ,greater<pair<int,int> > > pq;
- dp[n] = arr[n];
- for(int I = n - 1; I >= 1; I --){
- while(!pq.empty() && pq.top().second > I + k)pq.pop();
- pq.push({dp[I+1],I+1});
- dp[I] = pq.top().first + arr[I];
- }
- printf("%d",dp[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement