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
jllew: 2018-10-14 22:29:03

consider int overflow for big input

adipat: 2018-10-11 19:50:29

Classic DP problem. If you're using C++, consider using vector, it has a larger capacity so you can pre-compute more values. I used bottom up DP. You can't compute upto 10^9 values because it's too slow but you can definitely compute upto 10^8 without any considerable loss in speed.

suryacodes: 2018-10-10 18:24:36

And it said accepted !!

harry_shit: 2018-09-19 16:19:35

real happiness is getting AC. :)

anant6025: 2018-09-13 22:33:49

Wohoo finally!
First DP problem AC

v_ns: 2018-09-09 17:53:01

Very interesting problem, you can't store values upto 10^9 in array, think of using DP till values you can precompute, then use the old recursion technique.

anupam125: 2018-09-07 19:44:17

how 120 for n=100

strikas: 2018-09-07 08:46:48

use map instead of array, dp and recursion

Last edit: 2018-09-07 08:47:20
vipul_17: 2018-08-07 16:17:51

used unsigned long int for array in c;
DP,recursion and memoization.

ashish malgawa: 2018-08-04 11:04:40

Do we need to take in input number of test cases also?

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