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?


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.


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




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
anuragdw0710: 2023-03-15 07:22:34

Memo can be done with hashing.

r_aj1_23rj: 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

ive1010: 2022-03-21 19:43:52

Java code to read the input
Scanner sc = new Scanner(;
while (sc.hasNext()){
long n =sc.nextLong();
// add code here

rz_ai: 2022-01-13 21:31:36

Keep in mind the declaration of map must be outside the function

rinku_2002: 2022-01-11 07:37:19

when should we stop reading data?

levii_ackerman: 2021-09-03 03:01:48

easy basic dp problem

x_sparrow_x: 2021-09-03 02:03:34

AC in one Go
quite easy recursion problem

huangdachuan: 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
dev_manus: 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
the_art_maniac: 2021-05-16 20:29:29

Reason for why arrays are not working-: because using them will need a memory of 10^9 integers which is approx.. 10X times more than maximum limit , in maps memory is not wasted.

Added by:Tomek Czajka
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