#include <cstdlib> n #include <cstdarg> n #include <cassert> n #include <cctype> // tolower n #include <ctime> n #include <cmath> n  n #include <iostream> n #include <sstream> n #include <fstream> n #include <iomanip> n #include <stdexcept> n  n #include <map> n #include <tuple> n #include <unordered_map> n #include <set> n #include <list> n #include <stack> n #include <queue> n #include <vector> n #include <string> n  n #include <limits> n #include <utility> n #include <memory> n  n #include <numeric> n #include <iterator> n #include <algorithm> n #include <functional> n  n /* n  * g++ -g -std=c++11 -DBUG -D_GLIBCXX_DEBUG -Wall -Wfatal-errors -o cforce{,.cpp} n  * n  * TODO: n  *      C++ dataframe n  *      stl11 -> c++11 standard template lib in c++98 n  *      overload >> for map and set, using (insert) iterator n  *      chmod:: consider an algorithm stable to int64 overflow n  *      shortest path algorithm n  *      shortest path in a tree n  *      maximum network flow n  *      partial idx/iter sort n  *      a prime number generator which traverses prime numbers w/ ++ n  *      a divisor generator which traverses divisors efficiently n  *      Apply1st ?! n  *      Apply2nd and bind2nd ?! n  *          count_if ( a.begin(), a.end(), a.second < x ) n  *      Arbitrary-precision arithmetic / Big Integer / Fraction - rational num n  *      tuple class --> cplusplus.com/reference/tuple n  *      get<1>, get<2>, bind2nd ( foo ( get<2> pair ), val ) n  *      isin( const T & val, first, last ) n  *      fuinction composition in c++ n  *          blogea.bureau14.fr/index.php/2012/11/function-composition-in-c11/ n  *          cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition/ n  *      TimeWrapper -- accumulate time of a function call n  *          stackoverflow.com/questions/879408 n  *      hash map -- possible hash value & obj % some_big_prime [ b272 ] n  *          lower level can be a simple map to resolve hash collisions n  *      add explicit everywhere necessary n  *      bloom filters n  *      heap -> how to update values in place / increase-key or decrease-key ... IterHeap ?! n  *      median maintaince --> max_heap / min_heap n  *      prim s min spaning tree alg. O ( m log n ) using heap contianing V - X vertices n  *      kruskal algorithm minimum cost spanning tree with union find data structure n  *      unique_ptr n  *      hufman codes n  *      simple arithmatic tests n  *      longest common subsequence using seq. alignment type algorithm n  *      longest common substring ( consequative subsequeance ) n  *      Graham scan; en.wikipedia.org/wiki/Graham_scan n  *      problem/120/F -- how to extend in order to calculate all-pair distances in a tree n  *      compile time prime number calculator using templates n  *      swith to type_cast < T1, T2 > for val2str & str2val n  *      overload plus<> for two vectors?! n  *      overloading operator << for tuple: n  *          stackoverflow.com/questions/9247723 n  *      bitmask class with iterator protocol n  *      ??? segment tree +++ interval tree n  *      ??? an lru cache: a heap which maintains last access, an a map which maintains key:value n  *      ??? in lazy-union how to query size of a connected component( 437/D ) n  */ n  n /* n  * @recepies n  * ---------------------------------------------- n  * odd / even n  *      transform ( x.begin(), x.end(), x.begin(), bind2nd( modulus<int>(), 2 )); n  *      count_if ( x.begin(), x.end(), bind2nd( modulus < int > (), 2)); n  * Apply2nd n  *      max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) n  *      sort ( a.begin(), a.end(), Apply2nd < char, int , greater< int > > ) n  *      count_if ( m.begin(), m.end(), Apply2nd < string, int, modulus < int > > ) n  *      accumulate ( m.begin(), m.end(), 0.0, Apply2nd < int, double, plus < double > > ) n  *      accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) n  * abs_diff n  *      adjacent_difference ( a.begin(), a.end(), adj_diff.begin( ), abs_diff < int > ( ) ) n  *      accumulate ( a.begin(), a.end(), 0, abs_diff < int > ( ) ) n  * erase n  *      a.erase ( remove_if ( a.begin( ), a.end( ), bind2nd ( less < int >( ), 0 ) ), a.end( ) ) n  *      a.erase ( remove ( a.begin( ), a.end( ), b.begin( ), b.end( ) ), a.end ( ) ) n  * binding n  *      bind2nd ( mem_fun_ref (& ClassName::m_func ), arg )  // binds the argument of the object n  * iterator generators n  *      generate_n ( back_inserter ( a ), n, rand );  // calls push_back n  *      generate_n ( inserter( a, a.begin( ) + 5 ), 10, RandInt( 0 , 100 ) )  // calls insert n  *      copy ( foo.begin( ), foo.end( ), insert_iterator < list < double > > ( bar, bar.begin( ) + 5 )) n  * copy ( a.begin( ), a.end( ), ostream_iterator < double > ( cout,  ,   )) n  * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) n  * transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length)); n  */ n  n /* n  * @good read n  * ---------------------------------------------- n  *  [ partial ] template specialization n  *      cprogramming.com/tutorial/template_specialization.html n  *  function composition n  *      cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition n  */ n  n /* n  * @prob set n  * ---------------------------------------------- n  *  purification --> c330 n  */ n  n /* n  * @limits n  * ---------------------------------------------- n  * int                  31    2.14e+09 n  * long int             31    2.14e+09 n  * unsigned             32    4.29e+09 n  * long unsigned        32    4.29e+09 n  * size_t               32    4.29e+09 n  * long long int        63    9.22e+18 n  * long long unsigned   64    1.84e+19 n  */ n  n /* n  * issues n  * ---------------------------------------------- n  * stackoverflow.com/questions/10281809 n  * mem_fun -> func_obj ( pointer to instance, origanal argument ) n  * bind1st ( mem_fun ( & ClassName::m_func ), this )    // binds obj of the class n  * bind1st takes  const T &  as the first argument n  */ n  n /* n  *  typedef / define n  * ---------------------------------------------- n  */ n  n typedef          long long int  int64; n typedef unsigned long long int uint64; n  n #ifndef M_PI n #define M_PI 3.14159265358979323846L n #endif n  n #define DOUBLE_INF numeric_limits< double >::infinity() n #define DOUBLE_NAN numeric_limits< double >::quiet_NaN() n  n #define __STR__(a) #a n #define STR(a) __STR__(a) n  n #define ASSERT(expr, msg)   n     if (!( expr ))   n         throw runtime_error(__FILE__  :  STR(__LINE__)   -   msg); n  n #define DECLARE( X )   n     typedef       shared_ptr < X >  X ## _shared_ptr;   n     typedef const shared_ptr < X >  X ## _const_shared_ptr; n  n #ifdef BUG n     #define DEBUG(var) { cout << #var <<  :   << (var) << endl; } n     #define COND_DEBUG(expr, var) if (expr)   n         { cout << #var <<  :   << (var) << endl; } n     #define EXPECT(expr) if ( ! (expr) ) cerr <<  Assertion     n         << #expr   failed at   << __FILE__ <<  :  << __LINE__ << endl; n #else n     #define DEBUG(var) n     #define COND_DEBUG(expr, var) n     #define EXPECT(expr) n #endif n  n #define DBG(v) copy( v.begin(), v.end(), ostream_iterator < typeof( *v.begin() )> ( cout,     ) ) n  n #if __cplusplus < 201100 n     #define move( var ) var n #endif n  n /* n  * http://rootdirectory.de/wiki/SSTR() n  * usage: n  *      SSTR(  x^2:   << x*x ) n  */ n #define SSTR( val ) dynamic_cast< ostringstream & >( ostringstream() << dec << val ).str() n  n using namespace std; n  n /* https://www.quora.com/C++-programming-language/What-are-some-cool-C++-tricks */ n // template <typename T, size_t N> n // char (&ArraySizeHelper(T (&array)[N]))[N]; n // #define arraysize(array) (sizeof(ArraySizeHelper(array))) n  n /* n  * forward decleration n  * ---------------------------------------------- n  */ n class ScopeTimer; n  n /* n  *  functional utils n  * ---------------------------------------------- n  */ n  n template < typename T > n struct abs_diff : binary_function < T, T, T > n { n     typedef T value_type; n  n     inline value_type operator( ) ( const value_type & x, const value_type & y ) const n     { n         return abs( x - y ); n     } n }; n  n // template < class InputIterator, class T > n // class isin : public binary_function < InputIterator, InputIterator, bool > n // { n //     public: n //         typedef T value_type; n // n //         isin ( const InputIterator & first, const InputIterator & last ): n //             m_first ( first ), m_last ( last ) { } n // n //         bool operator ( ) ( const value_type & val ) const n //         { n //             return find ( m_first, m_last, val ) != m_last; n //         } n //     private: n //         const InputIterator m_first, m_last; n // } n  n template < typename value_type, typename cont_type > n class isin : public unary_function < value_type, bool > n { n     public: n         isin( const cont_type & vals ): m_vals ( vals ) { }; n  n         bool operator ( ) ( const value_type & x ) const n         { n             return find ( m_vals.begin( ), m_vals.end( ), x ) != m_vals.end( ); n         } n  n     private: n         const cont_type m_vals; n }; n  n /* n  * max_element, min_element, count_if ... on the 2nd element n  * eg: max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) n  */ n template < class T1, class T2, class BinaryOperation > n class Apply2nd : binary_function < typename std::pair < T1, T2 >, n                                         typename std::pair < T1, T2 >, n                                         typename BinaryOperation::result_type > n { n     public: n         typedef T1 first_type; n         typedef T2 second_type; n  n         typedef typename BinaryOperation::result_type result_type; n         typedef typename std::pair < first_type, second_type > value_type; n  n         inline result_type operator( ) ( const value_type & x, const value_type & y ) const n         { n             return binary_op ( x.second , y.second ); n         } n     private: n         BinaryOperation binary_op; n }; n  n /* n  * algo utils n  * ---------------------------------------------- n  */ n  n  n /** n  * count the number of inversions in a permutation; i.e. how many n  * times two adjacent elements need to be swaped to sort the list; n  * 3 5 2 4 1  -->  7 n  */ n template < class InputIterator > n typename iterator_traits<InputIterator>::difference_type n count_inv ( InputIterator first, InputIterator last ) n { n     typedef typename iterator_traits<InputIterator>::difference_type difference_type; n     typedef typename iterator_traits<InputIterator>::value_type value_type; n  n     list < value_type > l; /* list of sorted values */ n     difference_type cnt = 0; n  n     for ( difference_type n = 0; first != last; ++first, ++n ) n     { n         /* count how many elements larger than *first appear before *first */ n         typename list < value_type >::iterator iter = l.begin( ); n         cnt += n; n  n         for ( ; iter != l.end( ) && * iter <= * first; ++ iter, -- cnt ) n             ; n         l.insert( iter, * first ); n     } n  n     return cnt; n } n  n template < class ForwardIterator, class T > n inline void fill_inc_seq ( ForwardIterator first, ForwardIterator last, T val ) n { n     for ( ; first != last; ++first, ++val ) n         * first = val; n } n  n template <class ForwardIterator, class InputIterator > n ForwardIterator remove ( ForwardIterator first, ForwardIterator last, InputIterator begin, InputIterator end ) n { n     ForwardIterator result = first; n  n     for ( ; first != last; ++ first ) n         if ( find ( begin, end, *first ) == end ) n         { n             *result = *first; n             ++result; n         } n  n     return result; n } n  n /* stackoverflow.com/questions/1577475 */ n template < class RAIter, class Compare > n class ArgSortComp n { n     public: n         ArgSortComp ( const RAIter & first, Compare comp ):  m_first ( first ), m_comp( comp ) { } n         inline bool operator() ( const size_t & i, const size_t & j ) const n         { n             return m_comp (  m_first[ i ] ,  m_first[ j ] ); n         } n     private: n         const RAIter & m_first; n         const Compare m_comp; n }; n  n /*! n  * usage: n  *      vector < size_t > idx; n  *      argsort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); n  */ n template < class RAIter, class Compare=less< typename RAIter::value_type > > n void argsort( const RAIter & first, n               const RAIter & last, n               vector < size_t > & idx, n               Compare comp = Compare()) // less< typename RAIter::value_type >() ) n { n     const size_t n = last - first; n     idx.resize ( n ); n  n     for ( size_t j = 0; j < n; ++ j ) n         idx[ j ] = j ; n  n     stable_sort( idx.begin(), idx.end(), ArgSortComp< RAIter, Compare >( first, comp )); n } n  n template < class RAIter, class Compare > n class IterSortComp n { n     public: n         IterSortComp ( Compare comp ): m_comp ( comp ) { } n         inline bool operator( ) ( const RAIter & i, const RAIter & j ) const n         { n             return m_comp ( * i, * j ); n         } n     private: n         const Compare m_comp; n }; n  n /*! n  * usage: n  *      vector < list < Type >::const_iterator > idx; n  *      itersort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); n  */ n template <class INIter, class RAIter, class Compare> n void itersort ( INIter first, INIter last, vector < RAIter > & idx, Compare comp ) n { n     /* alternatively: stackoverflow.com/questions/4307271 */ n     idx.resize ( distance ( first, last ) ); n     for ( typename vector < RAIter >::iterator j = idx.begin( ); first != last; ++ j, ++ first ) n         * j = first; n  n     sort ( idx.begin( ), idx.end( ), IterSortComp< RAIter, Compare > (comp ) ); n } n  n  n /* n  *  string utils n  * ---------------------------------------------- n  */ n  n inline void erase ( string & str, const char & ch ) n { n     binder2nd < equal_to < char > > isch ( equal_to< char >(), ch ); n     string::iterator iter = remove_if ( str.begin(), str.end(), isch ); n     str.erase ( iter, str.end() ); n } n  n inline void erase ( string & str, const string & chrs ) n { n     isin < char, string > isin_chrs ( chrs ); n     string::iterator iter = remove_if ( str.begin(), str.end(), isin_chrs ); n     str.erase ( iter, str.end() ); n } n  n template < typename value_type> n inline string val2str ( const value_type & x ) n { n     ostringstream sout ( ios_base::out ); n     sout << x; n     return sout.str(); n } n  n template < typename value_type> n inline value_type str2val ( const string & str ) n { n     istringstream iss ( str, ios_base::in ); n     value_type val; n  n     iss >> val; n     return val; n } n  n vector< string > tokenize( const string & str, const char & sep ) n { n     /*! n      * outputs empty tokens and assumes str does not start with sep n      * corner cases: n      *      empty string, one char string, n      *      string starting/ending with sep, all sep, end with two sep n      */ n  n     vector < string > res; n     string::const_iterator follow = str.begin(), n                                 lead   = str.begin(); n  n     while ( true ) n     { n         while ( lead != str.end() && * lead != sep ) n             ++ lead; n  n         res.push_back ( string( follow, lead ) ); n  n         if ( lead != str.end () ) n             follow = 1 + lead ++ ; n         else n             break; n     } n  n     return res; n } n  n /*! n  * chunk a string into strings of size [ at most ] k n  */ n void chunk ( const string::const_iterator first, n              const string::const_iterator last, n              const size_t k, n              const bool right_to_left, n              list < string > & str_list ) n { n     str_list.clear( ); n  n     if ( right_to_left ) n         /* chunk from the end of the string */ n         for ( string::const_iterator i, j = last; j != first; j = i ) n         { n             i = first + k < j ? j - k : first; n             str_list.push_back ( string ( i, j ) ); n         } n     else n         /* chunk from the begining of the string */ n         for ( string::const_iterator i = first, j; i != last; i = j ) n         { n             j = i + k < last ? i + k : last; n             str_list.push_back ( string ( i, j ) ); n         } n } n  n /*! n  * next lexicographically smallest string n  * within char set a..z n  */ n string & operator++( string & s ) n { n     /* find the first char from right less than  z  */ n     string::reverse_iterator j = find_if( s.rbegin( ), s.rend( ), n             bind2nd( less < char > ( ),  z  )); n  n     if ( j != s.rend( )) n     { n         ++ *j; n         fill( s.rbegin( ), j,  a  ); n     } n     else n         s.assign( s.length( ) + 1,  a  ); n  n     return s; n } n  n /*! if a starts with b */ n inline bool starts_with( const string & a, const string & b ) n { n     return !( a.size() < b.size() || n         mismatch( begin(b), end(b), begin(a) ).first != end(b) ); n } n  n /*! prefix tree */ n auto get_prefix_tree( const vector< string > & word ) n     -> vector< map< char, size_t > > n { n     typedef map< char, size_t > node; n     /* empty string as the first element */ n     vector< node > tree(1); n     for( const auto & w: word ) n     { n         /* start with empty string as the root node */ n         size_t root = 0; n         for( const auto & a : w ) n         { n             auto & ref = tree[ root ][ a ]; n             if ( ref == 0 ) /* new prefix, add to the end of tree */ n             { n                 ref = tree.size(); n                 tree.push_back( node() ); n             } n             root = ref; n         } n     } n     return tree; n } n  n  n /*! n  * getline ( cin, str ) n  *      requires ctrl-D n  *      cin >> str; does not pass after space char n  */ n  n  n /* n  *  number utils n  * ---------------------------------------------- n  */ n  n  n class BigInteger n { n #if ULONG_MAX <= 1 << 32 n     typedef long long unsigned val_type; n #else n     typedef long unsigned val_type; n #endif n     const static int WSIZE = 32; n     const static val_type BASE = 1LL << WSIZE; n     public: n     private: n         list < val_type > val;  /* val[ 0 ] is most significant */ n         bool pos;                    /* true if sign is positive     */ n }; n  n /** n  * greatest common divisor - Euclid s alg. n  */ n template < typename value_type > inline n value_type gcd ( value_type a, value_type b ) n { n     return ! b ? a : gcd( b, a % b ); n } n  n /** n  * prime factorization n  */ n template < class T > n void prime_factors( T n, map < T, size_t > & fac ) n { n     for ( T k = 2; n > 1; ++ k ) n         if ( ! ( n % k ) ) n         { n             size_t & ref = fac[ k ]; n             while ( ! ( n % k ) ) n             { n                 ++ ref; n                 n /= k; n             } n         } n } n  n /* abs diff - safe for unsigned types */ n template < class T > n inline T absdiff( T a, T b ) n { n     return a < b ? b - a : a - b; n } n  n namespace n { n     template < class T > n     pair < T, T > __extgcd ( const T & x0, const T & y0, n                                   const T & x1, const T & y1, n                                   const T & r0, const T & r1 ) n     { n         const T q  = r0 / r1; n         const T r2 = r0 % r1; n  n         if ( ! ( r1 % r2 ) ) n             return make_pair < T, T > ( x0 - q * x1, y0 - q * y1 ); n  n         const T x2 = x0 - q * x1; n         const T y2 = y0 - q * y1; n  n         return __extgcd ( x1, y1, x2, y2, r1, r2 ); n     } n } n  n /** n  * extended euclidean algorithm: a x  +  b y = gcd( a, b) n  * en.wikipedia.org/wiki/Extended_Euclidean_algorithm n  */ n template < class value_type > inline n pair < value_type, value_type > extgcd ( value_type a, value_type b ) n { n     return a % b n         ? __extgcd < value_type > ( 1, 0, 0, 1, a, b ) n         : make_pair < value_type, value_type > ( 0, 1 ); n } n  n  n /** n  * modular multiplicative inverse n  * en.wikipedia.org/wiki/Modular_multiplicative_inverse n  */ n template < class value_type > inline n value_type modinv ( value_type a, value_type m ) n { n     const pair < value_type, value_type > coef ( extgcd( a, m ) ); n  n     /* a needs to be coprime to the modulus, or the inverse won t exist */ n     if ( a * coef.first + m * coef.second != 1 ) n         throw runtime_error ( val2str( a ) +   is not coprime to   + val2str( m )); n  n     /* return a pos num between 1 & m-1 */ n     return ( m + coef.first % m ) % m; n } n  n inline bool isnan ( const double & a ) n { n     return ! ( a == a ); n } n  n template < typename value_type > n inline value_type mini ( int n, ... ) n { n     va_list vl; n     va_start (vl, n); n     value_type res = va_arg ( vl, value_type ); n  n     for ( int i = 1; i < n; ++i ) { n         const value_type val = va_arg ( vl, value_type ); n         res = min ( res, val ); n     } n  n     va_end( vl ); n     return res; n } n  n template < typename value_type > n inline value_type maxi ( int n, ... ) n { n     va_list vl; n     va_start (vl, n); n     value_type res = va_arg ( vl, value_type ); n  n     for ( int i = 1; i < n; ++i ) { n         const value_type val = va_arg ( vl, value_type ); n         res = max ( res, val ); n     } n  n     va_end( vl ); n     return res; n } n  n // XXX look this up how is this implemented n template < class T > n inline int sign ( const T & x ) n { n     if ( x == T() ) n         return 0; n     else if ( x < T() ) n         return -1; n     else n         return 1; n } n  n /* n  * change moduluos from n to m n  */ n string chmod ( string num, const unsigned n, const unsigned m ) n { n     const char * digit =  0123456789abcdefghijklmnopqrstuvwxyz ; n     transform ( num.begin(), num.end(), num.begin(), ::tolower ); n  n     isin < char, string > is_alpha_num ( digit ); n     assert ( find_if ( num.begin( ), num.end( ), not1 ( is_alpha_num ) ) == num.end( )); n  n     unsigned long long int val ( 0 ); n  n     if ( n == 10U ) n     { n         istringstream iss ( num, ios_base::in ); n         iss >> val; n     } n     else n         for ( string::const_iterator iter = num.begin( ); iter != num.end( ); ++ iter ) n             val = val * n + (  a  <= *iter ? *iter -  a  + 10U : *iter -  0 ); n  n     if ( m == 10U ) n     { n         ostringstream sout ( ios_base::out ); n         sout << val; n         return sout.str ( ); n     } n     else n     { n         string res; n         for ( ; val ; val /= m ) n             res.push_back( digit [ val % m ] ); n  n         return res.length( ) ? string( res.rbegin( ), res.rend( )) :  0 ; n     } n } n  n template < class value_type >  /* a^n mod m */ n value_type powmod ( value_type a, const value_type & n, const value_type & m ) n { n     if ( a == 1 || ! n ) n         return m != 1 ? 1 : 0; n  n     value_type res = 1; n     for ( value_type k = 1; k <= n; a = a * a % m, k = k << 1 ) n         if ( k & n ) n             res =  ( res * a ) % m; n  n     return res; n } n  n template < class T > n inline T atan(const T & x, const T & y) { return atan( y / x ) + ( x < 0 ) * M_PI; } n  n /* n  * Fermat pseudoprime test n  * www.math.umbc.edu/~campbell/Computers/Python/numbthy.py n  * NOTE: since return type is bool, and powmod may break for ints, n  *       the argument is always casted to long long n  */ n inline bool is_pseudo_prime ( const long long & a ) n { n     /* all the primes less than 1000 ( 168 primes )*/ n     const long long p [ ] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73, n                               79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157, n                               163,167,173,179,181,191,193,197,199,211,223,227,229,233,239, n                               241,251,257,263,269,271,277,281,283,293,307,311,313,317,331, n                               337,347,349,353,359,367,373,379,383,389,397,401,409,419,421, n                               431,433,439,443,449,457,461,463,467,479,487,491,499,503,509, n                               521,523,541,547,557,563,569,571,577,587,593,599,601,607,613, n                               617,619,631,641,643,647,653,659,661,673,677,683,691,701,709, n                               719,727,733,739,743,751,757,761,769,773,787,797,809,811,821, n                               823,827,829,839,853,857,859,863,877,881,883,887,907,911,919, n                               929,937,941,947,953,967,971,977,983,991,997 }; n  n     const size_t n = sizeof( p ) / sizeof ( p[ 0 ] ); n  n     if ( a < p[ n - 1 ] + 1) n         return binary_search ( p, p + n , a ); n  n     if ( find_if ( p, p + n, not1 ( bind1st ( modulus< long long >( ), a ))) != p + n ) n         return false; n  n     const size_t k = a <    9006401LL ? 3 : n                      a <   10403641LL ? 4 : n                      a <   42702661LL ? 5 : n                      a < 1112103541LL ? 6 : 7; n  n     for ( size_t j = 0; j < k; ++ j ) n         if ( powmod ( p[ j ], a - 1, a ) != 1 ) n             return false; n  n     return true; n } n  n  n /* n  * returns a sorted vector of all primes less than or equal to n n  * maximum adj diff of all primes less than 1e5 is 72 ( 114 for 1e6 ) n  */ n template < typename value_type > n vector < value_type > get_primes ( const value_type n ) n { n #ifdef BUG n     ScopeTimer scope_timer (  vector < value_type > get_primes ( const value_type n )  ); n #endif n  n     // typedef typename vector < value_type >::iterator iterator; n     vector < value_type > primes; n  n     for ( value_type k = 2 ; k <= n; ++ k ) n         if ( is_pseudo_prime ( k ) ) n         { n             // FIXME this is very stupid n             // const value_type sqrt_k = 1 + static_cast < value_type > ( sqrt ( k + 1 ) ); n             // iterator iend = upper_bound ( primes.begin( ), primes.end( ), sqrt_k ); n  n             // if ( find_if ( primes.begin( ), iend, not1 ( bind1st ( modulus< value_type >( ), k ) ) ) != iend ) n             //     continue; n  n             primes.push_back ( k ); n         } n  n     return primes; n } n  n template < class T > n inline vector < pair < T, size_t > > get_prime_fact( T a ) n { n     vector< pair < T, size_t > >fac; n  n     for ( T k = 2; a > 1; ++ k ) n         if ( ! ( a % k ) )  // no need to check if k is prime n         { n             size_t m = 0; n             for ( ; ! ( a % k ) ; ++m, a/= k ) n                 ; n  n             fac.push_back ( pair < T, size_t > ( k, m ) ); n         } n     return fac; n } n  n template < class T >  /* prime factorization */ n vector < pair < T, size_t > > get_prime_fac( T a ) n { n     vector < pair < T, size_t > > fac; n  n     size_t k = 0; n     while (!(a % 2)) { a /= 2; ++ k; } n     if ( k != 0 ) fac.push_back( make_pair(2, k)); n  n     k = 0; n     while (!(a % 3)) { a /= 3; ++ k; } n     if ( k != 0 ) fac.push_back( make_pair(3, k)); n  n     for ( T j = 5; j * j < a + 1 && a > 1 && j < a + 1; j += 4 ) n     { n         size_t k = 0; n         while (!(a % j)) { a /= j; ++ k; } n         if ( k != 0 ) fac.push_back( make_pair(j, k)); n  n         j += 2; k = 0; n         while (!(a % j)) { a /= j; ++ k; } n         if ( k != 0 ) fac.push_back( make_pair(j, k)); n     } n  n     if ( a > 1 ) fac.push_back( make_pair(a, 1)); n     return fac; n } n  n template < class T > /* generates all divisors from vector of prime factorization */ n void gen_div( T a, vector < T > & divs, n         typename vector < pair < T, size_t > >::const_iterator iter, n         typename vector < pair < T, size_t > >::const_iterator last ) n { n     if ( iter != last ) n     { n         auto next = iter + 1; n         const auto k = iter->second + 1; n         const auto prime = iter->first; n         for ( size_t j = 0; j < k; ++ j ) n         { n             gen_div(a, divs, next, last); n             a *= prime; n         } n     } n     else n         divs.push_back( a ); n } n  n  n  n template < class T > n T n_choose_k ( T n, T k ) n { n     if ( k > n ) n         return 0; n  n     const T lb = min ( k, n - k ) + 1; n     const T ub = n - lb + 1; n  n     T res = 1, j = 2; n     while ( n > ub && j < lb) n     { n         res *= n--; n         while ( j < lb and ! (res % j) ) n             res /= j++; n     } n  n     while ( n > ub ) n         res *= n--; n  n     return res; n } n  n  n /** n  * median calculator, using two heaps n  */ n template < class InputIter > n inline pair < typename InputIter::value_type, typename InputIter::value_type > n median ( InputIter first, InputIter last ) n { n     typedef typename InputIter::value_type      value_type; n     typedef pair< value_type, value_type > result_type; n  n     /* n      * max_heap: n      *      - the lower half of the elements n      *      - the biggest of such elements is on the top n      */ n     vector < value_type > max_heap, min_heap; n  n     /* n      * comp argument to heap algorithm should provide n      *  strict weak ordering ; in particular n      *      not2 ( less < value_type > ) n      * does not have such a strict weak ordering; n      */ n     less    < value_type > max_heap_comp; n     greater < value_type > min_heap_comp; n  n     if ( first == last )  /* corner case: empty vector */ n         throw runtime_error (  median of an empty vector is undefined!  ); n  n     InputIter iter = first; n     max_heap.push_back ( * iter ); n  n     for ( ++iter ; iter != last; ++ iter ) n         if ( * iter < max_heap.front() ) n         { n             max_heap.push_back ( * iter ); n             push_heap ( max_heap.begin(), max_heap.end(), max_heap_comp ); n  n             if ( min_heap.size() + 1 < max_heap.size() ) n             { n                 /* max_heap has got too large */ n                 min_heap.push_back( max_heap.front() ); n                 push_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); n  n                 pop_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); n                 max_heap.pop_back(); n             } n         } n         else n         { n             min_heap.push_back ( * iter ); n             push_heap ( min_heap.begin(), min_heap.end(), min_heap_comp ); n  n             if ( max_heap.size() + 1 < min_heap.size() ) n             { n                 /* min_heap has got too large */ n                 max_heap.push_back( min_heap.front() ); n                 push_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); n  n                 pop_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); n                 min_heap.pop_back(); n             } n         } n  n     DEBUG( max_heap ); n     DEBUG( min_heap ); n  n     return min_heap.empty( )  /* corner case: ++first = last */ n         ? result_type ( *first, *first ) n         : result_type ( max_heap.size() < min_heap.size() ? min_heap.front() : max_heap.front(), n                         min_heap.size() < max_heap.size() ? max_heap.front() : min_heap.front() ); n } n  n /* n  * geometry util n  * ---------------------------------------------- n  */ n  n struct xyPoint n { n     double x, y; n     xyPoint( const double & a = .0, const double & b = .0 ): x ( a ), y( b ) { }; n }; n  n  n struct xyCircle n { n     xyPoint center; n     double radius; n }; n  n ostream & operator<< ( ostream & out, const xyPoint & p ) n { n     out <<  (  << p.x <<  ,   << p.y <<  ) ; n     return out; n } n  n istream & operator>> ( istream & ist, xyPoint & p ) n { n     ist >> p.x >> p.y; n     return ist; n } n  n ostream & operator<< ( ostream & out, const xyCircle & o ) n { n     out <<  {(  << o.center.x <<  ,   << o.center.y <<  )   << o.radius <<  } ; n     return out; n } n  n istream & operator>> ( istream & ist, xyCircle & o ) n { n     ist >> o.center.x >> o.center.y >> o.radius; n     return ist; n } n  n inline double cartesian_dist ( const xyPoint & a, const xyPoint & b ) n { n     const double d = a.x - b.x; n     const double e = a.y - b.y; n     return sqrt ( d * d + e * e ); n } n  n class xyLine n { n     public: n         xyLine ( const xyPoint & , const xyPoint & ); n         xyLine ( const double slope, const double intercept ); n  n         /* n          *  signed  orthogonal distance; the sign is useful n          * to compare which side of the line the point is n          */ n         inline double orth_dist ( const xyPoint & ) const; n     private: n         double m_slope; n         double m_intercept; n         double m_normfac;   /* normalization factor for orth_dist calc */ n  n         bool   m_vertical;  /* if the line is verticcal                */ n         double m_xcross;    /* x axis cross point for vertical line    */ n }; n  n xyLine::xyLine ( const xyPoint & a, const xyPoint & b ) n { n     if ( a.x == b.x )  /* vertical line */ n     { n         m_vertical  = true; n         m_xcross    = a.x; n  n         m_intercept = DOUBLE_NAN; n         m_slope     = DOUBLE_INF; n         m_normfac   = DOUBLE_NAN; n     } n     else n     { n         m_vertical  = false; n         m_xcross    = DOUBLE_NAN; n  n         m_slope     = ( b.y - a.y ) / ( b.x - a.x ); n         m_intercept = a.y - m_slope * a.x; n         m_normfac   = sqrt ( m_slope * m_slope + 1.0 ); n     } n } n  n xyLine::xyLine ( const double slope, const double intercept ): n     m_slope ( slope ),  m_intercept ( intercept ) n { n      m_vertical = false; n      m_xcross   = DOUBLE_NAN; n      m_normfac  = sqrt ( m_slope * m_slope + 1.0 ); n } n  n double xyLine::orth_dist ( const xyPoint & o ) const /*  signed  orthogonal distance */ n { n     if ( m_vertical ) n         return o.x - m_xcross; n     else n         return ( m_slope * o.x - o.y + m_intercept ) / m_normfac; n } n  n inline double triangle_area ( const xyPoint & a, const xyPoint & b, const xyPoint & c ) n { n     const xyLine l ( a, b ); n     const double h = abs ( l.orth_dist ( c ) ); n     const double e = cartesian_dist ( a, b ); n     return h * e; n } n  n /* n  *  operator<< overrides n  * ---------------------------------------------- n  */ n  n namespace n { n     /* helper function to output containers */ n     template < typename T > n     ostream & __output ( ostream & out, const T & a ) n     { n         typedef typename T::const_iterator const_iterator; n         out <<  {  ; n  n         // does not work for  pair  value type n         // copy ( a.begin( ), a.end( ), ostream_iterator < typename T::value_type > ( cout,  ,   )); n  n         for ( const_iterator iter = a.begin(); iter != a.end(); ++ iter ) n              out << ( iter != a.begin( ) ?  ,   :    ) << *iter ; n  n         return out <<   } ; n     } n } n  n template < typename key_type, typename value_type > n ostream & operator<< ( ostream & out, const pair < key_type, value_type > & p) n { n     out <<  (  << p.first <<  ,   << p.second <<  ) ; n     return out; n } n  n template < typename T0, typename T1, typename T2 > n ostream & operator<< ( ostream & out, const tuple < T0, T1, T2 > & t ) n { n     out <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ,   << get<2>(t) <<  ) ; n     return out; n } n  n template < typename T0, typename T1 > n ostream & operator<< ( ostream & out, const tuple < T0, T1 > & t ) n { n     out <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ) ; n     return out; n } n  n template < typename key_type, typename value_type, typename comp > n ostream & operator<< ( ostream & out, const map < key_type, value_type, comp > & m ) n { n     return __output ( out, m ); n } n  n template < typename value_type, typename comp > n ostream & operator<< ( ostream & out, const set < value_type, comp > & s ) n { n     return __output ( out, s ); n } n  n template < typename value_type > n ostream & operator<< ( ostream & out, const vector < value_type > & a ) n { n     return __output ( out, a ); n } n  n template < typename value_type > n ostream & operator<< ( ostream & out, const list < value_type > & a ) n { n     return __output ( out, a ); n } n  n template < typename value_type > n ostream & operator<< ( ostream & out, const vector < vector < value_type > > & a ) n { n     typedef typename vector < vector < value_type > >::const_iterator const_iterator; n  n     for ( const_iterator iter = a.begin( ); iter != a.end( ); ++ iter ) n         out <<   n  << *iter ; n  n     return out; n } n  n /* n  *  operator>> overrides n  * ---------------------------------------------- n  */ n  n template < typename key_type, typename value_type > n istream & operator>> ( istream & in, pair < key_type, value_type > & p) n { n     in >> p.first >> p.second; n     return in; n } n  n template < typename T0, typename T1, typename T2 > n istream & operator>> ( istream & fin, tuple < T0, T1, T2 > & t ) n { n     fin >> get<0>(t) >> get<1>(t) >> get<2>(t); n     return fin; n } n  n template < typename value_type > n istream & operator>> ( istream & in, vector < value_type > & a ) n { n     typedef typename vector < value_type >::iterator iterator; n  n     if ( ! a.size( ) ) n     { n         size_t n; n         in >> n; n         a.resize( n ); n     } n  n     for ( iterator iter = a.begin(); iter != a.end(); ++ iter ) n         in >> * iter; n  n     return in; n } n  n /* n  *  readin quick utilities n  * ---------------------------------------------- n  */ n  n // template < typename value_type > n // inline void readin ( vector < value_type > & a, size_t n = 0, istream & in = cin ) n // { n //     // if ( ! n ) cin >> n; n //     if ( ! n ) in >> n ; n //     a.resize ( n ); n //     // cin >> a; n //     in >> a; n // } n  n // XXX consider removing n // template < typename key_type, typename value_type > n // inline void readin (vector < pair < key_type , value_type > > & a, size_t n = 0 ) n // { n //     if ( !n ) cin >> n; n //     a.resize( n ); n //     cin >> a; n // } n  n /* n  *  pair utility n  * ---------------------------------------------- n  */ n  n /* n  * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ); n  * stackoverflow.com/questions/18640152 n  */ n template < typename T1, typename T2 > n inline pair < T1, T2 > operator+ ( const pair < T1, T2 > & a, const pair < T1, T2 > & b ) n { n     return make_pair < T1, T2 > ( a.first + b.first, a.second + b.second ); n } n  n template < typename T1, typename T2 > n inline pair < T1, T2 > operator- ( const pair < T1, T2 > & a, const pair < T1, T2 > & b ) n { n     return make_pair < T1, T2 > ( a.first - b.first, a.second - b.second ); n } n  n // template < class T1, class T2, class BinaryOperation > n // class Apply2nd : binary_function < typename pair < T1, T2 >, n //                                         typename pair < T1, T2 >, n //                                         typename BinaryOperation::result_type > n  n namespace n { n     /*! n      * helper template to do the work n      */ n     template < size_t J, class T1, class T2 > n     struct Get; n  n     template < class T1, class T2 > n     struct Get < 0, T1, T2 > n     { n         typedef typename pair < T1, T2 >::first_type result_type; n  n         static       result_type & elm (       pair < T1, T2 > & pr ) { return pr.first; } n         static const result_type & elm ( const pair < T1, T2 > & pr ) { return pr.first; } n     }; n  n     template < class T1, class T2 > n     struct Get < 1, T1, T2 > n     { n         typedef typename pair < T1, T2 >::second_type result_type; n  n         static       result_type & elm (       pair < T1, T2 > & pr ) { return pr.second; } n         static const result_type & elm ( const pair < T1, T2 > & pr ) { return pr.second; } n     }; n } n  n template < size_t J, class T1, class T2 > n typename Get< J, T1, T2 >::result_type & get ( pair< T1, T2 > & pr ) n { n     return Get < J, T1, T2 >::elm( pr ); n } n  n template < size_t J, class T1, class T2 > n const typename Get< J, T1, T2 >::result_type & get ( const pair< T1, T2 > & pr ) n { n     return Get < J, T1, T2 >::elm( pr ); n } n  n /* n  * graph utils n  * ---------------------------------------------- n  */ n  n /* maximum in sub-tree distance */ n void get_indist( const size_t root, n         const vector < vector < size_t > > & tree, n         vector < tuple< size_t, size_t > > & indist ) n { n     const auto NIL = numeric_limits< size_t >::max(); n     size_t a = 0, b = NIL; n  n     for ( const auto u: tree[ root ] ) n     { n         get_indist( u, tree, indist ); n         if ( a < 1 + get< 0 >( indist[ u ] ) ) n         { n             b = a; n             a = 1 + get< 0 >( indist[ u ] ); n         } n         else n             b = b != NIL n                 ? max( b, 1 + get< 0 >( indist[ u ] )) n                 : 1 + get< 0 >( indist[ u ] ); n     } n  n     indist[ root ] = make_tuple(a, b); n } n  n /* maximum out of subtree distance */ n void get_outdist( const size_t root, n         const vector< vector< size_t > > & tree, n         const vector< tuple< size_t, size_t > > & indist, n         vector < size_t > & outdist ) n { n     const auto NIL = numeric_limits< size_t >::max(); n  n     for( const auto u: tree[ root ] ) n     { n         if ( 1 + get< 0 >( indist[ u ] ) == get< 0 >( indist[ root ] ) ) n             outdist[ u ] = get< 1 >( indist[ root ] ) != NIL n                 ? 1 + max( get< 1 >( indist[ root ] ), outdist[ root ] ) n                 : 1 + outdist[ root ]; n         else n             outdist[ u ] = 1 + n                 max( get< 0 >( indist[ root ] ), outdist[ root ] ); n  n         get_outdist( u, tree, indist, outdist ); n     } n } n  n  n /* n  * Dijkstra :: single-source shortest path problem for n  * a graph with non-negative edge path costs, producing n  * a shortest path tree n  * en.wikipedia.org/wiki/Dijkstra s_algorithm n  */ n template < typename DistType > n void Dijekstra ( const size_t & source, n                  const vector < list < size_t > > & adj,          // adjacency list n                  const vector < vector < DistType > > & edge_len, // pair-wise distance for adjacent nodes n                  vector < DistType > & dist,  // distance from the source n                  vector < size_t > prev )     // previous node in the shortest path tree n { n     // TODO n } n  n // TODO http://en.wikipedia.org/wiki/Shortest_path_problem n // TODO Graph class, Weighted graph, ... n  n /* n  *  maximum cardinality matching in a bipartite graph n  *        G = G1 ∪ G2 ∪ {NIL} n  *  where G1 and G2 are partition of graph and NIL is a special null vertex n  *  https://en.wikipedia.org/wiki/Hopcroft-Karp_algorithm n  */ n class HopcroftKarp n { n     public: n         HopcroftKarp ( const vector < list < size_t > > & adj, n                        const vector < bool > & tag ); n  n         size_t get_npair ( ) { return npair; }; n         map < size_t, size_t > get_map ( ); n  n     private: n         bool mf_breadth_first_search ( );  // breadth first search from unpaired nodes in G1 n         bool mf_depth_first_search ( const size_t v ); // dfs w/ toggeling augmenting paths n  n         const vector < list < size_t > > & m_adj;  // adjacency list for each node n         const vector < bool > & m_tag;  // binary tag distinguishing partitions n  n         size_t npair; n         const size_t NIL;  // special null vertex n         const size_t INF;  // practically infinity distance n  n         vector < size_t > m_g1;    // set of nodes with tag = true n         vector < size_t > m_dist;  // dist from unpaired vertices in G1 n         vector < size_t > m_pair; n }; n  n  n map < size_t, size_t > HopcroftKarp::get_map ( ) n { n     map < size_t, size_t > m; n  n     for ( size_t j = 0; j < m_pair.size( ); ++ j ) n         if ( m_pair[ j ] != NIL  && m_tag[ j ]) n             m[ j ] = m_pair[ j ]; n  n     return m; n } n  n  n HopcroftKarp::HopcroftKarp ( const vector < list < size_t > > & adj, n                              const vector < bool > & tag ): n     m_adj ( adj ), n     m_tag ( tag ), n     npair ( 0 ), n     NIL ( adj.size( )), n     INF ( adj.size( ) + 1 ), n     m_dist ( vector < size_t > ( adj.size( ) + 1, INF)), n     m_pair ( vector < size_t > ( adj.size( ), NIL ))  // initially everything is paired with nil n { n     assert ( m_adj.size() == m_tag.size() ); n  n     for ( size_t j = 0; j < tag.size( ); ++ j ) n         if ( tag[ j ] ) n             m_g1.push_back ( j ); n  n     while ( mf_breadth_first_search ( ) ) n         for ( vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++ v ) n             if ( m_pair[ *v ] == NIL && mf_depth_first_search ( *v ) ) n                 ++ npair; n } n  n bool HopcroftKarp::mf_breadth_first_search( ) n { n     /* only nodes from g1 are queued */ n     queue < size_t > bfs_queue; n  n     /* initialize queue with all unpaired nodes from g1 */ n     for ( vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++v ) n         if ( m_pair[ *v ] == NIL ) n         { n             m_dist[ *v ] = 0; n             bfs_queue.push ( *v ); n         } n         else n             m_dist[ *v ] = INF; n  n     m_dist[ NIL ] = INF; n  n     /* find all the shortest augmenting paths to node nil */ n     while ( ! bfs_queue.empty() ) n     { n         const size_t v = bfs_queue.front( ); n         bfs_queue.pop ( ); n  n         if ( m_dist[ v ] < m_dist[ NIL ] ) n             for ( list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) n                 if ( m_dist[ m_pair[ * u ] ] == INF ) n                 { n                     m_dist[ m_pair[ * u ] ] = m_dist[ v ] + 1; n                     bfs_queue.push ( m_pair[ * u ] ); n                 } n     } n  n     return m_dist[ NIL ] != INF; n } n  n  n bool HopcroftKarp::mf_depth_first_search( const size_t v ) n { n     if ( v == NIL ) n         return true; n     else n     { n         for ( list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) n             if ( m_dist[ m_pair[ *u ] ] == m_dist[ v ] + 1  && mf_depth_first_search( m_pair[ *u ] )) n             { n                 /* n                  * there is an augmenting path to nil from m_pair[ *u ] n                  * and hence there is an augmenting path from v to u and n                  * u to to nil; therefore v and u can be paired together n                  */ n                 m_pair [ *u ] = v; n                 m_pair [  v ] = *u; n                 return true; n             } n         m_dist[ v ] = INF; n         return false; n     } n } n  n  n /** n  * lazy all pairs shortest path in a tree with only one BFS n  * test case:  book of evil  n  *      codeforces.com/problemset/problem/337/D n  */ n class All_Pairs_Tree_Shortest_Path n { n     public: n         All_Pairs_Tree_Shortest_Path( const vector< list < size_t > > & adj ): n             n( adj.size( ) ), n             depth( vector < size_t > ( n, All_Pairs_Tree_Shortest_Path::INF ) ), n             parent( vector < size_t > ( n ) ), n             dist( vector < vector < unsigned short > > ( n ) ) n         { n  n             /* perform bfs from root node  0  and assign depth to each node */ n             /* XXX probably would be worth to set the root as node with highest degree */ n             queue< size_t > bfs_queue; n             bfs_queue.push( 0 ); n             depth[ 0 ]  = 0; n             parent[ 0 ] = 0; n  n             while ( ! bfs_queue.empty( ) ) n             { n                 const size_t u = bfs_queue.front( ); n                 bfs_queue.pop( ); n  n                 for ( list< size_t >::const_iterator j = adj[ u ].begin( ); j != adj[ u ].end( ); ++ j ) n                     if ( depth[ u ] + 1 < depth[ *j ] ) n                     { n                         depth[ *j ]  = depth[ u ] + 1; n                         parent[ *j ] = u; n                         bfs_queue.push( *j ); n                     } n             } n  n             /* adjust pair-wise distance to zero along the diagonal */ n             for ( size_t j = 1; j < n; ++ j ) n                 dist[ j ].resize( j, All_Pairs_Tree_Shortest_Path::INF ); n         } n  n         /* interface object function as to lazily look-up distances */ n         size_t operator( )( const size_t u, const size_t v ) n         { n             if ( u == v ) n                 return 0; n             else if ( u < v) n                 return (*this)( v, u ); n             else if ( dist[ u ][ v ] == All_Pairs_Tree_Shortest_Path::INF ) n             { n                 if ( depth[ u ] < depth[ v ] ) n                     /* u is in a lower level than v */ n                     dist[ u ][ v ] = 1 + (*this)( u, parent[ v ]); n                 else if ( depth[ v ] < depth[ u ] ) n                     /* v is in a lower level than u */ n                     dist[ u ][ v ] = 1 + (*this)( parent[ u ], v ); n                 else n                     /* u and v are at the same depth */ n                     dist[ u ][ v ] = 2 + (*this)( parent[ u ], parent[ v ] ); n            } n             return dist[ u ][ v ]; n         } n  n         /* TODO populate; a method which populates pair-wise distances n          * and returns the matrix */ n  n     private: n             /* n              * constant infinity value for initializing distances n              * even though this is private it will be assigned outside of the class n              */ n             static const unsigned short INF; n  n             const size_t n;                 /* numbert of nodes in the tree */ n             vector < size_t > depth;   /* distance to root node  0  */ n             vector < size_t > parent;  /* parent of each node with root node  0  */ n             vector < vector < unsigned short > > dist; /* pair-wise shortest path distance */ n }; n  n const unsigned short All_Pairs_Tree_Shortest_Path::INF = numeric_limits< unsigned short >::max( ); n  n /* n  *  data-structure utility n  * ---------------------------------------------- n  */ n  n template < class T, class Comp = less< T > > n class Heap  /* less< T > --> max-heap */ n { n     typedef T value_type; n     typedef typename vector < value_type >::size_type size_type; n     public: n         /* n          * stackoverflow.com/questions/10387751 n          * possible work-around: a memebr pointer to m_val n          * TODO static/friend heapify ( val, & heap )  XXX O( n ) ?! n          * TODO implement insert iterator n          */ n         Heap(): m_val( vector < value_type >() ), m_comp( Comp() ) {} n         Heap( const Comp & comp ): m_val( vector< value_type >() ), m_comp( comp ){ } n  n         template < class InputIter > n         Heap ( InputIter first, InputIter last ): n             m_val ( vector < value_type > ( ) ), m_comp( Comp( ) ) n         { n             for ( ; first != last ; ++ first ) n                 m_val.push_back ( * first ); n  n             make_heap( m_val.begin( ), m_val.end( ), m_comp ); n         } n  n         /*! n          * to avoid destroying heap property, front( ) n          * should always return a  const  reference n          */ n         inline const value_type & front( ) const { return m_val.front( ); } n         inline bool empty( ) const { return m_val.empty( ); } n         inline size_type size( ) const { return m_val.size( );  } n  n         inline void push( const value_type & a ) n         { n             m_val.push_back( a ); n             push_heap( m_val.begin( ), m_val.end( ), m_comp ); n         } n         inline void pop( ) n         { n             pop_heap ( m_val.begin( ), m_val.end( ), m_comp ); n             m_val.pop_back( ); n         } n  n         // inline void swap( Heap< T, Comp> & other ) { m_val.swap( other.m_val ) }; n         // void sort( ) { sort_heap ( m_val.begin( ), m_val.end( ), m_comp ); } n         // template < class X, class Y > n         // friend ostream & operator<<( ostream & out, const Heap < X, Y> & heap ); n     private: n         vector < value_type > m_val; n         const Comp m_comp; n }; n  n /* n  * boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm n  */ n #if 1 < 0 n template < class Type > n class shared_ptr n { n     typedef Type value_type; n     public: n         explicit shared_ptr ( value_type * p = NULL ) : ptr ( p ), count ( new size_t ( 1U ) ) { } n         shared_ptr ( const shared_ptr < value_type > & sp ): ptr ( sp.ptr ), count ( sp.count ) { ++ * count; } n         ~ shared_ptr ( ) { release( ); } n  n         bool operator== ( const shared_ptr < value_type > & sp ) { return ptr == sp.ptr; } n         bool operator!= ( const shared_ptr < value_type > & sp ) { return ptr != sp.ptr; } n  n         shared_ptr < value_type > & operator= ( const shared_ptr < value_type > & sp ) n         { n             if ( this != & sp && ptr != sp.ptr ) n             { n                 release( ); n                 ptr   = sp.ptr; n                 count = sp.count; n                 ++ * count; n             } n             return * this; n         } n  n         value_type * operator-> ( ) { return ptr ; } n         value_type & operator* ( ) { return *ptr ; } n         const value_type * operator-> ( ) const { return ptr ; } n         const value_type & operator* ( ) const { return *ptr; } n  n         void swap ( shared_ptr < value_type > & sp ) n         { n             if ( this != &sp && ptr != sp.ptr ) n             { n                 swap ( ptr, sp.ptr ); n                 swap ( count, sp.count ); n             } n         } n  n     private: n         void release ( ) n         { n             /* stackoverflow.com/questions/615355 */ n             -- * count; n             if ( ! * count ) n             { n                 delete count; n                 delete ptr; n                 count = NULL; n                 ptr   = NULL; n             } n         } n         value_type * ptr; n         size_t     * count; n }; n #endif n  n /*! n  * union find data structure with n  *      - lazy unions n  *      - union by rank n  *      - path compression n  */ n class UnionFind n { n     public: n         UnionFind( const size_t n ): n             parent ( vector < size_t > ( n ) ),   /* initialize each node as its own   */ n             rank   ( vector < size_t > ( n, 0 ))  /* parent and set all the ranks to 0 */ n         { n             for ( size_t j = 0; j < n; ++ j ) n                 parent[ j ] = j ; n         } n  n         inline size_t find( const size_t s ) n         { n             /* n              * perform path compresion and add shortcut n              * if parent[ s ] is not a root node n              */ n             const size_t p = parent[ s ]; n             return parent[ p ] == p ? p : parent[ s ] = find( p ) ; n         } n  n         inline void lazy_union ( size_t i, size_t j ) n         { n             /* unions should be done on root nodes */ n             i = find( i ); n             j = find( j ); n  n             if ( i != j ) n             { n                 if ( rank [ i ] < rank[ j ] ) n                     parent[ i ] = j; n                 else n                 { n                     parent[ j ] = i; n                     rank[ i ]  += rank[ i ] == rank[ j ]; n                 } n             } n         } n     private: n         vector < size_t > parent; n         vector < size_t > rank; n }; n  n  n // TODO XXX n // template < class NumType > n // unsigned num_hash_func ( const NumType & a ) n // { n //     // XXX what will happen in case of overflow? n //     return static_cast < unsigned > ( a % 9973 ) % 9973 ; n // } n  n /* n  * XXX: HashMap: map< Key, T > data [ 9973 ] n  * data [ num_hash_func ( key ) ][ key ] n  */ n  n  n /* n  *  testing util n  * ---------------------------------------------- n  */ n  n // TODO add a preprocessor which automatically includes the funciton name, or __line__ n // and disables if not in debug mode n /* prints the life length of the object when it goes out of scope */ n class ScopeTimer n { n     public: n         ScopeTimer ( const string & msg =    ): tic ( clock ( )), m_msg( msg ) { }; n  n         ~ ScopeTimer ( ) n         { n             const clock_t toc = clock(); n             const uint64 dt   = 1000L * ( toc - tic ) / CLOCKS_PER_SEC; n  n             const uint64 mil = dt % 1000L; n             const uint64 sec = ( dt / 1000L ) % 60L; n             const uint64 min = ( dt / 60000L ) % 60L; n             const uint64 hrs = ( dt / 3600000L ); n  n             cout <<   n  << m_msg <<   n telapsed time:  ; n  n             if ( hrs )  cout << hrs <<   hrs,  ; n             if ( min )  cout << min <<   min,  ; n             if ( sec )  cout << sec <<   sec,  ; n             cout << mil <<   mil-sec n ; n         } n  n     private: n         typedef unsigned long long int  uint64; n         const clock_t tic; n         const string m_msg; n }; n  n class RandInt n { n     public: n         RandInt ( int a = 0, int b = 100 ): m ( a ), f ( static_cast < double > ( b - a ) / RAND_MAX ) { } n         inline int operator() ( ) { return m + ceil ( f * rand( ) ); } n     private: n         const int m; n         const double f; n }; n  n  n class RandDouble n { n     public: n         RandDouble ( double a = 0.0, double b = 1.0 ): m ( a ), f ( ( b - a ) / RAND_MAX ) { } n         inline double operator() ( ) { return m + f * rand( ); } n     private: n         const double m, f; n }; n  n class Noisy n { n     public: n         Noisy ( string str ): msg ( str ) n         { n             cout <<   Noisy (   << msg <<   ) t@   << this << endl; n         } n         ~Noisy ( ) n         { n             cout <<  ~Noisy (   << msg <<   ) t@   << this << endl; n         } n         void beep ( ) n         { n             cout <<   beep (   << msg <<   ) t@   << this << endl; n         } n         void beep ( ) const n         { n             cout <<   const beep (   << msg <<   ) t@   << this << endl; n         } n     private: n         const string msg; n }; n  n DECLARE ( Noisy ); n  n  n /* n  * ---------------------------------------------- n  * ---------------------------------------------- n  */ n  n /* n  * -- @@@ ------------------------------------------------- n  */ n  n inline int minchar( const size_t j, const vector< int > & a  ) n { n     const auto n = a.size(); n     int k = 0; n  n     while( ( j + 1 < n && a[ j + 1 ] == k ) || ( j + 2 < n && a[ j + 2 ] == k )) n         ++ k; n  n     return k; n } n  n void probA() n { n     size_t n; n     int k; n     cin >> n >> k; n  n     string str; n     cin >> str; n  n     vector< int > a( n ); n     transform(begin(str), end(str), a.rbegin(), bind(minus<char>(), placeholders::_1,  a )); n  n     // first letter that can increase n     size_t j = 0; // find_if(begin(a), end(a), bind(less<int>(), placeholders::_1, k)) - begin(a); n     bool flag = false; n  n     while( j < n && ! flag ) n     { n         ++ a[ j ]; n         while( ( j + 1 < n && a[ j ] == a[ j + 1 ]) || ( j + 2 < n && a[ j ] == a[ j + 2 ] )) n             ++ a[ j ]; n  n         if( k < 1 + a[ j ] ) n         { n             ++ j; n             continue; n         } n  n         bool fail = false; n         for( size_t i = j; 0 < i && !fail; -- i ) n         { n             a[ i - 1 ] = minchar(i - 1, a); n             fail = k < 1 + a[ i - 1 ]; n         } n  n         flag = ! fail; n     } n  n     if( !flag ) n         cout <<  NO ; n     else n     { n         for( auto iter = a.rbegin(); iter != a.rend(); ++ iter ) n             cout << (char)( a  + *iter); n     } n } n  n  n int main ( const int argc, char * argv [ ]) n { n     probA(); n     // cout << setprecision( 10 ) n  n     return EXIT_SUCCESS; n } n  n /** n  * mislav.uniqpath.com/2011/12/vim-revisited/ n  * set encoding=utf-8 n  * %s/ (. {60,70 } ) / 1 r/gc n  * %s/ / /gc n  * %s/10 ([0-9] {1,2 } )/10^ 1/gc n  */ n


#include <bits/stdc++.h> n using namespace std; n int v[112345]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &v[i]); n   sort(v, v + n); n   int res = 1; n   for (int i = 0; i < n; i++) { n     if (v[i] >= res) { n       v[i] = res; n       res++; n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n long long a[maxn], b[maxn]; n int main() { n   int m, n; n   long long sum1 = 0, sum2 = 0, max1 = 0, max2 = 0, t1, t2; n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= m; i++) { n     scanf( %lld , &a[i]); n     sum1 += a[i]; n     if (a[i] > max1) { n       t1 = i; n       max1 = a[i]; n     } n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &b[i]); n     sum2 += b[i]; n     if (b[i] > max2) { n       t2 = i; n       max2 = b[i]; n     } n   } n   long long ans1 = sum1 * n, ans2 = sum2 * m; n   for (int i = 1; i <= m; i++) { n     if (i != t1 && a[i] < sum2) ans2 = ans2 - sum2 + a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (i != t2 && b[i] < sum1) ans1 = ans1 - sum1 + b[i]; n   } n   printf( %lld n , min(ans1, ans2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string st; n int call() { n   int s = 0, r = -1, neg = 0; n   for (int i = 0; st[i]; i++) { n     if (st[i] ==  ( ) n       s++; n     else n       s--; n     if (s <= 1) r = i; n     if (s < 0) neg = 1; n   } n   int ans = 0; n   if (s == 2 && !neg) { n     s = 0; n     for (int i = 0; st[i]; i++) { n       if (st[i] ==  ( ) n         s++; n       else n         s--; n       if (i > r) { n         if (st[i] ==  ( ) { n           if (s - 2 >= 0) ans++; n         } n       } n     } n   } n   return ans; n } n int main() { n   cin >> n >> st; n   int ans = call(); n   string y; n   for (int i = st.size() - 1; i >= 0; i--) { n     if (st[i] ==  ) ) n       y +=  ( ; n     else n       y +=  ) ; n   } n   st = y; n   ans += call(); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n int n, m, far[MAXN], a[MAXN]; n struct LCT { n   int fa[MAXN], ch[MAXN][2]; n   int s[MAXN], xs[MAXN]; n   long long xs2[MAXN], sum[MAXN], ans[MAXN], xans[MAXN]; n   bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } n   int get(int x) { return ch[fa[x]][1] == x; } n   void up(int x) { n     s[x] = s[ch[x][0]] + s[ch[x][1]] + xs[x] + 1; n     sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + 1ll * a[x] * (xs[x] + 1); n     ans[x] = ans[ch[x][0]] + ans[ch[x][1]] + xans[x] + n              a[x] * (1ll * (xs[x] + 1) * (xs[x] + 1) - xs2[x]) + n              2ll * a[x] * (xs[x] + 1) * s[ch[x][1]] + n              2ll * sum[ch[x][0]] * (s[x] - s[ch[x][0]]); n   } n   void rotate(int x) { n     int f1 = fa[x], f2 = fa[f1], id = get(x); n     ch[f1][id] = ch[x][id ^ 1]; n     fa[ch[f1][id]] = f1; n     if (!isroot(f1)) ch[f2][get(f1)] = x; n     fa[x] = f2; n     ch[x][id ^ 1] = f1; n     fa[f1] = x; n     up(f1); n   } n   void splay(int x) { n     while (!isroot(x)) { n       int f1 = fa[x]; n       if (!isroot(f1)) { n         if (get(x) == get(f1)) n           rotate(f1); n         else n           rotate(x); n       } n       rotate(x); n     } n     up(x); n   } n   void access(int x) { n     for (int y = 0; x; y = x, x = fa[x]) { n       splay(x); n       xs[x] += s[ch[x][1]] - s[y]; n       xs2[x] += 1ll * s[ch[x][1]] * s[ch[x][1]] - 1ll * s[y] * s[y]; n       xans[x] += ans[ch[x][1]] - ans[y]; n       ch[x][1] = y; n       up(x); n     } n   } n   void link(int x, int y) { n     access(x), splay(x); n     access(y), splay(y); n     fa[x] = y; n     xs[y] += s[x]; n     xans[y] += ans[x]; n     xs2[y] += 1ll * s[x] * s[x]; n     up(y); n   } n   void cut(int x, int y) { n     access(y), splay(y); n     splay(x); n     fa[x] = 0; n     xs[y] -= s[x]; n     xans[y] -= ans[x]; n     xs2[y] -= 1ll * s[x] * s[x]; n     up(y); n   } n   bool is_anc(int x, int y) { n     access(y); n     splay(y), splay(x); n     return !isroot(y); n   } n   long long ask() { n     splay(1); n     return ans[1]; n   } n   void modify(int x, int v) { n     access(x), splay(x); n     a[x] = v, up(x); n   } n } tr; n char op[2]; n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) scanf( %d , &far[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) tr.s[i] = 1; n   for (int i = 2; i <= n; i++) tr.link(i, far[i]); n   printf( %.10lf n , (double)tr.ask() / n / n); n   scanf( %d , &m); n   while (m--) { n     int x, y; n     scanf( %s%d%d , op, &x, &y); n     if (op[0] ==  P ) { n       if (tr.is_anc(x, y)) swap(x, y); n       tr.cut(x, far[x]); n       far[x] = y; n       tr.link(x, far[x]); n     } else n       tr.modify(x, y); n     printf( %.10lf n , (double)tr.ask() / n / n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010], b[200010]; n int n, m; n inline bool isprime(int x) { n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return 0; n   return 1; n } n unsigned long long hashseed; n unsigned long long h1[200010], h2[200010], t[200010]; n inline void prehash() { n   for (int i = 1; i < n; i++) n     h1[i] = h1[i - 1] * hashseed + (unsigned long long)b[i]; n   for (int i = n - 1; i; i--) n     h2[i] = h2[i + 1] * hashseed + (unsigned long long)b[i]; n   t[0] = 1; n   for (int i = 1; i <= n; i++) t[i] = t[i - 1] * hashseed; n } n inline unsigned long long q1(int l, int r) { n   return h1[r] - h1[l - 1] * t[r - l + 1]; n } n inline unsigned long long q2(int l, int r) { n   return h2[l] - h2[r + 1] * t[r - l + 1]; n } n inline bool check(int l, int r) { return q1(l, r) == q2(l, r); } n set<int> ans; n int main() { n   srand(time(NULL)); n   hashseed = rand() * rand(); n   while (!isprime(hashseed)) hashseed++; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i < n; i++) b[i] = a[i + 1] - a[i]; n   prehash(); n   bool flag; n   for (int i = 1; i <= n; i++) { n     flag = 1; n     if (i != 1 && !check(1, i - 1)) flag = 0; n     if (i != n) { n       if (a[1] + a[i] + m != a[i + 1] + a[n]) flag = 0; n       if (i != n - 1 && !check(i + 1, n - 1)) flag = 0; n     } n     if (flag) ans.insert((a[1] + a[i]) % m); n   } n   cout << ans.size() << endl; n   for (set<int>::iterator it = ans.begin(); it != ans.end(); it++) n     printf( %d  , *it); n } n


#include <iostream> n #include <algorithm> n #include<cmath> n #include<cstring> n #include<cstdio> n #include<cstdlib> n #include<vector> n #include<iomanip> n #include<ctime> n #include<set> n #include<map> n #include<queue> n #include<stack> n #include<bitset> n #define sqr(x) ((x)*(x)) n #define fz1(i,n) for ((i)=1;(i)<=(n);(i)++) n #define fd1(i,n) for ((i)=(n);(i)>=1;(i)--) n #define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++) n #define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--) n #define fz0k(i,n) for ((i)=0;(i)<(n);(i)++) n #define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--) n #define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++) n #define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--) n #define fzin fz1(i,n) n #define fzim fz1(i,m) n #define fzjn fz1(j,n) n #define fzjm fz1(j,m) n #define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr) n #define rdst(st,len){static char ss[len];scanf(  %s ,ss);(st)=ss;} n #define incm(x,y) {x=((x)+(y))%mod;} n #define spln(i,n) (i==n?  n :   ) n #define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}} n #define fi first n #define se second n #define mk make_pair n using namespace std; n inline void read(int &x) n { n  char c;int f=1; n  while(!isdigit(c=getchar()))if(c== - )f=-1; n  x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15); n  x*=f; n } n int n,m,q,i,j,cur; n vector<unsigned long long> all; n unsigned long long a[200005],op[200005],b[200005]; n struct bit n { n  int b[400005]; n  unsigned long long a[400005]; n  void add(int x,int y) n  { n   unsigned long long t=y*all[x-1]; n   while(x<=m){ n    a[x]+=t; n    b[x]+=y; n    x+=(x&-x); n   } n  } n  unsigned long long query1(int x) n  { n   unsigned long long s=0; n   while(x){ n    s+=a[x]; n    x-=(x&-x); n   } n   return s; n  } n  int query0(int x) n  { n   int s=0; n   while(x){ n    s+=b[x]; n    x-=(x&-x); n   } n   return s; n  } n  int kth(int x) n  { n   int s=0,i; n   for(i=18;i>=0;i--){ n    if(s+(1<<i)>m) continue; n    if(b[s+(1<<i)]<x){ n     x-=b[s+(1<<i)]; n     s+=(1<<i); n    } n   } n   return s+1; n  } n }tr1; n struct nd n { n  unsigned long long s1,s2; n  int sz; n }; n struct seg n { n  nd s[1600005]; n  nd merge(nd x,nd y) n  { n   nd z; n   z.sz=x.sz+y.sz; n   z.s1=x.s1+y.s1; n   z.s2=x.s2+y.s2+y.s1*(unsigned long long)x.sz; n   return z; n  } n  void pushup(int x) n  { n   s[x]=merge(s[x+x],s[x+x+1]); n  } n  void update(int x,int l,int r,int y,int c) n  { n   if(l==r){ n    s[x].s1+=((unsigned long long)c)*all[l-1]; n    s[x].s2+=((unsigned long long)c)*all[l-1]; n    s[x].sz+=c; n    return; n   } n   int mid=(l+r)/2; n   if(y<=mid)update(x+x,l,mid,y,c); n   else update(x+x+1,mid+1,r,y,c); n   pushup(x); n  } n  nd query(int x,int l,int r,int ql,int qr) n  { n   if(ql<=l&&r<=qr) return s[x]; n   int mid=(l+r)/2; n   if(qr<=mid) return query(x+x,l,mid,ql,qr); n   if(ql>mid) return query(x+x+1,mid+1,r,ql,qr); n   return merge(query(x+x,l,mid,ql,qr),query(x+x+1,mid+1,r,ql,qr)); n  } n }tr2; n void ins(int x) n { n  cur++; n  tr1.add(x,1); n  tr2.update(1,1,m,x,1); n } n void del(int x) n { n  cur--; n  tr1.add(x,-1); n  tr2.update(1,1,m,x,-1); n } n unsigned long long gsum1(int r) n { n  if(r==0) return 0; n  if(r==cur) return tr1.query1(m); n  int t=tr1.kth(r); n  unsigned long long s1=tr1.query1(t),s0=tr1.query0(t); n  return s1-(s0-r)*all[t-1]; n } n unsigned long long gsum1(int l,int r) n { n  return gsum1(r)-gsum1(l-1); n } n unsigned long long c2(unsigned long long x) n { n  unsigned long long y=x+1; n  if(x%2==0)x/=2;else y/=2; n  return x*y; n } n unsigned long long gsuml(int len) n { n  if(len==0) return 0; n  int t=tr1.kth(len); n  nd s=tr2.query(1,1,m,1,t); n  return s.s2-(c2(s.sz)-c2(len))*all[t-1]-(s.s1-(s.sz-len)*all[t-1])*(len+1); n } n unsigned long long gsumr(int len) n { n  if(len==0) return 0; n  int t=tr1.kth(cur-len); n  nd s=tr2.query(1,1,m,t,m); n  unsigned long long res=s.s2-(c2(s.sz-len))*all[t-1]-(s.s1-(s.sz-len)*all[t-1])*(s.sz-len); n  return res; n } n void query() n { n  if(cur<=1){ n   puts( 0 ); n   return; n  } n  unsigned long long ans=tr2.s[1].s2*2; n  int s=0,l=0,r=cur/2+1,mid; n  while(l<r){ n   mid=(l+r)/2; n   unsigned long long sl=gsum1(1,mid+1); n   unsigned long long sr=gsum1(cur-mid+1,cur); n   if(sl>=sr){ n    s=mid; n    l=mid+1; n   } n   else r=mid; n  } n  ans+=gsuml(s+1); n  ans+=gsumr(s); n  s=cur,l=cur/2,r=cur-1,mid; n  while(l<r){ n   mid=(l+r+1)/2; n   unsigned long long sl=gsum1(1,mid+1); n   unsigned long long sr=gsum1(cur-mid+1,cur); n   if(sl>=sr){ n    s=mid; n    r=mid-1; n   } n   else l=mid; n  } n  ans-=gsuml(s); n  ans-=gsumr(s-1); n  ans-=tr2.s[1].s1*(cur+1); n  printf( %I64u n ,ans); n } n int main() n { n  scanf( %d%d ,&n,&q); n  fz1(i,n){ n   scanf( %I64u ,&a[i]); n   all.push_back(a[i]); n  } n  fz1(i,q){ n   scanf( %d%I64u ,&op[i],&b[i]); n   all.push_back(b[i]); n  } n  sort(all.begin(),all.end()); n  all.resize(unique(all.begin(),all.end())-all.begin()); n  m=all.size(); n  fz1(i,n){ n   ins(upper_bound(all.begin(),all.end(),a[i])-all.begin()); n  } n  query(); n  fz1(i,q){  n   if(op[i]==1) ins(upper_bound(all.begin(),all.end(),b[i])-all.begin()); else del(upper_bound(all.begin(),all.end(),b[i])-all.begin()); n   query(); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long int T, n; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> T; n   while (T--) { n     cin >> n; n     set<long long int> chk_div; n     vector<long long int> g; n     for (long long int i = 0, u; i < n; i++) cin >> u, g.push_back(u); n     sort(g.begin(), g.end()); n     long long int p = (g[0] * g[n - 1]); n     for (long long int i = 2; i * i <= p; i++) n       if (!(p % i)) chk_div.insert(i), chk_div.insert(p / i); n     set<long long int> div(g.begin(), g.end()); n     (chk_div == div) ? (cout << p <<   n ) : (cout <<  -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1 << 15 + 5; n int deg[MAXN], s[MAXN]; n queue<int> q; n int main() { n   int n, sum = 0, u, v; n   cin >> n; n   while (!q.empty()) q.pop(); n   for (int i = 0; i < n; i++) { n     cin >> deg[i] >> s[i]; n     if (deg[i] == 1) q.push(i); n     sum += deg[i]; n   } n   cout << sum / 2 << endl; n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     if (deg[u] == 0) continue; n     deg[u] = 0; n     v = s[u]; n     s[v] ^= u; n     deg[v]--; n     if (deg[v] == 1) q.push(v); n     cout << u <<     << v << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     vector<long> vt; n     long i = 0, n, temp, count = 1; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> temp; n       vt.push_back(temp); n     } n     sort(vt.begin(), vt.end()); n     while (i < n) { n       if (vt[i] <= count) { n         count++; n       } else { n         temp = count; n         bool check = true; n         for (int j = i; j < n; j++) { n           if (vt[j] <= temp) { n             check = true; n             temp++; n             i = j; n             break; n           } else { n             check = false; n             temp++; n           } n         } n         if (check == false) n           break; n         else n           count = temp; n       } n       i++; n     } n     cout << count << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long h[1000005]; n long long dp[1000005][2]; n const int mod = 1e9 + 7; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &h[i]), h[i]--; n   for (int i = 1; i <= n; i++) { n     dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] * min(h[i], h[i - 1]) + h[i]) % mod; n     dp[i][1] = ((min(h[i + 1], h[i])) + n                 min(h[i + 1], min(h[i], h[i - 1])) * dp[i - 1][1]) % n                mod; n   } n   printf( %I64d n , dp[n][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(pair<int, int> p1, pair<int, int> p2) { n   return (p1.first < p2.first) || n          (p1.first == p2.first && p1.second > p2.second); n } n int main() { n   bool b[200000]; n   int t[200000]; n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> t[i]; n     b[i] = true; n   } n   int answer = 1; n   for (int i = 2; i <= n; ++i) { n     if (t[i] == 0) { n       ++answer; n       continue; n     } n     if (b[t[i]]) n       b[t[i]] = false; n     else n       ++answer; n   } n   cout << answer; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <class T> n using pq = priority_queue<T>; n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n int scan() { return getchar(); } n template <class T> n void scan(T a) { n   cin >> a; n } n void scan(int &a) { cin >> a; } n void scan(long long &a) { cin >> a; } n void scan(char &a) { cin >> a; } n void scan(double &a) { cin >> a; } n void scan(long double &a) { cin >> a; } n void scan(char a[]) { scanf( %s , a); } n void scan(string &a) { cin >> a; } n template <class T> n void scan(vector<T> &); n template <class T, size_t size> n void scan(array<T, size> &); n template <class T, class L> n void scan(pair<T, L> &); n template <class T, size_t size> n void scan(T (&)[size]); n template <class T> n void scan(vector<T> &a) { n   for (auto &i : a) scan(i); n } n template <class T> n void scan(deque<T> &a) { n   for (auto &i : a) scan(i); n } n template <class T, size_t size> n void scan(array<T, size> &a) { n   for (auto &i : a) scan(i); n } n template <class T, class L> n void scan(pair<T, L> &p) { n   scan(p.first); n   scan(p.second); n } n template <class T, size_t size> n void scan(T (&a)[size]) { n   for (auto &i : a) scan(i); n } n template <class T> n void scan(T &a) { n   cin >> a; n } n void IN() {} n template <class Head, class... Tail> n void IN(Head &head, Tail &...tail) { n   scan(head); n   IN(tail...); n } n string stin() { n   string s; n   cin >> s; n   return s; n } n template <class T, class S> n inline bool chmax(T &a, S b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T, class S> n inline bool chmin(T &a, S b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n vector<int> iota(int n) { n   vector<int> a(n); n   iota(begin(a), end(a), 0); n   return a; n } n template <class T> n void UNIQUE(vector<T> &x) { n   sort(begin(x), end(x)); n   x.erase(unique(begin(x), end(x)), x.end()); n } n int in() { n   int x; n   cin >> x; n   return x; n } n long long lin() { n   unsigned long long x; n   cin >> x; n   return x; n } n void print() { putchar(   ); } n void print(bool a) { cout << a; } n void print(int a) { cout << a; } n void print(long long a) { cout << a; } n void print(char a) { cout << a; } n void print(string &a) { cout << a; } n void print(double a) { cout << a; } n template <class T> n void print(const vector<T> &); n template <class T, size_t size> n void print(const array<T, size> &); n template <class T, class L> n void print(const pair<T, L> &p); n template <class T, size_t size> n void print(const T (&)[size]); n template <class T> n void print(const vector<T> &a) { n   if (a.empty()) return; n   print(a[0]); n   for (auto i = a.begin(); ++i != a.end();) { n     cout <<    ; n     print(*i); n   } n   cout << endl; n } n template <class T> n void print(const deque<T> &a) { n   if (a.empty()) return; n   print(a[0]); n   for (auto i = a.begin(); ++i != a.end();) { n     cout <<    ; n     print(*i); n   } n } n template <class T, size_t size> n void print(const array<T, size> &a) { n   print(a[0]); n   for (auto i = a.begin(); ++i != a.end();) { n     cout <<    ; n     print(*i); n   } n } n template <class T, class L> n void print(const pair<T, L> &p) { n   cout <<  ( ; n   print(p.first); n   cout <<  , ; n   print(p.second); n   cout <<  ) ; n } n template <class T> n void print(set<T> &x) { n   for (auto e : x) print(e), cout <<    ; n   cout << endl; n } n template <class T> n void print(multiset<T> &x) { n   for (auto e : x) print(e), cout <<    ; n   cout << endl; n } n template <class T, size_t size> n void print(const T (&a)[size]) { n   print(a[0]); n   for (auto i = a; ++i != end(a);) { n     cout <<    ; n     print(*i); n   } n } n template <class T> n void print(const T &a) { n   cout << a; n } n int out() { n   putchar(  n ); n   return 0; n } n template <class T> n int out(const T &t) { n   print(t); n   putchar(  n ); n   return 0; n } n template <class Head, class... Tail> n int out(const Head &head, const Tail &...tail) { n   print(head); n   putchar(   ); n   out(tail...); n   return 0; n } n long long gcd(long long a, long long b) { n   while (b) { n     long long c = b; n     b = a % b; n     a = c; n   } n   return a; n } n long long lcm(long long a, long long b) { n   if (!a || !b) return 0; n   return a * b / gcd(a, b); n } n vector<pair<long long, long long>> factor(long long x) { n   vector<pair<long long, long long>> ans; n   for (long long i = 2; i * i <= x; i++) n     if (x % i == 0) { n       ans.push_back({i, 1}); n       while ((x /= i) % i == 0) ans.back().second++; n     } n   if (x != 1) ans.push_back({x, 1}); n   return ans; n } n template <class T> n vector<T> divisor(T x) { n   vector<T> ans; n   for (T i = 1; i * i <= x; i++) n     if (x % i == 0) { n       ans.push_back(i); n       if (i * i != x) ans.push_back(x / i); n     } n   return ans; n } n template <typename T> n void zip(vector<T> &x) { n   vector<T> y = x; n   sort(begin(y), end(y)); n   for (int i = 0; i < x.size(); ++i) { n     x[i] = distance((y).begin(), lower_bound(begin(y), end(y), (x[i]))); n   } n } n int popcount(long long x) { return __builtin_popcountll(x); } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); } n long long rndll(long long n) { n   return uniform_int_distribution<long long>(0, n - 1)(rng); n } n template <typename T> n void shuffle(vector<T> &v) { n   for (long long i = v.size() - 1; i >= 1; --i) { n     swap(v[i], v[rnd(i)]); n   } n } n vector<string> YES{ NO ,  YES }; n vector<string> Yes{ No ,  Yes }; n vector<string> yes{ no ,  yes }; n template <class... T> n void err(const T &...) {} n template <typename T> n struct edge { n   int from, to; n   T cost; n   int id; n   edge(int to, T cost) : from(-1), to(to), cost(cost) {} n   edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} n   edge(int from, int to, T cost, int id) n       : from(from), to(to), cost(cost), id(id) {} n   edge &operator=(const int &x) { n     to = x; n     return *this; n   } n   operator int() const { return to; } n }; n template <typename T> n using Edges = vector<edge<T>>; n struct Graph : vector<vector<int>> { n   using vector<vector<int>>::vector; n   Graph(int n, int m) : vector(n) { read(m); } n   inline void add(int a, int b, bool directed = false) { n     (*this)[a].emplace_back(b); n     if (!directed) (*this)[b].emplace_back(a); n   } n   void read(int n = -1, int offset = 1, bool directed = false) { n     if (n == -1) n = this->size() - 1; n     int a, b; n     while (n--) { n       cin >> a >> b; n       Graph::add(a - offset, b - offset, directed); n     } n   } n }; n template <typename T> n struct WeightedGraph : vector<Edges<T>> { n   using vector<Edges<T>>::vector; n   inline void add(int a, int b, T c, bool directed = false) { n     (*this)[a].emplace_back(b, c); n     if (!directed) (*this)[b].emplace_back(a, c); n   } n   void read(int n = -1, int offset = 1) { n     if (n == -1) n = this->size() - 1; n     int a, b; n     T c; n     while (n--) { n       cin >> a >> b >> c; n       WeightedGraph::add(a - offset, b - offset, c); n     } n   } n }; n struct Setup_io { n   Setup_io() { n     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     cout << fixed << setprecision(15); n   } n } setup_io; n int main() { n   int n; n   IN(n); n   vector<int> a(n); n   IN(a); n   vector<vector<vector<bool>>> dp( n       n + 1, vector<vector<bool>>(100, vector<bool>(3000))); n   vector<vector<vector<pair<int, int>>>> pre( n       n + 1, vector<vector<pair<int, int>>>(100, vector<pair<int, int>>(3000))); n   dp[0][0][0] = 1; n   vector<int> tr(100); n   for (long long i = 1; i <= 98; ++i) tr[i + 1] = tr[i] + i; n   sort(begin(a), end(a)); n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < a[i] * 2 + 2; ++j) { n       for (long long k = 0; k < 3000; ++k) { n         if (!dp[i][j][k]) continue; n         int jj = j + 1, kk = k + a[i]; n         while (kk >= tr[jj]) { n           dp[i + 1][jj][kk] = true; n           pre[i + 1][jj][kk] = pair<int, int>(j, k); n           jj++, kk += a[i]; n         } n       } n     } n   } n   int m = 0; n   vector<int> v; n   for (long long i = 1; i <= 64; ++i) { n     if (dp[n][i][tr[i]]) { n       m = i; n       int j = i, k = tr[i]; n       int t = n; n       while (t > 0) { n         auto [jj, kk] = pre[t][j][k]; n         for (long long _ = 0; _ < j - jj; ++_) v.emplace_back(a[t - 1]); n         t--, j = jj, k = kk; n       } n       break; n     } n   } n   if (!m) { n     cout <<  =(  <<   n ; n     return 0; n   } n   reverse(begin(v), end(v)); n   cout << m <<   n ; n   vector<vector<int>> g(m, vector<int>(m, -1)); n   auto id = iota(m); n   for (long long i = m - 1; i >= 0; --i) { n     for (long long j = 0; j < v[id[i]]; ++j) n       g[id[i]][id[j]] = 1, g[id[j]][id[i]] = 0; n     for (long long j = v[id[i]]; j <= i - 1; ++j) n       g[id[i]][id[j]] = 0, g[id[j]][id[i]] = 1, v[id[j]]--; n     sort(id.begin(), id.begin() + i, [&](int a, int b) { return v[a] < v[b]; }); n   } n   for (long long i = 0; i < m; ++i) g[i][i] = 0; n   for (long long i = 0; i < m; ++i) { n     for (long long j = 0; j < m; ++j) { n       cout << g[i][j]; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n FILE* stream; n const long long P = 998244353; n long long mpow(long long n, int deg) { n   if (deg == 0) { n     return 1; n   } n   if (deg % 2 == 0) { n     return mpow((n * n) % P, deg / 2) % P; n   } n   return (mpow(n, deg - 1) * n) % P; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long w, h; n   cin >> w >> h; n   cout << mpow(2, w + h) % P << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n map<int, int> a; n bool found; n int b[5][5], sum, cols[5]; n void backTrack(int x, int y, int sumRow, int sumDia1, int sumDia2) { n   if (x == n) { n     if (sumDia1 != sum || sumDia2 != sum) return; n     for (int i = 0; i < n; i++) n       if (cols[i] != sum) return; n     found = true; n     return; n   } n   for (typeof(a.begin()) it = a.begin(); it != a.end(); it++) n     if (it->second > 0) { n       it->second--; n       sumRow += it->first; n       if (x == y) sumDia1 += it->first; n       if (x + y == n - 1) sumDia2 += it->first; n       cols[y] += it->first; n       int xx = x, yy = y + 1; n       if (yy == n) yy = 0, xx++; n       b[x][y] = it->first; n       if (yy == 0) { n         if (sumRow == sum) backTrack(xx, yy, 0, sumDia1, sumDia2); n       } else n         backTrack(xx, yy, sumRow, sumDia1, sumDia2); n       it->second++; n       sumRow -= it->first; n       if (x == y) sumDia1 -= it->first; n       if (x + y == n - 1) sumDia2 -= it->first; n       cols[y] -= it->first; n       if (found) return; n     } n } n int main() { n   sum = 0; n   scanf( %d , &n); n   for (int i = 0, val; i < n * n; i++) { n     scanf( %d , &val); n     a[val]++; n     sum += val; n   } n   sum /= n; n   found = false; n   memset(cols, 0, sizeof(cols)); n   backTrack(0, 0, 0, 0, 0); n   printf( %d n , sum); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (j) putchar(   ); n       printf( %d , b[i][j]); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = 3.141592653589793; n const double EPS = 1e-9; n const int N = 1e6 + 5; n const long long mod = 1e9 + 7; n const int oo = 1e9 + 9; n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n int n, m; n long long row[1005], col[1005]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i <= n - 1; i++) { n     for (int j = 0; j <= m - 1; j++) { n       int x; n       scanf( %d , &x); n       row[i] += x; n       col[j] += x; n     } n   } n   long long min_row = 1e18; n   int ans_row; n   for (int i = 0; i <= n; i++) { n     long long curr = 0; n     for (int j = i, x = 2; j < n; j++, x += 4) { n       curr += x * x * row[j]; n     } n     for (int j = i - 1, x = 2; j >= 0; j--, x += 4) { n       curr += x * x * row[j]; n     } n     if (curr < min_row) { n       ans_row = i; n       min_row = curr; n     } n   } n   long long min_col = 1e18; n   int ans_col; n   for (int i = 0; i <= m; i++) { n     long long curr = 0; n     for (int j = i, y = 2; j < m; j++, y += 4) { n       curr += (long long)y * y * col[j]; n     } n     for (int j = i - 1, y = 2; j >= 0; j--, y += 4) { n       curr += (long long)y * y * col[j]; n     } n     if (curr < min_col) { n       ans_col = i; n       min_col = curr; n     } n   } n   printf( %lld n%d %d , min_col + min_row, ans_row, ans_col); n } n


#include <bits/stdc++.h> n using namespace std; n long long input() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= ch ==  - , ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n const int N = 107; n string s[N]; n map<string, int> p; n int vis[N]; n string res; n int main() { n   int n = input(), m = input(); n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     if (p[s[i]]) n       res += s[i], vis[i] = vis[p[s[i]]] = 1; n     else n       reverse(s[i].begin(), s[i].end()), p[s[i]] = i; n   } n   string ser = res; n   reverse(ser.begin(), ser.end()); n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     int flag = 1; n     for (int j = 0; j < m / 2; j++) { n       if (s[i][j] != s[i][m - j - 1]) { n         flag = 0; n         break; n       } n     } n     if (flag == 1) { n       res += s[i]; n       break; n     } n   } n   res += ser; n   cout << res.length() << endl; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long sum = 0, f = 1; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) ch = getchar(), f = -1; n   while (ch <=  9  && ch >=  0 ) sum = sum * 10 + ch -  0 , ch = getchar(); n   return sum * f; n } n int n; n long long w[22]; n bool used[(1 << 20) + 10]; n int st[22], num, end_, maxn; n bool col[22]; n long long A[(1 << 20) + 10], B[(1 << 20) + 10], cntA, cntB; n long long AA, BB, AA_, BB_; n void dfs(int a, long long cha, int ty, int allodd, int alleven) { n   if (a == end_) { n     if (!ty) n       A[++cntA] = cha; n     else n       B[++cntB] = cha; n     if (!ty) { n       if (allodd) AA = cha; n       if (alleven) AA_ = cha; n     } else { n       if (allodd) BB = cha; n       if (alleven) BB_ = cha; n     } n     return; n   } n   col[a] = 0, dfs(a + 1, cha - w[st[a]], ty, 0, alleven); n   col[a] = 1, dfs(a + 1, cha + w[st[a]], ty, allodd, 0); n } n bool chk(int sta) { n   num = 0; n   long long sum = 0; n   for (int i = 1; i <= n; i++) n     if (sta & (1 << (i - 1))) st[++num] = i, sum += w[i]; n   if (num == 1 || (sum & 1) != ((num - 1) & 1)) return 0; n   int mid = num >> 1; n   cntA = cntB = 0; n   AA = AA_ = BB = BB_ = 2e18; n   end_ = mid + 1, dfs(1, 0, 0, 1, 1); n   end_ = num + 1, dfs(mid + 1, 0, 1, 1, 1); n   sort(A + 1, A + 1 + cntA); n   sort(B + 1, B + 1 + cntB); n   int pB = cntB, pA = cntA; n   if (abs(AA + BB_) <= num - 1 || abs(AA_ + BB) <= num - 1) return 1; n   for (int i = 1; i <= cntA; i++) { n     if (A[i] == AA_ || A[i] == AA) continue; n     long long l = 1 - num - A[i], r = num - 1 - A[i]; n     while (pB && B[pB] > r) pB--; n     if (!pB) return 0; n     if (B[pB] >= l) return 1; n   } n   for (int i = 1; i <= cntB; i++) { n     if (B[i] == BB_ || B[i] == BB) continue; n     long long l = 1 - num - B[i], r = num - 1 - B[i]; n     while (pA && A[pA] > r) pA--; n     if (!pA) return 0; n     if (A[pA] >= l) return 1; n   } n   return 0; n } n int f[(1 << 20) + 10 + 10]; n int s[22]; n int main() { n   n = read(); n   int q = 0; n   for (int i = 1; i <= n; i++) { n     w[i] = read(); n     if (w[i]) w[++q] = w[i]; n   } n   n = q; n   maxn = (1 << n) - 1; n   for (int i = 1; i <= maxn; i++) { n     used[i] = chk(i); n     if (used[i]) f[i] = 1; n   } n   for (int i = 1; i <= maxn; i++) n     if (used[i]) { n       int g = maxn ^ i; n       for (int j = g; j; j = (j - 1) & g) { n         f[i + j] = max(f[i + j], f[j] + f[i]); n       } n     } n   cout << n - f[maxn]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n string to_string(string s) { return     + s +    ; } n string to_string(char s) { return string(1, s); } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A> n string to_string(A); n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool f = 1; n   string r =  { ; n   for (const auto& x : v) { n     if (!f) r +=  ,  ; n     f = 0; n     r += to_string(x); n   } n   return r +  } ; n } n void debug_out() { cout << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cout <<     << to_string(H); n   debug_out(T...); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int v; n   cin >> v; n   while (v--) { n     int n = 250; n     vector<int> a(n); n     double m = 0; n     for (auto i = (0); i <= (n - 1); ++i) cin >> a[i], m += a[i]; n     m /= n; n     double var = 0; n     for (auto i = (0); i <= (n - 1); ++i) var += (a[i] - m) * (a[i] - m); n     var /= n; n     if (abs(var - m) > abs(var - (m * m + m) / 3)) { n       int mx = *max_element((a).begin(), (a).end()); n       cout << mx / 2 <<   n ; n     } else { n       cout << (int)round(m) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int find(int u[], int i) { n   if (u[i] == i) { n     return i; n   } n   return (u[i] = find(u, u[i])); n } n int main() { n   int n, m; n   cin >> n >> m; n   if (n != m + 1) { n     cout <<  no  << endl; n     return 0; n   } n   int ufs[n]; n   for (int i = 0; i < n; i++) { n     ufs[i] = i; n   } n   int a, b; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     int c = find(ufs, a - 1); n     int d = find(ufs, b - 1); n     if (c == d) { n       cout <<  no  << endl; n       return 0; n     } n     int m = min(c, d); n     ufs[c] = m; n     ufs[d] = m; n   } n   for (int i = 0; i < n; i++) { n     if (find(ufs, i) != 0) { n       cout <<  no  << endl; n       return 0; n     } n   } n   cout <<  yes  << endl; n } n


#include <bits/stdc++.h> n #include<cmath> n using namespace std; n  n int main(){ n     long long t; n     cin>>t; n     while(t--){ n         int a,b,n; n         long long ans=0; n         cin>>a>>b>>n; n         int ar[a+1],br[b+1],arr[2][n]; n         memset(ar,0,sizeof(ar)); n         memset(br,0,sizeof(br)); n         for(int i=0;i<n;i++){ n             cin>>arr[0][i]; n             ar[arr[0][i]]++; n         } n         for(int i=0;i<n;i++){ n             cin>>arr[1][i]; n             br[arr[1][i]]++; n         } n         for(int i=0;i<n-1;i++){ n             ans+=n-i-1 -ar[arr[0][i]]+1 -br[arr[1][i]]+1; n             ar[arr[0][i]]--; n             br[arr[1][i]]--; n         } n         cout<<ans<<endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100 * 3; n const int INF = 1E9; n const int base = 1E9 + 7; n void no() { n   cout <<  no n ; n   exit(0); n } n int a[maxn]; n pair<int, int> b[maxn]; n char ans[3030][3030]; n int n, m; n void validate() { n   vector<int> draws; n   n = m; n   for (int i = 0; i < n; i++) n     if (a[i] & 1) draws.push_back(i); n   memset(ans, 0, sizeof(ans)); n   for (int i = 0; i < draws.size(); i += 2) { n     int x, y; n     x = draws[i], y = draws[i + 1]; n     a[x]--, a[y]--; n     ans[x][y] = ans[y][x] =  D ; n   } n   for (int i = 0; i < n; i++) b[i] = make_pair(a[i], i); n   for (int i = 0; i < n; i++) ans[i][i] =  X ; n   for (int i = 0; i < n; i++) { n     sort(b + i, b + n); n     reverse(b + i, b + n); n     int g = b[i].second; n     for (int j = n - 1; j > i; j--) n       if (ans[g][b[j].second] == 0) { n         char w =  W , l =  L ; n         if (b[i].first > 0) { n           ans[g][b[j].second] = w, ans[b[j].second][g] = l; n           b[i].first -= 2; n         } else { n           ans[g][b[j].second] = l, ans[b[j].second][g] = w; n           b[j].first -= 2; n         } n       } n     if (b[i].first != 0) no(); n   } n   puts( yes ); n   for (int i = 0; i < n; i++) puts(ans[i]); n } n int solve() { n   cin >> m >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int sum = 0; n   for (int i = 0; i < n; i++) sum += a[i]; n   if (sum > m * (m - 1)) no(); n   if (sum + (m - n) * a[n - 1] < m * (m - 1)) no(); n   while (sum < m * (m - 1)) n     for (int i = n; sum < m * (m - 1) && i < m; i++) a[i]++, sum++; n   validate(); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n const long long mod = 1e9 + 7; n long long num[N]; n long long a[N]; n long long b[N]; n long long ans[N]; n signed main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long T = 1; n   cin >> T; n   while (T--) { n     long long n; n     cin >> n; n     for (long long i = 1; i <= n; i++) cin >> num[i]; n     a[1] = num[1] - 1, b[1] = 1; n     for (long long i = 2; i <= n; i++) { n       a[i] = min(a[i - 1], num[i] - b[i - 1]); n       b[i] = num[i] - a[i]; n     } n     bool flag = true; n     long long ma = *min_element(a + 1, a + 1 + n); n     long long mb = *min_element(b + 1, b + n + 1); n     if (ma + mb < 0) flag = false; n     cout << (flag ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b, c, d; n   scanf( %lld %lld %lld %lld , &a, &b, &c, &d); n   printf( %lld %lld %lld n , b, c, c); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[300005], m[300005], t[300005], ans, sum, n, x, qwq; n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld%lld , &a[i], &m[i]); n     x = m[i]; n     while (x) { n       x >>= 1ll; n       t[i]++; n     } n     sum += a[i]; n   } n   if (sum < 0) n     for (long long i = 1; i <= n; i++) a[i] *= -1ll; n   for (long long i = 0; i <= 62; i++) { n     qwq = 0ll; n     for (long long j = 1; j <= n; j++) n       if (m[j] & (1ll << i) && t[j] - 1ll == i) qwq += a[j]; n     if (qwq > 0) { n       for (long long j = 1; j <= n; j++) n         if (m[j] & (1ll << i)) a[j] *= -1ll; n       ans += (1ll << i); n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long ans[maxn << 2]; n int main() { n   long long n, k, m; n   scanf( %lld%lld , &n, &k); n   m = (n * 6 - 1) * k; n   printf( %lld n , m); n   for (int i = 1; i <= n; i++) { n     long long tmp = 6 * (i - 1); n     printf( %lld %lld %lld %lld n , (tmp + 1LL) * k, (tmp + 2LL) * k, n            (tmp + 3LL) * k, (tmp + 5LL) * k); n   } n   return 0; n } n