#include <bits/stdc++.h>   using namespace std;   int a[1000005];   queue<long long int> q;   vector<long long int> v;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int i, j, k, n, m, mid;     cin >> k;     string s;     cin >> s;     n = s.size();     int co = 0;     long long int sum = 0;     if (k == 0) {       for (i = 0; i < n; i++) {         if (s[i] ==  0 ) {           co++;         } else           co = 0;         sum += (co);       }       cout << sum;       return 0;     }     for (i = 0; i < n; i++) {       if (s[i] ==  1 ) {         a[i] = co;         co = 0;         v.push_back(i);       } else         co++;     }     long long int first = 0;     long long int l = v.size();     for (i = k - 1; i < l; i++) {       long long int temp1 = a[v[first]] + 1;       long long int temp2;       if (i + 1 < l)         temp2 = v[i + 1] - v[i];       else         temp2 = n - v[i];       sum += temp1 * temp2;       first++;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int readint() {     int x = 0;     bool f = 0;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - ) {       f = 1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     return f ? -x : x;   }   const int maxn = 4e5 + 5;   int n, m, c[maxn];   vector<int> g[maxn];   int fa[maxn];   void dfs(int u) {     for (int i = 0; i < (int)g[u].size(); i++) {       int v = g[u][i];       if (v == fa[u]) continue;       fa[v] = u;       dfs(v);     }   }   struct lct {     int fa[maxn], ch[maxn][2], s[maxn], s2[maxn];     long long ss[maxn];     void init() {       for (int i = 1; i <= n; i++) s[i] = 1;     }     void pushup(int x) {       s[x] = s2[x] + 1;       if (ch[x][0]) s[x] += s[ch[x][0]];       if (ch[x][1]) s[x] += s[ch[x][1]];     }     bool isroot(int x) {       return !fa[x] || (ch[fa[x]][0] != x && ch[fa[x]][1] != x);     }     void rotate(int x) {       int y = fa[x], z = fa[fa[x]];       if (!isroot(y)) ch[z][ch[z][1] == y] = x;       bool d = ch[y][1] == x;       int k = ch[x][!d];       if (k) fa[k] = y;       ch[y][d] = k;       ch[x][!d] = y;       fa[y] = x;       fa[x] = z;       pushup(y);       pushup(x);     }     void splay(int x) {       while (!isroot(x)) {         int y = fa[x], z = fa[fa[x]];         if (!isroot(y)) {           if ((ch[y][1] == x) ^ (ch[z][1] == y))             rotate(x);           else             rotate(y);         }         rotate(x);       }     }     void access(int x) {       int y = 0;       while (x) {         splay(x);         if (ch[x][1]) {           s2[x] += s[ch[x][1]];           ss[x] += 1ll * s[ch[x][1]] * s[ch[x][1]];         }         ch[x][1] = y;         if (y) {           s2[x] -= s[y];           ss[x] -= 1ll * s[y] * s[y];         }         y = x;         x = fa[x];       }     }     int findroot(int x) {       access(x);       splay(x);       while (ch[x][0]) x = ch[x][0];       splay(x);       return x;     }     void link(int x, int y) {       access(y);       splay(y);       splay(x);       fa[x] = y;       s2[y] += s[x];       ss[y] += 1ll * s[x] * s[x];       pushup(y);     }     void cut(int x, int y) {       access(y);       splay(y);       splay(x);       fa[x] = 0;       s2[y] -= s[x];       ss[y] -= 1ll * s[x] * s[x];       pushup(y);     }   } t;   vector<pair<int, int> > q[maxn];   bool c2[maxn];   long long query(int u) {     int x = t.findroot(u);     if (c2[x])       return 1ll * t.s[t.ch[x][1]] * t.s[t.ch[x][1]];     else       return 1ll * t.s[x] * t.s[x];   }   long long modify(int u) {     c2[u] ^= 1;     if (c2[u]) {       if (u == 1) {         t.access(1);         return t.ss[1] - 1ll * t.s[1] * t.s[1];       } else {         long long res = -query(u);         t.cut(u, fa[u]);         t.access(u);         res += t.ss[u];         if (!c2[fa[u]]) res += query(fa[u]);         return res;       }     } else {       if (u == 1) {         t.access(1);         return 1ll * t.s[1] * t.s[1] - t.ss[1];       } else {         t.access(u);         long long res = -t.ss[u];         if (!c2[fa[u]]) res -= query(fa[u]);         t.link(u, fa[u]);         res += query(u);         return res;       }     }   }   long long ans[maxn];   void solve(int c) {     for (int i = 0; i < (int)q[c].size(); i++)       ans[q[c][i].first] += modify(q[c][i].second);     for (int i = (int)q[c].size() - 1; i >= 0; i--) modify(q[c][i].second);   }   int main() {     n = readint();     m = readint();     for (int i = 1; i <= n; i++) c[i] = readint();     for (int i = 1; i < n; i++) {       int u, v;       u = readint();       v = readint();       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1);     t.init();     for (int i = 2; i <= n; i++) t.link(i, fa[i]);     for (int i = 1; i <= n; i++) q[c[i]].push_back(pair<int, int>(0, i));     for (int i = 1; i <= m; i++) {       int u, x;       u = readint();       x = readint();       q[c[u]].push_back(pair<int, int>(i, u));       q[c[u] = x].push_back(pair<int, int>(i, u));     }     for (int i = 1; i <= n; i++) solve(i);     for (int i = 1; i <= m; i++) ans[i] += ans[i - 1];     for (int i = 0; i <= m; i++) printf( %lld n , -ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long a[100000], b[100000];   long long val(long long p) {     long long ret = 0;     for (long long i = 0; i < n; ++i) ret += max((long long)0, p - a[i]);     for (long long i = m - 1; i >= 0; --i) ret += max((long long)0, b[i] - p);     return ret;   }   int32_t main() {     cin >> n >> m;     for (long long i = 0; i < n; ++i) cin >> a[i];     for (long long i = 0; i < m; ++i) cin >> b[i];     sort(a, a + n), sort(b, b + m);     long long l = 0, r = 1e9 + 1, m1, m2, v1, v2;     long long ans = LLONG_MAX;     while (l <= r) {       m1 = l + (r - l) / 3;       m2 = l + 2 * (r - l) / 3;       v1 = val(m1), v2 = val(m2);       if (v1 <= v2) {         ans = min(v1, ans);         r = m2 - 1;       } else {         ans = min(v2, ans);         l = m1 + 1;       }     }     cout << ans;   }


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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