#include <bits/stdc++.h> n using namespace std; n constexpr long long MOD = 1e9 + 7; n constexpr long double EPS = 0.0 + 1e-6; n void YES() { n   cout <<  YES  n        <<   n ; n } n void NO() { n   cout <<  NO  n        <<   n ; n } n bool search(const string& s, long long i, const string& t, long long j, n             bool going_left, bool going_right, bool can_turn_left) { n   if (j >= t.size()) return false; n   if (i < 0 or i >= s.size()) return false; n   if (s[i] != t[j]) return false; n   if (s[i] == t[j] and j + 1 == t.size()) return true; n   if (going_right and search(s, i + 1, t, j + 1, false, true, true)) n     return true; n   if (going_right and can_turn_left and n       search(s, i - 1, t, j + 1, true, false, false)) n     return true; n   if (going_left and search(s, i - 1, t, j + 1, true, false, false)) n     return true; n   return false; n } n void solve() { n   string s; n   cin >> s; n   string t; n   cin >> t; n   for (long long i = 0; i < s.size(); ++i) { n     if (search(s, i, t, 0, false, true, true)) return YES(); n   } n   NO(); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int counter = 0; n int ans = 0; n vector<int> L[500000]; n bool visited[500000]; n bool cat[500000]; n int main() { n   int a, b, k1, k2; n   deque<int> q1, q2, t1, t2; n   set<pair<deque<int>, deque<int> > > st; n   scanf( %d , &n); n   scanf( %d , &k1); n   for (int i = 1; i <= k1; i++) { n     cin >> a; n     q1.push_back(a); n   } n   scanf( %d , &k2); n   for (int i = 1; i <= k2; i++) { n     cin >> a; n     q2.push_back(a); n   } n   int fight = 0; n   int x, y; n   t1 = q1; n   t2 = q2; n   bool loop = false; n   int winner = 0; n   st.insert(make_pair(q1, q2)); n   while (true) { n     if (q1.empty()) { n       winner = 2; n       break; n     } n     if (q2.empty()) { n       winner = 1; n       break; n     } n     x = q1.front(); n     y = q2.front(); n     q1.pop_front(); n     q2.pop_front(); n     if (x > y) n       q1.push_back(y), q1.push_back(x); n     else n       q2.push_back(x), q2.push_back(y); n     fight++; n     st.insert(make_pair(q1, q2)); n     if (t1 == q1 and t2 == q2 or (t2 == q1 and t1 == q2)) { n       loop = true; n       break; n     } n     if (st.size() != fight + 1) { n       loop = true; n       break; n     } n   } n   if (!loop) n     cout << fight <<     << winner << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void start() {} n vector<long int> g[200007]; n map<long int, bool> vis; n long int siz = 0; n void dfs(long int x) { n   vis[x] = 1; n   siz++; n   for (auto i : g[x]) { n     if (!vis[i]) { n       dfs(i); n     } n   } n   return; n } n double dist(long int x, long int y, long int a, long int b) { n   double ans = sqrt((a - x) * (a - x) + (b - y) * (b - y)); n   return ans; n } n void sober() { n   double r, x, y, x1, y1; n   cin >> r >> x >> y >> x1 >> y1; n   double ans = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)); n   ans = ans / (2 * r); n   cout << ceil(ans) <<   n ; n } n signed main() { n   start(); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long int t = 1; n   while (t--) { n     sober(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 5; n vector<int> G[1000010]; n int a[MAXN], s[MAXN], L[MAXN], R[MAXN]; n int n, k; n long long ans; n int query(int k, int l, int r) { n   return upper_bound(G[k].begin(), G[k].end(), r) - n          lower_bound(G[k].begin(), G[k].end(), l); n } n int main() { n   scanf( %d%d , &n, &k); n   G[0].push_back(0); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     s[i] = (s[i - 1] + a[i]) % k; n     G[s[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     L[i] = i - 1; n     while (L[i] && a[L[i]] < a[i]) L[i] = L[L[i]]; n   } n   for (int i = n; i >= 1; i--) { n     R[i] = i + 1; n     while (R[i] <= n && a[R[i]] <= a[i]) R[i] = R[R[i]]; n   } n   for (int i = 1; i <= n; i++) a[i] %= k; n   for (int i = 1; i <= n; i++) { n     if (i - L[i] < R[i] - i) { n       for (int j = L[i] + 1; j <= i; j++) n         ans += query((s[j - 1] + a[i]) % k, i, R[i] - 1); n     } else { n       for (int j = i; j < R[i]; j++) n         ans += query((s[j] - a[i] + k) % k, L[i], i - 1); n     } n   } n   printf( %lld n , ans - n); n   return 0; n } n


#include <bits/stdc++.h> n char s[200010]; n int tree[26][200010 << 2]; n bool vis[200010]; n inline void PushUp(int t, int rt) { n   tree[t][rt] = tree[t][rt << 1] + tree[t][rt << 1 | 1]; n } n void Update(int t, int x, int L, int R, int rt) { n   if (L == R) n     tree[t][rt] = 1; n   else { n     int mid = (L + R) >> 1; n     if (mid >= x) n       Update(t, x, L, mid, rt << 1); n     else n       Update(t, x, mid + 1, R, rt << 1 | 1); n     PushUp(t, rt); n   } n } n int Find(int t, int x, int L, int R, int rt) { n   if (L == R) { n     tree[t][rt] = 0; n     return L; n   } n   int ans, mid = (L + R) >> 1; n   if (tree[t][rt << 1] >= x) n     ans = Find(t, x, L, mid, rt << 1); n   else { n     x -= tree[t][rt << 1]; n     ans = Find(t, x, mid + 1, R, rt << 1 | 1); n   } n   PushUp(t, rt); n   return ans; n } n int main() { n   char ch; n   int k, i, j, q, len, n; n   while (~scanf( %d %s%d , &k, s + 1, &q)) { n     memset(vis, false, sizeof(vis)); n     memset(tree, 0, sizeof(tree)); n     len = strlen(s + 1); n     n = len * k; n     for (j = len + 1; --k;) { n       for (i = 1; i <= len; i++) s[j++] = s[i]; n     } n     s[j] = 0; n     for (i = 1; s[i]; i++) Update(s[i] -  a , i, 1, n, 1); n     while (q--) { n       scanf( %d %c , &i, &ch); n       k = Find(ch -  a , i, 1, n, 1); n       vis[k] = true; n     } n     for (i = 1; s[i]; i++) { n       if (!vis[i]) putchar(s[i]); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n int numb[MAXN], numw[MAXN]; n int dp[MAXN][2]; n string s[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   for (int i = 0; i < MAXN; i++) dp[i][0] = dp[i][1] = -1; n   int n, m, x, y; n   cin >> n >> m >> x >> y; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int t = 0; t < m; t++) { n     int num = 0; n     for (int i = 0; i < n; i++) n       if (s[i][t] ==  # ) num++; n     numb[t] = num; n     numw[t] = n - num; n   } n   for (int t = 1; t < m; t++) numb[t] += numb[t - 1], numw[t] += numw[t - 1]; n   for (int t = x - 1; t < m; t++) { n     for (int j = x; j <= y && t - j + 1 >= 0; j++) { n       if (t - j + 1 == 0) { n         if (dp[t][0] == -1) dp[t][0] = 1e9; n         if (dp[t][1] == -1) dp[t][1] = 1e9; n         dp[t][0] = min(dp[t][0], numb[t]); n         dp[t][1] = min(dp[t][1], numw[t]); n         continue; n       } n       if (dp[t - j][0] != -1) { n         if (dp[t][1] == -1) dp[t][1] = 1e9; n         dp[t][1] = min(dp[t][1], dp[t - j][0] + (numw[t] - numw[t - j])); n       } n       if (dp[t - j][1] != -1) { n         if (dp[t][0] == -1) dp[t][0] = 1e9; n         dp[t][0] = min(dp[t][0], dp[t - j][1] + (numb[t] - numb[t - j])); n       } n     } n   } n   cout << min(dp[m - 1][0], dp[m - 1][1]) <<   n ; n } n


#include<bits/stdc++.h> n using namespace std; n #define forg(i,x) for(register int i=fir[x];i;i=nxt[i]) n #define uu unsigned n #define scanf a14=scanf n #define rint register int n #define fre(x) freopen(#x .in , r ,stdin),freopen(#x .out , w ,stdout) n typedef long long ll; n typedef uu long long ull; n typedef pair<int,int>pii; n int a14; n inline int rd(int l,int r){return rand()%(r-l+1)+l;} n  n const int mxn=1e5+3,sq=200,qs=320; n int n,b[mxn],lp[mxn],rp[mxn],kn,be[mxn],po[mxn],qn; n int ju[mxn/sq+2][mxn],ad[mxn/sq+2][mxn/qs+1]; n int TG; n struct shu{ n     int a[mxn],tg[mxn]; n     void add(int x){for(;x<=n;x+=x&-x){if(tg[x]!=TG)tg[x]=TG,a[x]=0;++a[x];}} n     int ask(int x){ n         int su=0,p=0; n         for(int i=1<<16;i;i>>=1){ n             if(p+i>=n)continue; n             p+=i;if(tg[p]!=TG)tg[p]=TG,a[p]=0; n             if(p+a[p]+su>=x)p-=i;else su+=a[p]; n         } n         return p+1; n     } n     void gb(int t){for(int i=1;i<=n;++i){if(tg[i]!=TG)a[i]=0;ju[t][i]=ju[t][i-(i&-i)]+a[i];}for(int i=1;i<=n;++i)ju[t][i]+=i;} n }ar; n void add2(int t,int x,int y){ n     int k=(x-1)/qs; n     for(int i=x;i<=(k+1)*qs;++i)ju[t][i]+=y; n     for(++k;k*qs+1<=n;++k)ad[t][k]+=y; n } n int main(){ n //cout<<(sizeof(ju)>>20)<<endl; n     scanf( %d ,&n);for(int i=1;i<=n;++i)scanf( %d ,b+i),b[i]=i-b[i];scanf( %d ,&qn); n     for(int l=1,r;r!=n;l=r+1){r=min(l+sq-1,n);lp[++kn]=l,rp[kn]=r;for(int i=l;i<=r;++i)be[i]=kn;} n     for(int t=1;t<=kn;++t){ n         ++TG; for(int i=lp[t];i<=rp[t];++i)po[i]=ar.ask(b[i]),ar.add(po[i]); n         ar.gb(t); n     } n     while(qn--){ n         int o,x,y;scanf( %d%d ,&o,&x);if(o==1){ n             scanf( %d ,&y);b[x]=x-y;int k=be[x],ls; n             ++TG;for(int i=lp[k];i<=rp[k];++i){ n                 if(i<x)ar.add(po[i]);else{ n                     ls=po[i],po[i]=ar.ask(b[i]),ar.add(po[i]); n                     if(ls==po[i])continue; n                     if(ls==po[i]-1){--ju[k][ls];continue;} n                     if(ls==po[i]+1){++ju[k][po[i]];continue;} n                     assert(i==x); n                     add2(k,ls,-1),add2(k,po[i],1); n                 } n             } n         }else{ n             int t=be[x],re=b[x];for(int i=x+1;i<=rp[t];++i)re+=b[i]<=re; n             for(++t;t<=kn;++t)re=ju[t][re]+ad[t][(re-1)/qs]; n             printf( %d n ,re); n         } n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MOD = 1e9 + 7; n vector<int> prefix_function(vector<int> s) { n   int n = s.size(); n   vector<int> pi(n); n   pi[0] = 0; n   for (int i = 1; i < n; ++i) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) { n       j = pi[j - 1]; n     } n     if (s[i] == s[j]) { n       ++j; n     } n     pi[i] = j; n   } n   return pi; n } n vector<int> res_vector(vector<int> s, vector<int> t) { n   vector<int> res = s; n   res.push_back(2 * INF); n   for (int i = 0; i < t.size(); ++i) { n     res.push_back(t[i]); n   } n   return res; n } n vector<int> process(vector<int> res, vector<int> pi, int n) { n   vector<int> ans(res.size()); n   for (int i = n + 1; i < res.size(); ++i) { n     ++ans[pi[i]]; n   } n   for (int i = res.size() - 1; i >= n + 1; --i) { n     ans[pi[i - 1]] += ans[i]; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   int n, w; n   cin >> n >> w; n   if (w == 1) { n     cout << n << endl; n     return 0; n   } n   vector<int> a(n - 1); n   int last; n   cin >> last; n   for (int i = 0; i < n - 1; ++i) { n     int temp; n     cin >> temp; n     a[i] = temp - last; n     last = temp; n   } n   cin >> last; n   vector<int> b(w - 1); n   for (int i = 0; i < w - 1; ++i) { n     int temp; n     cin >> temp; n     b[i] = temp - last; n     last = temp; n   } n   vector<int> res = res_vector(b, a); n   vector<int> pi = prefix_function(res); n   vector<int> ans = process(res, pi, b.size()); n   cout << ans[w - 1] << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<char> v; n   int i = 0; n   int j = 1; n   int sum = -1; n   while (sum < n - 1) { n     sum = sum + j; n     v.push_back(s[sum]); n     j++; n   } n   for (auto it = v.begin(); it != v.end(); it++) { n     cout << *it; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const long long int N = 1000005; n struct segmenttree { n   vector<long long int> st; n   void init(long long int _n) { n     st.clear(); n     st.resize(4 * _n); n   } n   void build(long long int l, long long int r, long long int node) { n     if (l == r) { n       return; n     } n     long long int mid = (l + r) / 2; n     build(l, mid, node * 2 + 1); n     build(mid + 1, r, node * 2 + 2); n     st[node] = max(st[2 * node + 1], st[2 * node + 2]); n   } n   void update(long long int l, long long int r, long long int indup, n               long long int val, long long int node) { n     if (l == r) { n       st[node] = val; n       return; n     } else { n       long long int mid = (l + r) / 2; n       if (indup >= l && indup <= mid) { n         update(l, mid, indup, val, node * 2 + 1); n       } else { n         update(mid + 1, r, indup, val, node * 2 + 2); n       } n       st[node] = max(st[2 * node + 1], st[2 * node + 2]); n     } n   } n   long long int query(long long int si, long long int se, long long int l, n                       long long int r, long long int node) { n     if (se < l || si > r || l > r) { n       return INT_MIN; n     } n     if (si >= l && se <= r) { n       return st[node]; n     } n     long long int mid = (si + se) / 2; n     long long int q1 = query(si, mid, l, r, node * 2 + 1); n     long long int q2 = query(mid + 1, se, l, r, node * 2 + 2); n     return max(q1, q2); n   } n }; n segmenttree tree[N]; n long long int sz[N], res[N], mx[N], n, m; n vector<long long int> pos[N]; n long long int give(long long int row, long long int pos) { n   long long int mn, mx, ret; n   if (pos < sz[row]) { n     mn = max(0ll, pos - (m - sz[row])), mx = pos; n     if ((m - 1) - pos >= sz[row]) n       ret = 0; n     else n       ret = INT_MIN; n   } else { n     pos = sz[row] - (m - pos); n     mn = pos, mx = max(sz[row] - 1, pos + (m - sz[row])); n     if (pos >= sz[row]) n       ret = INT_MIN; n     else n       ret = 0; n   } n   return max(ret, tree[row].query(0, sz[row] - 1, mn, mx, 0)); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   { n     long long int i, j, k, ans = 0, cnt = 0, sum = 0; n     cin >> n >> m; n     for (i = 0; i < n; i++) { n       cin >> sz[i]; n       tree[i].init(sz[i]); n       for (j = 0; j < sz[i]; j++) { n         long long int x; n         cin >> x; n         mx[i] = max(mx[i], x); n         tree[i].update(0, sz[i] - 1, j, x, 0); n       } n       for (j = 0; j < sz[i]; j++) pos[j].push_back(i); n       for (j = max(sz[i], (m - sz[i])); j < m; j++) pos[j].push_back(i); n     } n     for (i = 0; i < n; i++) { n       ans += mx[i]; n     } n     for (i = 0; i < m; i++) { n       res[i] = ans; n       for (auto x : pos[i]) { n         res[i] -= mx[x]; n         res[i] += give(x, i); n       } n     } n     for (i = 0; i < m; i++) { n       cout << res[i] <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T prod(const T &a, const T &b, const T &mod) { n   return ((a % mod) * (b % mod)) % mod; n } n template <typename T> n T pow_(const T &a, const T &b) { n   if (!b) return 1; n   T p = pow_(a, b / 2); n   p *= p; n   return (b % 2) ? (p * a) : p; n } n template <typename T> n T modpow(const T &a, const T &b, const T &mod) { n   if (!b) return 1; n   T p = modpow(a, b / 2); n   p = prod(p, p); n   return (b % 2) ? (prod(p, a)) : p; n } n template <typename T> n T max(const T &a, const T &b, const T &c) { n   return max(a, max(b, c)); n } n template <typename T> n T min(const T &a, const T &b, const T &c) { n   return min(a, min(b, c)); n } n template <typename T> n void dbg(const vector<T> &a, int l, int r) { n   for (long long int i = l; i <= r; i++) cout << a[i] <<    ; n   cout << endl; n } n template <typename T> n void dbg(const vector<T> &a) { n   for (long long int i = 0; i <= a.size() - 1; i++) cout << a[i] <<    ; n   cout << endl; n } n void main_() { n   long long int n; n   cin >> n; n   vector<long long int> a(n + 1, 0); n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] += a[i - 1]; n   } n   long long int x, y, z; n   long long int res = -LLONG_MAX; n   for (long long int i = 0; i <= n; i++) { n     long long int m1 = -LLONG_MAX, m2 = -LLONG_MAX; n     long long int y_, x_, z_; n     y_ = i; n     for (long long int j = 0; j <= i; j++) { n       if (m1 < a[j]) { n         m1 = a[j]; n         x_ = j; n       } n     } n     for (long long int j = i; j <= n; j++) { n       if (m2 < a[j]) { n         m2 = a[j]; n         z_ = j; n       } n     } n     long long int sum = 2 * (a[x_] + a[z_] - a[y_]) - a[n]; n     if (res < sum) { n       res = sum; n       x = x_; n       y = y_; n       z = z_; n     } n   } n   cout << x <<     << y <<     << z << endl; n } n int main() { n   { main_(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class T, class U> n using Pa = pair<T, U>; n template <class T> n using vec = vector<T>; n template <class T> n using vvec = vector<vec<T>>; n template <typename Monoid, typename F> n class SegmentTree { n  private: n   int sz; n   vector<Monoid> seg; n   const F op; n   const Monoid e; n  n  public: n   SegmentTree(int n, const F op, const Monoid &e) : op(op), e(e) { n     sz = 1; n     while (sz <= n) sz <<= 1; n     seg.assign(2 * sz, e); n   } n   void set(int k, const Monoid &x) { seg[k + sz] = x; } n   void build() { n     for (int i = sz - 1; i > 0; i--) { n       seg[i] = op(seg[2 * i], seg[2 * i + 1]); n     } n   } n   void update(int k, const Monoid &x) { n     k += sz; n     seg[k] += x; n     while (k >>= 1) { n       seg[k] = op(seg[2 * k], seg[2 * k + 1]); n     } n   } n   Monoid query(int l, int r) { n     Monoid L = e, R = e; n     for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) { n       if (l & 1) L = op(L, seg[l++]); n       if (r & 1) R = op(seg[--r], R); n     } n     return op(L, R); n   } n   Monoid operator[](const int &k) const { return seg[k + sz]; } n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int N, M; n   cin >> N >> M; n   vvec<char> F(N, vec<char>(M)); n   vec<int> len(N + M); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) { n       cin >> F[i][j]; n       len[i + j]++; n     } n   vvec<int> L(N, vec<int>(M, 0)); n   vvec<int> R = L, D = L; n   for (int i = N - 1; i >= 0; i--) { n     for (int j = 0; j < M; j++) { n       int c = (F[i][j] ==  z ); n       if (!c) { n         L[i][j] = 0; n         D[i][j] = 0; n       } else { n         L[i][j] = 1 + (j != 0 ? L[i][j - 1] : 0); n         D[i][j] = 1 + ((j != 0 && i != N - 1) ? D[i + 1][j - 1] : 0); n       } n     } n     for (int j = M - 1; j >= 0; j--) { n       int c = (F[i][j] ==  z ); n       if (!c) n         R[i][j] = 0; n       else n         R[i][j] = 1 + (j != M - 1 ? R[i][j + 1] : 0); n     } n   } n   auto op = [](ll a, ll b) { return a + b; }; n   vec<SegmentTree<ll, decltype(op)>> segs; n   for (int l = 0; l < N + M - 1; l++) { n     SegmentTree<ll, decltype(op)> seg(M, op, 0); n     segs.push_back(seg); n   } n   ll ans = 0; n   for (int j = M - 1; j >= 0; j--) { n     for (int i = 0; i < N; i++) { n       int y = j; n       while (y >= 0 && y + R[i][y] - 1 == j) { n         segs[i + y].update(y, 1); n         y--; n       } n     } n     for (int i = 0; i < N; i++) { n       int a = min(L[i][j], D[i][j]); n       ans += segs[i + j].query(j - a + 1, j + 1); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a, b, i = 0, arr[100000], j = 0; n   cin >> n >> k; n   for (int x = 0; x < n; x++) { n     cin >> a >> b; n     i++; n     while (a != b) n       if (a < b) n         a++, i++; n       else n         a--, i++; n   } n   while (i % k != 0) { n     i++; n     j++; n   } n   cout << j; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, top, len[100005], F1[1005][1005], F2[1005][1005]; n pair<int, int> F[30]; n char ans[30], dir[100005], a[1005][1005]; n bool outside(int first, int second) { n   return (first < 1 || first > n || second < 1 || second > m); n } n int tinh1(int first, int y1, int y2) { n   if (y1 > y2) swap(y1, y2); n   return F1[first][y2] - F1[first][y1 - 1]; n } n int tinh2(int second, int x1, int x2) { n   if (x1 > x2) swap(x1, x2); n   return F2[x2][second] - F2[x1 - 1][second]; n } n void tinh(int first, int second, int i, int& x2, int& y2) { n   int dx = 0, dy = 0; n   if (dir[i] ==  N ) n     dx = -1; n   else if (dir[i] ==  S ) n     dx = 1; n   else if (dir[i] ==  W ) n     dy = -1; n   else if (dir[i] ==  E ) n     dy = 1; n   x2 = first + dx * len[i], y2 = second + dy * len[i]; n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       scanf( %c , &a[i][j]); n       if (a[i][j] >=  A  && a[i][j] <=  Z ) { n         F[a[i][j]] = pair<int, int>(i, j); n         a[i][j] =  . ; n       } n     } n     scanf(  n ); n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       F1[i][j] = F1[i][j - 1] + (a[i][j] ==  # ); n       F2[i][j] = F2[i - 1][j] + (a[i][j] ==  # ); n     } n   int k; n   scanf( %d n , &k); n   for (int i = 1; i <= k; ++i) scanf( %c %d n , &dir[i], &len[i]); n   for (char t =  A ; t <=  Z ; ++t) n     if (F[t].first != 0) { n       int first = F[t].first, second = F[t].second; n       bool b = true; n       for (int i = 1; i <= k; ++i) { n         int x2, y2; n         tinh(first, second, i, x2, y2); n         if (outside(x2, y2) || (x2 == first && tinh1(first, second, y2) != 0) || n             (y2 == second && tinh2(second, first, x2) != 0)) { n           b = false; n           break; n         } n         first = x2, second = y2; n       } n       if (b) ans[++top] = t; n     } n   if (!top) n     cout <<  no solution  << endl; n   else { n     for (int i = 1; i <= top; ++i) printf( %c , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n; n int a[5001]; n bool coutNO; n vector<int> check[5001]; n int main() { n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   for (int ct = 1; ct <= t; ct++) { n     cin >> n; n     coutNO = true; n     for (int i = 1; i <= n; i++) check[i].clear(); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       check[a[i]].push_back(i); n       if (check[a[i]].size() >= 3) coutNO = false; n     } n     if (!coutNO) { n       cout <<  YES  <<   n ; n       continue; n     } n     for (int i = 1; i <= n; i++) n       if (check[i].size() == 2) { n         if (check[i][0] + 1 != check[i][1]) { n           coutNO = false; n           break; n         } n       } n     if (coutNO) n       cout <<  NO  <<   n ; n     else n       cout <<  YES  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int const lac = 100000; n long long int modmul(long long int a, long long int b) { n   return ((a % 1000000007) * (b % 1000000007)) % 1000000007; n } n long long int modadd(long long int a, long long int b) { n   return (((a % 1000000007) + (b % 1000000007))) % 1000000007; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n class comp { n  public: n   bool operator()(int a, int b) const { return a > b; } n }; n bool compa(int a, int b) { return a > b; } n void solve(long long int t1) { n   long long int n; n   cin >> n; n   vector<long long int> arr(n + 1); n   for (long long int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   vector<long long int> prefix(n + 1, 0); n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     prefix[i] = prefix[i - 1] ^ i; n     ans ^= arr[i]; n   } n   if (n == 1) { n     cout << ans <<   n ; n     return; n   } n   for (long long int i = 1; i <= n - 1; i++) { n     long long int temp = (n) / (i + 1); n     long long int rem = n % i; n     if (temp & 1) { n       ans ^= prefix[i]; n     } n     ans ^= prefix[rem]; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   t = 1; n   for (long long int i = 1; i <= t; i++) { n     solve(t); n   } n   cerr <<  time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n pair<double, double> p[301]; n double a, b, c, den, len; n double area(int i) { n   double num = abs(a * p[i].first + b * p[i].second + c); n   return (num) / (2.0); n } n double f(int i, int j) { n   a = (p[j].second - p[i].second); n   b = (p[i].first - p[j].first); n   c = 0 - p[i].second * b - p[i].first * a; n   den = sqrtf(a * a + b * b); n   len = sqrtf((p[i].first - p[j].first) * (p[i].first - p[j].first) + n               (p[i].second - p[j].second) * (p[i].second - p[j].second)); n   double m1 = -1, m2 = -1; n   for (int k = 0; k < n; ++k) { n     if (k == i or k == j) continue; n     long long temp = a * p[k].first + b * p[k].second + c; n     double flag = area(k); n     if (temp >= 0) n       m1 = max(m1, flag); n     else n       m2 = max(m2, flag); n   } n   if (m1 == -1 or m2 == -1) return 0; n   return m1 + m2; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     double x, y; n     scanf( %lf%lf , &x, &y); n     p[i] = {x, y}; n   } n   double ans = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       ans = max(ans, f(i, j)); n     } n   } n   printf( %0.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s; n vector<long long> v(1001); n long long check(long long x) { n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     if (v[i] < x) return 0; n     sum += max((long long)0, v[i] - x); n   } n   return (sum >= s); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> s; n   for (long long i = 0; i < n; i++) cin >> v[i]; n   long long l = 0; n   long long r = 1e15; n   while (l < r) { n     long long mid = (l + r + 1) / 2; n     if (check(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   if (check(l)) n     cout << l; n   else n     cout << -1; n } n


#include <bits/stdc++.h> n long long n, m, a, d, t[100007], old, ans, aind, firstA, stepD; n int main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   std::cout.tie(0); n   std::cin >> n >> m >> a >> d; n   for (int i = 0; i < m; ++i) std::cin >> t[i]; n   aind = d / a * a + a; n   for (int i = 0; i < m; ++i) { n     if (t[i] <= old) continue; n     if (t[i] <= n * a) { n       firstA = (old / a + 1) * a; n       if (t[i] <= firstA) { n         ++ans; n         old = t[i] + d; n       } else { n         stepD = (t[i] - firstA) / aind + ((t[i] - firstA) % aind ? 1 : 0); n         ans += stepD; n         old = firstA + stepD * aind - (a - d % a); n         if (old < t[i]) { n           ++ans; n           firstA = (old / a + 1) * a; n           old = std::min(t[i], firstA) + d; n         } n       } n     } else { n       if (old < n * a) { n         firstA = (old / a + 1) * a; n         stepD = (n * a - firstA) / aind + ((n * a - firstA) % aind ? 1 : 0); n         ans += stepD; n         old = firstA + stepD * aind - (a - d % a); n         if (old < n * a) { n           ++ans; n           firstA = (old / a + 1) * a; n           old = firstA + d; n         } n       } n       if (old < t[i]) { n         ++ans; n         old = t[i] + d; n       } n     } n   } n   if (old < a * n) { n     firstA = (old / a + 1) * a; n     stepD = (a * n - firstA) / aind + ((a * n - firstA) % aind ? 1 : 0); n     ans += stepD; n     old = firstA + stepD * aind - (a - d % a); n     if (old < a * n) { n       ++ans; n     } n   } n   std::cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int inf = 50000000; n const int maxn = 2010; n int n, A[maxn][maxn], vis[maxn]; n int bfs(int src, int d) { n   queue<int> q; n   q.push(src); n   for (int i = 1; i <= n; i++) vis[i] = 0; n   vis[src] = 1; n   int ret = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 1; i <= n; i++) { n       if (!d && !vis[i] && A[u][i] > 0) { n         vis[i] = 1; n         ret += 1; n         q.push(i); n       } n       if (d && !vis[i] && A[i][u] > 0) { n         vis[i] = 1; n         ret += 1; n         q.push(i); n       } n     } n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       scanf( %d , &A[i][j]); n     } n   } n   if (bfs(1, 0) == n && bfs(1, 1) == n) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n char str[20][N]; n int n, m, des; n int dp[20][2]; n int call(int flr, int side) { n   if (flr == des) { n     if (side == 0) { n       int p = 0; n       for (int i = 1; i <= m; i++) n         if (str[flr][i] ==  1 ) p = i; n       return p; n     } else { n       int p = m + 1; n       for (int i = m; i >= 1; i--) n         if (str[flr][i] ==  1 ) p = i; n       return m + 1 - p; n     } n   } n   if (dp[flr][side] != -1) return dp[flr][side]; n   int ret = 1e9; n   if (side == 0) { n     ret = min(ret, m + 2 + call(flr - 1, 1)); n     int p = 0; n     for (int i = 1; i <= m; i++) n       if (str[flr][i] ==  1 ) p = i; n     ret = min(ret, 2 * p + 1 + call(flr - 1, 0)); n   } else { n     ret = min(ret, m + 2 + call(flr - 1, 0)); n     int p = m + 1; n     for (int i = m; i >= 1; i--) n       if (str[flr][i] ==  1 ) p = i; n     ret = min(ret, (m + 1 - p) * 2 + 1 + call(flr - 1, 1)); n   } n   return dp[flr][side] = ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   memset(dp, -1, sizeof(dp)); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> str[i]; n   des = n - 1; n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 1; j <= m; j++) n       if (str[i][j] ==  1 ) des = i; n   } n   cout << call(n - 1, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x, y, sum, ans, T; n char c; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     vector<int> v, b; n     for (int i = 1; i <= n; i += 2) { n       if (i + 1 > n) { n         v.push_back(i); n         b.push_back(i); n       } else { n         printf( ? %d %d n , i, i + 1); n         fflush(stdout); n         scanf(  %c , &c); n         if (c ==  < ) n           v.push_back(i + 1), b.push_back(i); n         else if (c ==  > ) n           v.push_back(i), b.push_back(i + 1); n         else n           v.push_back(i), b.push_back(i); n       } n     } n     while (v.size() > 1) { n       x = v[v.size() - 1]; n       v.pop_back(); n       y = v[v.size() - 1]; n       v.pop_back(); n       printf( ? %d %d n , x, y); n       fflush(stdout); n       scanf(  %c , &c); n       if (c ==  < ) n         v.push_back(y); n       else if (c ==  > ) n         v.push_back(x); n       else n         v.push_back(x); n     } n     while (b.size() > 1) { n       x = b[b.size() - 1]; n       b.pop_back(); n       y = b[b.size() - 1]; n       b.pop_back(); n       printf( ? %d %d n , x, y); n       fflush(stdout); n       scanf(  %c , &c); n       if (c ==  < ) n         b.push_back(x); n       else if (c ==  > ) n         b.push_back(y); n       else n         b.push_back(x); n     } n     printf( ! %d %d n , b[0], v[0]); n     fflush(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n vector<int> v; n set<double> S; n set<double>::iterator it, it2; n int main() { n   int n, k; n   cin >> n >> k; n   cin >> s; n   for (int i = 0; i < s.size(); i++) n     if (s[i] ==  0 ) v.push_back(i + 1); n   int i = 0, j = 0; n   for (i = 0; i <= k; i++) S.insert(v[i]); n   j = i - 1; n   i = 0; n   int mn = INT_MAX; n   n = v.size(); n   while (j < n) { n     double x = (v[i] + v[j]) / 2.0; n     it = S.lower_bound(x); n     it2 = it; n     it2--; n     int a = max(v[j] - *it, *it - v[i]); n     int b = max(*it2 - v[i], v[j] - *it2); n     mn = min(mn, min(a, b)); n     j++; n     if (j < n) { n       S.erase(v[i]); n       S.insert(v[j]); n       i++; n     } n   } n   cout << mn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int firstOne(string s) { n   int len = s.length(); n   for (int i = len - 1; i >= 0; i--) { n     if (s[i] ==  1 ) { n       return len - i; n     } n   } n } n int firstOneAfter(string s, int idx) { n   int len = s.length(); n   for (int i = len - idx; i >= 0; i--) { n     if (s[i] ==  1 ) { n       return len - i; n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     string x, y; n     cin >> x >> y; n     int firstOneX = firstOne(x), firstOneY = firstOne(y); n     if (firstOneX >= firstOneY) { n       cout << firstOneX - firstOneY << endl; n     } else { n       cout << firstOneAfter(x, firstOneY) - firstOneY << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int val(vector<int> a) { n   int cur = a[0]; n   for (int i = 1; i < a.size(); i++) { n     cur = (a[i] | cur) - a[i]; n   } n   return cur; n } n int brute(vector<int> a) { n   vector<int> P(a.size()); n   for (int i = 0; i < a.size(); i++) { n     P[i] = i; n   } n   int ans = 0; n   vector<int> f; n   do { n     vector<int> b = a; n     for (int i = 0; i < b.size(); i++) { n       b[i] = a[P[i]]; n     } n     if (ans < val(b)) f = b; n     ans = max(ans, val(b)); n   } while (next_permutation((P).begin(), (P).end())); n   return ans; n } n pair<long long, long long> vec(pair<long long, long long> P, n                                pair<long long, long long> Q) { n   return {P.first - Q.first, P.second - Q.second}; n } n bool okay(pair<long long, long long> P1, pair<long long, long long> P2, n           pair<long long, long long> P3, pair<long long, long long> P4) { n   return vec(P1, P2) == vec(P3, P4); n } n int main() { n   int n; n   cin >> n; n   vector<pair<long long, long long> > P(n); n   for (int i = 0; i < n; i++) { n     cin >> P[i].first >> P[i].second; n   } n   if (n % 2) { n     cout <<  no ; n     return 0; n   } n   P.push_back(P[0]); n   for (int i = 0; i < n / 2; i++) { n     if (okay(P[i], P[i + 1], P[n / 2 + i + 1], P[n / 2 + i]) || n         okay(P[i], P[i + 1], P[n / 2 + i], P[n / 2 + i + 1])) { n       continue; n     } n     cout <<  no ; n     return 0; n   } n   cout <<  yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n using ll = long long; n int n, m, b[N]; n ll a[N], c1, c2; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 2; i <= m * 2; i++) { n     for (int j = 1; j <= n; j++) b[j] = a[j] % i; n     sort(b + 1, b + n + 1); n     b[n + 1] = m; n     if (~i & 1) { n       int t1 = 0; n       for (int j = 1; j <= n; j++) n         if (b[j] >= i / 2) t1 ^= 1; n       if (t1) ++c2; n     } n     for (int j = 1; j <= n + 1; j++) { n       int l = max(b[j - 1] + 1, i / 2 + 1), r = min(min(i - 1, m), b[j]); n       if (r < l) continue; n       int al = max(b[j - 1], b[n - 1] / 2), bl = max(l, i - al); n       if (bl <= r) c1 += r - bl + 1, r = bl - 1; n       if (r < l) continue; n       if ((n - j + 1) & 1) n         c2 += (r - l + 1) * 2; n       else { n         al = b[n - 1] / 2 + 1; n         int ar = b[n] / 2; n         bl = max(l, i - ar); n         int br = min(r, i - al); n         if (br >= bl) c1 += br - bl + 1; n       } n     } n   } n   cout << c1 <<     << c1 <<     << c2 <<     << m * 1ll * m - c1 * 2 - c2 n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int T = 1 << 19; n int n, q, a[N]; n long long s[N]; n int p, x; n int tn = 1; n long long mx[T], sum[T]; n long long tmx[T], tsum[T]; n inline void pushup(int x) { n   mx[x] = max(sum[x << 1] ? mx[x << 1] : -0x3f3f3f3f3f3f3f3f, n               sum[x << 1 ^ 1] ? mx[x << 1 ^ 1] : -0x3f3f3f3f3f3f3f3f); n   sum[x] = sum[x << 1] + sum[x << 1 ^ 1]; n } n inline void pushdown(int x) { n   if (tmx[x]) { n     if (mx[x << 1] != -0x3f3f3f3f3f3f3f3f) { n       mx[x << 1] += tmx[x]; n     } n     if (x << 1 ^ 1 != -0x3f3f3f3f3f3f3f3f) { n       mx[x << 1 ^ 1] += tmx[x]; n     } n     tmx[x << 1] += tmx[x]; n     tmx[x << 1 ^ 1] += tmx[x]; n     tmx[x] = 0; n   } n   if (tsum[x]) { n     sum[x << 1] += tsum[x]; n     sum[x << 1 ^ 1] += tsum[x]; n     tsum[x << 1] += (tsum[x] >> 1); n     tsum[x << 1 ^ 1] += (tsum[x] >> 1); n     tsum[x] = 0; n   } n } n void update(int l, int r, int x, int y, int nl = 1, int nr = tn, int cur = 1) { n   if (l == nl && r == nr) { n     if (mx[cur] != -0x3f3f3f3f3f3f3f3f) { n       mx[cur] += x; n     } n     tmx[cur] += x; n     sum[cur] += 1ll * y * (nr - nl + 1); n     tsum[cur] += 1ll * y * (nr - nl + 1); n     return; n   } n   pushdown(cur); n   int mid = (nl + nr) >> 1; n   if (r <= mid) { n     update(l, r, x, y, nl, mid, cur << 1); n   } else if (l > mid) { n     update(l, r, x, y, mid + 1, nr, cur << 1 ^ 1); n   } else { n     update(l, mid, x, y, nl, mid, cur << 1); n     update(mid + 1, r, x, y, mid + 1, nr, cur << 1 ^ 1); n   } n   pushup(cur); n } n int query(int l, int r, int x, int nl = 1, int nr = tn, int cur = 1) { n   if (nl == nr) { n     return (sum[cur] && (mx[cur] == x)) ? nl : -1; n   } n   pushdown(cur); n   int mid = (nl + nr) >> 1; n   if (l == nl && r == nr) { n     int res = -1; n     if (sum[cur << 1] > 0 && mx[cur << 1] >= x) { n       res = max(res, query(nl, mid, x, nl, mid, cur << 1)); n     } n     if (sum[cur << 1 ^ 1] > 0 && mx[cur << 1 ^ 1] >= x) { n       res = max(res, query(mid + 1, nr, x, mid + 1, nr, cur << 1 ^ 1)); n     } n     return res; n   } else { n     if (r <= mid) { n       return query(l, r, x, nl, mid, cur << 1); n     } else if (l > mid) { n       return query(l, r, x, mid + 1, nr, cur << 1 ^ 1); n     } else { n       return max(query(l, mid, x, nl, mid, cur << 1), n                  query(mid + 1, r, x, mid + 1, nr, cur << 1 ^ 1)); n     } n   } n } n int query0(int* a) { return a[1] ? -1 : 1; } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     s[i] = s[i - 1] + a[i]; n   } n   while (tn < n) { n     tn <<= 1; n   } n   for (int i = tn; i < tn + n; i++) { n     mx[i] = a[i - tn + 1] - s[i - tn]; n     sum[i] = a[i - tn + 1]; n   } n   for (int i = tn + n; i < tn + tn; i++) { n     mx[i] = -0x3f3f3f3f3f3f3f3f; n     sum[i] = 0; n   } n   for (int i = tn - 1; i; i--) { n     pushup(i); n   } n   while (q--) { n     scanf( %d%d , &p, &x); n     update(p, p, x - a[p], x - a[p]); n     if (p + 1 <= n) { n       update(p + 1, n, -(x - a[p]), 0); n     } n     a[p] = x; n     int ans = max(query(1, n, 0), query0(a)); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MM = 100010; n int N, M; n bool vis[MM]; n int c[MM]; n set<int> edge[MM]; n void get_data() { n   int i, j, k, a, b; n   for (i = 1; i <= N; i++) { n     edge[i].clear(); n     scanf( %d , &c[i]); n   } n   while (M--) { n     scanf( %d%d , &a, &b); n     if (c[b] != c[a]) { n       edge[c[a]].insert(c[b]); n       edge[c[b]].insert(c[a]); n     } n   } n } n void solve() { n   int i, j, k, mx = -1, col = 100001; n   int count; n   sort(c + 1, c + N + 1); n   for (i = 1; i <= N; i++) { n     if (mx == -1 || mx < edge[c[i]].size()) mx = edge[c[i]].size(), col = c[i]; n   } n   printf( %d n , col); n } n int main() { n   while (scanf( %d%d , &N, &M) != EOF) get_data(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = (1e6) + 5; n const int IT_MAX = 1 << 20; n const int INF = -1; n int bit[IT_MAX + 10]; n void update(int p, int dv) { n   for (; p <= IT_MAX; p += p & (-p)) bit[p] += dv; n } n int query(int p) { n   int rv = 0; n   for (; p; p -= p & (-p)) rv += bit[p]; n   return rv; n } n int largerpos(int v) { n   if (bit[IT_MAX] <= v) return INF; n   int p = 0; n   int s = 0; n   for (int i = 19; i >= 0; i--) { n     if (s + bit[p + (1 << i)] <= v) { n       s += bit[p + (1 << i)]; n       p |= (1 << i); n     } n   } n   return p + 1; n } n vector<int> st[MAX], en[MAX]; n int l[MAX], r[MAX]; n int pa[MAX], sz[MAX]; n int finPa(int p) { return pa[p] == p ? p : pa[p] = finPa(pa[p]); } n bool mergeNode(int u, int v) { n   int r1 = finPa(u), r2 = finPa(v); n   if (sz[r1] < sz[r2]) { n     swap(r1, r2); n   } n   pa[r2] = r1; n   sz[r1] += sz[r2]; n   return (r1 == r2); n } n int main() { n   ios::sync_with_stdio(false); n   for (int i = 1; i <= MAX; i++) { n     sz[i] = 1; n     pa[i] = i; n   } n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> l[i] >> r[i]; n     st[l[i]].push_back(i); n     en[r[i]].push_back(i); n   } n   for (int i = 1; i <= 2 * n; i++) { n     for (int s : st[i]) { n       update(l[s], 1); n     } n     for (int s : en[i]) { n       int nex = largerpos(query(l[s])); n       while (nex != -1) { n         for (int ns : st[nex]) { n           if (mergeNode(s, ns)) { n             cout <<  NO  << endl; n             return 0; n           } n         } n         nex = largerpos(query(nex)); n       } n     } n     for (int s : en[i]) { n       update(l[s], -1); n     } n   } n   int p = finPa(1); n   cout << ((sz[p] == n) ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n long long int Mod(long long int n) { return ((n % mod) + mod) % mod; } n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long int f[200005]; n long long int invfact[200005]; n long long int power(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % mod; n     } n     x = (x * x) % mod; n     y >>= 1; n   } n   return res; n } n void precal() { n   f[0] = 1; n   invfact[0] = 1; n   for (int i = 1; i < 200005; i++) { n     f[i] = (f[i - 1] * i) % mod; n     invfact[i] = power(f[i], mod - 2); n   } n } n long long int fact(long long int n, long long int k) { n   if (n < k) { n     return 0; n   } n   return ((f[n] * invfact[k]) % mod * invfact[n - k]) % mod; n } n bool prime[10000011]; n void primenumbers(long long int n) { n   memset(prime, true, sizeof(prime)); n   for (long long int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n long long int modI(long long int a, long long int m) { n   long long int m0 = m, y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long int q = a / m; n     long long int t = m; n     m = a % m; n     a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n int spf[100001]; n void sieve() { n   spf[1] = 1; n   for (int i = 2; i < 100001; i++) spf[i] = i; n   for (int i = 4; i < 100001; i += 2) spf[i] = 2; n   for (int i = 3; i * i < 100001; i++) { n     if (spf[i] == i) { n       for (int j = i * i; j < 100001; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n vector<int> getFactorization(int x) { n   vector<int> ret; n   while (x != 1) { n     ret.push_back(spf[x]); n     x = x / spf[x]; n   } n   return ret; n } n const long long int mxn = 400010; n long long int st[mxn]; n long long int a[mxn]; n void build(long long int v, long long int l, long long int r) { n   if (l == r) { n     st[v] = a[l]; n   } else { n     build(2 * v, l, (l + r) / 2); n     build(2 * v + 1, (l + r) / 2 + 1, r); n     st[v] = st[2 * v] * st[2 * v + 1]; n   } n } n void update(long long int v, long long int l, long long int r, n             long long int pos, long long int val) { n   if (pos < l || pos > r) { n     return; n   } n   if (pos == l && pos == r) { n     st[v] = val; n   } else { n     update(2 * v, l, (l + r) / 2, pos, val); n     update(2 * v + 1, (l + r) / 2 + 1, r, pos, val); n     st[v] = st[2 * v] * st[2 * v + 1]; n   } n } n long long int query(long long int v, long long int l, long long int r, n                     long long int tl, long long int tr, long long int mo) { n   if (tr < l || tl > r || tl > tr) { n     return 1; n   } n   if (l == tl && r == tr) { n     return st[v] % mo; n   } else { n     return (((query(2 * v, l, (l + r) / 2, tl, min(tr, (l + r) / 2), mo)) % n              mo) * n             ((query(2 * v + 1, (l + r) / 2 + 1, r, max(tl, (l + r) / 2 + 1), tr, n                     mo)) % n              mo)) % n            mo; n   } n } n long long int maxSubArraySum(vector<long long int> a, long long int size) { n   long long int max_so_far = INT_MIN, max_ending_here = 0, start = 0, end = 0, n                 s = 0; n   for (long long int i = 0; i < size; i++) { n     max_ending_here += a[i]; n     if (max_so_far < max_ending_here) { n       max_so_far = max_ending_here; n     } n     if (max_ending_here < 0) { n       max_ending_here = 0; n       s = i + 1; n     } n   } n   return max_so_far; n } n int main() { n   fast(); n   long long int n, q; n   cin >> n >> q; n   vector<long long int> d(n + 1, 0); n   for (long long int i = 1; i < n + 1; i++) { n     cin >> d[i]; n   } n   sort(d.begin(), d.end()); n   vector<long long int> pr(n + 2, 0); n   while (q--) { n     long long int x, y; n     cin >> x >> y; n     pr[x]++; n     pr[y + 1]--; n   } n   vector<long long int> get(n + 1, 0); n   for (long long int i = 1; i < n + 1; i++) { n     get[i] = get[i - 1] + pr[i]; n   } n   sort(get.begin(), get.end()); n   long long int ans = 0; n   for (long long int i = 1; i < n + 1; i++) { n     ans = ans + get[i] * d[i]; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using ld = long double; n const ld PI = 4 * atanl(1); n const ld EPS = 1e-12; n const int MN = 1e5 + 10; n struct vec { n  public: n   int x, y; n   void in() { scanf( %d%d , &x, &y); } n   ld mag() const { return hypotl(x, y); } n   ld ang() const { return atan2l(y, x); } n }; n struct Evt { n  public: n   ld t; n   int v; n   Evt(ld _t = 0, int _v = 0) : t(_t), v(_v) {} n   bool operator<(const Evt& o) const { return t < o.t; } n }; n struct BIT { n  public: n   int a[MN * 2], L; n   void reset(int _L) { n     memset(a, 0, L * sizeof *a); n     L = _L; n   } n   void upd(int n, int v) { n     for (++n; n <= L; n += -n & n) a[n - 1] += v; n   } n   int qry(int n) { n     int f = 0; n     for (; n; n -= -n & n) f += a[n - 1]; n     return f; n   } n   int qry(int l, int r) { return qry(r) - qry(l); } n }; n int N, p[MN]; n ll K; n vec a[MN]; n std::vector<Evt> evt; n BIT bit; n ll count(ld d) { n   ll f = (ll)N * (N - 1) / 2; n   int ctr = 0; n   for (int i = 0; i < N; ++i) n     if (d < a[i].mag()) { n       ld m = a[i].ang(); n       ld dif = acosl(d / a[i].mag()); n       ld s = m - dif, e = m + dif; n       if (s + EPS < 0) s += 2 * PI, e += 2 * PI; n       if (2 * PI < e + EPS) e -= 2 * PI; n       if (e < s) std::swap(s, e); n       evt.push_back({s, ctr}); n       evt.push_back({e, ctr}); n       ctr++; n     } n   std::sort(evt.begin(), evt.end()); n   memset(p, -1, ctr * sizeof *p); n   bit.reset(ctr * 2); n   for (int i = 0; i < evt.size(); ++i) { n     int v = evt[i].v; n     if (~p[v]) n       bit.upd(p[v], -1), f -= bit.qry(p[v] + 1, i); n     else n       p[v] = i, bit.upd(i, 1); n   } n   evt.clear(); n   return f; n } n int main() { n   scanf( %d%lld , &N, &K); n   for (int i = 0; i < N; ++i) a[i].in(); n   ld l = -logl(1e8), r = logl(2e4), m; n   for (int i = 0; i < 100; ++i) { n     ld m = (r + l) / 2; n     if (count(exp(m)) < K) n       l = m; n     else n       r = m; n   } n   printf( %.9lf n , (double)exp(r)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tot, a[1001000], ans[1001000 << 2], k[1001000]; n long long s; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]), s += a[i], k[i] = s % n; n   k[m] = n; n   sort(k + 1, k + m + 1); n   for (int i = m; i; i--) k[i] -= k[i - 1]; n   for (int i = 1, j = 1; i <= m; ++i) { n     int x = a[i]; n     while (x > 0) ans[++tot] = i, x -= k[j], j = j % m + 1; n   } n   while (tot % m) ans[++tot] = 1; n   printf( %d n , tot / m); n   for (int i = 1; i <= m; ++i) printf( %d  , k[i]); n   puts(  ); n   for (int i = 1; i <= tot; ++i) { n     printf( %d , ans[i]); n     putchar(i % m ?     :   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   int count = 0; n   int min = a; n   if (b < a) { n     min = b; n   } n   int x = c / min; n   for (int i = 0; i <= x; i++) { n     for (int j = 0; j <= x; j++) { n       if (count != 0) { n         break; n       } n       if (a * i + b * j == c) { n         count++; n         cout <<  Yes ; n       } n     } n   } n   if (count == 0) { n     cout <<  No ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void PT(long long a); n void PT1(long long a); n const long long M = 2 * (1e5) + 7; n long long a[M]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   { n     long long n, k; n     cin >> n >> k; n     if (k * (k - 1) < n) n       return cout <<  NO  n                   <<   n , n              0; n     cout <<  YES  n          <<   n ; n     vector<pair<long long, long long>> v; n     long long i = 1, j = 2; n     while (v.size() < n) { n       if (v.size() == 0) { n         v.push_back({i, j}); n         v.push_back({j, i}); n       } else if ((v.back()).first == i || (v.back()).second == j) { n         v.push_back({j, i}); n         v.push_back({i, j}); n       } else { n         v.push_back({i, j}); n         v.push_back({j, i}); n       } n       if (j == k) { n         i++; n         j = i + 1; n       } else n         j++; n     } n     for (long long i = 0; i < n; i++) { n       cout << v[i].first <<     << v[i].second <<   n ; n     } n   } n   return 0; n } n void PT(long long a) { cout << a <<   n ; } n void PT1(long long a) { cout << a <<    ; } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int u, v; n } e[200050 << 1]; n int fa[200050]; n int n, color[200050], s, d; n int cnt[2], dis[200050]; n bool vis[200050]; n vector<int> G[200050]; n int find(int x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n void _union(int x, int y) { n   int f1 = find(x); n   int f2 = find(y); n   if (f1 == f2) return; n   if (f1 < f2) n     fa[f2] = f1; n   else n     fa[f1] = f2; n } n queue<int> q; n void bfs(int u) { n   memset(vis, false, sizeof(vis)); n   memset(dis, 0, sizeof(dis)); n   while (!q.empty()) q.pop(); n   q.push(u); n   vis[u] = true; n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     for (int i = 0; i < G[now].size(); i++) { n       int v = G[now][i]; n       if (!vis[v]) { n         q.push(v); n         vis[v] = true; n         s = v; n         dis[v] = dis[now] + 1; n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &color[i]); n     fa[i] = i; n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     e[2 * i - 1].u = u; n     e[2 * i - 1].v = v; n     e[i * 2].u = v; n     e[i * 2].v = u; n     if (color[u] == color[v]) _union(u, v); n   } n   for (int i = 1; i <= n; i++) fa[i] = find(i); n   for (int i = 1; i < n; i++) { n     int u = e[i * 2 - 1].u, v = e[i * 2 - 1].v; n     if (fa[u] != fa[v]) { n       G[fa[u]].push_back(fa[v]); n       G[fa[v]].push_back(fa[u]); n     } n   } n   int i = 1; n   for (; G[i].size() == 0 && i <= n; i++) n     ; n   if (i > n) { n     printf( 0 n ); n     return 0; n   } n   bfs(i); n   bfs(s); n   printf( %d n , (dis[s] + 1) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<int> g[100010]; n const int inf = 1e9; n int d[100010], p[100010]; n queue<int> Q; n void bfs(int x) { n   for (int i = 1; i <= n; i++) d[i] = inf; n   d[x] = 0; n   while (!Q.empty()) Q.pop(); n   Q.push(x); n   while (!Q.empty()) { n     int y = Q.front(); n     Q.pop(); n     for (int z : g[y]) { n       if (d[z] < inf) continue; n       d[z] = d[y] + 1; n       p[z] = y; n       Q.push(z); n     } n   } n } n bool dfs(int x, int pa, int c) { n   int d = g[x].size(); n   if (pa) d--; n   if (c) { n     if (d < 3) return 0; n   } else { n     if (d) return 0; n   } n   for (int y : g[x]) { n     if (y == pa) continue; n     if (!dfs(y, x, c - 1)) return 0; n   } n   return 1; n } n int main() { n   cin >> n >> k; n   for (int i = 1, x, y; i < n; i++) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   bfs(1); n   int x = max_element(d + 1, d + n + 1) - d; n   bfs(x); n   int y = max_element(d + 1, d + n + 1) - d; n   int l = d[y]; n   while (2 * d[y] > l) y = p[y]; n   puts(dfs(y, 0, k) ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n void code() { n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if ((a[i] - a[0]) % k != 0) { n       cout <<  -1  << endl; n       return; n     } n     ans += ((a[i] - a[0]) / k); n   } n   cout << ans << endl; n } n signed main() { code(); } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n istream &operator>>(istream &in, pair<T1, T2> &P) { n   in >> P.first >> P.second; n   return in; n } n template <class T1, class T2> n ostream &operator<<(ostream &out, const pair<T1, T2> &P) { n   out <<  (  << P.first <<  ,   << P.second <<  ) ; n   return out; n } n template <class T> n istream &operator>>(istream &in, vector<T> &arr) { n   for (auto &x : arr) in >> x; n   return in; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &arr) { n   for (auto &x : arr) out << x <<    ; n   cout <<   n ; n   return out; n } n template <class T> n istream &operator>>(istream &in, deque<T> &arr) { n   for (auto &x : arr) in >> x; n   return in; n } n template <class T> n ostream &operator<<(ostream &out, const deque<T> &arr) { n   for (auto &x : arr) out << x <<    ; n   cout <<   n ; n   return out; n } n int f(int x, int y) { n   if (x % y == 0) { n     return f(x / y, y); n   } n   return x; n } n int n, k; n int32_t main() { n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   int u = 900000; n   cin >> n >> k; n   vector<int> v(n); n   cin >> v; n   vector<int> stv = v; n   while (u--) { n     vector<pair<int, int> > op; n     stv = v; n     while (v.size() > 1) { n       shuffle(v.begin(), v.end(), rng); n       pair<int, int> nv; n       nv.first = v.back(); n       int second = v.back(); n       v.pop_back(); n       nv.second = v.back(); n       second += v.back(); n       v.pop_back(); n       op.push_back(nv); n       v.push_back(f(second, k)); n     } n     if (v[0] == 1) { n       cout <<  YES  << endl; n       for (auto w : op) { n         cout << w.first <<     << w.second << endl; n       } n       return 0; n     } n     v = stv; n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   long double tp, temp, temp2, div; n   vector<long double> v; n   map<long double, long long int> mp; n   string in; n   double storage; n   cin >> t; n   while (t--) { n     cin >> in; n     storage = 0; n     temp = -1; n     temp2 = -1; n     for (int a1 = 0; a1 < in.size(); a1++) { n       if (isdigit(in[a1])) { n         storage *= 10; n         storage += in[a1] -  0 ; n       } else { n         if (storage != 0) { n           if (temp == -1) { n             temp = storage; n             storage = 0; n             continue; n           } else if (temp2 == -1) { n             temp2 = storage; n             storage = 0; n             continue; n           } n         } n       } n     } n     div = storage; n     tp = (temp + temp2) / div; n     v.push_back(tp); n     mp[tp]++; n   } n   for (int a1 = 0; a1 < v.size(); a1++) { n     if (a1 != v.size() - 1) n       cout << mp[v[a1]] <<    ; n     else n       cout << mp[v[a1]] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<string, string>>::iterator findit( n     vector<pair<string, string>>::iterator st, n     vector<pair<string, string>>::iterator en, string s) { n   while (st != en) { n     if ((*st).second == s) { n       return st; n     } else { n       ++st; n     } n   } n   return en; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int q; n   cin >> q; n   string old, newi; n   long long int i; n   vector<pair<string, string>> v; n   long long int count = 0; n   for (i = 0; i < q; ++i) { n     cin >> old >> newi; n     vector<pair<string, string>>::iterator it = findit(v.begin(), v.end(), old); n     if (it == v.end()) { n       v.push_back(make_pair(old, newi)); n       count++; n     } else { n       (*it).second = newi; n     } n   } n   cout << count <<   n ; n   for (i = 0; i < count; ++i) { n     cout << v[i].first <<     << v[i].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int last = 0; n   int ans = 0; n   string s =   ; n   int l = 0, r = n - 1; n   while (1) { n     int k = INT_MAX; n     if (arr[l] > last) { n       k = arr[l]; n     } n     if (arr[r] > last) { n       k = min(k, arr[r]); n     } n     if (k != arr[l] && k != arr[r]) { n       break; n     } n     ans++; n     last = k; n     if (k == arr[r] && k == arr[l]) { n       int c1 = 0, c2 = 0; n       for (int i = l + 1; i < r; i++) { n         if (arr[i] > arr[i - 1]) { n           c1++; n         } else { n           break; n         } n       } n       c1++; n       c2++; n       for (int i = r - 1; i > l; i--) { n         if (arr[i] > arr[i + 1]) { n           c2++; n         } else { n           break; n         } n       } n       ans += max(c1, c2); n       cout << ans - 1 << endl; n       cout << s; n       if (c1 >= c2) { n         for (int i = 0; i < c1; i++) { n           cout <<  L ; n         } n       } else { n         for (int i = 0; i < c2; i++) { n           cout <<  R ; n         } n       } n       return 0; n     } else if (k == arr[r]) { n       r--; n       s +=  R ; n     } else { n       l++; n       s +=  L ; n     } n     if (l > r) { n       break; n     } n   } n   cout << ans << endl; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   if (c <= b / 2 && c <= a / 2) n     cout <<  First ; n   else n     cout <<  Second ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1001; n long long int n, m, k; n vector<pair<long long int, long long int> > adj[N]; n vector<long long int> func(long long int x) { n   vector<long long int> dis(n + 1, INT_MAX); n   if (x == 0) { n     return dis; n   } n   vector<int> vis(n + 1, 0); n   set<pair<long long int, long long int> > s; n   s.insert(make_pair(0, x)); n   dis[x] = 0; n   while (!s.empty()) { n     long long int v = (*s.begin()).second; n     s.erase(s.begin()); n     if (vis[v]) { n       continue; n     } n     vis[v] = 1; n     for (auto u : adj[v]) { n       if (dis[u.first] > dis[v] + u.second) { n         dis[u.first] = dis[v] + u.second; n         s.insert(make_pair(dis[u.first], u.first)); n       } n     } n   } n   return dis; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> k; n   vector<pair<pair<long long int, long long int>, long long int> > edges; n   for (long long int i = 0; i < m; ++i) { n     long long int a, b, w; n     cin >> a >> b >> w; n     adj[a].push_back(make_pair(b, w)); n     adj[b].push_back(make_pair(a, w)); n     edges.push_back(make_pair(make_pair(a, b), w)); n   } n   vector<pair<long long int, long long int> > queries(k); n   for (long long int i = 0; i < k; ++i) { n     cin >> queries[i].first >> queries[i].second; n   } n   vector<vector<long long int> > dis; n   for (long long int i = 0; i <= n; ++i) { n     dis.push_back(func(i)); n   } n   long long int ans = 0; n   for (long long int i = 0; i < k; ++i) { n     ans += dis[queries[i].first][queries[i].second]; n   } n   for (long long int i = 0; i < m; ++i) { n     long long int a = edges[i].first.first; n     long long int b = edges[i].first.second; n     long long int w = edges[i].second; n     long long int cost = 0; n     for (long long int j = 0; j < k; ++j) { n       long long int x = queries[j].first, y = queries[j].second; n       long long int price = dis[x][y]; n       if (dis[x][a] < dis[x][b] && dis[y][a] > dis[y][b]) { n         price = min(price, dis[x][a] + dis[b][y]); n       } else if (dis[x][b] < dis[x][a] && dis[y][a] < dis[y][b]) { n         price = min(price, dis[x][b] + dis[y][a]); n       } n       cost += price; n     } n     ans = min(ans, cost); n   } n   cout << ans; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long x; n   long long y; n   long long id; n } a[200005]; n long long n, t; n long long totx, toty; n inline void read(long long &x) { n   long long f; n   char c; n   for (f = 1, c = getchar(); c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (x = 0; c <=  9  && c >=  0 ; c = getchar()) x = x * 10 + (c & 15); n   x *= f; n } n inline bool cmp(node p, node q) { return p.y < q.y; } n int main() { n   read(t); n   while (t--) { n     totx = toty = 0; n     read(n); n     for (int i = 1; i < 2 * n; i++) { n       read(a[i].x); n       read(a[i].y); n       a[i].id = (long long)i; n       totx += a[i].x; n     } n     sort(a + 1, a + 2 * n, cmp); n     for (int i = 1; i < 2 * n; i += 2) toty += a[i].x; n     puts( YES ); n     if (toty * 2 >= totx) { n       for (int i = 1; i < 2 * n; i += 2) printf( %lld  , a[i].id); n       puts(  ); n     } else { n       for (int i = 2; i < 2 * n; i += 2) printf( %lld  , a[i].id); n       printf( %lld n , a[2 * n - 1].id); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > G(n + 5, vector<int>(0)); n   int c[m + 5][2]; n   memset(c, (0), sizeof(c)); n   for (int i = 1; i <= m; i++) { n     int l, r; n     cin >> l >> r; n     for (int j = l; j <= r; j++) G[j].push_back(i); n   } n   int ans[n + 5]; n   for (int i = 1; i <= n; i++) { n     int rs = 0, ls = 0; n     int put = 0; n     for (int j = 0; j < int(G[i].size()); j++) { n       int v = G[i][j]; n       rs += c[v][0]; n       ls += c[v][1]; n       if (rs > ls) n         put = 1; n       else n         put = 0; n     } n     ans[i] = put; n     for (int j = 0; j < int(G[i].size()); j++) { n       int v = G[i][j]; n       c[v][put]++; n     } n   } n   for (int i = 1; i <= n; i++) cout << ans[i]; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, r[20], b[20]; n char ch[20]; n long long dp[(1 << 16)][260]; n long long dfs(int bit, int discount) { n   long long& ret = dp[bit][discount]; n   if (ret != -1) return ret; n   if (bit == (1 << n) - 1) { n     return ret = 0; n   } n   ret = 0xffffffffffffff; n   int Rcard = 0, Bcard = 0, cnt = 0; n   ; n   long long Rused = 0, Bused = 0; n   for (int i = 0; i < n; i++) { n     if (bit & (1 << i)) { n       ++cnt; n       Rused += r[i]; n       Bused += b[i]; n       if (ch[i] ==  R ) n         ++Rcard; n       else n         Bcard++; n     } n   } n   int gap = discount - 120; n   long long remainR = 0, remainB = 0; n   if (gap > 0) n     Rused -= gap; n   else n     Bused += gap; n   if (Rused > Bused) { n     remainB = Rused - Bused; n   } else { n     remainR = Bused - Rused; n   } n   for (int i = 0; i < n; i++) { n     if (!(bit & (1 << i))) { n       long long maxi = max((long long)(r[i] - Rcard - remainR), n                            (long long)(b[i] - Bcard - remainB)); n       maxi = max(maxi, (long long)0); n       long long discountR = min(r[i], Rcard); n       long long discountB = min(b[i], Bcard); n       long long go = dfs(bit | (1 << i), discount + discountR - discountB); n       ret = min(ret, go + maxi); n     } n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> ch[i]; n     scanf( %d %d , &r[i], &b[i]); n   } n   long long ans = dfs(0, 120); n   ans += n; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i = 0, j = 0, flag = 0, hash[100002] = {0}, stack[100002] = {0}, n          top = -1; n   string x; n   char item; n   getline(cin, x); n   n = x.length(); n   for (i = 0; i < n; i++) { n     if (x[i] ==  ( ) { n       stack[++top] = i; n     } n     if (x[i] ==  ) ) { n       if (top < 0) { n         flag = 1; n         break; n       } else { n         item = stack[top]; n         top--; n       } n     } n     if (x[i] ==  # ) { n       hash[j] = i; n       j++; n       if (top < 0) { n         flag = 1; n         break; n       } n     } n   } n   if (flag == 1 || stack[top] > hash[j - 1]) { n     cout <<  -1 ; n     return 0; n   } else { n     for (i = 0; i < j - 1; i++) { n       hash[i] = 1; n       item = stack[top]; n       top--; n     } n   } n   if (top < 0) n     cout <<  -1 ; n   else { n     hash[i] = top + 1; n     for (i = 0; i < j; i++) { n       cout << hash[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long phi(long long x); n const long long mod = 1e9 + 7; n int main() { n   long long N, K; n   cin >> N >> K; n   while (K > 0 && N > 1) { n     N = phi(N); n     K -= 2; n   } n   cout << N % mod << endl; n } n long long phi(long long x) { n   int R = sqrt(x); n   long long ans = 1; n   for (int p = 2; p <= R; ++p) { n     if (x % p == 0) { n       long long curr = 1; n       while (x % p == 0) { n         curr *= p; n         x /= p; n       } n       ans *= (curr - curr / p); n     } n   } n   if (x > 1) { n     ans *= (x - 1); n   } n   return ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     string x; n     cin >> n >> x; n     string a(n,  0 ), b(n,  0 ); n     for (long long i = 0; i < n; ++i) { n       if (x[i] ==  1 ) { n         a[i] =  1 ; n         b[i] =  0 ; n         for (int j = i + 1; j < n; ++j) { n           b[j] = x[j]; n         } n         break; n       } else { n         a[i] = b[i] =  0  + (x[i] -  0 ) / 2; n       } n     } n     cout << a << endl << b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } n const int INF = 1 << 30; n const double eps = 1e-8; n const int row = 3000; n const int cow = 220; n const int MAXN = cow * cow; n int n, m; n long long a[50000]; n long long sum[50000]; n char s[5000]; n int main() { n   int i, j, t; n   long long k; n   cin >> k; n   scanf( %s , &s); n   n = strlen(s); n   sum[0] = 0; n   for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + s[i - 1] -  0 ; n   memset(a, 0, sizeof(a)); n   for (i = 0; i <= n; i++) { n     for (j = i + 1; j <= n; j++) { n       a[sum[j] - sum[i]]++; n     } n   } n   long long ans = 0; n   if (k == 0) { n     ans += (a[0] * a[0]); n     for (i = 1; i < 50000; i++) { n       ans += (a[0] * a[i] * 2); n     } n   } else { n     for (i = 1; i <= 40000; i++) { n       if (k % i == 0 && k / i <= 40000) { n         ans += (a[i] * a[k / i]); n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x, y; n     cin >> n >> x >> y; n     vector<int> a(n); n     for (auto &el : a) { n       cin >> el; n       el--; n     } n     int need = y - x; n     vector<vector<int>> where(n + 1); n     for (int i = 0; i < n; i++) { n       where[a[i]].push_back(i); n     } n     int free = 0; n     while (where[free].size()) { n       free++; n     } n     set<pair<int, int>, greater<pair<int, int>>> st; n     for (int i = 0; i < n + 1; i++) { n       st.insert({where[i].size(), i}); n     } n     vector<int> ans(n, free); n     while (x--) { n       auto [sz, x] = *st.begin(); n       st.erase(st.begin()); n       ans[where[x].back()] = x; n       where[x].pop_back(); n       st.insert({sz - 1, x}); n     } n     int d = st.begin()->first; n     vector<pair<int, int>> poses; n     while (st.size()) { n       auto [sz, x] = *st.begin(); n       st.erase(st.begin()); n       while (where[x].size()) { n         poses.emplace_back(where[x].back(), x); n         where[x].pop_back(); n       } n     } n     for (int i = 0; i < poses.size() && need; i++) { n       int j = (i + d) % poses.size(); n       if (poses[j].second != poses[i].second) { n         need--; n         ans[poses[j].first] = poses[i].second; n       } n     } n     if (need) { n       cout <<  NO n ; n       continue; n     } n     cout <<  YES n ; n     for (auto &el : ans) { n       cout << el + 1 <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 10; n struct Edge { n   int u, v, cap, flow = 0; n   Edge(int u, int v, int cap) : u(u), v(v), cap(cap) {} n }; n class Dinic { n  public: n   vector<Edge> edges; n   vector<vector<int>> G; n   int n, m = 0, s, t; n   vector<int> H, ptr; n   queue<int> q; n   Dinic(int n, int s, int t) : n(n), s(s), t(t) { n     G.resize(n + 1); n     H = ptr = vector<int>(n + 1); n   } n   void add(int u, int v, int cap) { n     edges.emplace_back(u, v, cap); n     edges.emplace_back(v, u, 0); n     G[u].push_back(m++); n     G[v].push_back(m++); n   } n   bool bfs() { n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       for (int v : G[u]) { n         int to = edges[v].v; n         int gap = edges[v].cap - edges[v].flow; n         if (H[to] != -1 or gap < 1) continue; n         H[to] = H[u] + 1, q.push(to); n       } n     } n     return H[t] != -1; n   } n   int dfs(int u, int X) { n     if (!X) return 0; n     if (u == t) return X; n     for (int &i = ptr[u]; i < (int)(G[u]).size(); ++i) { n       int v = G[u][i], to = edges[v].v; n       int gap = edges[v].cap - edges[v].flow; n       if (H[to] != (H[u] + 1) or gap < 1) continue; n       int Y = dfs(to, min(X, gap)); n       if (!Y) continue; n       edges[v].flow += Y; n       edges[v ^ 1].flow -= Y; n       return Y; n     } n     return 0; n   } n   int flow() { n     int ans = 0; n     while (true) { n       fill(H.begin(), H.end(), -1); n       H[s] = 0, q.push(s); n       if (!bfs()) break; n       fill(ptr.begin(), ptr.end(), 0); n       while (int X = dfs(s, (int)(1e18))) ans += X; n     } n     return ans; n   } n }; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, pair<int, int>>> edges; n   vector<int> cand; n   for (int i = 0; i < m; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     edges.push_back({w, {u, v}}); n     cand.push_back(w); n   } n   sort((cand).begin(), (cand).end()); n   cand.erase(unique((cand).begin(), (cand).end()), cand.end()); n   int lo = 0, hi = (int)(cand).size() - 1, mid, ans = -1; n   int s = 0, t = 2 * n + 1; n   while (lo <= hi) { n     mid = (lo + hi) >> 1; n     Dinic dinic(2 * n + 2, s, t); n     for (int i = 1; i <= n; ++i) { n       dinic.add(s, i, 1); n       dinic.add(i + n, t, 1); n     } n     for (auto [w, p] : edges) n       if (w <= cand[mid]) dinic.add(p.first, p.second + n, 1); n     int x = dinic.flow(); n     if (x == n) n       hi = mid - 1, ans = cand[mid]; n     else n       lo = mid + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     int arr[n + 1]; n     for (int i = 1; i <= n; i++) { n       cin >> arr[i]; n     } n     int peaks = 0, ind = 1; n     for (int i = 2; i < k; i++) { n       if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) peaks++; n     } n     int ans = peaks; n     for (int j = 2; j + k - 1 <= n; j++) { n       if (arr[j] > arr[j - 1] && arr[j] > arr[j + 1] && ans > 0) ans--; n       if (arr[j + k - 2] > arr[j + k - 1] && arr[j + k - 2] > arr[j + k - 3]) n         ans++; n       if (ans > peaks) { n         peaks = ans; n         ind = j; n       } n     } n     cout << peaks + 1 <<     << ind << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a[n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int x; n       cin >> x; n       x--; n       a[i].push_back(x); n     } n   } n   int ans = 0; n   for (int j = 0; j < m; j++) { n     vector<int> cnt(n); n     int c = 0; n     for (int i = 0; i < n; i++) { n       if (a[i][j] % m != j || a[i][j] / m > n) n         c++; n       else { n         if (i >= a[i][j] / m) n           cnt[i - a[i][j] / m]++; n         else n           cnt[i + n - a[i][j] / m]++; n       } n     } n     int mn = 1e9; n     for (int i = 0; i < n; i++) { n       mn = min(mn, n - cnt[i] + i); n     } n     ans += mn; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int sol = (int)1e9 + 7; n int op, lim; n int ask(int r, int c) { n   op++; n   if (op == lim) { n     cout <<  !   << sol << endl; n     exit(0); n   } n   cout <<  ?   << r <<     << c << endl; n   int x; n   cin >> x; n   sol = min(sol, x); n   return x; n } n int go = 1; n void solve(int r1, int c1, int r2, int c2) { n   if (r1 > r2 || c1 > c2) { n     return; n   } n   if (r1 == r2 && c1 == c2) { n     ask(r1, c1); n     return; n   } n   int rm = (r1 + r2) / 2; n   vector<int> e; n   for (int j = max(go, c1); j <= c2; j++) { n     e.push_back(ask(rm, j)); n   } n   int mn = e[0]; n   for (auto &x : e) mn = min(mn, x); n   for (int j = c1; j <= c2; j++) n     if (e[j - c1] == mn) { n       solve(r1, c1, rm - 1, j); n       go = max(go, j); n       solve(rm + 1, j, r2, c2); n       return; n     } n } n int main() { n   int n, m; n   cin >> n >> m; n   lim = 4 * (n + m); n   solve(1, 1, n, m); n   cout <<  !   << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   map<int, int> set; n   int n, a, b, check1, check2, temp; n   cin >> n >> a >> b; n   int arr[n]; n   vector<int> check; n   for (int i = int(0); i <= int(n - 1); i++) { n     cin >> arr[i]; n     set[arr[i]] = -1; n   } n   if (a == b) { n     for (int i = int(0); i <= int(n - 1); i++) { n       if (set[a - arr[i]] == 0) { n         cout <<  NO  << endl; n         return 0; n       } n     } n     cout <<  YES  << endl; n     for (int i = int(0); i <= int(n - 1); i++) { n       cout <<  0  ; n     } n     return 0; n   } n   for (int i = int(0); i <= int(n - 1); i++) { n     if (set[arr[i]] != -1) continue; n     check.clear(); n     check.push_back(arr[i]); n     check1 = a - arr[i]; n     check2 = b - arr[i]; n     bool f = false; n     while (!f) { n       if (set[check1] == -1 && set[check2] == -1) { n         check.push_back(check1); n         check.push_back(check2); n         temp = check1; n         check1 = a - check2; n         check2 = b - temp; n       } else if (set[check1] == -1) { n         check.push_back(check1); n         f = 1; n         for (int i = int(0); i <= int(check.size() - 1); i++) { n           set[check.at(i)] = 1; n         } n       } else if (set[check2] == -1) { n         check.push_back(check2); n         f = 1; n         for (int i = int(0); i <= int(check.size() - 1); i++) { n           set[check.at(i)] = 2; n         } n       } else { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = int(0); i <= int(n - 1); i++) { n     cout << set[arr[i]] - 1 <<    ; n   } n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int _next[10000000]; n struct event { n   int l, r, x; n   event() {} n   event(int l, int r, int x) : l(l), r(r), x(x) {} n }; n vector<event> e[7005]; n inline bool cross(int l1, int r1, int l2, int r2) { n   if (l1 > r2) return false; n   if (r1 < l2) return false; n   return true; n } n int main() { n   for (int i = 1; i < (1 << 21) - 1; i++) { n     _next[i] = 1; n   } n   for (int i = 0; i < 21; i++) { n     _next[1 << i]++; n   } n   for (int i = 2; i < (1 << 21) - 1; i++) { n     _next[i] += _next[i - 1]; n   } n   cin >> N; n   cin >> M; n   int t, v; n   int n, l, r, x; n   for (int i = 0; i < M; i++) { n     cin >> t; n     if (t == 1) { n       cin >> n >> l >> r >> x; n       e[n].push_back(event(l, r, x)); n     } else { n       set<int> S; n       cin >> n >> v; n       l = v - 1, r = v; n       for (int i = n; i <= N; i++) { n         for (int j = 0; j < e[i].size(); j++) { n           if (cross(l + 1, r, e[i][j].l, e[i][j].r)) S.insert(e[i][j].x); n         } n         l = _next[l], r = _next[r]; n       } n       cout << S.size() << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long count(long long num) { n   long long ans = 0, sum = num, cnt = 0, hest = 0; n   while (sum) { n     hest = sum % 10; n     sum /= 10; n     cnt++; n   } n   cnt--; n   long long k = 1; n   for (long long i = 1; i <= cnt; i++) { n     ans += k; n     k *= 10; n   } n   if (hest > 1) { n     ans += k; n   } else { n     ans += num - k + 1; n   } n   return ans; n } n double pro(long long l, long long r) { n   double ans = 0; n   ans = ((double)(count(r) - count(l - 1))) / ((double)(r - l + 1)); n   return ans; n } n long long l, r; n double p[1005]; n double dp[1005][1005]; n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld%lld , &l, &r); n     p[i] = pro(l, r); n   } n   scanf( %lld , &k); n   dp[0][0] = 1; n   for (long long j = 1; j <= n; j++) { n     dp[0][j] = dp[0][j - 1] * (1.0 - p[j]); n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= n; j++) { n       dp[i][j] = dp[i - 1][j - 1] * p[j] + dp[i][j - 1] * (1 - p[j]); n     } n   } n   double ans = 0; n   for (long long i = (long long)(n * k / 100.0 + 0.999); i <= n; i++) { n     ans += dp[i][n]; n   } n   printf( %.10lf , ans); 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 = 1; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     vector<long long> a(n + 1), pref_a(n + 1); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       pref_a[i] = pref_a[i - 1] + a[i]; n     } n     vector<long long> b(m + 1), pref_b(m + 1); n     for (int i = 1; i <= m; i++) { n       cin >> b[i]; n       pref_b[i] = pref_b[i - 1] + b[i]; n     } n     vector<int> divs; n     for (int i = 1; i * 1ll * i <= k; i++) { n       if (k % i == 0) { n         divs.push_back(i); n         if (k / i != i) { n           divs.push_back(k / i); n         } n       } n     } n     long long ans = 0; n     for (int x : divs) { n       int y = k / x; n       int lin = 0, col = 0; n       for (int i = x; i <= n; i++) { n         if (pref_a[i] - pref_a[i - x] == x) { n           lin++; n         } n       } n       for (int i = y; i <= m; i++) { n         if (pref_b[i] - pref_b[i - y] == y) { n           col++; n         } n       } n       ans += lin * col; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n clock_t t = clock(); n namespace my_std { n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n inline T rnd(T l, T r) { n   return uniform_int_distribution<T>(l, r)(rng); n } n template <typename T> n inline bool chkmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <typename T> n inline bool chkmin(T &x, T y) { n   return x > y ? x = y, 1 : 0; n } n template <typename T> n inline void read(T &t) { n   t = 0; n   char f = 0, ch = getchar(); n   double d = 0.1; n   while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar(); n   while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar(); n   if (ch ==  . ) { n     ch = getchar(); n     while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar(); n   } n   t = (f ? -t : t); n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n char __sr[1 << 21], __z[20]; n int __C = -1, __zz = 0; n inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; } n inline void print(register int x) { n   if (__C > 1 << 20) Ot(); n   if (x < 0) __sr[++__C] =  - , x = -x; n   while (__z[++__zz] = x % 10 + 48, x /= 10) n     ; n   while (__sr[++__C] = __z[__zz], --__zz) n     ; n   __sr[++__C] =   n ; n } n void file() {} n inline void chktime() {} n long long ksm(long long x, int y) { n   long long ret = 1; n   for (; y; y >>= 1, x = x * x) n     if (y & 1) ret = ret * x; n   return ret; n } n }  // namespace my_std n using namespace my_std; n int n, K; n vector<int> bl[30100]; n int ok[30100]; n struct hh { n   int t, nxt; n } edge[30100]; n int head[30100], ecnt; n void make_edge(int f, int t) { n   edge[++ecnt] = (hh){t, head[f]}; n   head[f] = ecnt; n } n int vis[30100]; n int pth[30100], c; n void dfs(int x) { n   for (int &i = head[x], t; (t = i); i = edge[i].nxt) n     if (!vis[i]) vis[i] = 1, dfs(edge[i].t), pth[++c] = edge[t].t; n } n int ask(int x) { n   printf( ? %d n , x); n   fflush(stdout); n   char c; n   cin >> c; n   return c ==  Y ; n } n void add(int id, int flg) { n   for (int i = (0); i <= (K - 1); i++) ok[bl[id][i]] &= !(flg & ask(bl[id][i])); n } n void add2(int id, int flg) { n   int p = -1; n   for (int i = (K - 1); i >= (0); i--) n     if (ok[bl[id][i]]) p = i; n   if (p == -1) { n     for (int i = (0); i <= (K - 1); i++) ask(bl[id][i]); n     return; n   } n   if (p == 0) { n     for (int i = (0); i <= (K - 1); i++) n       if (ok[bl[id][i]]) n         ok[bl[id][i]] &= !(flg & ask(bl[id][i])); n       else n         ask(bl[id][p]); n   } else { n     ok[bl[id][p]] &= !(flg & ask(bl[id][p])); n     for (int i = (1); i <= (p); i++) ask(bl[id][p]); n     for (int i = (p + 1); i <= (K - 1); i++) n       if (ok[bl[id][i]]) n         ok[bl[id][i]] &= !(flg & ask(bl[id][i])); n       else n         ask(bl[id][p]); n   } n } n void work(int t) { n   int n = ::n / K; n   for (int i = (1); i <= (n); i++) n     for (int j = (i + 1); j <= (n); j++) make_edge(i, j); n   for (int i = (1); i <= (n / 2); i++) n     for (int j = (1); j <= (n - 2 * i + 1); j++) make_edge(n - i + 1, i); n   dfs(1); n   pth[++c] = 1; n   reverse(pth + 1, pth + c + 1); n   --c; n   add(1, !t); n   for (int i = (2); i <= (c); i++) n     t ? add2(pth[i], pth[i] > pth[i - 1]) : add(pth[i], pth[i] > pth[i - 1]); n   for (int i = (1); i <= (ecnt); i++) vis[i] = 0; n   c = ecnt = 0; n   for (int i = (1); i <= (n); i++) head[i] = 0; n } n int main() { n   read(n, K); n   for (int i = (1); i <= (n); i++) ok[i] = 1; n   for (int i = (1); i <= (n / K); i++) n     for (int j = ((i - 1) * K + 1); j <= (i * K); j++) bl[i].push_back(j); n   work(0); n   for (int i = (1); i <= (n / K); i++) reverse(bl[i].begin(), bl[i].end()); n   if (K != n) work(1); n   int ans = 0; n   for (int i = (1); i <= (n); i++) ans += ok[i]; n   printf( ! %d n , ans); n   fflush(stdout); 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, m; n     cin >> n >> m; n     vector<string> G(n); n     for (int i = 0; i < n; i++) { n       cin >> G[i]; n     } n     vector<vector<pair<int, int>>> arr(27, vector<pair<int, int>>()); n     bool ret_flag = false; n     bool empty_flag = true; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (G[i][j] ==  . ) continue; n         empty_flag = false; n         auto temp = G[i][j] -  a ; n         if (arr[temp].size() == 1 && n             !(arr[temp][0].first == i || arr[temp][0].second == j)) { n           ret_flag = true; n           break; n         } else if (arr[temp].size() > 1 && n                    !((arr[temp][0].first == i && n                       arr[temp][arr[temp].size() - 1].first == i) || n                      (arr[temp][0].second == j && n                       arr[temp][arr[temp].size() - 1].second == j))) { n           ret_flag = true; n           break; n         } n         arr[temp].push_back(make_pair(i, j)); n       } n       if (ret_flag) break; n     } n     if (empty_flag) { n       cout <<  YES  << endl; n       cout << 0 << endl; n       continue; n     } n     if (ret_flag) { n       cout <<  NO  << endl; n       continue; n     } n     int max_snake = 0; n     for (int i = 25; i >= 0; i--) { n       if (arr[i].size() == 0) continue; n       max_snake = max(max_snake, i); n       if (arr[i][0].first == arr[i][arr[i].size() - 1].first) { n         int r = arr[i][0].first; n         for (int j = arr[i][0].second; j <= arr[i][arr[i].size() - 1].second; n              j++) { n           if (G[r][j] -  a  >= i) continue; n           ret_flag = true; n           break; n         } n         if (ret_flag) break; n       } else if (arr[i][0].second == arr[i][arr[i].size() - 1].second) { n         int c = arr[i][0].second; n         for (int j = arr[i][0].first; j <= arr[i][arr[i].size() - 1].first; n              j++) { n           if (G[j][c] >=  a  + i) continue; n           ret_flag = true; n           break; n         } n         if (ret_flag) break; n       } n       if (ret_flag) break; n     } n     if (ret_flag) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     cout << max_snake + 1 << endl; n     for (int i = 0; i <= 25; i++) { n       int j = i; n       if (arr[i].empty()) { n         while (i < 26 && arr[i].empty()) i++; n       } n       if (i == 26) break; n       cout << arr[i][0].first + 1 <<     << arr[i][0].second + 1 <<     n            << arr[i].back().first + 1 <<     << arr[i].back().second + 1 n            << endl; n       i = j; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int t; n string ssort(string x) { n   if (x.size() & 1) return x; n   int len = x.size() / 2; n   string xx, yy; n   xx = ssort(x.substr(0, len)); n   yy = ssort(x.substr(len, len)); n   if (xx < yy) n     return xx + yy; n   else n     return yy + xx; n } n signed main() { n   cin >> a >> b; n   if (a == b) { n     printf( YES n ); n     return 0; n   } n   if (ssort(a) == ssort(b)) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int T, n; n char s[50]; n int a[50], b[50]; n int main() { n   scanf( %d , &T); n   while (T--) { n     int cnt = 0; n     scanf( %d , &n); n     scanf( %s , s + 1); n     for (int i = 1; i <= n; ++i) a[i] = s[i] -  a , b[i] = a[i]; n     sort(a + 1, a + n + 1); n     for (int i = 1; i <= n; ++i) n       if (a[i] != b[i]) cnt++; n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N_MAX = 1e5, V = (1 << 20); n int n, x[N_MAX], t[N_MAX]; n struct edge { n   int l, v; n }; n vector<edge> G[N_MAX]; n long long cnt_tree[V + 1], total_tree[V + 1]; n void add(int i, long long a, long long tree[]) { n   while (i <= V) { n     tree[i] += a; n     i += (i & -i); n   } n } n long long sum(int i, long long tree[]) { n   long long s = 0; n   while (i) { n     s += tree[i]; n     i -= (i & -i); n   } n   return s; n } n int ind(long long a, long long tree[]) { n   int idx = 0, mask = V; n   a--; n   while (mask && idx < V) { n     int t = idx + mask; n     if (a >= tree[t]) { n       idx = t; n       a -= tree[t]; n     } n     mask >>= 1; n   } n   return idx + 1; n } n long long T, sol[N_MAX]; n void DFS(int v) { n   add(t[v], x[v], cnt_tree); n   add(t[v], (long long)x[v] * t[v], total_tree); n   int i = ind(T, total_tree); n   if (i > V) n     sol[v] = sum(V, cnt_tree); n   else n     sol[v] = sum(i - 1, cnt_tree) + (T - sum(i - 1, total_tree)) / i; n   long long m1 = -1, m2 = -1; n   for (edge e : G[v]) { n     if (T <= e.l) continue; n     T -= e.l; n     DFS(e.v); n     T += e.l; n     if (m1 < sol[e.v]) { n       m2 = m1; n       m1 = sol[e.v]; n     } else if (m2 < sol[e.v]) n       m2 = sol[e.v]; n   } n   if (v == 0) n     sol[v] = max(sol[v], m1); n   else n     sol[v] = max(sol[v], m2); n   add(t[v], -x[v], cnt_tree); n   add(t[v], -(long long)x[v] * t[v], total_tree); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> T; n   for (int i = 0; i < n; i++) cin >> x[i]; n   for (int i = 0; i < n; i++) cin >> t[i]; n   for (int i = 1; i <= n - 1; i++) { n     int p; n     edge e; n     cin >> p >> e.l; n     e.v = i; n     e.l *= 2; n     G[p - 1].push_back(e); n   } n   DFS(0); n   cout << sol[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1100; n const int M = 256; n const int K = 26; n const int INF = 2000000000; n const double eps = 1e-12; n int k, m, n, d[K], a[N]; n char s[N]; n bool ok; n int F(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   k = 1; n   for (int i = 0; i < 26; i++) d[i] = k, k <<= 1; n   for (int i = 0; i < n; i++) { n     cin >> s; n     m = 0; n     for (int j = 0; s[j]; j++) m = m | d[s[j] -  a ]; n     a[i] = m; n   } n   sort(a, a + n); n   m = 1; n   for (int i = 1; i < n; i++) n     if (a[i] != a[i - 1]) m++; n   cout << m; n   return 0; n } n int F() { return 0; } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long 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   int 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 int main() { n   long long test, n, a[55], b[55]; n   scanf( %lld , &test); n   while (test--) { n     scanf( %lld , &n); n     long long mina = 1000000000; n     long long minb = 1000000000; n     for (long long i = 0; i < n; i++) { n       scanf( %lld , &a[i]); n       mina = min(a[i], mina); n     } n     for (long long i = 0; i < n; i++) { n       scanf( %lld , &b[i]); n       minb = min(b[i], minb); n     } n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       long long p = a[i] - mina; n       long long q = b[i] - minb; n       ans += min(p, q); n       ans += (p - min(p, q)) + (q - min(p, q)); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001], p[100001]; n int main() { n   int min = 1000, sum = 0, n, i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d %d , &a[i], &p[i]); n     if (p[i] < min) { n       min = p[i]; n     } n     sum += a[i] * min; n   } n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n const long long MOD = 1e9 + 7; n long long n, m, connected = 0; n long long col[N], vis[N]; n vector<pair<long long, long long> > g[N]; n long long pow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans; n } n void dfs(long long k, long long c) { n   if (vis[k]) { n     if (col[k] != c) { n       cout <<  0 ; n       exit(0); n     } n     return; n   } n   vis[k] = 1; n   col[k] = c; n   for (auto &it : g[k]) { n     dfs(it.first, c ^ (1 - it.second)); n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) { n     long long u, v, t; n     cin >> u >> v >> t; n     g[u].push_back({v, t}); n     g[v].push_back({u, t}); n   } n   for (long long i = 1; i <= n; i++) { n     if (!vis[i]) { n       dfs(i, 0); n       connected++; n     } n   } n   long long ans = pow(2LL, connected - 1, MOD); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 510000; n int n; n int sz[MAXN]; n int was[MAXN]; n int up[MAXN]; n vector<int> eds[MAXN]; n int ans[MAXN]; n void dfs1(int v) { n   sz[v] = 1; n   was[v] = 1; n   for (int u : eds[v]) { n     if (!was[u]) { n       dfs1(u); n       sz[v] += sz[u]; n     } n   } n } n void dfs2(int v) { n   was[v] = 1; n   up[v] = 0; n   if (sz[v] * 2 <= n) up[v] = sz[v]; n   for (int u : eds[v]) { n     if (!was[u]) { n       dfs2(u); n       up[v] = max(up[v], up[u]); n     } n   } n } n void dfs3(int v, int now) { n   ans[v] = 1; n   was[v] = 1; n   if ((n - sz[v]) * 2 <= n) now = max(now, n - sz[v]); n   if ((n - sz[v] - now) * 2 > n) ans[v] = 0; n   int mx1 = 0; n   int mx2 = 0; n   for (int u : eds[v]) { n     if (!was[u]) { n       mx2 = max(mx2, up[u]); n       if (mx2 > mx1) swap(mx1, mx2); n     } n   } n   for (int u : eds[v]) { n     if (!was[u]) { n       if ((sz[u] - up[u]) * 2 > n) ans[v] = 0; n       int go = now; n       if (up[u] == mx1) n         go = max(now, mx2); n       else n         go = max(now, mx1); n       dfs3(u, go); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a; n     --b; n     eds[a].push_back(b); n     eds[b].push_back(a); n   } n   dfs1(0); n   memset(was, 0, sizeof(was)); n   dfs2(0); n   memset(was, 0, sizeof(was)); n   dfs3(0, 0); n   for (int i = 0; i < n; ++i) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n void print_row(int s, int *l, int size) { n   int i = s + 1; n   printf( %d , l[s]); n   while (i < size) printf(  %d , l[i++]); n   i = 0; n   while (i < s) printf(  %d , l[i++]); n   printf(  n ); n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int l[n]; n   l[0] = k; n   for (int i = 1; i < n; i++) l[i] = 0; n   for (int i = 0; i < n; i++) print_row(i, l, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long 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   int 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 int ara[55][55]; n void solve() { n   int n; n   cin >> n; n   vector<pair<int, int> > odds; n   map<pair<int, int>, int> done; n   for (int i = 1; i <= n; i++) { n     odds.push_back({i, n / 2 + 1}); n     done[{i, n / 2 + 1}] = 1; n   } n   for (int j = 1; j <= n; j++) { n     if (!done[{n / 2 + 1, j}]) odds.push_back({n / 2 + 1, j}); n     done[{n / 2 + 1, j}] = 1; n   } n   for (int k = 1; k <= n / 2; k++) { n     int i = k; n     int j = 1; n     while (i < n && j < n) { n       if (!done[{i, j}]) { n         odds.push_back({i, j}); n         done[{i, j}] = 1; n       } n       if (!done[{i, n - j + 1}]) { n         odds.push_back({i, n - j + 1}); n         done[{i, n - j + 1}] = 1; n       } n       if (!done[{n - i + 1, j}]) { n         odds.push_back({n - i + 1, j}); n         done[{n - i + 1, j}] = 1; n       } n       if (!done[{n - i + 1, n - j + 1}]) { n         odds.push_back({n - i + 1, n - j + 1}); n         done[{n - i + 1, n - j + 1}] = 1; n       } n       i++; n       j++; n     } n   } n   for (int i = 0, k = 1; k <= n * n; k += 2, i++) { n     int x = odds[i].first; n     int y = odds[i].second; n     ara[x][y] = k; n   } n   for (int i = 1, k = 2; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (!ara[i][j]) { n         ara[i][j] = k; n         k += 2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) cout << ara[i][j] <<    ; n     cout <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n