#include <bits/stdc++.h>   using namespace std;   long long dx[] = {1, 0, -1, 0};   long long dy[] = {0, 1, 0, -1};   long long gcd(long long x, long long y) {     if (y == 0)       return x;     else       return gcd(y, x % y);   }   long long expo(long long n, long long m, long long p) {     long long r = 1;     n = n % p;     while (m > 0) {       if (m % 2) r = (r * n) % p;       n = (n * n) % p;       m = m / 2;     }     return r % p;   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     map<double, long long> p;     vector<string> v(n);     for (long long j = 0; j < n; j++) {       string s;       cin >> s;       v[j] = s;       long long a = 0, b = 0, c = 0;       long long i = 1;       while (s[i] !=  + ) {         a = a * 10 + s[i] -  0 ;         i++;       }       i++;       while (s[i] !=  ) ) {         b = b * 10 + s[i] -  0 ;         i++;       }       i++;       i++;       while (i != s.size()) {         c = c * 10 + s[i] -  0 ;         i++;       }       double aa = (a * 1.0 + b) / c;       p[aa]++;     }     for (long long j = 0; j < n; j++) {       string s = v[j];       long long a = 0, b = 0, c = 0;       long long i = 1;       while (s[i] !=  + ) {         a = a * 10 + s[i] -  0 ;         i++;       }       i++;       while (s[i] !=  ) ) {         b = b * 10 + s[i] -  0 ;         i++;       }       i++;       i++;       while (i != s.size()) {         c = c * 10 + s[i] -  0 ;         i++;       }       double aa = (a * 1.0 + b) / c;       cout << p[aa] <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;       mt19937_64 rng(time(0));   const long double eps = 1e-15;   #define rep(i, a, b) for(int i = a; i < (b); ++i)   #define trav(a, x) for(auto& a : x)   #define all(x) x.begin(), x.end()   #define sz(x) (int)(x).size()   typedef long long ll;   typedef pair<int, int> pii;   typedef pair<ll,ll> pll;   typedef vector<int> vi;   const int mn=300+10;   const ll mod=1e9+7;      ll po(ll a,ll b=mod-2){    ll ans=1;    for(;b;b>>=1,a*=a,a%=mod)if(b&1)ans*=a,ans%=mod;    return ans;   }      ll c[mn],b[mn];   ll ans[mn*2];   ll dp[2][10100];      int main() {    cin.tie(0);    cin.sync_with_stdio(0);    int n;    cin>>n;    for(int i=0;i<n;i++)cin>>c[i];    for(int i=1;i<n;i++)cin>>b[i],b[i]+=b[i-1];    for(int i=1;i<n;i++)b[i]+=b[i-1];    int ma=INT_MAX,mi=INT_MAX,sa=0,si=0;    for(int i=0;i<n;i++){     sa+=c[i];     si+=0;     ma=min(ll(ma),(sa-b[i])/(i+1)+1);     mi=min(ll(mi),(si-b[i])/(i+1)-1);    }    //cerr<<mi<<   <<ma<<endl;        for(int m=mi;m<=ma;m++){     memset(dp,0,sizeof(dp));     dp[0][0]=1;     for(int i=0;i<n;i++){      ll lim = m*(i+1)+b[i];      //cerr<<lim<<endl;      for(int j=1;j<10100;j++)dp[0][j]+=dp[0][j-1],dp[0][j]%=mod;      for(int j=0;j<10100;j++){       dp[1][j]=(dp[0][j]-((j>=c[i]+1)?dp[0][j-c[i]-1]:0))%mod;      }      for(int j=0;j<min(lim,ll(10100));j++)dp[1][j]=0;      memcpy(dp[0],dp[1],sizeof(dp[0]));      memset(dp[1],0,sizeof(dp[1]));     }     for(int i=0;i<10100;i++)ans[m-mi]+=dp[0][i],ans[m-mi]%=mod;     if(ans[m-mi]<0)ans[m-mi]+=mod;    }    int q;    cin>>q;    ll aa=1;    for(int i=0;i<n;i++)aa*=c[i]+1,aa%=mod;    for(int i=0;i<q;i++){     ll x;     cin>>x;     if(x<mi){      printf( %lld n ,aa);     }     else if(x>ma)printf( 0 n );     else printf( %lld n ,ans[x-mi]);    }   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int main() {     long long s = 0;     cin >> n >> k;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       s += b - a + 1;     }     s %= k;     if (s) s = k - s;     cout << s << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 9;   const int MAXN = 3e5 + 1;   int l[MAXN], r[MAXN];   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> v(n);       for (int i = (int)(0); i < (int)(n); ++i) {         l[i] = INF;         r[i] = -INF;       }       for (int i = (int)(0); i < (int)(n); ++i) {         cin >> v[i];         --v[i];         l[v[i]] = min(l[v[i]], i);         r[v[i]] = max(r[v[i]], i);       }       sort(v.begin(), v.end());       v.erase(unique(v.begin(), v.end()), v.end());       vector<int> dp(n, 0);       int ans = n;       for (int i = 0; i < v.size(); ++i) {         if (!i || l[v[i]] <= r[v[i - 1]])           dp[i] = 1;         else           dp[i] = dp[i - 1] + 1;         ans = min(ans, (int)(v.size() - dp[i]));       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>      #define ed end()   #define bg begin()   #define mp make_pair   #define pb push_back   #define vv(T) v(v(T))   #define v(T) vector<T>   #define all(x) x.bg,x.ed   #define newline puts(  )   #define si(x) ((int)x.size())   #define rep(i,n) for(int i=1;i<=n;++i)   #define rrep(i,n) for(int i=0;i<n;++i)   #define srep(i,s,t) for(int i=s;i<=t;++i)   #define drep(i,s,t) for(int i=t;i>=s;--i)      #define DEBUG   #define d1(x) std::cout << #x   =   << (x) << std::endl   #define d2(x, y) std::cout << #x   =   << (x) <<   ,  #y   =   << (y) << std::endl   #define disp(arry, fr, to)        {            std::cout << #arry   :  ;            for(int _i = fr; _i <= to; _i++) std::cout << arry[_i] <<    ;            std::cout << std::endl;        }      using namespace std;   typedef long long ll;   typedef pair<int,int> pii;   const int Maxn = 2e5+10;   const int Inf = 0x7f7f7f7f;   const ll Inf_ll = 1ll*Inf*Inf;   const int Mod = 1e9+7;   const double eps = 1e-7;      int n, a[Maxn], tol;   ll Sum, sum[Maxn], s[Maxn];      int calc(int l, int r, ll x){       // if( l > r )  return 0;       int ll = l, rr = r, ans = l-1;       while( ll <= rr )       {           int mid = (ll+rr) >> 1;           if( s[mid] - s[l-1] <= x )  ll = mid+1, ans = mid;           else rr = mid-1;       }       // printf( l = %d, r = %d, x = %lld, ans = %d n , l, r, x, ans-l+1 + (x>=0));       return ans-l+1 + (x>=0);   }      ll work(){       tol = 0;       ll ans = 0, las = 0;       for(int i=1;i<=n;i+=2)       {              s[++tol] = las + a[i];           las += a[i];       }       for(int i=1,j=1;i<=n;i+=2,j++)       {           if( n&1 )           {               ll tmp = (Sum-1)/2 - sum[i];               ans += calc(j+1, tol-1, tmp);               if( i+1 < n )                   ans += calc(j+1, tol-1, tmp-a[n]);               if( i != 1 )               {                   ans += calc(j+1, tol-1, tmp+a[1]);                   if( i+1 < n )                       ans += calc(j+1, tol-1, tmp-a[n]+a[1]);               }           }           else            {               ll tmp = (Sum-1)/2 - sum[i];               ans += calc(j+1, tol, tmp);               if( i+1 < n )                   ans += calc(j+1, tol-1, tmp-a[n]);               if( i != 1 )               {                   ans += calc(j+1, tol, tmp+a[1]);                   if( i+1 < n )                       ans += calc(j+1, tol-1, tmp-a[n]+a[1]);               }           }           // d2(i, ans);       }       las = tol = 0;          for(int i=2;i<=n;i+=2)       {           s[++tol] = las + a[i];           las += a[i];       }       for(int i=2,j=1;i<=n;i+=2,j++)       {           if( n&1 )           {               ll tmp = (Sum-1)/2 - sum[i];               ans += calc(j+1, tol, tmp);               if( i+1 < n )                   ans += calc(j+1, tol-1, tmp-a[n]);               if( i != 1 )               {                   ans += calc(j+1, tol, tmp+a[1]);                   if( i+1 < n )                       ans += calc(j+1, tol-1, tmp-a[n]+a[1]);               }           }           else            {               ll tmp = (Sum-1)/2 - sum[i];               ans += calc(j+1, tol-1, tmp);               if( i+1 < n )                   ans += calc(j+1, tol-1, tmp-a[n]);               if( i != 1 )               {                   ans += calc(j+1, tol-1, tmp+a[1]);                   if( i+1 < n )                       ans += calc(j+1, tol-1, tmp-a[n]+a[1]);               }           }           // d2(i, ans);       }       las = 0;       for(int i=n;i>=3;i--)       {           las += a[i];           if( las*2 >= Sum )  break;           ans++;       }       return ans + 1;   }      void solve(){       scanf( %d ,&n);       Sum = 0;       for(int i=1;i<=n;i++)       {           scanf( %d ,&a[i]);           Sum += a[i];           sum[i] = sum[i-1] + a[i];       }       printf( %lld n , work()%998244353);   }         int main(){       int T;       scanf( %d ,&T);       for(int _=1;_<=T;_++)       {           solve();       }       return 0;   }


#include<iostream>   #include<map>   #include<cstring>   #include<cmath>   #include<algorithm>   #include <queue>   #include<map>   using namespace std;   typedef long long ll;   inline ll gcd(ll a,ll b) {       return b>0 ? gcd(b,a%b):a;   }   int main()   {    ll T,m1,m,k,a,b;    cin>>T;    while(T--){     int a[30]={0},n;     cin>>n;     string str;     cin>>str;     int num=-1,f=1;     for(int i=0;i<str.length();i++){      int tmp=str[i]- A ;      if(tmp!=num)      {       if(a[tmp])       f=0;       else       {        num=tmp;        a[tmp]++;       }       }     }     if(f)     cout<< YES n ;     else     cout<< NO n ;    }    }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, c, ans[4010];     scanf( %d %d %d %d , &n, &a, &b, &c);     memset(ans, -1, sizeof(ans));     ans[0] = 0;     for (int i = 0; i <= n; i++) {       if (i >= a && ans[i - a] != -1) ans[i] = ans[i - a] + 1;       if (i >= b && ans[i - b] != -1) ans[i] = max(ans[i], ans[i - b] + 1);       if (i >= c && ans[i - c] != -1) ans[i] = max(ans[i], ans[i - c] + 1);     }     printf( %d n , ans[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int M = 1e18 + 7;   const long long int mod = 1e9 + 7;   const long long int infi = LLONG_MAX;   long long int i, j, ans, k, n, x, y, m, mymax = LLONG_MIN, mymin = LLONG_MAX, b,                                           c, z, sum;   int main() {     long long int x1, x2, s, p, t1, t2, d;     scanf( %lld %lld %lld , &s, &x1, &x2);     scanf( %lld %lld , &t1, &t2);     scanf( %lld %lld , &p, &d);     if (x2 < x1) {       ans = (x1 - x2) * t2;       if (d == 1) {         if (p <= x2) {           ans = min(ans, (2 * s - x2 - p) * t1);         } else if (p > x2 && p <= x1) {           ans = min(ans, (2 * s - x2 - p) * t1);         } else {           ans = min(ans, (2 * s - p - x2) * t1);         }       } else {         if (p < x2) {           ans = min(ans, (2 * s - x2 + p) * t1);         } else if (p >= x2 && p < x1) {           ans = min(ans, (2 * s - x2 + p) * t1);         } else {           ans = min(ans, (p - x2) * t1);         }       }       printf( %lld n , ans);       return 0;     }     ans = (x2 - x1) * t2;     if (d == 1) {       if (p <= x1) {         ans = min(ans, (x2 - p) * t1);       } else if (p > x1 && p <= x2) {         ans = min(ans, (2 * s + x2 - p) * t1);       } else {         ans = min(ans, (2 * s - p + x2) * t1);       }     } else {       if (p < min(x1, x2)) {         ans = min(ans, (x2 + p) * t1);       } else if (p >= x1 && p <= x2) {         ans = min(ans, (p + x2) * t1);       } else {         ans = min(ans, (p + x2) * t1);       }     }     printf( %lld n , ans);     return 0;   }


#include <iostream>   #include <algorithm>   #include <vector>   #include <string>   #include <set>   // #include <stdlib.h>   // #include <stdio.h>   // #include <ctype.h>      using namespace std;      int main(void){          int n = 0;       cin >> n;       for(; n > 0; n--){           int num;           cin >> num;           int input, last = -1;           int count = 1, max = 1;           if(num == 0){               cout <<  0  << endl;               continue;           }           for(; num > 0; num--){               cin >> input;               if(input == last){                   count++;                   if(count > max){                       max = count;                   }               }               else{                   count = 1;               }               last = input;           }           cout << max << endl;                     }             return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;      int MOD;   struct modint {      private:          int v;          static int minv(int a, int m) {           a %= m;           assert(a);           return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);       }      public:          modint() : v(0) {}       modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }       explicit operator int() const { return v; }       friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }       friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }          friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }       friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }          modint inv() const {           modint res;           res.v = minv(v, MOD);           return res;       }       friend modint inv(const modint& m) { return m.inv(); }       modint neg() const {           modint res;           res.v = v ? MOD-v : 0;           return res;       }       friend modint neg(const modint& m) { return m.neg(); }          modint operator- () const {           return neg();       }       modint operator+ () const {           return modint(*this);       }          modint& operator ++ () {           v ++;           if (v == MOD) v = 0;           return *this;       }       modint& operator -- () {           if (v == 0) v = MOD;           v --;           return *this;       }       modint& operator += (const modint& o) {           v += o.v;           if (v >= MOD) v -= MOD;           return *this;       }       modint& operator -= (const modint& o) {           v -= o.v;           if (v < 0) v += MOD;           return *this;       }       modint& operator *= (const modint& o) {           v = int(ll(v) * ll(o.v) % MOD);           return *this;       }       modint& operator /= (const modint& o) {           return *this *= o.inv();       }          friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }       friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }       friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }       friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }       friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }       friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }   };   namespace IO {       template<class T>       void _R(T &x) { cin >> x; }       void _R(int &x) { scanf( %d , &x); }       void _R(ll &x) { scanf( %lld , &x); }       void _R(double &x) { scanf( %lf , &x); }       void _R(char &x) { x = getchar(); }       void _R(char *x) { scanf( %s , x); }       void R() {}       template<class T, class... U>       void R(T &head, U &... tail) { _R(head), R(tail...); }       template<class T>       void _W(const T &x) { cout << x; }       void _W(const int &x) { printf( %d , x); }       void _W(const ll &x) { printf( %lld , x); }       void _W(const double &x) { printf( %.16f , x); }       void _W(const char &x) { putchar(x); }       void _W(const char *x) { printf( %s , x); }       template<class T, class U>       void _W(const pair<T, U> &x) { _W(x.first), putchar(   ), _W(x.second); }       template<class T>       void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(   ); }       void W() {}       template<class T, class... U>       void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ?     :   n ), W(tail...); }   }   using namespace IO;   template <typename T> T pow(T a, long long b) {       assert(b >= 0);       T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;   }      const int maxn = 2e5+50;   const int maxp = maxn*32+5;   const int K = 20;   const int M = 1 << K;   int ls[maxp], rs[maxp], tot;   // 0, 1, cnt, XOR   int t0[maxp], t1[maxp], p[maxp], txor[maxp], tor[maxp];      inline void pushup(int x) {       p[x] = p[ls[x]] + p[rs[x]];       t0[x] = t0[ls[x]] | t0[rs[x]];       t1[x] = t1[ls[x]] | t1[rs[x]];   }   inline void XOR(int x, int t) {       if (!x) return;       txor[x] ^= t;       if (~tor[x] && t >> tor[x] & 1) swap(ls[x], rs[x]);       int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);       t0[x] = a, t1[x] = b;   }   inline void pushdown(int x) {       if (txor[x]) {           XOR(ls[x], txor[x]), XOR(rs[x], txor[x]);           txor[x] = 0;       }   }   inline void insert(int &x, int s, int k) {       if (!x) x = ++tot;       tor[x] = k;       if (k == -1) {           t1[x] = s, t0[x] = s ^ (M - 1);           p[x] = 1;           return;       }       insert((s >> k & 1) ? rs[x] : ls[x], s, k - 1);       pushup(x);   }   inline void split(int &x, int &y, int l, int r, int le, int re) {       if (!x || re < l || le > r) {           y = 0;           return;       }       if (le <= l && r <= re) {           y = x;           x = 0;           return;       }       int mid = l + r >> 1; pushdown(x);       tor[y = ++tot] = tor[x];       split(ls[x], ls[y], l, mid, le, re);       split(rs[x], rs[y], mid + 1, r, le, re);       pushup(x), pushup(y);   }   inline void merge(int &x, int y) {       if (!x || !y) {           x = x | y;           return;       }       pushdown(x), pushdown(y);       merge(ls[x], ls[y]), merge(rs[x], rs[y]);       if (~tor[x]) pushup(x);   }   inline void OR(int x, int s) {       if (!x) return;       if (!(s & t0[x] & t1[x])) {           XOR(x, s & t0[x]);           return;       }       pushdown(x);       if (s >> tor[x] & 1) XOR(ls[x], 1 << tor[x]), merge(rs[x], ls[x]), ls[x] = 0;       OR(ls[x], s), OR(rs[x], s);       pushup(x);   }      int main() {       int n, q, x = 0;       R(n, q);       for (int i = 1; i <= n; ++i) {           int v; R(v); insert(x, v, K - 1);       }       for (; q; --q) {           int oth;           int t, l, r, v; R(t, l, r);           split(x, oth, 0, M - 1, l, r);           if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);           else if (t == 2) R(v),OR(oth, v);           else if (t == 3) R(v), XOR(oth, v);           else W(p[oth]);           merge(x, oth);       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 20;   const int maxn = (1 << maxN) + 20;   int t[maxn], r[maxn][maxN + 3], x[maxn][maxN + 3];   string s[maxN];   void cal(int n, int k) {     if (!n) return;     cal(n - 1, k);     cal(n - 1, k + (1 << (n - 1)));     for (int i = 0; i < (1 << n); i++)       for (int j = 0; j <= n; j++) x[k + i][j] = r[k + i][j];     for (int i = 0; i < (1 << (n - 1)); i++)       for (int j = 1; j <= n; j++) {         r[k + i][j] += x[k + i + (1 << (n - 1))][j - 1];         r[k + i + (1 << (n - 1))][j] += x[k + i][j - 1];       }   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) t[i] = (t[i] * 2 + (s[j][i] ==  1 ));       r[t[i]][0]++;     }     cal(n, 0);     int ans = 1e9;     for (int mask = 0; mask < (1 << n); mask++) {       int res = 0;       for (int j = 0; j <= n; j++) res += r[mask][j] * min(j, n - j);       ans = min(ans, res);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, bar;     vector<int> choco;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> bar;       choco.push_back(bar);     }     if (n == 1)       cout <<  1 0 n ;     else {       auto l = choco.begin();       auto r = choco.end() - 1;       int diff = 0;       int l_count = 0;       int r_count = 0;       while (true) {         if ((l + 1) == r) {           ++l_count;           ++r_count;           break;         }         diff = (*l) - (*r);         if (diff < 0) {           (*r) -= (*l);           ++l_count;           ++l;         } else if (diff > 0) {           (*l) -= (*r);           ++r_count;           --r;         } else {           ++l_count;           (*l) = 0;           if ((l + 2) == r)             ++l;           else {             ++r_count;             (*r) = 0;             ++l;             --r;           }         }       }       cout << l_count <<     << r_count <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 0x3f3f3f3f;   int f[1 << 23], pre[1 << 23], zt[1 << 23], tot, ans[23];   int n, fn[23], fri[23][23];   int tans[23], Ans[23][23];   void Solve(int o) {     int ni, st = 1 << (o - 1), ed = (1 << n) - 1;     for (int i = (1); i <= (int)(fn[o]); ++i) st |= 1 << (fri[o][i] - 1);     memset(f, oo, sizeof(f));     f[st] = 1;     for (int i = (st); i <= (int)(ed); ++i) {       if (f[i] == oo) continue;       for (int j = (1); j <= (int)(n); ++j)         if (i & (1 << (j - 1))) {           ni = i;           for (int k = (1); k <= (int)(fn[j]); ++k) ni |= 1 << (fri[j][k] - 1);           if (f[ni] > f[i] + 1) {             f[ni] = f[i] + 1;             pre[ni] = i;             zt[ni] = j;           }         }     }     int now = ed, tp = 0;     while (now != st) {       ans[++tp] = zt[now];       now = pre[now];     }     Ans[o][tans[o] = 1] = o;     while (tp) Ans[o][++tans[o]] = ans[tp--];   }   int main() {     int m, u, v;     scanf( %d%d , &n, &m);     if (m == n * (n - 1) / 2) {       puts( 0 );       puts(  );       return 0;     }     int mx = 0;     for (int i = (1); i <= (int)(m); ++i) {       scanf( %d%d , &u, &v);       fri[u][++fn[u]] = v;       fri[v][++fn[v]] = u;       mx = max(mx, max(fn[u], fn[v]));     }     int mn = n + 1, id;     for (int i = (1); i <= (int)(n); ++i) {       Solve(i);       if (fn[i] < mx - 6) continue;       if (tans[i] < mn) mn = tans[i], id = i;     }     printf( %d n , tans[id]);     for (int i = (1); i <= (int)(tans[id]); ++i)       printf( %d%c , Ans[id][i], i == tans[id] ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, q;   long long int gcd(long long int t, long long int u) {     if (u == 0) {       return t;     }     return gcd(u, t % u);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> q;     long long int gcd_nm = gcd(n, m);     long long int a, b;     a = n / gcd_nm;     b = m / gcd_nm;     for (int i = 1; i <= q; i++) {       long long int sx, sy, ex, ey;       cin >> sx >> sy >> ex >> ey;       sy--;       ey--;       if (sx == 1) {         sy /= a;       } else {         sy /= b;       }       if (ex == 1) {         ey /= a;       } else {         ey /= b;       }       if (sy == ey) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int a, b, r;     scanf( %d %d %d , &a, &b, &r);     r *= 2;     if (r > a || r > b) {       printf( Second n );       return 0;     }     printf( First n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 105;   long long int r[sz], a[sz][sz], b[sz][sz], ans[sz][sz], m;   void multiply(long long int a[][sz], long long int b[][sz]) {     for (int i = 0; i < m; i++) {       for (int j = 0; j < m; j++) {         ans[i][j] = 0;         for (int k = 0; k < m; k++) {           ans[i][j] += a[i][k] * b[k][j] % 1000000007;           ans[i][j] %= 1000000007;         }       }     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < m; j++) {         a[i][j] = ans[i][j];       }     }   }   void power(long long int a[][sz], long long int n) {     if (n <= 1) {       return;     }     power(a, n / 2);     multiply(a, a);     if (n % 2 == 1) {       multiply(a, b);     }   }   int main() {     long long int n;     cin >> n >> m;     r[0] = 1;     for (int i = 1; i <= m; i++) {       r[i] = r[i - 1];       if (i - m >= 0) {         r[i] += r[i - m];       }       r[i] %= 1000000007;     }     for (int i = 1; i < m; i++) {       a[i][i - 1] = b[i][i - 1] = 1;     }     a[0][m - 1] = b[0][m - 1] = 1;     a[m - 1][m - 1] = b[m - 1][m - 1] = 1;     power(a, n);     long long int res = 0;     for (int k = 0; k < m; k++) {       res += r[k] * a[k][0] % 1000000007;       res %= 1000000007;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modexpo(long long x, long long p) {     long long res = 1;     x = x % 1000000007;     while (p) {       if (p % 2) res = res * x;       p >>= 1;       x = x * x % 1000000007;       res %= 1000000007;     }     return res;   }   long long max(long long a, long long b) { return (a > b ? a : b); }   long long min(long long a, long long b) { return (a < b ? a : b); }   struct edge {     long long u, v, w;     edge(long long a, long long b, long long c) {       u = a;       v = b;       w = c;     }   };   struct compare {     bool operator()(const edge a, const edge b) const {       if (a.u == b.u) {         if (a.v == b.v) {           return a.w < b.w;         }         return a.v < b.v;       }       return a.u < b.u;     }   };   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       long long l, r, d;       cin >> l >> r >> d;       if (d < l) {         cout << d <<   n ;       } else if (r % d == 0) {         cout << r + d <<   n ;       } else {         long long x = ceil((long double)r / (long double)d);         cout << x * d <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int Comb[2015][2015], Fact[2015], used[2015], a[2015];   int main() {     cin.sync_with_stdio(false);     int n, m = 0, k = 0;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       if (a[i] != -1) used[a[i]] = 1;     }     Fact[0] = 1;     for (int i = 1; i <= n; ++i) {       if (used[i] == 0) {         ++m;         if (a[i] == -1) ++k;       }       Fact[i] = 1LL * Fact[i - 1] * i % MOD;     }     for (int i = 0; i <= n; ++i) {       Comb[i][0] = 1;       for (int j = 1; j <= i; ++j)         Comb[i][j] = (Comb[i - 1][j] + Comb[i - 1][j - 1]) % MOD;     }     int sol = 0;     for (int i = 1; i <= k; ++i) {       int x = (1LL * Comb[k][i] * Fact[m - i]) % MOD;       if (i & 1)         sol = (sol + x) % MOD;       else         sol = (0LL + sol + MOD - x) % MOD;     }     cout << (0LL + Fact[m] + MOD - sol) % MOD;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int r, g, b;     cin >> r >> g >> b;     int ans = 0;     ans = r / 3 + g / 3 + b / 3;     if (min(r, min(g, b)) >= 1)       ans = max(ans, (r - 1) / 3 + (g - 1) / 3 + (b - 1) / 3 + 1);     if (min(r, min(g, b)) >= 2)       ans = max(ans, (r - 2) / 3 + (g - 2) / 3 + (b - 2) / 3 + 2);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long double pi{3.1415926535897932384};   constexpr long long mod{1000000007};   long long power(long long x, long long y) {     long long res = 1;     x = x % mod;     while (y > 0) {       if (y & 1) res = (res * x) % mod;       y = y >> 1;       x = (x * x) % mod;     }     return res;   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }   pair<long long, long long> v[3001];   long long n, dp[3001][3001];   const long long INF = 1000000000000000;   long long getAns(long long pos, long long pre) {     if (pos > n) return 0;     if (dp[pos][pre] != -INF) return dp[pos][pre];     long long x = v[pos].second + getAns(pos + 1, pos);     long long y = v[pos].first - v[pre].first + getAns(pos + 1, pre);     return dp[pos][pre] = min(x, y);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> v[i].first >> v[i].second;     }     sort(v + 1, v + n + 1);     for (long long i = 0; i <= n; i++) {       for (long long j = 0; j <= n; j++) {         dp[i][j] = -INF;       }     }     long long ans = getAns(2, 1) + v[1].second;     cout << ans <<   n ;     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long   #define endl   n    const unsigned int mod = 1000000007;               int main(){        ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);        bool prime[1000001];    memset(prime, true, sizeof(prime));         for(int p=2; p*p<=1000000; p++) {            // If prime[p] is not changed, then it is a prime            if (prime[p] == true)            {                  for (int i=p*p; i<=1000000; i += p)                    prime[i] = false;            }    }           ll t;     cin>>t;     while(t--){       int a;       cin>>a;       int ans = 1;       for(int i=a+1;i<1000001;i++){         if (prime[i]){           ans*=i;break;         }       }       for(int i=ans+a;i<1000001;i++){         if (prime[i]){           ans*=i;break;         }       }       cout<<ans<<endl;     }           return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void print_container(ostream& os, const T& c) {     const char* _s =    ;     if (!c.empty()) {       __typeof__(c.begin()) last = --c.end();       for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) {         os << *it;         if (it != last) os << _s;       }     }   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& c) {     print_container(os, c);     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& c) {     print_container(os, c);     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const multiset<T>& c) {     print_container(os, c);     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const deque<T>& c) {     print_container(os, c);     return os;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, const map<T, U>& c) {     print_container(os, c);     return os;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, const pair<T, U>& p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   template <typename T>   void print(T a, int n, const string& split =    ) {     for (int i = 0; i < n; i++) {       cout << a[i];       if (i + 1 != n) cout << split;     }     cout << endl;   }   template <typename T>   void print2d(T a, int w, int h, int width = -1, int br = 0) {     for (int i = 0; i < h; ++i) {       for (int j = 0; j < w; ++j) {         if (width != -1) cout.width(width);         cout << a[i][j] <<    ;       }       cout << endl;     }     while (br--) cout << endl;   }   template <typename T>   void input(T& a, int n) {     for (int i = 0; i < n; ++i) cin >> a[i];   }   template <typename T, typename U>   void upmin(T& a, const U& b) {     a = min<T>(a, b);   }   template <typename T, typename U>   void upmax(T& a, const U& b) {     a = max<T>(a, b);   }   template <typename T>   void uniq(T& a) {     sort(a.begin(), a.end());     a.erase(unique(a.begin(), a.end()), a.end());   }   template <class T>   string to_s(const T& a) {     ostringstream os;     os << a;     return os.str();   }   template <class T>   T to_T(const string& s) {     istringstream is(s);     T res;     is >> res;     return res;   }   void fast_io() {     cin.tie(0);     ios::sync_with_stdio(false);   }   bool in_rect(int x, int y, int w, int h) {     return 0 <= x && x < w && 0 <= y && y < h;   }   const int dx[] = {0, 1, 0, -1};   const int dy[] = {1, 0, -1, 0};   int main() {     int n;     cin >> n;     int c[6666] = {};     for (int i = 0; i < (int)(n); ++i) {       int a;       cin >> a;       ++c[a];     }     int res = 0;     for (int i = 0; i < (int)(6600); ++i) {       if (c[i] > 1) {         res += c[i] - 1;         c[i + 1] += c[i] - 1;       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const int N = 200500;   void solve() {     long long n, k;     cin >> n >> k;     if ((n + k) % 2 == 0 && n >= k && (n + k) / 2 >= (k * (k + 1)) / 2) {       cout <<  YES n ;     } else       cout <<  NO n ;   }   int main() {     ios::sync_with_stdio(false);     int q = 1;     cin >> q;     for (int i = 1; i <= q; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     int count = 1;     a[0] = 0;     a[1] = 2;     for (int i = 2; i < n; i++) {       count++;       a[i] = a[i - 1] + count;       if (a[i] > n) {         int rem = a[i] - n;         a[i] = a[0] + rem;       }     }     for (int i = 1; i < n; i++) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     scanf( %d , &n);     vector<int> cnt(20, 0);     for (int i = 0; i < n; i++) {       scanf( %d , &x);       for (int b = 0; b < 20; b++) {         if (1 & (x >> b)) cnt[b]++;       }     }     long long ans = 0;     for (int i = 0; i < n; i++) {       long long val = 0;       for (int b = 0; b < 20; b++) {         if (cnt[b] > 0) {           cnt[b]--;           val |= 1 << b;         }       }       ans += val * val;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 3e5 + 10;   long long N, L, R, X, arr[maxn], ans;   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> N >> L >> R >> X;     for (register long long i = 1; i <= N; ++i) {       cin >> arr[i];     }     for (register long long i = 0; i < (1 << N); ++i) {       long long cnt = __builtin_popcount(i);       if (i < 2) continue;       vector<long long> v;       for (register long long j = 0; j < N; ++j) {         if (i & 1 << j) {           v.push_back(arr[j + 1]);         }       }       sort(v.begin(), v.end());       if (v.back() - v.front() < X) {         continue;       }       long long sum = accumulate(v.begin(), v.end(), 0);       if (sum < L || sum > R) {         continue;       }       ++ans;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const char al[100] = { A ,  B ,  C ,  D ,  E ,  F ,  G ,  H ,  I ,                          J ,  K ,  L ,  M ,  N ,  O ,  P ,  Q ,  R ,                          S ,  T ,  U ,  V ,  W ,  X ,  Y ,  Z };   int main() {     long long int n, m;     cin >> n >> m;     vector<long long int> nums(n + 1);     vector<int> op(10000000 + 1, false);     vector<int> prost;     for (int i = 2; i <= 10000000; ++i) {       if (op[i] == 0) {         op[i] = i;         prost.push_back(i);       }       for (int j = 0;            j < (int)prost.size() && prost[j] <= op[i] && i * prost[j] <= 10000000;            ++j)         op[i * prost[j]] = prost[j];     }     map<long long int, long long int> my;     long long int x;     long long int j;     long long int count = 0;     for (long long int i = 0; i < n; i++) {       cin >> x;       nums[i] = x;       j = 0;       if (op[x] == x) {         count++;         my[x]++;       } else {         while (x > 1) {           if (op[x] != x) {             while (x % prost[j] != 0) j++;             x /= prost[j];             my[prost[j]]++;             count++;           } else {             my[x]++;             count++;             x = 0;           }         }       }     }     vector<long long int> second;     for (long long int i = 0; i < m; i++) {       cin >> x;       j = 0;       if (op[x] == x) {         if (my.find(x) != my.end() && my[x] > 0) {           my[x]--;           count--;         } else {           if (second.size() != 0 && second[second.size() - 1] * x <= 10000000)             second[second.size() - 1] *= x;           else             second.push_back(x);         }       } else {         while (x > 1) {           if (op[x] != x) {             while (x % prost[j] != 0) j++;             x /= prost[j];             if (my.find(prost[j]) != my.end() && my[prost[j]] > 0) {               my[prost[j]]--;               count--;             } else {               if (second.size() != 0 &&                   second[second.size() - 1] * prost[j] <= 10000000)                 second[second.size() - 1] *= prost[j];               else                 second.push_back(prost[j]);             }           } else {             if (my.find(x) != my.end() && my[x] > 0) {               my[x]--;               count--;             } else {               if (second.size() != 0 && second[second.size() - 1] * x <= 10000000)                 second[second.size() - 1] *= x;               else                 second.push_back(x);             }             x = 0;           }         }       }     }     long long int prev = 1;     long long int o;     long long int value;     vector<long long int> ans;     vector<pair<int, int> > ssort;     map<long long int, long long int> now;     int pre = 1;     for (int i = 0; i < n; i++) {       pre = 1;       x = nums[i];       j = 0;       while (x > 1) {         if (op[x] == x) {           now[x]++;           if (now[x] <= my[x]) pre *= x;           x = 1;         } else {           while (x % prost[j] != 0) j++;           x /= prost[j];           now[prost[j]]++;           if (now[prost[j]] <= my[prost[j]]) pre *= prost[j];         }       }       ans.push_back(pre);     }     if (second.size() == 0) second.push_back(1);     cout << ans.size() <<     << second.size() << endl;     for (long long int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout << endl;     for (long long int i = 0; i < second.size(); i++) cout << second[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     int n, r1 = 0, r2 = 0;     cin >> n;     vector<int> r(n);     for (int &i : r) cin >> i;     for (int i = 0; i < n; i++) {       int a;       cin >> a;       if (a != r[i]) {         if (a)           r1++;         else           r2++;       }     }     if (!r2)       cout <<  -1 n ;     else if (r2 > r1)       cout <<  1 n ;     else       cout << (int)ceil((r1 + 1.0) / r2) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   char s[45][20] = {        Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe ,        Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler ,        Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan ,        Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield ,        Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley ,        Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge ,        Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy ,        Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan };   int n;   void solve() {     cin >> n;     cout << s[n - 1];   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool setmax(T &_a, T _b) {     if (_b > _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   bool setmin(T &_a, T _b) {     if (_b < _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   T gcd(T _a, T _b) {     return _b == 0 ? _a : gcd(_b, _a % _b);   }   int a[110][4], n, m;   int main() {     cin >> n >> m;     int cur = 1;     memset(a, 0, sizeof(a));     for (int i = int(1); i <= int(n); ++i) {       if (cur > m) break;       a[i][0] = cur++;       if (cur > m) break;       a[i][3] = cur++;     }     for (int i = int(1); i <= int(n); ++i) {       if (cur > m) break;       a[i][1] = cur++;       if (cur > m) break;       a[i][2] = cur++;     }     vector<int> ans;     for (int i = int(1); i <= int(n); ++i) {       ans.push_back(a[i][1]);       ans.push_back(a[i][0]);       ans.push_back(a[i][2]);       ans.push_back(a[i][3]);     }     for (int x : ans)       if (x > 0) cout << x <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int OO = 2e7;   const double EPS = (1e-7);   int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }   bool sort_p(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     int n;     cin >> n;     vector<int> a(n);     vector<int> b(n, 0);     vector<bool> s(n + 1);     vector<int> t(n + 1);     bool k = false, q = false;     for (int i = 0; i < (int)(n); ++i) {       cin >> a[i];       if (a[i] == 0) k = true;       if (a[i] != 0 && k) q = true;       s[n - a[i]] = true;       t[a[i]]++;     }     if (k) {       for (int i = 0; i < (int)(n); ++i)         if (a[i] != 0) {           cout <<  Impossible ;           return 0;         }     }     if (k) {       cout <<  Possible n ;       for (int i = 0; i < (int)(n); ++i) cout << 1 <<    ;       return 0;     }     long long int sum = 0;     for (int i = 0; i < (int)(n); ++i) {       sum = t[i];       if (n - i != 0 && t[i] != 0 && sum % (n - i)) {         cout <<  Impossible ;         return 0;       }     }     vector<pair<int, int>> p;     for (int i = 0; i < (int)(n); ++i) {       pair<int, int> r = make_pair(n - a[i], i);       p.push_back(r);     }     sort(((p).begin()), ((p).end()), sort_p);     int l = 1;     int j = 0;     for (int i = 0; i < ((int)((p).size())); ++i) {       for (j = 0; j < p[i].first; j++) {         b[p[i + j].second] = l;       }       i = i + j - 1;       l++;     }     if (sum > n) {       cout <<  Impossible ;       return 0;     }     cout <<  Possible n ;     for (int i = 0; i < (int)(n); ++i) cout << b[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   int n, m;   bool used[MAXN];   set<int> graph[MAXN];   stack<int> path;   vector<int> cycle;   int fake;   vector<pair<int, int> > edges;   vector<pair<int, int> > answer;   void EulerCycle(int x) {     cycle.clear();     edges.clear();     path.push(x);     while (path.empty() == false) {       used[x] = true;       if (graph[x].empty() == false) {         path.push(*graph[x].begin());         int y = *graph[x].begin();         graph[x].erase(y);         graph[y].erase(x);         x = y;       } else {         cycle.push_back(x);         path.pop();         if (path.empty() == false) x = path.top();       }     }     for (int i = 0; i < cycle.size() - 1; i++) {       edges.push_back({cycle[i], cycle[i + 1]});     }   }   bool isFake(pair<int, int> x) {     if (x.first == fake || x.second == fake) return true;     return false;   }   void solve(int x) {     EulerCycle(x);     for (int i = 0; i < edges.size(); i++) {       bool toDelete;       if (isFake(edges[i]) == true) {         continue;       }       if (i % 2 != 0)         toDelete = true;       else         toDelete = false;       if (i != 0 && isFake(edges[i - 1]) == true) toDelete = false;       if (i == 0 && isFake(edges[edges.size() - 1]) == true) toDelete = false;       if (i != edges.size() - 1 && isFake(edges[i + 1]) == true) toDelete = false;       if (i == edges.size() - 1 && isFake(edges[0]) == true) toDelete = false;       if (toDelete == false) {         answer.push_back(edges[i]);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       graph[u].insert(v);       graph[v].insert(u);     }     fake = -1;     for (int i = 1; i <= n; i++) {       if (graph[i].size() % 2 != 0) {         fake = n + 1;         graph[i].insert(fake);         graph[fake].insert(i);       }     }     for (int x = 1; x <= n + 1; x++) {       if (used[x] == false) {         solve(x);       }     }     cout << answer.size() <<   n ;     for (int i = 0; i < answer.size(); i++) {       cout << answer[i].first <<     << answer[i].second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int a, b, maxa;   void use_battery(int s) {     if (s == 1) a = min(a + 1, maxa);     --b;   }   void use_accum() { --a; }   int main() {     int ans = 0;     int n;     cin >> n >> b >> a;     maxa = a;     vector<int> s(n);     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = 0; i < n; i++) {       if (a == 0 && b == 0)         break;       else if (a == 0)         use_battery(s[i]);       else if (b == 0)         use_accum();       else if (s[i] == 1 && a < maxa)         use_battery(s[i]);       else         use_accum();       ans++;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k;     cin >> k;     int a[k];     for (auto &it : a) cin >> it;     sort(a, a + k, greater<int>());     int declined = max(a[0], 25) - 25;     cout << declined;   }


#include <bits/stdc++.h>   using namespace std;   int d[1000001] = {0};   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0, a, b; i < m; ++i) scanf( %d%d , &a, &b), d[a]++, d[b]++;     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long de = d[i], ade = n - 1 - de;       ans += de * (de - 1) + ade * (ade - 1) - de * ade;     }     cout << ans / 6 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[200];   int n, x;   vector<pair<int, int> > res;   int main() {     int sum = 0;     cin >> n;     cin >> a[0].first;     a[0].second = 0;     sum += a[0].first;     for (int i = 1; i < n; i++) {       cin >> a[i].first;       a[i].second = i;       sum += a[i].first;     }     if (a[0].first == 0 || sum < n - 1) {       cout << -1;       return 0;     }     sort(a + 1, a + n);     reverse(a + 1, a + n);     int x = 0;     int y = 1;     cout << n - 1 << endl;     while (y < n) {       cout << a[x].second + 1 <<     << a[y].second + 1 << endl;       y++;       a[x].first--;       if (a[x].first == 0) x++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int m, x, y, a, b;       cin >> x >> y >> a >> b;       m = abs(x - a) + abs(y - b);       if (x != a && y != b) m = m + 2;       cout << m <<   n ;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( 03 )   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   template <class T, class T2>   T _f_pow(T a, T2 b) {     T res = 1LL;     while (b > 0) {       if (b & 1) res = (res * a);       a = (a * a);       b >>= 1;     }     return (res);   }   template <class T>   T gcd(T a, T b) {     while (a && b)       if (a > b)         (a %= b);       else         (b %= a);     return a + b;   }   template <class T>   T lcm(T a, T b) {     return (a * b) / gcd(a, b);   }   int n, m;   long long par[(int)2e6 + 5], sz[(int)2e6 + 5], cmp, mxComp;   void init() {     iota(par, par + n + 1, 0);     fill(sz, sz + n + 1, 1);     cmp = n;     mxComp = 1;   }   int Find(int u) { return u == par[u] ? u : par[u] = Find(par[u]); }   int join(int u, int v) {     u = Find(u);     v = Find(v);     if (u == v) return 0;     if (sz[u] > sz[v]) swap(u, v);     par[u] = v;     sz[v] += sz[u];     mxComp = max(mxComp, sz[v]);     cmp--;     return 1;   }   int from[(int)2e6 + 5], to[(int)2e6 + 5], cst[(int)2e6 + 5],       sorted[(int)2e6 + 5];   int f, s;   int krus() {     init();     sort(sorted, sorted + m, [](int a, int b) { return cst[a] < cst[b]; });     int ret = 0;     for (int k = 0; k < m; k++) {       int e = sorted[k];       if (join(from[e], to[e])) {         if (Find(f) == Find(s)) {           cout << cst[e] << endl;           return 0;         }         ret += cst[e];       }     }     return 1;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     cin >> n >> m;     int tm;     m--;     cin >> f >> s >> tm;     for (int i = 0, _n = (m); i < _n; i++) {       cin >> from[i] >> to[i] >> cst[i];       sorted[i] = i;     }     if (krus()) cout << ((int)1e9) << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int M, N, d;     scanf( %d %d , &M, &N);     d = (M * N) / 2;     printf( %d , d);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 1000000;   const int OPAND = 2, OPOR = 3, OPXOR = 4, OPNOT = 5;   int n;   int op[MAXN];   vector<int> ch[MAXN];   int res[MAXN];   int ans[MAXN];   int dfsres(int at) {     if (op[at] == 0 || op[at] == 1) {       res[at] = op[at];     } else if (op[at] == OPNOT) {       res[at] = 1 - dfsres(ch[at][0]);     } else if (op[at] == OPAND) {       int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]);       res[at] = a == 1 && b == 1 ? 1 : 0;     } else if (op[at] == OPOR) {       int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]);       res[at] = a == 1 || b == 1 ? 1 : 0;     } else if (op[at] == OPXOR) {       int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]);       res[at] = (a == 1) ^ (b == 1) ? 1 : 0;     } else {       assert(false);     }     return res[at];   }   void dfsans(int at, int fix, bool neg) {     if (op[at] == 0 || op[at] == 1) {       ans[at] = fix != -1 ? fix : 1 - op[at];       if (fix == -1 && neg) ans[at] = 1 - ans[at];     } else if (op[at] == OPNOT) {       dfsans(ch[at][0], fix, !neg);     } else if (op[at] == OPAND) {       for (int i = (0); i < (2); ++i)         dfsans(ch[at][i],                fix != -1                 ? fix                : res[ch[at][1 - i]] == 0 ? (neg ? 1 : 0)                                          : -1,                neg);     } else if (op[at] == OPOR) {       for (int i = (0); i < (2); ++i)         dfsans(ch[at][i],                fix != -1                 ? fix                : res[ch[at][1 - i]] == 1 ? (neg ? 0 : 1)                                          : -1,                neg);     } else if (op[at] == OPXOR) {       for (int i = (0); i < (2); ++i)         dfsans(ch[at][i], fix != -1 ? fix : -1,                res[ch[at][1 - i]] == 1 ? !neg : neg);     } else {       assert(false);     }   }   void run() {     scanf( %d , &n);     for (int i = (0); i < (n); ++i) {       char buff[10];       scanf( %s , buff);       if (strcmp(buff,  AND ) == 0) {         int a, b;         scanf( %d%d , &a, &b);         --a, --b;         op[i] = OPAND;         ch[i].push_back(a);         ch[i].push_back(b);       } else if (strcmp(buff,  OR ) == 0) {         int a, b;         scanf( %d%d , &a, &b);         --a, --b;         op[i] = OPOR;         ch[i].push_back(a);         ch[i].push_back(b);       } else if (strcmp(buff,  XOR ) == 0) {         int a, b;         scanf( %d%d , &a, &b);         --a, --b;         op[i] = OPXOR;         ch[i].push_back(a);         ch[i].push_back(b);       } else if (strcmp(buff,  NOT ) == 0) {         int a;         scanf( %d , &a);         --a;         op[i] = OPNOT;         ch[i].push_back(a);       } else if (strcmp(buff,  IN ) == 0) {         int val;         scanf( %d , &val);         op[i] = val;       } else {         printf( err: %s n , buff);         assert(false);       }     }     dfsres(0);     dfsans(0, -1, false);     for (int i = (0); i < (n); ++i)       if (op[i] == 0 || op[i] == 1) printf( %d , ans[i]);     puts(  );   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long tab[12], pr[12];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     long long n, m, w, x;     for (int z = 0; z < q; z++) {       cin >> n >> m;       x = 1;       for (long long i = 1; i <= 11; i++) {         tab[i] = (tab[i - 1] + (m % 10)) % 10;         pr[i] = pr[i - 1] + tab[i];         if (tab[i] == tab[1] && i > 1) {           x = i - 1;           break;         }       }       w = pr[x] * (n / (x * m));       n %= x * m;       w += pr[n / m];       cout << w <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ar[30005];   int diff[30005];   int main() {     int t;     cin >> t;     while (t--) {       string ans =  YES ;       int n;       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> ar[i];         diff[i] = 0;       }       for (int i = 2; i <= n; i++) {         if (ar[i] < ar[i - 1]) {           diff[1] += ar[i - 1] - ar[i];           diff[i] -= ar[i - 1] - ar[i];         }       }       for (int i = 1; i <= n; i++) {         diff[i] += diff[i - 1];         ar[i] -= diff[i];         if (ar[i] < 0 || (i > 1 && ar[i] < ar[i - 1])) {           ans =  NO ;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = 0;       c = getchar();     }     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return f ? x : -x;   }   long long n, m, ans, s = 1, num;   bool exia[20][20], exib[20][20], s1[20][20], s2[20][20];   signed main() {     n = read(), m = read();     for (long long i = 1; i <= n; i++) {       long long x = read(), y = read();       exia[x][y] = exia[y][x] = 1;     }     for (long long i = 1; i <= m; i++) {       long long x = read(), y = read();       exib[x][y] = exib[y][x] = 1;     }     for (long long i = 1; i <= 9; i++) {       for (long long j = 1; j <= 9; j++) {         if (i == j) continue;         for (long long k = 1; k <= 9; k++) {           if (i == k || j == k) continue;           if (exia[i][j] && exib[i][k]) {             s1[i][j] = s2[i][k] = 1;             ans = i;             if (num && num != i) s = 0;             num = i;           }         }       }     }     if (s)       printf( %lld , ans);     else {       ans = 0;       for (long long i = 1; i <= 9; i++) {         for (long long j = 1; j <= 9; j++) {           if (i == j) continue;           if (s1[i][j] && s1[j][i]) {             ans = -1;             break;           }           if (s2[i][j] && s2[j][i]) {             ans = -1;             break;           }         }       }       printf( %lld , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modex(long long a, long long p, long long m) {     if (!p) return 1;     if (p & 1) return a * modex(a, p - 1, m) % m;     long long v = modex(a, p >> 1, m);     return v * v % m;   }   namespace fft {   struct cpx {     double r, i;     cpx(double r = 0, double i = 0) : r(r), i(i) {}     cpx operator+(cpx b) { return {r + b.r, i + b.i}; }     cpx operator-(cpx b) { return {r - b.r, i - b.i}; }     cpx operator*(cpx b) { return {r * b.r - i * b.i, r * b.i + i * b.r}; }     cpx operator/(double b) { return {r / b, i / b}; }     inline cpx conj() { return {r, -i}; }   };   const double PI = acos(-1);   vector<int> rev(1, 0);   vector<cpx> roots;   int base = 0;   void precompute(int nbase) {     if (nbase <= base) return;     rev.resize(1 << nbase);     for (int i = 0; i < (1 << nbase); ++i) {       rev[i] = rev[i >> 1] >> 1 | ((i & 1) << nbase - 1);     }     roots.resize(1 << nbase);     for (; base < nbase; ++base) {       int len = 1 << base;       double angle = 2 * PI / (1 << (base + 1));       for (int i = 0; i < len; ++i) {         double cur = angle * i;         roots[len + i] = cpx(cos(cur), sin(cur));       }     }   }   void fft(vector<cpx>& a, int n = -1) {     if (n == -1) n = a.size();     assert((n & (n - 1)) == 0);     int zeros = __builtin_ctz(n);     precompute(zeros);     int shift = base - zeros;     for (int i = 0; i < n; ++i) {       if (i < (rev[i] >> shift)) swap(a[i], a[rev[i] >> shift]);     }     for (int len = 1; len < n; len <<= 1)       for (int i = 0; i < n; i += 2 * len)         for (int j = 0; j < len; ++j) {           cpx u = a[i + j], v = a[i + j + len] * roots[len + j];           a[i + j] = u + v;           a[i + j + len] = u - v;         }   }   vector<cpx> fa, fb;   vector<long long> multiply(vector<int> a, vector<int> b) {     if (a.empty() || b.empty()) return {};     int sz = a.size() + b.size() - 1;     int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));     if (n > fa.size()) fa.resize(n);     for (int i = 0; i < n; ++i) {       int x = i < a.size() ? a[i] : 0;       int y = i < b.size() ? b[i] : 0;       fa[i] = cpx(x, y);     }     fft(fa, n);     cpx r(0, -0.5 / n);     for (int i = 0; i <= (n >> 1); ++i) {       int j = (n - i) & (n - 1);       cpx x = (fa[i] + fa[j].conj()) / 2;       cpx y = (fa[i] - fa[j].conj()) * r;       fa[i] = x * y;       if (i != j) fa[j] = fa[i].conj();     }     fft(fa, n);     reverse(fa.begin() + 1, fa.end());     vector<long long> res(sz);     for (int i = 0; i < sz; ++i) {       res[i] = llround(fa[i].r);     }     return res;   }   vector<int> multiply(const vector<int>& a, const vector<int>& b, int m) {     if (a.empty() || b.empty()) return {};     int eq = (a == b);     int sz = a.size() + b.size() - 1;     int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));     if (n > fa.size()) fa.resize(n);     for (int i = 0; i < a.size(); ++i) {       fa[i] = cpx(a[i] & ((1 << 15) - 1), a[i] >> 15);     }     fill(fa.begin() + a.size(), fa.begin() + n, cpx(0, 0));     fft(fa, n);     if (n > fb.size()) fb.resize(n);     if (eq) {       copy(fa.begin(), fa.begin() + n, fb.begin());     } else {       for (int i = 0; i < b.size(); ++i) {         fb[i] = cpx(b[i] & ((1 << 15) - 1), b[i] >> 15);       }       fill(fb.begin() + b.size(), fb.begin() + n, cpx(0, 0));     }     fft(fb, n);     double ratio = 0.5 / n;     cpx r2(0, -0.5);     cpx r3(ratio, 0);     cpx r4(0, -ratio);     cpx r5(0, 1);     for (int i = 0; i <= (n >> 1); ++i) {       int j = (n - i) & (n - 1);       cpx a1 = (fa[i] + fa[j].conj()) / 2;       cpx a2 = (fa[i] - fa[j].conj()) * r2;       cpx b1 = (fb[i] + fb[j].conj()) * r3;       cpx b2 = (fb[i] - fb[j].conj()) * r4;       cpx x = a1 * b1;       cpx y = a2 * b2;       fa[i] = x + y * r5;       fb[i] = a1 * b2 + a2 * b1;       if (i != j) {         fa[j] = x.conj() + y.conj() * r5;         fb[j] = fb[i].conj();       }     }     fft(fa, n);     fft(fb, n);     reverse(fa.begin() + 1, fa.end());     reverse(fb.begin() + 1, fb.end());     vector<int> res(sz);     for (int i = 0; i < sz; ++i) {       long long aa = llround(fa[i].r);       long long bb = llround(fb[i].r);       long long cc = llround(fa[i].i);       assert(llround(fb[i].i) == 0);       res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;     }     return res;   }   };  // namespace fft   namespace polynomial {   int MOD = 998244353;   template <typename T>   vector<T> subtract(vector<T> a, const vector<T>& b) {     if (a.size() < b.size()) {       a.resize(b.size());     }     for (int i = 0; i < b.size(); ++i) {       a[i] -= b[i];       if (a[i] < 0) a[i] += MOD;     }     return a;   }   template <typename T>   vector<T> inverse(const vector<T>& a) {     assert(!a.empty() && a[0] != 0);     int n = a.size();     vector<T> b = {modex(a[0], MOD - 2, MOD)};     for (int m = 2; b.size() < n; m <<= 1) {       vector<T> aCut(a.begin(), a.begin() + min(a.size(), m));       auto x = fft::multiply(aCut, fft::multiply(b, b, MOD), MOD);       b.resize(m);       for (int i = 0; i < m; ++i) {         b[i] += b[i] - x[i];         if (b[i] >= MOD) b[i] -= MOD;         if (b[i] < 0) b[i] += MOD;       }     }     b.resize(n);     return b;   }   template <typename T>   vector<T> divide(vector<T> a, vector<T> b) {     assert(!b.empty() && b.back() != 0);     int n = a.size();     int m = b.size();     if (n < m) return {};     reverse(a.begin(), a.end());     reverse(b.begin(), b.end());     b.resize(n - m + 1);     a = multiply(a, inverse(b), MOD);     a.erase(a.begin() + n - m + 1, a.end());     reverse(a.begin(), a.end());     return a;   }   template <typename T>   vector<T> remainder(const vector<T>& a, const vector<T>& b) {     return subtract(a, fft::multiply(b, divide(a, b), MOD));   }   template <typename T>   vector<T> integral(const vector<T>& a) {     vector<T> ret(a.size() + 1);     for (int i = 1; i < ret.size(); ++i) {       ret[i] = a[i] * modex(i, MOD - 2, MOD) % MOD;     }     return ret;   }   template <typename T>   vector<T> derivative(const vector<T>& a) {     if (a.empty()) return a;     vector<T> ret(a.size() - 1);     for (int i = 0; i + 1 < a.size(); ++i)       ret[i] = (long long)a[i + 1] * (i + 1) % MOD;     return ret;   }   template <typename T>   vector<T> logarithm(const vector<T>& a) {     assert(!a.empty() && a[0] == 1);     auto ret = integral(fft::multiply(derivative(a), inverse(a), MOD));     ret.resize(a.size());     return ret;   }   template <typename T>   vector<T> exponent(const vector<T>& a) {     assert(!a.empty() && a[0] == 0);     int n = a.size();     vector<T> b = {1};     for (int m = 2; b.size() < n; m <<= 1) {       vector<T> x(a.begin(), a.begin() + min(a.size(), m));       ++x[0];       if (x[0] >= MOD) x[0] -= MOD;       auto oldB = b;       b.resize(m);       b = fft::multiply(oldB, subtract(x, logarithm(b)), MOD);       b.resize(m);     }     b.resize(n);     return b;   }   template <typename T>   vector<T> multipoint(const vector<T>& a, vector<T> xs) {     int n = xs.size();     vector<vector<T>> prods(2 * n - 1);     function<void(int, int, int)> getProds = [&](int sn, int s, int e) {       if (s == e) {         prods[sn] = {(MOD - xs[s]) % MOD, 1};         return;       }       int m = (s + e) >> 1, lsn = sn + 1, rsn = sn + ((m - s + 1) << 1);       getProds(lsn, s, m);       getProds(rsn, m + 1, e);       prods[sn] = fft::multiply(prods[lsn], prods[rsn]);     };     getProds(0, 0, n - 1);     function<void(int, int, int, vector<T>)> calc = [&](int sn, int s, int e,                                                         vector<T> f) {       if (s == e) {         xs[s] = f[0];         return;       }       int m = (s + e) >> 1, lsn = sn + 1, rsn = sn + ((m - s + 1) << 1);       calc(lsn, s, m, remainder(f, prods[lsn]));       calc(rsn, m + 1, e, remainder(f, prods[rsn]));     };     calc(0, 0, n - 1, a);     return xs;   }   template <typename T>   vector<T> chirpz(vector<T> a, T g, int n) {     int d = a.size();     int phi = MOD - 1;     vector<long long> pw(phi + 1, 1);     for (int i = 1; i <= phi; ++i) pw[i] = pw[i - 1] * g % MOD;     auto chirpzEven = [&](const vector<T>& b, int m) {       vector<T> u(d), v(m + d - 1);       for (int i = 0; i < d; ++i) {         u[i] = b[i] * pw[(long long)i * i % phi] % MOD;       }       for (int i = -(d - 1); i < m; ++i) {         v[d - 1 + i] = pw[phi - (long long)i * i % phi];       }       auto z = fft::multiply(u, v, MOD);       vector<T> ret(m);       for (int i = 0; i < m; ++i) {         ret[i] = pw[(long long)i * i % phi] * z[d - 1 + i] % MOD;       }       return ret;     };     auto even = chirpzEven(a, (n + 1) >> 1);     for (int i = 0; i < d; ++i) a[i] = a[i] * pw[i] % MOD;     auto odd = chirpzEven(a, n >> 1);     vector<T> ret(n);     for (int i = 0; i < n; ++i) {       ret[i] = (i & 1) ? odd[i >> 1] : even[i >> 1];     }     return ret;   }   };  // namespace polynomial   using namespace polynomial;   int primitiveRoot(int n) {     vector<int> fact;     int phi = n - 1, x = phi;     for (int i = 2; i * i <= x; ++i)       if (x % i == 0) {         fact.push_back(i);         while (x % i == 0) x /= i;       }     if (x > 1) fact.push_back(x);     for (int res = 1; res <= n; ++res) {       bool ok = true;       for (int i = 0; i < fact.size() && ok; ++i)         ok &= modex(res, phi / fact[i], n) != 1;       if (ok) return res;     }     return -1;   }   char str[100005];   int main() {     int n, m;     scanf( %d%d , &n, &m);     vector<int> poly(n);     scanf( %s , str);     for (int i = 0; i < n; ++i) {       poly[i] += str[i] -  a ;     }     scanf( %s , str);     for (int i = 0; i < n; ++i) {       poly[i] -= str[i] -  a ;     }     const int MAXN = 300005;     bitset<MAXN> notp;     notp[1] = notp[0] = 1;     for (int i = 2; i < MAXN; ++i)       if (!notp[i])         for (int j = i + i; j < MAXN; j += i) notp[j] = 1;     mt19937 rng(42);     for (;;) {       int p = m + n + rng() % 100000;       if (notp[p]) continue;       MOD = p;       int g = primitiveRoot(p);       auto z = poly;       for (int i = 0; i < z.size(); ++i) z[i] = (z[i] % MOD + MOD) % MOD;       auto res = chirpz(z, g, p - 1);       long long pw = 1;       for (int i = 0; i < p - 1; ++i) {         if (1 < pw && pw < p - 1 && res[i] == 0) {           long long z = 0;           long long r = pw;           for (int i = n - 1; i >= 0; --i) {             z = (z * r + poly[i]) % MOD;           }           cerr <<  z                 <<  =  << z << endl;           printf( %d %lld n , MOD, r);           return 0;         }         pw = pw * g % MOD;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b == 1) return a;     long long int r = power(a, b / 2) % 1000000007;     if (b % 2 == 0)       return (r * r) % 1000000007;     else       return (((r * r) % 1000000007) * a) % 1000000007;   }   int prime[1000009];   int sumprime[1000009];   void sieve(int n) {     prime[0] = prime[1] = 1;     for (int i = 2; i <= sqrt(n); i++) {       if (!prime[i]) {         for (int j = 2 * i; j <= n; j = j + i) {           prime[j]++;         }       }     }     for (int i = 1; i <= n; i++) {       if (!prime[i]) {         sumprime[i] = sumprime[i - 1] + 1;       } else {         sumprime[i] = sumprime[i - 1];       }     }   }   bool comp(const pair<long double, long long int> &a,             const pair<long double, long long int> &b) {     if (a.first == b.first) return a.second < b.second;     return (a.first < b.first);   }   const int N = 5 * 1e5 + 9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int m, n;     cin >> n >> m;     vector<pair<long double, long long int>> v;     for (int i = 1; i <= m; i++) {       long double d = abs((m + 1) / 2.0 - i);       v.push_back({d, i});     }     sort(v.begin(), v.end(), comp);     for (int i = 0; i < n; i++) {       int val = i % m;       cout << v[val].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, m;   struct point {     int x, y;     point() {}     point(int x, int y) : x(x), y(y) {}     point operator-(point a) { return point(x - a.x, y - a.y); }     bool operator<(const point& a) const { return x == a.x ? y < a.y : x < a.x; }   } a[N], b[N];   long long dot(point a, point b) {     return (long long)a.x * b.x + (long long)a.y * b.y;   }   long long cross(point a, point b) {     return (long long)a.x * b.y - (long long)a.y * b.x;   }   long long dist2(point a, point b) { return dot(a - b, a - b); }   vector<point> convex_hull(point* p, int n) {     sort(p + 1, p + 1 + n);     vector<point> convex;     for (int i = 1; i <= n; ++i) {       for (int j; (j = convex.size()) > 1 &&                   cross(convex[j - 1] - convex[j - 2], p[i] - convex[j - 2]) <= 0;            convex.pop_back())         ;       convex.push_back(p[i]);     }     int old = convex.size();     for (int i = n - 1; i; --i) {       for (int j; (j = convex.size()) > old &&                   cross(convex[j - 1] - convex[j - 2], p[i] - convex[j - 2]) <= 0;            convex.pop_back())         ;       convex.push_back(p[i]);     }     convex.pop_back();     return convex;   }   struct p3 {     long long x, y, z;     p3() {}     p3(long long x, long long y, long long z) : x(x), y(y), z(z) {}     bool operator<(const p3& a) const {       return x == a.x ? y == a.y ? z < a.z : y < a.y : x < a.x;     }   };   long long f(point a, point b) { return dot(a, b) + cross(a, b); }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &a[i].x, &a[i].y);     }     for (int i = 1; i <= m; ++i) {       scanf( %d%d , &b[i].x, &b[i].y);     }     vector<point> convex_a = convex_hull(a, n);     vector<point> convex_b = convex_hull(b, m);     if (convex_a.size() != convex_b.size()) {       return puts( NO ), 0;     } else {       map<p3, int> appeared;       for (int i = 0; i < convex_a.size(); ++i) {         int j = i + 1 == convex_a.size() ? 0 : i + 1;         int k = i == 0 ? convex_a.size() - 1 : i - 1;         ++appeared[p3(dist2(convex_a[k], convex_a[i]),                       f(convex_a[k] - convex_a[i], convex_a[j] - convex_a[i]),                       dist2(convex_a[j], convex_a[i]))];       }       for (int i = 0; i < convex_b.size(); ++i) {         int j = i + 1 == convex_b.size() ? 0 : i + 1;         int k = i == 0 ? convex_b.size() - 1 : i - 1;         --appeared[p3(dist2(convex_b[k], convex_b[i]),                       f(convex_b[k] - convex_b[i], convex_b[j] - convex_b[i]),                       dist2(convex_b[j], convex_b[i]))];       }       bool same = true;       for (map<p3, int>::iterator it = appeared.begin(); it != appeared.end();            ++it) {         if (it->second != 0) {           same = false;         }       }       return puts(same ?  YES  :  NO ), 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 35, L = 10005;   int T, k, i, j, n, m;   int a[2][N], b[N * 2], bb[N * 2];   char cc[5], ans[L], *ass, out[26][L], *pp[26];   inline void getseq() {     int xb = 0;     for (j = 0; j < n; ++j)       if (a[0][j]) b[xb++] = a[0][j];     for (j = n - 1; j >= 0; --j)       if (a[1][j]) b[xb++] = a[1][j];   }   inline void push(char c1, char c2) { *pp[c1 -  A ]++ = c2; }   inline void opt(char c) { *ass++ = c; }   inline void get_rot2(int x, char c) {     int i;     x -= k;     if (x < 0) x += m;     for (i = 0; i < x; ++i) push(c,  R );     for (i = 0; i < k; ++i) push(c,  l );     push(c,  u );     for (i = 0; i < k; ++i) push(c,  r );     push(c,  d );     for (i = x; i < m; ++i) push(c,  R );   }   inline void rot2(int x) {     int i;     x -= k;     if (x < 0) x += m;     for (i = 0; i < x; ++i) opt( R );     for (i = 0; i < k; ++i) opt( l );     opt( u );     for (i = 0; i < k; ++i) opt( r );     opt( d );     for (i = x; i < m; ++i) opt( R );   }   inline void rot3(int x) {     swap(b[x], b[x + 1]);     swap(b[x + 1], b[x + 2]);     if (k == 1) return rot2(x);     int i;     if (x >= 8) opt(x / 8 - 1 +  A );     if (x % 8) opt(x % 8 - 1 +  I );     opt( U );     x = m - x;     if (x >= 8) opt(x / 8 - 1 +  A );     if (x % 8) opt(x % 8 - 1 +  I );   }   int calcI(int* b) {     int ass = 0;     for (i = 0; i < m; ++i)       for (j = i + 1; j < m; ++j) ass += b[i] > b[j];     return ass;   }   int main() {     for (scanf( %d , &T); T--;) {       scanf( %d , &k);       n = k * 2 + 1;       m = n * 2 - 1;       ass = ans;       for (i = 0; i < 2; ++i)         for (j = 0; j < n; ++j)           scanf( %s , cc), *cc ==  E  ? a[i][j] = 0 : sscanf(cc,  %d , a[i] + j);       for (j = 0; j + 1 < n; ++j)         for (i = 0; i < 2; ++i)           if (a[i][j] == 0) opt( r ), swap(a[i][j], a[i][j + 1]);       if (a[0][n - 1] == 0) opt( d ), swap(a[0][n - 1], a[1][n - 1]);       ;       int i1, i2;       getseq();       i1 = calcI(b);       for (i = 0; i < m; ++i) bb[i] = i + 1;       reverse(bb + n, bb + m);       i2 = calcI(bb);       if ((i2 - i1) & 1) {         puts( SURGERY FAILED );         continue;       }       for (i = 0; i < 26; ++i) pp[i] = out[i];       for (i = n - 1; i; --i) push( R ,  l );       push( R ,  u );       for (i = 0; i < n - 1; ++i) push( R ,  r );       push( R ,  d );       for (i = 1; i <= 8; ++i) push( A ,  R );       for (i = 2; i <= 8; ++i)         push( A  + i - 1,  A  + i - 2), push( A  + i - 1,  A );       push( I ,  R );       for (i = 1; i < 8; ++i) push( I  + i,  I  + i - 1), push( I  + i,  R );       get_rot2(0,  S );       get_rot2(1,  T );       push( U ,  T );       push( U ,  T );       for (i = 1; i <= 2 * k; i += 2) push( U ,  T ), push( U ,  S );       for (i = 0; i + 2 < m; ++i) {         for (j = i; b[j] != bb[i]; ++j)           ;         for (; j > i + 1; rot3(j - 2), --j)           ;         if (j > i) rot3(j - 1);       }       puts( SURGERY COMPLETE );       *ass = 0;       puts(ans);       for (i = 0; i < 26; ++i)         if (pp[i] != out[i]) *pp[i] = 0, printf( %c %s n , i +  A , out[i]);       puts( DONE );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   using pll = pair<ll, ll>;   const int N = 100010;   int m, n, q[N], a[N];   int main() {     scanf( %d , &m);     for (int i = 0; i < m; i++) scanf( %d , &q[i]);     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     int mn = *min_element(q, q + m) + 2;     int ans = 0;     for (int i = 0; i < n; i++) ans += a[i];     sort(a, a + n);     reverse(a, a + n);     for (int i = mn - 2; i < n; i += mn) {       ans -= a[i];       if (i + 1 < n) ans -= a[i + 1];     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, tc;     cin >> tc;     while (tc-- != 0) {       cin >> n;       if (n & 1) {         cout <<  7 ;         for (int i = 1; i < n / 2; i++) {           cout <<  1 ;         }       } else {         for (int i = 1; i <= n / 2; i++) {           cout <<  1 ;         }       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, inf = 1e9 + 7;   long long n, k, i, j;   long long A[N], l[N], r[N], len[N];   set<pair<long long, long long> > st;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long i, j, a, b, ans = 0, cur;     cin >> n;     for (i = 1; i < n + 1; i++) cin >> A[i];     i = 1;     while (i <= n) {       j = i;       while (j <= n && A[j] == A[i]) j++;       r[i] = j;       l[j] = i;       len[i] = j - i;       st.insert(pair<long long, long long>(i - j, i));       i = j;     }     while (!st.empty()) {       cur = st.begin()->second;       st.erase(st.begin());       ans++;       len[cur] = 0;       a = l[cur];       b = r[cur];       if (st.empty()) break;       if (A[a] == A[b] && a > 0 && b <= n) {         st.erase(pair<long long, long long>(-len[a], a));         st.erase(pair<long long, long long>(-len[b], b));         r[a] = r[b];         l[r[a]] = a;         len[a] += len[b];         len[b] = 0;         st.insert(pair<long long, long long>(-len[a], a));       } else {         r[a] = b;         l[b] = a;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   struct M {     int t, x[4];     M(int s, int a, int b, int c, int d = 0) {       t = s;       x[0] = a;       x[1] = b;       x[2] = c;       x[3] = d;     }     void p() {       printf( %d , t);       for (int i = 0; i < (int)(t); ++i) printf(  %d , x[i]);       printf(  n );     }   };   vector<M> an;   int main() {     int n;     scanf( %d , &n);     while (n > 4) {       int a = n, b = n - 1;       an.push_back(M(3, 1, a, b));       an.push_back(M(3, 1, a, b));       for (int i = 3; i < (int)(b); ++i) an.push_back(M(4, i - 1, a, i, b));       an.push_back(M(4, b - 1, a, 2, b));       n -= 2;     }     if (n == 4) {       an.push_back(M(4, 1, 2, 3, 4));       an.push_back(M(4, 1, 3, 4, 2));       an.push_back(M(4, 1, 4, 2, 3));     } else {       an.push_back(M(3, 1, 2, 3));       an.push_back(M(3, 1, 2, 3));     }     printf( %d n , (int)an.size());     for (int i = 0; i < (int)(an.size()); ++i) an[i].p();   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     ios_base ::sync_with_stdio(false), cin.tie(NULL);     int test;     cin >> test;     while (test--) {       int n, k;       string s;       cin >> s;       int a = 1, poss = true;       while (poss) {         poss = false;         string t;         int len = s.size();         for (int i = 0; i < len - 1; ++i) {           if ((s[i] ==  1  && s[i + 1] ==  0 ) ||               (s[i] ==  0  && s[i + 1] ==  1 )) {             i += 2;             poss = true;             a = (a == 1) ? 0 : 1;             while (i < len) {               t += s[i];               i++;             }           } else             t += s[i];         }         s = t;       }       if (a == 0)         cout <<  DA n ;       else         cout <<  NET n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int mid = 0;   int num = 0;   int e[21][21];   int flag[21];   void dfs(int x, int sum) {     if (x > n) {       num = num + 1;       return;     }     dfs(x + 1, sum);     for (int y = 1; y <= n; y++) {       if (num > k) {         break;       }       if (flag[y] == 0 && e[x][y] != 0 && sum + e[x][y] <= mid) {         flag[y] = 1;         dfs(x + 1, sum + e[x][y]);         flag[y] = 0;       }     }     return;   }   int main() {     int u, v, w;     cin >> n >> m >> k;     int l = 0;     int h = 1000 * n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         e[i][j] = 0;       }     }     for (int i = 0; i < m; i++) {       cin >> u >> v >> w;       e[u][v] = w;     }     while (l < h) {       mid = (l + h) / 2;       num = 0;       dfs(1, 0);       if (num < k) {         l = mid + 1;       } else {         h = mid;       }     }     cout << l << endl;   }


#include <bits/stdc++.h>   using namespace std;   bool win(long long A, long long B) {     if (A == 0 || B == 0) return false;     if (A > B) swap(A, B);     if (!win(A, B % A)) return true;     long long N = B / A;     long long MOD = A + 1;     return ((N % MOD) % 2 == 0);   }   int main(void) {     int T, t;     long long A, B;     cin >> T;     for ((t) = 0; (t) < (int)(T); (t)++) {       cin >> A >> B;       bool ans = win(A, B);       cout << (ans ?  First  :  Second ) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int *b = new int[n];     int max_val = 0;     for (int i = 0; i < n; i++) {       cin >> b[i];       if (i == 0) {       } else if (i == 1) {         b[i] = b[0] + b[1];         max_val = (b[0] > b[1]) ? b[0] : b[1];       } else {         b[i] = max_val + b[i];         if (max_val < b[i]) max_val = b[i];       }       cout << b[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, cnt = 1;     cin >> n;     int x[n];     for (int i = 0; i < n; i++) {       cin >> x[i];     }     for (int i = 0; i < n - 1; i++) {       if (x[i] != x[i + 1]) {         cnt++;       }     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   const long long mod = 1e9 + 7;   const long long maxn = 1e5 + 10;   long long n, x, y;   long long money[maxn];   pair<long long, long long> show[maxn];   multiset<pair<long long, long long> > s;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int main() {     long long ans = 0;     n = read(), x = read(), y = read();     for (long long i = 1; i <= n; i++) {       show[i].first = read(), show[i].second = read();       s.insert({show[i].second, show[i].first});     }     sort(show + 1, show + 1 + n);     for (long long i = 1; i <= n; i++) {       money[i] = (x + y * (show[i].second - show[i].first));       if (!s.size() || (*s.begin()).first >= show[i].first) continue;       multiset<pair<long long, long long> >::iterator iter =           s.lower_bound({show[i].first, 0});       long long tmp = (*(--iter)).first;       if (y * (show[i].second - tmp) >= money[i]) continue;       money[i] = y * (show[i].second - tmp);       s.erase(iter);     }     for (long long i = 1; i <= n; i++) {       ans += money[i];       ans %= mod;     }     ans %= mod;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 9;   long long Pow(long long a, long long x) {     long long res = 1;     while (x) {       if (x & 1) res = res * a % mod;       a = a * a % mod;       x >>= 1;     }     return res;   }   long long inv(long long a) { return Pow(a, mod - 2); }   long long n, a, b, k;   string s;   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> a >> b >> k;     cin >> s;     long long ans = 0;     for (int i = 0; i < k; ++i) {       int f = s[i] ==  -  ? -1 : 1;       ans = (ans + (f * Pow(a, n - i) * Pow(b, i) % mod + mod) % mod) % mod;     }     long long q = Pow(inv(a) * b % mod, k);     if (q == 1) {       long long res = (n + 1) / k;       ans = ans * res % mod;     } else {       long long res = Pow(q, (n + 1) / k);       res--;       if (res < 0) res += mod;       res = res * inv(q - 1) % mod;       ans = res * ans % mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   template <typename T>   T pow(T a, T b, long long m) {     T ans = 1;     while (b > 0) {       if (b % 2 == 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans % m;   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   int main() {     long long i, t, a;     cin >> t;     while (t--) {       cin >> a;       double z = abs(a - 180);       z = (double)360 / z;       if (floor(z) == ceil(z)) {         cout <<  YES ;       } else {         cout <<  NO ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   multiset<long long> mset;   void add(long long k) { mset.insert(k); }   long long getmin() { return *mset.begin(); }   long long getmax() { return *prev(mset.end()); }   long long lower(long long k) {     if (mset.size() == 0) return -1;     if (k < getmin()) return -1;     if (mset.count(k)) return k;     auto it = mset.lower_bound(k);     return *prev(it);   }   long long uper(long long k) {     if (mset.size() == 0) return -1;     auto it = mset.lower_bound(k);     if (it == mset.end()) return -1;     return *it;   }   void erase(long long k) { mset.erase(mset.find(k)); }   long long t, n;   const long long inf = 0x3f3f3f3f;   long long dp[200005][2], a[200005];   signed main() {     ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       cin >> n;       for (long long i = 0; i < n; i++) {         cin >> a[i];         dp[i][1] = inf;         dp[i][0] = inf;       }       dp[0][0] = a[0];       dp[1][0] = dp[0][0] + a[1];       dp[1][1] = dp[0][0];       for (long long i = 2; i < n; i++) {         dp[i][0] = min(dp[i - 1][1] + a[i], dp[i - 2][1] + a[i - 1] + a[i]);         dp[i][1] = min(dp[i - 1][0], dp[i - 2][0]);       }       cout << min(dp[n - 1][0], dp[n - 1][1]) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5, maxl = 20;   bool mark[maxn];   long long sum = 0, cmp, d[maxn];   pair<long long, long long> par[maxn][maxl];   vector<pair<long long, long long> > adj[maxn];   struct Type_Dsu {     long long par[maxn], h[maxn];     void make_set(long long x) {       par[x] = x;       h[x] = 0;     }     long long findpar(long long x) {       return par[x] = (x == par[x] ? x : findpar(par[x]));     }     bool merge(long long x, long long y) {       x = findpar(x);       y = findpar(y);       if (x == y) return false;       if (d[x] > d[y]) swap(x, y);       par[x] = y;       h[y] += (h[x] == h[y]);       return true;     }     bool same(long long x, long long y) { return (findpar(x) == findpar(y)); }   } dsu;   struct Edge {     long long fi, se, w;   };   vector<Edge> edges;   void input() {     long long n, m;     cin >> n >> m;     cmp = n;     for (long long i = 0; i < n; i++) dsu.make_set(i);     for (long long i = 0; i < m; i++) {       long long fi, se, w;       cin >> fi >> se >> w;       fi--;       se--;       Edge edg;       edg.fi = fi;       edg.se = se;       edg.w = w;       edges.push_back(edg);     }   }   bool comp(Edge a, Edge b) { return (a.w < b.w); }   void mst() {     sort(edges.begin(), edges.end(), comp);     for (long long i = 0; i < edges.size(); i++) {       long long fi = edges[i].fi, se = edges[i].se, w = edges[i].w;       if (!dsu.same(fi, se)) {         cmp--;         sum += w;         dsu.merge(fi, se);         adj[fi].push_back(make_pair(w, se));         adj[se].push_back(make_pair(w, fi));       }     }   }   void dfs(long long x) {     mark[x] = true;     for (long long i = 0; i < adj[x].size(); i++) {       long long child = adj[x][i].second, w = adj[x][i].first;       if (mark[child]) continue;       par[child][0] = make_pair(x, w);       d[child] = d[x] + 1;       for (long long j = 1; j < maxl; j++) {         long long p = par[par[child][j - 1].first][j - 1].first;         long long maxe = max(par[child][j - 1].second,                              par[par[child][j - 1].first][j - 1].second);         par[child][j] = make_pair(p, maxe);       }       dfs(child);     }   }   pair<long long, long long> findpar(long long x, long long d) {     long long p = x, maxe = 0;     for (long long i = 0; i < maxl; i++)       if ((d >> i) & 1) {         maxe = max(maxe, par[p][i].second);         p = par[p][i].first;       }     return make_pair(p, maxe);   }   long long lca(long long x, long long y) {     long long maxe = 0;     if (d[x] > d[y]) swap(x, y);     maxe = findpar(y, d[y] - d[x]).second;     y = findpar(y, d[y] - d[x]).first;     if (x == y) return maxe;     for (long long i = maxl - 1; i >= 0; i--)       if (par[x][i].first != par[y][i].first) {         maxe = max(maxe, par[x][i].second);         maxe = max(maxe, par[y][i].second);         x = par[x][i].first;         y = par[y][i].first;       }     maxe = max(maxe, par[x][0].second);     maxe = max(maxe, par[y][0].second);     return maxe;   }   void output() {     long long q;     cin >> q;     for (long long i = 0; i < q; i++) {       long long A, B;       cin >> A >> B;       A--;       B--;       cout << endl;       if (cmp > 2) cout << -1 << endl;       if (cmp == 2)         if (dsu.same(A, B))           cout << -1 << endl;         else           cout << sum << endl;       if (cmp == 1) cout << sum - lca(A, B) << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     input();     mst();     dfs(0);     output();     return 0;   }


#include <bits/stdc++.h>   size_t solve(const std::vector<std::string>& c) {     const size_t n = c.size();     size_t count = 0;     for (size_t i = 0; i < n; ++i) {       for (size_t j = 0; j < n; ++j) {         for (size_t k = j + 1; k < n; ++k) {           if (c[i][j] ==  C  && c[i][k] ==  C ) ++count;           if (c[j][i] ==  C  && c[k][i] ==  C ) ++count;         }       }     }     return count;   }   int main() {     size_t n;     std::cin >> n;     std::vector<std::string> c(n);     for (size_t i = 0; i < n; ++i) std::cin >> c[i];     std::cout << solve(c) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5;   int main() {     vector<int> goh_nakhor[maxn];     int n, k, ans = 0;     cin >> n >> k;     for (int i = 0; i < k; i++) {       int num, tmp;       cin >> num >> tmp;       if (tmp != 1) ans += num - 1;       for (int i = 0; i < num - 1; i++) {         int val;         cin >> val;         goh_nakhor[tmp].push_back(val);       }     }     int cnt = 0;     for (int i = 0; i < goh_nakhor[1].size(); i++) {       if (goh_nakhor[1][i] == i + 2) cnt++;     }     cout << ans + (goh_nakhor[1].size() - cnt - 1) + n - cnt <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   char color[16];   int r[16], b[16];   int redCount[1 << 16], blueCount[1 << 16];   int dp[1 << 16][2][257];   int calc(int mask, bool noRed, int other) {     other = min(other, n * n);     if (dp[mask][noRed][other] != -1) return dp[mask][noRed][other];     if (mask == (1 << n) - 1) return dp[mask][noRed][other] = 0;     dp[mask][noRed][other] = 1000000000;     for (int i = 0; i < n; ++i) {       if (mask & (1 << i)) continue;       int nextMask = mask | (1 << i);       int payRed = max(0, r[i] - redCount[mask]),           payBlue = max(0, b[i] - blueCount[mask]);       int haveRed = 0, haveBlue = 0;       if (noRed)         haveBlue = other;       else         haveRed = other;       int add = max(max(payRed - haveRed, payBlue - haveBlue), 0);       haveRed += add, haveBlue += add;       haveRed -= payRed, haveBlue -= payBlue;       if (haveRed == 0)         dp[mask][noRed][other] =             min(dp[mask][noRed][other], add + calc(nextMask, 1, haveBlue));       else if (haveBlue == 0)         dp[mask][noRed][other] =             min(dp[mask][noRed][other], add + calc(nextMask, 0, haveRed));     }     return dp[mask][noRed][other];   }   int main() {     cin >> n;     for (int i = 0; i < n; ++i) cin >> color[i] >> r[i] >> b[i];     for (int i = 0; i < 1 << n; ++i) {       for (int j = 0; j < n; ++j) {         if ((i & (1 << j)) == 0) continue;         if (color[j] ==  R )           redCount[i]++;         else           blueCount[i]++;       }       for (int j = 0; j < 2; ++j)         for (int k = 0; k <= 256; ++k) dp[i][j][k] = -1;     }     int needRed = 0, needBlue = 0;     for (int i = 0; i < n; ++i) {       needRed += max(0, r[i] - n);       needBlue += max(0, b[i] - n);       r[i] = min(r[i], n);       b[i] = min(b[i], n);     }     if (needRed >= needBlue)       cout << needRed + n + calc(0, 1, needRed - needBlue);     else       cout << needBlue + n + calc(0, 0, needBlue - needRed);     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 5;   struct segment_change {     int to_add;     segment_change(int _to_add = 0) : to_add(_to_add) {}     void reset() { to_add = 0; }     bool has_change() const { return to_add != 0; }     segment_change combine(const segment_change &other) const {       return segment_change(to_add + other.to_add);     }   };   struct segment {     int maximum;     segment(int _maximum = -INF) : maximum(_maximum) {}     void apply(int, const segment_change &change) { maximum += change.to_add; }     void join(const segment &other) { maximum = max(maximum, other.maximum); }     void join(const segment &a, const segment &b) {       *this = a;       join(b);     }   };   struct add_max_seg_tree {     int tree_n = 0;     vector<segment> tree;     vector<segment_change> changes;     add_max_seg_tree(int n = 0) {       if (n > 0) init(n);     }     void init(int n) {       tree_n = 1;       while (tree_n < n) tree_n *= 2;       tree.assign(2 * tree_n, segment());       changes.assign(tree_n, segment_change());     }     void build(const vector<segment> &initial) {       int n = initial.size();       assert(n <= tree_n);       for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i];       for (int position = tree_n - 1; position > 0; position--)         tree[position].join(tree[2 * position], tree[2 * position + 1]);     }     void push_down(int position, int length) {       if (!changes[position].has_change()) return;       if (2 * position < tree_n) {         changes[2 * position] = changes[2 * position].combine(changes[position]);         changes[2 * position + 1] =             changes[2 * position + 1].combine(changes[position]);       }       tree[2 * position].apply(length / 2, changes[position]);       tree[2 * position + 1].apply(length / 2, changes[position]);       changes[position].reset();     }     void push_all(int a, int b) {       assert(0 <= a && a < b && b <= tree_n);       a += tree_n;       b += tree_n - 1;       for (int up = 31 - __builtin_clz(tree_n); up > 0; up--) {         int x = a >> up, y = b >> up;         push_down(x, 1 << up);         if (x != y) push_down(y, 1 << up);       }     }     void join_and_apply(int position, int length) {       tree[position].join(tree[2 * position], tree[2 * position + 1]);       tree[position].apply(length, changes[position]);     }     void join_all(int a, int b) {       assert(0 <= a && a < b && b <= tree_n);       a += tree_n;       b += tree_n - 1;       int length = 1;       while (a > 1) {         a /= 2;         b /= 2;         length *= 2;         join_and_apply(a, length);         if (a != b) join_and_apply(b, length);       }     }     pair<int, int> right_half[32];     template <typename T_range_op>     void process_range(int a, int b, bool needs_join, T_range_op &&range_op) {       if (a == b) return;       push_all(a, b);       int original_a = a, original_b = b;       int length = 1, r_size = 0;       for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) {         if (a & 1) range_op(a++, length);         if (b & 1) right_half[r_size++] = {--b, length};       }       for (int i = r_size - 1; i >= 0; i--)         range_op(right_half[i].first, right_half[i].second);       if (needs_join) join_all(original_a, original_b);     }     segment query(int a, int b) {       assert(0 <= a && a <= b && b <= tree_n);       segment answer;       process_range(a, b, false,                     [&](int position, int) { answer.join(tree[position]); });       return answer;     }     void update(int a, int b, const segment_change &change) {       assert(0 <= a && a <= b && b <= tree_n);       process_range(a, b, true, [&](int position, int length) {         tree[position].apply(length, change);         if (position < tree_n)           changes[position] = changes[position].combine(change);       });     }     void set_value(int index, int value) {       assert(0 <= index && index < tree_n);       int current = query(index, index + 1).maximum;       update(index, index + 1, segment_change(value - current));     }   };   int N;   vector<int> parent;   vector<vector<int>> children;   vector<int> tour_start, tour_end;   int tour;   void dfs(int node) {     tour_start[node] = tour++;     for (int child : children[node]) dfs(child);     tour_end[node] = tour;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) {       if (vi != v.begin()) os <<  ,  ;       os << *vi;     }     os <<  } ;     return os;   }   template <>   ostream &operator<<(ostream &os, const vector<string> &v) {     os <<  { ;     for (vector<string>::const_iterator vi = v.begin(); vi != v.end(); ++vi) {       if (vi != v.begin()) os <<  ,  ;       os <<     << *vi <<    ;     }     os <<  } ;     return os;   }   vector<int> solve(vector<int> perm) {     assert((int)perm.size() == N);     assert(perm.front() == 1);     for (int &p : perm) p--;     parent.assign(N, -1);     vector<pair<int, int>> events;     vector<int> stack = {-1};     for (int x : perm) {       int erased = -1;       while (stack.back() > x) {         erased = stack.back();         stack.pop_back();       }       events.emplace_back(x, stack.back());       events.emplace_back(erased, x);       parent[x] = stack.back();       if (erased >= 0) parent[erased] = x;       stack.push_back(x);     }     children.assign(N, {});     tour_start.resize(N);     tour_end.resize(N);     for (int i = 0; i < N; i++)       if (parent[i] >= 0) children[parent[i]].push_back(i);     tour = 0;     dfs(0);     vector<int> answers(N, INF);     add_max_seg_tree tree(N);     for (int i = 0; i < N; i++) {       int a = events[2 * i].first;       int b = events[2 * i].second;       assert(a >= 0);       tree.set_value(           tour_start[a],           b < 0 ? 1 : tree.query(tour_start[b], tour_start[b] + 1).maximum + 1);       a = events[2 * i + 1].first;       b = events[2 * i + 1].second;       if (a >= 0) {         tree.update(tour_start[a], tour_end[a], segment_change(+1));       }       answers[i] = tree.query(0, N).maximum;     }     return answers;   }   vector<int> A;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> N;     A.resize(N);     for (int &a : A) cin >> a;     int one = find(A.begin(), A.end(), 1) - A.begin();     vector<int> start_one, end_one;     for (int i = one; (int)start_one.size() < N; i = (i + 1) % N)       start_one.push_back(A[i]);     for (int i = one; (int)end_one.size() < N; i = (i + N - 1) % N)       end_one.push_back(A[i]);     vector<int> left = solve(end_one), right = solve(start_one);     int best_left = 0, best = INF;     for (int i = 0; i < N; i++) {       if (max(left[i], right[N - 1 - i]) < best) {         best = max(left[i], right[N - 1 - i]);         best_left = i;       }     }     cout << best <<     << (one - best_left + N) % N <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int c1, c2, c3, c4, n, m;   int rs1, rs2, rs3, rs4, rs;   int a, b;   int main() {     ios_base::sync_with_stdio(0);     ;     cin.tie(0);     ;     cin >> c1 >> c2 >> c3 >> c4;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a;       rs1 += min(a * c1, c2);     }     rs2 = min(rs1, c3);     for (int i = 1; i <= m; i++) {       cin >> b;       rs3 += min(b * c1, c2);     }     rs4 = min(rs3, c3);     cout << min(rs2 + rs4, c4);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   const int MOD = 1000000007;   const int INF = 0x3f3f3f3f;   void add(int &x, int y) {     x += y;     if (x >= MOD) x -= MOD;   }   int n, h[N];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &h[i]);       h[i]--;     }     h[n + 1] = INF;     int ans = h[1], sum = min(h[1], h[2]);     for (int i = 2; i <= n; i++) {       add(ans, h[i]);       add(ans, 1LL * sum * min(h[i - 1], h[i]) % MOD);       sum = 1LL * sum * min(min(h[i - 1], h[i]), h[i + 1]) % MOD;       add(sum, min(h[i], h[i + 1]));     }     printf( %d n , ans);     return 0;   }