Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ff first
- #define ss second
- #define m_p make_pair
- #define pb push_back
- #define ppb pop_back
- #define pf push_front
- #define ppf pop_front
- #define ll long long
- #define l_b lower_bound
- #define u_b upper_bound
- int ar[15];
- bool dp[1500];
- int used[1500];
- int stamp[15][15],len[15];
- int main()
- {
- int s;
- while(scanf("%d",&s)==1 && s)
- {
- int n;
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- scanf("%d",&len[i]);
- for(int j=1;j<=len[i];j++) scanf("%d",&stamp[i][j]);//input
- }
- int ch=-1,ans=0; // ans=final overall maximum cover , ch= appropriate set's index for maximum coverage
- for(int i=1;i<=n;i++) // loop for every set
- {
- memset(dp,0,sizeof dp);//initialization
- for(int ii=0;ii<=1050;ii++) used[ii]=1e9;//initialization
- dp[0]=1;//initialization
- used[0]=0;//initialization
- int mcov=0,mpos;//mcov= maximum cover for this set
- //mpos= hudai :P
- for(int j=0;j<=1050;j++)
- {
- if(!dp[j])
- {
- mcov=j-1; // found our range no more loop is need.
- break;
- }
- for(int k=1;k<=len[i];k++)//picking the coins of this set one by one
- {
- for(int l=1;l<=(s-used[j]);l++)
- {
- dp[j+stamp[i][k]*l]=1;// covering the next amounts
- used[j+stamp[i][k]*l]=min(used[j+stamp[i][k]*l],l+used[j]); //updating used
- }
- }
- }
- if(ch==-1 || mcov>ans) ans=mcov,ch=i;
- else if(mcov==ans) // in case of equality , finding the appropriate set
- {
- if(len[i]<len[ch]) ch=i;
- else if(len[i]==len[ch])
- {
- int ind=len[i];
- while(ind>0 && stamp[i][ind]==stamp[ch][ind]) ind--;
- if(ind && stamp[i][ind]<stamp[ch][ind]) ch=i;
- }
- }
- }
- printf("max coverage =%4d :",ans);//a bit typical output format
- for(int i=1;i<=len[ch];i++)
- {
- printf("%3d",stamp[ch][i]);//a bit typical output format
- }
- puts("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement