Twin Towers DP.
DP[I][J] Represents the first I items, divided into two piles (can not be used), the value of the difference is J, the smaller pile of value is dp[i][j].
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;intdp[ the][4000+Ten];inta[ the];intn, sum;voidRead () { for(inti =1; I <= N; i++) scanf ("%d", &a[i]);}voidWork () {sum=0; for(inti =1; I <= N; i++) sum = sum +A[i]; Memset (DP,-1,sizeofDP); intH H = a[1]; dp[1][h + sum] = dp[1][0-H + sum] = dp[1][sum] =0; for(inti =2; I <= N; i++) {h=A[i]; for(intj =0; J <= sum*2; J + +) Dp[i][j] = dp[i-1][j]; for(intj =0; J <= sum*2; J + +) { if(Dp[i-1][J] = =-1)Continue; intTMP = J-sum; if(TMP >=0) {dp[i][h+ tmp + sum] = max (dp[i][h + tmp + sum), dp[i-1][j]); Dp[i][tmp-H + sum] = max (dp[i][tmp-h + sum), dp[i-1][J] +min (tmp, h)); } Else if(tmp<0) {tmp= -tmp; dp[i][0-(tmp + h) + sum] = max (dp[i][0-(tmp + h) + sum], dp[i-1][j]); Dp[i][h-tmp + sum] = max (dp[i][h-tmp + sum), dp[i-1][J] +min (tmp, h)); } } } intAns =0; for(inti =1; I <= N; i++) ans =Max (ans, dp[i][sum]); if(ans = =0) printf ("sorry\n"); Elseprintf"%d\n", ans);}intMain () { while(~SCANF ("%d", &N)) {read (); if(N <0) Break; if(N <=1) printf ("sorry\n"); ElseWork (); } return 0;}
ZOJ 2059 the Twin Towers