Comments

@Warawreh

So in case when L[a[i]]!=i...then dp[i]=max(dp[i+1],cf[a[i]]+dp[r[a[i]+1]) but u are only considering cf[a[i]](maximum unmoved books from i to n-1)
cf[a[i]]+dp[r[a[i]+1]>=cf[a[i]] as term one is greater. so shouldn't it be like the way i have shown. I am vey confused over this..Please correct me.

Can you please explain your code/logic.TOMLAU