这是HDU第400个题。
1 #include <cstdio> 2 #include <cstring> 3 #include <set> 4 #include <iostream> 5 using namespace std; 6 int p[30],qur[60001][6]; 7 int main() 8 { 9 int i,j,k,n,m,num; 10 while(scanf("%d%d",&n,&m)!=EOF) 11 { 12 multiset<int> se[40]; 13 multiset<int>::iterator it; 14 for(k = 1; k <= n; k ++) 15 { 16 scanf("%d",&num); 17 if(num == 0) 18 { 19 for(i = 0; i < m; i ++) 20 { 21 scanf("%d",&p[i]); 22 qur[k][i] = p[i]; 23 } 24 for(i = 0; i < 1<<m; i ++) 25 { 26 int temp = 0; 27 for(j = 0; j < m; j ++) 28 { 29 if(i&(1<<j)) 30 temp += p[j]; 31 else 32 temp -= p[j]; 33 } 34 35 se[i].insert(temp); 36 } 37 } 38 else 39 { 40 scanf("%d",&num); 41 for(i = 0; i < 1<<m; i ++) 42 { 43 int temp = 0; 44 for(j = 0; j < m; j ++) 45 { 46 if(i&(1<<j)) 47 temp += qur[num][j]; 48 else 49 temp -= qur[num][j]; 50 } 51 it = se[i].find(temp); 52 se[i].erase(it); 53 } 54 } 55 int maxz = 0; 56 for(i = 0; i < 1<<m; i ++) 57 { 58 j = (~i)&((1<<m)-1); 59 int t1,t2; 60 it = se[i].end(); 61 it --; 62 t1 = (*it); 63 it = se[j].end(); 64 it --; 65 t2 = (*it); 66 if(maxz < t1 + t2) 67 maxz = t1 + t2; 68 } 69 printf("%d ",maxz); 70 } 71 } 72 return 0; 73 }