103072024-03-30 13:33:29111Majomházcpp17Wrong answer 10/1003.101s88768 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-1,k2=0;
	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
2Accepted578ms10428 KiB
subtask210/10
3Accepted3ms2320 KiB
4Accepted3ms2308 KiB
5Accepted3ms2520 KiB
6Accepted3ms2864 KiB
7Accepted3ms2836 KiB
subtask30/10
8Accepted4ms3148 KiB
9Accepted14ms3608 KiB
10Wrong answer32ms4272 KiB
11Accepted74ms5300 KiB
12Accepted186ms7948 KiB
subtask40/20
13Accepted4ms3972 KiB
14Accepted27ms4752 KiB
15Accepted193ms7460 KiB
16Accepted1.534s22312 KiB
17Time limit exceeded3.059s20324 KiB
18Time limit exceeded3.019s24744 KiB
subtask50/29
19Accepted230ms10968 KiB
20Time limit exceeded3.059s18792 KiB
21Time limit exceeded3.068s20256 KiB
22Time limit exceeded3.101s21608 KiB
23Time limit exceeded3.068s23148 KiB
subtask60/31
24Accepted14ms7320 KiB
25Accepted14ms7316 KiB
26Accepted14ms7628 KiB
27Accepted17ms8136 KiB
28Wrong answer56ms10976 KiB
29Wrong answer425ms17648 KiB
30Time limit exceeded3.072s20284 KiB
31Time limit exceeded3.059s20876 KiB
32Time limit exceeded3.058s21724 KiB
33Time limit exceeded3.059s23076 KiB
34Time limit exceeded3.078s24712 KiB
35Time limit exceeded3.075s27768 KiB
36Time limit exceeded3.072s32556 KiB
37Time limit exceeded3.065s38920 KiB
38Time limit exceeded3.038s61176 KiB
39Time limit exceeded3.068s71492 KiB
40Time limit exceeded3.066s88768 KiB