#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int k, n, w; n   cin >> k >> n >> w; n   long long t = k * (w) * (w + 1) / 2, h = 0; n   cout << max(h, t - n) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3005; n const int MOD = 998244353; n int f[N][N]; n char s[N], t[N]; n int main() { n   scanf( %s %s , s + 1, t + 1); n   int n = strlen(s + 1), m = strlen(t + 1); n   for (int l = 1; l <= n; ++l) { n     for (int r = n; r >= l; --r) { n       int pos = r - l + 1; n       if (l == 1 and r >= m) f[l][r] = 1; n       char cur = s[pos + 1]; n       if (l > 1 and (l - 1 > m or t[l - 1] == cur)) { n         f[l][r] += f[l - 1][r]; n         if (f[l][r] >= MOD) f[l][r] -= MOD; n       } n       if (r < n and (r + 1 > m or t[r + 1] == cur)) { n         f[l][r] += f[l][r + 1]; n         if (f[l][r] >= MOD) f[l][r] -= MOD; n       } n     } n   } n   int ans = 0; n   for (int l = 1; l <= n; ++l) { n     if (l > m or s[1] == t[l]) { n       ans += f[l][l]; n       if (ans >= MOD) ans -= MOD; n       ans += f[l][l]; n       if (ans >= MOD) ans -= MOD; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> a, b; n int n, m, k; n string s; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     a.push_back(s); n   } n   for (int i = 0; i < m; ++i) { n     cin >> s; n     b.push_back(s); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i] == b[j]) ++k; n     } n   } n   if (k % 2 == 0) { n     if (n > m) n       cout <<  YES ; n     else n       cout <<  NO ; n   } else { n     if (n >= m) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n long double intial[100009], usage[100009]; n bool solve(long double x) { n   long double pp = p * x; n   for (int i = 0; i < n; i++) { n     pp -= max(x * usage[i] - intial[i], (long double)0); n     if (pp < -1e-10) return false; n   } n   return true; n } n int main() { n   cin >> n >> p; n   for (int i = 0; i < n; i++) cin >> usage[i] >> intial[i]; n   long double low = 0, high = 1e19, m; n   for (int i = 0; i < 200; i++) { n     m = (low + high) / 2; n     if (solve(m)) n       low = m; n     else n       high = m; n   } n   if (low > 1e18 + 20) return cout << -1, 0; n   cout << setprecision(6) << fixed << low; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int a[N]; n int b[N]; n int n; n int mp[2 * N]; n int ans; n unordered_map<int, int> num; n int sol_01(int a[], int n) { n   memset(mp, -1, sizeof mp); n   int cur = 0; n   int ans = 0; n   int offset = 2e5; n   for (int i = 1; i <= n; i++) { n     cur += a[i]; n     if (cur == 0) ans = i; n     if (mp[cur + offset] != -1) n       ans = max(ans, i - mp[cur + offset]); n     else n       mp[cur + offset] = i; n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     num[a[i]]++; n   } n   int maxx = 0; n   int cc = 0; n   for (int i = 1; i <= 100; i++) n     if (num[i] > cc) { n       maxx = i; n       cc = num[i]; n     } n   for (int i = 1; i <= 100; i++) n     if (num[i] && i != maxx) { n       for (int j = 1; j <= n; j++) n         if (a[j] == maxx) n           b[j] = 1; n         else if (a[j] == i) n           b[j] = -1; n         else n           b[j] = 0; n       ans = max(ans, sol_01(b, n)); n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string st; n   while (getline(cin, st)) { n     int f = 0; n     int s = st.size() - 2; n     if (st[s] ==    ) { n       while (st[s] ==    ) { n         if (st[s - 1] ==  a  || st[s - 1] ==  e  || st[s - 1] ==  i  || n             st[s - 1] ==  o  || st[s - 1] ==  u  || st[s - 1] ==  y  || n             st[s - 1] ==  A  || st[s - 1] ==  E  || st[s - 1] ==  I  || n             st[s - 1] ==  O  || st[s - 1] ==  U  || st[s - 1] ==  Y ) { n           f++; n           break; n         } n         s--; n       } n     } n     if (st[s] ==  a  || st[s] ==  e  || st[s] ==  i  || st[s] ==  o  || n         st[s] ==  u  || st[s] ==  y  || st[s] ==  A  || st[s] ==  E  || n         st[s] ==  I  || st[s] ==  O  || st[s] ==  U  || st[s] ==  Y ) { n       f++; n     } n     if (f == 0) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int wrapper(int n) { n   while (n) { n     int a = n % 3; n     n = n / 3; n     if (a == 2) return 0; n   } n   return 1; n } n void solve() { n   int n; n   cin >> n; n   while (wrapper(n) == 0) n++; n   cout << n <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   long long int sz = (int)s.size(); n   int i = 0, ct = 0; n   while (s[i] ==  a ) { n     ct++; n     i++; n   } n   while (sz && ct * 2 <= sz) { n     char ch = s.back(); n     sz--; n     if (ch ==  a ) ct--; n   } n   cout << sz; n   return 0; n } n


#include <bits/stdc++.h> n #include <stdio.h> n using namespace std; n  n typedef long long ll; n typedef unsigned long long ul; n #define len (int)1e5+5 n #define mod (ll)(1e9+7) n #define pq(a) priority_queue< a > n #define pqd(b) priority_queue< b , vector< b > , greater< b >> n #define MP make_pair n #define v(i) vector<i> n #define p(aa,bb) pair< aa,bb> n #define pb push_back n #define pp pop_back n #define clr(a) memset((a),0,sizeof((a))) n #define rep(i,a,b) for(int i=(a);i<=(b);i++) n #define repd(i,a,b) for(ll i=(a);i>=(b);i--) n #define all(a) (a).begin(),(a).end() n #define F first n #define S second n #define ln length() n #define INF (1LL<<61) n #define co cout<< n #define ci cin>> n #define sc(a,b) scanf( %(a) ,&(b)) n #define nl   n  n  n #define fio(a,b) freopen(  a  , r ,stdin); freopen(  b , w ,stdout); n #define ios ios::sync_with_stdio(0); cin.tie(0); n  n ul modpow(ul x,ul n,ul m){ n     if(n==0) return 1UL%m; n     ul u=modpow(x,n/2,m); n     u = (u*u)%m; n     if(n%2==1) u = (u*x)%m; n     return u; n } n bool prm[len]; n v(int) prm_no; n void make_prm(){ n     prm[0]=prm[1]=true; n     for(int first=2;first<=len;first++){ n         if(!prm[first]){ n             prm_no.pb(first); n             for(int second=2*first;second<=len;second+=first)prm[second]=true; n         } n     } n } n v(int) factor; n void make_factor(int n) n { n     for(int i=1;i*i<=n;i++){ n         if(n%i==0){ n             factor.pb(i);factor.pb(n/i); n             if(i*i==n)factor.pp(); n         } n     } n } n ul pascal_tri[2][(int)105]; n void make_pascalTri(int n){ n     bool f=0;clr(pascal_tri); n     for(int i=0;i<=n;i++){ n         pascal_tri[f][0]=1; n         for(int j=1;j<=i;j++){ n             pascal_tri[f][j]=pascal_tri[!f][j]+pascal_tri[!f][j-1]; n         } n         f=!f; n     } n } n int prm_div[len]; n void make_prm_div(){ n     for(auto u:prm_no){ n         for(int i=u;i<=len;i+=u)prm_div[i]++; n     } n } n  n ll gcd(ll a, ll b) n { n     if (b == 0) n         return a; n     return gcd(b, a % b); n } n ll give_floor(ll n,ll a) n { n     if(n%a==0)return n/a; n     else{ n         return n/a+1; n     } n } n bool is_perfect(ll n) n { n     ll i; n     for(i=1;i*i<n;i++); n     return i*i==n; n } n  n int n,k; n void solve(){ n     ci n>>k; n     string s;ci s; n     int cnt=0; n     for(int i=1;i<=(n-i);i++){ n         if(s[i-1]==s[n-i])cnt++; n         else break; n     } n     if(cnt>=k and n>=2*k+1)co  YES n ; n     else co  NO n ; n } n  n signed main(){ n     //ios n     int t;//=1; n     ci t; n     while(t--){ n         solve(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, e, f[200009], i, j, zx, xc, s, lf[200009], rg[200009], n     tim, up[200009]; n vector<long long> v[200009], shv[200009]; n pair<long long, long long> dp[200009]; n bool bo[200009], ka[200009]; n bool anc(long long q, long long w) { n   if (lf[q] <= lf[w] && rg[q] >= rg[w]) n     return 1; n   else n     return 0; n } n void dfs(long long q, long long w) { n   bo[q] = 1; n   tim++; n   lf[q] = rg[q] = tim; n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if (bo[(*it)] == 1 && (*it) != w) { n       ka[q] = 1; n     } n     if (bo[(*it)] == 1) continue; n     shv[q].push_back((*it)); n     dfs((*it), q); n     if (rg[q] < rg[(*it)]) rg[q] = rg[(*it)]; n     if (ka[(*it)] == 1) ka[q] = 1; n   } n } n void dfs2(long long q, long long w) { n   up[q] = q; n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if ((*it) == w) continue; n     if (anc((*it), up[q]) == 1) up[q] = (*it); n   } n   for (vector<long long>::iterator it = shv[q].begin(); it != shv[q].end(); n        it++) { n     dfs2((*it), w); n     if (anc(up[(*it)], up[q]) == 1) up[q] = up[(*it)]; n   } n } n void dfs3(long long q, long long w) { n   dp[q].first = 0; n   dp[q].second = 0; n   long long pas = 0, mx = 0, mxx = 0, ma = 0; n   for (vector<long long>::iterator it = shv[q].begin(); it != shv[q].end(); n        it++) { n     dfs3((*it), q); n     pas += dp[(*it)].first; n     if (mxx < dp[(*it)].second) mxx = dp[(*it)].second; n     if (ma < dp[(*it)].second - dp[(*it)].first) n       ma = dp[(*it)].second - dp[(*it)].first; n   } n   if (ka[q] == 1) { n     dp[q].first = pas + f[q]; n   } n   dp[q].second = pas + f[q] + ma; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> a >> b; n   for (i = 1; i <= a; i++) cin >> f[i]; n   for (i = 1; i <= b; i++) { n     cin >> c >> d; n     v[c].push_back(d); n     v[d].push_back(c); n   } n   cin >> s; n   dfs(s, 0); n   dfs2(s, 0); n   dfs3(s, 0); n   cout << dp[s].second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 0x7FFFFFFF; n int n; n char bad[105][105]; n int len[105]; n char st[105]; n int leng; n char let[5], letter; n bool mai[105]; n bool alterar[105]; n void read() { n   scanf( %d , &n); n   char *c; n   for (int i = 0; i < n; i++) { n     scanf( %s , bad[i]); n     c = bad[i]; n     while (*c) { n       if ( A  <= *c && *c <=  Z ) *c = *c -  A  +  a ; n       c++; n     } n     len[i] = c - bad[i]; n   } n   scanf( %s%s , st, let); n   leng = 0; n   while (st[leng]) { n     if ( a  <= st[leng] && st[leng] <=  z ) n       mai[leng] = false; n     else n       mai[leng] = true; n     alterar[leng] = false; n     if (mai[leng]) st[leng] = st[leng] -  A  +  a ; n     leng++; n   } n   letter = let[0]; n } n void comp(int k) { n   int lasts = leng - len[k] + 1; n   for (int i = 0; i < lasts; i++) { n     bool igual = true; n     for (int j = 0; j < len[k]; j++) n       if (st[i + j] != bad[k][j]) { n         igual = false; n         break; n       } n     if (!igual) continue; n     for (int j = 0; j < len[k]; j++) alterar[i + j] = true; n   } n } n void process() { n   for (int i = 0; i < n; i++) comp(i); n   char prox =  a ; n   if (letter ==  a ) prox =  b ; n   for (int i = 0; i < leng; i++) { n     if (alterar[i]) { n       if (st[i] == letter) n         st[i] = prox; n       else n         st[i] = letter; n     } n     if (mai[i]) st[i] = st[i] +  A  -  a ; n   } n   printf( %s n , st); n } n int main() { n   read(); n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int odd, even; n bool win(int k, int odd, int even) { n   if (odd * 2 <= k || (even * 2 <= k && (k - even) % 2 == odd % 2)) n     return false; n   return true; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (x & 1) n       odd++; n     else n       even++; n   } n   k = n - k; n   if (k == 0) { n     if (odd & 1) n       printf( Stannis n ); n     else n       printf( Daenerys n ); n   } else if (k % 2 == 0) { n     if (even * 2 <= k && (k - even) % 2 != odd % 2) n       printf( Stannis n ); n     else n       printf( Daenerys n ); n   } else if (k % 2 == 1) { n     if ((odd > 0 && win(k - 1, odd - 1, even)) || n         (even > 0 && win(k - 1, odd, even - 1))) n       printf( Stannis n ); n     else n       printf( Daenerys n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > v; n long long x; n long long k; n int broj; n int main(void) { n   cin >> x; n   k = 0; n   long long i = 1; n   for (long long i = 1; i <= sqrt(x); ++i) { n     if (k + i * i > (1000000000000000000)) goto van; n     k = k + i * i; n     if (x - k < 0) goto van; n     if ((2 * (x - k)) % (i * (i + 1)) == 0) { n       v.push_back( n           pair<long long, long long>(i, i + (2 * (x - k)) / (i * (i + 1)))); n     } n   } n van:; n   for (int i = 0; i < v.size(); ++i) { n     broj++; n   } n   for (int i = v.size() - 1; i >= 0; --i) { n     if (v[i].second == v[i].first) continue; n     broj++; n   } n   cout << broj << endl; n   for (int i = 0; i < v.size(); ++i) { n     broj++; n     cout << v[i].first <<     << v[i].second << endl; n   } n   for (int i = v.size() - 1; i >= 0; --i) { n     if (v[i].second == v[i].first) continue; n     broj++; n     cout << v[i].second <<     << v[i].first << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long k; n   scanf( %d , &n); n   scanf( %I64d , &k); n   int a; n   scanf( %d , &a); n   int ans = a; n   int count = 0; n   if (k > n - 2) { n     for (int i = 1; i < n; ++i) { n       scanf( %d , &a); n       if (a > ans) ans = a; n     } n   } else { n     for (int i = 1; i < n; ++i) { n       scanf( %d , &a); n       if (count == k) break; n       if (ans > a) n         count++; n       else { n         count = 1; n         ans = a; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = n * 4, cnt = 1; cnt < n; cnt++, i -= 2) printf( %d  , i); n     printf( %d n , n * 2 + 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class BigInt { n  public: n   string value; n   bool sign; n   BigInt(); n   BigInt(string); n   BigInt(long long); n   BigInt operator+(BigInt &); n   BigInt operator-(BigInt &); n   bool operator==(BigInt &); n   bool operator!=(BigInt &); n   bool operator>(BigInt &); n   bool operator<(BigInt &); n   BigInt operator++(int); n   BigInt operator--(int); n   friend ostream &operator<<(ostream &, const BigInt &); n   friend istream &operator>>(istream &, BigInt &); n }; n string toString(long long n) { n   stringstream t; n   t << n; n   return t.str(); n } n string add(string a, string b) { n   if (a.length() > b.length()) swap(a, b); n   a = string(b.length() - a.length(),  0 ) + a; n   string res =   ; n   int carry = 0; n   for (int i = a.length() - 1; i >= 0; i--) { n     carry += (a[i] -  0 ) + (b[i] -  0 ); n     res = char(carry % 10 +  0 ) + res; n     carry /= 10; n   } n   res = (carry != 0 ? string(1, char(carry +  0 )) :   ) + res; n   return res; n } n string sub(string a, string b) { n   b = string(a.length() - b.length(),  0 ) + b; n   string res =   ; n   for (int i = a.length() - 1; i >= 0; i--) { n     if (a[i] >= b[i]) n       res = char(a[i] - b[i] +  0 ) + res; n     else { n       for (int j = i - 1; j >= 0; j--) { n         if (a[j] !=  0 ) { n           a[j]--; n           break; n         } else n           a[j] =  9 ; n       } n       res = char(10 + a[i] - b[i] +  0 ) + res; n     } n   } n   return res.find_first_not_of( 0 ) != -1 n              ? res.substr(res.find_first_not_of( 0 )) n              :  0 ; n } n int compare(string a, string b) { n   if (a.length() != b.length()) return a.length() > b.length(); n   for (int i = 0; i < a.length(); i++) n     if (a[i] != b[i]) return a[i] > b[i]; n   return 2; n } n BigInt::BigInt(string s) { n   value = s[0] ==  -  ? s.substr(1) : s; n   sign = s[0] ==  -  && s.substr(1) !=  0 ; n   value = s.find_first_not_of( -0 ) != -1 ? s.substr(s.find_first_not_of( -0 )) n                                           :  0 ; n } n BigInt::BigInt(){}; n BigInt::BigInt(long long n) : BigInt(toString(n)) {} n BigInt BigInt::operator+(BigInt &secondNumber) { n   if (sign == secondNumber.sign) n     return BigInt((sign ?  -  :   ) + add(value, secondNumber.value)); n   else if (compare(value, secondNumber.value) == 1) n     return BigInt((sign ?  -  :   ) + sub(value, secondNumber.value)); n   else n     return BigInt((secondNumber.sign ?  -  :   ) + n                   sub(secondNumber.value, value)); n } n BigInt BigInt::operator-(BigInt &secondNumber) { n   BigInt a = *this, n          b = BigInt((secondNumber.sign ?    :  - ) + secondNumber.value); n   return a + b; n } n bool BigInt::operator==(BigInt &secondNumber) { n   return value == secondNumber.value && sign == secondNumber.sign; n } n bool BigInt::operator!=(BigInt &secondNumber) { n   return !(*this == secondNumber); n } n bool BigInt::operator>(BigInt &secondNumber) { n   BigInt res = *this - secondNumber; n   return !res.sign && res.value !=  0 ; n } n bool BigInt::operator<(BigInt &secondNumber) { n   return !(*this > secondNumber) && *this != secondNumber; n } n BigInt BigInt::operator++(int dummy) { n   BigInt a = *this, b = BigInt( 1 ); n   *this = a + b; n   return a; n } n BigInt BigInt::operator--(int dummy) { n   BigInt a = *this, b = BigInt( 1 ); n   *this = a - b; n   return a; n } n ostream &operator<<(ostream &out, const BigInt &c) { n   out << (c.sign ?  -  :   ) << c.value; n   return out; n } n istream &operator>>(istream &in, BigInt &c) { n   string s; n   in >> s; n   c = BigInt(s); n   return in; n } n int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n int main() { n   long long n; n   scanf( %lld , &n); n   map<long long, long long> f; n   long long x, cnt = 0; n   long long sum = 0; n   BigInt ans = 0; n   for (int i = 0; i < n; i++) { n     scanf( %lld , &x); n     long long prev = cnt - f[x] - f[x + 1] - f[x - 1]; n     long long nwSum = n         sum - (f[x] * x) - (f[x + 1] * (x + 1)) - (f[x - 1] * (x - 1)); n     BigInt add = prev * x - nwSum; n     ans = ans + add; n     sum += x; n     cnt++; n     f[x]++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n namespace BF { n long long solve(int N, int M, int K, const vector<int>& T, const vector<int>& A, n                 const vector<int>& B) { n   long long res = INF; n   for (int mask = 0, _n = (1 << N); mask < _n; ++mask) { n     if ((__builtin_popcount((mask))) != M) continue; n     int cntA = 0, cntB = 0; n     long long cur = 0; n     for (int i = 0, _n = (N); i < _n; ++i) { n       if (mask & (1 << i)) { n         if (A[i]) cntA++; n         if (B[i]) cntB++; n         cur += T[i]; n       } n     } n     if (cntA >= K && cntB >= K) res = min(res, cur); n   } n   return res < INF ? res : -1; n } n }  // namespace BF n struct Item { n   int id, value; n   bool operator<(const Item i) const { n     if (value != i.value) return value < i.value; n     return id < i.id; n   } n   bool operator>(const Item i) const { n     if (value != i.value) return value > i.value; n     return id > i.id; n   } n }; n ostream& operator<<(ostream& out, Item const& item) { n   return out <<  (  << item.id <<  :  << item.value <<  ) ; n } n class MBest { n   long long best_sum; n   multiset<int, greater<int> > le; n   multiset<int> gt; n   void transfer_gt_to_le() { n     assert(!gt.empty()); n     auto it = gt.begin(); n     le.insert(*it); n     best_sum += *it; n     gt.erase(it); n   } n   void transfer_le_to_gt() { n     assert(!le.empty()); n     auto it = le.begin(); n     gt.insert(*it); n     best_sum -= *it; n     le.erase(it); n   } n  n  public: n   MBest() : best_sum(0) {} n   int getM() const { return le.size(); } n   long long query_best_sum() const { return best_sum; } n   void add(int x) { n     le.insert(x); n     best_sum += x; n     transfer_le_to_gt(); n   } n   void remove(int x) { n     auto it_gt = gt.find(x); n     if (it_gt != gt.end()) { n       gt.erase(it_gt); n       return; n     } n     auto it_le = le.find(x); n     assert(it_le != le.end()); n     le.erase(it_le); n     best_sum -= x; n     transfer_gt_to_le(); n   } n   void increaseM() { transfer_gt_to_le(); } n }; n vector<long long> get_psum(const vector<Item>& V) { n   vector<long long> res(int((V).size())); n   for (int i = (1), _b = (int((V).size()) - 1); i <= _b; ++i) n     res[i] = res[i - 1] + V[i].value; n   return res; n } n long long solve(int N, int M, int K, const vector<int>& T, const vector<int>& A, n                 const vector<int>& B) { n   MBest mb; n   vector<Item> alice, bob, both; n   for (int i = 0, _n = (N); i < _n; ++i) { n     Item item = {i + 1, T[i]}; n     if (A[i] && B[i]) n       both.push_back(item); n     else if (A[i]) n       alice.push_back(item); n     else if (B[i]) n       bob.push_back(item); n     else n       mb.add(item.value); n   } n   int minj = min(int((alice).size()), int((bob).size())); n   if (minj + int((both).size()) < K) { n     cout <<  -1  <<   n ; n     return -1; n   } n   sort(alice.begin(), alice.end()); n   sort(bob.begin(), bob.end()); n   sort(both.begin(), both.end()); n   minj = min(minj, K); n   for (int j = (minj), _b = (int((alice).size()) - 1); j <= _b; ++j) n     mb.add(alice[j].value); n   alice.erase(alice.begin() + minj, alice.end()); n   alice.insert(alice.begin(), {0, 0}); n   for (int j = (minj), _b = (int((bob).size()) - 1); j <= _b; ++j) n     mb.add(bob[j].value); n   bob.erase(bob.begin() + minj, bob.end()); n   bob.insert(bob.begin(), {0, 0}); n   both.insert(both.begin(), {0, 0}); n   for (int k = (K - minj), _b = (int((both).size()) - 1); k <= _b; ++k) n     mb.add(both[k].value); n   vector<long long> psum_alice = get_psum(alice); n   vector<long long> psum_bob = get_psum(bob); n   vector<long long> psum_both = get_psum(both); n   long long res = INF; n   int bestj = -1; n   for (int j = minj; j >= 0; --j) { n     int k = K - j; n     if (k >= int((both).size())) break; n     mb.remove(both[k].value); n     int nbooks = K + j; n     if (nbooks <= M) { n       int nmissing = M - nbooks; n       while (mb.getM() < nmissing) mb.increaseM(); n       long long best_other = mb.query_best_sum(); n       long long cur = psum_alice[j] + psum_bob[j] + psum_both[k] + best_other; n       if (res > cur) { n         bestj = j; n         res = cur; n       } n     } n     if (j > 0) { n       mb.add(alice[j].value); n       mb.add(bob[j].value); n     } n   } n   if (bestj < 0) { n     cout <<  -1 n ; n     return -1; n   } n   set<Item> avail; n   for (int i = 0, _n = (N); i < _n; ++i) avail.insert({i + 1, T[i]}); n   vector<int> best_books; n   for (int j = (1), _b = (bestj); j <= _b; ++j) { n     Item item = alice[j]; n     avail.erase(item); n     best_books.push_back(item.id); n   } n   for (int j = (1), _b = (bestj); j <= _b; ++j) { n     Item item = bob[j]; n     avail.erase(item); n     best_books.push_back(item.id); n   } n   for (int k = (1), _b = (K - bestj); k <= _b; ++k) { n     Item item = both[k]; n     avail.erase(item); n     best_books.push_back(item.id); n   } n   assert(int((best_books).size()) <= M); n   while (int((best_books).size()) < M) { n     auto it = avail.begin(); n     best_books.push_back(it->id); n     avail.erase(it); n   } n   cout << res <<   n ; n   long long sum = 0; n   for (int id : best_books) { n     cout << id <<    ; n     sum += T[id - 1]; n   } n   cout <<   n ; n   return res; n } n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int N, M, K; n   cin >> N >> M >> K; n   vector<int> T(N), A(N), B(N); n   for (int i = 0, _n = (N); i < _n; ++i) cin >> T[i] >> A[i] >> B[i]; n   solve(N, M, K, T, A, B); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n char s[111111][12]; n const int OFF = 128, BITS = 7, MASK = 127; n int dy[111111][12], dx[111111][12]; n char qu[5]; n int mx; n void go(int &y, int &x, int upTo) { n   for (int z = 0; z < (int)(mx); ++z) { n     if (s[y][x] ==  ^ ) { n       --y; n       if (y == upTo || y == 0) break; n     } else if (s[y][x] ==  < ) { n       if (--x == 0) break; n     } else { n       if (++x > m) break; n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; ++i) n     scanf( %s , s[i] + 1), s[i][0] = s[i][m + 1] =  f ; n   for (int i = 0; i < (int)(m + 2); ++i) s[0][i] = s[n + 1][i] =  f ; n   mx = (OFF + 2) * m; n   for (int i = OFF; i <= n; i += OFF) n     for (int j = 1; j <= m; ++j) { n       int y = i, x = j; n       go(y, x, i - OFF); n       if (y != i - OFF && y != 0 && x != 0 && x != m + 1) n         dy[i][j] = -1; n       else { n         dy[i][j] = y; n         dx[i][j] = x; n       } n     } n   for (int query = 0; query < (int)(q); ++query) { n     scanf( %s , qu); n     if (qu[0] ==  C ) { n       int Y, X; n       scanf( %d%d%s , &Y, &X, qu); n       s[Y][X] = qu[0]; n       if (Y & MASK) { n         Y &= ~MASK; n         Y += OFF; n       } n       for (int j = 1; j <= m; ++j) { n         int y = Y, x = j; n         go(y, x, Y - OFF); n         if (y != Y - OFF && y != 0 && x != 0 && x != m + 1) n           dy[Y][j] = -1; n         else { n           dy[Y][j] = y; n           dx[Y][j] = x; n         } n       } n     } else { n       int y, x; n       scanf( %d%d , &y, &x); n       if (y & MASK) { n         int upTo = y & ~MASK; n         go(y, x, upTo); n         if (y != upTo && y != 0 && x != 0 && x != m + 1) { n           printf( -1 -1 n ); n           continue; n         } n       } n       bool flag = false; n       while (y != 0 && x != 0 && x != m + 1) { n         int newY = dy[y][x]; n         x = dx[y][x]; n         y = newY; n         if (y == -1) { n           printf( -1 -1 n ); n           flag = true; n           break; n         } n       } n       if (flag) continue; n       printf( %d %d n , y, x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 2e5 + 5; n int p[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int x; n     cin >> x; n     p[i] = x; n   } n   vector<int> path; n   int x = n; n   while (1) { n     path.push_back(x); n     x = p[x]; n     if (x == 0) break; n   } n   reverse(begin(path), end(path)); n   for (int x : path) cout << x <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, res, n, sum, t, w, temp, sz1, sz2; n   while (scanf( %d , &n) == 1) { n     vector<int> v[3]; n     sum = 0; n     for (i = 0; i < n; i++) { n       scanf( %d%d , &t, &w); n       v[t].push_back(w); n       sum += w; n     } n     sort(v[1].begin(), v[1].end()); n     sort(v[2].begin(), v[2].end()); n     sz1 = v[1].size(); n     sz2 = v[2].size(); n     res = 10000000; n     for (i = 0; i <= sz1; i++) { n       for (j = 0; j <= sz2; j++) { n         temp = sum; n         for (k = sz1 - 1; k > sz1 - 1 - i; k--) { n           temp -= v[1][k]; n         } n         for (k = sz2 - 1; k > sz2 - 1 - j; k--) { n           temp -= v[2][k]; n         } n         if (temp <= i + j * 2) res = res < i + j * 2 ? res : i + j * 2; n       } n     } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool valid(long long num, long long den) { n   if (den == 0) { n     return num == 0; n   } else { n     return (num % den) == 0; n   } n } n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } else { n     return gcd(b, a % b); n   } n } n bool valid(long long a, long long b, long long c) { n   if (a == 0 && b == 0) { n     return c == 0; n   } n   if (a < 0) { n     a = -a; n   } n   if (b < 0) { n     b = -b; n   } n   return c % gcd(a, b) == 0; n } n int main() { n   long long ax, ay, bx, by, cx, cy; n   cin >> ax >> ay >> bx >> by >> cx >> cy; n   long long a_x = -ay, a_y = ax; n   long long c_x = -cy, c_y = cx; n   for (long long a = -1; a <= 1; ++a) { n     for (long long a_ = -1; a_ <= 1; ++a_) { n       if (abs(a) + abs(a_) == 1) { n         long long X = bx - a * ax - a_ * a_x; n         long long Y = by - a * ay - a_ * a_y; n         long long num1 = X * c_y - Y * c_x; n         long long den1 = cx * c_y - cy * c_x; n         long long num2 = X * cy - Y * cx; n         long long den2 = c_x * cy - c_y * cx; n         bool ok; n         if (den1 != 0 && den2 != 0) { n           ok = (num1 % den1) == 0 && (num2 % den2) == 0; n         } else { n           if (cx == 0 && c_x == 0) { n             ok = X == 0 && valid(cy, c_y, Y); n           } else if (cy == 0 && c_y == 0) { n             ok = Y == 0 && valid(cx, c_x, X); n           } else { n             if (cx == 0) { n               ok = (X % c_x) == 0 && (Y % c_y) == 0 && X / c_x == Y / c_y; n             } else if (c_x == 0) { n               ok = (X % cx) == 0 && (Y % cy) == 0 && X / cx == Y / cy; n             } else { n               ok = valid(cx, c_x, X) && X * cy == Y * cx; n             } n           } n         } n         if (ok) { n           cout <<  YES  << endl; n           return 0; n         } n       } n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double pi = 4 * atan(1.0); n double eps = 1e-9; n string stringmin =  ! ; n string stringmax =  ~ ; n int main() { n   cout.precision(14); n   int n; n   cin >> n; n   vector<int> v(n), vlen(n); n   for (auto &i1 : v) cin >> i1; n   for (int i = 0; i < n; i++) { n     int f1 = 0; n     int q = i; n     for (int j = 0; j < 101; j++) { n       q = v[q] - 1; n       if (q == i) { n         f1 = 1; n         vlen[i] = j + 1; n         break; n       } n     } n     if (!f1) { n       cout << -1; n       return 0; n     } n   } n   for (auto &i1 : vlen) n     if ((i1 & 1) == 0) i1 >>= 1; n   long long ans = 1; n   for (int i = 2; i < 110; i++) { n     int maxcnt = 0; n     for (auto &i1 : vlen) { n       int cnt = 0; n       while (i1 % i == 0) { n         cnt++; n         i1 /= i; n       } n       maxcnt = max(cnt, maxcnt); n     } n     for (int j = 0; j < maxcnt; j++) ans *= i; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct S { n   int dist, u, v; n   S(int dd, int uu, int vv) { n     dist = dd; n     u = uu; n     v = vv; n   } n   bool operator<(const S& a) const { n     if (dist != a.dist) return dist < a.dist; n     return u < a.u; n   } n   bool operator>(const S& a) const { n     if (dist != a.dist) return dist > a.dist; n     return u > a.u; n   } n }; n vector<pair<int, int> > pA; n vector<pair<int, int> > pB; n vector<int> A; n vector<int> B; n pair<int, int> res[100007]; n int main() { n   int N, M; n   scanf( %d %d  , &N, &M); n   ; n   for (int i = (0); i < (M); i++) { n     int a, b; n     scanf( %d %d  , &a, &b); n     ; n     if (b) n       A.push_back(a); n     else n       B.push_back(a); n     if (b) n       pA.push_back(make_pair(a, i)); n     else n       pB.push_back(make_pair(a, i)); n   } n   sort(A.begin(), A.end()); n   sort(B.begin(), B.end()); n   sort(pA.begin(), pA.end()); n   sort(pB.begin(), pB.end()); n   priority_queue<S, vector<S>, greater<S> > pq; n   for (int i = (0); i < ((int)A.size() - 1); i++) n     pq.push(S(A[i + 1], i, i + 1)); n   for (int m = (0); m < (B.size()); m++) { n     S s = pq.top(); n     pq.pop(); n     if (B[m] < s.dist) { n       cout << -1 << endl; n       return 0; n     } n     res[pB[m].second] = make_pair(s.u, s.v); n     s.v++; n     if (s.v < A.size()) { n       s.dist -= A[s.v - 1]; n       s.dist += A[s.v]; n       pq.push(s); n     } n   } n   for (int i = (0); i < (A.size()); i++) n     res[pA[i].second] = make_pair(i, N - 1); n   for (int i = (0); i < (M); i++) n     cout << res[i].first + 1 <<     << res[i].second + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, s1 = 0, s2 = 0; n     scanf( %d , &n); n     int a[100] = {0}; n     for (int i = 0; i < n; i++) { n       scanf( %d , a + i); n       if (a[i] % 2) n         s1++; n       else n         s2++; n     } n     if (s1 && s2) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long l1, r1, l2, r2, k, l, r; n   cin >> l1 >> r1 >> l2 >> r2 >> k; n   l = max(l1, l2); n   r = min(r1, r2); n   if (r < l) n     cout << 0; n   else if (l <= k && k <= r) n     cout << r - l; n   else n     cout << r - l + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int putere(long long int a, long long int b) { n   if (b == 0) { n     return 1; n   } else { n     if (b == 1) n       return a % 1000000007; n     else { n       long long int r; n       r = putere(a, b / 2); n       r = (r * r) % 1000000007; n       if (b % 2 == 0) n         return r; n       else n         return (r * a) % 1000000007; n     } n   } n } n int main() { n   long long int x, k, nr1, nr2; n   scanf( %lld%lld , &x, &k); n   if (x == 0) n     printf( 0 ); n   else { n     x = x % 1000000007; n     nr1 = putere(2, k); n     nr2 = nr1 * x * 2 % 1000000007; n     printf( %lld , (nr2 - nr1 + 1 + 1000000007) % 1000000007); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int k, n, first = -1, sum = 0; n   cin >> n >> k; n   long long int a[n]; n   long long int ans = 0; n   for (long long int i = 0; i < n; ++i) cin >> a[i]; n   string s; n   cin >> s; n   int flag = 0; n   for (long long int i = 0; i < n; ++i) { n     if (s[i] == s[i + 1]) { n       if (first == -1) first = i; n       flag = 1; n     } else if (flag) { n       sort(a + first, a + i + 1); n       long long int mini = min(k, i - first + 1); n       for (long long int j = 0; j < mini; ++j) { n         sum += a[i - j]; n       } n       flag = 0; n       first = -1; n     } else { n       sum += a[i]; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct DSU { n   long long int parent[500001], connected[500001]; n   void intialize(int x) { n     for (long long int i = 1; i <= x; i++) { n       parent[i] = i; n       connected[i] = 1; n     } n   } n   long long int getparent(long long int k) { n     while (k != parent[k]) { n       parent[k] = parent[parent[k]]; n       k = parent[k]; n     } n     return k; n   } n   long long int getsize(long long int k) { return connected[getparent(k)]; } n   void unionn(long long int u, long long int v) { n     long long int parent1 = getparent(u), parent2 = getparent(v); n     if (parent1 == parent2) return; n     if (connected[parent1] > connected[parent2]) swap(parent1, parent2); n     connected[parent2] += connected[parent1]; n     connected[parent1] = 0; n     parent[parent1] = parent[parent2]; n   } n }; n DSU dsu; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     dsu.intialize(n + 1); n     for (long long int i = 1; i <= n; i++) { n       long long int x; n       cin >> x; n       dsu.unionn(i, x); n     } n     for (long long int i = 1; i <= n; i++) cout << dsu.getsize(i) <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string str; n bool judge(char c) { n   return (c ==  _  || islower(c) || isdigit(c) || isupper(c)); n } n int main() { n   int cnt = 0, count = 0, type = 0, len; n   cin >> str; n   type = 0; n   len = str.length(); n   for (int i = 0; i < len; i++) { n     char c = str[i]; n     if (c ==  @ ) { n       if (type == 1) { n         cout <<  NO  << endl; n         return 0; n       } n       type = 1; n       if (cnt < 1 || cnt > 16) { n         cout <<  NO  << endl; n         return 0; n       }; n       cnt = 0; n     } else if (c ==  / ) { n       if (type == 2) { n         cout <<  NO  << endl; n         return 0; n       } n       type = 2; n       if (cnt < 1 || cnt > 16) { n         cout <<  NO  << endl; n         return 0; n       } n       if (count < 1 || count > 32) { n         cout <<  NO  << endl; n         return 0; n       } n       cnt = 0; n     } else if (c ==  . ) { n       if (type != 1) { n         cout <<  NO  << endl; n         return 0; n       } n       if (cnt < 1 || cnt > 16) { n         cout <<  NO  << endl; n         return 0; n       } n       cnt = 0; n     } else if (c > 127 || c < 33) { n       cout <<  NO  << endl; n       return 0; n     } else if (type == 0) { n       if (judge(c)) { n         cnt++; n         continue; n       } else { n         cout <<  NO  << endl; n         return 0; n       } n     } else if (type == 1) { n       if (judge(c)) { n         cnt++; n         count++; n         continue; n       } else { n         cout <<  NO  << endl; n         return 0; n       } n     } else if (type == 2) { n       if (judge(c)) { n         cnt++; n         continue; n       } else { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   if (cnt < 0 || cnt > 16) { n     cout <<  NO  << endl; n     return 0; n   } n   if (type >= 1 && cnt > 0) { n     cout <<  YES  << endl; n     return 0; n   } else { n     cout <<  NO  << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n struct cc { n   int x, y, z; n   bool operator<(const cc &t) const { n     if (y != t.y) n       return y > t.y; n     else n       return x > t.x; n   } n } a[N]; n struct xx { n   int w, id; n } q[N]; n long long dp[N]; n int main() { n   int n, m, i, j, k, ans, num, sum; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].z); n   } n   sort(a + 1, a + n + 1); n   long long maxx = dp[1] = a[1].z; n   q[1] = {a[1].x, 1}; n   int l = 1, r = 1; n   for (i = 2; i <= n; i++) { n     while (l <= r && q[r].w >= a[i].y) r--; n     dp[i] = dp[q[r].id] + a[i].z; n     q[++r] = {a[i].x, i}; n     maxx = max(maxx, dp[i]); n   } n   printf( %lld n , maxx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[300005]; n long long dp[300005], ans = -LONG_LONG_MAX; n vector<int> G[300005]; n int cnt = 0; n void dfs(int now, int fa) { n   for (int k : G[now]) { n     if (k != fa) { n       dfs(k, now); n       dp[now] += max((long long)0, dp[k]); n     } n   } n   ans = max(ans, dp[now]); n } n void dfs2(int now, int fa) { n   for (int k : G[now]) { n     if (k != fa) { n       dfs2(k, now); n       dp[now] += max((long long)0, dp[k]); n     } n   } n   if (dp[now] == ans) { n     dp[now] = 0; n     cnt++; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> num[i]; n     dp[i] = num[i]; n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   dfs(1, -1); n   for (int i = 1; i <= n; i++) dp[i] = num[i]; n   dfs2(1, -1); n   cout << cnt * ans <<     << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int kmp(string s) { n   vector<int> lps(s.size(), 0); n   for (int i = 1; i < s.size(); ++i) { n     int index = lps[i - 1]; n     while (index > 0 && s[i] != s[index]) index = lps[index - 1]; n     if (s[i] == s[index]) n       lps[i] = index + 1; n     else n       lps[i] = index; n   } n   return lps[lps.size() - 1]; n } n int func(string s) { n   string t = s +  # ; n   reverse(s.begin(), s.end()); n   t += s; n   return kmp(t); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int l = 0, n = s.length(); n     for (int i = 0, j = n - 1; i < j; i++, j--) { n       if (s[i] == s[j]) n         l++; n       else n         break; n     } n     string remaining = s.substr(l, n - 2 * l); n     string ans = s.substr(0, l); n     if (remaining.size()) { n       int lp = func(remaining); n       reverse(remaining.begin(), remaining.end()); n       int ls = func(remaining); n       if (lp > ls) { n         reverse(remaining.begin(), remaining.end()); n         ans += remaining.substr(0, lp); n       } else n         ans += remaining.substr(0, ls); n     } n     ans += s.substr(n - l, l); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long m) { n   long long r = 1; n   for (; y; y >>= 1) { n     if (y & 1) r = r * x % m; n     x = x * x % m; n   } n   return r; n } n const long long N = 105, M = 105; n long long a[N][M]; n int run_test() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) cin >> a[i][j]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) { n       if (a[i][j] % 2 != (i + j) % 2) a[i][j]++; n     } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) cout << a[i][j] <<    ; n     cout <<   n ; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   srand(time(NULL)); n   ; n   long long t; n   t = 1; n   cin >> t; n   while (t--) { n     run_test(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   if (n < 6) n     printf( -1 n ); n   else { n     printf( 1 2 n2 3 n2 4 n3 5 n3 6 n ); n     for (int i = 7; i <= n; i++) printf( 3 %d n , i); n   } n   for (int i = 2; i <= n; i++) printf( %d %d n , i - 1, i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long int LINF = 0x3f3f3f3f3f3f3f3fll; n const long double pi = acos(-1); n const int MOD = 1e9 + 7; n int main() { n   int n, i; n   double ans = 0.0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     ans += 1 / (float)i; n   } n   printf( %.6lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int maxn = 120000; n int p[maxn], sz[maxn], top[maxn], hs[maxn], dfn[maxn], ssz[maxn]; n namespace segtree { n bool marked[maxn << 2]; n int sum[maxn << 2], mss[maxn << 2]; n void push_down(int v, int tl, int tr) { n   if (marked[v]) { n     sum[(v << 1)] = -(((tl + tr) >> 1) - tl + 1); n     sum[(v << 1 | 1)] = -(tr - ((tl + tr) >> 1)); n     mss[(v << 1)] = mss[(v << 1 | 1)] = -1; n     marked[(v << 1)] = marked[(v << 1 | 1)] = true; n     marked[v] = false; n   } n } n void push_up(int v) { n   sum[v] = sum[(v << 1)] + sum[(v << 1 | 1)]; n   mss[v] = max(mss[(v << 1 | 1)], mss[(v << 1)] + sum[(v << 1 | 1)]); n } n void build(int v, int tl, int tr) { n   if (tl == tr) n     sum[v] = mss[v] = -1; n   else { n     build((v << 1), tl, ((tl + tr) >> 1)); n     build((v << 1 | 1), ((tl + tr) >> 1) + 1, tr); n     push_up(v); n   } n } n void add(int v, int tl, int tr, int p, int x) { n   if (tl == tr) n     sum[v] = mss[v] += x; n   else { n     push_down(v, tl, tr); n     if (p <= ((tl + tr) >> 1)) n       add((v << 1), tl, ((tl + tr) >> 1), p, x); n     else n       add((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, p, x); n     push_up(v); n   } n } n int tmp; n int query(int v, int tl, int tr, int l, int r) { n   static int res; n   if (v == 1) res = -1; n   if (tl >= l and tr <= r) { n     res = max(res, mss[v] + tmp); n     tmp += sum[v]; n   } else { n     push_down(v, tl, tr); n     if (r > ((tl + tr) >> 1)) n       query((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, l, r); n     if (l <= ((tl + tr) >> 1)) query((v << 1), tl, ((tl + tr) >> 1), l, r); n   } n   return res; n } n void set(int v, int tl, int tr, int l, int r) { n   if (tl >= l and tr <= r) { n     sum[v] = -(tr - tl + 1); n     mss[v] = -1; n     marked[v] = true; n   } else { n     push_down(v, tl, tr); n     if (l <= ((tl + tr) >> 1)) set((v << 1), tl, ((tl + tr) >> 1), l, r); n     if (r > ((tl + tr) >> 1)) set((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, l, r); n     push_up(v); n   } n } n }  // namespace segtree n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 2; i <= n; i += 1) cin >> p[i]; n   for (int i = n; i >= 2; i -= 1) sz[p[i]] += sz[i] += 1; n   for (int i = 2; i <= n; i += 1) n     if (sz[i] > sz[hs[p[i]]]) hs[p[i]] = i; n   for (int i = 1; i <= n; i += 1) n     if (i == hs[p[i]]) { n       top[i] = top[p[i]]; n       dfn[i] = dfn[p[i]] + 1; n     } else { n       top[i] = i; n       dfn[i] = dfn[p[i]] + ssz[p[i]] + sz[hs[p[i]]] + 1; n       ssz[p[i]] += sz[i]; n     } n   segtree::build(1, 1, n); n   for (int t, v; q; q -= 1) { n     cin >> t >> v; n     if (t == 1) n       segtree::add(1, 1, n, dfn[v], 1); n     else if (t == 3) { n       int res = -1; n       segtree::tmp = 0; n       for (int x = v; x; x = p[top[x]]) n         res = max(res, segtree::query(1, 1, n, dfn[top[x]], dfn[x])); n       cout << (res >= 0 ?  black n  :  white n ); n     } else { n       segtree::set(1, 1, n, dfn[v], dfn[v] + sz[v] - 1); n       int res = -1; n       segtree::tmp = 0; n       for (int x = v; x; x = p[top[x]]) n         res = max(res, segtree::query(1, 1, n, dfn[top[x]], dfn[x])); n       if (res >= 0) segtree::add(1, 1, n, dfn[v], -res - 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   if (n == 0) { n     cout << 0 << endl; n   } else if ((n + 1) % 2 == 0) { n     cout << (n + 1) / 2 << endl; n   } else { n     cout << n + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   int a[n + 1]; n   int ev = 2, od = 1; n   int tev = 0, tod = 0; n   int tot = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n     if (a[i] % 2 == 0) n       tev++; n     else n       tod++; n   } n   for (int i = 0; i < n; i++) { n     if (mp[a[i]] > 1) { n       if (a[i] % 2) { n         if (tod > tev) { n           while (mp[ev] != 0) ev += 2; n           if (ev > m) return cout << -1, 0; n           mp[ev] = 1; n           mp[a[i]]--; n           a[i] = ev; n           tev++; n           tod--; n           tot++; n         } else { n           while (mp[od] != 0) od += 2; n           if (od > m) return cout << -1, 0; n           mp[od] = 1; n           mp[a[i]]--; n           a[i] = od; n           tot++; n         } n       } else { n         if (tod < tev) { n           while (mp[od] != 0) od += 2; n           if (od > m) return cout << -1, 0; n           mp[od] = 1; n           mp[a[i]]--; n           a[i] = od; n           tev--; n           tod++; n           tot++; n         } else { n           while (mp[ev] != 0) ev += 2; n           if (ev > m) return cout << -1, 0; n           mp[ev] = 1; n           mp[a[i]]--; n           a[i] = ev; n           tot++; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[i] % 2) { n       if (tev < tod) { n         tot++; n         while (mp[ev] != 0) ev += 2; n         if (ev > m) return cout << -1, 0; n         mp[ev] = 1; n         a[i] = ev; n         tod--; n         tev++; n       } n     } else { n       if (tev > tod) { n         tot++; n         while (mp[od] != 0) od += 2; n         if (od > m) return cout << -1, 0; n         mp[od] = 1; n         a[i] = od; n         tod++; n         tev--; n       } n     } n   } n   cout << tot << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n long long power(long long x, unsigned long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) { n       res = res * x; n     } n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long powermod(long long x, unsigned long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % p; n     } n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long min(long long a, long long b) { return a < b ? a : b; } n long long max(long long a, long long b) { return a > b ? a : b; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void snow() { n   long long a, b, n; n   cin >> a >> b >> n; n   while (n--) { n     long long l, t, m; n     cin >> l >> t >> m; n     l--; n     long long ans = l; n     long long r = 1e9; n     long long faci = m * t; n     if (t < (a + l * (b))) { n       cout << -1 <<   n ; n       continue; n     } n     long long start = a + l * (b); n     long long ll = l; n     while (l <= r) { n       long long mid = (l + r) / 2; n       long long height = a + mid * (b); n       long long sum = n           start * (mid - ll + 1) + b * ((mid - ll) * (mid - ll + 1)) / 2; n       if (height <= t && sum <= m * t) { n         ans = mid; n         l = mid + 1; n       } else { n         r = mid - 1; n       } n     } n     cout << ans + 1 <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   t = 1; n   while (t--) snow(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n inline T max(T &a, U &b) { n   return a > b ? a : b; n } n template <class T, class U> n inline T min(T &a, U &b) { n   return a < b ? a : b; n } n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n class CLASS_NAME { n  public: n   int method_NAME() {} n }; n int main() { n   int n; n   string a, b; n   cin >> n; n   for (__typeof(0) i = 0; i <= n - 1; i++) a.push_back( 0 ); n   for (__typeof(0) i = 0; i <= n - 1; i++) b.push_back( 0 ); n   int k = n / 2; n   for (__typeof(0) i = 0; i <= k - 1; i++) { n     a[i] =  1 ; n     b[i] =  1 ; n   } n   vector<int> v, arra, arrb; n   for (__typeof(0) i = 0; i <= n - 1; i++) { n     int x, y; n     cin >> x >> y; n     arra.push_back(x); n     arrb.push_back(y); n     v.push_back(x); n     v.push_back(y); n   } n   sort(v.begin(), v.end()); n   int tmp = v[n - 1]; n   for (__typeof(0) i = 0; i <= n - 1; i++) { n     if (arra[i] <= tmp) a[i] =  1 ; n     if (arrb[i] <= tmp) b[i] =  1 ; n   } n   cout << a << endl << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     for (int i = n; i >= 1; i--) cout << a[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T in() { n   char ch; n   T n = 0; n   bool ng = false; n   while (1) { n     ch = getchar(); n     if (ch ==  - ) { n       ng = true; n       ch = getchar(); n       break; n     } n     if (ch >=  0  && ch <=  9 ) break; n   } n   while (1) { n     n = n * 10 + (ch -  0 ); n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n   } n   return (ng ? -n : n); n } n template <typename T> n inline T POW(T B, T P) { n   if (P == 0) return 1; n   if (P & 1) n     return B * POW(B, P - 1); n   else n     return (POW(B, P / 2) * POW(B, P / 2)); n } n template <typename T> n inline T Gcd(T a, T b) { n   if (a < 0) return Gcd(-a, b); n   if (b < 0) return Gcd(a, -b); n   return (b == 0) ? a : Gcd(b, a % b); n } n template <typename T> n inline T Lcm(T a, T b) { n   if (a < 0) return Lcm(-a, b); n   if (b < 0) return Lcm(a, -b); n   return a * (b / Gcd(a, b)); n } n long long Bigmod(long long base, long long power, long long MOD) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret = (ret * base) % MOD; n     base = (base * base) % MOD; n     power >>= 1; n   } n   return ret; n } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n template <typename T> n long long int isLeft(T a, T b, T c) { n   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); n } n long long ModInverse(long long number, long long MOD) { n   return Bigmod(number, MOD - 2, MOD); n } n bool isConst(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n int toInt(string s) { n   int sm; n   stringstream second(s); n   second >> sm; n   return sm; n } n char s[5007]; n unordered_map<string, int> mp; n vector<string> A[5007]; n int dp[5007][2001]; n int n, m; n int koto(string p, int pos, int b) { n   if (p[0] ==  0  || p[0] ==  1 ) return (p[pos] -  0 ); n   if (p[0] ==  ? ) return b; n   int id = mp[p]; n   return dp[id][pos]; n } n void solve(int pos, int b) { n   for (int i = 1; i <= n; i++) { n     if (A[i].size() == 3) { n       dp[i][pos] = koto(A[i][2], pos, b); n     } else { n       int x = koto(A[i][2], pos, b); n       int y = koto(A[i][4], pos, b); n       if (A[i][3][0] ==  A ) x = x & y; n       if (A[i][3][0] ==  O ) x = x | y; n       if (A[i][3][0] ==  X ) x = x ^ y; n       dp[i][pos] = x; n     } n   } n } n int main() { n   cin >> n >> m; n   getchar(); n   for (int i = 1; i < n + 1; i++) { n     gets(s); n     vector<string> v; n     string tp; n     int ln = strlen(s); n     for (int j = 0; j < ln; j++) { n       if (s[j] ==    ) { n         v.push_back(tp); n         tp.clear(); n       } else { n         tp += s[j]; n       } n     } n     v.push_back(tp); n     mp[v[0]] = i; n     for (int j = 0; j < v.size(); j++) A[i].push_back(v[j]); n   } n   string x, y; n   for (int i = 0; i < m; i++) { n     solve(i, 0); n     int cnt = 0; n     for (int j = 1; j < n + 1; j++) { n       cnt += dp[j][i]; n     } n     solve(i, 1); n     int cnt1 = 0; n     for (int j = 1; j < n + 1; j++) { n       cnt1 += dp[j][i]; n     } n     if (cnt >= cnt1) { n       y +=  0 ; n     } else n       y +=  1 ; n     if (cnt <= cnt1) { n       x +=  0 ; n     } else n       x +=  1 ; n   } n   cout << x << endl; n   cout << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n const int mod = 1e9 + 7; n const double eps = 1e-8; n const double PI = acos(-1.0); n long long qpow(long long a, long long b, long long p) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % p; n     a = a * a % p; n     b >>= 1; n   } n   return res; n } n long long a[N]; n int main() { n   std::ios::sync_with_stdio(false); n   int n, p, k; n   while (cin >> n >> p >> k) { n     for (int i = 1; i <= n; i++) cin >> a[i]; n     map<long long, long long> mp; n     for (int i = 1; i <= n; i++) n       mp[(qpow(a[i], 4, p) - k % p * a[i] % p + p) % p]++; n     map<long long, long long>::iterator it = mp.begin(); n     long long ans = 0; n     for (it; it != mp.end(); it++) { n       ans += (*it).second * ((*it).second - 1) / 2; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     vector<int> arr(n); n     vector<pair<int, int>> vpr(n); n     for (long long i = 0; i <= n - 1; i++) { n       cin >> arr[i]; n       vpr[i] = {arr[i], i}; n     } n     sort(vpr.begin(), vpr.end()); n     int j = 0; n     vector<int> trfs(n); n     unordered_multiset<int> mst; n     for (int i = 0; i < n; ++i) { n       if (i > 0 && vpr[i].first != vpr[i - 1].first) { n         j++; n       } n       trfs[vpr[i].second] = j; n       mst.insert(j); n     } n     vector<int> dp1(n, 1), dp2(n), dp3(n); n     unordered_map<int, int> pos; n     for (int i = 0; i < n; ++i) { n       if (mst.count(trfs[i])) mst.erase(mst.find(trfs[i])); n       if (pos.count(trfs[i] - 1)) { n         if (!pos.count(trfs[i])) { n           dp2[i] = dp1[pos[trfs[i] - 1]] + 1; n           if (!mst.count(trfs[i] - 1)) { n             dp2[i] = max(dp2[i], dp2[pos[trfs[i] - 1]] + 1); n           } n         } n         if (!mst.count(trfs[i] - 1)) { n           dp3[i] = dp2[pos[trfs[i] - 1]] + 1; n         } n         dp3[i] = max(dp3[i], dp1[pos[trfs[i] - 1]] + 1); n       } n       if (pos.count(trfs[i])) { n         dp1[i] = dp1[pos[trfs[i]]] + 1; n         dp3[i] = max(dp3[i], dp3[pos[trfs[i]]] + 1); n         dp2[i] = max(dp2[i], dp2[pos[trfs[i]]] + 1); n       } n       dp2[i] = max(dp2[i], dp1[i]); n       dp3[i] = max(dp3[i], dp2[i]); n       pos[trfs[i]] = i; n     } n     cout << n - *max_element(dp3.begin(), dp3.end()) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const mod = 1000000007; n long long const md = 998244353; n long long mypow(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n ifstream in( txt.in ); n ofstream out( txt.out ); n int main() { n   cout << fixed << setprecision(12); n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   string s; n   cin >> s; n   long long n; n   cin >> n; n   while (n--) { n     long long l, r, k; n     cin >> l >> r >> k; n     --l; n     --r; n     int u = r - l + 1; n     k %= u; n     char c[10005]; n     int t = r - k + 1; n     for (int i = 1; i <= k; ++i) { n       c[i] = s[t]; n       ++t; n     } n     t = l; n     int i = k + 1; n     while (t < r - k + 1) { n       c[i] = s[t]; n       ++t; n       ++i; n     } n     t = 1; n     for (int i = l; i <= r; ++i) s[i] = c[t], ++t; n   } n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n template <typename T, typename U> n istream& operator>>(istream& is, pair<T, U>& p) { n   is >> p.first >> p.second; n   return is; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, pair<T, U>& p) { n   os << p.first <<     << p.second; n   return os; n } n int main() { n   long long b; n   cin >> b; n   vector<pair<long long, long long>> v; n   long long p = 2; n   while (p * p <= b) { n     if (b % p == 0) { n       v.emplace_back(p, 0); n       while (b % p == 0) { n         b /= p; n         v.back().second++; n       } n     } n     ++p; n   } n   if (b != 1) v.emplace_back(b, 1); n   long long c = 1; n   for (auto p : v) { n     c *= p.second + 1; n   } n   cout << c <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string S, T; n int L = -1, R = -1; n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> S >> T; n   x = 0; n   for (i = 0; i < T.size(); i++) { n     if (T[i] == S[x]) { n       x++; n       if (x == S.size()) { n         L = i; n         break; n       } n     } n   } n   x = S.size() - 1; n   for (i = T.size() - 1; i >= 0; i--) { n     if (T[i] == S[x]) { n       x--; n       if (x == -1) { n         R = i; n         break; n       } n     } n   } n   if (L >= 0 && R >= 0 && L <= R) n     cout << R - L << endl; n   else n     cout << 0 << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false); n   for (i = 0; i < argc - 1; i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n string A; n string B; n int main() { n   ios_base::sync_with_stdio(NULL); n   cout.tie(NULL); n   cin.tie(NULL); n   cin >> N; n   cin >> A >> B; n   int answer = 0; n   for (int i = 0; i < N - 1; ++i) { n     if (A[i] == B[i]) continue; n     if (A[i + 1] == B[i] && A[i] == B[i + 1]) n       swap(A[i], A[i + 1]); n     else n       A[i] = B[i]; n     ++answer; n   } n   if (A[N - 1] != B[N - 1]) { n     ++answer; n     A[N - 1] = B[N - 1]; n   } n   cerr << A << endl; n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> toOrder; n map<int, set<int>> offersByDays; n map<int, set<int>> offers; n int totSum; n int bSearch(int a, int b) { n   if (a == b) { n     return a; n   } n   int curSum = totSum; n   int mid = (a + b) / 2; n   int money = 0; n   for (int i = 0; i <= mid; i++) { n     money++; n     if (offersByDays.count(i) == 0) { n       continue; n     } n     for (auto offer : offersByDays[i]) { n       if (money == 0) { n         break; n       } n       set<int>::iterator itr = offers[offer].lower_bound(mid); n       if (itr != offers[offer].end() && *itr == mid) { n         if (i != mid) { n           continue; n         } n       } else { n         itr = prev(itr); n         if (i != *itr) { n           continue; n         } n       } n       if (money >= toOrder[offer]) { n         money -= toOrder[offer]; n         curSum -= toOrder[offer]; n       } else { n         curSum -= money; n         money = 0; n       } n     } n   } n   if (money >= 2 * curSum) { n     return bSearch(a, mid); n   } n   return bSearch(mid + 1, b); n } n int main() { n   int n, m; n   cin >> n >> m; n   totSum = 0; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     toOrder.push_back(temp); n     totSum += temp; n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     if (offersByDays.count(a) == 0) { n       offersByDays.insert({a, {b}}); n     } else { n       offersByDays[a].insert(b); n     } n     if (offers.count(b) == 0) { n       offers.insert({b, {a}}); n     } else { n       offers[b].insert(a); n     } n   } n   cout << bSearch(0, int(4e5)) + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, n, d, h; n   cin >> n >> d >> h; n   if (n >= d + 1 && d >= h + 1 && d <= 2 * h && h + 1 <= n && d >= 2) { n     for (i = 1; i <= h; i++) { n       cout << i <<     << i + 1 << endl; n     } n     i++; n     int f = 0; n     if (d - h > 0) { n       f = 1; n       cout <<  1   << i << endl; n       int temp = d - h - 1; n       for (int j = 1; j <= temp; j++, i++) cout << i <<     << i + 1 << endl; n     } n     if (f == 1) i++; n     for (int j = 1; j <= (n - d - 1); j++, i++) { n       cout <<  1   << i << endl; n     } n   } else if (n >= d + 1 && d == h && d <= 2 * h && h + 1 <= n && d >= 2) { n     for (i = 1; i <= h; i++) cout << i <<     << i + 1 << endl; n     i++; n     int k = i; n     for (int j = 1; j <= (n - k + 1); j++, i++) { n       cout <<  2   << i << endl; n     } n   } else if (d == 1 && n == 2 && h == 1) { n     cout <<  1   n          <<  2  << endl; n   } else n     cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const long long MOD = 1e6 + 7; n long long gcd(long long a, long long b) { n   if (a % b) return gcd(b, a % b); n   return b; n } n long long n, m; n long long a[200005], c[6]; n string s[200005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long k = *max_element(a, a + n); n   long long s = accumulate(a, a + n, 0LL); n   if (s % 2 || k > s / 2) n     cout <<  NO ; n   else n     cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   std::vector<int> power(n, -1), school(n, -1), chosen(k, -1); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> power[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> school[i]; n   } n   for (int i = 0; i < k; i++) { n     cin >> chosen[i]; n   } n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       if (power[j] > power[chosen[i] - 1] && n           school[chosen[i] - 1] == school[j] && chosen[i] - 1 != j) { n         ans++; n         break; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define int long long n  n int n, k; n  n int mod(int i){return (i%n+n)%n;} n  n int q(int x){ n  x = mod(x) + 1; n  cout <<  ?   << x << endl; n  int ret; cin >> ret; n  return ret; n } n  n int32_t main(){ n  cin >> n >> k; n  int i=0, j=0, x; n  int lo, hi; n  while(1){ n   x = q(j); n   if(x > k){lo = j-i-i, hi = j; break;} n   if(x < k){lo = j, hi = j+i+i; break;} n   j += i; ++i; n  } n  while(lo <= hi){ n   int mid = (lo+hi)/2; n   int x = q(mid); n   if(x == k){ n    cout <<  !   << mod(mid) + 1 << endl; n    exit(0); n   } n   if(x < k) lo = mid+1; n   else hi = mid-1; n  } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n char arr[N][N] = {}; n bool vis[N][N] = {}; n int n, m; n inline bool valid(int nx, int ny) { n   return (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny] && n           arr[nx][ny] ==  # ); n } n void dfs(int xx, int yy) { n   vis[xx][yy] = true; n   for (int i = -1; i <= (int)1; ++i) { n     for (int j = -1; j <= (int)1; ++j) { n       if (j != i && (!j || !i)) { n         int nx = xx + i; n         int ny = yy + j; n         if (valid(nx, ny)) dfs(nx, ny); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0), n       cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = 1; i <= (int)n; ++i) n     for (int j = 1; j <= (int)m; ++j) cin >> arr[i][j]; n   int z = 0; n   for (int x = 1; x <= (int)n; ++x) { n     for (int y = 1; y <= (int)m; ++y) { n       if (arr[x][y] ==  # ) { n         z++; n         arr[x][y] =  . ; n         int cmp = 0; n         memset(vis, 0, sizeof(vis)); n         ; n         for (int i = 1; i <= (int)n; ++i) { n           for (int j = 1; j <= (int)m; ++j) { n             if (!vis[i][j] && arr[i][j] ==  # ) { n               dfs(i, j); n               ++cmp; n             } n           } n         } n         if (cmp >= 2) return cout << 1 <<   n , 0; n         arr[x][y] =  # ; n       } n     } n   } n   if (z <= 2) return cout << -1 <<   n , 0; n   return cout << 2 <<   n , 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n int gi() { n   int x = 0, o = 1; n   char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) o = -1, ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n int n, m, f[N], id[N], dfn[N], tim, fa[N], fe[N], u[N], v[N], w[N]; n bool in[N]; n vector<pair<int, int> > E[N]; n void dfs(int u) { n   dfn[u] = ++tim; n   for (auto e : E[u]) { n     int v = e.first; n     if (v != fa[u]) { n       if (!dfn[v]) n         fa[v] = u, fe[v] = e.second, dfs(v); n       else if (dfn[v] > dfn[u]) { n         int x = v; n         vector<int> vec = {e.second}; n         do vec.push_back(fe[x]), x = fa[x]; n         while (x != u); n         rotate(vec.begin(), min_element(vec.begin(), vec.end()), vec.end()); n         int p = max_element(vec.begin(), vec.end()) - vec.begin(); n         bool fl = 1; n         for (int i = 0; i < p && fl; i++) n           if (vec[i] > vec[i + 1]) fl = 0; n         for (int i = p; i < int(vec.size()) - 1 && fl; i++) n           if (vec[i] < vec[i + 1]) fl = 0; n         if (fl) id[vec[0]] = vec[p]; n       } n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) n     u[i] = gi(), v[i] = gi(), E[u[i]].push_back(make_pair(v[i], i)), n     E[v[i]].push_back(make_pair(u[i], i)); n   dfs(1); n   for (int i = 1; i <= n; i++) f[i] = 1; n   for (int i = m; i; i--) { n     w[i] = f[u[i]] + f[v[i]]; n     f[u[i]] = f[v[i]] = w[i] - w[id[i]]; n   } n   for (int i = 1; i <= n; i++) cout << f[i] - 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int i, n, k; n   cin >> n >> k; n   if ((k == 1 && n > 1) || k > n) { n     cout <<  -1 ; n   } else { n     if (n == 1 && k == 1) { n       cout <<  a ; n       return 0; n     } n     string str; n     for (i = 0; i < n - (k - 2); i++) n       if (i % 2 == 0) n         str.push_back( a ); n       else n         str.push_back( b ); n     char ch =  c ; n     for (; i < n; i++) str.push_back(ch++); n     cout << str; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5, maxh = 3; n int mod[maxh] = {(int)1e9 + 7, (int)1e9 + 9, (int)998244353}; n struct data { n   int v[maxh]; n   data(int a = 0, int b = 0, int c = 0) { n     v[0] = a; n     v[1] = b; n     v[2] = c; n   } n   int& operator[](int w) { return v[w]; } n   friend data operator+(data a, data b) { n     for (int i = 0; i < maxh; ++i) a[i] = (a[i] + b[i]) % mod[i]; n     return a; n   } n   friend data operator+(data a, int b) { n     for (int i = 0; i < maxh; ++i) a[i] = (a[i] + b) % mod[i]; n     return a; n   } n   friend data operator*(data a, data b) { n     for (int i = 0; i < maxh; ++i) a[i] = 1ll * a[i] * b[i] % mod[i]; n     return a; n   } n   friend data operator*(data a, int b) { n     for (int i = 0; i < maxh; ++i) a[i] = 1ll * a[i] * b % mod[i]; n     return a; n   } n   friend bool operator==(data a, data b) { n     for (int i = 0; i < maxh; ++i) n       if (a[i] != b[i]) return false; n     return true; n   } n   friend data operator-(data a, data b) { n     for (int i = 0; i < maxh; ++i) n       a[i] = ((a[i] - b[i]) % mod[i] + mod[i]) % mod[i]; n     return a; n   } n   void print() { cout << v[0] <<     << v[1] <<     << v[2] <<   n ; } n }; n data H[N], Pow[N]; n int n; n char s[N]; n data geth(int L, int R) { return H[R] - H[L - 1] * Pow[R - L + 1]; } n vector<int> Ans; n int main() { n   scanf( %d%s , &n, s + 1); n   Pow[0] = data(1, 1, 1); n   for (int i = 1; i <= n; ++i) Pow[i] = Pow[i - 1] * 1331; n   for (int i = 1; i <= n; ++i) H[i] = (H[i - 1] * 1331) + (int)s[i]; n   for (int i = (n + 1) / 2, lst = 10000000, cur; i >= 1; --i) { n     int st = i, ed = n + 1 - st, mx = min(lst + 2, ed - st); n     mx -= (mx % 2 == 0); n     cur = -1; n     for (int t = mx; t >= 1; t -= 2) n       if (geth(st, st + t - 1) == geth(ed - t + 1, ed)) { n         cur = t; n         break; n       } n     Ans.push_back(cur); n     lst = cur; n   } n   reverse(Ans.begin(), Ans.end()); n   for (auto x : Ans) cout << x <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int l, r; n     cin >> l >> r; n     if (l * 2 <= r) { n       cout << l <<     << l * 2 << endl; n     } else { n       cout << -1 <<     << -1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int SIZE = 1e6 + 10; n using namespace std; n int main() { n   long long m, h1, a1, x1, y1, h2, a2, x2, y2; n   cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2; n   long long res1 = 0, per1 = 0, now = h1, now3 = h1, now4 = h2, p = 0; n   for (int i = 0; i < (20000000); ++i) { n     now = (now * x1 + y1) % m; n     res1++; n     if (now == a1) n       break; n     else if (i == 19999999) { n       cout << -1; n       return 0; n     } n   } n   for (int i = 0; i < (10000000); ++i) { n     now3 = (now3 * x1 + y1) % m; n     now4 = (now4 * x2 + y2) % m; n     p++; n     if (now3 == a1 && now4 == a2) { n       cout << p; n       return 0; n     } n   } n   for (int i = 0; i < (20000000); ++i) { n     now = (now * x1 + y1) % m; n     per1++; n     if (now == a1) break; n   } n   long long res2 = 0, per2 = 0, now2 = h2; n   for (int i = 0; i < (9999999); ++i) { n     now2 = (now2 * x2 + y2) % m; n     res2++; n     if (now2 == a2) n       break; n     else if (i == 9999998) { n       cout << -1; n       return 0; n     } n   } n   for (int i = 0; i < (20000000); ++i) { n     now2 = (now2 * x2 + y2) % m; n     per2++; n     if (now2 == a2) break; n   } n   for (int i = 0; i < (2000000); ++i) { n     if (res1 == res2) { n       cout << res1; n       return 0; n     } n     if (res1 > res2) n       res2 += per2; n     else if (res2 > res1) n       res1 += per1; n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, dp[105], a, b; n   memset(dp, 0, sizeof dp); n   cin >> n; n   for (int i = 1; i < n; ++i) { n     cin >> dp[i]; n     dp[i] += dp[i - 1]; n   } n   cin >> a >> b; n   cout << dp[b - 1] - dp[a - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double pi = acos(-1); n void OJ() {} n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n, x, od = 0, ev = 0; n     cin >> n >> x; n     for (long long i = 0; i < n; i++) { n       long long temp; n       cin >> temp; n       if (temp & 1) n         od++; n       else n         ev++; n     } n     if (ev >= x) { n       if (od) n         cout <<  YES n ; n       else n         cout <<  NO n ; n       continue; n     } else { n       if (ev > 0) { n         if (od % 2 == 0) od--; n         if (ev + od >= x) { n           cout <<  YES n ; n           continue; n         } n       } else { n         if (x & 1) n           cout <<  YES n ; n         else n           cout <<  NO n ; n         continue; n       } n     } n     cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool sortp(const pair<int, int> a, const pair<int, int> b) { n   if (a.first > b.first) n     return 0; n   else if (a.first < b.first) n     return 1; n   else n     return a.second > b.second; n } n int main() { n   int n, m; n   cin >> n >> m; n   pair<int, int> b; n   vector<pair<int, int>> a; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     b.first = x; n     b.second = y; n     a.push_back(b); n   } n   sort(a.begin(), a.end(), sortp); n   int flag = 0; n   for (int i = 0; i < m; i++) { n     if (n <= a[i].first) { n       cout <<  NO  << endl; n       flag = 1; n       break; n     } else { n       n += a[i].second; n     } n   } n   if (flag == 0) { n     cout <<  YES  << endl; n   } n } n


#include<bits/stdc++.h> n #define int long long n #define p pair<int, int> n #define endl   n  n const int INF = 1000000001; n  n using namespace std; n  n int A(int k, int n, int C){ n     int ans = 1; n     for (int q = n; q > n-k; q--){ n         ans = ans*q % C; n     } n     return ans; n } n  n signed main(){ n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     cout.tie(nullptr); n     int n, C; n     cin >> n >> C; n     int k = n*(n-1)/2; n     int num[n+1][k+1], pref[n+1][k+2]; n     num[1][0] = 1, pref[1][0] = 0; n     fill(num[1]+1, num[1]+k+1, 0); n     fill(pref[1]+1, pref[1]+k+2, 1); n     for (int q = 2; q <= n; q++){ n         for (int q1 = 0; q1 <= k; q1++){ n             num[q][q1] = ((pref[q-1][q1+1]-pref[q-1][max(0LL, q1-q+1)]) % C+C) % C; n         } n         pref[q][0] = 0; n         for (int q1 = 1; q1 <= k+1; q1++){ n             pref[q][q1] = (pref[q][q1-1]+num[q][q1-1]) % C; n         } n     } n     int d[n+1]; n     fill(d, d+n+1, 0); n     for (int q = 2; q <= n; q++){ n         for (int q1 = 1; q1 < q; q1++){ n             for (int q3 = 0; q3 <= k; q3++){ n                 d[q] += num[q-1][q3]*(pref[q-1][k+1]-pref[q-1][min(k+1, q3+q1+1)]) % C*(q-q1) % C; n             } n         } n         d[q] = (d[q] % C+C) % C; n     } n     int ans = 0; n     for (int q = 0; q < n; q++){ n         ans += d[n-q]*A(q, n, C) % C; n     } n     cout << ans % C << endl; n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n const double EPS = 1e-9; n int n, k, deg[1000015], ans, vis[1000015]; n vector<int> e[1000015]; n int dfs(int u) { n   if (e[u].size() == 1) return 1; n   vis[u] = 1; n   vector<int> d; n   for (int i = 0; i < e[u].size(); ++i) { n     int v = e[u][i]; n     if (vis[v]) continue; n     int x = dfs(v); n     if (~x) d.push_back(x); n   } n   sort(d.begin(), d.end()); n   if (0 == d.size()) { n     return -1; n   } else if (1 == d.size()) { n     if (d[0] >= k) { n       ++ans; n       return -1; n     } else { n       return ~d[0] ? d[0] + 1 : d[0]; n     } n   } n   int i = 0; n   while (i + 1 < d.size() && d[i] + d[i + 1] <= k) ++i; n   ans += d.size() - i - 1; n   return ~d[i] ? d[i] + 1 : d[i]; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     ++deg[u], ++deg[v]; n     e[u].push_back(v), e[v].push_back(u); n   } n   for (int i = 1; i <= n; ++i) { n     if (1 != deg[i]) { n       if (~dfs(i)) ++ans; n       break; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 2e18 + 7; n const long long mod = 1e9 + 7; n const double eps = 1e-9; n const double PI = 2 * acos(0.0); n const double E = 2.71828; n int n; n pair<int, int> a[200005]; n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     a[i] = {x + y, x - y}; n   } n   sort(a, a + n); n   int mx = -mod; n   int res = 0; n   for (int i = 0; i < n; ++i) { n     if (mx <= a[i].second) { n       mx = a[i].first; n       ++res; n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   int a, b, q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     cin >> n >> a >> b; n     if (a * 2 <= b) n       cout << n * a << endl; n     else { n       { n         if (n % 2 == 0) n           cout << b * n / 2 << endl; n         else n           cout << b * (n / 2) + a << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int64_t max(int32_t a, int64_t b) { return max((int64_t)a, b); } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   pair<int64_t, int64_t> lc, rc; n   cin >> rc.second >> rc.first; n   lc.first = lc.second = 1; n   int64_t k; n   cin >> k; n   int64_t cnt = 0; n   int64_t ans = 0; n   int64_t cur = 1; n   while (cnt < k) { n     ans += (rc.first - lc.first + 1) * (rc.second - lc.second + 1); n     rc.first--; n     rc.second--; n     lc.second++; n     lc.first++; n     ans -= (rc.first - lc.first + 1) * (rc.second - lc.second + 1); n     cnt++; n     rc.first--; n     rc.second--; n     lc.second++; n     lc.first++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     long long n, side = 0, ans = 0; n     cin >> n; n     int i = (n + 1) % 4; n     if (i == 0) n       ans = n + 1; n     else if (i == 1 || i == 3) n       ans = 4 * n + 1; n     else n       ans = 2 * n + 1; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2, s; n   cin >> s1 >> s2 >> s; n   string s3 = s1 + s2; n   sort(s3.begin(), s3.end()); n   sort(s.begin(), s.end()); n   if (s == s3) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   int x, y, next; n } e[100861]; n int head[100861], book[100861] = {0}, b[100861]; n int main() { n   memset(head, -1, sizeof(head)); n   int n, m, a; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &e[i].x, &e[i].y); n     e[i].next = head[e[i].x]; n     head[e[i].x] = i; n     book[e[i].y]++; n   } n   int sum = 0, flag = 1, maxx = 0, f; n   for (int i = 1; i <= n; i++) n     if (book[i] == 0) { n       b[sum++] = i; n     } n   if (sum != 1) flag = 0; n   for (int i = 0; i < sum; i++) { n     int num = 0; n     for (int j = head[b[i]]; j != -1; j = e[j].next) { n       book[e[j].y]--; n       if (book[e[j].y] == 0) { n         b[sum++] = e[j].y; n         num++; n         if (j > maxx) maxx = j; n       } n     } n     if (num > 1) flag = 0; n   } n   if (flag == 0 || sum < n) n     printf( -1 n ); n   else n     printf( %d n , maxx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reader() {} n const long long MOD = 1e9 + 7; n const long long N = 3e7 + 5; n const long long inf = 1e18; n long long Set(long long qq, long long pos) { return (qq | (1 << pos)); } n long long chk(long long qq, long long pos) { return (qq & (1 << pos)); } n void solve() { n   long long n, first, second; n   cin >> n >> first >> second; n   vector<long long> dp(n + 1); n   dp[1] = first; n   dp[0] = 0; n   for (long long i = 2; i <= n; i++) { n     if (i % 2 == 0) n       dp[i] = min(dp[i - 1] + first, dp[i / 2] + second); n     else n       dp[i] = min(dp[(1 + i) / 2] + first + second, dp[i - 1] + first); n   } n   cout << dp[n]; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   reader(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100; n int ar[MAXN]; n int N; n int main() { n   int sum = 0; n   for (int i = 0; i < 6; ++i) { n     scanf( %d , ar + i); n     sum ^= ar[i]; n   } n   sort(ar, ar + 6); n   if (ar[0] != ar[3] && ar[1] != ar[4] && ar[2] != ar[5]) { n     printf( Alien n ); n   } else if (sum) { n     printf( Bear n ); n   } else { n     printf( Elephant n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n int n, a, b; n void solve() { n   cin >> n >> a >> b; n   string s; n   cin >> s; n   a--; n   b--; n   if (s[a] == s[b]) { n     cout << 0; n   } else { n     cout << 1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n int main() { n   int n1, n2, n3, n4, n5, n6 = 0; n   std::cin >> n1 >> n2 >> n3 >> n4; n   for (int i = 0; i < n4; i++) { n     std::cin >> n5; n     if (n5 > n2 && n5 < n3 && n1 > n2 && n1 < n3) n6++; n   } n   std::cout << n6; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 110031; n long long n, m; n vector<vector<long long> > one(int sz) { n   vector<vector<long long> > ret; n   ret.resize(sz); n   for (int i = 0; i < ret.size(); i++) { n     ret[i].resize(sz); n     for (int j = 0; j < ret.size(); j++) { n       ret[i][j] = (i == j); n     } n   } n   return ret; n } n vector<vector<long long> > mult(vector<vector<long long> > a, n                                 vector<vector<long long> > b) { n   vector<vector<long long> > ret; n   ret.resize(a.size()); n   for (int i = 0; i < ret.size(); i++) { n     ret[i].resize(a.size()); n     for (int j = 0; j < ret.size(); j++) { n       ret[i][j] = 0; n     } n   } n   for (int k = 0; k < ret.size(); k++) { n     for (int i = 0; i < a.size(); i++) { n       for (int j = 0; j < b.size(); j++) { n         ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]); n         ret[i][j] %= 1000000007; n       } n     } n   } n   return ret; n } n vector<vector<long long> > pw(vector<vector<long long> > a, long long b) { n   if (b == 0) return one((int)a.size()); n   if (b % 2) return mult(a, pw(a, b - 1)); n   return pw(mult(a, a), b / 2); n } n vector<vector<long long> > dp; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   dp.resize(m); n   for (int i = 0; i < m; i++) { n     dp[i].resize(m); n   } n   for (int i = 0; i < dp.size(); i++) { n     for (int j = 0; j < dp.size(); j++) { n       dp[i][j] = 0; n     } n   } n   for (int i = 0; i < dp.size(); i++) { n     dp[i][(i + 1) % m]++; n   } n   dp[0][0]++; n   dp = pw(dp, n); n   cout << dp[0][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); n   return x * f; n } n int n, m, f[4005][4005], l[4005], s[4005], c[4005]; n int main() { n   n = read(), m = read() + n; n   for (int i = n; i >= 1; --i) l[i] = read(); n   for (int i = n; i >= 1; --i) s[i] = read(); n   for (int i = 1; i <= m; ++i) c[i] = read(); n   memset(f, -0x3f, sizeof(f)); n   for (int i = 0; i <= m; ++i) f[i][0] = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = n; j >= 1; --j) n       f[l[i]][j] = max(f[l[i]][j], f[l[i]][j - 1] + c[l[i]] - s[i]); n     for (int j = l[i]; j < m; ++j) { n       for (int k = 0; k <= (n >> j - l[i]); ++k) { n         f[j + 1][k / 2] = max(f[j + 1][k / 2], f[j][k] + k / 2 * c[j + 1]); n       } n     } n   } n   cout << f[m][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[10005]; n int vis[100005]; n void init() { n   f[1] = 1; n   f[2] = 1; n   vis[1] = 1; n   for (int i = 3; i <= 20; i++) { n     f[i] = f[i - 1] + f[i - 2]; n     vis[f[i]] = 1; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   init(); n   for (int i = 1; i <= n; i++) { n     if (vis[i] == 1) n       printf( O ); n     else n       printf( o ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n string s, t; n int pre[maxn][26], pos[26]; n int f[maxn]; n int g[maxn]; n void solve(string &s, string &t, int f[]) { n   for (int i = 0; i < 26; i++) pos[i] = -1; n   int n = t.size(), m = s.size(); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j < 26; j++) { n       pre[i][j] = pos[j]; n     } n     if (i < n) pos[t[i] -  a ] = i; n   } n   int len = 0; n   for (int i = 0; i < m; i++) { n     if (len < n && s[i] == t[len]) { n       f[i] = ++len; n     } else { n       f[i] = pre[len][s[i] -  a ] + 1; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> s >> t; n   solve(s, t, f); n   reverse(s.begin(), s.end()); n   reverse(t.begin(), t.end()); n   solve(s, t, g); n   bool ok = true; n   int m = s.size(), n = t.size(); n   for (int i = 0; i < m; i++) { n     if (f[i] + g[m - i - 1] - 1 < n) { n       ok = false; n       break; n     } n   } n   if (ok) n     cout <<  Yes n ; n   else n     cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long sqr(long long x) { return (x * x); } n inline long long dist(pair<long long, long long> a, n                       pair<long long, long long> b) { n   return sqr(a.first - b.first) + sqr(a.second - b.second); n } n int main() { n   vector<pair<long long, pair<long long, long long> > > a; n   int d; n   double answer; n   int n; n   scanf( %d , &n); n   long long x, y, r; n   for (int i = 0; i < n; i++) { n     scanf( %I64d %I64d %I64d , &x, &y, &r); n     a.push_back(make_pair(r, make_pair(x, y))); n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; i++) { n     d = 0; n     for (int j = i + 1; j < n; j++) n       if (dist(a[i].second, a[j].second) <= sqr(a[j].first)) d++; n     answer += (double)acos(-1) * sqr(a[i].first) * ((!d || d & 1) ? 1 : -1); n   } n   printf( %.10lf n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n template <typename T, typename U> n static void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static void amax(T &x, U y) { n   if (x < y) x = y; n } n template <typename It> n void make_fenwick(It beg, size_t n) { n   for (size_t i = 0; i < n; i++) { n     size_t p = i | (i + 1); n     if (p < n) beg[p] += beg[i]; n   } n } n template <typename It, typename Val> n void add_fenwick(It beg, size_t n, size_t i, Val val) { n   for (; i < n; i |= i + 1) beg[i] += val; n } n template <typename It, typename Val> n Val sum_fenwick(It beg, size_t i, Val sum) { n   for (; i > 0; i = i & (i - 1)) sum += beg[i - 1]; n   return sum; n } n template <typename It> n typename std::iterator_traits<It>::value_type sum_fenwick(It beg, size_t i) { n   return sum_fenwick(beg, i, typename std::iterator_traits<It>::value_type()); n } n size_t highestOneBit(size_t v) { n   v |= v >> 1; n   v |= v >> 2; n   v |= v >> 4; n   v |= v >> 8; n   v |= v >> 16; n   return (v >> 1) + 1; n } n template <typename It, typename Val, typename Cmp> n size_t search_fenwick(It beg, size_t n, Val val, Val left, Cmp cmp) { n   if (!cmp(left, val)) return 0; n   size_t i = 0; n   for (size_t w = highestOneBit(n); w > 0; w >>= 1) { n     if (i + w <= n) { n       Val mid = left; n       mid += beg[i + w - 1]; n       if (cmp(mid, val)) { n         i += w; n         left = mid; n       } n     } n   } n   return i + 1; n } n template <typename It, typename Val> n size_t lower_bound_fenwick(It beg, size_t n, Val val) { n   return search_fenwick(beg, n, val, Val(), std::less<Val>()); n } n int main() { n   int n; n   int m; n   while (~scanf( %d%d , &n, &m)) { n     vector<vector<int> > graph(n); n     for (int i = 0; i < m; ++i) { n       int u, v; n       scanf( %d%d , &u, &v), --u, --v; n       graph[u].push_back(v); n       graph[v].push_back(u); n     } n     int w; n     scanf( %d , &w); n     vector<vector<pair<int, int> > > shops(n); n     vector<int> prices; n     for (int(i) = 0; (i) < (int)(w); ++(i)) { n       int c; n       int k; n       int p; n       scanf( %d%d%d , &c, &k, &p), --c; n       shops[c].emplace_back(k, p); n       prices.push_back(p); n     } n     sort(prices.begin(), prices.end()); n     prices.erase(unique(prices.begin(), prices.end()), prices.end()); n     int P = (int)prices.size(); n     for (int(i) = 0; (i) < (int)(n); ++(i)) n       for (auto &s : shops[i]) n         s.second = (int)(lower_bound(prices.begin(), prices.end(), s.second) - n                          prices.begin()); n     int q; n     scanf( %d , &q); n     vector<int> que(n), dist, ft; n     vector<long long> ftsum; n     for (int(ii) = 0; (ii) < (int)(q); ++(ii)) { n       int g; n       int r; n       int a; n       scanf( %d%d%d , &g, &r, &a), --g; n       int qt = 0; n       que[qt++] = g; n       dist.assign(n, INF); n       dist[g] = 0; n       ft.assign(P, 0); n       ftsum.assign(P, 0); n       int ans = -1; n       for (int qh = 0; qh < qt; ++qh) { n         int i = que[qh]; n         int d = dist[i]; n         for (auto s : shops[i]) { n           int k, pi; n           tie(k, pi) = s; n           add_fenwick(ft.begin(), ft.size(), pi, k); n           add_fenwick(ftsum.begin(), ftsum.size(), pi, n                       (long long)k * prices[pi]); n         } n         int pi = lower_bound_fenwick(ft.begin(), ft.size(), r) - 1; n         if (pi < P) { n           long long p = sum_fenwick(ftsum.begin(), pi); n           p += (long long)prices[pi] * (r - sum_fenwick(ft.begin(), pi)); n           if (p <= a) { n             ans = d; n             break; n           } n         } n         for (int j : graph[i]) { n           if (dist[j] != INF) continue; n           dist[j] = dist[i] + 1; n           que[qt++] = j; n         } n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <iostream> n #include <cmath> n  n using namespace std; n   n const int N=2e3+3; n int a[N]; n int b[N]; n   n int main(){ n  int n; n  cin>>n; n  n<<=1; n  for(int i=1;i<=n;i++) cin>>a[i]; n   n  int ans=n; n   n  for(int C=0;C<=1;C++){ n   n  for(int j=1;j<=n;j++) b[j]=a[j]; n   n  int cnt=0; n  for(int i=0;i<n;i++){ n   int ok=1; n   for(int j=1;j<=n;j++) ok&=b[j]==j; n   if(ok) ans=min(ans,i); n    n   if(cnt^C) for(int j=1;j<=n;j+=2) swap(b[j],b[j+1]); n   else for(int j=1;j<=n>>1;j++) swap(b[j],b[j+n/2]); n    n   cnt^=1; n   } n     } n     if(ans==n) cout<<-1; n     else cout<<ans; n }


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T min(T a, T b, T c, T d) { n   return min(min(a, b), min(c, d)); n } n template <class T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T max(T a, T b, T c, T d) { n   return max(max(a, b), max(c, d)); n } n const int INF = 1e9; n const long long INF_LL = 4e18; n const double pi = acos(-1.0); n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n int n; n vector<long long> odd; n int main() { n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     if (x % 2L) n       odd.push_back(x); n     else n       ans += x; n   } n   sort((odd).begin(), (odd).end()); n   for (int i = int((odd).size()) % 2; i < int((odd).size()); i++) ans += odd[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long double nb; n   cin >> nb; n   if (nb <= 127) n     cout <<  byte ; n   else if (nb <= 32767) n     cout <<  short ; n   else if (nb <= 2147483647) n     cout <<  int ; n   else if (nb <= 9223372036854775807LL) n     cout <<  long ; n   else n     cout <<  BigInteger ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a1, a2, b1, b2, c1, c2; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2; n   if (!a1 && !b1 && c1 || !a2 && !b2 && c2) { n     cout <<  0 ; n     return 0; n   } n   if (a1 * b2 != a2 * b1) { n     cout << 1; n     return 0; n   } n   if (c1 * b2 == c2 * b1 && c1 * a2 == c2 * a1) { n     cout <<  -1 ; n     return 0; n   } n   cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 5; n int A[M]; n char B[M]; n int main() { n   int n; n   scanf( %d , &n); n   for (int j = 0; j < n; j++) scanf( %d , &A[j]); n   scanf( %s , B); n   int cnt = 0; n   for (int j = 0; j < n; j++) n     if (B[j] ==  1 ) cnt += A[j]; n   int ans = cnt, sum = 0, sum1 = 0; n   for (int j = 0; j < n; j++) { n     if (B[j] ==  1 ) { n       ans = max(ans, cnt - A[j] + sum1 - sum); n       sum += A[j]; n     } n     sum1 += A[j]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n string s; n long long ans; n long long mem[N][5]; n int n; n long long dp(int i, int last) { n   if (i == n) { n     if (last == 2 || last == 4) return 0; n     return 1; n   } n   if (mem[i][last] != -1) return mem[i][last]; n   long long p = 0; n   if (last == 0) { n     if (s[i] ==  2  || s[i] ==  * ) return 0; n     if (s[i] ==  1 ) n       p += dp(i + 1, 4); n     else if (s[i] ==  0 ) n       p += dp(i + 1, 0); n     else { n       p += dp(i + 1, 4); n       p += dp(i + 1, 0); n     } n   } else if (last == 1) { n     if (s[i] ==  2  || s[i] ==  * ) return 0; n     if (s[i] ==  1 ) n       p += dp(i + 1, 4); n     else if (s[i] ==  0 ) n       p += dp(i + 1, 0); n     else { n       p += dp(i + 1, 4); n       p += dp(i + 1, 0); n     } n   } else if (last == 4) { n     if (s[i] ==  *  || s[i] ==  ? ) n       p += dp(i + 1, 3); n     else n       return 0; n   } else if (last == 3) { n     if (s[i] ==  0 ) n       return 0; n     else if (s[i] ==  * ) n       p += dp(i + 1, 3); n     else if (s[i] ==  1 ) n       p += dp(i + 1, 1); n     else if (s[i] ==  2 ) n       p += dp(i + 1, 2); n     else { n       p += dp(i + 1, 3); n       p += dp(i + 1, 1); n       p += dp(i + 1, 2); n     } n   } else { n     if (s[i] ==  *  || s[i] ==  ? ) n       p += dp(i + 1, 3); n     else n       return 0; n   } n   return mem[i][last] = p % 1000000007; n } n int main() { n   cin >> s; n   n = s.length(); n   memset(mem, -1, sizeof mem); n   if (s[0] ==  ? ) { n     ans += dp(1, 0); n     ans += dp(1, 4); n     ans += dp(1, 3); n   } else { n     if (s[0] ==  1 ) n       ans += dp(1, 4); n     else if (s[0] ==  0 ) n       ans += dp(1, 0); n     else if (s[0] ==  * ) n       ans += dp(1, 3); n     else n       ans = 0; n   } n   cout << ans % 1000000007; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long f[n - 1]; n   long long s[n - 1]; n   for (long long i = 0; i < n - 1; i++) { n     f[i] = abs((a[i] - a[i + 1])) * pow(-1, i); n     s[i] = abs((a[i] - a[i + 1])) * (pow(-1, i + 1)); n   } n   long long curr = f[0]; n   long long max_sofar = f[0]; n   for (long long i = 1; i <= n - 2; i++) { n     curr = max(f[i], curr + f[i]); n     max_sofar = max(max_sofar, curr); n   } n   if (n > 2) curr = s[1]; n   max_sofar = max(max_sofar, curr); n   for (long long i = 2; i <= n - 2; i++) { n     curr = max(s[i], curr + s[i]); n     max_sofar = max(max_sofar, curr); n   } n   cout << max_sofar; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     while (n--) { n       if (n == 0) { n         cout <<  1  <<   n ; n       } else { n         cout <<  1  ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a, b; n int main() { n   scanf( %d:%d , &a, &b); n   if (a > 11) a = a - 12; n   cout << (b / 2.0 + a * 30) <<     << 6 * b; n } n