103242024-03-30 21:09:02111Majomházcpp17Runtime error 0/1003.101s40396 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+2);
		int o=0;
		auto bs=[&](int l,int i)->int{
			int 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(dp[j]+cost(j,i)<dp[o]+cost(o,i)){
					o=j;
				}
				if(j>=o){
					chk[bs(i+1,j)].push_back(j);
				}
			}
			dp[i]=dp[o]+cost(o,i)+y;
			cnt[i]=cnt[o]+1;
			chk[bs(i+1,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
1Runtime error3ms1936 KiB
2Accepted163ms3548 KiB
subtask20/10
3Runtime error3ms2212 KiB
4Accepted3ms2364 KiB
5Accepted3ms2576 KiB
6Accepted3ms2660 KiB
7Runtime error3ms2512 KiB
subtask30/10
8Accepted13ms2860 KiB
9Accepted14ms3072 KiB
10Accepted13ms3024 KiB
11Runtime error4ms3392 KiB
12Runtime error4ms3596 KiB
subtask40/20
13Accepted153ms4832 KiB
14Accepted178ms5260 KiB
15Accepted188ms5276 KiB
16Accepted179ms5616 KiB
17Accepted159ms5476 KiB
18Runtime error41ms5648 KiB
subtask50/29
19Wrong answer2.565s21736 KiB
20Wrong answer2.569s21848 KiB
21Accepted2.559s21696 KiB
22Accepted2.397s22100 KiB
23Accepted2.223s22036 KiB
subtask60/31
24Time limit exceeded3.062s21788 KiB
25Time limit exceeded3.068s21888 KiB
26Time limit exceeded3.062s21652 KiB
27Time limit exceeded3.065s21728 KiB
28Time limit exceeded3.075s21860 KiB
29Time limit exceeded3.101s21976 KiB
30Time limit exceeded3.073s21848 KiB
31Time limit exceeded3.075s21780 KiB
32Time limit exceeded3.071s21976 KiB
33Time limit exceeded3.066s21652 KiB
34Time limit exceeded3.066s21548 KiB
35Time limit exceeded3.049s21480 KiB
36Time limit exceeded3.033s21548 KiB
37Time limit exceeded3.053s21588 KiB
38Accepted2.96s40396 KiB
39Runtime error1.315s40264 KiB
40Runtime error1.187s40264 KiB