#include <bits/stdc++.h> n using namespace std; n long long n, m, L, start, endP; n vector<vector<long long> > edges; n vector<vector<long long> > neighbors; n vector<vector<long long> > paths; n long long weights[1000][1000] = {0}; n long long dijk() { n   vector<long long> distances(n, LLONG_MAX); n   distances[start] = 0; n   vector<bool> visited(n, false); n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   pq.push({0, start}); n   while (!pq.empty()) { n     long long currentWeight = pq.top().first; n     long long currentNode = pq.top().second; n     pq.pop(); n     if (visited[currentNode]) continue; n     visited[currentNode] = true; n     if (currentNode == endP) break; n     for (long long i = 0; i < neighbors[currentNode].size(); i++) { n       long long currNeighbor = neighbors[currentNode][i]; n       long long currDist = weights[currentNode][currNeighbor]; n       if (currDist == -1) continue; n       if (currentWeight + currDist < distances[currNeighbor]) n         distances[currNeighbor] = currentWeight + currDist; n       if (!visited[currNeighbor]) n         pq.push({distances[currNeighbor], currNeighbor}); n     } n   } n   return distances[endP]; n } n long long dijk2() { n   vector<long long> distances(n, LLONG_MAX); n   distances[start] = 0; n   vector<bool> visited(n, false); n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   pq.push({0, start}); n   paths[start].push_back(start); n   while (!pq.empty()) { n     long long currentWeight = pq.top().first; n     long long currentNode = pq.top().second; n     pq.pop(); n     if (visited[currentNode]) continue; n     visited[currentNode] = true; n     if (currentNode == endP) break; n     for (long long i = 0; i < neighbors[currentNode].size(); i++) { n       long long currNeighbor = neighbors[currentNode][i]; n       long long currDist = weights[currentNode][currNeighbor]; n       if (currDist == -1) currDist = 1; n       if (currentWeight + currDist < distances[currNeighbor]) { n         distances[currNeighbor] = currentWeight + currDist; n         paths[currNeighbor] = paths[currentNode]; n         paths[currNeighbor].push_back(currNeighbor); n       } n       if (!visited[currNeighbor]) n         pq.push({distances[currNeighbor], currNeighbor}); n     } n   } n   return distances[endP]; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> L >> start >> endP; n   edges.resize(m); n   neighbors.resize(n); n   paths.resize(n); n   long long from, to, weight; n   for (long long i = 0; i < m; i++) { n     cin >> from >> to >> weight; n     if (weight == 0) weight = -1; n     weights[from][to] = weight; n     weights[to][from] = weight; n     edges[i] = {from, to, weight}; n     neighbors[from].push_back(to); n     neighbors[to].push_back(from); n   } n   long long currMin = dijk(); n   if (currMin < L) { n     cout <<  NO  << endl; n     return 0; n   } n   long long newMin = dijk2(); n   if (newMin > L) { n     cout <<  NO  << endl; n     return 0; n   } n   for (long long i = 1; i < paths[endP].size(); i++) { n     long long prev = paths[endP][i - 1]; n     long long curr = paths[endP][i]; n     if (weights[prev][curr] == -1) { n       weights[prev][curr] = -2; n       weights[curr][prev] = -2; n     } n   } n   for (long long i = 0; i < edges.size(); i++) { n     long long from = edges[i][0]; n     long long to = edges[i][1]; n     long long weight = weights[from][to]; n     if (weight == -1) { n       weights[from][to] = 999999999999; n       weights[to][from] = 999999999999; n     } n   } n   for (long long i = 1; i < paths[endP].size(); i++) { n     long long prev = paths[endP][i - 1]; n     long long curr = paths[endP][i]; n     if (weights[prev][curr] == -2) { n       weights[prev][curr] = -1; n       weights[curr][prev] = -1; n     } n   } n   while (newMin != L) { n     long long diff = L - newMin; n     for (long long i = 1; i < paths[endP].size(); i++) { n       long long prev = paths[endP][i - 1]; n       long long curr = paths[endP][i]; n       if (weights[prev][curr] == -1) { n         weights[prev][curr] = diff + 1; n         weights[curr][prev] = diff + 1; n         break; n       } n     } n     paths.clear(); n     paths.resize(n); n     newMin = dijk2(); n   } n   for (long long i = 1; i < paths[endP].size(); i++) { n     long long prev = paths[endP][i - 1]; n     long long curr = paths[endP][i]; n     if (weights[prev][curr] == -1) { n       weights[prev][curr] = 1; n       weights[curr][prev] = 1; n     } n   } n   cout <<  YES  << endl; n   for (long long i = 0; i < edges.size(); i++) { n     long long from = edges[i][0]; n     long long to = edges[i][1]; n     long long weight = weights[from][to]; n     if (weight == -1) weight = 999999999999; n     cout << from <<     << to <<     << weight << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int socks[n + 1]; n   memset(socks, 0, sizeof(socks)); n   int maxi = -1; n   int brc = 0; n   for (int i = 1; i <= 2 * n; i++) { n     int x; n     cin >> x; n     if (socks[x] == 0) { n       socks[x]++; n       brc++; n     } else { n       socks[x]++; n       brc--; n     } n     if (maxi < brc) { n       maxi = brc; n     } n   } n   cout << maxi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Freopen() { n   freopen( n        title  n        .in , n        r , stdin); n   freopen( n        title  n        .out , n        w , stdout); n } n int read() { n   int g = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  ||  9  < ch) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     g = g * 10 + ch -  0 ; n     ch = getchar(); n   } n   return g * f; n } n const int N = 1e5 + 5; n const int INF = 1e9 + 1; n struct Seg { n   int tag[N << 2], mx[N << 2], cx[N << 2], size[N << 2], ok[N << 2]; n   long long su[N << 2]; n   void updata(int v, int w) { n     su[v] -= 1ll * ok[v] * size[v] * mx[v]; n     tag[v] = min(tag[v], w); n     mx[v] = min(mx[v], w); n     su[v] += 1ll * (ok[v] = 1) * size[v] * mx[v]; n   } n   void down(int v) { n     if (!(tag[v] ^ INF)) return void(); n     updata(v << 1, tag[v]), updata(v << 1 | 1, tag[v]), tag[v] = INF; n   } n   void up(int v) { n     ok[v] = ok[v << 1] & ok[v << 1 | 1]; n     su[v] = su[v << 1] + su[v << 1 | 1]; n     mx[v] = max(mx[v << 1], mx[v << 1 | 1]); n     cx[v] = max(cx[v << 1], cx[v << 1 | 1]); n     if (mx[v << 1] > mx[v << 1 | 1]) cx[v] = max(cx[v], mx[v << 1 | 1]); n     if (mx[v << 1] < mx[v << 1 | 1]) cx[v] = max(cx[v], mx[v << 1]); n     size[v] = size[v << 1] * (mx[v << 1] == mx[v]) + n               size[v << 1 | 1] * (mx[v << 1 | 1] == mx[v]); n   } n   void build(int v, int l, int r) { n     mx[v] = INF, size[v] = r - l + 1, tag[v] = INF, cx[v] = -INF; n     if (l == r) return void(); n     int mid = l + r >> 1; n     build(v << 1, l, mid), build(v << 1 | 1, mid + 1, r); n   } n   void change(int v, int l, int r, int ql, int qr, int w) { n     if (ql <= l && r <= qr) { n       if (mx[v] <= w) n         return void(); n       else if (w >= cx[v]) n         return updata(v, w), void(); n     } n     int mid = l + r >> 1; n     down(v); n     if (ql <= mid) change(v << 1, l, mid, ql, qr, w); n     if (qr > mid) change(v << 1 | 1, mid + 1, r, ql, qr, w); n     up(v); n   } n } seg[2]; n long long query(int v, int l, int r, int ql, int qr) { n   if (ql <= l && r <= qr) { n     if (seg[0].ok[v] && seg[1].ok[v]) n       return seg[0].su[v] + seg[1].su[v]; n     else if (l == r || !seg[0].su[v] || !seg[1].su[v]) n       return 0; n   } n   int mid = l + r >> 1; n   seg[0].down(v), seg[1].down(v); n   if (qr <= mid) return query(v << 1, l, mid, ql, qr); n   if (ql > mid) return query(v << 1 | 1, mid + 1, r, ql, qr); n   return query(v << 1, l, mid, ql, qr) + query(v << 1 | 1, mid + 1, r, ql, qr); n } n signed main() { n   int n = 1e5; n   seg[0].build(1, 1, n), seg[1].build(1, 1, n); n   for (int T = read(); T--;) { n     int opt = read(), l = read(), r = read() - 1, x; n     if (opt == 1) n       x = read(), seg[x > 0].change(1, 1, n, l, r, abs(x)); n     else n       cout << query(1, 1, n, l, r) <<   n ; n   } n   return signed(); n } n


#include <bits/stdc++.h> n using namespace std; n int prime[1000000]; n bool status[1000100]; n int pos; n void sieve() { n   int i, j; n   for (i = 0; i < 1000100; i++) { n     status[i] = false; n   } n   int sq = sqrt(1000100); n   for (i = 4; i <= 1000100; i += 2) { n     status[i] = true; n   } n   for (i = 3; i <= sq; i += 2) { n     if (status[i] == false) { n       for (j = 2 * i; j <= 1000100; j += i) { n         status[j] = true; n       } n     } n   } n   status[1] = true; n   pos = 1; n   for (int z = 1; z <= 1000100; z++) { n     if (status[z] == false) { n       prime[pos++] = z; n     } n   } n } n int main() { n   sieve(); n   long long n; n   while (cin >> n) { n     long long ans = 1; n     for (int i = 1; i < pos; i++) { n       if (n == 1) break; n       bool f = false; n       while (n % prime[i] == 0) { n         f = true; n         n /= prime[i]; n       } n       if (f) ans *= prime[i]; n     } n     if (n > 1) ans *= n; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n void solve() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<pair<int, int>> res; n   string s2; n   int n2 = n - 2 * (k - 1); n   for (int i = 0; i < 2 * (k - 1); ++i) { n     if (i % 2) { n       s2.push_back( ) ); n     } else { n       s2.push_back( ( ); n     } n   } n   for (int i = 0; i < n2; ++i) { n     if (i < n2 / 2) { n       s2.push_back( ( ); n     } else { n       s2.push_back( ) ); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (s[i] != s2[i]) { n       for (int j = i + 1; j < n; ++j) { n         if (s[j] == s2[i]) { n           reverse(s.begin() + i, s.begin() + j + 1); n           res.push_back({i, j}); n           break; n         } n       } n     } n   } n   cout << res.size() <<   n ; n   for (const auto &p : res) { n     cout << p.first + 1 <<     << p.second + 1 <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long i, j, k, l, n, m, a, b, u; n   cin >> n; n   vector<long long> v(n); n   map<long long, long long> mp; n   long long ans = 0LL; n   priority_queue<pair<long long, long long>> pq; n   for (i = 0; i < n; i++) { n     cin >> v[i]; n     mp[v[i]]++; n   } n   unordered_set<long long> st; n   for (auto it : mp) { n     pq.push({it.second, it.first}); n   } n   map<long long, long long> mp2; n   while (!pq.empty()) { n     auto p = pq.top(); n     pq.pop(); n     if (p.first == 0) continue; n     if (st.find(p.first) == st.end()) { n       st.insert(p.first); n       ans += p.first; n     } else { n       pq.push({p.first - 1, p.second}); n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int m, n; n   scanf( %d %d , &n, &m); n   if (n == 1) { n     if (m == 2) { n       printf( 0 n ); n       printf( 1 1 n ); n       printf( 1 2 n ); n       printf( 1 1 n ); n       return 0; n     } else { n       printf( 1 n ); n       printf( 1 %d 1 1 n , m); n       for (int i = 1; i <= m; ++i) printf( 1 %d n , i); n       printf( 1 1 n ); n     } n   } else if (m == 1) { n     if (n == 2) { n       printf( 0 n ); n       printf( 1 1 n ); n       printf( 2 1 n ); n       printf( 1 1 n ); n       return 0; n     } else { n       printf( 1 n ); n       printf( %d 1 1 1 n , n); n       for (int i = 1; i <= n; ++i) printf( %d 1 n , i); n       printf( 1 1 n ); n     } n   } else if (n % 2 == 0) { n     printf( 0 n ); n     for (int i = 1; i <= n; ++i) printf( %d 1 n , i); n     bool direita = true; n     for (int lin = n; lin >= 1; --lin) { n       if (direita) { n         for (int j = 2; j <= m; ++j) printf( %d %d n , lin, j); n       } else { n         for (int j = m; j > 1; --j) printf( %d %d n , lin, j); n       } n       direita = !direita; n     } n     printf( 1 1 n ); n   } else if (m % 2 == 0) { n     printf( 0 n ); n     for (int j = 1; j <= m; ++j) printf( 1 %d n , j); n     bool baixo = true; n     for (int col = m; col >= 1; --col) { n       if (baixo) { n         for (int i = 2; i <= n; ++i) printf( %d %d n , i, col); n       } else { n         for (int i = n; i > 1; --i) printf( %d %d n , i, col); n       } n       baixo = !baixo; n     } n     printf( 1 1 n ); n   } else { n     printf( 1 n ); n     printf( %d %d 1 1 n , n, m); n     bool direita = true; n     for (int i = 1; i <= n; ++i) { n       if (direita) { n         for (int j = 1; j <= m; ++j) printf( %d %d n , i, j); n       } else { n         for (int j = m; j >= 1; --j) printf( %d %d n , i, j); n       } n       direita = !direita; n     } n     printf( 1 1 n ); n   } n } n


n #include<bits/stdc++.h> n #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL) ;cout.tie(NULL) n using namespace std; n  n #define ll long long int n #define pb push_back n #define endn   n  n #define mp make_pair n #define ss second n #define ff first n #define deb(x) cout << #x <<     << x << endl n #define deb1(x,y) cout<<#x<<   <<x<<  ,  <<#y<<   <<y<<endl n #define deb2(x,y,z) cout<<#x<<   <<x<<  ,  <<#y<<   <<y<<  ,  <<#z<<   <<z<<endl n const long long INF = 1e18; n  n int small = INT_MIN; n int large = INT_MAX; n  n void solve() { n  int n, k; n  cin >> n >> k; n  int a[n]; n  int i = 2, j = n; n  a[0] = 1; n  int t = 1; n  n  while (k-- && t < n - 1) { n   a[t] = j; n   t++; n   j--; n   a[t] = i; n   t++; n   i++; n  } n  n  if (k != -1) { n   cout << -1; n  } n  else { n   while (i <= j) { n    a[t] = i; n    t++; n    i++; n   } n   for (int x : a) { n    cout << x <<    ; n   } n  } n  cout << endn; n  n } n  n int main() { n  n  fast_cin(); n  n  ll T = 1; n  cin >> T; n  while (T--) solve(); n  n  return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long x = 0; n   char s = getchar(); n   bool w = 0; n   while (s <  0  || s >  9 ) { n     w |= s ==  - ; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = (x << 3) + (x << 1) + (s ^ 48); n     s = getchar(); n   } n   return w ? -x : x; n } n const long long inf = 1e18; n long long n, m, sx, sy, tim; n struct edge { n   long long ne, to, dis; n } e[2100], E[2100000]; n struct node { n   long long t, c; n } a[2100]; n long long head[2100], head1[2100000], tot, num, dis[1100]; n bool vis[1100]; n priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, n                greater<pair<long long, long long> > > n     q; n inline void add(long long fr, long long to, long long dis) { n   e[++tot].ne = head[fr]; n   e[tot].to = to; n   e[tot].dis = dis; n   head[fr] = tot; n } n inline void Add(long long fr, long long to, long long dis) { n   E[++num].ne = head1[fr]; n   E[num].to = to; n   E[num].dis = dis; n   head1[fr] = num; n } n inline void Dij1(long long s) { n   for (long long i = 1; i <= n; i++) { n     dis[i] = inf; n     vis[i] = 0; n   } n   dis[s] = 0; n   q.push(make_pair(dis[s], s)); n   while (!q.empty()) { n     long long u = q.top().second; n     q.pop(); n     if (vis[u]) continue; n     vis[u] = 1; n     for (long long i = head[u]; i; i = e[i].ne) { n       long long v = e[i].to; n       if (dis[v] > dis[u] + e[i].dis && dis[u] + e[i].dis <= a[s].t) { n         dis[v] = dis[u] + e[i].dis; n         Add(s, v, a[s].c); n         if (!vis[v]) q.push(make_pair(dis[v], v)); n       } n     } n   } n } n inline void Dij2(long long s) { n   for (long long i = 1; i <= n; i++) { n     dis[i] = inf; n     vis[i] = 0; n   } n   dis[s] = 0; n   q.push(make_pair(dis[s], s)); n   while (!q.empty()) { n     long long u = q.top().second; n     q.pop(); n     if (vis[u]) continue; n     vis[u] = 1; n     for (long long i = head1[u]; i; i = E[i].ne) { n       long long v = E[i].to; n       if (dis[v] > dis[u] + E[i].dis) { n         dis[v] = dis[u] + E[i].dis; n         if (!vis[v]) q.push(make_pair(dis[v], v)); n       } n     } n   } n } n signed main() { n   n = read(); n   m = read(); n   sx = read(); n   sy = read(); n   for (long long i = 1; i <= m; i++) { n     long long u, v, w; n     u = read(); n     v = read(); n     w = read(); n     add(u, v, w); n     add(v, u, w); n   } n   for (long long i = 1; i <= n; i++) a[i].t = read(), a[i].c = read(); n   for (long long i = 1; i <= n; i++) Dij1(i); n   Dij2(sx); n   if (dis[sy] != inf) n     printf( %I64d n , dis[sy]); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool prime[10000007]; n void SieveOfEratosthenes() { n   memset(prime, true, sizeof(prime)); n   for (long long int p = 2; p * p <= 10000007; p++) { n     if (prime[p] == true) { n       for (long long int i = p * p; i <= 10000007; i += p) prime[i] = false; n     } n   } n } n const long long int N = 300500; n long long int fact[N]; n long long int invFact[N]; n long long int fast_pow(long long int a, long long int p, long long int m) { n   long long int res = 1; n   while (p) { n     if (p % 2 == 0) { n       a = (a * a) % m; n       p /= 2; n     } else { n       res = (res * a) % m; n       p--; n     } n   } n   return res; n } n void Call_For_nCr() { n   fact[0] = invFact[0] = 1; n   for (long long int i = 1; i < N; i++) { n     fact[i] = (fact[i - 1] * i) % 1000000007; n     invFact[i] = fast_pow(fact[i], 1000000007 - 2, 1000000007); n   } n } n long long int nCR(long long int n, long long int k, long long int m) { n   if (k > n) { n     return 0; n   } n   return fact[n] * invFact[k] % m * invFact[n - k] % m; n } n long long int sumOfDigit(long long int n, long long int b) { n   long long int unitDigit, sum = 0; n   while (n > 0) { n     unitDigit = n % b; n     sum += unitDigit; n     n = n / b; n   } n   return sum; n } n long long int factorial(long long int n) { n   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); n } n bool compare(pair<long long int, long long int> a, n              pair<long long int, long long int> b) { n   if (a.first < b.first) return 1; n   if (b.first < a.first) return 0; n   if (a.second > b.second) return 1; n   return 0; n } n void solve() { n   string s; n   cin >> s; n   long long int ans = 0, one = 0, ok = 0, zero = 0; n   for (long long int i = 0; i < s.length(); i++) { n     if (s[i] ==  0 ) { n       zero = 1; n       ok = 1; n       if (one && zero) ans = 1; n     } n     if (s[i] ==  1 ) { n       if (zero) one = 1; n     } n   } n   if (!ok) { n     cout << 0 <<   n ; n     return; n   } n   if (ans) n     cout << 2 <<   n ; n   else n     cout << 1 <<   n ; n } n void init_code() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n signed main() { n   init_code(); n   long long int t = 1; n   cin >> t; n   long long int i = 1; n   while (t--) { n     solve(); n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool is_palindrome(const string& s) { n   return std::equal(s.begin(), s.end(), s.rbegin()); n } n const long long MOD = 1000000007; n const long long INF = 1e9 + 5; n const double eps = 1e-7; n const double PI = acos(-1.0); n inline void debug_vi(vector<int> a) { n   for (long long i = (long long)(0); i < (long long)(a.size()); i++) n     cout << a[i] <<    ; n } n inline void debug_vll(vector<long long> a) { n   for (long long i = (long long)(0); i < (long long)(a.size()); i++) n     cout << a[i] <<    ; n } n const int N = 1e5 + 5; n int arr[N]; n pair<int, int> xpos, ypos; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, l, x, y, j; n   cin >> n >> l >> x >> y; n   xpos = {-1, -1}; n   ypos = xpos; n   for (long long i = (long long)(0); i < (long long)(n); i++) { n     cin >> arr[i]; n   } n   j = 0; n   for (long long i = (long long)(0); i < (long long)(n); i++) { n     while (j < n && arr[j] - arr[i] < x) j++; n     if (j < n && arr[j] - arr[i] == x) { n       xpos = {i, j}; n       break; n     } n   } n   j = 0; n   for (long long i = (long long)(0); i < (long long)(n); i++) { n     while (j < n && arr[j] - arr[i] < y) j++; n     if (j < n && arr[j] - arr[i] == y) { n       ypos = {i, j}; n       break; n     } n   } n   if (xpos.first != -1 && ypos.first != -1) { n     cout << 0; n   } else if (xpos.first != -1 || ypos.first != -1) { n     cout << 1 <<   n ; n     if (xpos.first != -1) { n       cout << y; n     } else { n       cout << x; n     } n   } else { n     j = 0; n     for (long long i = (long long)(0); i < (long long)(n); i++) { n       while (j < n && arr[j] - arr[i] < x + y) j++; n       if (j < n && arr[j] - arr[i] == x + y) { n         cout << 1 <<   n ; n         cout << arr[i] + x <<   n ; n         return 0; n       } n     } n     j = 0; n     for (long long i = (long long)(0); i < (long long)(n); i++) { n       while (j < n && arr[j] - arr[i] < y - x) j++; n       if (j < n && arr[j] - arr[i] == y - x) { n         if (arr[i] - x >= 0) { n           cout << 1 <<   n ; n           cout << arr[i] - x; n           return 0; n         } else if (arr[j] + x <= l) { n           cout << 1 <<   n ; n           cout << arr[j] + x; n           return 0; n         } n       } n     } n     cout << 2 <<   n ; n     cout << x <<     << y; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void in(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while ( 0  <= c && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   if (f) x = -x; n } n char tc[1000005]; n int a[1000005], b[1000005]; n int n, m; n int sum[1000005][3][3]; n signed main() { n   scanf( %s , tc + 1); n   n = strlen(tc + 1); n   for (register int i = 1; i <= n; ++i) n     a[i] = (tc[i] ==  R  ? 0 : (tc[i] ==  G  ? 1 : 2)); n   scanf( %s , tc + 1); n   m = strlen(tc + 1); n   for (register int i = 1; i <= m; ++i) n     b[i] = (tc[i] ==  R  ? 0 : (tc[i] ==  G  ? 1 : 2)); n   for (register int i = 2; i <= m; ++i) { n     for (register int j = 0; j < 3; ++j) n       for (register int k = 0; k < 3; ++k) sum[i][j][k] = sum[i - 1][j][k]; n     ++sum[i][b[i - 1]][b[i]]; n   } n   int l = 1, r = 0; n   long long ans = 0; n   for (register int i = 1; i <= n; ++i) { n     ++r; n     while (r < m && b[r] != a[i]) ++r; n     if (r > m) r = m; n     if (i > 1 && b[l] == a[i - 1]) ++l; n     if (l > m) break; n     ans += r - l + 1; n     if (i > 1 && a[i] != a[i - 1]) n       ans -= sum[r][a[i]][a[i - 1]] - sum[l - 1][a[i]][a[i - 1]]; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 100001; n long long n, k, l[MAX], inf = 1000000000000; n multiset<long long> kek; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> l[i + 1]; n   } n   long long a; n   cin >> a; n   long long ans = 0; n   kek.insert(inf); n   for (int i = 1; i <= n; i++) { n     long long b; n     cin >> b; n     kek.insert(b); n     while (k < l[i]) { n       if (*(kek.begin()) == inf) { n         cout << -1; n         return 0; n       } n       long long take = *(kek.begin()); n       kek.erase(kek.find(take)); n       k += a; n       ans += take; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a, b; n int main() { n   scanf( %lld %lld %lld %lld , &n, &m, &a, &b); n   long long int mm = n % m; n   unsigned long long ans = min((m - mm) * a, mm * b); n   printf( %llu n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << setprecision(20); n   auto solve = [&]() -> string { n     long long int n, k, a, b; n     cin >> n >> k >> a >> b; n     long long int remG = a; n     long long int remB = b; n     string ans; n     vector<long long int> rem({remG, remB}); n     vector<long long int> lab({ G ,  B }); n     long long int cur = (remG > remB) ? 0 : 1; n     while (true) { n       if (rem[cur] == 0) { n         return  NO ; n       } else if (rem[cur] < rem[1 - cur]) { n         ans += lab[cur]; n         rem[cur] -= 1; n       } else { n         long long int t = min(rem[cur], k); n         for (long long int i = 0; i < t; i++) ans += lab[cur]; n         rem[cur] -= t; n       } n       if (rem[0] == 0 && rem[1] == 0) return ans; n       cur = 1 - cur; n     } n   }; n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int bit[2][100011], head[100011], tail[100011], name[100011], dist[100011], n, n     z, D; n vector<int> adj[100011]; n int query(int x, int* bit) { n   int r = 0; n   while (x) { n     r += bit[x]; n     x -= x & -x; n   } n   return r; n } n void update(int x, int v, int* bit) { n   while (x < 100011) { n     bit[x] += v; n     x += x & -x; n   } n } n void dfs(int p, int h, int d) { n   if (name[p]) return; n   D = max(D, d); n   name[p] = z++; n   head[p] = h; n   dist[p] = d; n   for (int i = 0; i < adj[p].size(); ++i) dfs(adj[p][i], h, d + 1); n } n int main() { n   int q, t, v, x, d; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   z = 2; n   name[1] = head[1] = 1; n   for (int i = 0; i < adj[1].size(); ++i) { n     int u = adj[1][i]; n     dfs(u, z, 1); n     tail[name[u]] = z - 1; n   } n   ++D; n   while (q--) { n     scanf( %d , &t); n     if (t == 0) { n       scanf( %d%d%d , &v, &x, &d); n       int g = d - dist[v]; n       if (g >= 0) update(max(1, D - g), x, bit[0]); n       if (g >= 0) { n         int cob = head[v] + g; n         if (cob <= tail[head[v]]) { n           update(cob, x, bit[1]); n           update(min(tail[head[v]] + 1, name[v] + d + 1), -x, bit[1]); n         } n       } else { n         update(name[v] - d, x, bit[1]); n         update(min(tail[head[v]] + 1, name[v] + d + 1), -x, bit[1]); n       } n     } else { n       scanf( %d , &v); n       int resa = query(D - dist[v], bit[0]); n       int resb = query(name[v], bit[1]); n       printf( %d n , resa + resb); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, j, k, p, t, a = 0, b = 0; n   cin >> n; n   vector<long long int> vec; n   for (i = 0; i < n; i++) { n     cin >> p; n     if (p % 2 == 0) n       a++; n     else n       b++; n     vec.push_back(p); n   } n   if (a == 0 || b == 0) { n     for (i = 0; i < n; i++) cout << vec[i] <<    ; n   } else { n     sort(vec.begin(), vec.end()); n     for (i = 0; i < n; i++) cout << vec[i] <<    ; n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, p, q, p1[150], q1[150], pq[300], i, j, k, l, m; n   scanf( %d , &n); n   scanf( %d , &p); n   for (i = 0; i < p; i++) { n     scanf( %d , &p1[i]); n   } n   scanf( %d , &q); n   for (j = 0; j < q; j++) { n     scanf( %d , &q1[j]); n   } n   if ((p == 0) && (q == 0)) { n     printf( Oh, my keyboard! n ); n     return 0; n   } else { n     for (k = 0; k < (p + q); k++) { n       if (k < p) { n         pq[k] = p1[k]; n       } else { n         pq[k] = q1[k - p]; n       } n     } n     for (l = 1; l <= n; l++) { n       for (m = 0; m < p + q; m++) { n         if (pq[m] == l) { n           break; n         } else if (m == (p + q - 1)) { n           printf( Oh, my keyboard! n ); n           return 0; n         } n       } n     } n     printf( I become the guy. n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-9; n long double _A[19][100009], _b[19], _c[100009]; n struct LPSolver { n   int m, n; n   vector<int> B, N; n   vector<vector<long double> > D; n   LPSolver(const vector<vector<long double> > &A, const vector<long double> &b, n            const vector<long double> &c) n       : m(b.size()), n         n(c.size()), n         N(n + 1), n         B(m), n         D(m + 2, vector<long double>(n + 2)) { n     for (int i = 0; i < m; i++) n       for (int j = 0; j < n; j++) D[i][j] = A[i][j]; n     for (int i = 0; i < m; i++) { n       B[i] = n + i; n       D[i][n] = -1; n       D[i][n + 1] = b[i]; n     } n     for (int j = 0; j < n; j++) { n       N[j] = j; n       D[m][j] = -c[j]; n     } n     N[n] = -1; n     D[m + 1][n] = 1; n   } n   void Pivot(int r, int s) { n     double inv = 1.0 / D[r][s]; n     for (int i = 0; i < m + 2; i++) n       if (i != r) n         for (int j = 0; j < n + 2; j++) n           if (j != s) D[i][j] -= D[r][j] * D[i][s] * inv; n     for (int j = 0; j < n + 2; j++) n       if (j != s) D[r][j] *= inv; n     for (int i = 0; i < m + 2; i++) n       if (i != r) D[i][s] *= -inv; n     D[r][s] = inv; n     swap(B[r], N[s]); n   } n   bool Simplex(int phase) { n     int x = phase == 1 ? m + 1 : m; n     while (true) { n       int s = -1; n       for (int j = 0; j <= n; j++) { n         if (phase == 2 && N[j] == -1) continue; n         if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) n           s = j; n       } n       if (D[x][s] > -EPS) return true; n       int r = -1; n       for (int i = 0; i < m; i++) { n         if (D[i][s] < EPS) continue; n         if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] || n             (D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r]) n           r = i; n       } n       if (r == -1) return false; n       Pivot(r, s); n     } n   } n   long double Solve(vector<long double> &x) { n     int r = 0; n     for (int i = 1; i < m; i++) n       if (D[i][n + 1] < D[r][n + 1]) r = i; n     if (D[r][n + 1] < -EPS) { n       Pivot(r, n); n       if (!Simplex(1) || D[m + 1][n + 1] < -EPS) n         return -numeric_limits<long double>::infinity(); n       for (int i = 0; i < m; i++) n         if (B[i] == -1) { n           int s = -1; n           for (int j = 0; j <= n; j++) n             if (s == -1 || D[i][j] < D[i][s] || n                 D[i][j] == D[i][s] && N[j] < N[s]) n               s = j; n           Pivot(i, s); n         } n     } n     if (!Simplex(2)) return numeric_limits<long double>::infinity(); n     x = vector<long double>(n); n     for (int i = 0; i < m; i++) n       if (B[i] < n) x[B[i]] = D[i][n + 1]; n     return D[m][n + 1]; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, i; n   long double p, q; n   cin >> n >> p >> q; n   m = 2; n   for (i = 0; i < n; i++) { n     cin >> _A[0][i] >> _A[1][i]; n     _A[0][i] = -_A[0][i]; n     _A[1][i] = -_A[1][i]; n     _c[i] = -1; n   } n   _b[0] = -p; n   _b[1] = -q; n   vector<vector<long double> > A(m); n   vector<long double> b(_b, _b + m); n   vector<long double> c(_c, _c + n); n   for (i = 0; i < m; i++) A[i] = vector<long double>(_A[i], _A[i] + n); n   LPSolver solver(A, b, c); n   vector<long double> x; n   long double value = solver.Solve(x); n   cout << fixed << setprecision(10) << (-value) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long a[1111], b[1111]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) cin >> b[i]; n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     long long cur1 = 0, cur2 = 0; n     for (int j = i; j <= n; ++j) { n       cur1 = cur1 | a[j]; n       cur2 = cur2 | b[j]; n       ans = max(ans, cur1 + cur2); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 10; n const long long M = 3e5 + 1; n const long long oo = 1e9; n long long n, minn, ans; n long long bit[N], deg[N]; n pair<long long, long long> nex[N]; n vector<long long> vi[N]; n void upd(long long pos, long long val) { n   for (long long i = pos; i < N; i += (i & -i)) bit[i] += val; n } n long long get(long long pos) { n   long long res = 0; n   for (long long i = pos; i >= 1; i -= (i & -i)) res += bit[i]; n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> minn; n   for (long long i = 1; i <= n; ++i) { n     long long x, y; n     cin >> x >> y; n     nex[i] = pair<long long, long long>(x, y); n     vi[x].push_back(i); n     vi[y].push_back(i); n     deg[x]++; n     deg[y]++; n   } n   for (long long i = 1; i <= n; ++i) upd(M - vi[i].size(), 1); n   for (long long i = 1; i <= n; ++i) { n     long long cur = vi[i].size(); n     for (long long v : vi[i]) { n       long long die = (nex[v].first == i) ? nex[v].second : nex[v].first; n       upd(M - deg[die], -1); n       deg[die]--; n       upd(M - deg[die], 1); n     } n     upd(M - deg[i], -1); n     long long rem = max(0ll, minn - cur); n     ans += get(M - rem); n     upd(M - deg[i], 1); n     for (long long v : vi[i]) { n       long long die = (nex[v].first == i) ? nex[v].second : nex[v].first; n       upd(M - deg[die], -1); n       deg[die]++; n       upd(M - deg[die], 1); n     } n   } n   cout << ans / 2; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, c[3000001], s; n set<pair<long long, long long> > d, t; n set<pair<long long, long long> >::iterator p; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= k; i++) d.insert({c[i], i}); n   for (int i = k + 1; i <= k + n; i++) { n     if (i <= n) d.insert({c[i], i}); n     p = d.end(); n     p--; n     long long r = (*p).first; n     long long l = (*p).second; n     s += ((i - l) * r); n     d.erase(p); n     t.insert({l, i}); n   } n   cout << s << endl; n   for (auto e : t) cout << e.second <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sqr(long long x) { return x * x; } n int n, m, x, y; n int main() { n   cin >> x >> y >> n; n   long double ans = 1e9, p = 0; n   for (int i = 0; i < n; ++i) { n     int x1, y1, v; n     cin >> x1 >> y1 >> v; n     p = sqrt(sqr(x1 - x) + sqr(y1 - y)) / v; n     ans = min(ans, p); n   } n   cout << fixed << setprecision(10) << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 20; n long long n, m, x, y, ans, low, dp[maxn][1 << maxn]; n bool f[maxn][maxn]; n long long lowbit(long long x) { n   long long ret = 0; n   while (x) { n     if (x & 1) n       return ret; n     else n       ret++; n     x /= 2; n   } n } n int main() { n   scanf( %lld%lld , &n, &m); n   for (long long i = 1; i <= m; i++) n     scanf( %d%d , &x, &y), x--, y--, f[x][y] = f[y][x] = true; n   for (long long i = 0; i < n; i++) dp[i][1 << i] = 1; n   for (long long i = 1; i < (1 << n); i++) { n     long long minn = lowbit(i); n     for (long long j = 0; j < n; j++) n       if ((i & (1 << j)) && dp[j][i]) n         for (long long k = minn; k < n; k++) n           if (f[j][k]) { n             if ((!(i & (1 << k)))) n               dp[k][i | (1 << k)] += dp[j][i]; n             else if (k == minn) n               ans += dp[j][i]; n           } n   } n   printf( %lld n , (ans - m) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q[2005], a[2005], cum[2005]; n int cnt = 1, n; n set<int> use; n set<pair<int, int> > s; n set<pair<int, int> >::iterator it; n map<int, vector<int> > mp; n long long diff(int dif) { return (dif * 1LL * (dif + 1)) / 2; } n int main() { n   scanf( %d , &n); n   int cur = 0; n   for (int i = 0; i < n; i++) { n     int ti; n     scanf( %d , &ti); n     int ok = 1, ni = ti; n     while (ni > 0) { n       if (!(ni % 10 == 4 || ni % 10 == 7)) { n         ok = 0; n         break; n       } n       ni /= 10; n     } n     if (ok == 1) { n       if (cur > 0) { n         q[cnt] = cur; n         a[cnt] = 0; n         cnt++; n         cur = 0; n       } n       mp[ti].push_back(cnt); n       q[cnt] = 1; n       a[cnt] = ti; n       cnt++; n     } else n       cur++; n   } n   if (cur > 0) { n     q[cnt] = cur; n     a[cnt] = 0; n     cnt++; n   } n   cum[0] = 0; n   for (int i = 1; i < cnt; i++) { n     cum[i] = cum[i - 1] + q[i]; n   } n   long long ret = 0; n   for (int i = 1; i < cnt; i++) { n     if (q[i] >= 2) { n       for (int j = 1; j < q[i]; j++) { n         ret += j * 1LL * (j + 1) / 2 * (q[i] - j); n       } n       for (int j = 1; j < q[i]; j++) { n         ret += j * 1LL * (cum[cnt - 1] - cum[i]) * 1LL * (q[i] - j); n         ret += (q[i] - j) * 1LL * (cum[i - 1]) * 1LL * j; n       } n     } n     s.clear(); n     use.clear(); n     long long sum = diff(cum[i - 1]); n     if (i > 1) s.insert(make_pair(1, i - 1)); n     for (int j = i; j < cnt; j++) { n       if (a[j] == 0) { n         if (i != j) { n           ret += sum * 1LL * q[j] * 1LL * q[i]; n         } else { n           ret += sum * 1LL * q[i] * 1LL * (q[i] + 1) / 2; n         } n       } else { n         if (use.find(a[j]) == use.end()) { n           int len = mp[a[j]].size(); n           for (int k = 0; k < len; k++) { n             int pos = mp[a[j]][k]; n             if (pos >= i) break; n             it = s.lower_bound(make_pair(pos + 1, 0)); n             if (it != s.begin()) { n               it--; n               if ((*it).second >= pos) { n                 int pa = (*it).second, pb = (*it).first; n                 sum -= diff(cum[pa] - cum[pb - 1]); n                 s.erase(*it); n                 if (pa > pos) { n                   s.insert(make_pair(pos + 1, pa)); n                   sum += diff(cum[pa] - cum[pos]); n                 } n                 if (pb < pos) { n                   s.insert(make_pair(pb, pos - 1)); n                   sum += diff(cum[pos - 1] - cum[pb - 1]); n                 } n               } n             } n           } n           use.insert(a[j]); n         } n         ret += sum * 1LL * q[i]; n       } n       if (i != j) { n         if (!s.empty()) { n           it = s.end(); n           it--; n           if ((*it).second == i - 1) { n             int pos = cum[i - 1] - cum[(*it).first - 1]; n             ret += q[j] * 1LL * pos * 1LL * q[i] * 1LL * (q[i] - 1) / 2; n           } n         } n       } n     } n   } n   printf( %I64d n , ret); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 101000 * 8; n const int G = 3; n const int Mod = 998244353; n int n; n long long m; n int Fc[maxN], Ifc[maxN]; n int In[maxN], A[maxN], B[maxN], C[maxN], R[maxN]; n int QPow(int x, int cnt); n void NTT(int *P, int N, int opt); n int main() { n   Fc[0] = Ifc[0] = 1; n   for (int i = 1; i < maxN; i++) Fc[i] = 1ll * Fc[i - 1] * i % Mod; n   Ifc[maxN - 1] = QPow(Fc[maxN - 1], Mod - 2); n   for (int i = maxN - 2; i >= 1; i--) Ifc[i] = 1ll * Ifc[i + 1] * (i + 1) % Mod; n   scanf( %d%lld , &n, &m); n   int N = 1; n   while (N <= n + n) N <<= 1; n   m = m % (Mod - 1); n   for (int i = 0; i <= n; i++) n     scanf( %d , &In[i]), B[i] = 1ll * Fc[i] * In[i] % Mod; n   for (int i = 0; i <= n; i++) C[i] = Ifc[n - i]; n   NTT(B, N, 1); n   NTT(C, N, 1); n   for (int i = 0; i < N; i++) B[i] = 1ll * B[i] * C[i] % Mod; n   NTT(B, N, -1); n   for (int i = 0; i <= n; i++) A[i] = 1ll * B[i + n] * Ifc[i] % Mod; n   for (int i = 0; i <= n; i++) n     A[i] = 1ll * A[i] * QPow(QPow(i + 1, Mod - 2), m) % Mod * Fc[i] % Mod; n   memset(C, 0, sizeof(C)); n   for (int i = 0; i <= n; i++) C[i] = (i & 1) ? Mod - Ifc[i] : Ifc[i]; n   reverse(&C[0], &C[n + 1]); n   NTT(A, N, 1); n   NTT(C, N, 1); n   for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * C[i] % Mod; n   NTT(A, N, -1); n   for (int i = 0; i <= n; i++) printf( %I64d  , 1ll * A[i + n] * Ifc[i] % Mod); n   printf(  n ); n   return 0; n } n int QPow(int x, int cnt) { n   int ret = 1; n   while (cnt) { n     if (cnt & 1) ret = 1ll * ret * x % Mod; n     cnt >>= 1; n     x = 1ll * x * x % Mod; n   } n   return ret; n } n void NTT(int *P, int N, int opt) { n   int l = -1, _n = N; n   while (_n) ++l, _n >>= 1; n   for (int i = 0; i < N; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (l - 1)); n   for (int i = 0; i < N; i++) n     if (i < R[i]) swap(P[i], P[R[i]]); n   for (int i = 1; i < N; i <<= 1) { n     int dw = QPow(G, (Mod - 1) / (i << 1)); n     if (opt == -1) dw = QPow(dw, Mod - 2); n     for (int j = 0; j < N; j += (i << 1)) n       for (int k = 0, w = 1; k < i; k++, w = 1ll * w * dw % Mod) { n         int X = P[j + k], Y = 1ll * P[j + k + i] * w % Mod; n         P[j + k] = (X + Y) % Mod; n         P[j + k + i] = (X - Y + Mod) % Mod; n       } n   } n   if (opt == -1) { n     int inv = QPow(N, Mod - 2); n     for (int i = 0; i < N; i++) P[i] = 1ll * P[i] * inv % Mod; n   } n   return; n } n


#include <bits/stdc++.h> n using namespace std; n long long mi = 2e9; n long long ma = -2 * 1e9; n const int N = 1e5 + 5; n int main() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long na, nb, ka, kb; n   cin >> na >> nb >> ka >> kb; n   long long a[na], b[nb]; n   for (int i = 0; i < na; ++i) cin >> a[i]; n   for (int i = 0; i < nb; ++i) cin >> b[i]; n   sort(a, a + na); n   sort(b, b + nb); n   long long x = nb - kb; n   if (a[ka - 1] < b[nb - 1] && b[x] > a[ka - 1]) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n const long long Inf = 1ll << 50; n const long long N = 200010; n using namespace std; n long long cl[2 * N], b, c, d, i, j, k, l, n, ans, a[N], y, kk; n int main() { n   scanf( %I64d%I64d , &n, &k); n   for (i = n + 1; i > 0; i--) scanf( %I64d , &a[i]); n   memset(cl, 0, sizeof(cl)); n   cl[0] = a[0] = 0; n   for (i = 1; i <= n + 1; i++) { n     cl[i] = (cl[i - 1] + a[i - 1]) << 1ll; n     if ((cl[i] > Inf) || (cl[i] < -Inf)) break; n   } n   y = 0; n   ans = 0; n   i--; n   for (j = n + 1; j > i; j--) { n     if ((y & 1) == 0) n       y >>= 1ll; n     else n       break; n     y += a[j]; n   } n   if (i == j) { n     for (kk = i; kk > 0; kk--) { n       if ((y % 2) == 0) n         y /= 2ll; n       else n         break; n       if (cl[kk] + y - y == cl[kk] && abs(cl[kk] + y) <= k && n           (!(kk == 1 && (cl[kk] + y == 0)))) n         ans++; n       y += a[kk]; n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[300000]; n long long temp[300000]; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       temp[i] = arr[i]; n     } n     int st = 0; n     int endIdx1 = -1; n     for (int i = 0; i < n; i++) { n       if (arr[i] >= st) { n         st++; n         endIdx1 = i; n       } else { n         break; n       } n     } n     if (endIdx1 == n - 1) { n       cout <<  Yes  << endl; n       continue; n     } n     st = 0; n     int endIdx = n; n     for (int i = n - 1; i >= 0; i--) { n       if (arr[i] >= st) { n         st++; n         endIdx = i; n       } else { n         break; n       } n     } n     if (endIdx == 0) { n       cout <<  Yes  << endl; n       continue; n     } n     if (endIdx1 >= endIdx) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   long long k = 0, m = 1; n   while (t > 0) { n     if (t % 10 == 4) { n       k = k + m; n     } else { n       k = k + 2 * m; n     } n     m = m * 2; n     t = t / 10; n   } n   cout << k; n } n


#include <bits/stdc++.h> n using namespace std; n int marked[1000006]; n int f(int i, int j) { n   if (j == 1) { n     return marked[i]; n   } n   return f(marked[i], j - 1); n } n vector<int> v; n int main() { n   int n, a, b; n   scanf( %d %d %d , &n, &a, &b); n   int cut = -1; n   for (int i = 0; i <= n; i++) { n     if (i % a == 0 && (n - i) % b == 0) { n       cut = i; n       break; n     } n   } n   if (cut == -1) { n     printf( -1 n ); n     return 0; n   } n   int cnt = 0; n   for (int i = 1; i <= cut; i++) { n     cnt++; n     v.push_back(i); n     if (cnt == a) { n       if ((int)v.size() >= 2) { n         rotate(v.begin(), v.begin() + 1, v.end()); n       } n       for (int j = 0; j < v.size(); j++) { n         printf( %d  , v[j]); n       } n       cnt = 0; n       v.clear(); n     } n   } n   cnt = 0; n   for (int i = cut + 1; i <= n; i++) { n     cnt++; n     v.push_back(i); n     if (cnt == b) { n       if ((int)v.size() >= 2) { n         rotate(v.begin(), v.begin() + 1, v.end()); n       } n       for (int j = 0; j < v.size(); j++) { n         printf( %d  , v[j]); n       } n       cnt = 0; n       v.clear(); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void vin(vector<long long int> &v, long long int t) { n   for (long long int i = 0; i < t; i++) cin >> v[i]; n } n void vout(vector<long long int> &v, long long int t) { n   for (long long int i = 0; i < t; i++) cout << v[i] <<    ; n   cout << endl; n } n void ingraph(vector<vector<long long int>> &graph, long long int m) { n   long long int x, y; n   for (long long int i = 0; i < m; i++) { n     cin >> x >> y; n     x--, y--; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return a * b / gcd(a, b); n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   vector<vector<long long int>> a(n, vector<long long int>(m)), n       dp(n, vector<long long int>(m)); n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) cin >> a[i][j]; n   } n   long long int ans = (long long int)1e18; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       long long int b = a[i][j] - i - j; n       if (a[0][0] < b) continue; n       vector<vector<long long int>> dp( n           n, vector<long long int>(m, (long long int)1e18)); n       dp[0][0] = a[0][0] - b; n       for (long long int p = 0; p < n; p++) { n         for (long long int q = 0; q < m; q++) { n           if (a[p][q] < (b + p + q)) continue; n           if (p > 0) n             dp[p][q] = min(dp[p][q], dp[p - 1][q] + a[p][q] - (b + q + p)); n           if (q > 0) n             dp[p][q] = min(dp[p][q], dp[p][q - 1] + a[p][q] - (b + q + p)); n         } n       } n       ans = min(ans, dp[n - 1][m - 1]); n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct SCC { n   int N; n   vector<bool> used; n   vector<int> vs; n   vector<int> cmp; n   void dfs(int v, const vector<int> G[]) { n     used[v] = true; n     for (auto u : G[v]) { n       if (!used[u]) dfs(u, G); n     } n     vs.push_back(v); n   } n   void rdfs(int v, int k, const vector<int> rG[]) { n     used[v] = true; n     cmp[v] = k; n     for (auto u : rG[v]) { n       if (!used[u]) rdfs(u, k, rG); n     } n   } n   void calculate(int Nin, const vector<int> G[], const vector<int> rG[]) { n     N = Nin; n     used.resize(N); n     fill(used.begin(), used.end(), false); n     for (int v = 0; v < N; v++) { n       if (!used[v]) dfs(v, G); n     } n     fill(used.begin(), used.end(), false); n     cmp.resize(N); n     int k = 0; n     for (int i = vs.size() - 1; i >= 0; i--) { n       if (!used[vs[i]]) rdfs(vs[i], k++, rG); n     } n   } n   int dump_graph(const vector<int> G[], vector<int> edges2[]) { n     for (int i = 0; i < N; i++) { n       for (auto j : G[i]) { n         if (cmp[i] != cmp[j]) { n           edges2[cmp[i]].push_back(cmp[j]); n         } n       } n     } n     return *max_element(cmp.begin(), cmp.end()) + 1; n   } n }; n vector<int> edges[5000000], edges2[5000000]; n int main() { n   int N, M, D; n   cin >> N >> M >> D; n   for (int i = 0; i < M; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     a--; n     b--; n     for (int d = 0; d < D; d++) { n       int s = a * D + d, t = b * D + (d + 1) % D; n       edges[s].push_back(t); n       edges2[t].push_back(s); n     } n   } n   SCC scc; n   scc.calculate(D * N, edges, edges2); n   for (int i = 0; i < N * D; i++) edges2[i].clear(); n   int N2 = scc.dump_graph(edges, edges2); n   vector<int> num(N * D); n   char s[50]; n   for (int i = 0; i < N; i++) { n     scanf( %s , s); n     set<int> st; n     for (int d = 0; d < D; d++) { n       if (s[d] ==  0 ) continue; n       int c = scc.cmp[i * D + d]; n       if (!st.count(c)) { n         num[c]++; n         st.insert(c); n       } n     } n   } n   vector<int> indeg(N2); n   for (int i = 0; i < N2; i++) n     for (int j : edges2[i]) indeg[j]++; n   vector<int> dp(N2); n   queue<int> que; n   for (int i = 0; i < N2; i++) { n     dp[i] = -1e9; n     if (indeg[i] == 0) que.push(i); n   } n   int st = scc.cmp[0]; n   dp[st] = num[st]; n   while (que.size()) { n     int i = que.front(); n     que.pop(); n     for (int j : edges2[i]) { n       dp[j] = max(dp[j], dp[i] + num[j]); n       indeg[j]--; n       if (indeg[j] == 0) que.push(j); n     } n   } n   int ans = *max_element(dp.begin(), dp.end()); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n(0); n   scanf( %d n , &n); n   std::string number; n   getline(std::cin, number); n   if (number.size() <= 3) { n     std::cout << number << std::endl; n   } else { n     int index(0); n     if (number.size() % 2) { n       std::cout << number.substr(0, 3); n       index = 3; n     } else { n       std::cout << number.substr(0, 2); n       index = 2; n     } n     while (index < number.size()) { n       std::cout <<  - ; n       std::cout << number.substr(index, 2); n       index += 2; n     } n     std::cout << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct E { n   int ad, id, ne; n } e[100010 * 4]; n int n, m, q, u[100010], v[100010], w[100010], h1[100010], h2[100010], n     de[100010], fa[100010], f[100010], p0[100010], p1[100010]; n bool o[100010]; n void add(int* he, int x, int y, int d) { n   static int t = 0; n   ++t, e[t].ne = he[x], he[x] = t, e[t].ad = y, e[t].id = d; n } n int ff(int* f, int x) { n   if (f[x] == x) n     return x; n   else n     return f[x] = ff(f, f[x]); n } n void ff1(int x) { n   p0[x] = p1[x] = x; n   for (int p = h1[x]; p; p = e[p].ne) { n     int y = e[p].ad; n     if (!de[y]) { n       fa[y] = x, de[y] = de[x] + 1, ff1(y), p1[y] = x; n       if (ff(p0, x) == ff(p0, y)) o[x] |= o[y]; n     } else if (de[y] + 1 < de[x]) { n       int z = ff(p0, x); n       while (de[z] > de[y] + 1) p0[z] = fa[z], z = ff(p0, z); n       if ((de[x] - de[y]) % 2 == 0) o[x] = 1; n     } n   } n   for (int p = h2[x]; p; p = e[p].ne) n     if (de[e[p].ad]) w[e[p].id] = ff(p1, e[p].ad); n } n void ff2(int x) { n   if (o[ff(p0, x)]) f[x]++; n   for (int i = h1[x]; i; i = e[i].ne) { n     int y = e[i].ad; n     if (de[y] == de[x] + 1) { n       if (ff(p0, x) == ff(p0, y)) o[y] |= o[x]; n       f[y] = f[x], ff2(y); n     } n   } n } n bool chk(int u, int v, int w) { n   if (ff(p1, u) != ff(p1, v)) return 0; n   if ((de[u] - de[v]) % 2) return 1; n   return f[u] + f[v] - f[w] * 2 > 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, x, y; i <= m; i++) n     scanf( %d%d , &x, &y), add(h1, x, y, i), add(h1, y, x, i); n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) n     scanf( %d%d , u + i, v + i), add(h2, u[i], v[i], i), add(h2, v[i], u[i], i); n   for (int i = 1; i <= n; i++) n     if (!de[i]) de[i] = 1, ff1(i), ff2(i); n   for (int i = 1; i <= q; i++) puts(chk(u[i], v[i], w[i]) ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ,  unroll-loops ) n using namespace std; n template <typename T1, typename T2> n inline void chmin(T1& a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n inline void chmax(T1& a, T2 b) { n   if (a < b) a = b; n } n const long long MOD = 1e9 + 7; n const long long MAX = 200010; n const double pi = acos(-1); n vector<long long> G[200010]; n long long dp[200020]; n long long dp_init(long long n, long long pre) { n   if (dp[n] != 0) return dp[n]; n   long long res = 1; n   for (auto nex : G[n]) { n     if (nex == pre) continue; n     res += dp_init(nex, n); n   } n   dp[n] = res; n   return res; n } n long long ans = 0; n long long N; n bool used[200020]; n void calc(long long n, long long res) { n   used[n] = true; n   for (auto nex : G[n]) { n     if (used[nex]) continue; n     long long tmp = res + N - 2 * dp[nex]; n     chmax(ans, tmp); n     used[nex] = true; n     calc(nex, tmp); n   } n } n void solve() { n   cin >> N; n   for (long long i = 0; i < N - 1; i++) { n     long long u, v; n     cin >> u >> v; n     u--, v--; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   long long root = 0; n   dp_init(0, 0); n   long long tmp = 0; n   for (long long i = 0; i < N; i++) tmp += dp[i]; n   ans = tmp; n   calc(0, tmp); n   cout << ans <<   n ; n   ; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int xd[] = {0, 1, 0, -1}; n int yd[] = {1, 0, -1, 0}; n int xx[] = {0, 1, 0, -1, 1, -1, 1, -1}; n int yy[] = {1, 0, -1, 0, 1, 1, -1, -1}; n int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1}; n int ky[] = {1, 2, -1, -2, -2, -1, 1, 2}; n bool islucky(int x) { n   int four = 0, seven = 0; n   while (x) { n     if (x % 10 != 7 && x % 10 != 4) return false; n     if (x % 10 == 7) n       seven++; n     else n       four++; n     x = x / 10; n   } n   return four == seven; n } n bool is_prime(long long x) { n   if (x == 1) return false; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n long long C(int k, int l) { n   if (l == k) return 1; n   if (l * 2 > k) l = k - l; n   long long res = 1, temp = l; n   for (int i = k - l + 1; i <= k; i++) { n     res *= i / temp; n     temp--; n   } n   return res; n } n long long sumOfDigit(int x) { n   int res = 0; n   while (x) { n     res += x % 10; n     x /= 10; n   } n   return res; n } n uint64_t bigPow(uint64_t base, uint64_t p) { n   uint64_t temp = 1; n   while (p--) { n     temp *= base; n   } n   return temp; n } n vector<int> soso; n bool prime[10000001] = {0}; n void SieveOfEratosthenes(int n) { n   soso.push_back(2); n   for (int p = 3; p * p <= n; p += 2) { n     if (prime[p] == false) { n       for (int i = p * 2; i <= n; i += p) prime[i] = true; n     } n   } n   for (int i = 3; i <= n; i += 2) n     if (!prime[i]) soso.push_back(i); n } n vector<vector<int> > AdjList; n bool visited[100005]; n void dfs(int s) { n   if (visited[s]) return; n   visited[s] = 1; n   for (int i = 0; i < AdjList[s].size(); i++) dfs(AdjList[s][i]); n   return; n } n long long gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   char grid[8][8]; n   for (int i = 0; i < 8; i++) cin >> grid[i]; n   int w = 0, b = 0; n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) { n       if (grid[i][j] !=  . ) { n         char c = grid[i][j]; n         if (c ==  q ) b += 9; n         if (c ==  Q ) w += 9; n         if (c ==  r ) b += 5; n         if (c ==  R ) w += 5; n         if (c ==  b  || c ==  n ) b += 3; n         if (c ==  B  || c ==  N ) w += 3; n         if (c ==  p ) b += 1; n         if (c ==  P ) w += 1; n       } n     } n   if (b == w) n     cout <<  Draw ; n   else if (b > w) n     cout <<  Black ; n   else n     cout <<  White ; n } n


#include <bits/stdc++.h> n const long long N = 100100; n const long long MAX = 1000000100; n using namespace std; n long long a[N] = {0}; n int main() { n   long long n; n   while (scanf( %lld , &n) != EOF) { n     map<long long, long long> num; n     for (long long i = 0; i < n; i++) { n       scanf( %lld , &a[i]); n       num[a[i]]++; n     } n     sort(a, a + n); n     long long min1 = MAX + 1, min2 = MAX + 2, min3 = MAX + 3; n     for (long long i = 0; i < n; i++) { n       if (a[i] < min1) { n         min1 = a[i]; n       } else if (a[i] > min1 && a[i] < min2) { n         min2 = a[i]; n       } else if (a[i] > min2 && a[i] < min3) { n         min3 = a[i]; n         break; n       } n     } n     if (num[min1] >= 3) { n       long long sum = 1; n       for (long long i = 3; i >= 1; i--) sum *= num[min1]--; n       cout << sum / 6 << endl; n     } else if (num[min1] == 2) { n       cout << num[min2] << endl; n     } else { n       if (num[min2] >= 2) { n         long long sum = 1; n         for (long long i = 2; i >= 1; i--) sum *= num[min2]--; n         cout << sum / 2 << endl; n       } else { n         cout << num[min3] << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m = 0, b = 0; n int main() { n   int n; n   cin >> n; n   int Arrpi[n]; n   for (int j = 0; j < n; ++j) { n     cin >> Arrpi[j]; n   } n   int A[n]; n   for (int l = 0; l < n; ++l) { n     m = Arrpi[l] - 1; n     b = l + 1; n     A[m] = b; n   } n   for (int y = 0; y < n; ++y) { n     cout << A[y] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   register T sum = 0; n   register char cc = getchar(); n   int sym = 1; n   while (cc !=  -  && (cc >  9  || cc <  0 )) cc = getchar(); n   if (cc ==  - ) sym = -1, cc = getchar(); n   sum = sum * 10 + cc -  0 ; n   cc = getchar(); n   while (cc >=  0  && cc <=  9 ) sum = sum * 10 + cc -  0 , cc = getchar(); n   return sym * sum; n } n template <typename T> n inline T read(T& a) { n   a = read<T>(); n   return a; n } n template <typename T, typename... Others> n inline void read(T& a, Others&... b) { n   a = read(a); n   read(b...); n } n int n, ans = 0x7fffffff, b[200010], prim[1000010]; n long long a[200010]; n set<long long> s; n void init() { n   for (int i = 2; i <= 1000000; i++) { n     if (!prim[i]) prim[++prim[0]] = i; n     for (int j = 1; j <= prim[0] && i * prim[j] <= 1000000; j++) { n       prim[i * prim[j]] = true; n       if (i % prim[j]) break; n     } n   } n } n void div(long long x) { n   for (int i = 1; i <= prim[0]; i++) { n     if (x % prim[i]) continue; n     s.insert(prim[i]); n     while (x % prim[i] == 0) x /= prim[i]; n   } n   if (x != 1) s.insert(x); n } n int solve(long long p) { n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     long long d = a[i] < p ? p - a[i] : min(a[i] % p, p - a[i] % p); n     res = min(1ll * n, res + d); n   } n   return res; n } n int main() { n   srand(time(NULL)); n   init(); n   read(n); n   for (int i = 1; i <= n; i++) { n     read(a[i]); n     b[i] = i; n   } n   random_shuffle(b + 1, b + 1 + n); n   for (int i = 1; i <= 100 && i <= n; i++) { n     long long t = a[b[i]]; n     div(t); n     div(t + 1); n     if (t > 1) div(t - 1); n   } n   for (auto i : s) ans = min(ans, solve(i)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, c, q, i; n   cin >> q; n   for (i = 1; i <= q; i++) { n     cin >> n; n     c = 0; n     while (1) { n       if (n % 2 == 0) { n         n = n / 2; n         c++; n       } else if (n % 3 == 0) { n         n = (2 * n) / 3; n         c++; n       } else if (n % 5 == 0) { n         n = (4 * n) / 5; n         c++; n       } else n         break; n     } n     if (n == 1) n       cout << c << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1505; n const int mo = 998244353; n int n, m, a[N][N]; n int fa[N], sz[N], S[N]; n int f[N][N], tmp[N]; n pair<int, int> e[N * N]; n int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); } n void merge(int x, int y) { n   memset(tmp, 0, sizeof(tmp)); n   for (int i = (int)(1); i <= (int)(sz[x]); i++) n     for (int j = (int)(1); j <= (int)(sz[y]); j++) n       tmp[i + j] = (tmp[i + j] + 1ll * f[x][i] * f[y][j]) % mo; n   memcpy(f[x], tmp, sizeof(f[x])); n   sz[x] += sz[y]; n   S[x] += S[y]; n   fa[y] = x; n } n bool cmp(pair<int, int> x, pair<int, int> y) { n   return a[x.first][x.second] < a[y.first][y.second]; n } n int main() { n   scanf( %d , &n); n   for (int i = (int)(1); i <= (int)(n); i++) n     for (int j = (int)(1); j <= (int)(n); j++) scanf( %d , &a[i][j]); n   for (int i = (int)(1); i <= (int)(n); i++) n     for (int j = (int)(i + 1); j <= (int)(n); j++) n       e[++m] = pair<int, int>(i, j); n   sort(e + 1, e + m + 1, cmp); n   for (int i = (int)(1); i <= (int)(n); i++) n     fa[i] = i, sz[i] = 1, S[i] = 0, f[i][1] = 1; n   for (int i = (int)(1); i <= (int)(m); i++) { n     int x = get(e[i].first); n     int y = get(e[i].second); n     if (x != y) merge(x, y); n     ++S[x]; n     if (S[x] == sz[x] * (sz[x] - 1) / 2) f[x][1] = (f[x][1] + 1) % mo; n   } n   for (int i = (int)(1); i <= (int)(n); i++) cout << f[get(1)][i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(string s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n class segtree { n  public: n   struct node { n     long long mn = 0; n     long long add = 0; n     void apply(int l, int r, long long v) { n       mn += v; n       add += v; n     } n   }; n   node unite(const node &a, const node &b) const { n     node res; n     res.mn = min(a.mn, b.mn); n     return res; n   } n   inline void push(int x, int l, int r) { n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     if (tree[x].add != 0) { n       tree[x + 1].apply(l, y, tree[x].add); n       tree[z].apply(y + 1, r, tree[x].add); n       tree[x].add = 0; n     } n   } n   inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } n   int n; n   vector<node> tree; n   void build(int x, int l, int r) { n     if (l == r) { n       return; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     build(x + 1, l, y); n     build(z, y + 1, r); n     pull(x, z); n   } n   template <typename M> n   void build(int x, int l, int r, const vector<M> &v) { n     if (l == r) { n       tree[x].apply(l, r, v[l]); n       return; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     build(x + 1, l, y, v); n     build(z, y + 1, r, v); n     pull(x, z); n   } n   node get(int x, int l, int r, int ll, int rr) { n     if (ll <= l && r <= rr) { n       return tree[x]; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     push(x, l, r); n     node res{}; n     if (rr <= y) { n       res = get(x + 1, l, y, ll, rr); n     } else { n       if (ll > y) { n         res = get(z, y + 1, r, ll, rr); n       } else { n         res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); n       } n     } n     pull(x, z); n     return res; n   } n   template <typename... M> n   void modify(int x, int l, int r, int ll, int rr, const M &...v) { n     if (ll <= l && r <= rr) { n       tree[x].apply(l, r, v...); n       return; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     push(x, l, r); n     if (ll <= y) { n       modify(x + 1, l, y, ll, rr, v...); n     } n     if (rr > y) { n       modify(z, y + 1, r, ll, rr, v...); n     } n     pull(x, z); n   } n   int find_first_knowingly(int x, int l, int r, n                            const function<bool(const node &)> &f) { n     if (l == r) { n       return l; n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res; n     if (f(tree[x + 1])) { n       res = find_first_knowingly(x + 1, l, y, f); n     } else { n       res = find_first_knowingly(z, y + 1, r, f); n     } n     pull(x, z); n     return res; n   } n   int find_first(int x, int l, int r, int ll, int rr, n                  const function<bool(const node &)> &f) { n     if (ll <= l && r <= rr) { n       if (!f(tree[x])) { n         return -1; n       } n       return find_first_knowingly(x, l, r, f); n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res = -1; n     if (ll <= y) { n       res = find_first(x + 1, l, y, ll, rr, f); n     } n     if (rr > y && res == -1) { n       res = find_first(z, y + 1, r, ll, rr, f); n     } n     pull(x, z); n     return res; n   } n   int find_last_knowingly(int x, int l, int r, n                           const function<bool(const node &)> &f) { n     if (l == r) { n       return l; n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res; n     if (f(tree[z])) { n       res = find_last_knowingly(z, y + 1, r, f); n     } else { n       res = find_last_knowingly(x + 1, l, y, f); n     } n     pull(x, z); n     return res; n   } n   int find_last(int x, int l, int r, int ll, int rr, n                 const function<bool(const node &)> &f) { n     if (ll <= l && r <= rr) { n       if (!f(tree[x])) { n         return -1; n       } n       return find_last_knowingly(x, l, r, f); n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res = -1; n     if (rr > y) { n       res = find_last(z, y + 1, r, ll, rr, f); n     } n     if (ll <= y && res == -1) { n       res = find_last(x + 1, l, y, ll, rr, f); n     } n     pull(x, z); n     return res; n   } n   segtree(int _n) : n(_n) { n     assert(n > 0); n     tree.resize(2 * n - 1); n     build(0, 0, n - 1); n   } n   template <typename M> n   segtree(const vector<M> &v) { n     n = v.size(); n     assert(n > 0); n     tree.resize(2 * n - 1); n     build(0, 0, n - 1, v); n   } n   node get(int ll, int rr) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return get(0, 0, n - 1, ll, rr); n   } n   node get(int p) { n     assert(0 <= p && p <= n - 1); n     return get(0, 0, n - 1, p, p); n   } n   template <typename... M> n   void modify(int ll, int rr, const M &...v) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     modify(0, 0, n - 1, ll, rr, v...); n   } n   int find_first(int ll, int rr, const function<bool(const node &)> &f) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return find_first(0, 0, n - 1, ll, rr, f); n   } n   int find_last(int ll, int rr, const function<bool(const node &)> &f) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return find_last(0, 0, n - 1, ll, rr, f); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, tt; n   cin >> n >> tt; n   vector<int> p(n); n   vector<int> w(n); n   for (int i = 1; i < n; i++) { n     cin >> p[i] >> w[i]; n     p[i]--; n   } n   vector<vector<int>> qs(n); n   vector<int> L(tt), R(tt), v(tt); n   vector<long long> res(tt, -1); n   for (int i = 0; i < tt; i++) { n     cin >> v[i] >> L[i] >> R[i]; n     v[i]--; n     L[i]--; n     R[i]--; n     qs[v[i]].push_back(i); n   } n   vector<long long> dst(n); n   for (int i = 1; i < n; i++) { n     dst[i] = dst[p[i]] + w[i]; n   } n   vector<int> is_leaf(n, 1); n   for (int i = 1; i < n; i++) { n     is_leaf[p[i]] = 0; n   } n   const long long inf = (long long)1e18; n   for (int i = 0; i < n; i++) { n     if (!is_leaf[i]) { n       dst[i] = inf; n     } n   } n   segtree st(dst); n   vector<int> stk; n   vector<int> to(n, n - 1); n   for (int i = 0; i < n; i++) { n     while (!stk.empty() && stk.back() != p[i]) { n       int u = stk.back(); n       to[u] = i - 1; n       stk.pop_back(); n     } n     stk.push_back(i); n   } n   42; n   stk.clear(); n   for (int i = 0; i < n; i++) { n     while (!stk.empty() && stk.back() != p[i]) { n       int u = stk.back(); n       st.modify(0, n - 1, -w[u]); n       st.modify(u, to[u], 2 * w[u]); n       stk.pop_back(); n     } n     stk.push_back(i); n     st.modify(0, n - 1, w[i]); n     st.modify(i, to[i], -2 * w[i]); n     for (int id : qs[i]) { n       res[id] = st.get(L[id], R[id]).mn; n     } n   } n   for (int i = 0; i < tt; i++) { n     cout << res[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n string s; n string result; n int main() { n   cin >> N; n   cin >> s; n   result = s; n   result[(N - 1) / 2] = s[0]; n   int sign = N % 2 == 0 ? 1 : -1; n   int val = 1; n   int start = (N - 1) / 2; n   for (int i = 1; i < N; ++i, sign *= -1, val++) n     start = start + val * sign, result[start] = s[i]; n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n struct edge { n   int u, v, id; n   edge(int a = 0, int b = 0, int c = 0) { u = a, v = b, id = c; } n }; n vector<edge> S, M, E; n struct dsu { n   int par[N]; n   vector<edge> ed; n   dsu() { memset(par, -1, sizeof par); } n   int get_par(int u) { return ~par[u] ? par[u] = get_par(par[u]) : u; } n   bool merge(edge e) { n     if (get_par(e.u) == get_par(e.v)) return false; n     par[get_par(e.u)] = get_par(e.v), ed.push_back(e); n     return true; n   } n   int size() { return ed.size(); } n } A, B, C; n int n, m; n void notValid() { cout <<  -1 n , exit(0); } n void readInput() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     char c; n     int u, v; n     cin >> u >> v >> c; n     u--, v--; n     if (c ==  S ) n       S.push_back(edge(u, v, i)); n     else n       M.push_back(edge(u, v, i)); n   } n } n void solve() { n   for (auto e : S) C.merge(e); n   if (n & 1 ^ 1 || C.size() < n >> 1) notValid(); n   for (auto e : M) n     if (C.merge(e)) A.merge(e); n   if (C.size() != n - 1) notValid(); n   for (auto e : M) n     if (A.size() < n >> 1) A.merge(e); n   if (A.size() != n >> 1) notValid(); n   E = A.ed; n   for (auto e : S) n     if (A.merge(e)) B.merge(e); n   E.insert(E.end(), B.ed.begin(), B.ed.end()); n } n void writeOutput() { n   cout << n - 1 << endl; n   for (auto e : E) cout << e.id + 1 <<    ; n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   readInput(), solve(), writeOutput(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll inf = 1e9 + 10; n const ll inf_ll = 1e18 + 10; n ll qry(ll t, ll i, ll j, ll k) { n   cout << t <<     << i <<     << j <<     << k << endl; n   ll r; n   cin >> r; n   return r; n } n int main() { n   ll n; n   cin >> n; n   ll b = 2; n   for (ll i = 2; i <= n; i++) n     if (i != b && qry(2, 1, i, b) > 0) b = i; n   vector<ll> h(n + 5), ord(n - 2), l, r; n   for (ll i = 2; i <= n; i++) n     if (i != b) h[i] = qry(1, 1, i, b); n   iota((ord).begin(), (ord).end(), 3); n   for (ll& i : ord) n     if (i == b) i = 2; n   sort((ord).begin(), (ord).end(), [&](ll i, ll j) { return h[i] < h[j]; }); n   ll c = ord.back(); n   for (ll i : ord) { n     if (i != c && qry(2, 1, c, i) > 0) n       l.push_back(i); n     else n       r.push_back(i); n   } n   cout <<  0 1   << b; n   for (ll i : r) cout <<     << i; n   reverse((l).begin(), (l).end()); n   for (ll i : l) cout <<     << i; n   cout << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   int x; n   int sum = 0; n   std::cin >> n; n   std::cin >> x; n   unsigned int t1 = 0, t2 = 0, tcurr = 1; n   for (int i = 0; i < n; i++) { n     std::cin >> t1; n     std::cin >> t2; n     sum += (t1 - tcurr) % x + t2 - t1 + 1; n     tcurr = t2 + 1; n   } n   std::cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> A; n vector<int> d; n vector<int> P; n vector<int> g; n vector<vector<int>> G; n int bestu, bestv, bestw, bestvv; n void dfs(int x, int v, int n) { n   for (int i = 0; i < G[x].size(); ++i) { n     int y = G[x][i]; n     int u = g[y]; n     if (u >= 0) { n       if (d[u] < 0) { n         d[u] = v; n         P[u] = y; n         P[y] = x; n         dfs(u, v, n); n       } n     } else if (A[y] + A[v] > bestw) { n       bestw = A[y] + A[v]; n       bestu = x; n       bestv = y; n       bestvv = v; n     } n   } n } n bool cmp(int a, int b) { return A[a] > A[b]; } n void solve() { n   int n, k; n   scanf( %d%d , &n, &k); n   vector<int> a(1 << n); n   for (int i = 0; i < a.size(); ++i) scanf( %d , &a[i]); n   vector<array<int, 3>> edges; n   { n     vector<int> bc(1 << n); n     for (int i = 1; i < bc.size(); ++i) bc[i] = bc[i & (i - 1)] + 1; n     array<int, 3> e; n     for (int i = 0; i < bc.size(); ++i) { n       if (bc[i] % 2 == 0) n         for (int j = 0; j < n; ++j) { n           e[0] = -(a[i] + a[i ^ (1 << j)]); n           e[1] = i; n           e[2] = i ^ (1 << j); n           edges.push_back(e); n         } n     } n   } n   int edges_cnt = min(2 * n * k + 1, (int)edges.size()); n   nth_element(edges.begin(), edges.begin() + edges_cnt, edges.end()); n   vector<int> idx(1 << n, -1); n   int vert = 0; n   for (int i = 0; i < edges_cnt; ++i) n     if (idx[edges[i][1]] < 0) { n       A.push_back(a[edges[i][1]]); n       idx[edges[i][1]] = vert; n       ++vert; n     } n   G.resize(vert); n   int vert2 = vert; n   for (int i = 0; i < edges_cnt; ++i) { n     if (idx[edges[i][2]] < 0) { n       A.push_back(a[edges[i][2]]); n       idx[edges[i][2]] = vert2; n       ++vert2; n     } n     G[idx[edges[i][1]]].push_back(idx[edges[i][2]]); n   } n   vector<int> idx1(vert); n   for (int i = 0; i < vert; ++i) idx1[i] = i; n   sort(idx1.begin(), idx1.end(), cmp); n   vector<char> was(vert); n   g.assign(vert2, -1); n   P.resize(vert2); n   int res = 0; n   for (int i = 0; i < k; ++i) { n     d.assign(vert, -1); n     bestw = -1; n     for (int j = 0; j < vert; ++j) { n       int x = idx1[j]; n       if (!was[x] && d[x] < 0) { n         P[x] = -1; n         d[x] = x; n         dfs(x, x, n); n       } n     } n     if (bestw >= 0) { n       res += bestw; n       g[bestv] = bestu; n       was[bestvv] = 1; n       for (int x = P[bestu]; x >= 0;) { n         int y = P[x]; n         g[x] = y; n         x = P[y]; n       } n     } else n       break; n   } n   printf( %d n , res); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:300000000 ) n #pragma warning(disable : 4800) n using namespace std; n const double pi = 3.1415926535897932384626433832795; n template <class T> n T abs(const T &a) { n   return a >= 0 ? a : -a; n }; n template <class T> n T sqr(const T &x) { n   return x * x; n } n int n, m, k; n vector<vector<int> > g; n vector<bool> visited; n vector<int> ans; n vector<pair<int, int> > traders; n pair<int, int> erased; n bool isErased(int x, int y) { n   return pair<int, int>(x, y) == erased || pair<int, int>(y, x) == erased; n } n void dfs(int k) { n   if (visited[k]) return; n   visited[k] = true; n   for (int i = 0; i < (int((g[k]).size())); i++) { n     int to = g[k][i]; n     if (!isErased(k, to)) dfs(to); n   } n } n void work(int x, int y) { n   erased = pair<int, int>(x, y); n   visited.assign(n, false); n   dfs(x); n   for (int i = 0; i < (k); i++) n     if (visited[traders[i].first] && !visited[traders[i].second]) ans[i]++; n } n int main() { n   cin >> n >> m; n   g.resize(n); n   for (int i = 0; i < (m); i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   cin >> k; n   ans.resize(k); n   traders.resize(k); n   for (int i = 0; i < (k); i++) { n     cin >> traders[i].first >> traders[i].second; n     traders[i].first--; n     traders[i].second--; n   } n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (int((g[i]).size())); j++) work(i, g[i][j]); n   for (int i = 0; i < (k); i++) cout << ans[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0; n   bool pos = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) pos = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return pos ? x : -x; n } n const int N = 1 << 18 | 2, M = N; n const int mod = 998244353, G = 3; n int sz[N], dep[N]; n vector<int> v[N]; n inline int ksm(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = (long long)ans * a % mod; n     a = (long long)a * a % mod; n   } n   return ans; n } n struct ffter { n   int wn[N], rev[N]; n   void init(int n) { n     int t = 1, L = 0; n     while (t < n) { n       t <<= 1; n       L++; n     } n     int w = ksm(G, (mod - 1) / n); n     for (int i = wn[0] = 1; i < n; i++) { n       wn[i] = (long long)wn[i - 1] * w % mod; n       rev[i] = rev[i >> 1] >> 1 | ((i & 1) << (L - 1)); n     } n   } n   void zheng(vector<int> &a) { n     for (auto &i : a) i = (i + mod) % mod; n   } n   void dft(vector<int> &a) { n     int n = a.size(); n     zheng(a); n     for (int i = 0; i < n; i++) n       if (rev[i] < i) swap(a[rev[i]], a[i]); n     for (int i = 1; i < n; i <<= 1) { n       for (int j = 0; j < n; j += i << 1) { n         for (int k = j, l = 0; l < i; k++, l++) { n           long long t = (long long)wn[l * (n / (i << 1))] * a[k + i] % mod; n           a[k + i] = (a[k] < t) ? a[k] - t + mod : a[k] - t; n           a[k] = a[k] + t >= mod ? a[k] + t - mod : a[k] + t; n         } n       } n     } n   } n   void idft(vector<int> &a) { n     int n = a.size(); n     dft(a); n     reverse(a.begin() + 1, a.end()); n     int zs = ksm(n, mod - 2); n     for (auto &i : a) i = (long long)i * zs % mod; n   } n } T; n void FFT(vector<int> &a, vector<int> &b) { n   int n = a.size() + b.size() - 1; n   int len = 1; n   while (len < n) len <<= 1; n   T.init(len); n   a.resize(len); n   b.resize(len); n   T.dft(a); n   T.dft(b); n   for (int i = 0; i < len; i++) a[i] = (long long)a[i] * b[i] % mod; n   T.idft(a); n   a.resize(n); n } n int n, son[N]; n vector<int> tr[M], Tr[M], dp[N], f[N]; n void solve(int p); n void zy(vector<int> &a, int len) { n   a.resize(a.size() + len); n   for (int i = a.size() - 1; i >= len; i--) a[i] = a[i - len]; n   for (int i = 0; i < len; i++) a[i] = 0; n } n void bao(int p, int son) { n   for (auto i : v[p]) n     if (dep[i] > dep[p] && i != son) solve(i); n } n vector<int> get(int p, int son) { n   priority_queue<pair<int, vector<int> > > q; n   while (q.size()) q.pop(); n   for (auto i : v[p]) n     if (dep[i] > dep[p] && i != son) { n       q.push(make_pair(-dp[i].size(), dp[i])); n     } n   if (q.size() == 0) { n     vector<int> jb(1); n     jb[0] = 1; n     return jb; n   } n   while (q.size() > 1) { n     vector<int> a = q.top().second; n     q.pop(); n     vector<int> b = q.top().second; n     q.pop(); n     FFT(a, b); n     q.push(make_pair(-a.size(), a)); n   } n   vector<int> jb = q.top().second; n   q.pop(); n   return jb; n } n vector<int> operator+(vector<int> a, vector<int> b) { n   if (a.size() < b.size()) swap(a, b); n   for (unsigned i = 0; i < b.size(); i++) a[i] = (a[i] + b[i]) % mod; n   return a; n } n int TOT; n void bao(int l, int r, int nod) { n   if (l == r) { n     tr[nod] = f[l]; n     if (r == TOT) { n       Tr[nod] = tr[nod]; n       zy(Tr[nod], 1); n     } else n       Tr[nod].resize(1); n     Tr[nod][0] = 1; n     return; n   } n   int mid = (l + r) >> 1; n   bao(l, mid, nod << 1); n   bao(mid + 1, r, nod << 1 | 1); n   tr[nod] = tr[nod << 1]; n   FFT(tr[nod], tr[nod << 1 | 1]); n   FFT(Tr[nod << 1 | 1], tr[nod << 1]); n   zy(Tr[nod << 1 | 1], mid - l + 1); n   Tr[nod] = Tr[nod << 1] + Tr[nod << 1 | 1]; n } n void solve(int p) { n   int tot = 0, jb = 0; n   for (int i = p; i; i = son[i]) bao(i, son[i]); n   for (int i = p; i; i = son[i]) { n     f[++tot] = get(i, son[i]); n   } n   for (int i = 1; i <= tot; i++) jb += f[i].size(); n   TOT = tot; n   bao(1, tot, 1); n   dp[p] = Tr[1]; n } n void init(int p, int fa) { n   sz[p] = 1; n   dep[p] = dep[fa] + 1; n   for (auto i : v[p]) n     if (i != fa) { n       init(i, p); n       sz[p] += sz[i]; n       if (sz[i] > sz[son[p]]) son[p] = i; n     } n } n int main() { n   n = read(); n   int x = read() % mod; n   for (int i = 1; i < n; i++) { n     int x = read(), y = read(); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   init(1, 0); n   solve(1); n   vector<int> ans = dp[1]; n   ans[0]--; n   int dq = 1, Ans = 0; n   for (int i = 1; i <= n; i++) { n     if (i > 1) n       dq = (long long)dq * (i + x - 1) % mod * ksm(i - 1, mod - 2) % mod; n     Ans = (Ans + (long long)dq * ans[i]) % mod; n   } n   cout << (Ans + mod) % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_N = 3e5 + 7; n const long long MOD = 998244353; n long long n; n long long deg[MAX_N], fac[MAX_N]; n long long f_pow(long long base, long long b, long long mod) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * base % mod; n     base = base * base % mod; n     b >>= 1; n   } n   return res; n } n namespace NTT { n const long long MAX_N = 3e6 + 7; n const long long MOD = 998244353; n const long long G = 3; n long long rev[MAX_N]; n long long f[MAX_N], g[MAX_N]; n long long n, m; n vector<vector<long long>> p; n struct Poly { n   long long n; n   vector<long long> p; n   Poly(vector<long long> _p = {}, long long _n = 0) : n(_n) { n     n = _p.size(); n     p.resize(n); n     for (long long i = 0; i < n; ++i) p[i] = _p[i]; n   } n   bool operator<(const Poly &a) const { return n > a.n; } n }; n void ntt(long long *a, long long n, long long dft) { n   for (long long i = 0; i < n; i++) { n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   } n   for (long long i = 1; i < n; i <<= 1) { n     long long wn = f_pow(G, (MOD - 1) / (i << 1), MOD); n     if (dft < 0) wn = f_pow(wn, MOD - 2, MOD); n     for (long long j = 0; j < n; j += (i << 1)) { n       long long wnk = 1; n       for (long long k = j; k < j + i; k++) { n         long long a1 = a[k], a2 = a[k + i]; n         a[k] = (a1 + wnk * a2 % MOD) % MOD; n         a[k + i] = (a1 - wnk * a2 % MOD) % MOD; n         wnk = wnk * wn % MOD; n       } n     } n   } n   if (dft == -1) { n     long long inv = f_pow(n, MOD - 2, MOD); n     for (long long i = 0; i < n; i++) a[i] = a[i] * inv % MOD; n   } n } n vector<long long> merge(const vector<long long> &F, n                         const vector<long long> &G) { n   n = F.size() - 1, m = G.size() - 1; n   long long N = 1, p = 0; n   while (N < (m + n + 1)) N <<= 1, ++p; n   for (long long i = 0; i < N; ++i) rev[i] = f[i] = g[i] = 0; n   for (long long i = 0; i < N; ++i) n     rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (p - 1))); n   for (long long i = 0; i <= n; ++i) f[i] = F[i]; n   for (long long i = 0; i <= m; ++i) g[i] = G[i]; n   ntt(f, N, 1), ntt(g, N, 1); n   for (long long i = 0; i < N; ++i) f[i] = f[i] * g[i] % MOD; n   ntt(f, N, -1); n   vector<long long> res(m + n + 1); n   for (long long i = 0; i <= n + m; ++i) res[i] = (f[i] + MOD) % MOD; n   return res; n } n vector<long long> div_ntt(long long l, long long r) { n   if (l == r) return p[l]; n   long long mid = (l + r) >> 1; n   vector<long long> F = div_ntt(l, mid), G = div_ntt(mid + 1, r); n   return merge(F, G); n } n void merge(long long idx_f, long long idx_g) { n   vector<long long> &F = p[idx_f]; n   vector<long long> &G = p[idx_g]; n   n = F.size() - 1, m = G.size() - 1; n   long long N = 1, p = 0; n   while (N < (m + n + 1)) N <<= 1, p++; n   for (long long i = 0; i < N; ++i) rev[i] = f[i] = g[i] = 0; n   for (long long i = 0; i < N; i++) n     rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (p - 1))); n   for (long long i = 0; i <= n; i++) f[i] = F[i]; n   for (long long i = 0; i <= m; i++) g[i] = G[i]; n   ntt(f, N, 1); n   ntt(g, N, 1); n   for (long long i = 0; i < N; i++) { n     f[i] = f[i] * g[i] % MOD; n   } n   ntt(f, N, -1); n   F.resize(m + n + 1); n   for (long long i = 0; i <= (n + m); ++i) F[i] = (f[i] + MOD) % MOD; n } n vector<long long> solve() { n   priority_queue<Poly> q; n   for (long long i = 0; i < p.size(); ++i) q.push(Poly(p[i])); n   while (q.size() > 1) { n     vector<long long> F = q.top().p; n     q.pop(); n     vector<long long> G = q.top().p; n     q.pop(); n     q.push(Poly(merge(F, G))); n   } n   return q.top().p; n } n vector<long long> solve(long long l, long long r) { return div_ntt(l, r); } n }  // namespace NTT n signed main() { n   scanf( %lld , &n); n   for (long long i = 1; i < n; ++i) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     ++deg[x], ++deg[y]; n   } n   for (long long i = 2; i <= n; ++i) --deg[i]; n   for (long long i = 1; i <= n; ++i) { n     vector<long long> t; n     t.push_back(1ll); n     if (deg[i]) t.push_back(deg[i]); n     NTT::p.push_back(t); n   } n   vector<long long> F = NTT::solve(); n   long long ans = 0, flag = 1; n   fac[0] = 1; n   for (long long i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % MOD; n   for (long long i = 0; i < n && i < F.size(); ++i) { n     ans = (ans + flag * F[i] * fac[n - i] % MOD) % MOD; n     flag = -flag; n   } n   printf( %lld n , (ans + MOD) % MOD); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5, inf = 998244353; n long long n, k, i; n long long A[N], B[N]; n long long in[N], f[N]; n inline long long expo(long long n, long long k, long long p = inf) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n inline long long inv(long long a, long long p = inf) { n   return expo(a, p - 2, p); n } n vector<long long> BIT(N); n void upd(long long first, long long val) { n   for (; first <= n; first += (first & -first)) BIT[first] += val; n } n long long q(long long first) { n   long long res = 0; n   for (; first > 0; first -= (first & -first)) res += BIT[first]; n   return res; n } n long long m[N]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long i, j, first, second, ans = 0, c = 0, a; n   cin >> n; n   for (i = 1; i < n + 1; i++) { n     cin >> A[i]; n     if (A[i] != -1) { n       m[A[i]] = 1; n       ans += c - q(A[i] - 1); n       upd(A[i], 1); n       c++; n     } n   } n   vector<long long> v; n   for (i = 1; i < n + 1; i++) n     if (!m[i]) v.emplace_back(i); n   ans += 1LL * (long long)v.size() * ((long long)v.size() - 1) % inf * inv(4) % n          inf; n   long long t = 0; n   c = 0; n   for (i = 1; i < n + 1; i++) { n     if (A[i] != -1) { n       a = lower_bound(v.begin(), v.end(), A[i]) - v.begin(); n       ans += n           c * ((long long)v.size() - a) % inf * inv((long long)v.size()) % inf; n       ans += n           a * ((long long)v.size() - c) % inf * inv((long long)v.size()) % inf; n     } else n       c++; n   } n   cout << ans % inf; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100 * 1000 + 10; n int n, m, l[Maxn], ans1, ans2; n long double a, b; n pair<long double, int> arr[Maxn], brr[Maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> a >> b; n   for (int i = 0; i < n; i++) { n     cin >> arr[i].first; n     arr[i].second = i; n   } n   for (int i = 0; i < m; i++) { n     cin >> brr[i].first; n     brr[i].second = i; n   } n   for (int i = 0; i < m; i++) cin >> l[i]; n   sort(arr, arr + n); n   sort(brr, brr + m); n   int cup; n   long double Min = LLONG_MAX; n   for (int i = 0; i < m; i++) { n     long double p = (brr[i].first * a) / b; n     int ind = lower_bound(arr, arr + n, make_pair(p, -1)) - arr; n     long double c, dis; n     if (ind < n) { n       c = arr[ind].first, n       dis = sqrt(a * a + c * c) + n             sqrt((b - a) * (b - a) + (brr[i].first - c) * (brr[i].first - c)); n       cup = ind; n     } n     if (ind > 0) { n       ind--; n       c = arr[ind].first; n       if (sqrt(a * a + c * c) + sqrt((b - a) * (b - a) + n                                      (brr[i].first - c) * (brr[i].first - c)) < n           dis) { n         cup = ind; n         dis = sqrt(a * a + c * c) + n               sqrt((b - a) * (b - a) + (brr[i].first - c) * (brr[i].first - c)); n       } n     } n     if (dis + l[brr[i].second] < Min) { n       Min = dis + l[brr[i].second]; n       ans1 = arr[cup].second + 1, ans2 = brr[i].second + 1; n     } n   } n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n unsigned long long h[100010], bin[100010]; n inline unsigned long long hsh(const int l, const int r) { n   return h[r] - h[l - 1] * bin[r - l + 1]; n } n inline int find(const int l1, const int r1, const int l2, const int r2) { n   int l = 0, r = r1 - l1 + 1; n   while (l < r) { n     const int mid = l + r + 1 >> 1; n     if (hsh(l1, l1 + mid - 1) == hsh(l2, l2 + mid - 1)) n       l = mid; n     else n       r = mid - 1; n   } n   return l + 1; n } n char s[100010]; n int n, pre[100010][26], suf[100010][26], cnt[100010][26]; n unordered_map<int, int> gy[100010]; n long long det[26][100010], sum[100010]; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = bin[0] = 1; i <= n; i++) n     bin[i] = bin[i - 1] * 131, h[i] = h[i - 1] * 131 + s[i], s[i] -=  a ; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 26; j++) n       pre[i][j] = pre[i - 1][j], cnt[i][j] = cnt[i - 1][j]; n     if (i != 1) pre[i][s[i - 1]] = i - 1; n     cnt[i][s[i]]++; n   } n   for (int i = 0; i < 26; i++) suf[n + 1][i] = n + 1; n   for (int i = n; i; i--) { n     for (int j = 0; j < 26; j++) suf[i][j] = suf[i + 1][j]; n     if (i != n) suf[i][s[i + 1]] = i + 1; n   } n   long long lst = 0; n   for (int i = 1; i <= n; i++) { n     const int L = min(i - pre[i][s[i]], suf[i][s[i]] - i); n     for (int j = 0; j < L; j = j << 1 | 1) n       if ((!j || gy[i - j].count(i - 1)) && n           hsh(i - j, i - 1) == hsh(i + 1, i + j)) { n         const long long tmp = 1ll * (j << 1 | 1) * (j << 1 | 1); n         lst += tmp, gy[i - j][i + j] = 1; n         sum[i - j] -= tmp; n         sum[i] += tmp; n         sum[i + 1] -= tmp; n         sum[i + j + 1] += tmp; n       } n     for (int ch = 0; ch < 26; ch++) { n       const int nL = min(i - pre[i][ch], suf[i][ch] - i); n       for (int j = 0; j <= min(i - 1, n - i); j = j << 1 | 1) n         if ((!j || gy[i - j].count(i - 1)) && n             hsh(i - j, i - 1) == hsh(i + 1, i + j)) n           if (j >= nL && j < L) n             det[ch][i] -= 1ll * (j << 1 | 1) * (j << 1 | 1); n           else if (j >= L && j < nL) n             det[ch][i] += 1ll * (j << 1 | 1) * (j << 1 | 1); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= min(i - 1, n - i); j = j << 1 | 1) n       if ((gy[i - j].count(i - 1) || gy[i + 1].count(i + j)) && n           hsh(i - j, i - 1) != hsh(i + 1, i + j)) { n         const int p = find(i - j, i - 1, i + 1, i + j); n         if (p <= j && hsh(i - j + p, i - 1) == hsh(i + 1 + p, i + j)) { n           if (gy[i - j].count(i - 1) && n               cnt[i + j][s[i]] - cnt[i - j - 1][s[i]] - (s[i + p] == s[i]) == 1) n             det[s[i - j + p - 1]][i + p] += 1ll * (j << 1 | 1) * (j << 1 | 1); n           if (gy[i + 1].count(i + j) && cnt[i + j][s[i]] - n                                                 cnt[i - j - 1][s[i]] - n                                                 (s[i - j + p - 1] == s[i]) == n                                             1) n             det[s[i + p]][i - j + p - 1] += 1ll * (j << 1 | 1) * (j << 1 | 1); n         } n       } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < 26; j++) n       ans = max(ans, lst + det[j][i] + (j != s[i] ? sum[i] : 0)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 4e5 + 5; n struct edge { n   int v, nx; n } e[Maxn << 1]; n int n, m, ne = 1, f[Maxn], dfn[Maxn], low[Maxn], scc[Maxn]; n int rt, tot, cnt, top, st[Maxn]; n int ans, cu[Maxn], cv[Maxn], nx[Maxn], ansu[Maxn], ansv[Maxn]; n bool vis[Maxn]; n void read(int u, int v) { n   e[++ne].v = v; n   e[ne].nx = f[u]; n   f[u] = ne; n } n void Tarjan(int u, int fa) { n   dfn[u] = low[u] = ++tot; n   vis[u] = 1; n   st[++top] = u; n   for (int i = f[u]; i; i = e[i].nx) { n     int v = e[i].v; n     if (v == fa) continue; n     if (!dfn[v]) { n       Tarjan(v, u); n       low[u] = min(low[u], low[v]); n     } else if (vis[v]) n       low[u] = min(low[u], low[v]); n   } n   if (dfn[u] == low[u]) { n     cnt++; n     int x, sz = 0; n     do { n       x = st[top]; n       top--; n       sz++; n       scc[x] = cnt; n       vis[x] = 0; n     } while (x != u); n     if (sz > ans) ans = sz, rt = u; n   } n } n void dfs2(int u) { n   vis[u] = 1; n   for (int i = f[u]; i; i = e[i].nx) { n     int v = e[i].v; n     if (vis[v]) { n       ansu[i >> 1] = u; n       ansv[i >> 1] = v; n       continue; n     } n     if (low[v] != low[u]) { n       ansu[i >> 1] = v; n       ansv[i >> 1] = u; n     } else { n       ansu[i >> 1] = u; n       ansv[i >> 1] = v; n     } n     dfs2(v); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     read(u, v); n     read(v, u); n   } n   Tarjan(1, 0); n   ne = 0; n   printf( %d n , ans); n   dfs2(rt); n   for (int i = 1; i <= m; i++) printf( %d %d n , ansu[i], ansv[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Solve() { n   string s; n   cin >> s; n   int n = s.size(); n   int cnt1 = 0, cnt2 = 0; n   for (int i = 0; i < n - 1; i++) { n     string t = s.substr(i, 2); n     if (t ==  ab ) { n       cnt1++; n     } else if (t ==  ba ) { n       cnt2++; n     } n   } n   if (cnt1 == cnt2) { n     cout << s <<   n ; n     return; n   } n   if (cnt1 > cnt2) { n     for (int i = n - 1; i >= 0; i--) { n       if (s[i] ==  b ) { n         s[i] =  a ; n         break; n       } n     } n   } else { n     for (int i = n - 1; i >= 0; i--) { n       if (s[i] ==  a ) { n         s[i] =  b ; n         break; n       } n     } n   } n   cout << s <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int TC = 1; n   cin >> TC; n   while (TC--) Solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 5; n int a[MAXN], b[MAXN], c[MAXN], d[MAXN], k[MAXN], ans[MAXN]; n pair<int, pair<char, int> > p[3 * MAXN]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     p[i] = make_pair(a[i], make_pair(1, i)); n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> c[i] >> d[i] >> k[i]; n     p[n + 2 * i] = make_pair(c[i], make_pair(0, i)); n     p[n + 2 * i + 1] = make_pair(d[i], make_pair(2, i)); n   } n   sort(p, p + n + 2 * m); n   set<pair<int, int> > st; n   for (int i = 0; i < n + 2 * m; i++) { n     int id = p[i].second.second; n     if (p[i].second.first == 0) n       st.insert(make_pair(d[id], id)); n     else if (p[i].second.first == 2) n       st.erase(make_pair(d[id], id)); n     else { n       set<pair<int, int> >::iterator it = st.lower_bound(make_pair(b[id], -1)); n       if (it == st.end()) { n         cout <<  NO n ; n         return 0; n       } n       int segId = it->second; n       ans[id] = segId; n       k[segId]--; n       if (!k[segId]) st.erase(it); n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) cout << ans[i] + 1 <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l[103], a[103]; n int num[103]; n vector<int> v; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d , &l[i]); n   for (int i = 1; i < m; i++) { n     int t = l[i + 1]; n     while (t <= l[i]) t += n; n     if (a[l[i]] && a[l[i]] != t - l[i]) { n       printf( -1 n ); n       return 0; n     } n     if (!a[l[i]]) { n       a[l[i]] = t - l[i]; n       num[a[l[i]]]++; n     } n   } n   for (int i = 1; i <= n; i++) n     if (num[i] > 1) { n       printf( -1 n ); n       return 0; n     } else if (!num[i]) n       v.push_back(i); n   for (int i = 1; i <= n; i++) { n     if (!a[i]) a[i] = v.back(), v.pop_back(); n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int x[maxn], y[maxn]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> x[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> y[i]; n   } n   int u = 0, d = 0, ans = 0; n   int sumu = 0, sumd = 0; n   while (u <= n && d <= m) { n     if (sumu && sumu == sumd) { n       ans++; n       sumd = y[++d]; n       sumu = x[++u]; n     } else if (sumu > sumd) { n       sumd += y[++d]; n     } else if (sumu <= sumd) { n       sumu += x[++u]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string lower(string second) { n   for (int i = 0; i < second.size(); i++) n     if (second[i] >=  A  && second[i] <=  Z ) second[i] = second[i] -  A  +  a ; n   return second; n } n char reg(char c, char t) { n   if (c >=  A  && c <=  Z ) return t -  a  +  A ; n   return t; n } n string second, a[105], sl; n bool u[105]; n int n; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] = lower(a[i]); n   } n   char let; n   cin >> second >> let; n   sl = lower(second); n   for (int i = 0; i < second.size(); i++) n     for (int j = 0; j < n; j++) n       if (i + a[j].size() <= second.size() && n           sl.substr(i, a[j].size()) == a[j]) { n         for (int k = i; k < i + a[j].size(); k++) u[k] = true; n       } n   for (int i = 0; i < second.size(); i++) n     if (u[i]) { n       if (sl[i] == let) { n         if (sl[i] ==  a ) n           second[i] = reg(second[i],  b ); n         else n           second[i] = reg(second[i],  a ); n       } else n         second[i] = reg(second[i], let); n     } n   cout << second; n   return 0; n } n