Skip to content

Instantly share code, notes, and snippets.

@mhmoodlan
Created September 20, 2017 05:12
Show Gist options
  • Save mhmoodlan/c777fad760f2d070557c43c6f9579b7d to your computer and use it in GitHub Desktop.
Save mhmoodlan/c777fad760f2d070557c43c6f9579b7d to your computer and use it in GitHub Desktop.
#DP #MemorizingInMap #Solved #SPOJ
//http://www.spoj.com/problems/COINS/
#include <bits/stdc++.h>
#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)
using namespace std;
const int MAX = 15;
const int OO = 1e4;
map<int, ll> cache;
ll calcMax(int x) {
if(x <= 2)
return x == 0 ? 0 : x;
if(cache.find(x) != cache.end())
return cache[x];
ll &ret = cache[x];
ll y = calcMax(x/2) + calcMax(x/3) + calcMax(x/4);
ret = (x > y) ? x : y;
return ret;
}
int main() {
cache.clear();
int n;
while(cin>>n)
cout << calcMax(n) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment