103002024-03-30 12:34:13111Majomházcpp17Wrong answer 10/1002.66s521632 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=1;i<=N;i++){
		cin>>v[i];
		pf[i]=pf[i-1]+v[i];
	}
	vector<vector<int>>dp(K+1,vector<int>(N+1,INF));
	vector<vector<int>>p(K+1,vector<int>(N+1));
	dp[0][0]=0;
	for(int i=1;i<=N;i++){
		dp[0][i]=pf[i]*i;
	}
	for(int i=1;i<=K;i++){
		for(int j=i;j<=N;j++){
			auto calc=[&](int k)->int{
				return dp[i-1][k]+(pf[j]-pf[k])*(j-k);
			};
			int l=0,h=j-1;
			while(h-l>=3){
				int m1=l+(h-l)/3;
				int m2=h-(h-l)/3;
				if(calc(m1)<=calc(m2)){
					h=m2;
				}
				else{
					l=m1;
				}
			}
			for(int k=l;k<=h;k++){
				int c=calc(k);
				if(c<dp[i][j]){
					dp[i][j]=c;
					p[i][j]=k;
				}
			}
		}
	}
	cout<<dp[K][N]<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted20ms5444 KiB
subtask210/10
3Accepted3ms2340 KiB
4Accepted3ms2632 KiB
5Accepted3ms2716 KiB
6Accepted3ms2728 KiB
7Accepted3ms2708 KiB
subtask30/10
8Accepted3ms3024 KiB
9Accepted3ms3304 KiB
10Wrong answer4ms3440 KiB
11Accepted4ms3960 KiB
12Accepted8ms4700 KiB
subtask40/20
13Accepted6ms4556 KiB
14Wrong answer8ms4888 KiB
15Accepted12ms5912 KiB
16Accepted39ms10672 KiB
17Accepted94ms20212 KiB
18Accepted449ms82820 KiB
subtask50/29
19Wrong answer71ms16484 KiB
20Accepted192ms32676 KiB
21Wrong answer620ms87700 KiB
22Wrong answer1.235s166404 KiB
23Wrong answer2.434s323620 KiB
subtask60/31
24Accepted17ms15092 KiB
25Accepted43ms18844 KiB
26Accepted70ms22616 KiB
27Accepted94ms26532 KiB
28Wrong answer120ms30308 KiB
29Accepted149ms34428 KiB
30Wrong answer277ms50680 KiB
31Wrong answer671ms98280 KiB
32Accepted1.332s177396 KiB
33Wrong answer2.66s334804 KiB
34Runtime error206ms521632 KiB
35Runtime error196ms521608 KiB
36Runtime error196ms521580 KiB
37Runtime error197ms521552 KiB
38Runtime error197ms521548 KiB
39Runtime error196ms521544 KiB
40Runtime error196ms521528 KiB