103272024-03-30 22:48:51111Majomházcpp17Wrong answer 40/1001.149s26328 KiB
// Lagrangian Relaxation & Li Chao Tree

#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 solve=[&](int y)->pair<int,int>{
		vector<int>dp(N+1,INF),cnt(N+1),t((N+1)*4);
		auto cost=[&](int s,int e)->int{
			return dp[s]+(pf[e]-pf[s])*(e-s);
		};
		auto add=[&](int i,int l,int r,int p)->void{
			while(true){
				int m=(l+r)/2;
				bool ll=cost(p,l)<cost(t[i],l);
				bool mm=cost(p,m)<cost(t[i],m);
				if(mm){
					swap(t[i],p);
				}
				if(l==r){
					break;
				}
				if(ll!=mm){
					i=i*2+1;
					r=m;
				}
				else{
					i=i*2+2;
					l=m+1;
				}
			}
		};
		auto get=[&](int i,int l,int r,int x)->int{
			int bc=INF,bi=0;
			while(true){
				int m=(l+r)/2;
				int c=cost(t[i],x);
				if(c<bc){
					bc=c;
					bi=t[i];
				}
				if(l==r){
					break;
				}
				if(x<m){
					i=i*2+1;
					r=m;
				}
				else{
					i=i*2+2;
					l=m+1;
				}
			}
			return bi;
		};
		dp[0]=-y;
		cnt[0]=-1;
		add(0,0,N,0);
		for(int i=1;i<=N;i++){
			int o=get(0,0,N,i);
			dp[i]=cost(o,i)+y;
			cnt[i]=cnt[o]+1;
			add(0,0,N,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
1Accepted3ms1824 KiB
2Accepted46ms2920 KiB
subtask210/10
3Accepted3ms2332 KiB
4Accepted3ms2504 KiB
5Accepted3ms2544 KiB
6Accepted3ms2628 KiB
7Accepted3ms2848 KiB
subtask310/10
8Accepted6ms2868 KiB
9Accepted6ms3136 KiB
10Accepted6ms3464 KiB
11Accepted6ms3316 KiB
12Accepted6ms3380 KiB
subtask420/20
13Accepted46ms3984 KiB
14Accepted48ms4016 KiB
15Accepted50ms4056 KiB
16Accepted54ms4096 KiB
17Accepted54ms4128 KiB
18Accepted52ms4288 KiB
subtask50/29
19Wrong answer519ms10244 KiB
20Wrong answer522ms10456 KiB
21Accepted538ms10924 KiB
22Accepted532ms11376 KiB
23Accepted550ms11652 KiB
subtask60/31
24Wrong answer1.08s18536 KiB
25Wrong answer1.09s19296 KiB
26Wrong answer1.074s20040 KiB
27Wrong answer1.088s20572 KiB
28Wrong answer1.083s21236 KiB
29Wrong answer1.082s22008 KiB
30Wrong answer1.082s22576 KiB
31Wrong answer1.083s23640 KiB
32Wrong answer1.085s24196 KiB
33Accepted1.087s24880 KiB
34Accepted1.128s25520 KiB
35Accepted1.139s25648 KiB
36Accepted1.146s25896 KiB
37Accepted1.149s25896 KiB
38Accepted1.149s25900 KiB
39Accepted1.118s26112 KiB
40Accepted1.09s26328 KiB