Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 21 December 2022 [21:15]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int val,idx;
- bool operator < (const A&o) const{
- return val>o.val;
- }
- };
- priority_queue<A > heap;
- int dp[500010];
- int main(){
- int n,k;
- scanf("%d %d",&n,&k);
- for(int i=1;i<=n;i++){
- scanf("%d",&dp[i]);
- if(heap.empty()){
- heap.push({dp[i],i});
- continue;
- }
- // i <-> heap.top().idx
- while(!heap.empty() && i-heap.top().idx>k)
- heap.pop();
- dp[i]+=heap.top().val;
- heap.push({dp[i],i});
- }
- printf("%d\n",dp[n]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment