103262024-03-30 21:44:16111Majomházcpp17Wrong answer 0/100880ms12760 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<int>v(N+1),pf(N+1);
	for(int i=0;i<N;i++){
		cin>>v[i];
		pf[i+1]=pf[i]+v[i];
	}
	auto cost=[&](int s,int e)->int{
		return (pf[e]-pf[s])*(e-s);
	};
	auto solve=[&](int y)->pair<int,int>{
		vector<int>dp(N+1,1e18),cnt(N+1);
		deque<int>q;
		int o=0;
		auto bs=[&](int l,int i)->int{
			int h=N;
			int ll=l;
			while(l<h){
				int m=(l+h)/2;
				if(dp[i]+cost(i,m)<=dp[o]+cost(o,m)){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			return h;
		};
		dp[0]=-y;
		cnt[0]=-1;
		for(int i=1;i<=N;i++){
			while(!q.empty()){
				int j=q.front();
				if(dp[j]+cost(j,i)<dp[o]+cost(o,i)){
					o=j;
					q.pop_front();
				}
				else if(j<o){
					q.pop_front();
				}
				else{
					break;
				}
			}
			dp[i]=dp[o]+cost(o,i)+y;
			cnt[i]=cnt[o]+1;
			while(!q.empty()&&bs(i,i)<=bs(i,q.back())){
				q.pop_back();
			}
			q.push_back(i);
		}
		return{dp[N],cnt[N]};
	};
	int l=0,h=1e12;
	while(l<h){
		int m=(l+h)/2;
		auto [x,c]=solve(m);
		if(c<=K){
			h=m;
		}
		else{
			l=m+1;
		}
	}
	auto [x,c]=solve(h);
	cout<<x-c*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Wrong answer37ms2460 KiB
subtask20/10
3Wrong answer3ms2284 KiB
4Wrong answer3ms2372 KiB
5Wrong answer3ms2500 KiB
6Wrong answer3ms2720 KiB
7Wrong answer3ms2828 KiB
subtask30/10
8Wrong answer6ms3196 KiB
9Wrong answer4ms3404 KiB
10Wrong answer4ms3488 KiB
11Wrong answer4ms3444 KiB
12Wrong answer4ms3532 KiB
subtask40/20
13Wrong answer45ms3732 KiB
14Wrong answer43ms3684 KiB
15Wrong answer43ms4008 KiB
16Wrong answer41ms3976 KiB
17Wrong answer37ms4012 KiB
18Wrong answer37ms4320 KiB
subtask50/29
19Wrong answer432ms7396 KiB
20Wrong answer430ms7780 KiB
21Wrong answer437ms8360 KiB
22Wrong answer426ms8492 KiB
23Wrong answer412ms8620 KiB
subtask60/31
24Wrong answer871ms11740 KiB
25Wrong answer870ms11864 KiB
26Wrong answer871ms11696 KiB
27Wrong answer870ms11696 KiB
28Wrong answer871ms12468 KiB
29Wrong answer866ms12596 KiB
30Wrong answer871ms12552 KiB
31Wrong answer870ms12560 KiB
32Wrong answer870ms12712 KiB
33Wrong answer880ms12636 KiB
34Wrong answer870ms12760 KiB
35Wrong answer834ms12680 KiB
36Wrong answer799ms12620 KiB
37Wrong answer808ms12644 KiB
38Wrong answer799ms12604 KiB
39Wrong answer755ms12612 KiB
40Wrong answer538ms12616 KiB