103092024-03-30 13:38:19111Majomházcpp17Wrong answer 20/1003.107s140364 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,k2;
	if(k==1){
		k1=0;
		k2=0;
	}
	else{
		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
1Accepted3ms1840 KiB
2Accepted913ms72356 KiB
subtask210/10
3Accepted3ms2296 KiB
4Accepted3ms2512 KiB
5Accepted3ms2568 KiB
6Accepted3ms2676 KiB
7Accepted3ms2652 KiB
subtask310/10
8Accepted3ms2960 KiB
9Accepted16ms4888 KiB
10Accepted43ms6756 KiB
11Accepted90ms8368 KiB
12Accepted217ms11552 KiB
subtask40/20
13Accepted3ms3660 KiB
14Wrong answer4ms3928 KiB
15Accepted188ms24600 KiB
16Wrong answer2.421s135948 KiB
17Time limit exceeded3.068s50320 KiB
18Time limit exceeded3.082s33364 KiB
subtask50/29
19Accepted12ms5796 KiB
20Time limit exceeded3.107s117000 KiB
21Time limit exceeded3.075s82556 KiB
22Time limit exceeded3.081s51324 KiB
23Time limit exceeded3.063s42156 KiB
subtask60/31
24Accepted14ms6820 KiB
25Accepted14ms6948 KiB
26Accepted14ms6956 KiB
27Accepted14ms7000 KiB
28Wrong answer17ms7348 KiB
29Accepted18ms7484 KiB
30Time limit exceeded3.045s140364 KiB
31Time limit exceeded3.068s113008 KiB
32Time limit exceeded3.082s83852 KiB
33Time limit exceeded3.072s54156 KiB
34Time limit exceeded3.052s42708 KiB
35Time limit exceeded3.059s35064 KiB
36Time limit exceeded3.072s34080 KiB
37Time limit exceeded3.048s37708 KiB
38Time limit exceeded3.072s53944 KiB
39Time limit exceeded3.072s66872 KiB
40Time limit exceeded3.082s86968 KiB