103212024-03-30 18:09:04111Majomházcpp17Time limit exceeded 40/1003.099s7564 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define INF (int)1e18

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),cnt(N+1);
		for(int i=1;i<=N;i++){
			dp[i]=cost(0,i);
			cnt[i]=0;
			for(int j=1;j<i;j++){
				int x=dp[j]+cost(j,i)+y;
				if(x<dp[i]){
					dp[i]=x;
					cnt[i]=cnt[j]+1;
				}
			}
		}
		return{dp[N],cnt[N]};
	};
	int l=0,h=INF;
	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
1Accepted3ms2092 KiB
2Accepted1.039s2416 KiB
subtask210/10
3Accepted3ms2444 KiB
4Accepted3ms2508 KiB
5Accepted3ms2740 KiB
6Accepted3ms2772 KiB
7Accepted3ms2960 KiB
subtask310/10
8Accepted14ms3068 KiB
9Accepted16ms3264 KiB
10Accepted16ms3352 KiB
11Accepted16ms3608 KiB
12Accepted16ms3560 KiB
subtask420/20
13Accepted1.246s3888 KiB
14Accepted1.271s3808 KiB
15Accepted1.299s4056 KiB
16Accepted1.322s4184 KiB
17Accepted1.325s4184 KiB
18Accepted1.328s4384 KiB
subtask50/29
19Time limit exceeded3.065s5136 KiB
20Time limit exceeded3.069s5056 KiB
21Time limit exceeded3.059s4984 KiB
22Time limit exceeded3.065s4996 KiB
23Time limit exceeded3.051s5180 KiB
subtask60/31
24Time limit exceeded3.042s6956 KiB
25Time limit exceeded3.066s7228 KiB
26Time limit exceeded3.065s7244 KiB
27Time limit exceeded3.059s7376 KiB
28Time limit exceeded3.039s7332 KiB
29Time limit exceeded3.073s7440 KiB
30Time limit exceeded3.055s7308 KiB
31Time limit exceeded3.062s7364 KiB
32Time limit exceeded3.059s7352 KiB
33Time limit exceeded3.099s7344 KiB
34Time limit exceeded3.055s7564 KiB
35Time limit exceeded3.071s7384 KiB
36Time limit exceeded3.062s7420 KiB
37Time limit exceeded3.058s7440 KiB
38Time limit exceeded3.062s7296 KiB
39Time limit exceeded3.066s7424 KiB
40Time limit exceeded3.049s7392 KiB