➤ Problem Link : 106C. Buns
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std;; #define ll long long int struct node { int a,b,c,d; }; typedef struct node Node; int main() { int n,m,c0,d0; cin>>n>>m>>c0>>d0; Node arr[m+2]; for(int i=2;i<=m+1;i++) cin>>arr[i].a>>arr[i].b>>arr[i].c>>arr[i].d; ll dp[m+2][n+1]; arr[1].a=99999; arr[1].b=1; arr[1].c=c0; arr[1].d=d0; memset(dp,0,sizeof(dp)); for(int i=1;i<=m+1;i++) { for(int j=1;j<=n;j++) { for(int k=0;k<=min(arr[i].a/arr[i].b,j/arr[i].c);k++) { dp[i][j]=max(dp[i][j],dp[i-1][j-k*arr[i].c]+k*arr[i].d); } } } cout<<dp[m+1][n]; }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!