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
2024-08-04 04:40:11
You can definitely solve it with an array. I did it in C. You can decide to memoize solutions for numbers smaller than the max you are willing to put in memory. Using that, and watching the maximum array I can put in memory keeping the program resident set size below the memory limit, I got it AC.
The max unsigned long long array I was able to put in memory to memoize was 180 000 000.
I also reused the array of solutions for all test cases.

Last edit: 2024-08-04 04:42:05
2023-03-15 07:22:34
Memo can be done with hashing.
2022-05-23 10:49:34
For C++ users=> Use while(cin>>n){} for taking the input, use map instead of arrays for memoization else you'll get an overflow. Use long long instead of int
2022-03-21 19:43:52
Java code to read the input
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
long n =sc.nextLong();
// add code here
}
2022-01-13 21:31:36
Keep in mind the declaration of map must be outside the function
2022-01-11 07:37:19
when should we stop reading data?
2021-09-03 03:01:48
easy basic dp problem
2021-09-03 02:03:34
AC in one Go
quite easy recursion problem
2021-08-22 21:17:17
It has to be long long, int does not work, why is that for the same formula when we only do division operation?

Last edit: 2021-08-22 21:17:52
2021-07-29 14:43:58
//just tell me whether this is right logic or I am wrong

return m[n]=max(n , solve(n/2) + solve(n/3) + solve(n/4) );

Last edit: 2021-07-29 15:04:10
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.