Echange Coin DP

Why i am getting run error for bottom up DP

see
check for the following things are missing
when the coin is divided it needs to be checked for
dp[op1] + dp[op2] + dp[op3]
and u have to assign max(n, dp[op1] + dp[op2] + dp[op3]) to the dp[i]

Here is my bottom-up impl : https://ideone.com/WNvAv7
Here is my top-down impl : https://ideone.com/JMTSIA
Bottom up code has complexity O(n.log(n)) and n=1e9, It will give TLE. Because in bottom up you are calculating all values of n from 2 to 1e9 in worst case, but for e.g all the values between 1e9-1 and 1e9/2+1 will never be used by dp[1e9] itself. Bottom up is not always feasible.