103052024-03-30 13:22:34111Majomházcpp17Runtime error 20/1001.177s117568 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=(k-1)/2;
	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;
	if(N*K>100000)exit(1);
	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';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1832 KiB
2Accepted1.177s117568 KiB
subtask210/10
3Accepted3ms2300 KiB
4Accepted2ms2388 KiB
5Accepted3ms2412 KiB
6Accepted3ms2764 KiB
7Accepted3ms2980 KiB
subtask310/10
8Accepted3ms2892 KiB
9Accepted9ms3888 KiB
10Accepted63ms8160 KiB
11Accepted266ms14200 KiB
12Accepted689ms20136 KiB
subtask40/20
13Accepted3ms3348 KiB
14Accepted4ms3588 KiB
15Accepted43ms9176 KiB
16Runtime error3ms3328 KiB
17Runtime error2ms3268 KiB
18Runtime error2ms3268 KiB
subtask50/29
19Runtime error2ms3268 KiB
20Runtime error3ms3496 KiB
21Runtime error3ms3704 KiB
22Runtime error3ms3692 KiB
23Runtime error3ms3688 KiB
subtask60/31
24Accepted14ms6576 KiB
25Accepted16ms6876 KiB
26Runtime error3ms4168 KiB
27Runtime error2ms4204 KiB
28Runtime error3ms4208 KiB
29Runtime error2ms4208 KiB
30Runtime error2ms4208 KiB
31Runtime error2ms4208 KiB
32Runtime error2ms4200 KiB
33Runtime error2ms4120 KiB
34Runtime error2ms4208 KiB
35Runtime error3ms4252 KiB
36Runtime error2ms4132 KiB
37Runtime error2ms4220 KiB
38Runtime error2ms4132 KiB
39Runtime error2ms4128 KiB
40Runtime error2ms4220 KiB