103062024-03-30 13:31:03111Majomházcpp17Wrong answer 10/1003.086s87592 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,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
1Accepted3ms2112 KiB
2Accepted575ms10556 KiB
subtask210/10
3Accepted3ms2256 KiB
4Accepted3ms2456 KiB
5Accepted3ms2544 KiB
6Accepted3ms2548 KiB
7Accepted3ms2892 KiB
subtask30/10
8Accepted4ms2820 KiB
9Accepted13ms3072 KiB
10Wrong answer32ms3572 KiB
11Accepted75ms4700 KiB
12Accepted186ms7192 KiB
subtask40/20
13Accepted4ms3284 KiB
14Accepted28ms4144 KiB
15Accepted192ms6724 KiB
16Accepted1.521s21736 KiB
17Time limit exceeded3.058s19668 KiB
18Time limit exceeded3.046s24332 KiB
subtask50/29
19Wrong answer233ms10728 KiB
20Time limit exceeded3.053s18140 KiB
21Time limit exceeded3.062s19492 KiB
22Time limit exceeded3.062s20452 KiB
23Time limit exceeded3.055s22308 KiB
subtask60/31
24Accepted14ms6360 KiB
25Accepted14ms6360 KiB
26Accepted14ms6488 KiB
27Accepted17ms6912 KiB
28Accepted54ms9640 KiB
29Wrong answer423ms16400 KiB
30Time limit exceeded3.069s19220 KiB
31Time limit exceeded3.053s19368 KiB
32Time limit exceeded3.065s20312 KiB
33Time limit exceeded3.062s21560 KiB
34Time limit exceeded3.086s23132 KiB
35Time limit exceeded3.046s26228 KiB
36Time limit exceeded3.078s31344 KiB
37Time limit exceeded3.059s37832 KiB
38Time limit exceeded3.072s59912 KiB
39Time limit exceeded3.073s70108 KiB
40Time limit exceeded3.062s87592 KiB