103082024-03-30 13:36:40111Majomházcpp17Runtime error 0/100238ms524876 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define INF (int)1e18

int pf[100001];

map<tuple<int,int,int>,int>mem;

int cost(int s,int e){
	return (pf[e]-pf[s])*(e-s);
}

int solve(int s,int e,int k){
	if(k==0){
		return cost(s,e);
	}
	if(mem.find({s,e,k})!=mem.end()){
		return mem[{s,e,k}];
	}
	int k1=k-2,k2=1;
	auto calc=[&](int i)->int{
		return solve(s,i,k1)+solve(i,e,k2);
	};
	int l=s+k1+1,h=e-k2-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;
		}
	}
	int res=INF;
	for(int i=l;i<=h;i++){
		res=min(res,calc(i));
	}
	mem[{s,e,k}]=res;
	return res;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<int>v(N);
	for(int i=0;i<N;i++){
		cin>>v[i];
	}
	for(int i=0;i<N;i++){
		pf[i+1]=pf[i]+v[i];
	}
	cout<<solve(0,N,K)<<'\n';
	// cout<<K<<" "<<mem.size()<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Runtime error180ms524876 KiB
2Runtime error219ms524636 KiB
subtask20/10
3Runtime error180ms524404 KiB
4Runtime error180ms524180 KiB
5Runtime error177ms523948 KiB
6Runtime error186ms523720 KiB
7Runtime error180ms523480 KiB
subtask30/10
8Runtime error177ms523372 KiB
9Runtime error225ms523140 KiB
10Runtime error180ms523136 KiB
11Runtime error221ms522904 KiB
12Runtime error219ms522676 KiB
subtask40/20
13Runtime error224ms522456 KiB
14Runtime error180ms522452 KiB
15Runtime error225ms522464 KiB
16Runtime error224ms522228 KiB
17Runtime error224ms522232 KiB
18Runtime error224ms522244 KiB
subtask50/29
19Runtime error226ms522228 KiB
20Runtime error180ms522228 KiB
21Runtime error180ms522036 KiB
22Runtime error180ms522040 KiB
23Runtime error228ms521808 KiB
subtask60/31
24Accepted14ms7900 KiB
25Runtime error234ms521808 KiB
26Runtime error231ms521816 KiB
27Runtime error234ms521792 KiB
28Runtime error234ms521816 KiB
29Runtime error231ms521824 KiB
30Runtime error231ms521816 KiB
31Runtime error236ms521588 KiB
32Runtime error234ms521560 KiB
33Runtime error236ms521564 KiB
34Runtime error238ms521584 KiB
35Runtime error233ms521576 KiB
36Runtime error234ms521588 KiB
37Runtime error190ms521396 KiB
38Runtime error189ms521396 KiB
39Runtime error186ms521392 KiB
40Runtime error231ms521400 KiB