This is my approch of above problem
memoization technique
0--not preform any task
1--perform easy task
2--preform hard task
public static void main(String[] args) {
System.out.print("hi");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int type=sc.nextInt();
int[]ep=new int[n];
int[]dp= new int[n];
for(int i=0;i<n;i++) {
ep[i]=sc.nextInt();
dp[i]=sc.nextInt();
}
int[][]dpp=new int[ep.length][3];
for(int[]d:dpp) {
Arrays.fill(d, -1);
}
System.out.print(check(ep,dp,0,0,dpp));
}
public static int check(int[]easy,int[]diff,int prev,int i,int[][]dpp) {
if(i==easy.length) {
return 0;
}
if(dpp[i][prev]!=-1)return dpp[i][prev];
int notpick=check(easy,diff,0,i+1,dpp);
int easypick=easy[i]+check(easy,diff,1,i+1,dpp);
int diffpick=Integer.MIN_VALUE;
if(prev==0) {
diffpick=diff[i]+check(easy,diff,2,i+1,dpp);
}
return dpp[i][prev]=Math.max(notpick, Math.max(easypick, diffpick));
}