Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. struct stanje {
  9.   int x, a, b, c;
  10.   void normalize() {
  11.     if( a > b ) swap( a, b );
  12.     if( a > c ) swap( a, c );
  13.     if( b > c ) swap( b, c );
  14.   }
  15.   stanje() {};
  16.   stanje( int _x, int _a, int _b, int _c ) { x = _x; a = _a; b = _b; c = _c; normalize(); }
  17.   friend bool operator < ( const stanje &a, const stanje &b ) {
  18.     if( a.x != b.x ) return a.x < b.x;
  19.     if( a.a != b.a ) return a.a < b.a;
  20.     if( a.b != b.b ) return a.b < b.b;
  21.     return a.c < b.c;
  22.   }
  23. };
  24.  
  25. int bio[50][500][500][8];
  26. int a[105], s[105];
  27. int ans, n;
  28.  
  29. inline int get( int x, int a, int b, int c ) {
  30.   if( (bio[x][a][b][c>>5]>>(c&31))&1 ) return 1;
  31.   bio[x][a][b][c>>5] |= 1<<(c&31);
  32.   return 0;
  33. }
  34.  
  35. void dfs( stanje x ) {
  36.   if( x.a == x.b && x.b == x.c && x.a > ans ) ans = x.a;
  37.   int w = x.a + x.b + x.c + s[x.x]; w /= 3;
  38.   if( w < ans || x.a > w || x.b > w || x.c > w ) return;
  39.   if( x.x == n ) return;
  40.  
  41.   if( get( x.x, x.a, x.b, x.c ) ) return;
  42.  
  43.   dfs( stanje( x.x+1, x.a, x.b, x.c ) );
  44.   dfs( stanje( x.x+1, x.a+a[x.x], x.b, x.c ) );
  45.   dfs( stanje( x.x+1, x.a, x.b+a[x.x], x.c ) );
  46.   dfs( stanje( x.x+1, x.a, x.b, x.c+a[x.x] ) );
  47. }
  48.  
  49. int main( void ) {
  50.   while( scanf( "%d", &n ) == 1 ) {
  51.     if( n == 0 ) break;
  52.  
  53.     int ss = 0;
  54.     for( int i = 0; i < n; ++i ) {
  55.       scanf( "%d", a+i );
  56.       ss += a[i];
  57.     }
  58.     //  printf( "%d\n", ss/3 );
  59.     s[n] = 0;
  60.     for( int i = n-1; i >= 0; --i )
  61.       s[i] = s[i+1] + a[i];
  62.  
  63.     ans = 0;
  64.     memset( bio, 0, sizeof( bio ) );
  65.  
  66.     dfs( stanje( 0, 0, 0, 0 ) );
  67.     printf( "%d\n", ans );
  68.   }
  69.   return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement