Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : Electricity
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 12 December 2022 [21:45]
- */
- #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(){
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int n,k;
- cin >> n >> k;
- for(int i=1;i<=n;i++){
- cin >> dp[i];
- if(heap.empty()){
- heap.push({dp[i],1});
- continue;
- }
- while(!heap.empty() && i-heap.top().idx > k) heap.pop();
- dp[i]+=heap.top().val;
- heap.push({dp[i],i});
- }
- cout << dp[n];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment