Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <set>
  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. set< stanje > S;
  26. int a[105], s[105];
  27. int ans, n;
  28.  
  29. void dfs( stanje x ) {
  30.   if( S.find(x) != S.end() ) return;
  31.   S.insert(x);
  32.  
  33.   if( x.a == x.b && x.b == x.c && x.a > ans ) ans = x.a;
  34.   int w = x.a + x.b + x.c + s[x.x]; w /= 3;
  35.   if( w < ans || x.a > w || x.b > w || x.c > w ) return;
  36.   if( x.x == n ) return;
  37.  
  38.   dfs( stanje( x.x+1, x.a, x.b, x.c ) );
  39.   dfs( stanje( x.x+1, x.a+a[x.x], x.b, x.c ) );
  40.   dfs( stanje( x.x+1, x.a, x.b+a[x.x], x.c ) );
  41.   dfs( stanje( x.x+1, x.a, x.b, x.c+a[x.x] ) );
  42. }
  43.  
  44. int main( void ) {
  45.   while( scanf( "%d", &n ) == 1 ) {
  46.     if( n == 0 ) break;
  47.  
  48.     for( int i = 0; i < n; ++i )
  49.       scanf( "%d", a+i );
  50.     s[n] = 0;
  51.     for( int i = n-1; i >= 0; --i )
  52.       s[i] = s[i+1] + a[i];
  53.  
  54.     ans = 0;
  55.     S.clear();
  56.  
  57.     dfs( stanje( 0, 0, 0, 0 ) );
  58.     printf( "%d\n", ans );
  59.   }
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement