103042024-03-30 13:18:50111Majomházcpp17Wrong answer 20/1003.111s191372 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+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';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1708 KiB
2Accepted1.277s116752 KiB
subtask210/10
3Accepted3ms2132 KiB
4Accepted3ms2344 KiB
5Accepted3ms2700 KiB
6Accepted3ms2904 KiB
7Accepted3ms2808 KiB
subtask310/10
8Accepted3ms3112 KiB
9Accepted10ms4132 KiB
10Accepted75ms8480 KiB
11Accepted319ms14560 KiB
12Accepted794ms20504 KiB
subtask40/20
13Accepted3ms3896 KiB
14Accepted4ms4056 KiB
15Wrong answer45ms9588 KiB
16Time limit exceeded3.062s111852 KiB
17Time limit exceeded3.102s45408 KiB
18Time limit exceeded3.065s31360 KiB
subtask50/29
19Accepted12ms5920 KiB
20Accepted435ms50444 KiB
21Time limit exceeded3.094s174024 KiB
22Time limit exceeded3.079s159148 KiB
23Time limit exceeded3.062s115268 KiB
subtask60/31
24Accepted14ms7340 KiB
25Accepted14ms7228 KiB
26Accepted14ms7280 KiB
27Accepted16ms7232 KiB
28Wrong answer17ms7660 KiB
29Wrong answer19ms7852 KiB
30Accepted224ms31980 KiB
31Time limit exceeded3.061s185832 KiB
32Time limit exceeded3.111s191372 KiB
33Time limit exceeded3.071s162468 KiB
34Time limit exceeded3.078s156268 KiB
35Time limit exceeded3.078s73312 KiB
36Time limit exceeded3.078s31828 KiB
37Time limit exceeded3.071s31604 KiB
38Time limit exceeded3.078s35560 KiB
39Time limit exceeded3.068s38372 KiB
40Time limit exceeded3.075s47204 KiB