#include <bits/stdc++.h> n using namespace std; n long long a[1000001]; n int nodeNumber[1000001], nodeStart[1000001], nodeEnd[1000001], n     leftStart[1000001], leftEnd[1000001], rightStart[1000001], n     rightEnd[1000001]; n void Initialise(int n) { n   sort(a + 1, a + n + 1); n   for (int i = 0; i <= n; ++i) { n     nodeStart[i] = 0; n     nodeEnd[i] = -1; n     nodeNumber[i] = 1; n   } n   nodeStart[1] = 1; n   nodeEnd[1] = n; n } n void FixStartEnd(int &i, int &j) { n   if (i > j) { n     i = 0, j = -1; n   } n } n void FillChildNumbers(int n, int k) { n   for (int i = 0; i <= n; ++i) { n     int j = nodeStart[i]; n     for (; j <= nodeEnd[i] && (a[j] & (1ll << k)) == 0; ++j) n       ; n     leftStart[i] = nodeStart[i], leftEnd[i] = j - 1; n     rightStart[i] = j, rightEnd[i] = nodeEnd[i]; n     FixStartEnd(leftStart[i], leftEnd[i]); n     FixStartEnd(rightStart[i], rightEnd[i]); n     j = (nodeStart[i] > 0) ? nodeEnd[i] : i; n     nodeStart[i] = 0; n     nodeEnd[i] = -1; n     i = j; n   } n } n long long ZeroBitCount(int n, int k) { n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     int targetBit = ((a[i] & (1ll << k)) > 0), j = nodeNumber[i]; n     ans += ((targetBit == 1) ? (rightEnd[j] - rightStart[j] + 1) n                              : (leftEnd[j] - leftStart[j] + 1)); n   } n   return ans; n } n void TakeZeroBit(int n, int k) { n   for (int i = 1; i <= n; ++i) { n     int targetBit = ((a[i] & (1ll << k)) > 0), j = nodeNumber[i], x, start, end; n     if (targetBit) { n       x = rightStart[j], start = rightStart[j], end = rightEnd[j]; n     } else { n       x = leftStart[j], start = leftStart[j], end = leftEnd[j]; n     } n     nodeNumber[i] = x, nodeStart[x] = start, nodeEnd[x] = end; n   } n } n void TakeOneBit(int n, int k) { n   for (int i = 1; i <= n; ++i) { n     int targetBit = 1 - ((a[i] & (1ll << k)) > 0), j = nodeNumber[i], x, start, n         end; n     if (targetBit) { n       x = rightStart[j], start = rightStart[j], end = rightEnd[j]; n     } else { n       x = leftStart[j], start = leftStart[j], end = leftEnd[j]; n     } n     nodeNumber[i] = x, nodeStart[x] = start, nodeEnd[x] = end; n   } n } n int main() { n   int n; n   long long m; n   scanf( %d%lld , &n, &m); n   for (int j = 2; j <= n; ++j) { n     int i; n     long long w; n     scanf( %d%lld , &i, &w); n     a[j] = (w ^ a[i]); n   } n   Initialise(n); n   long long ans = 0; n   for (int k = 61; k > -1; --k) { n     FillChildNumbers(n, k); n     long long cnt = ZeroBitCount(n, k); n     if (cnt >= m) { n       TakeZeroBit(n, k); n     } else { n       ans += (1ll << k); n       m -= cnt; n       TakeOneBit(n, k); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, s, p, i, c; n   scanf( %d , &n); n   s = 1; n   while (1) { n     c = 0; n     p = n * s + 1; n     for (i = 2; i < p; i++) { n       if (p % i == 0) { n         c++; n       } n     } n     if (c >= 1) { n       break; n     } n     s++; n   } n   printf( %d n , s); 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 n, x, c1 = 0, c2 = 0; n   bool flag = false; n   cin >> n; n   while (n--) { n     cin >> x; n     if (x == 100) n       ++c1; n     else n       ++c2; n   } n   if (c1 % 2 == 0) { n     if ((c1 == 0 && c2 % 2 == 0) || (c1 > 0)) flag = true; n   } n   if (flag) { n     cout <<  YES ; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int F(int d); n int G(int d); n int n, ara[2 * 100010], dpf[2 * 100010], dpg[2 * 100010], k; n bool visf[2 * 100010], visg[2 * 100010]; n vector<int> vec; n int add(int _a, int _b) { n   _a = (_a + 998244353) % 998244353; n   _b = (_b + 998244353) % 998244353; n   return (_a + _b) % 998244353; n } n int mul(int _a, int _b) { n   _a = (_a + 998244353) % 998244353; n   _b = (_b + 998244353) % 998244353; n   return ((long long int)((long long int)_a * (long long int)_b)) % 998244353; n } n void input() { n   int i, j; n   scanf( %d %d , &n, &k); n   for (i = 0; i < n; i++) { n     scanf( %d , &ara[i]); n   } n } n int G(int d) { n   int i, j; n   if (d == 0) { n     return 1; n   } n   if (visg[d]) { n     return dpg[d]; n   } n   visg[d] = true; n   int s1 = 0, s2 = 0; n   s1 = F(d - 1) % 998244353; n   s2 = mul(max(k - 2, 0), G(d - 1) % 998244353); n   return dpg[d] = add(s1, s2); n } n int F(int d) { n   int i, j; n   if (d == 0) { n     return 0; n   } n   if (visf[d]) { n     return dpf[d]; n   } n   visf[d] = true; n   return dpf[d] = mul(k - 1, G(d - 1) % 998244353); n } n int sol(int f) { n   int i, j, cnt, ret = 1, s; n   for (i = 0, vec.clear(); i < n; i++) { n     if (i % 2 == f) { n       vec.push_back(ara[i]); n     } n   } n   for (i = 0; i < vec.size(); i = j) { n     int respect = vec[i]; n     for (j = i + 1, cnt = 0; j < vec.size() && vec[j] == -1; j++) { n       cnt++; n     } n     if (j == vec.size()) { n       if (respect == -1) { n         for (j = 0; j < vec.size(); j++) { n           if (j == 0) { n             ret = mul(ret, k); n           } else { n             ret = mul(ret, k - 1); n           } n         } n       } else { n         if (cnt == 0) { n           ret = mul(ret, 1); n         } else { n           s = add(F(cnt - 1) % 998244353, mul(k - 1, G(cnt - 1) % 998244353)); n           ret = mul(ret, s); n         } n       } n     } else { n       if (respect == -1) { n         s = add(F(cnt) % 998244353, mul(k - 1, G(cnt) % 998244353)); n         ret = mul(ret, s); n       } else if (respect == vec[j]) { n         ret = mul(ret, F(cnt) % 998244353); n       } else { n         ret = mul(ret, G(cnt) % 998244353); n       } n     } n   } n   return ret; n } n void solve() { n   int i, j; n   int ret = mul(sol(0), sol(1)); n   printf( %d , ret); n   puts(  ); n } n int main() { n   input(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long int; n const int MOD = 1000000007; n const int MOD1 = 998244353; n const int maxn = 100010; n const int lim = (int)1e9; n struct flowEdge { n   int v, u; n   lli cap, flow = 0ll; n   flowEdge(int v, int u, lli cap) : v(v), u(u), cap(cap) {} n }; n struct Dinic { n   const lli flow_inf = 1e18; n   vector<flowEdge> edges; n   vector<vector<int> > adj; n   int n, m = 0; n   int s, t; n   vector<int> level, ptr; n   queue<int> q; n   Dinic(int n, int s, int t) : n(n), s(s), t(t) { n     adj.resize(n); n     level.resize(n); n     ptr.resize(n); n   } n   void addEdge(int v, int u, lli cap) { n     edges.emplace_back(v, u, cap); n     edges.emplace_back(u, v, 0); n     adj[v].push_back(m); n     adj[u].push_back(m + 1); n     m += 2; n   } n   bool bfs() { n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (auto id : adj[v]) { n         if (edges[id].cap - edges[id].flow < 1) continue; n         if (level[edges[id].u] != -1) continue; n         level[edges[id].u] = level[v] + 1; n         q.push(edges[id].u); n       } n     } n     return level[t] != -1; n   } n   lli dfs(int v, lli pushed) { n     if (pushed == 0ll) return 0ll; n     if (v == t) return pushed; n     for (int &cid = ptr[v]; cid < (int)adj[v].size(); ++cid) { n       int id = adj[v][cid]; n       int u = edges[id].u; n       if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1) n         continue; n       lli tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow)); n       if (tr == 0ll) continue; n       edges[id].flow += tr; n       edges[id ^ 1].flow -= tr; n       return tr; n     } n     return 0ll; n   } n   lli flow() { n     lli f = 0ll; n     while (true) { n       fill(level.begin(), level.end(), -1); n       level[s] = 0; n       q.push(s); n       if (!bfs()) break; n       fill(ptr.begin(), ptr.end(), 0); n       while (lli pushed = dfs(s, flow_inf)) f += pushed; n     } n     return f; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n = 0, m = 0, x = 0, y = 0, w = 0; n   cin >> n >> m; n   vector<pair<int, pair<int, int> > > v; n   for (int i = 0; i < m; ++i) { n     cin >> x >> y >> w; n     --x; n     --y; n     v.push_back({w, {x, y}}); n   } n   sort(v.begin(), v.end()); n   int l = n, r = m; n   while (l != r) { n     int mid = (l + r) / 2; n     Dinic g(2 * n + 2, 2 * n, 2 * n + 1); n     for (int i = 0; i < n; ++i) { n       g.addEdge(2 * n, i, 1); n       g.addEdge(n + i, 2 * n + 1, 1); n     } n     for (int i = 0; i <= mid; ++i) n       g.addEdge(v[i].second.first, n + v[i].second.second, 1); n     if (g.flow() == n) n       r = mid; n     else n       l = mid + 1; n   } n   if (l >= m) n     cout <<  -1 ; n   else n     cout << v[l].first; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, t; n   cin >> n >> t; n   vector<int> books(n); n   for (int i = 0; i < n; i++) { n     cin >> books[i]; n   } n   int i, j; n   i = 0, j = 0; n   int ac = 0; n   int ans = 0; n   while (i < n) { n     if (i > j) j = i; n     while (ac < t && j < n) { n       ac += books[j]; n       j++; n     } n     ans = max(ans, (j - (ac > t)) - i); n     ac -= books[i]; n     i++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n int ans, a[5010], K, n; n map<int, int> g; n int main() { n   int i, j, k, ca = 0, m; n   scanf( %d%d , &n, &K); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n; i++) n     for (j = 1; j <= K; j++) { n       int first = a[i] * j; n       if (!g.count(first)) n         g[first] = j; n       else n         g[first] = min(g[first], j); n     } n   scanf( %d , &m); n   while (m--) { n     int first; n     scanf( %d , &first); n     if (first < a[0]) { n       puts( -1 ); n       continue; n     } else { n       int ans = K + 1; n       if (g.count(first)) ans = min(ans, g[first]); n       for (i = 0; i < n; i++) { n         if (a[i] > first) break; n         int second = min(K, first / a[i]); n         for (j = 1; j < second; j++) { n           int rem = first - j * a[i]; n           if (g.count(rem)) ans = min(ans, j + g[rem]); n         } n       } n       if (ans >= K + 1) ans = -1; n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const double tiaohe = 0.57721566490153286060651209; n long long oula(long long x) { n   long long res = x; n   for (register int i = 2; i <= x / i; ++i) { n     if (x % i == 0) { n       res = res / i * (i - 1); n       while (x % i == 0) x /= i; n     } n   } n   if (x > 1) res = res / x * (x - 1); n   return res; n } n long long quickmod(long long a, long long n, long long m) { n   long long s = 1; n   while (n) { n     if (n & 1) { n       s = s * a % m; n     } n     a = (a * a) % m; n     n = n / 2; n   } n   return s; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void ex_gcd(long long a, long long b, long long &x, long long &y, n             long long &d) { n   if (!b) { n     d = a, x = 1, y = 0; n   } else { n     ex_gcd(b, a % b, y, x, d); n     y -= x * (a / b); n   } n } n long long inv(long long t, long long p) { n   long long d, x, y; n   ex_gcd(t, p, x, y, d); n   return d == 1 ? (x % p + p) % p : -1; n } n bool isPrime(long long x) { n   if (x == 2) return true; n   if (x % 2 == 0) return false; n   for (long long i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n double eqa = (1 + sqrt(5.0)) / 2.0; n const int N = 6e5 + 5; n int t; n int main() { n   cin >> t; n   long long n, w; n   while (t--) { n     cin >> n >> w; n     set<long long> st; n     long long x, y; n     long long mx = 0; n     for (register int i = 1; i <= n; ++i) { n       scanf( %lld%lld , &x, &y); n       mx = max(mx, x); n       if (x - y > 0) st.insert(x - y); n     } n     if (st.empty()) { n       if (mx >= w) n         puts( 1 ); n       else n         puts( -1 ); n     } else { n       auto it = st.end(); n       it--; n       cout << (long long)ceil((max(w - mx, (long long)0)) * 1.0 / (*it)) + 1 n            << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ans[100000]; n int main() { n   long long int n, m, mx, mn, i, j, k, diff, flag = 0, flag1 = 0, flag2 = 0; n   cin >> n >> m >> mn >> mx; n   long long int arr[m + 5]; n   for (i = 0; i < m; i++) { n     cin >> arr[i]; n     if (arr[i] == mn) flag1 = 1; n     if (arr[i] == mx) flag2 = 1; n     if (arr[i] < mn || arr[i] > mx) flag = 1; n   } n   diff = n - m; n   if (flag == 1) n     cout <<  Incorrect  << endl; n   else if (flag1 == 1 && flag2 == 1) n     cout <<  Correct  << endl; n   else if (flag1 == 1 && diff >= 1 || flag2 == 1 && diff >= 1) n     cout <<  Correct  << endl; n   else if (flag1 == 0 && flag2 == 0 && diff >= 2) n     cout <<  Correct  << endl; n   else n     cout <<  Incorrect  << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define int long long n #define ld long double n #define fi first n #define se second n #define vll vector<int> n #define pii pair<int,int> n #define pb push_back n #define sz(v) (int)(v).size() n #define inf (int)(1e18) n #define md (int)(998244353) n #define all(v) (v).begin(),(v).end() n #define rep(i,a,b) for(int i=a;i<b;++i) n using namespace std; n  n const int M = 1e6 + 10; n  n int ans[M],d[M]; n  n int32_t main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(0); n      n     rep(i,1,M)for(int j=i;j<M;j+=i) d[j]++; n      n     ans[1]=1,ans[2]=3; n     int sum=4; n     rep(i,3,M) { n      ans[i]=(sum+d[i])%md; n      (sum+=ans[i])%=md; n     } n     int n; n     cin>>n; n     cout<<ans[n]; n      n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n struct node { n   int u, v, val; n } ans[maxn * 10]; n std::vector<int> g[maxn]; n int n, tot; n int in[maxn]; n int x[maxn], y[maxn], val[maxn]; n void addedge(int u, int v, int val) { ans[++tot] = (node){u, v, val}; } n void find(int u, int fa, int &x3, int &x4) { n   if (g[u].size() == 1) { n     if (x3 == 0) n       x3 = u; n     else n       x4 = u; n     return; n   } else n     for (int i = 0; i < g[u].size(); i++) { n       if (g[u][i] == fa) continue; n       find(g[u][i], u, x3, x4); n     } n } n int main() { n   scanf( %d , &n); n   if (n == 2) { n     printf( YES n1 n ); n     int x, y, k; n     scanf( %d%d%d , &x, &y, &k); n     printf( %d %d %d n , x, y, k); n     return 0; n   } n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &x[i], &y[i], &val[i]); n     g[x[i]].push_back(y[i]); n     g[y[i]].push_back(x[i]); n     in[x[i]]++; n     in[y[i]]++; n   } n   for (int i = 1; i <= n; i++) n     if (in[i] == 2) return !printf( NO n ); n   printf( YES n ); n   for (int k = 1; k < n; k++) { n     if (in[x[k]] > in[y[k]]) swap(x[k], y[k]); n     if (in[x[k]] == 1) { n       int x4 = 0, x1 = 0, x2 = 0, x5 = 0, x3 = 0; n       x1 = y[k]; n       x3 = x[k]; n       find(x1, x3, x4, x5); n       addedge(x3, x4, val[k] / 2); n       addedge(x3, x5, val[k] / 2); n       addedge(x4, x5, -val[k] / 2); n     } else { n       int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0; n       x1 = x[k]; n       x2 = y[k]; n       find(x1, x2, x3, x4); n       find(x2, x1, x5, x6); n       addedge(x3, x5, val[k] / 2); n       addedge(x4, x6, val[k] / 2); n       addedge(x3, x4, -val[k] / 2); n       addedge(x5, x6, -val[k] / 2); n     } n   } n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++) { n     printf( %d %d %d n , ans[i].u, ans[i].v, ans[i].val); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char yam[1 << 23]; n int main() { n   long long b, n, c; n   gets(yam); n   char *bp = yam; n   char *np = strchr(bp,    ) + 1; n   char *cp = strchr(np,    ) + 1; n   c = atoi(cp); n   for (b = 0; isdigit(*bp); bp++) { n     b = (10 * b + (*bp -  0 )) % c; n   } n   long long res = (b - 1 + c) % c; n   int carry = 1; n   for (np = cp - 2; isdigit(*np); np--) { n     int cur = *np -  0  - carry; n     if (cur < 0) { n       cur += 10; n       carry = 1; n     } else n       carry = 0; n     while (cur--) { n       res = (res * b) % c; n     } n     long long oldb = b; n     for (int i = 0; i < 9; i++) { n       b = (b * oldb) % c; n     } n   } n   cout << (res ?: c); n   return 0; n } n


/********   All Required Header Files ********/ n #include <iostream> n #include <string> n #include <vector> n #include <algorithm> n #include <sstream> n #include <queue> n #include <deque> n #include <bitset> n #include <iterator> n #include <list> n #include <stack> n #include <map> n #include <set> n #include <functional> n #include <numeric> n #include <utility> n #include <limits> n #include <time.h> n #include <math.h> n #include <stdio.h> n #include <string.h> n #include <stdlib.h> n #include <assert.h> n  n using namespace std; n /*******  All Required define Pre-Processors and typedef Constants *******/ n #define ll long long int n #define ull unsigned long long int n #define li long int n #define uli unsigned long int n #define MOD 1e10+7 n #define all(a) a.begin(),a.end() n #define FOR(i,j,k,l) for(int i=j;i<k;i+=l) n #define RFOR(i,j,k,l) for(int i=j;i>=k;i-=l) n #define REP(i,j) FOR(i,0,j,1) n #define RREP(i,j) FOR(i,j,0,1) n #define PB push_back n #define _finout ios::sync_with_stdio(0);cin.tie(0) n typedef vector<int> VI; n typedef vector<char> VC; n typedef vector<string> VS; n typedef vector<VI> VVI; n typedef vector<ll> VL; n typedef set<int> SETI; n typedef map<int,int> MPII; n typedef multiset<int> MSETI; n // const int N = (int)(2*1e5 +10); n long long sq(int x){return 1ll * x*x;} n // ll dp[N]; n // ll lev[N]; n // VL adj[N]; n bool isprime(long long int num){ n     for(long long int i=2;i<=sqrt(num);i++){ n         if (num%i == 0) return false; n     } n     return true; n } n vector<int> check(int n,vector<int>a,int x){ n     MSETI s; n     for(auto e:a) s.insert(e); n     vector<int> res; n     for(int i=0;i<n;i++){ n         auto it1 = s.end(); n         it1--; n         int y = x - *it1; n         s.erase(it1); n         auto it2 = s.find(y); n         if(it2==s.end()) return {}; n         res.push_back(x-y); n         res.push_back(y); n         x = max(x-y,y); n         s.erase(it2); n     } n     return res; n } n void solve() n { n     int n; n     cin >> n; n     vector<int> a(2 * n); n     for (int i = 0; i < 2 * n; i++) n         cin >> a[i]; n     sort(a.begin(), a.end()); n     for (int i = 0; i < 2 * n - 1; i++) n     { n         int x = a[i] + a[2 * n - 1]; n         vector<int> res = check(n, a, x); n         if (res.size()) n         { n             cout <<  YES n ; n             cout << x <<   n ; n             for (int j = 0; j < n; j++) n             { n                 cout << res[2 * j] <<     << res[2 * j + 1] <<   n ; n             } n             return; n         } n     } n     cout <<  NO n ; n } n int main(){ n     _finout; n     int T; n     cin>>T; n     while(T--){ n         solve(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n void file() {} n const long long nax = 2e5 + 1, mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   file(); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     n *= 3; n     vector<int> vis(n + 1), cnt(n + 1); n     pair<int, int> p[m + 1]; n     map<pair<int, int>, int> mapp; n     for (long long i = 1; i <= m; i++) { n       cin >> p[i].first >> p[i].second; n       cnt[p[i].first]++; n       cnt[p[i].second]++; n       mapp[make_pair(p[i].first, p[i].second)] = i; n     } n     vector<pair<int, pair<int, int> > > v; n     for (long long i = 1; i <= m; i++) { n       v.push_back(make_pair(cnt[p[i].first] + cnt[p[i].second], n                             make_pair(p[i].first, p[i].second))); n     } n     sort(v.begin(), v.end()); n     vector<int> ans1, ans2; n     for (auto it : v) { n       if (!vis[it.second.first] && !vis[it.second.second]) n         ans1.push_back(mapp[make_pair(it.second.first, it.second.second)]), n             vis[it.second.first] = true, vis[it.second.second] = true; n     } n     for (long long i = 1; i <= n; i++) { n       if (!vis[i]) ans2.push_back(i); n     } n     if (ans1.size() >= n / 3) { n       cout <<  Matching  n            <<   n ; n       int ct = n / 3; n       for (auto it : ans1) { n         ct--; n         cout << it <<    ; n         if (ct == 0) break; n       } n     } else { n       cout <<  IndSet  n            <<   n ; n       int ct = n / 3; n       for (auto it : ans2) { n         ct--; n         cout << it <<    ; n         if (ct == 0) break; n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long ans = 0; n   long long total; n   long long tleft = 0; n   long long tright = 0; n   vector<pair<long long, long long> > soldier; n   for (long long i = 0; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     soldier.push_back(make_pair(a, b)); n     tleft += a; n     tright += b; n   } n   total = abs(tleft - tright); n   for (long long i = 0; i < n; i++) { n     tleft -= soldier[i].first; n     tleft += soldier[i].second; n     tright -= soldier[i].second; n     tright += soldier[i].first; n     if (total < abs(tleft - tright)) { n       total = abs(tleft - tright); n       ans = i + 1; n     } n     tleft += soldier[i].first; n     tleft -= soldier[i].second; n     tright += soldier[i].second; n     tright -= soldier[i].first; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD(1000000007); n const int INF((1 << 30) - 1); n const int MAXN(); n map<string, int> d; n int main() { n   d[ void ] = 0; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     string a, b, c; n     cin >> a; n     if (a ==  typedef ) { n       cin >> b >> c; n       int k1 = 0, k2 = b.size() - 1, m = 0; n       while (k1 < b.size() && b[k1] ==  & ) k1++, m--; n       while (k2 >= 0 && b[k2] ==  * ) k2--, m++; n       string t = b.substr(k1, k2 - k1 + 1); n       if (!d.count(t) || d[t] == -1 || d[t] + m < 0) n         d[c] = -1; n       else n         d[c] = d[t] + m; n     } else { n       cin >> b; n       int k1 = 0, k2 = b.size() - 1, m = 0; n       while (k1 < b.size() && b[k1] ==  & ) k1++, m--; n       while (k2 >= 0 && b[k2] ==  * ) k2--, m++; n       string t = b.substr(k1, k2 - k1 + 1); n       if (!d.count(t) || d[t] == -1 || d[t] + m < 0) n         printf( errtype n ); n       else { n         printf( void ); n         int l = d[t]; n         for (int j = 0; j < l + m; j++) printf( * ); n         printf(  n ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool f[300010]; n map<pair<int, int>, int> mp; n vector<int> g[300010]; n long long nm[300010], kk[300010], x, y, p; n long long ans, n, i; n void go(int now) { n   f[now] = 1; n   for (int i = 0; i < g[now].size(); i++) n     if (!f[g[now][i]]) n       if (nm[now] + nm[g[now][i]] - mp[make_pair(now, g[now][i])] < p && n           p <= nm[now] + nm[g[now][i]]) n         ans -= 2; n   for (int i = 0; i < g[now].size(); i++) n     if (!f[g[now][i]]) go(g[now][i]); n } n int main() { n   cin >> n >> p; n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     mp[make_pair(x, y)]++; n     mp[make_pair(y, x)]++; n     if (mp[make_pair(x, y)] == 1) { n       g[x].push_back(y); n       g[y].push_back(x); n     } n     nm[x]++; n     nm[y]++; n   } n   for (i = 1; i <= n; i++) n     if (2 * nm[i] >= p) ans--; n   for (i = 1; i <= n; i++) kk[nm[i]]++; n   for (i = 300000; i >= 0; i--) kk[i] += kk[i + 1]; n   for (i = 1; i <= n; i++) ans += kk[max(0LL, p - nm[i])]; n   for (i = 1; i <= n; i++) n     if (!f[i]) go(i); n   cout << ans / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   set<long long> v; n   set<long long>::iterator j; n   for (long long i = 1; i <= pow(n, 0.5); i++) { n     if (n % i == 0) { n       long long x = n / i; n       v.insert((x * (2 + (x - 1) * i)) / 2); n       v.insert((i * (2 + (i - 1) * x)) / 2); n     } n   } n   for (j = v.begin(); j != v.end(); j++) { n     cout << *j <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int dcmp(double a, double b) { n   return fabs(a - b) < (1e-9) ? 0 : a < b ? -1 : +1; n } n const int N = 1000 + 9; n int pr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; n int ans[N]; n vector<vector<int> > g; n int x[N]; n bitset<N> del; n int main() { n   int tc; n   scanf( %d , &tc); n   while (tc--) { n     int n; n     scanf( %d , &n); n     for (auto i = 0; i < n; ++i) scanf( %d , x + i); n     for (int cnt = 0, p = 0; cnt != n; p++) { n       g.emplace_back(vector<int>()); n       for (auto i = 0; i < n; ++i) n         if (!del[i] && x[i] % pr[p] == 0) n           del[i] = 1, g.back().emplace_back(i), cnt++; n       if (g.back().empty()) g.pop_back(); n     } n     for (auto i = 0; i < ((int)((g).size())); ++i) n       for (auto j = 0; j < ((int)((g[i]).size())); ++j) ans[g[i][j]] = i; n     printf( %d n , ((int)((g).size()))); n     for (auto i = 0; i < n; ++i) printf( %d  , ans[i] + 1); n     puts(  ); n     del.reset(); n     g.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } n int main() { n   int n, m, x, y, a, b; n   while (cin >> n >> m >> x >> y >> a >> b) { n     int g = gcd(a, b); n     a /= g; n     b /= g; n     g = min(n / a, m / b); n     a *= g; n     b *= g; n     int lx = max(0, x - (a + 1) / 2); n     int ly = max(0, y - (b + 1) / 2); n     int rx = lx + a; n     int ry = ly + b; n     if (rx > n) { n       lx -= rx - n; n       rx = n; n     } n     if (ry > m) { n       ly -= ry - m; n       ry = m; n     } n     if (lx > rx) swap(lx, rx); n     if (ly > ry) swap(ly, ry); n     cout << lx <<     << ly <<     << rx <<     << ry << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > a; n vector<long long> bus; n long long n, t, z; n int main() { n   cin >> n >> t; n   for (int i = 0; i < n; i++) { n     cin >> z; n     t -= z; n   } n   if (t > n - 1 || t < n - 1) { n     cout <<  NO ; n   } else n     cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d; n   cin >> n >> d; n   int a[n]; n   int x = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     x += a[i]; n   } n   if (x + (n - 1) * 10 > d) { n     cout << -1; n   } else { n     cout << 2 * n - 2 + (d - x - (n - 1) * 10) / 5; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[1000000], pref[1000000], suf[1000000]; n int main() { n   long long n, k; n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   for (long long i = 0; i < n; i++) pref[i + 1] = pref[i] + arr[i]; n   long long mini = 1e18; n   for (long long i = 0, j = 0; i < n; i = j) { n     for (; j < n && arr[j] == arr[i]; j++) n       ; n     long long dif = j - i; n     if (dif >= k) { n       cout << 0; n       return 0; n     } n     long long l = ((arr[i] - 1) * i) - pref[i], n               r = pref[n] - pref[j] - ((arr[i] + 1) * (n - j)); n     if (i + dif >= k && mini > l + k - dif) mini = l + k - dif; n     if (dif + n - j >= k && mini > r + k - dif) mini = r + k - dif; n     if (mini > l + r + k - dif) mini = l + r + k - dif; n   } n   cout << mini; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) { n   return {l.first + r.first, l.second + r.second}; n } n typedef void (*callback_function)(void); n const long long INF64 = 1e18; n const int INF32 = 1e9; n const int MOD = 1e9 + 7; n const long double PI = acos(-1.0L); n const long double EPS = static_cast<long double>(1e-9); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   { n     int x; n     cin >> x; n     string s = to_string(x); n     set<char> st; n     for (char &c : s) st.insert(c); n     int sqrtX = sqrt(x), res = 0; n     for (int i = (1); i <= (sqrtX); i++) n       if (x % i == 0) { n         s = to_string(i); n         for (char &c : s) n           if (st.count(c)) { n             res++; n             break; n           } n         int j = x / i; n         if (j == i) break; n         s = to_string(x / i); n         for (char &c : s) n           if (st.count(c)) { n             res++; n             break; n           } n       } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ii = pair<ll, ll>; n using vi = vector<ll>; n using vb = vector<bool>; n using vvi = vector<vi>; n using vii = vector<ii>; n using vvii = vector<vii>; n const int INF = 2000000000; n const ll LLINF = 9000000000000000000; n void fix_mod(ll &v, ll M) { v = ((v - 1) % M + M) % M + 1; } n int main() { n   int N; n   ll M; n   ll T; n   cin >> N >> M >> T; n   vii Si(N, {0LL, 0LL}); n   vi pos(N, 0LL); n   ll shift = 0; n   for (int i = 0; i < N; i++) { n     ll x; n     char c; n     cin >> x >> c; n     ll slope = (c ==  L  ? -1 : 1); n     ll ic = (slope == 1 ? 1 - x : x); n     shift += ((T / M) % N) * slope; n     fix_mod(ic, M); n     if (ic <= T % M) shift += slope; n     Si[i] = {x, i}; n     pos[i] = x + (T % M) * slope; n     fix_mod(pos[i], M); n   } n   sort(Si.begin(), Si.end()); n   sort(pos.begin(), pos.end()); n   fix_mod(shift, N); n   vi ans(N, 0LL); n   for (int i = 0; i < N; i++) ans[Si[i].second] = pos[(i + shift) % N]; n   for (int i = 0; i < N - 1; i++) cout << ans[i] <<    ; n   cout << ans[N - 1] << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int r[10005]; n struct N { n   int l, r, x; n } p[10005]; n bool cmp(N a, N b) { return a.l < b.l; } n bool vis[10005]; n int main() { n   int n, q; n   cin >> n >> q; n   memset(vis, 0, sizeof vis); n   for (int i = 0; i < q; i++) { n     cin >> p[i].l >> p[i].r >> p[i].x; n   } n   sort(p, p + q, cmp); n   for (int i = 0; i < q; i++) { n     for (int j = n - p[i].x; j >= 0; j--) { n       if (r[j] >= p[i].l) n         vis[j + p[i].x] = 1, n                 r[j + p[i].x] = max(r[j + p[i].x], min(p[i].r, r[j])); n     } n     r[p[i].x] = max(r[p[i].x], p[i].r); n     vis[p[i].x] = 1; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (vis[i]) cnt++; n   cout << cnt << endl; n   for (int i = 1; i <= n; i++) { n     if (vis[i]) { n       if (i == n) n         cout << i << endl; n       else n         cout << i <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct nod { n   int x, y; n } num[110000]; n struct nod2 { n   int qian, zhi; n } bian[110000]; n struct nod3 { n   int ls, rs, l, r, mi; n } t[410000]; n int n, p, a, b, mi, i, x[110000], wei[410000], use[110000], use2[110000], n     w[110000]; n int build(int l, int r) { n   int mid, k; n   k = ++p; n   mid = (l + r) / 2; n   t[k].mi = 10000000; n   t[k].l = l; n   t[k].r = r; n   if (l == r) return (k); n   t[k].ls = build(l, mid); n   t[k].rs = build(mid + 1, r); n   return (k); n } n bool cmp(nod a, nod b) { n   if (a.x != b.x) return (a.x > b.x); n   return (a.y > b.y); n } n void build2(int d, int a) { n   p++; n   bian[p].qian = wei[d]; n   bian[p].zhi = a; n   wei[d] = p; n } n int qiu(int d, int l, int r) { n   int mid; n   if (l <= t[d].l && r >= t[d].r) return (t[d].mi); n   mid = (t[d].l + t[d].r) / 2; n   if (l > mid) return (qiu(t[d].rs, l, r)); n   if (r <= mid) return (qiu(t[d].ls, l, r)); n   return (min(qiu(t[d].ls, l, mid), qiu(t[d].rs, mid + 1, r))); n } n void jia(int d, int a, int b) { n   int mid; n   if (t[d].l == t[d].r) { n     t[d].mi = min(t[d].mi, b); n     return; n   } n   mid = (t[d].l + t[d].r) / 2; n   if (a <= mid) jia(t[d].ls, a, b); n   if (a > mid) jia(t[d].rs, a, b); n   t[d].mi = min(t[t[d].ls].mi, t[t[d].rs].mi); n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &num[i].x, &num[i].y); n     use[i] = num[i].y - num[i].x; n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &w[i]); n     use2[i] = w[i]; n   } n   sort(use + 1, use + 1 + n); n   sort(use2 + 1, use2 + 1 + n); n   for (i = 1; i <= n; i++) n     if (use[i] != use2[i]) { n       printf( No n ); n       return (0); n     } n   build(0, 100000); n   p = 0; n   for (i = 1; i <= n; i++) build2(w[i] + 110000, i); n   sort(num + 1, num + 1 + n, cmp); n   for (i = 1; i <= n; i++) { n     a = 110000 + num[i].y - num[i].x; n     b = wei[a]; n     mi = qiu(1, num[i].y, 100000); n     if (bian[b].zhi > mi) { n       printf( No n ); n       return (0); n     } n     jia(1, num[i].y, bian[b].zhi); n     x[bian[b].zhi] = i; n     wei[a] = bian[b].qian; n   } n   printf( Yes n ); n   for (i = 1; i <= n; i++) printf( %d %d n , num[x[i]].x, num[x[i]].y); n } n


#include <bits/stdc++.h> n using namespace std; n long long m, l, r, k, n; n long long a[2][2], b[2][2]; n void cc(long long a[2][2], long long b[2][2]) { n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) a[i][j] = b[i][j]; n } n void mul(long long a[2][2], long long b[2][2], long long m) { n   long long c[2][2]; n   memset(c, 0, sizeof(c)); n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) n       for (int k = 0; k < 2; k++) c[i][j] += a[i][k] * b[k][j], c[i][j] %= m; n   cc(a, c); n } n void mi(long long a[2][2], long long n, long long m) { n   long long y[2][2]; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) y[i][j] = 0; n   y[0][0] = 1; n   y[1][1] = 1; n   while (n > 0) { n     if (n % 2 == 1) mul(y, a, m); n     n = n / 2; n     mul(a, a, m); n   } n   cc(a, y); n } n int main() { n   cin >> m >> l >> r >> k; n   n = -1; n   for (long long i = k;; i++) { n     long long x = r / i; n     long long y = r / x; n     long long z = (l - 1) / x; n     if (x <= 1000000) break; n     if (y - z >= k) { n       n = x; n       break; n     } n   } n   if (n == -1) n     for (long long i = 1000000; i >= 1; i--) { n       long long y = r / i; n       long long z = (l - 1) / i; n       if (y - z >= k) { n         n = i; n         break; n       } n     } n   a[0][1] = 1; n   a[1][0] = 1; n   a[1][1] = 1; n   b[0][1] = 1; n   b[0][0] = 1; n   mi(a, n - 2, m); n   mul(b, a, m); n   cout << b[0][1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int total; n   cin >> total; n   while (total--) { n     int n, k, cont_l = 0, cont_r = 0; n     vector<pair<int, int> > ans; n     string s; n     cin >> n >> k; n     cin >> s; n     for (int i = 0; i < s.size(); i++) { n       if ((i + 2) / 2 < k) { n         if (i % 2 == 0) { n           if (s[i] !=  ( ) { n             for (int j = i + 1; j < s.size(); j++) { n               if (s[j] ==  ( ) { n                 ans.push_back(make_pair(i, j)); n                 int l = i, r = j; n                 while (l < r) { n                   swap(s[l], s[r]); n                   l++; n                   r--; n                 } n                 break; n               } n             } n           } n         } else { n           if (s[i] !=  ) ) { n             for (int j = i + 1; j < s.size(); j++) { n               if (s[j] ==  ) ) { n                 ans.push_back(make_pair(i, j)); n                 int l = i, r = j; n                 while (l < r) { n                   swap(s[l], s[r]); n                   l++; n                   r--; n                 } n                 break; n               } n             } n           } n         } n       } else { n         int cont = (n - (k - 1) * 2); n         if (cont_l < cont) { n           if (s[i] !=  ( ) { n             for (int j = i + 1; j < s.size(); j++) { n               if (s[j] ==  ( ) { n                 ans.push_back(make_pair(i, j)); n                 int l = i, r = j; n                 while (l < r) { n                   swap(s[l], s[r]); n                   l++; n                   r--; n                 } n                 break; n               } n             } n           } n         } else { n           if (s[i] !=  ) ) { n             for (int j = i + 1; j < s.size(); j++) { n               if (s[j] ==  ) ) { n                 ans.push_back(make_pair(i, j)); n                 int l = i, r = j; n                 while (l < r) { n                   swap(s[l], s[r]); n                   l++; n                   r--; n                 } n                 break; n               } n             } n           } n         } n       } n     } n     cout << ans.size() << endl; n     for (int i = 0; i < ans.size(); i++) { n       cout << ans[i].first + 1 <<     << ans[i].second + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 1e18; n const long long minN = -1e18; n const long long MOD = 1e9 + 7; n const long long MOD1 = 998244353; n const int baseHash = 331; n const int bigNumLength = 5000; n const long double PI = acos(-1); n const long long limit = 2e5 + 5; n const long long limit1 = 1e6 + 5; n const long long limit2 = 1e3 + 5; n pair<long long, long long> dir[] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0}, n                                     {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n int n, m; n map<int, bool> check[limit]; n set<int> s; n int root[limit]; n int cur[limit]; n int d = 0; n bool compo[limit]; n long long ans = 0; n int findRoot(int u) { return (root[u] < 0) ? u : findRoot(root[u]); } n void mergeRoot(int u, int v) { n   u = findRoot(u); n   v = findRoot(v); n   if (u == v) return; n   if (root[u] > root[v]) swap(u, v); n   root[u] += root[v]; n   root[v] = u; n } n void solveProblem() { n   queue<int> q; n   vector<int> cur; n   memset(root, -1, sizeof(root)); n   for (int i = (1); i <= (n); ++i) s.insert(i); n   for (int i = (1); i <= (n); ++i) { n     if (s.count(i)) { n       s.erase(i); n       q.push(i); n       ++ans; n       while (!q.empty()) { n         int u = q.front(); n         q.pop(); n         for (auto it : s) { n           if (check[it][u]) continue; n           cur.push_back(it); n           mergeRoot(u, it); n           q.push(it); n         } n         for (auto it : cur) s.erase(it); n         cur.clear(); n       } n     } n   } n   cout << ans - 1 <<   n ; n } n void fastInput() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void readInput() { n   cin >> n >> m; n   for (int i = (1); i <= (m); ++i) { n     int u, v; n     cin >> u >> v; n     check[u][v] = true; n     check[v][u] = true; n   } n } n int main() { n   fastInput(); n   readInput(); n   solveProblem(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 10, N = 2e6 + 10, mod = 1e9 + 7, LG = 20, SQ = 450; n long long n, T[N], L[N], dp[N], ps[N]; n inline void KMP(string s) { n   long long k = -1; n   T[0] = -1; n   for (int i = 1; i < s.size(); i++) { n     while (k != -1 and s[k + 1] != s[i]) k = T[k]; n     if (s[k + 1] == s[i]) k++; n     T[i] = k; n   } n } n string s, t; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s >> t; n   n = s.size(); n   KMP(t +  #  + s); n   for (int i = t.size() + 1; i < s.size() + t.size() + 1; i++) n     T[i - t.size() - 1] = T[i] + 1; n   L[0] = -1; n   for (int i = 0; i < n; i++) { n     L[i + 1] = L[i]; n     if (T[i] == t.size()) L[i + 1] = i - t.size() + 1; n   } n   dp[0] = 1; n   ps[0] = 1; n   for (int i = 1; i <= n; i++) { n     dp[i] = dp[i - 1]; n     if (L[i] > -1) dp[i] = (ps[L[i]] + dp[i]) % mod; n     ps[i] = (ps[i - 1] + dp[i]) % mod; n   } n   cout << ((dp[n] + mod - 1) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   cin >> n >> m; n   if (m == 3 && n >= 5) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= m; i++) { n     cout << i <<     << i * i + 10000000 << endl; n   } n   for (int i = 1; i <= n - m; i++) { n     cout << i <<     << -i * i - 10000000 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n int n, d, a[N], dp[N * 10005], gao[N * 10005]; n vector<int> v; n int main() { n   int sum = 0; n   cin >> n >> d; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   dp[0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = sum; j >= a[i]; j--) { n       dp[j] |= dp[j - a[i]]; n     } n   } n   for (int i = 0; i <= sum; i++) { n     if (dp[i]) v.push_back(i); n   } n   memset(gao, 0x11, sizeof(dp)); n   int now = 0, step = 0; n   while (true) { n     if (now == v.size() - 1) break; n     int nxt = now; n     while (nxt + 1 < v.size() && v[nxt + 1] - v[now] <= d) nxt++; n     if (nxt == now) break; n     now = nxt; n     step++; n   } n   cout << v[now] <<     << step << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n long long arr[200010]; n long long factorial[200010]; n long long factinv[200010]; n long long fact[200010]; n long long ans; n long long pw(long long a, long long b) { n   a %= 1000000007; n   if (b == 0) { n     return 1ll; n   } n   if (b == 1) { n     return a; n   } n   long long res = pw(a, b / 2); n   if (b % 2) { n     return (((res * res) % 1000000007) * a) % 1000000007; n   } n   return (res * res) % 1000000007; n } n long long inv(long long a) { return pw(a, 1000000007 - 2); } n long long choose(long long a, long long b) { n   return ((factorial[a] * factinv[b] % 1000000007) * factinv[a - b]) % n          1000000007; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   if (fopen( cf815b.in ,  r )) { n     freopen( cf815b.in ,  r , stdin); n     freopen( cf815b.out ,  w , stdout); n   } n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> arr[i]; n     arr[i] %= 1000000007; n   } n   factorial[0] = 1; n   for (int i = 1; i <= N; i++) { n     factorial[i] = factorial[i - 1] * i; n     factorial[i] %= 1000000007; n   } n   for (int i = 0; i <= N; i++) { n     factinv[i] = inv(factorial[i]); n   } n   if (N % 4 == 0) { n     long long choosev = (N - 2) / 2; n     for (int i = 0; i < N; i++) { n       fact[i] = choose(choosev, i / 2); n       if (i % 2) { n         fact[i] *= (1000000007 - 1); n       } n       fact[i] %= 1000000007; n     } n   } n   if (N % 4 == 1) { n     long long choosev = (N - 1) / 2; n     for (int i = 0; i < N; i += 2) { n       fact[i] = choose(choosev, i / 2); n     } n   } n   if (N % 4 == 2) { n     long long choosev = (N - 2) / 2; n     for (int i = 0; i < N; i++) { n       fact[i] = choose(choosev, i / 2); n     } n   } n   if (N % 4 == 3) { n     long long choosev = (N - 3) / 2; n     fact[0] = 1; n     fact[N - 1] = 1000000007 - 1; n     for (int i = 1; i < N - 1; i++) { n       if (i % 2) { n         fact[i] = choose(choosev, (i - 1) / 2) + choose(choosev, (i - 1) / 2); n       } else { n         fact[i] = choose(choosev, (i / 2)) - choose(choosev, (i - 1) / 2); n       } n       fact[i] %= 1000000007; n       fact[i] += 1000000007; n       fact[i] %= 1000000007; n     } n   } n   for (int i = 0; i < N; i++) { n     cerr << fact[i] <<    ; n     ans += fact[i] * arr[i]; n     ans %= 1000000007; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct seg { n   int l, r, i; n   bool operator<(const seg& b) const { return l < b.l; } n }; n int n, k; n seg a[200005]; n bool u[400005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i].l >> a[i].r, a[i].i = i; n   sort(a, a + n); n   set<pair<int, int>> pq; n   for (int i = 0; i < k; i++) pq.insert({0, n + i}); n   for (int i = 0; i < n; i++) { n     auto it = pq.lower_bound({a[i].l, -1}); n     if (it == pq.begin()) { n       it = --pq.end(); n       if (a[i].r < it->first) { n         u[it->second] = 1; n         pq.erase(it); n         pq.insert({a[i].r, a[i].i}); n       } else { n         u[a[i].i] = 1; n       } n     } else { n       --it; n       pq.erase(it); n       pq.insert({a[i].r, a[i].i}); n     } n   } n   cout << count(u, u + n, 1) <<   n ; n   for (int i = 0; i < n; i++) n     if (u[i]) cout << i + 1 <<    ; n   cout <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 3e3 + 10; n int n; n int a[N], b[N]; n long long dp[N][N]; n long long solve(int idx = 0, int val = 0) { n   if (idx == n) return 0; n   if (val == n) return 1e18; n   long long &ret = dp[idx][val]; n   if (ret == -1) n     ret = min(solve(idx + 1, val) + abs(a[idx] - b[val]), solve(idx, val + 1)); n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i] -= i; n   } n   sort(b, b + n); n   memset(dp, -1, sizeof(dp)); n   printf( %lld n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string ans, s; n   int n, t, p; n   char ch; n   scanf( %d , &n); n   ans.clear(); n   for (int i = 1; i <= n; i++) { n     t = i; n     s.clear(); n     while (t > 0) { n       p = t % 10; n       ch = p +  0 ; n       s = ch + s; n       t = t / 10; n     } n     ans = ans + s; n   } n   cout << ans[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, zhan[100500], tree[100500], o, ans[100500], now = 1; n struct edge { n   int x, t, id, r; n   bool operator<(const edge &xx) const { n     if (x != xx.x) return x < xx.x; n     return id < xx.id; n   } n } aa[100500]; n void add(int x, int y) { n   for (int i = x; i <= n; i += (i & (-i))) tree[i] += y; n } n int get(int x) { n   int ans = 0; n   for (int i = x; i >= 1; i -= (i & (-i))) ans += tree[i]; n   return ans; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &aa[i].r, &aa[i].t, &aa[i].x); n     aa[i].id = i; n     zhan[++o] = aa[i].t; n   } n   sort(zhan + 1, zhan + o + 1); n   o = unique(zhan + 1, zhan + o + 1) - zhan - 1; n   for (int i = 1; i <= n; i++) { n     aa[i].t = lower_bound(zhan + 1, zhan + o + 1, aa[i].t) - zhan; n   } n   sort(aa + 1, aa + n + 1); n   memset(ans, -1, sizeof(ans)); n   for (int i = 1; i <= n; i++) { n     if (aa[i].x != aa[i - 1].x) n       while (now != i) { n         if (aa[now].r == 1) add(aa[now].t, -1); n         if (aa[now].r == 2) add(aa[now].t, 1); n         now++; n       } n     if (aa[i].r == 1) add(aa[i].t, 1); n     if (aa[i].r == 2) add(aa[i].t, -1); n     if (aa[i].r == 3) ans[aa[i].id] = get(aa[i].t); n   } n   for (int i = 1; i <= n; i++) n     if (ans[i] != -1) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, cnt, num[100010], w[100010]; n int x[100010], y[100010]; n struct edge { n   int w, f, id; n } e[100010]; n inline bool operator<(edge a, edge b) { return a.w < b.w; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &e[i].w, &e[i].f); n     e[i].id = i; n   } n   sort(e + 1, e + m + 1); n   for (int i = 1; i <= m; i++) { n     if (e[i].f) { n       cnt++; n       num[cnt] = cnt - 1; n       w[cnt] = e[i].w; n       x[e[i].id] = cnt; n       y[e[i].id] = cnt + 1; n     } n   } n   int now = cnt; n   for (int i = m; i >= 1; i--) { n     if (e[i].f) continue; n     while (w[now] > e[i].w && now) now--; n     if (now == 0 || now == 1) { n       puts( -1 ); n       return 0; n     } n     y[e[i].id] = now + 1; n     x[e[i].id] = num[now]; n     num[now]--; n     if (num[now] == 0) now--; n   } n   for (int i = 1; i <= m; i++) { n     printf( %d %d n , x[i], y[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     int ans = 0; n     int sum = 0; n     while (n--) { n       int x; n       scanf( %d , &x); n       if (x == -1) { n         if (!sum) n           ans++; n         else n           sum--; n       } else n         sum += x; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma gcc optimize( O3, unroll-loops ) n using namespace std; n const int mod = 1000000007; n int gcds[4001][4001]; n int64_t fpow(int i, int j) { n   int64_t ans = 1; n   while (j--) ans *= i; n   return ans; n } n signed main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   int64_t n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) gcds[i][0] = i; n   for (int i = 1; i <= m; i++) gcds[0][i] = i; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       gcds[i][j] = i > j ? gcds[i % j][j] : gcds[i][j % i]; n   int64_t cnts = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int g = gcds[i][j]; n       int64_t targ = min((n - i) / (i / g), (m - j) / (j / g)); n       (cnts += targ * (targ + 1) * (2 * targ + 1) / 6 * (i / g) * (j / g) + n                (n - i + 1) * (m - j + 1) * targ - n                targ * (targ + 1) / 2 * n                    ((n - i + 1) * (j / g) + (m - j + 1) * (i / g))) %= mod; n     } n   cnts = (cnts * 2 * 6 + (n + 1) * (m + 1) * (m * (m - 1) + n * (n - 1)) + n           (n + 1) * (m + 1) * ((n + 1) * (m + 1) - 1) * 3 + (n + 1) * (m + 1)) % n          mod; n   int64_t ans = 0; n   for (int i = 0; i < (1 << 6); i++) n     if (~(i >> 2 & i >> 4 ^ i >> 0 & i >> 5 ^ i >> 1 & i >> 3 ^ n           i >> 2 & i >> 3 ^ i >> 0 & i >> 4 ^ i >> 1 & i >> 5) & n         1) n       ans += fpow((n + 1) / 2, __builtin_popcount(i & 7)) % mod * n              fpow((n + 2) / 2, 3 - __builtin_popcount(i & 7)) % mod * n              fpow((m + 1) / 2, __builtin_popcount(i >> 3)) % mod * n              fpow((m + 2) / 2, 3 - __builtin_popcount(i >> 3)) % mod; n   cout << ((ans - cnts) % mod + mod) % mod << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (!b) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<long long int> a(n); n     vector<bool> vis(n); n     deque<long long int> b; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n; i++) { n       bool x; n       cin >> x; n       vis[i] = x; n       if (!vis[i]) b.push_back(a[i]); n     } n     sort(b.begin(), b.end(), greater<long long int>()); n     for (int i = 0; i < n; i++) { n       if (!vis[i]) { n         cout << b.front() <<    ; n         b.pop_front(); n       } else { n         cout << a[i] <<    ; n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, n1, ans, m, per1, per2, add; n const int maxn = 1000007; n long long tree[maxn * 4][4]; n void build() { n   for (int j = n1 - 1; j < n1 + n - 1; j++) { n     tree[j][0] = j - n1 + 2; n     tree[j][1] = j - n1 + 2; n     tree[j][2] = 0; n     tree[j][3] = 0; n   } n   for (int j = n1 + n - 1; j < 2 * n1 - 1; j++) { n     tree[j][0] = j - n1 + 2; n     tree[j][1] = j - n1 + 2; n     tree[j][2] = 0; n     tree[j][3] = 0; n   } n   for (int j = n1 - 2; j > -1; j -= 1) { n     tree[j][0] = tree[j * 2 + 1][0]; n     tree[j][1] = tree[j * 2 + 2][1]; n     tree[j][2] = 0; n     tree[j][3] = 0; n   } n } n void push(int node) { n   tree[2 * node + 1][3] += tree[node][3]; n   tree[2 * node + 2][3] += tree[node][3]; n   tree[node][3] = 0; n } n void update(int node, int a, int b, int val) { n   int l, r; n   l = tree[node][0]; n   r = tree[node][1]; n   if (l == a and r == b) { n     tree[node][3] += val; n   } else if (r >= a and b >= l) { n     push(node); n     update(2 * node + 1, a, min(b, (l + r) / 2), val); n     update(2 * node + 2, max(a, (l + r) / 2 + 1), b, val); n     tree[node][2] = (tree[node * 2 + 1][2] + tree[node * 2 + 1][3] + n                      tree[node * 2 + 2][2] + tree[node * 2 + 2][3]); n   } n } n int query(int node, int a, int b) { n   int l, r; n   l = tree[node][0]; n   r = tree[node][1]; n   if (r < a || b < l) { n     return 0; n   } n   if (l == a && r == b) { n     return tree[node][2] + tree[node][3]; n   } n   push(node); n   ans = (query(node * 2 + 1, a, min(b, (l + r) / 2)) + n          query(node * 2 + 2, max(a, (l + r) / 2 + 1), b)); n   tree[node][2] = (tree[2 * node + 1][2] + tree[2 * node + 1][3] + n                    tree[2 * node + 2][2] + tree[2 * node + 2][3]); n   return ans; n } n int main() { n   cin >> n >> k; n   n1 = pow(2, ceil(log2(n))); n   ; n   build(); n   long long answer = 1; n   int now = 1; n   for (int j = 0; j < n; j++) { n     update(0, now, now, 1); n     per1 = 0; n     per2 = 0; n     if (now + k > n) { n       if (k - 1 <= (n - 2) / 2) { n         if (now != n) { n           per2 = query(0, now + 1, n); n         } n         if (now + k - n >= 2) { n           per1 = query(0, 1, now + k - 1 - n); n         } n         answer += (1 + per1 + per2); n       } else { n         answer += (query(0, now + 1 + k - n, now - 1) + 1); n       } n       now = now + k - n; n     } else { n       if (k - 1 <= (n - 2) / 2) n         answer += (query(0, now + 1, now + k - 1) + 1); n       else { n         if (now + k != n) { n           per2 = query(0, now + k + 1, n); n         } n         if (now >= 2) { n           per1 = query(0, 1, now - 1); n         } n         answer += (1 + per1 + per2); n       } n       now += k; n     } n     cout << answer <<    ; n     update(0, now, now, 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n int dblcmp(double d) { n   if (fabs(d) < eps) return 0; n   return (d > 0) ? 1 : -1; n } n int n, m, T; n bool win(long long a, long long b) { n   if (a * b == 0) return false; n   if (a > b) swap(a, b); n   if (!win(a, b % a)) return 1; n   return ((b / a) % (a + 1)) % 2 == 0; n } n int main() { n   cin >> T; n   while (T--) { n     long long a, b; n     cin >> a >> b; n     if (win(a, b)) n       printf( First n ); n     else n       printf( Second n ); n   } n   return 0; n } n


#include<bits/stdc++.h> n  n #define N 5000007 n #define M 200007 n #define ll long long n  n const ll Mod=1000000007; n using namespace std; n inline int read() n { n  int x=0,f=1;char ch=getchar(); n  while(!isdigit(ch)){if(ch== - )f=-1;ch=getchar();} n  while(isdigit(ch)){x=(x<<1)+(x<<3)+ch- 0 ;ch=getchar();} n  return x*f; n } n  n ll n,m,seed,base,tem; n ll num1,num2,ans; n ll a[N],t[N],hh[N]; n ll p[M],k[M],b[M],w[M]; n ll T1[N],T2[N],c1,c2; n  n ll rnd() n { n  int ret=seed; n  seed=1ll*(1ll*seed*base+233)%1000000007; n  return ret; n } n int main() n { n  n=read(),m=read(); n  num1=num2=0; n  for (int i=1;i<=m;i++) n   p[i]=read(),k[i]=read(),b[i]=read(),w[i]=read(); n  for (int i=1;i<=m;i++) n  { n   seed=b[i],base=w[i]; n   for (int j=p[i-1]+1;j<=p[i];j++) n   { n    t[j]=(rnd()%2)+1; n    a[j]=(rnd()%k[i])+1; n    hh[j]=a[j]; n    if (t[j]==1) num1+=a[j]; n    else num2+=a[j]; n   } n  } n // cout<<num1<<   <<num2<<endl;  n  //for (int i=1;i<=n;i++) n  // cout<<a[i]<<   <<t[i]<<endl; n /* for (int i=1;i<=n;i++) n   a[i]=hh[i]=read(); n  for (int i=1;i<=n;i++) n   t[i]=read(); n  for (int i=1;i<=n;i++) n   if (t[i]==1) num1+=a[i]; n   else num2+=a[i];*/ n  if (num1==0||num2==0) n  { n   ans=1; n   if (num1!=0) n   { n    ans=1ll*ans*(((1)^(1ll*1*1))%Mod+1)%Mod; n    for (int i=2;i<=n;i++) n     ans=1ll*ans*(((0)^(1ll*i*i))%Mod+1)%Mod; n    printf( %lld n ,ans); n   } n   else n   { n    for (int i=1;i<=n;i++) n     ans=1ll*ans*(((0)^(1ll*i*i))%Mod+1)%Mod; n    printf( %lld n ,ans); n   } n  } n  else n  { n   if (num1>num2) tem=2; n   else tem=1; n   n  // cout<<tem<<endl; n   n   /*if (t[1]!=tem) n   { n    if (tem==1&&hh[1]>=num1) n    { n     hh[1]-=num1; n     for (int i=1;i<=n;i++) n      if (t[i]==tem) hh[i]=0; n    } n    else if (tem==2&&hh[1]>=num2) n    { n     hh[1]-=num2; n     for (int i=1;i<=n;i++) n      if (t[i]==tem) hh[i]=0; n    } n    else n    { n     int z=2; n     while(hh[1]) n     { n      if (t[z]!=t[1]) n      { n       if (hh[1]>hh[z]) hh[1]-=hh[z],hh[z]=0; n       else hh[z]-=hh[1],hh[1]=0; n      } n      z++;  n     } n    } n   }*/ n   //cout<<tem<<   <<num1<<   <<num2<<endl; n   int z=1;ll num=0; n   if (t[1]!=tem) n   { n    hh[1]--; n    while(t[z]!=tem)z++;  n   } n  // for (int i=1;i<=z;i++) n  //  cout<<z<<   <<t[i]<<   <<hh[i]<<endl; n   for (int i=z;i<=n<<1;i++) n   { n    int p=(i>n)?i%n:i; n    if (t[p]==tem) num+=hh[p],hh[p]=0; n    else n    { n     if (hh[p]>num) hh[p]-=num,num=0; n     else num-=hh[p],hh[p]=0; n    } n   // cout<<num<<   <<hh[p]<<endl; n   } n    n   ans=1; n   for (int i=1;i<=n;i++) n    ans=1ll*ans*(((a[i]-hh[i])^(1ll*i*i))%Mod+1)%Mod; n   printf( %lld n ,ans); n  } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> di = {0, 0, 1, -1}; n vector<int> dj = {1, -1, 0, 0}; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   vector<vector<int>> dpa(1001, vector<int>(1001, 2140000000)); n   vector<vector<int>> dpb(1001, vector<int>(1001, 2140000000)); n   vector<vector<int>> dpc(1001, vector<int>(1001, 2140000000)); n   vector<vector<pair<int, int>>> pa( n       1001, vector<pair<int, int>>(1001, {2140000000, 0})); n   vector<vector<pair<int, int>>> pb( n       1001, vector<pair<int, int>>(1001, {2140000000, 0})); n   vector<vector<pair<int, int>>> pc( n       1001, vector<pair<int, int>>(1001, {2140000000, 0})); n   int xa, ya; n   cin >> xa >> ya; n   dpa[xa][ya] = 0; n   int xb, yb; n   cin >> xb >> yb; n   dpb[xb][yb] = 0; n   int xc, yc; n   cin >> xc >> yc; n   dpc[xc][yc] = 0; n   queue<pair<int, int>> q; n   q.push({xa, ya}); n   while (!q.empty()) { n     int vx = q.front().first; n     int vy = q.front().second; n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int tx = vx + di[i]; n       int ty = vy + dj[i]; n       if (tx >= 0 && tx <= 1000 && ty >= 0 && ty <= 1000 && n           dpa[tx][ty] == 2140000000) { n         q.push({tx, ty}); n         dpa[tx][ty] = dpa[vx][vy] + 1; n         pa[tx][ty] = {vx, vy}; n       } n     } n   } n   q.push({xb, yb}); n   while (!q.empty()) { n     int vx = q.front().first; n     int vy = q.front().second; n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int tx = vx + di[i]; n       int ty = vy + dj[i]; n       if (tx >= 0 && tx <= 1000 && ty >= 0 && ty <= 1000 && n           dpb[tx][ty] == 2140000000) { n         q.push({tx, ty}); n         dpb[tx][ty] = dpb[vx][vy] + 1; n         pb[tx][ty] = {vx, vy}; n       } n     } n   } n   q.push({xc, yc}); n   while (!q.empty()) { n     int vx = q.front().first; n     int vy = q.front().second; n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int tx = vx + di[i]; n       int ty = vy + dj[i]; n       if (tx >= 0 && tx <= 1000 && ty >= 0 && ty <= 1000 && n           dpc[tx][ty] == 2140000000) { n         q.push({tx, ty}); n         dpc[tx][ty] = dpc[vx][vy] + 1; n         pc[tx][ty] = {vx, vy}; n       } n     } n   } n   int ans = 2140000000; n   int ansx; n   int ansy; n   for (int i = 0; i <= 1000; i++) { n     for (int j = 0; j <= 1000; j++) { n       if (dpa[i][j] + dpb[i][j] + dpc[i][j] + 1 < ans) { n         ans = dpa[i][j] + dpb[i][j] + dpc[i][j] + 1; n         ansx = i; n         ansy = j; n       } n     } n   } n   cout << ans << endl; n   int sax = ansx; n   int say = ansy; n   int sbx = ansx; n   int sby = ansy; n   int scx = ansx; n   int scy = ansy; n   vector<vector<int>> used(1001, vector<int>(1001, 0)); n   while (make_pair(sax, say) != make_pair(2140000000, 0)) { n     int tx = sax; n     sax = pa[sax][say].first; n     say = pa[tx][say].second; n     if (make_pair(sax, say) != make_pair(2140000000, 0) && !used[sax][say]) { n       cout << sax <<     << say << endl; n       used[sax][say] = 1; n     } n   } n   while (make_pair(sbx, sby) != make_pair(2140000000, 0)) { n     int tx = sbx; n     sbx = pb[sbx][sby].first; n     sby = pb[tx][sby].second; n     if (make_pair(sbx, sby) != make_pair(2140000000, 0) && !used[sbx][sby]) { n       cout << sbx <<     << sby << endl; n       used[sbx][sby] = 1; n     } n   } n   while (make_pair(scx, scy) != make_pair(2140000000, 0)) { n     int tx = scx; n     scx = pc[scx][scy].first; n     scy = pc[tx][scy].second; n     if (make_pair(scx, scy) != make_pair(2140000000, 0) && !used[scx][scy]) { n       cout << scx <<     << scy << endl; n       used[scx][scy] = 1; n     } n   } n   cout << ansx <<     << ansy << endl; n   return 0; 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 << endl; 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 maxn = (long long)2e3 + 10; n const long long maxm = (long long)20; n const int LOGN = 20; n const long long INF = 1e15 + 10; n const long long MOD = 1e9 + 7; n const double PI = acos(-1.0); n const double EPS = 1e-12; n long long Power(long long x, long long n) { n   if (n == 0) return 1; n   long long r = Power(x, n / 2); n   r = r * r % MOD; n   if (n & 1) r = r * x % MOD; n   return r; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s1, s2; n   cin >> s1 >> s2; n   set<char> d1(s1.begin(), s1.end()); n   set<char> d2(s2.begin(), s2.end()); n   for (auto c : d2) n     if (!d1.count(c)) { n       cout << -1 << endl; n       return 0; n     } n   vector<vector<int>> a(128); n   for (int i = 0; i < s1.size(); ++i) { n     a[s1[i]].push_back(i); n   } n   int ans = 1; n   int p = -1; n   for (int i = 0; i < s2.size(); ++i) { n     auto c = s2[i]; n     auto it = upper_bound(a[c].begin(), a[c].end(), p); n     if (it == a[c].end()) { n       ++ans; n       p = *upper_bound(a[c].begin(), a[c].end(), -1); n     } else { n       p = *it; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define lop(i,n) for(ll i=0;i<n;i++) n #define lop1(i,n) for(ll i=1;i<=n;i++) n #define lopr(i,n) for(ll i=n-1;i>=0;i--) n #define ll long long int n #define pb push_back n #define all(v) v.begin(),v.end() n #define IOS  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) n #define endl   n  n #define F first n #define S second  n #define mem(arr,val) memset(arr,val,sizeof(arr)) n #define pii pair<int,int> n #define pll pair<ll,ll> n #define LCM(a,b) (a*b)/__gcd(a,b) n #define mii map<int,int> n #define mll map<ll,ll> n #define ub upper_bound n #define lb lower_bound n #define sz(x) (ll)x.size() n #define ld long double n #define pcnt(x) __builtin_popcountll(x) n const long long I1=1e9; n const long long I2=1e18; n const int32_t M1=1e9+7; n const int32_t M2=998244353; n template<typename T,typename T1>T maxn(T &a,T1 b){if(b>a)a=b;return a;} n template<typename T,typename T1>T minn(T &a,T1 b){if(b<a)a=b;return a;} n  n void solve() n { n   //code begins from here// n     int n,q,k; n     cin>>n>>q>>k; n     ll a[n+2]; n     lop1(i,n){ n         cin>>a[i]; n     } n     a[0] = 0; n     a[n+1] = k+1; n     ll pre[n+2]; n     lop1(i,n){ n         pre[i] = (a[i]-a[i-1]-1)+(a[i+1]-a[i]-1); n         pre[i]+=pre[i-1]; n     } n     while(q--){ n         int l,r; n         cin>>l>>r; n         if (l==r){ n             cout << k-1<<endl; n             continue; n         } n         ll cnt = 0; n         cnt += a[l+1]-2; n         cnt += k - a[r-1] -1; n         l++; r--; n         if (l<=r){ n             cnt += pre[r]-pre[l-1]; n         } n         cout << cnt<<endl; n     } n } n  n signed main() n { n     IOS; n     #ifdef MODULO n         initialize(); n     #endif n     #ifdef SIEVE n         sieve(); n     #endif n     int testcase=1; n    // cin>>testcase; n     while(testcase--) solve(); n     return 0; n }


#include <bits/stdc++.h> n template <typename _Tp> n void read(_Tp &x) { n   char ch(getchar()); n   bool f(false); n   while (!isdigit(ch)) f |= ch == 45, ch = getchar(); n   x = ch & 15, ch = getchar(); n   while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar(); n   if (f) x = -x; n } n template <typename _Tp, typename... Args> n void read(_Tp &t, Args &...args) { n   read(t); n   read(args...); n } n const int N = 10000005, mod = 998244353; n template <typename _Tp1, typename _Tp2> n inline void add(_Tp1 &a, _Tp2 b) { n   a = a + b >= mod ? a + b - mod : a + b; n } n template <typename _Tp1, typename _Tp2> n inline void sub(_Tp1 &a, _Tp2 b) { n   a = a - b < 0 ? a - b + mod : a - b; n } n long long ksm(long long a, long long b = mod - 2) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod, b >>= 1; n   } n   return res; n } n int pw[N], a[N]; n void init() { n   pw[0] = 1; n   for (int i = 1; i < N; ++i) pw[i] = pw[i - 1], add(pw[i], pw[i - 1]); n   int mul = 1; n   for (int i = 1; i < N; ++i) mul = 1ULL * mul * (pw[i] - 1) % mod; n   a[N - 1] = ksm(mul); n   for (int i = N - 2; i >= 1; --i) n     a[i] = 1ULL * a[i + 1] * (pw[i + 1] - 1) % mod; n } n void MAIN() { n   int n, k, x; n   read(n, k, x); n   if (!x) { n     if (n > k) return puts( 0 ), void(); n     int ans = 1; n     for (int i = 0; i < n; ++i) ans = 1LL * ans * (pw[k] - pw[i] + mod) % mod; n     printf( %d n , ans); n     return; n   } n   int ans = pw[k] - 1, cur = 1, qwq = ksm(2, n), iq = ksm(2); n   for (int i = 1, coef = pw[k] - 1; i < k; ++i) { n     cur = 1ULL * cur * (qwq - 1) % mod, qwq = 1ULL * qwq * iq % mod, n     coef = 1ULL * coef * (pw[k] - pw[i] + mod) % mod; n     add(ans, 1ULL * coef * cur % mod * a[i] % mod); n   } n   printf( %lld n , ksm(pw[k] - 1) * ans % mod); n } n int main() { n   init(); n   int _; n   read(_); n   while (_--) MAIN(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     long long a[26] = {0}; n     long long cnt = 0; n     for (long long i = 0; i < s.size(); i++) { n       a[s[i] - 97]++; n     } n     for (long long i = 0; i < 26; i++) { n       if (a[i] <= 2) n         cnt += a[i]; n       else n         cnt += 2; n     } n     cout << cnt / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, i, sum; n   int a[100010]; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   sum = a[n - 1]; n   int f = 1; n   for (i = 0; i < n - 1; i++) { n     sum -= a[i]; n     if (sum < 0) { n       f = 0; n       break; n     } n   } n   if (f) n     printf( %d , sum + 1); n   else n     printf( 1 ); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e6 + 10; n const int mod = 1e9 + 7; n const int INF = 0x3f3f3f3f; n using namespace std; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n inline int read() { n   int X = 0; n   bool flag = 1; n   char ch = getchar(); n   while (ch< 0  | ch>  9 ) { n     if (ch ==  - ) flag = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     X = (X << 1) + (X << 3) + ch -  0 ; n     ch = getchar(); n   } n   if (flag) return X; n   return ~(X - 1); n } n int t; n string s; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> s; n     sort(s.begin(), s.end()); n     bool flag = true; n     for (int i = 0; i <= (int)s.size() - 2; ++i) { n       if (s[i] + 1 != s[i + 1]) { n         flag = false; n         break; n       } n     } n     if (flag) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int i, j, K; n   int num[101]; n   memset(num, 0, sizeof(num)); n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     scanf( %d , &K); n     num[K]++; n   } n   int cut = 0; n   int Max = 0; n   for (i = 1; i <= 100; i++) { n     if (num[i]) { n       cut++; n       Max = max(Max, num[i]); n     } n   } n   Max % k ? (j = Max / k + 1) : (j = Max / k); n   long long ans = 0; n   ans = cut * j * k - n; n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 1010, K = 51; n int n, m, k, u, v, x, y, t, a, b, ans; n short dp[2][MAXN][K][K]; n bool good[2][MAXN]; n inline void upd(short &a, short b) { a = max(a, b); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < 2; i++) { n     cin >> t; n     while (t--) cin >> x, good[i][x] = 1; n   } n   m = min(m, 2 * n / k + 2); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       for (int x = 0; x <= k; x++) n         for (int y = 0; y <= k; y++) { n           int tmp = (x && good[0][i]) || (y && good[1][i]); n           upd(dp[1][j][x][y], dp[0][j][max(x - 1, 0)][max(y - 1, 0)] + tmp); n           if (j) { n             upd(dp[1][j][x][y], dp[1][j - 1][k][y]); n             upd(dp[1][j][x][y], dp[1][j - 1][x][k]); n           } n         } n     } n     for (int j = 0; j <= m; j++) n       for (int x = 0; x <= k; x++) n         for (int y = 0; y <= k; y++) { n           dp[0][j][x][y] = dp[1][j][x][y]; n           dp[1][j][x][y] = 0; n         } n   } n   cout << dp[0][m][0][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200200, M = 110, K = 30030, Inf = 1e17; n void Max(long long &x, long long y) { x = max(x, y); } n long long dp[4 * K]; n long long a[M], b[M]; n vector<pair<long long, long long>> p, neg; n bool cmp(pair<long long, long long> p1, pair<long long, long long> p2) { n   return p1.first - p1.second < p2.first - p2.second; n } n int main() { n   long long n, r, c = 0; n   cin >> n >> r; n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i]; n     if (b[i] >= 0) n       p.push_back({a[i], b[i]}); n     else n       neg.push_back({a[i], -b[i]}); n   } n   sort(p.begin(), p.end()); n   for (int i = 0; i < p.size(); i++) { n     if (p[i].first <= r) r += p[i].second, c++; n   } n   sort(neg.begin(), neg.end(), cmp); n   for (int i = 0; i < neg.size(); i++) { n     for (int j = 2 * K - 1; j >= neg[i].first; j--) { n       if (j - neg[i].second >= 0) Max(dp[j], dp[j - neg[i].second] + 1); n     } n   } n   long long Ans = 0; n   for (int i = 0; i <= r; i++) { n     Max(Ans, dp[i]); n   } n   Ans += c; n   cout << Ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n namespace LCT { n int fa[500005], ch[500005][2]; n bool rev[500005], val[500005]; n bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } n bool isc(int x) { return ch[fa[x]][1] == x; } n void upd(int x) { val[x] = val[ch[x][0]] | val[ch[x][1]] | (x > n); } n void pd(int x) { n   if (rev[x]) n     swap(ch[x][0], ch[x][1]), rev[x] = 0, rev[ch[x][0]] ^= 1, n                               rev[ch[x][1]] ^= 1; n } n void pdpath(int x) { n   if (!isr(x)) pdpath(fa[x]); n   pd(x); n } n void rot(int x) { n   int y = fa[x], z = fa[y], c = isc(x); n   if (!isr(y)) ch[z][ch[z][1] == y] = x; n   fa[ch[y][c] = ch[x][!c]] = y; n   fa[ch[x][!c] = y] = x, fa[x] = z; n   upd(y); n } n void splay(int x) { n   for (pdpath(x); !isr(x); rot(x)) n     if (!isr(fa[x])) rot(isc(fa[x]) == isc(x) ? fa[x] : x); n   upd(x); n } n void access(int x) { n   for (int y = 0; x; x = fa[y = x]) splay(x), ch[x][1] = y, upd(x); n } n void prefer(int x) { access(x), splay(x); } n void bert(int x) { prefer(x), rev[x] ^= 1; } n void link(int x, int y) { bert(x), fa[x] = y; } n void cut(int x, int y) { bert(x), prefer(y), fa[x] = ch[y][0] = 0, upd(y); } n int find(int x) { n   pd(x); n   if (val[ch[x][1]]) n     return find(ch[x][1]); n   else n     return x > n ? x : find(ch[x][0]); n } n }  // namespace LCT n using LCT::cut; n using LCT::link; n vector<int> G[500005]; n int X[500005], Y[500005], deg[500005], stk[500005], top, fa[500005]; n void dfs(int u) { n   for (int i = G[u].size() - 1, v; i >= 0; i--) n     if ((v = G[u][i]) != fa[u]) fa[v] = u, dfs(v); n } n int main() { n   scanf( %d , &n); n   for (int i = 1, x, y; i < n; i++) n     scanf( %d%d , &x, &y), G[x].push_back(y), G[y].push_back(x); n   for (int i = 1; i < n; i++) n     scanf( %d%d , &X[i], &Y[i]), link(X[i], n + i), link(Y[i], n + i); n   dfs(1); n   for (int i = 1; i <= n; i++) deg[fa[i]]++; n   for (int i = 1; i <= n; i++) n     if (!deg[i]) stk[++top] = i; n   printf( %d n , n - 1); n   for (int i = 1, k; i < n; i++) { n     int x = stk[i]; n     if (!--deg[fa[x]]) stk[++top] = fa[x]; n     LCT::bert(fa[x]), LCT::prefer(x); n     k = LCT::find(LCT::ch[x][0]) - n; n     printf( %d %d %d %d n , x, fa[x], X[k], Y[k]); n     cut(X[k], n + k), cut(Y[k], n + k), link(x, fa[x]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[3005], b[3005], c[3005], vis[3005]; n bool cmp(int i, int j) { return a[i] > a[j]; } n int main() { n   vector<int> p, q; n   int T, i, j, k, ca = 0, n, m, K; n   int s, t; n   scanf( %d%d%d , &n, &s, &t); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]), c[i] = i; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &b[i]); n   } n   sort(c, c + n, cmp); n   for (int i = 0; i < s; i++) { n     vis[c[i]] = 1; n   } n   while (t--) { n     k = 0; n     while (k < n && vis[c[k]]) k++; n     int mx = -1, v, l = c[k]; n     for (int i = n - 1; i >= 0; i--) { n       j = -0x7f7f7f7f; n       if (vis[i] == 0) n         j = b[i]; n       else if (vis[i] == 1) n         j = -a[i] + a[l] + b[i]; n       if (j > mx) mx = j, v = i; n     } n     if (vis[v]) vis[l] = 1; n     vis[v] = 2; n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (vis[i] == 1) { n       p.push_back(i + 1); n       ans += a[i]; n     } else if (vis[i] == 2) { n       q.push_back(i + 1); n       ans += b[i]; n     } n   } n   printf( %d n , ans); n   for (auto it : p) { n     printf( %d  , it); n   } n   puts(  ); n   for (auto it : q) { n     printf( %d  , it); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b; n vector<pair<int, int> > des[110]; n int dist(int i, int j) { n   if (i <= j) return j - i; n   return n - i + j; n } n bool cmp(pair<int, int> a, pair<int, int> b) { n   int i = a.second; n   int x = a.first, y = b.first; n   return dist(i, x) < dist(i, y); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &a, &b); n     des[a].push_back(pair<int, int>(b, a)); n   } n   for (int i = 1; i <= n; i++) sort(des[i].begin(), des[i].end(), cmp); n   for (int s = 1; s <= n; s++) { n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       if (des[i].empty()) continue; n       int d; n       if (s <= i) n         d = i - s; n       else n         d = n - s + i; n       d += n * (des[i].size() - 1); n       int j = des[i][0].first; n       if (i <= j) n         d += j - i; n       else n         d += n - i + j; n       ans = max(ans, d); n     } n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n long long a[maxn], b[maxn]; n long long dp[maxn]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] -= i; n     b[i] = a[i]; n   } n   sort(b + 1, b + 1 + n); n   memset(dp, (0), sizeof(dp)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (j == 1) n         dp[j] += abs(a[i] - b[j]); n       else { n         dp[j] = min(dp[j - 1], dp[j] + abs(a[i] - b[j])); n       } n     } n   } n   printf( %lld n , (dp[n])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Q, n, m, ans[600005], a[505][505]; n char S[505]; n struct O { n   int p, q, x, y, i; n } o[600005], O[600005]; n bitset<505> f[505][505], g[505][505]; n void go(int l, int r, int p, int q) { n   if (p > q) return; n   int mi = l + r >> 1, L, R, t; n   for (int i = 1; i <= n; ++i) n     if (!a[i][mi]) n       g[i][mi].reset(); n     else n       g[i][mi] = g[i - 1][mi], g[i][mi].set(i); n   for (int j = mi + 1; j <= r; ++j) n     for (int i = 1; i <= n; ++i) { n       g[i][j].reset(); n       if (!a[i][j]) continue; n       if (a[i][j - 1]) g[i][j] |= g[i][j - 1]; n       if (a[i - 1][j]) g[i][j] |= g[i - 1][j]; n     } n   for (int i = n; i; --i) n     if (!a[i][mi]) n       f[i][mi].reset(); n     else n       f[i][mi] = f[i + 1][mi], f[i][mi].set(i); n   for (int j = mi - 1; j >= l; --j) n     for (int i = n; i; --i) { n       f[i][j].reset(); n       if (!a[i][j]) continue; n       if (a[i][j + 1]) f[i][j] |= f[i][j + 1]; n       if (a[i + 1][j]) f[i][j] |= f[i + 1][j]; n     } n   L = p; n   R = q; n   for (int i = p; i <= q; ++i) n     if (o[i].q < mi && o[i].y < mi) n       O[L++] = o[i]; n     else if (o[i].q > mi && o[i].y > mi) n       O[R--] = o[i]; n   t = L; n   for (int i = p; i <= q; ++i) n     if (o[i].q <= mi && o[i].y >= mi) O[t++] = o[i]; n   for (int i = p; i <= q; ++i) o[i] = O[i]; n   for (int i = L; i <= R; ++i) n     if (o[i].p <= o[i].x && o[i].q <= o[i].y) n       ans[o[i].i] = (f[o[i].p][o[i].q] & g[o[i].x][o[i].y]).any(); n   if (l == r) return; n   go(l, mi - 1, p, L - 1); n   go(mi + 1, r, R + 1, q); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , S + 1); n     for (int j = 1; j <= m; ++j) a[i][j] = S[j] ==  . ; n   } n   scanf( %d , &Q); n   for (int i = 1; i <= Q; ++i) n     scanf( %d%d%d%d , &o[i].p, &o[i].q, &o[i].x, &o[i].y), o[i].i = i; n   go(1, m, 1, Q); n   for (int i = 1; i <= Q; ++i) puts(ans[i] ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const double pi = acos(-1.0); n const double G = 6.67430e-11; n int n, q; n set<int> p; n multiset<int> d; n void in(int x) { n   p.insert(x); n   if (p.size() == 1) return; n   auto pos = p.find(x); n   if (pos == --p.end()) { n     d.insert(x - *--pos); n   } else if (pos == p.begin()) { n     int y = *++pos; n     d.insert(y - x); n   } else { n     auto r = ++pos; n     pos--; n     auto l = --pos; n     pos++; n     d.insert(x - *l); n     d.insert(*r - x); n     d.erase(d.find(*r - *l)); n   } n } n void er(int x) { n   set<int>::iterator pos; n   pos = p.find(x); n   if (p.size() < 2) { n     p.erase(x); n     return; n   } n   if (pos == --p.end()) n     d.erase(d.find(x - *--pos)); n   else if (pos == p.begin()) n     d.erase(d.find(*++pos - x)); n   else { n     set<int>::iterator l; n     set<int>::iterator r; n     l = --pos; n     ++pos; n     r = ++pos; n     --pos; n     d.erase(d.find(x - *l)); n     d.erase(d.find(*r - x)); n     d.insert(*r - *l); n   } n   p.erase(x); n } n int kk() { n   if (p.size() <= 2) return 0; n   auto l = p.begin(); n   auto r = --p.end(); n   return (*r - *l - *--d.end()); n } n int main() { n   cin >> n >> q; n   int x; n   for (int i = 1; i <= n; ++i) { n     cin >> x; n     in(x); n   } n   cout << kk() <<   n ; n   int op; n   while (q--) { n     cin >> op >> x; n     if (op == 1) n       in(x); n     else n       er(x); n     cout << kk() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 2, MAX2 = 2e6 + 4; n int ans[MAX], val[MAX], depth[MAX]; n vector<int> G[MAX], d[MAX2], occ[MAX2]; n void init(int a) { n   if (d[a].size()) return; n   for (int i = 2; i * i <= a; i++) { n     if (a % i == 0) { n       d[a].push_back(a / i); n       if (i * i != a) d[a].push_back(i); n     } n   } n   if (a != 1) d[a].push_back(a); n } n void dfs(int vv, int par, int dv = 0) { n   ans[vv] = -1, depth[vv] = dv; n   int v = val[vv]; n   for (int i = 0; i < d[v].size(); i++) { n     int u = d[v][i]; n     if (not occ[u].empty()) { n       int x = occ[u].back(); n       if (ans[vv] == -1 or depth[ans[vv]] < depth[x]) ans[vv] = x; n     } n     occ[u].push_back(vv); n   } n   for (int i = 0; i < G[vv].size(); i++) { n     int u = G[vv][i]; n     if (u == par) continue; n     dfs(u, vv, dv + 1); n   } n   for (int i = 0; i < d[v].size(); i++) { n     int u = d[v][i]; n     occ[u].pop_back(); n   } n } n int main() { n   int n, q; n   cin >> n >> q; n   for (int i = 1; i < n + 1; i++) { n     scanf( %d , val + i); n     init(val[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     G[a].push_back(b); n     G[b].push_back(a); n   } n   dfs(1, 0); n   while (q--) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int x; n       scanf( %d , &x); n       printf( %d n , ans[x]); n     } else { n       int x, v; n       scanf( %d %d , &x, &v); n       val[x] = v; n       init(val[x]); n       dfs(1, 0); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 27, maxm = 5e4 + 20, inf = 1e9; n vector<int> ar[maxn][maxn][maxn][maxn]; n int q, a[maxm], bs[4], bs2[4]; n string s, qs, qss; n map<pair<string, string>, int> mp; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> s >> q; n   for (int i = 0; i < s.size(); i++) n     a[i] = (int)s[i] -  a , ar[(int)s[i] -  a ][26][26][26].push_back(i); n   for (int i = 0; i < (int)s.size() - 1; i++) n     ar[a[i]][a[i + 1]][26][26].push_back(i); n   for (int i = 0; i < (int)s.size() - 2; i++) n     ar[a[i]][a[i + 1]][a[i + 2]][26].push_back(i); n   for (int i = 0; i < (int)s.size() - 3; i++) n     ar[a[i]][a[i + 1]][a[i + 2]][a[i + 3]].push_back(i); n   while (q--) { n     cin >> qs >> qss; n     if (qss < qs) swap(qss, qs); n     fill(bs, bs + 4, 26), fill(bs2, bs2 + 4, 26); n     for (int i = 0; i < qs.size(); i++) bs[i] = qs[i] -  a ; n     for (int i = 0; i < qss.size(); i++) bs2[i] = qss[i] -  a ; n     if (ar[bs[0]][bs[1]][bs[2]][bs[3]].size() > n         ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].size()) n       swap(qs, qss), swap(bs, bs2); n     if (mp.count(make_pair(qs, qss))) { n       cout << mp[make_pair(qs, qss)] << endl; n       continue; n     } n     int ans = inf, j; n     for (int i : ar[bs[0]][bs[1]][bs[2]][bs[3]]) { n       auto it = upper_bound(ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].begin(), n                             ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].end(), i); n       if (it != ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].end()) { n         j = *it; n         ans = min(ans, max(i + (int)qs.size(), j + (int)qss.size()) - i); n       } n       if (it != ar[bs2[0]][bs2[1]][bs2[2]][bs2[3]].begin()) { n         it--; n         j = *it; n         ans = min(ans, max(i + (int)qs.size(), j + (int)qss.size()) - j); n       } n     } n     if (ans == inf) ans = -1; n     mp[make_pair(qs, qss)] = ans; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int deg[100000], x[100000]; n map<pair<int, int>, int> m; n vector<pair<int, int> > v; n int main() { n   int n, k; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &deg[i], &x[i]); n     if (deg[i] > 0) { n       pair<int, int> a; n       a.first = deg[i]; n       a.second = i; n       m[a] = i; n     } n   } n   while (!m.empty()) { n     pair<pair<int, int>, int> b = *(m.begin()); n     m.erase(m.begin()); n     int neighbor = x[b.first.second]; n     v.push_back(pair<int, int>(b.first.second, neighbor)); n     map<pair<int, int>, int>::iterator it = n         m.find(pair<int, int>(deg[neighbor], neighbor)); n     pair<int, int> c = it->first; n     m.erase(it); n     deg[neighbor]--; n     x[neighbor] ^= b.first.second; n     if (deg[neighbor] > 0) { n       c.first--; n       m[c] = neighbor; n     } n   } n   k = v.size(); n   printf( %d n , v.size()); n   for (int i = 0; i < k; i++) { n     printf( %d %d n , v[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 7340033; n long long n, k; n long long DP[100][1009], dp[100][1009]; n long long DFS(int h, int k); n long long dfs(int h, int k) { n   long long &d = dp[h][k]; n   if (~d) return d; n   d = 0; n   for (int i = 0; i <= k; i++) n     d = (d + DFS(h - 1, i) * DFS(h - 1, k - i)) % mod; n   return d; n } n long long DFS(int h, int k) { n   long long &d = DP[h][k]; n   if (~d) return d; n   if (h == 0) { n     if (k == 0) return d = 1; n     return d = 0; n   } n   if (k == 0) return 1; n   d = 0; n   for (int i = 0; i < k; i++) d = (d + dfs(h, i) * dfs(h, k - 1 - i)) % mod; n   return d; n } n void pre() { n   memset(dp, -1, sizeof(dp)); n   memset(DP, -1, sizeof(DP)); n   for (int i = 0; i <= 30; i++) n     for (int j = 0; j <= 1000; j++) DFS(i, j); n } n int main() { n   int w; n   cin >> w; n   pre(); n   while (w--) { n     int n, k; n     cin >> n >> k; n     int cnt = 0; n     while (n > 1 && (n & 1)) cnt++, n >>= 1; n     cout << DFS(cnt, k) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int ku[maxn], kd[maxn], posu[maxn], posd[maxn]; n long long tl[maxn], tr[maxn]; n int main() { n   int n; n   cin >> n; n   char s[maxn]; n   scanf( %s , s + 1); n   int a = 0, b = 0; n   for (int i = 1; i <= n; i++) { n     ku[i + 1] = ku[i] + (s[i] ==  U ); n     kd[n - i] = kd[n - i + 1] + (s[n - i + 1] ==  D ); n     tl[i] = tl[i - 1] + 2 * ku[i] + 1; n     tr[n - i + 1] = tr[n - i + 2] + 2 * kd[n - i + 1] + 1; n     if (s[i] ==  U ) n       posu[a++] = i; n     else n       posd[b++] = i; n   } n   long long ans = 0, nb, ind; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  U ) { n       if (ku[i] < kd[i]) { n         nb = kd[i] - ku[i] - 1; n         ind = posd[b - nb - 1]; n         ans = tl[i] + tr[i] - tr[ind] - (2 * nb + 1) * (ind - i); n       } else { n         nb = ku[i] - kd[i]; n         ind = posu[nb]; n         ans = tl[i] + tr[i] - tl[ind] - (2 * nb + 1) * (i - ind); n       } n     } else { n       if (kd[i] < ku[i]) { n         nb = ku[i] - kd[i] - 1; n         ind = posu[nb]; n         ans = tl[i] + tr[i] - tl[ind] - (2 * nb + 1) * (i - ind); n       } else { n         nb = kd[i] - ku[i]; n         ind = posd[b - nb - 1]; n         ans = tl[i] + tr[i] - tr[ind] - (2 * nb + 1) * (ind - i); n       } n     } n     cout << ans <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) n #pragma GCC optimize( no-stack-protector,fast-math ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int N = 5e5 + 100, OO = 1e6, T = (1 << 20) + 10, M = 998244353, P = 6151, n           SQ = 280, lg = 64; n int a[N], b[N]; n vector<int> v, v1, t, t1; n vector<pair<int, int> > is[N]; n int dp[N], dp1[N], ant[N], ans[N], n1[N], n2[N]; n int32_t main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     is[x - 1].push_back({y, i}); n   } n   for (int i = 0; i < n; i++) { n     for (auto u : is[i]) { n       int l = 0, r = (int)v.size(); n       int nw = u.first; n       while (l < r) { n         int mid = (l + r) / 2; n         if (v[mid] >= nw) n           r = mid; n         else n           l = mid + 1; n       } n       n1[u.second] = l + 1; n     } n     int l = 0, r = (int)v.size(); n     while (l < r) { n       int mid = (l + r) / 2; n       if (v[mid] >= a[i]) n         r = mid; n       else n         l = mid + 1; n     } n     if (l == (int)v.size()) n       v.push_back(a[i]); n     else n       v[l] = a[i]; n     dp[i] = l + 1; n     b[i] = -1 * a[i]; n   } n   int lis = (int)v.size(); n   for (int i = n - 1; i >= 0; i--) { n     for (auto u : is[i]) { n       int l = 0, r = (int)v1.size(); n       int nw = u.first * -1; n       while (l < r) { n         int mid = (l + r) / 2; n         if (v1[mid] >= nw) n           r = mid; n         else n           l = mid + 1; n       } n       n2[u.second] = l; n     } n     int l = 0, r = (int)v1.size(); n     while (l < r) { n       int mid = (l + r) / 2; n       if (v1[mid] >= b[i]) n         r = mid; n       else n         l = mid + 1; n     } n     if (l + dp[i] == (int)v.size()) n       ans[i] = 3, t.push_back(a[i]), t1.push_back(i); n     else n       ans[i] = 1; n     if (l == (int)v1.size()) n       v1.push_back(b[i]); n     else n       v1[l] = b[i]; n   } n   v.clear(); n   v1.clear(); n   for (int i = t.size() - 1; i >= 0; i--) { n     int l = 0, r = (int)v.size(); n     while (l < r) { n       int mid = (l + r) / 2; n       if (v[mid] >= t[i]) n         r = mid; n       else n         l = mid + 1; n     } n     if (l == (int)v.size()) { n       v.push_back(t[i]); n       v1.push_back(t1[i]); n     } else { n       ans[v1[l]] = 2; n       ans[t1[i]] = 2; n       v[l] = t[i]; n       v1[l] = t1[i]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (auto u : is[i]) { n       int lt = n1[u.second] + n2[u.second]; n       if (lt >= lis) { n         ant[u.second] = lt; n         continue; n       } n       if (ans[i] == 1 || ans[i] == 2) { n         ant[u.second] = lis; n         continue; n       } else n         ant[u.second] = lis - 1; n     } n   } n   for (int i = 0; i < m; i++) cout << ant[i] << endl; 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(0); n   cout.tie(0); n   int TESTS = 1; n   cin >> TESTS; n   while (TESTS--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     if ((long long int)s.size() == 1) n       cout <<  NO n ; n     else if ((long long int)s.size() == 2) { n       if (s[0] < s[1]) { n         cout <<  YES n2 n ; n         cout << s[0] <<     << s[1] <<   n ; n       } else n         cout <<  NO n ; n     } else { n       cout <<  YES n2 n ; n       cout << s[0] <<     << s.substr(1, n - 1) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int solve(int c, int d, int n, int m, int k) { n   int s = n * m; n   if (k >= n * m) return 0; n   s = n * m - k; n   int sum = 0; n   while (1) { n     if (s <= n) { n       if (c < s * d) n         sum += c; n       else n         sum += s * d; n       break; n     } n     if (c < n * d) n       sum += c; n     else n       sum += n * d; n     s -= n; n   } n   return sum; n } n int main() { n   int c, d, n, m, k; n   while (scanf( %d%d%d%d%d , &c, &d, &n, &m, &k) != EOF) { n     int s = solve(c, d, n, m, k); n     printf( %d n , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int maxn = 100 + 10; n long long ma[100]; n void init() { n   long long x = 1; n   for (int i = 0; i <= 32; i++) { n     ma[i] = x; n     x *= 2; n   } n } n int main() { n   init(); n   int T; n   scanf( %d , &T); n   while (T--) { n     long long n; n     long long ans = 0; n     scanf( %lld , &n); n     long long x1 = n; n     long long x2 = (n + 1); n     if (x1 % 2 == 0) n       x1 /= 2; n     else n       x2 /= 2; n     ans = x1 * x2; n     for (int i = 0; i <= 32; i++) { n       if (n < ma[i]) n         break; n       else n         ans -= 2 * ma[i]; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, p = 0; n   cin >> n >> m; n   if (n == 1 && m != 0) { n     cout << m <<     << m << endl; n     return 0; n   } else if (m == 1 && n != 0) { n     cout << n <<     << n << endl; n     return 0; n   } else if (n == 0 && m == 0) { n     cout << p <<     << p << endl; n     return 0; n   } else if (n == 0) { n     cout <<  Impossible n ; n     return 0; n   } else if (m == 0) { n     cout << n <<     << n << endl; n     return 0; n   } else if (n == m) { n     cout << m <<     << n + m - 1 << endl; n     return 0; n   } else if (n > m) { n     if (n % m == 0) n       cout << m + (n / m) <<     << n + m - 1 << endl; n     else n       cout << m + (n - m) <<     << n + m - 1 << endl; n   } else if (m > n) { n     if (m % n == 0) n       cout << n + (m / n) <<     << n + m - 1 << endl; n     else n       cout << n + (m - n) <<     << n + m - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, s; n   cin >> n >> k >> s; n   if ((n - 1) * k < s || s < k) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   long long m = s / k; n   int rem = s % k; n   int count = 0; n   int room = 1; n   for (; count < rem; ++count) { n     room = (count % 2 == 0) ? room + m + 1 : room - m - 1; n     cout << room <<    ; n   } n   for (; count < k; ++count) { n     room = (count % 2 == 0) ? room + m : room - m; n     cout << room <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n   int N; n   vector<int> V; n   cin >> N; n   int X; n   for (int i = 0; i < N; i++) { n     cin >> X; n     V.push_back(X); n   } n   int ans = 100100, t = 0; n   for (int i = 1; i <= 100; i++) { n     int cost = 0; n     for (int j = 0; j < N; j++) { n       if (abs(V[j] - i) > 1) { n         cost += min(abs(V[j] - (i + 1)), abs(V[j] - (i - 1))); n       } n     } n     if (cost < ans) { n       ans = cost; n       t = i; n     } n   } n   cout << t <<     << ans << endl; n   return 0; n } n