vector<int>maxSlidingWindow(vector<int>&nums,intk){vector<int>ret;deque<int>dq;for(inti=0;i<nums.size();i++){if(dq.size()&&dq.front()<=i-k){dq.pop_front();// 將過期元素移除}while(dq.size()&&nums[i]>=nums[dq.back()]){dq.pop_back();// 將隊尾用不上答案移除}dq.push_back(i);if(i>=k-1){ret.push_back(nums[dq.front()]);// 以 i 為結尾長度為 k 的極值}}returnret;}
#include<bits/stdc++.h>#define int long long#define F first#define S secondusingnamespacestd;constintMAXN=15e4+5;structNode{intt,a,b;};intn,m,d;Nodev[MAXN];intdp[MAXN],tmp[MAXN];signedmain(){cin>>n>>m>>d;for(inti=1;i<=m;i++){auto&[t,a,b]=v[i];cin>>a>>b>>t;}sort(v+1,v+m+1);intlast=0;for(inti=1;i<=m;i++){for(intj=1;j<=n;j++){tmp[j]=dp[j];dp[j]=0;}auto[t,a,b]=v[i];intrp=0;deque<pair<int,int>>st;for(intj=1;j<=n;j++){intL=max(1ll,j-1ll*(t-last)*d),R=min(n,j+1ll*(t-last)*d);while(rp<R){rp++;while(st.size()&&tmp[rp]>=st.back().S){st.pop_back();}st.push_back({rp,tmp[rp]});}while(st.front().F<L)st.pop_front();dp[j]=st.front().S;}for(intj=1;j<=n;j++){dp[j]+=b-abs(a-j);}last=t;}cout<<*max_element(dp+1,dp+n+1)<<'\n';}
#include<bits/stdc++.h>#define int long longusingnamespacestd;constintMAXN=1e6+5;constintINF=0x3f3f3f3f;intn,k;inta[MAXN];intdp[MAXN];signedmain(){ios::sync_with_stdio(0);cin.tie(0);cin>>n>>k;deque<int>dq;for(inti=0;i<n;i++){cin>>a[i];while(dq.size()&&dq.front()<i-k){dq.pop_front();}if(i<k){dp[i]=a[i];if(dq.size()&&dp[dq.front()]<0)dp[i]+=dp[dq.front()];}else{dp[i]=dp[dq.front()]+a[i];}while(dq.size()&&dp[dq.back()]>=dp[i]){dq.pop_back();}dq.push_back(i);}cout<<dp[dq.front()];}