COINS - Bytelandian gold coins

In Byteland they have a very strange monetary system.

Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin. What is the maximum amount of American dollars you can get for it?

Input

The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.

Output

For each test case output a single line, containing the maximum amount of American dollars you can make.

Example

Input:
12
2

Output:
13
2

You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them. It is better just to change the 2 coin directly into $2.


Added by:Tomek Czajka
Date:2005-05-03
Time limit:9s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:Purdue Programming Contest Training

hide comments
2017-08-17 12:17:51
"not more than 10" costs me 5 WA's
2017-08-11 21:21:47
It can be solved by simple recursion ,but DP approach makes it faster to solve....AC in 0.00sec
2017-08-11 21:20:21
i donot think there is a better question than it to test ur concepts in dp!!!!!!!!
thnx@Tomek Czajka
2017-08-11 20:30:57
the reason behind calling this problem as among the first good dp problem is because it helps us imagining recursion to solve overlapping problems along with memoization for subproblems

Last edit: 2017-08-11 20:31:18
2017-07-28 10:32:55
given input is wrong ? there is no value of number of testcases given:P
2017-07-27 16:26:21
my first dp problem :)). Initially had coded wrongly but then sample test cases from spoj toolkit helped me to find out the mistake and bingo! ac XD
2017-07-23 15:52:12
i have solved this problem with simple recursions with a little modifications, i am now learning dp, can anyone share code for that in c++
2017-07-22 08:46:24
solved in one go use simply recursion

2017-07-20 12:30:59
bottom up approach using map gives TLE, big time, can't figure out why???
<snip>

Last edit: 2022-08-09 22:48:51
2017-07-18 16:47:41
Very straightforward DP problem. The only thing you have to keep in your mind is that you cannot use DP with memoization for N as big as 10^9 . You can settle for some number in order of 10^5 and then do recursion for N bigger than that. That's AC in 0.00 .
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.