103012024-03-30 12:35:15111Majomházcpp17Time limit exceeded 20/1003.099s522324 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=0;k<=j-1;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
2Accepted379ms5444 KiB
subtask210/10
3Accepted3ms2268 KiB
4Accepted3ms2500 KiB
5Accepted3ms2716 KiB
6Accepted3ms2948 KiB
7Accepted3ms3172 KiB
subtask310/10
8Accepted4ms3452 KiB
9Accepted4ms3664 KiB
10Accepted8ms3892 KiB
11Accepted14ms4104 KiB
12Accepted29ms5144 KiB
subtask40/20
13Accepted64ms4808 KiB
14Accepted107ms5188 KiB
15Accepted215ms6080 KiB
16Accepted870ms11104 KiB
17Accepted2.206s20468 KiB
18Time limit exceeded3.073s43248 KiB
subtask50/29
19Time limit exceeded3.071s10084 KiB
20Time limit exceeded3.082s18192 KiB
21Time limit exceeded3.068s45964 KiB
22Time limit exceeded3.072s85576 KiB
23Time limit exceeded3.075s164412 KiB
subtask60/31
24Accepted18ms14656 KiB
25Time limit exceeded3.099s12536 KiB
26Time limit exceeded3.066s14628 KiB
27Time limit exceeded3.065s16908 KiB
28Time limit exceeded3.042s19148 KiB
29Time limit exceeded3.073s21640 KiB
30Time limit exceeded3.066s30076 KiB
31Time limit exceeded3.029s54180 KiB
32Time limit exceeded3.069s94236 KiB
33Time limit exceeded3.066s173328 KiB
34Runtime error209ms522324 KiB
35Runtime error240ms522296 KiB
36Runtime error202ms522280 KiB
37Runtime error243ms522024 KiB
38Runtime error241ms522012 KiB
39Runtime error243ms521984 KiB
40Runtime error238ms521976 KiB