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
manvirag982: 2019-10-25 11:22:39

hey guys pls help me

what is the meaning of

/home/UrwtJs/cc0wNpVQ.o: in function `_GLOBAL__sub_I_dp':
prog.cpp:(.text.startup+0x117): relocation truncated to fit: R_X86_64_PC32 against `.bss'
prog.cpp:(.text.startup+0x135): relocation truncated to fit: R_X86_64_PC32 against `.bss'
collect2: error: ld returned 1 exit status

tarun_28: 2019-10-24 07:14:54

Easy one. Thanks to @rmcr714

jvictormv: 2019-10-15 08:18:48

Be careful using int instead of long long int

aman_atg: 2019-09-16 10:43:50

ac in one go!! yeah!

emilio_cosy: 2019-09-13 10:09:18

prefetch 10million coins' results, use this part as cache;
recursively solve the big number like 1billion, since we cached first 10 million results, actually, it just takes 8 steps (log100) to get the 1billion coin's solution, pretty fast:)

rockybuzz: 2019-08-26 08:55:10

though only DP' is accepted ...

siddharthvp: 2019-08-16 18:25:31

Recursion with memoization

mriow: 2019-08-08 19:52:07

DP is not efficient here, because it is not needed to solve all previous subproblems (for n, we dont need all 1, 2, 3... n-1 solutions).
The best approach is to use recursion with memoization.

addictd: 2019-08-01 13:19:27

how to take input with javascript

dhia01: 2019-07-28 06:02:00

precompute 100 million number, the rest just use dp

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