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
2016-02-01 20:17:10
1st problem on SPOJ
AC in one go ;)
2016-01-29 05:26:08
YO!! First DP question got ACed, DP is the coolest thing ever
2016-01-25 20:35:27 chandan kumar
Nice question for beginners in DP
2016-01-20 22:01:35
Good one ......!!!!!!!!!!!!
2016-01-19 19:24:25
I am actually a bit confused about the number of testcases. what is the number of test case(which is given maximum of 10) and how to input the no of test cases?
2016-01-19 10:30:14
Single shot AC :) :D :P nice & very simple question on DP......
Thanks for the comments below for warning for edge cases.
2016-01-17 20:46:35
@Divyaanand Sinha To manage unknown number of test cases (in c++) use for example while(cin>>n){//do sth}
2016-01-08 19:56:15 Divyaanand Sinha
How do I get to know the number of testcases?Should I assume that there are 10 cases?
2016-01-05 20:31:41
was literally going to jump off my terrace..! finally it said accepted..!!

anyway learned a lot..!

Last edit: 2016-01-05 20:33:27
2016-01-04 16:19:48
To take input in java DO something like this
while(true){
num = sc.nextInt();
System.out.println(findProfit(num,memo));
if(!sc.hasNextInt())break;
}
For memoization use a map of integer and BigInteger. :) Never forget to import BigInteger :'(
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.