103232024-03-30 21:01:41111Majomházcpp17Wrong answer 0/1003.101s38576 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);
		vector<vector<int>>chk(N+1);
		int o=0;
		auto bs=[&](int i)->int{
			int l=i+1,h=N;
			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++){
			for(int j:chk[i]){
				if(j<o){
					continue;
				}
				if(dp[j]+cost(j,i)<dp[o]+cost(o,i)){
					o=j;
				}
				chk[bs(j)].push_back(j);
			}
			dp[i]=dp[o]+cost(o,i)+y;
			cnt[i]=cnt[o]+1;
			chk[bs(i)].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
1Accepted3ms1956 KiB
2Wrong answer164ms3612 KiB
subtask20/10
3Accepted3ms2316 KiB
4Accepted3ms2484 KiB
5Accepted3ms2644 KiB
6Wrong answer3ms2640 KiB
7Accepted3ms2884 KiB
subtask30/10
8Accepted14ms2948 KiB
9Accepted14ms3200 KiB
10Wrong answer13ms3416 KiB
11Wrong answer12ms3688 KiB
12Wrong answer9ms3728 KiB
subtask40/20
13Accepted158ms4884 KiB
14Accepted179ms4932 KiB
15Wrong answer190ms5012 KiB
16Wrong answer174ms4972 KiB
17Wrong answer157ms5096 KiB
18Wrong answer119ms5144 KiB
subtask50/29
19Wrong answer2.655s20416 KiB
20Wrong answer2.658s20312 KiB
21Accepted2.634s20424 KiB
22Wrong answer2.444s20372 KiB
23Wrong answer2.234s20328 KiB
subtask60/31
24Time limit exceeded3.062s20456 KiB
25Time limit exceeded3.069s20472 KiB
26Time limit exceeded3.062s20364 KiB
27Time limit exceeded3.051s20456 KiB
28Time limit exceeded3.101s20496 KiB
29Time limit exceeded3.051s20492 KiB
30Time limit exceeded3.016s20748 KiB
31Time limit exceeded3.042s20744 KiB
32Time limit exceeded3.053s20704 KiB
33Time limit exceeded3.048s20368 KiB
34Time limit exceeded3.075s20252 KiB
35Time limit exceeded3.059s20372 KiB
36Time limit exceeded3.066s20620 KiB
37Time limit exceeded3.055s20652 KiB
38Wrong answer2.782s38360 KiB
39Wrong answer2.395s38364 KiB
40Wrong answer2.125s38576 KiB