You need to set dp[n-1][2] = ar[n-1] ;

// because if there are only 1 or 2 value not exactly three in the indices n-1,n-2,n-3 i mean if the last three indices are not proper .We cannot find the minimun value if we dont store the last index value of the main array to our dp[][] array as it will be returning 0 each time as the minimum value without even considering the remaining index.