Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int N, ans;
- int songs[31];
- int dp[31][100];
- int mark[31][100];
- int solve (int i, int now, int n)
- {
- if (i==n)
- return 0;
- if (dp[i][now]!=-1) return dp[i][now];
- int r1=0, r2=0;
- if (now+songs[i]<=N)
- r1=songs[i]+solve(i+1, now+songs[i], n);
- r2=solve(i+1, now, n);
- int mx;
- if (r1>=r2) {
- mx=r1;
- mark[i][now]=1;
- }
- else {
- mx=r2;
- mark[i][now]=2;
- }
- return dp[i][now]=mx;
- }
- void print (int i, int now)
- {
- if (mark[i][now]==-1)
- return;
- if (mark[i][now]==1 && dp[i][now]<=ans) {
- cout<<songs[i]<<" ";
- print(i+1, now+songs[i]);
- }
- else if (mark[i][now]==2)
- print(i+1, now);
- }
- int main()
- {
- int k;
- while (cin>>N) {
- cin>>k;
- for (int i=0; i<k; i++) {
- cin>>songs[i];
- }
- memset(dp, -1, sizeof dp);
- memset(mark, -1, sizeof mark);
- ans=solve(0, 0, k);
- print(0, 0);
- cout<<"sum:"<<ans<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement