https://www.codechef.com/MARCH21C/problems/COLGLF4
This is the question, for which I have written the solution.
It shows Time Limit Exceeded(TLE) error.
Pls suggest how do I optimise it further.
I have optimised it to some extent. How do I Optimise it further. Pls Help!
Here in my code:
e-> egg,
h-> choclate
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#include <bits/stdc++.h>
using namespace std;
// help returns the min cost for n items
int help(int n, int e, int h, int& a, int& b, int& c, int *** dp){
if(n==0) return 0;
if(dp[h][e][n]!=-1) return dp[h][e][n];
int p1=INT_MAX,p2=INT_MAX,p3=INT_MAX;
if(e>=2){
int ans = help(n-1,e-2,h,a,b,c,dp);
// dp[h][e-2] = ans;
if(ans != INT_MAX) p1=ans + a;
}
if(h>=3){
int ans = help(n-1,e,h-3,a,b,c,dp);
// dp[h-3][e] = ans;
if(ans != INT_MAX) p2 = ans + b;
}
if(e>=1&&h>=1){
int ans = help(n-1, e-1,h-1,a,b,c,dp);
// dp[h-1][e-1] = ans;
if(ans != INT_MAX) p3= ans + c;
}
int f = min(p1,min(p2,p3));
dp[h][e][n] = f;
return f;
}
int main() {
// your code goes here
fastio();
int T;
cin>>T;
for(int t=0;t<T;t++){
int n,e,h,a,b,c;
cin>>n>>e>>h>>a>>b>>c;
// scanf("%d",&n);
// scanf("%d",&e);
// scanf("%d",&h);
// scanf("%d",&a);
// scanf("%d",&b);
// scanf("%d",&c);
int*** dp = new int**[h+1];
for(int i=0;i<=h;i++){
dp[i] = new int*[e+1];
for(int j=0;j<=e;j++){
dp[i][j] = new int[n+1];
for(int k=1;k<=n;k++) dp[i][j][k]=-1;
}
}
for(int i=0;i<=h;i++){
for(int j=0;j<=e;j++){
dp[i][j][0]=0;
}
}
int ans = help(n,e,h,a,b,c,dp);
if(ans == INT_MAX) cout<<-1<<'\n';
else cout<<ans<<'\n';
// for(int i=0;i<=h;i++){
// for(int j=0;j<=e;j++){
// cout<<dp[i][j]<<" ";
// }cout<<endl;
// }
for(int i=0;i<=h;i++){
for(int j=0;j<=e;j++){
delete [] dp[i][j];
}
delete dp[i];
}
delete [] dp;
}
return 0;
}