Prerequisite:-
Dynamic programming
Solution:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
long long ans(long long n,vector<long long>&dp){ | |
if(n==0) | |
return 0; | |
if(dp[n]) | |
return dp[n]; | |
return dp[n]=max(n,ans(n/2,dp)+ans(n/3,dp)+ans(n/4,dp)); | |
} | |
int main(){ | |
long long n; | |
while(scanf("%lld",&n)!=EOF){ | |
//cin>>n; | |
//cout<<"n= "<<n<<endl; | |
vector<long long>dp(n+1,0); | |
cout<<ans(n,dp)<<endl; | |
} | |
return 0; | |
} |
No comments:
Post a Comment