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.


hide comments
shantanu tripathi: 2015-08-18 20:28:02

rec + memo + maps :D

jyotman94: 2015-08-18 11:48:41

Good First DP Problem :)

abhishyam: 2015-08-15 06:07:29

At last after 2 weeks got it!!!

darkhire21: 2015-08-13 15:51:29

my first in dp..!!!

VIVEK GARG: 2015-08-10 20:41:34

See solution at :
<snip>

Last edit: 2022-08-09 22:48:58
satya_jha123: 2015-08-10 09:36:48

3 tle without dp,two wa then one segmentation fault .at last an ac
use eof in cin and to prevent segmentation don't store the result of very large number in your formed array instead return them there only

TUSHAR SINGHAL: 2015-08-04 05:48:14

so this time at the score of 98 ....here comes my first dp.....

einsdurchnull: 2015-07-29 00:19:22

From 59s (only recursion) to 0.009s (recursion with memoization in selfmade-hashtable :D).
Learned dp for this problem =)
Here some test data:
61->68, 777->1101, 55555->113327,999999999->4243218150

johntzwei: 2015-07-27 12:47:44

For those having NZEC errors (I did), check for the input case: 0. Here is some test data: 1000000000 - > 4243218150, 100 - > 120, 10 - > 10, 3 - > 3, and 0 - > 0.

kshitij_kohli: 2015-07-21 02:43:35

Is it NOT possible to solve this problems WITHOUT maps?

I'm using an array of size 10^6 and doing memorization as long as n < 10^6, for n greater than that it simply returns the value, however this approach gives a TLE.


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