Advertisement
royalsflush

Referência errada para 562

Jun 10th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. #define MAXC (500*100)
  7. #define ABS(x) ((x)<0? -(x):(x))
  8.  
  9. bool dp[110][MAXC+10];
  10. int _42;
  11. int m;
  12. int c[110];
  13. int sum=0;
  14.  
  15. int main() {
  16.     scanf("%d", &_42);
  17.  
  18.     while (_42--) {
  19.         scanf("%d", &m);
  20.  
  21.         for (int i=0; i<m; i++)
  22.             scanf("%d", &c[i]);
  23.  
  24.         sum=0;
  25.         for (int i=0; i<m; i++)
  26.             sum+=c[i];
  27.  
  28.         memset(dp,0,sizeof(dp));
  29.         dp[0][0]=true;
  30.         dp[0][c[0]]=true;
  31.  
  32.         for (int i=1; i<m; i++)
  33.             for (int j=MAXC; j>=0; j--)
  34.                 if (dp[i-1][j])
  35.                     dp[i][j+c[i]]=dp[i][j]=true;
  36.  
  37.         int dif=MAXC+10;
  38.         for (int i=0; i<=500*100; i++)
  39.             if (dp[m-1][i])
  40.                 dif=min(dif,ABS(sum-2*i));
  41.         printf("%d\n", dif);
  42.     }
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement