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.71 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[62][600][600][10];
  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 >=600 || x.b>=600 || x.c>=600 ) return;
  37.   if( x.a == x.b && x.b == x.c && x.a > ans ) ans = x.a;
  38.   int w = x.a + x.b + x.c + s[x.x]; w /= 3;
  39.   if( w < ans || x.a > w || x.b > w || x.c > w ) return;
  40.   if( x.x == n ) return;
  41.  
  42.   if( get( x.x, x.a, x.b, x.c ) ) return;
  43.  
  44.   dfs( stanje( x.x+1, x.a, x.b, x.c ) );
  45.   dfs( stanje( x.x+1, x.a+a[x.x], x.b, x.c ) );
  46.   dfs( stanje( x.x+1, x.a, x.b+a[x.x], x.c ) );
  47.   dfs( stanje( x.x+1, x.a, x.b, x.c+a[x.x] ) );
  48. }
  49.  
  50. int main( void ) {
  51.   while( scanf( "%d", &n ) == 1 ) {
  52.     if( n == 0 ) break;
  53.  
  54.     int ss = 0;
  55.     for( int i = 0; i < n; ++i ) {
  56.       scanf( "%d", a+i );
  57.       ss += a[i];
  58.      }
  59.     // printf( "%d\n", ss/3 );
  60.     s[n] = 0;
  61.     for( int i = n-1; i >= 0; --i )
  62.       s[i] = s[i+1] + a[i];
  63.  
  64.     ans = 0;
  65.     memset( bio, 0, sizeof( bio ) );
  66.  
  67.     dfs( stanje( 0, 0, 0, 0 ) );
  68.     printf( "%d\n", ans );
  69.   }
  70.   return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement