103222024-03-30 20:50:11111Majomházcpp17Time limit exceeded 40/1003.099s16196 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);
	};
	// if(1){
		// N=15;
		// v.resize(N+1),pf.resize(N+1);
		// for(int i=0;i<N;i++){
			// v[i]=rand()%100;
			// pf[i+1]=pf[i]+v[i];
		// }
		// for(int i=1;i<=N;i++){
			// for(int j=1;j<=N;j++){
				// cout<<setw(4)<<cost(i,j)<<' ';
			// }
			// cout<<'\n';
		// }
		// return 1;
	// }
	auto solve=[&](int y)->pair<int,int>{
		vector<int>dp(N+1,1e18),cnt(N+1);
		dp[0]=-y;
		cnt[0]=-1;
		for(int i=1,o=0;i<=N;i++){
			for(int j=o;j<i;j++){
				int x=dp[j]+cost(j,i)+y;
				if(x<dp[i]){
					dp[i]=x;
					cnt[i]=cnt[j]+1;
					o=j;
				}
			}
		}
		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
1Accepted3ms1832 KiB
2Accepted68ms2556 KiB
subtask210/10
3Accepted3ms2404 KiB
4Accepted3ms2396 KiB
5Accepted3ms2628 KiB
6Accepted3ms2716 KiB
7Accepted3ms2932 KiB
subtask310/10
8Accepted6ms3044 KiB
9Accepted4ms3300 KiB
10Accepted4ms3636 KiB
11Accepted4ms3740 KiB
12Accepted4ms3812 KiB
subtask420/20
13Accepted305ms4400 KiB
14Accepted222ms4648 KiB
15Accepted136ms4792 KiB
16Accepted59ms4632 KiB
17Accepted43ms4708 KiB
18Accepted32ms4740 KiB
subtask50/29
19Time limit exceeded3.058s5804 KiB
20Time limit exceeded3.085s6272 KiB
21Accepted2.911s9028 KiB
22Accepted1.524s9264 KiB
23Accepted912ms9460 KiB
subtask60/31
24Time limit exceeded3.049s9712 KiB
25Time limit exceeded3.069s10656 KiB
26Time limit exceeded3.099s11472 KiB
27Time limit exceeded3.065s12156 KiB
28Time limit exceeded3.053s12120 KiB
29Time limit exceeded3.082s12064 KiB
30Time limit exceeded3.046s12056 KiB
31Time limit exceeded3.058s12248 KiB
32Time limit exceeded3.049s12264 KiB
33Time limit exceeded3.065s12180 KiB
34Time limit exceeded3.078s12004 KiB
35Accepted1.595s16088 KiB
36Accepted1.1s16196 KiB
37Accepted865ms15916 KiB
38Accepted662ms16120 KiB
39Accepted632ms16120 KiB
40Accepted606ms16120 KiB