P1441-砝码称重
1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < b;i ++) 3 #define _rep(i,a,b) for(int i = (a);i > b;i --) 4 #define INF 0x3f3f3f3f 5 typedef long long ll; 6 using namespace std; 7 inline ll read() 8 { 9 ll ans = 0; 10 char ch = getchar(), last = ' '; 11 while(!isdigit(ch)) last = ch, ch = getchar(); 12 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 13 if(last == '-') ans = -ans; 14 return ans; 15 } 16 inline void write(ll x) 17 { 18 if(x < 0) x = -x, putchar('-'); 19 if(x >= 10) write(x / 10); 20 putchar(x % 10 + '0'); 21 } 22 int n,m; 23 int a[23]; 24 int dp[2003]; 25 int vis[23]; 26 int rnt = 0; 27 void dfs(int step,int rubb) 28 { 29 if(rubb>step || (step==n&&rubb!=m)) 30 return ; 31 32 if(step==n && rubb==m) 33 { 34 memset(dp,0,sizeof(dp)); 35 dp[0] = 1; 36 37 _for(i,1,n+1) 38 if(!vis[i]) 39 _rep(v,2001,0) 40 if(v>=a[i] && dp[v-a[i]]) 41 dp[v] = 1; 42 43 int t = 0; 44 _for(i,1,2003) 45 if(dp[i]) 46 t ++; 47 48 rnt = max(rnt,t); 49 return ; 50 } 51 52 dfs(step+1,rubb); 53 vis[step+1] = 1; 54 dfs(step+1,rubb+1); 55 vis[step+1] = 0; 56 } 57 int main() 58 { 59 n = read(), m = read(); 60 _for(i,1,n+1) 61 a[i] = read(); 62 63 dfs(0,0); 64 write(rnt); 65 return 0; 66 }