CLEARARR - Editorial

This is what I have done as it is correct but give tle :

#include <bits/stdc++.h>
using namespace std;
 
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define int long long
#define w(t) int t;cin>>t;while(t--) 
#define mkarr(arr,n,type) type *arr=new type[n]
#define vi vector<int>
#define si set<int>
#define umii unordered_map<int,int>
#define umib unordered_map<int,bool>
#define mii map<int,int>
#define pii pair<int,int>
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vector<int> ,greater<int> >
#define qi queue<int>
#define sti stack<int>
#define li list<int>
#define fr(i,n) for(int i=0;i<n;i++)
#define db double 
#define f first
#define s second
#define mkp make_pair
#define pb push_back
 
double PI=3.14159265358979323846264338327950L;
 
int32_t main()
{ 
    
fio;
w(t)
{ 
           int n , m , k ; 
           cin>>n >> k >> m; 
             vector< int > v(n); 
               for(int i = 0; i<n ; i++){ 
                  cin>>v[i];                                                  
               }
 vector< vector< pair< int , int >  >>dp(n , vector<  pair< int , int > >(n)); 
    
         for(int i = 0; i< n; i++){ 
        for(int j = 0; j<n;j++){ 
               dp[i][j] = {0,0} ;                                              
        }                                           
       }
          for(int i = 0; i<n; i++){ 
               dp[i][i] = {v[i] , 0};                                             
       }
       
       
            for(int gap = 1 ; gap <n ; gap++){ 
               for(int i = 0 , j = gap ; j<n ; i++ , j++){ 
                       int sum = INT_MAX; 
                       int pos = INT_MAX; 
                       for(int l =i; l<j ; l++){ 
                         if(dp[i][l].second + dp[l+1][j].second <= k){ 
                        if(sum > dp[i][l].first + dp[1+l][j].first)  { 
                          sum = dp[i][l].first + dp[1+l][j].first ;                               pos=dp[i][l].second + dp[1+l][j].second;         
                        }
                 else if(sum == dp[i][l].first + dp[1+l][j].first)  { 
                          sum = dp[i][l].first + dp[1+l][j].first ;                               pos= min(pos , dp[i][l].second + dp[1+l][j].second);           }                                                    
                         } }
                if(dp[i+1][j-1].second + 1 <= k){ 
                    if(sum > dp[i+1][j-1].first + m)  { 
                          sum = dp[i+1][j-1].first + m;                                        pos=dp[i+1][j-1].second + 1;         
                        }
                  if(sum ==dp[i+1][j-1].first + m)  { 
                           sum = dp[i+1][j-1].first + m;                                pos= min(pos , dp[i+1][j-1].second + 1);           } }                                 
                           dp[i][j] = {sum , pos};
               }                                                 
            }
            cout<<dp[0][n-1].first<<endl;
 }return 0;
}

hmm, actually the order of elements in array matters only if the order of operations matters now forget sorting and u have the given array now according to the editorialist’s choice mark each element as 1 or 2 according to the operations decided to do with them now once given these markings start applying the first operation from front as if the front element is 1 delete it now u will only stop if array is empty or 2 comes in front now u start keep applying the operation 1 on the rear of array and only stop if 2 comes on rear now there is 2 on both sides of arrays u can now apply the 2nd operation keep doing it if 1 comes either side start doing first again
example:-
12112212->2112212->11221->1221->221->22->empty
22111212->211121->21112->111->11->1->empty
this was nothing to do with coding only explained that order doesn’t matter

i feel if we use dp it will take n^3 space so that should not work

// Problem: Clear the Array
// Contest: CodeChef - Practice(Easy)
// URL: Contest Page | CodeChef
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include
#include
#include<unordered_map>
#include
#include<unordered_set>
#include
#include
#include
#include
#include<string.h>
#include
#include
#include<math.h>
#include
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define d1(x) cout<<#x<<": β€œ<<x<<endl
#define d2(x, y) cout<<#x<<”: β€œ<<x<<” | β€œ<<#y<<”: β€œ<<y<<endl
#define d3(x, y, z) cout<<#x<<”:" <<x<<" | β€œ<<#y<<”: β€œ<<y<<” | β€œ<<#z<<”: β€œ<<z<<endl
#define d4(a, b, c, d) cout<<#a<<”: β€œ<<a<<” | β€œ<<#b<<”: β€œ<<b<<” | β€œ<<#c<<”: β€œ<<c<<” | β€œ<<#d<<”: β€œ<<d<<endl
#define d5(a, b, c, d, e) cout<<#a<<”: β€œ<<a<<” | β€œ<<#b<<”: β€œ<<b<<” | β€œ<<#c<<”: β€œ<<c<<” | β€œ<<#d<<”: β€œ<<d<<” | "<<#e<< ": β€œ<<e<<endl
#define d6(a, b, c, d, e, f) cout<<#a<<”: β€œ<<a<<” | β€œ<<#b<<”: β€œ<<b<<” | β€œ<<#c<<”: β€œ<<c<<” | β€œ<<#d<<”: β€œ<<d<<” | "<<#e<< ": β€œ<<e<<” | β€œ<<#f<<”: "<<f<<endl

    /*SOME SHORTCUTS*/    

#define f(i,a,b) for(int i=a;i<b;i++)
#define fr(i,a,b) for(int i=a;i>=b;i–)
#define lb lower_bound
#define ub upper_bound
#define ll long long int
#define pb push_back
#define pf push_front
#define lcm(a, b) ((a) * (b)) / __gcd(a, b)
#define mp make_pair
#define in insert
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
// #define memset(a) memset(a,0,sizeof(a))
const int MOD = 1e9 + 7;
const int mod = 1e9 + 7;
int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,ans1=0,ans2=0,ans3=0;
// bool prime[2000001];
bool prime[1000001];
/*int f[400005],inverse[400005];
int divide(int n)
{return pow1(n,mod-2); }

int ncr(int n, int r)
{if(n<r) return 0;return (f[n]*((divide(f[r]) * divide(f[n-r])) % mod)) % mod; } */

bool comp(string s1, string s2){
if(s1.size()>s2.size()){
return true;
}
else if(s1.size()<s2.size()){
return false;
}
else{
if(s1>s2){
return false;
}
else{
return true;
}
}
}
void sieve() {

memset(prime, true, sizeof(prime));	
for (int i = 2; i * i <= 1000000; i++)
if (prime[i])	
	for (int j = i * i; j <= 1000000; j+= i)
		prime[j] = false;
prime[0] = prime[1] = false;

}
int n,k,x;
int a[5002];
int find1(int l ,int r ,int cnt1){
if(l>r){
return 0;
}
if(l==r){
return a[l];
}
if(cnt1<k){
return min(a[l]+find1(l+1,r,cnt1),min(a[r]+find1(l,r-1,cnt1) ,x+find1(l+1,r-1,cnt1+1)));
}
return min(a[l]+find1(l+1,r,cnt1), a[r]+find1(l,r-1,cnt1));
}
void solve(){
cin>>n>>k>>x;
f(i,0,n){
cin>>a[i];
}
int z1=find1(0,n-1,0);
cout<<z1<<endl;
return ;
}
signed main(){
//sieve();
IOS;
int t=1;
cin>>t;
while(t–){
solve();
}
return 0;
}

hey dont you feel you should had made 3d dp to take count of k also i.e no of time both end and start was also removed