Language: C++

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 99999999999999


ll** memo;
ll** memo2;

enum Type{
	max_value,
	min_value,
};

struct RMQ{
	void set(ll arg_minimum,ll arg_maximum){
		minimum = arg_minimum;
		maximum = arg_maximum;
	}
	ll minimum,maximum;
};

int N = 1;
RMQ* Tree;

void init(int first_N){
	while(N < first_N)N *= 2;
}

void update(int index, ll orange){

	index += N-1;

	Tree[index].minimum = min(Tree[index].minimum,orange);
	Tree[index].maximum = max(Tree[index].maximum,orange);

	index = (index-1)/2;

	while(true){

		Tree[index].minimum = min(Tree[2*index+1].minimum,Tree[2*index+2].minimum);
		Tree[index].maximum = max(Tree[2*index+1].maximum,Tree[2*index+2].maximum);

		if(index == 0)break;
		else{
			index = (index-1)/2;
		}
	}
}


ll find(int search_left,int search_right,int node_id,int node_left,int node_right,Type type){

	if(search_right < node_left || search_left > node_right){
		if(type == max_value){
			return -1;
		}else{ //type == min_value;
			return NUM;
		}
	}

	if(search_left <= node_left && search_right >= node_right){
		if(type == max_value){
			return Tree[node_id].maximum;
		}else{
			return Tree[node_id].minimum;
		}
	}

	if(type == min_value){
		ll left_min = find(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2,type);
		ll right_min = find(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right,type);

		return min(left_min,right_min);
	}else{ //type == max_value
		ll left_max = find(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2,type);
		ll right_max = find(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right,type);

		return max(left_max,right_max);
	}
}

ll box_Cost;
int first_N,contain_Max;

ll recursive(int left,int width){

	ll ret = 0,next = NUM;
	if(memo[left][width] != -1){
		return memo[left][width];
	}else{

		if(memo2[left][right] != -1){
			ret = memo2[left][right];
		}else{
			ll min_orange = find(left,min(N-1,left+width-1),0,0,N-1,min_value);
			ll max_orange = find(left,min(N-1,left+width-1),0,0,N-1,max_value);
			memo2[left][right] = box_Cost+(ll)width*(max_orange-min_orange);
			ret = memo2[left][right];
		}
	}

	if(left + width <= first_N-1){
		for(int i = 1; i <= contain_Max; i++){
			next = min(next,recursive(left+width,i));
		}
		return memo[left][width] = ret + next;
	}else{
		return memo[left][width] = ret;
	}
}

void func(){

	ll ans = NUM;

	for(int i = 1; i <= contain_Max; i++){
		ans = min(ans,recursive(0,i));
	}

	printf("%lld\n",ans);
}


int main(){

	scanf("%d %d %lld",&first_N,&contain_Max,&box_Cost);

	init(first_N);

	memo = new ll*[first_N];
	memo2 = new ll*[first_N];
	for(int i = 0; i < first_N; i++){
		memo[i] = new ll[contain_Max+1];
		memo2[i] = new ll[contain_Max+1];
		for(int k = 0; k <= contain_Max; k++){
			memo[i][k] = -1;
			memo2[i][k] = -1;
		}
	}

	Tree = (RMQ*)malloc(sizeof(RMQ)*(2*N));

	for(int i = 0; i < 2*N-1; i++){
		Tree[i].set(NUM,-1);
	}

	ll tmp;
	for(int i = 0; i < first_N; i++){
		scanf("%lld",&tmp);
		update(i,tmp);
	}

	func();

	return 0;
}

Compiler output:
a.cc: In function 'll recursive(int, int)':
a.cc:106:31: error: invalid types 'll* {aka long long int*}[std::ios_base&(std::ios_base&)]' for array subscript
  106 |                 if(memo2[left][right] != -1){
      |                               ^
a.cc:107:42: error: invalid types 'll* {aka long long int*}[std::ios_base&(std::ios_base&)]' for array subscript
  107 |                         ret = memo2[left][right];
      |                                          ^
a.cc:111:36: error: invalid types 'll* {aka long long int*}[std::ios_base&(std::ios_base&)]' for array subscript
  111 |                         memo2[left][right] = box_Cost+(ll)width*(max_orange-min_orange);
      |                                    ^
a.cc:112:42: error: invalid types 'll* {aka long long int*}[std::ios_base&(std::ios_base&)]' for array subscript
  112 |                         ret = memo2[left][right];
      |                                          ^