#include <bits/stdc++.h> n using namespace std; n const int maxn = 100050, maxm = 5050; n int n, m, K, p, h[maxn], a[maxn]; n long long hh[maxn], ans; n long long tim[maxn]; n int cnt[maxm]; n bool judg(long long x) { n   int i, j; n   long long tis = 0; n   for (i = 0; i < m; i++) cnt[i] = 0; n   for (i = 0; i < n; i++) tis += tim[i] = max(0LL, (hh[i] - x - 1 + p) / p); n   if (tis > K * m) return 0; n   for (i = 0; i < n; i++) { n     int now = 0, h0 = h[i]; n     for (j = 0; j < tim[i]; j++) { n       long long t = h0 + 1LL * (m - now) * a[i] - 1LL * (tim[i] - j - 1) * p - n                     x, n                 tn; n       if (h0 < t) n         tn = now + (t - h0 - 1) / a[i] + 1; n       else n         tn = now; n       if (tn >= m) return 0; n       h0 = max(0LL, h0 + (tn - now) * a[i] - p); n       cnt[now = tn]++; n     } n   } n   for (i = m - 1; i >= 0; i--) { n     cnt[i] += cnt[i + 1]; n     if (cnt[i] > (m - i) * K) return 0; n   } n   return 1; n } n int main() { n   int i, j; n   long long l = 0, r = 0, mid; n   scanf( %d%d%d%d , &n, &m, &K, &p); n   for (i = 0; i < n; i++) n     scanf( %d%d , &h[i], &a[i]), r = max(r, hh[i] = h[i] + 1LL * a[i] * m); n   while (l < r) { n     mid = (l + r) >> 1; n     if (judg(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   ans = l; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int n, num[100], cnt[N]; n long long a[N]; 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++) { n     long long temp = a[i]; n     while (temp && temp % 2 == 0) { n       cnt[i]++; n       temp /= 2; n     } n     num[cnt[i]]++; n   } n   int id = 0; n   for (int i = 1; i <= 63; i++) n     if (num[i] > num[id]) id = i; n   cout << n - num[id] << endl; n   for (int i = 1; i <= n; i++) n     if (cnt[i] != id) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000000 + 2; n const long long MOD = 1e9 + 7; n int N, p[MAXN]; n long long s[MAXN], c[MAXN], f[MAXN], Ans; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n int main() { n   cin >> N; n   for (int i = 1; i <= N; i++) scanf( %lld , s + i); n   for (int i = 2; i <= N; i++) scanf( %d , p + i); n   for (int i = N; i; i--) s[p[i]] += s[i]; n   for (int i = N; i; i--) { n     s[i] = s[1] / gcd(s[1], s[i]); n     if (s[i] <= N) c[s[i]]++; n   } n   for (int i = N; i; i--) n     for (int j = 2 * i; j <= N; j += i) c[j] = (c[j] + c[i]) % MOD; n   f[1] = 1; n   for (int i = 1; i <= N; i++) n     if (c[i] >= i) n       for (int j = 2 * i; j <= N; j += i) f[j] = (f[j] + f[i]) % MOD; n   for (int i = 1; i <= N; i++) n     if (c[i] >= i) Ans = (Ans + f[i]) % MOD; n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   int l = 0; n   int r = n - 1; n   for (int i = 0; i < n; i++) { n     if (i % 2) { n       cout << a[r--] <<    ; n     } else { n       cout << a[l++] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[125]; n char sa[5005]; n int main() { n   bool ok, whh; n   int lens, lent; n   string s, t, my; n   cin >> s >> t; n   memset(cnt, 0, sizeof cnt); n   lens = s.length(); n   for (int i = 0; i < lens; i++) cnt[s[i]]++; n   ok = true; n   whh = false; n   my = s; n   lent = t.length(); n   if (lens <= lent) { n     for (int i = 0; i < lens; i++) { n       char c = t[i]; n       if (whh) { n         c =  a ; n         while (c <=  z ) n           if (cnt[c]) { n             cnt[c]--; n             break; n           } else n             c++; n         my[i] = c; n         continue; n       } n       if (!ok) { n         c =  z ; n         while (c >=  a ) n           if (cnt[c]) { n             cnt[c]--; n             break; n           } else n             c--; n         my[i] = c; n         continue; n       } n       while (c <=  z ) n         if (cnt[c]) { n           cnt[c]--; n           break; n         } else n           c++; n       if (c == t[i]) n         my[i] = c; n       else if (c <=  z ) { n         my[i] = c; n         whh = true; n       } else { n         ok = false; n         c =  z ; n         while (c >=  a ) n           if (cnt[c]) { n             cnt[c]--; n             break; n           } else n             c--; n         my[i] = c; n         continue; n       } n     } n     if (my == t) { n       strcpy(sa, my.c_str()); n       if (!next_permutation(sa, sa + lens)) n         ok = false; n       else { n         string tmp(sa); n         my = tmp; n       } n     } else if (my < t) { n       strcpy(sa, my.c_str()); n       while (next_permutation(sa, sa + lens)) { n         string tmp(sa); n         if (tmp > t) { n           my = tmp; n           break; n         } n       } n       if (my <= t) n         ok = false; n       else n         ok = true; n     } n   } else { n     for (int i = 0; i < lens; i++) { n       char c = t[i]; n       if (whh) { n         c =  a ; n         while (c <=  z ) n           if (cnt[c]) { n             cnt[c]--; n             break; n           } else n             c++; n         my[i] = c; n         continue; n       } n       if (!ok) { n         c =  z ; n         while (c >=  a ) n           if (cnt[c]) { n             cnt[c]--; n             break; n           } else n             c--; n         my[i] = c; n         continue; n       } n       while (c <=  z ) n         if (cnt[c]) { n           cnt[c]--; n           break; n         } else n           c++; n       if (c == t[i]) n         my[i] = c; n       else if (c <=  z ) { n         my[i] = c; n         whh = true; n       } else { n         ok = false; n         c =  z ; n         while (c >=  a ) n           if (cnt[c]) { n             cnt[c]--; n             break; n           } else n             c--; n         my[i] = c; n         continue; n       } n     } n     if (my == t) { n       strcpy(sa, my.c_str()); n       if (!next_permutation(sa, sa + lens)) n         ok = false; n       else { n         string tmp(sa); n         my = tmp; n       } n     } else if (my < t) { n       strcpy(sa, my.c_str()); n       while (next_permutation(sa, sa + lens)) { n         string tmp(sa); n         if (tmp > t) { n           my = tmp; n           break; n         } n       } n       if (my <= t) n         ok = false; n       else n         ok = true; n     } n   } n   if (ok) n     cout << my << endl; n   else n     cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long qpow(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n % 2) { n       res = (res * a) % 100000007; n       n--; n     } else { n       n = n / 2; n       a = (a * a) % 100000007; n     } n   } n   return res; n } n void solve() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, q; n   cin >> n >> q; n   vector<long long> arr(n + 1); n   for (long long i = 1; i <= n; i++) cin >> arr[i]; n   vector<long long> H(n + 1); n   for (long long i = 0; i < q; i++) { n     long long l, r; n     cin >> l >> r; n     H[l]++; n     if (r + 1 <= n) H[r + 1]--; n   } n   for (long long i = 1; i <= n; i++) H[i] += H[i - 1]; n   sort(H.begin() + 1, H.end()); n   sort(arr.begin() + 1, arr.end()); n   long long sum = 0; n   for (long long i = 1; i <= n; i++) sum += H[i] * arr[i]; n   cout << sum <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string A[15]; n long long nr[100]; n int N, K; n long long mn = 1000000000; n int main() { n   scanf( %d %d n , &N, &K); n   for (int i = 1; i <= N; i++) cin >> A[i]; n   vector<int> v; n   for (int i = 1; i <= K; i++) v.push_back(i); n   do { n     for (int i = 1; i <= N; i++) { n       nr[i] = 0; n       for (int j = 0; j < K; j++) nr[i] = nr[i] * 10 + (A[i][v[j] - 1] -  0 ); n     } n     sort(nr + 1, nr + N + 1); n     mn = min(mn, nr[N] - nr[1]); n   } while (next_permutation(v.begin(), v.end())); n   cout << mn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, f = 0, s = 0; n   char pass[3], x[2]; n   cin >> pass[0] >> pass[1]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[0] >> x[1]; n     if (x[0] == pass[0] && x[1] == pass[1]) { n       cout <<  YES ; n       return 0; n     } n     if (x[1] == pass[0]) { n       f = 1; n     } n     if (x[0] == pass[1]) { n       s = 1; n     } n   } n   if (s && f) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long a, b, x, y, n; n     cin >> a >> b >> x >> y >> n; n     long long p = a - x, q = b - y; n     long long pp = min(p, n), qq = min(q, n); n     long long aa = a - pp, bb = b - qq; n     long long tt = a + b - n; n     if (p + q <= n) n       cout << x * y << endl; n     else { n       if (aa < bb) n         cout << aa * (tt - aa) << endl; n       else n         cout << bb * (tt - bb) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110; n int n; n char s[N][30]; n char ss[30]; n int p; n int len[N]; n int in[30]; n int nxt[30]; n int bf[30][30], ind[30]; n void go(int i) { n   ss[p++] = char(i +  a ); n   if (nxt[i] >= 0) go(nxt[i]); n } n int main() { n   scanf( %d , &n); n   memset(nxt, -1, sizeof(nxt)); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s[i]); n     len[i] = strlen(s[i]); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < len[i]; ++j) { n       in[s[i][j] -  a ] = 1; n       for (int k = j + 1; k < len[i]; ++k) { n         bf[s[i][j] -  a ][s[i][k] -  a ] = 1; n       } n     } n     for (int j = 0; j < len[i] - 1; ++j) { n       nxt[s[i][j] -  a ] = s[i][j + 1] -  a ; n     } n   } n   for (int i = 0; i < 26; ++i) { n     for (int j = 0; j < 26; ++j) { n       if (bf[j][i]) { n         ++ind[i]; n       } n     } n   } n   for (int i = 0; i < 26; ++i) { n     if (!ind[i] && in[i]) { n       go(i); n     } n   } n   puts(ss); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string st[30] = { zero ,     one ,      two ,        three ,     four , n                   five ,     six ,      seven ,      eight ,     nine , n                   ten ,      eleven ,   twelve ,     thirteen ,  fourteen , n                   fifteen ,  sixteen ,  seventeen ,  eighteen ,  nineteen , n                   twenty ,   thirty ,   forty ,      fifty ,     sixty , n                   seventy ,  eighty ,   ninety }; n int main() { n   int n; n   while (cin >> n) { n     if (n <= 20) n       cout << st[n]; n     else if (n % 10 == 0) n       cout << st[20 + n / 10 - 2]; n     else if (n < 30) n       cout << st[20] +  -  + st[n % 10]; n     else if (n < 40) n       cout << st[21] +  -  + st[n % 10]; n     else if (n < 50) n       cout << st[22] +  -  + st[n % 10]; n     else if (n < 60) n       cout << st[23] +  -  + st[n % 10]; n     else n       cout << st[20 + n / 10 - 2] +  -  + st[n % 10]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dpmax[200005][20]; n long long int dpmin[200005][20]; n long long int a[200005], b[200005]; n int n; n void ST() { n   for (int i = 1; i <= n; i++) { n     dpmax[i][0] = a[i]; n     dpmin[i][0] = b[i]; n   } n   for (int j = 1; (1 << j) <= n; j++) { n     for (int i = 1; i + (1 << j) - 1 <= n; i++) { n       dpmax[i][j] = dpmax[i][j - 1] > dpmax[i + (1 << (j - 1))][j - 1] n                         ? dpmax[i][j - 1] n                         : dpmax[i + (1 << (j - 1))][j - 1]; n       dpmin[i][j] = dpmin[i][j - 1] < dpmin[i + (1 << (j - 1))][j - 1] n                         ? dpmin[i][j - 1] n                         : dpmin[i + (1 << (j - 1))][j - 1]; n     } n   } n } n long long int RMQmax(int l, int r) { n   int k = 0; n   while ((1 << (k + 1)) <= r - l + 1) k++; n   return dpmax[l][k] > dpmax[r - (1 << k) + 1][k] ? dpmax[l][k] n                                                   : dpmax[r - (1 << k) + 1][k]; n } n long long int RMQmin(int l, int r) { n   int k = 0; n   while ((1 << (k + 1)) <= r - l + 1) k++; n   return dpmin[l][k] < dpmin[r - (1 << k) + 1][k] ? dpmin[l][k] n                                                   : dpmin[r - (1 << k) + 1][k]; n } 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   ST(); n   long long int ans = 0; n   for (int i = 1; i <= n; i++) { n     int l = i, r = n, mid, flag = 0; n     long long int x, y; n     long long int last = 0, first = 0; n     while (l <= r) { n       mid = (l + r) / 2; n       x = RMQmax(i, mid); n       y = RMQmin(i, mid); n       if (x == y) { n         flag = 1; n         last = mid; n       } n       if (x > y) r = mid - 1; n       if (x < y || x == y) l = mid + 1; n     } n     if (flag) { n       l = i; n       r = n; n       while (l <= r) { n         mid = (l + r) / 2; n         x = RMQmax(i, mid); n         y = RMQmin(i, mid); n         if (x < y) { n           l = mid + 1; n         } else { n           r = mid - 1; n           first = mid; n         } n       } n       ans += (last - first + 1); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e6 + 20; n const int maxm = 1e5 + 10; n const long long int mod = 998244353; n const long long int INF = 1e18 + 100; n const int inf = 0x3f3f3f3f; n const double pi = acos(-1.0); n const double eps = 1e-8; n using namespace std; n int a[50][50]; n int num[200], c[200]; n int main() { n   int _; n   for (scanf( %d , &_); _; _--) { n     memset(num, 0, sizeof num); n     memset(c, 0, sizeof c); n     memset(a, 0, sizeof a); n     int n, m; n     scanf( %d %d , &n, &m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n     if (n == 2 && m == 2) { n       if (a[0][0] != a[1][1]) n         puts( 1 ); n       else n         puts( 0 ); n     } else { n       n--; n       m--; n       for (int i = 0; i <= n; i++) { n         for (int j = 0; j <= m; j++) { n           c[i + j]++; n           num[i + j] += a[i][j]; n         } n       } n       int ans = 0; n       if ((n + m) % 2 == 1) { n         for (int i = 0; i < (n + m) / 2; i++) { n           ans += min(num[i] + num[n + m - i], n                      (c[i] + c[n + m - i]) - (num[i] + num[n + m - i])); n         } n         int i = (n + m) / 2; n         ans += min(num[i] + num[n + m - i], n                    (c[i] + c[n + m - i]) - (num[i] + num[n + m - i])); n       } else { n         for (int i = 0; i < (n + m) / 2; i++) { n           ans += min(num[i] + num[n + m - i], n                      (c[i] + c[n + m - i]) - (num[i] + num[n + m - i])); n         } n       } n       printf( %d n , ans); n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n const int MOD = 998244353; n int main() { n   int n; n   while (cin >> n) { n     int p; n     long long x = 0, y = 0; n     for (int i = 1; i <= n; i++) { n       cin >> p; n       x += p / 2; n       y += p / 2; n       if (p % 2) { n         if (i % 2) n           x++; n         else n           y++; n       } n     } n     printf( %I64d n , min(x, y)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int i; n   char n[100010], m; n   stack<char> s; n   scanf( %s , n); n   for (i = 0; n[i] != 0; i++) { n     if (s.empty()) { n       s.push(n[i]); n     } else { n       m = s.top(); n       if (m == n[i]) n         s.pop(); n       else n         s.push(n[i]); n     } n   } n   if (s.empty()) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n int NEXT_FREE_ID = 0; n std::unordered_map<std::string, int> stringId; n namespace persistentid { n int NEXT_FREE_INDEX = 1; n int s[10000000], L[10000000], R[10000000]; n int root[100005]; n int n = 1 << 30; n int upd(int p, int v, int id, int l = 0, int r = n) { n   int ID = NEXT_FREE_INDEX++; n   s[ID] = s[id] + v; n   if (r - l < 2) return ID; n   int mid = (l + r) / 2; n   L[ID] = L[id], R[ID] = R[id]; n   if (p < mid) n     L[ID] = upd(p, v, L[ID], l, mid); n   else n     R[ID] = upd(p, v, R[ID], mid, r); n   return ID; n } n int f(int p, int id, int l = 0, int r = n) { n   if (r - l < 2) return s[id]; n   int mid = (l + r) / 2; n   if (p < mid) n     return f(p, L[id], l, mid); n   else n     return f(p, R[id], mid, r); n } n }  // namespace persistentid n namespace persistentcount { n int NEXT_FREE_INDEX = 1; n int s[10000000], L[10000000], R[10000000]; n int root[100005]; n int n = 1 << 30; n int upd(int p, int v, int id, int l = 0, int r = n) { n   int ID = NEXT_FREE_INDEX++; n   s[ID] = s[id] + v; n   if (r - l < 2) return ID; n   int mid = (l + r) / 2; n   L[ID] = L[id], R[ID] = R[id]; n   if (p < mid) n     L[ID] = upd(p, v, L[ID], l, mid); n   else n     R[ID] = upd(p, v, R[ID], mid, r); n   return ID; n } n int sum(int x, int y, int id, int l = 0, int r = n) { n   if (x >= r || l >= y) return 0; n   if (x <= l && r <= y) return s[id]; n   int mid = (l + r) / 2; n   return sum(x, y, L[id], l, mid) + sum(x, y, R[id], mid, r); n } n }  // namespace persistentcount n int main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   std::cout.tie(0); n   int Q; n   std::cin >> Q; n   for (int q = 1; q <= Q; q++) { n     std::string type; n     std::cin >> type; n     if (type ==  set ) { n       std::string a; n       std::cin >> a; n       int x; n       std::cin >> x; n       if (stringId.count(a) == 0) { n         stringId[a] = NEXT_FREE_ID++; n         persistentid::root[q] = persistentid::NEXT_FREE_INDEX; n         persistentid::upd(stringId[a], x, persistentid::root[q - 1]); n         persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX; n         persistentcount::upd(x, 1, persistentcount::root[q - 1]); n       } else { n         persistentid::root[q] = persistentid::NEXT_FREE_INDEX; n         int val = persistentid::f(stringId[a], persistentid::root[q - 1]); n         persistentid::upd(stringId[a], -val + x, persistentid::root[q - 1]); n         int temp = persistentcount::NEXT_FREE_INDEX; n         persistentcount::upd(val, -1, persistentcount::root[q - 1]); n         persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX; n         persistentcount::upd(x, 1, temp); n       } n     } else if (type ==  remove ) { n       std::string a; n       std::cin >> a; n       if (stringId.count(a) != 0 && n           persistentid::f(stringId[a], persistentid::root[q - 1]) != 0) { n         int val = persistentid::f(stringId[a], persistentid::root[q - 1]); n         persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX; n         persistentcount::upd(val, -1, persistentcount::root[q - 1]); n         persistentid::root[q] = persistentid::NEXT_FREE_INDEX; n         persistentid::upd(stringId[a], -val, persistentid::root[q - 1]); n       } else { n         persistentcount::root[q] = persistentcount::root[q - 1]; n         persistentid::root[q] = persistentid::root[q - 1]; n       } n     } else if (type ==  query ) { n       std::string a; n       std::cin >> a; n       persistentcount::root[q] = persistentcount::root[q - 1]; n       persistentid::root[q] = persistentid::root[q - 1]; n       if (stringId.count(a) != 0 && n           persistentid::f(stringId[a], persistentid::root[q - 1]) != 0) { n         std::cout << persistentcount::sum( n                          1, n                          persistentid::f(stringId[a], n                                          persistentid::root[q - 1]), n                          persistentcount::root[q - 1]) n                   <<   n  n                   << std::flush; n       } else { n         std::cout <<  -1 n  << std::flush; n       } n     } else { n       int d; n       std::cin >> d; n       d = q - 1 - d; n       persistentcount::root[q] = persistentcount::root[d]; n       persistentid::root[q] = persistentid::root[d]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, pair<int, int> > > E(m); n   { n     int cnt = 0; n     for (int i = 0; i < m; i++) { n       cin >> E[i].first >> E[i].second.first; n       E[i].second.second = i; n       if (E[i].second.first == 1) cnt++; n       E[i].second.first = 1 - E[i].second.first; n     } n     if (cnt != n - 1) { n       cout << -1 << endl; n       return 0; n     } n   } n   sort(E.begin(), E.end()); n   vector<pair<int, int> > res(m); n   queue<pair<int, int> > que; n   int latest = 1; n   for (int i = 0; i < m; i++) { n     if (E[i].second.first == 0) { n       res[E[i].second.second] = make_pair(latest, latest + 1); n       for (int u = 1; u < latest && que.size() < m; u++) n         que.push(make_pair(u, latest + 1)); n       latest++; n     } else { n       if (que.empty()) { n         cout << -1 << endl; n         return 0; n       } else { n         auto v = que.front(); n         que.pop(); n         res[E[i].second.second] = v; n       } n     } n   } n   for (int i = 0; i < m; i++) n     cout << res[i].first <<     << res[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n const int M = 1e9 + 100; n long long a[N]; n int main() { n   int n, m; n   long long x, y; n   while (~scanf( %d %d , &n, &m)) { n     x = -M; n     y = M; n     for (int i = 0; i < n; i++) { n       scanf( %lld , &a[i]); n       x = max(a[i], x); n       y = min(a[i], y); n     } n     if (m == 1) n       printf( %lld n , y); n     else if (m >= 3) n       printf( %lld n , x); n     else n       printf( %lld n , max(a[0], a[n - 1])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     vector<int> deg(n); n     vector<vector<int>> adj(n); n     for (int i = 0, u, v; i < m; ++i) { n       cin >> u >> v; n       --u, --v; n       adj[u].push_back(v); n       adj[v].push_back(u); n       ++deg[u], ++deg[v]; n     } n     deque<int> q; n     for (int i = 0; i < n; ++i) { n       sort(adj[i].begin(), adj[i].end()); n       if (deg[i] == k - 1) q.push_back(i); n       if (deg[i] < k - 1) q.push_front(i); n     } n     vector<int> ans; n     vector<bool> mp(n); n     while (!q.empty()) { n       int u = q.front(); n       q.pop_front(); n       if (deg[u] == -1) continue; n       if (deg[u] == k - 1) { n         vector<int> &c = ans; n         for (auto v : adj[u]) n           if (deg[v] != -1) { n             c.push_back(v); n             mp[v] = true; n           } n         c.insert(upper_bound(c.begin(), c.end(), u), u); n         mp[u] = true; n         bool ok = true; n         for (auto &i : c) { n           if (adj[i].size() <= 3 * c.size()) { n             int cnt = 0; n             for (auto j : adj[i]) cnt += mp[j]; n             if (cnt != k - 1) { n               ok = false; n               break; n             } n           } else { n             for (size_t j = &i - &c[0]; ++j < c.size();) { n               auto it = lower_bound(adj[i].begin(), adj[i].end(), c[j]); n               if (it == adj[i].end() || *it != c[j]) { n                 ok = false; n                 break; n               } n             } n           } n         } n         if (ok) break; n         for (auto i : c) mp[i] = false; n         c.clear(); n       } n       deg[u] = -1; n       for (auto v : adj[u]) n         if (deg[v] != -1) { n           if (deg[v] == k - 1) q.push_front(v); n           if (deg[v] == k) q.push_back(v); n           --deg[v]; n         } n     } n     if (ans.empty()) { n       for (int i = 0; i < n; ++i) n         if (deg[i] != -1) ans.push_back(i); n       if (!ans.empty()) { n         cout <<  1   << ans.size() <<   n ; n         for (auto &i : ans) cout << i + 1 <<    n [&i == &ans.back()]; n       } else n         cout <<  -1 n ; n     } else { n       cout <<  2 n ; n       for (auto &i : ans) cout << i + 1 <<    n [&i == &ans.back()]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mas[1001][1001]; n char chars[1000][1001]; n int n, m; n int st = 0; n vector<int> woi; n int p = 0; n void dfs(int x, int y) { n   st++; n   mas[x][y] = p; n   if (x != 0) { n     if (mas[x - 1][y] == 0 && chars[x - 1][y] ==  . ) { n       dfs(x - 1, y); n     } n   } n   if (y != 0) { n     if (mas[x][y - 1] == 0 && chars[x][y - 1] ==  . ) { n       dfs(x, y - 1); n     } n   } n   if (x + 1 != n) { n     if (mas[x + 1][y] == 0 && chars[x + 1][y] ==  . ) { n       dfs(x + 1, y); n     } n   } n   if (y + 1 != m + 1) { n     if (mas[x][y + 1] == 0 && chars[x][y + 1] ==  . ) { n       dfs(x, y + 1); n     } n   } n } n void startdfs(int x, int y) { n   st = 0; n   p++; n   dfs(x, y); n   woi.push_back(st); n } n int calculate(int x, int y) { n   int ans = 1; n   set<int> f; n   if (x != 0) { n     f.emplace(mas[x - 1][y]); n   } n   if (y != 0) { n     f.emplace(mas[x][y - 1]); n   } n   if (x != n) { n     f.emplace(mas[x + 1][y]); n   } n   if (y != m + 1) { n     f.emplace(mas[x][y + 1]); n   } n   for (auto w : f) { n     ans += woi[w]; n   } n   return ans; n } n int main() { n   char ch; n   woi.push_back(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m + 1; j++) { n       scanf( %c , &ch); n       chars[i][j] = ch; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m + 1; j++) { n       if (chars[i][j] ==  .  && mas[i][j] == 0) { n         startdfs(i, j); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m + 1; j++) { n       if (chars[i][j] ==  * ) { n         int c = calculate(i, j) % 10; n         chars[i][j] = c +  0 ; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 1; j < m + 1; j++) { n       printf( %c , chars[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, A[100000], count0 = 0, max = 0; n   scanf( %i , &n); n   for (int i = 0; i < n; i++) { n     scanf( %i , A + i); n     if (A[i] < 0) A[i] = -A[i] - 1; n     if (!A[i]) count0++; n     if (A[i] > max) max = A[i]; n   } n   if (count0 == n) { n     for (int i = 1; i < n; i++) printf( -1  ); n     return 0 * printf(n & 1 ?  0  :  -1 ); n   } n   for (int i = 0; i < n; i++) { n     if (A[i]) { n       if (A[i] == max) { n         if (n & 1) n           printf( %i  , max); n         else n           printf( %i  , -A[i] - 1); n         max = 0; n       } else n         printf( %i  , -A[i] - 1); n     } else n       printf( -1  ); n   } n } n


#include <bits/stdc++.h> n const int S = 1 << 21; n char ibuf[S], *iS, *iT, obuf[S], *oS = obuf, *oT = oS + S - 1; n struct Flusher_ { n   ~Flusher_() { (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()); } n } flusher_; n template <class T> n inline void read(T &x) { n   x = 0; n   register char c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), n                                  (iS == iT ? EOF : *iS++)) n                               : *iS++); n   register bool f = 0; n   while (!isdigit(c)) n     f ^= c ==  - , c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), n                                     (iS == iT ? EOF : *iS++)) n                                  : *iS++); n   while (isdigit(c)) n     x = x * 10 + c -  0 , n     c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), n                      (iS == iT ? EOF : *iS++)) n                   : *iS++); n   if (f) x = -x; n } n template <class T> n inline void readchar(T &x) { n   x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), n                    (iS == iT ? EOF : *iS++)) n                 : *iS++); n   while (!isalpha(x)) n     x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), n                      (iS == iT ? EOF : *iS++)) n                   : *iS++); n } n template <class T> n inline void print(T x) { n   if (x < 0) n     (*oS++ = ( - ), n      oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) n               : void()), n         x = -x; n   if (x > 9) print(x / 10); n   (*oS++ = (x % 10 +  0 ), n    oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) : void()); n } n template <class T> n inline void print(T x, char c) { n   print(x), (*oS++ = (c), n              oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) n                       : void()); n } n const int N = 2e5 + 10, sqn = 1000; n int n, m, k, ans, cnt, a[N], b[N], s[N]; n struct sequence { n   int siz, ans; n   std::vector<int> vec, bln, tag; n   std::vector<std::array<short, (1 << 14)>> val; n   inline int left(int k) { return k * sqn; } n   inline int right(int k) { n     return std::min((int)vec.size() - 1, (k + 1) * sqn - 1); n   } n   void modify(int k, int x) { n     ::ans += ans, cnt -= (vec.back() ^ tag.back()) != 0; n     for (int i = bln[k] + 1; i <= bln.back(); i++) { n       ans -= val[i][tag[i]]; n       tag[i] ^= x; n       ans += val[i][tag[i]]; n     } n     if (bln.back()) n       for (int i = left(bln[k]); i <= right(bln[k]); i++) val[bln[k]][vec[i]]--; n     for (int i = k; i <= right(bln[k]); i++) { n       ans -= vec[i] == tag[bln[k]]; n       vec[i] ^= x; n       ans += vec[i] == tag[bln[k]]; n     } n     if (bln.back()) n       for (int i = left(bln[k]); i <= right(bln[k]); i++) val[bln[k]][vec[i]]++; n     ::ans -= ans, cnt += (vec.back() ^ tag.back()) != 0; n   } n   void init() { n     for (int i = 1; i < vec.size(); i++) vec[i] ^= vec[i - 1]; n     for (int i = 0; i < vec.size(); i++) ans += vec[i] == 0; n     for (int i = 0; i < vec.size(); i++) bln.push_back(i / sqn); n     tag.resize(bln.back() + 1); n     if (bln.back()) { n       val.resize(bln.back() + 1); n       for (int k = 0; k <= bln.back(); k++) { n         for (int i = left(k); i <= right(k); i++) val[k][vec[i]]++; n       } n     } n     ::ans += vec.size() - ans, cnt += (vec.back() ^ tag.back()) != 0; n   } n } f[N]; n void output() { print(cnt ? -1 : ans,   n ); } n int main() { n   read(n), read(k), read(m); n   for (int i = 1; i <= n; i++) read(a[i]); n   for (int i = 1; i <= n; i++) read(b[i]); n   for (int i = 1; i <= n + 1; i++) s[i] = a[i - 1] ^ b[i - 1] ^ a[i] ^ b[i]; n   for (int i = 1; i <= n + 1; i++) f[(i - 1) % k].vec.push_back(s[i]); n   for (int i = 0; i < k; i++) f[i].init(); n   output(); n   for (int c, p, x, v, i = 1; i <= m; i++) { n     readchar(c), read(p), read(x); n     if (c ==  a ) v = a[p] ^ x, a[p] = x; n     if (c ==  b ) v = b[p] ^ x, b[p] = x; n     f[p % k].modify(p / k, v); n     f[(p - 1) % k].modify((p - 1) / k, v); n     output(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010, LOG = 20; n int n, m, k, u, v, x, y, t, a, b, ans; n bool dead[MAXN]; n string A; n vector<vector<int>> out; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> A; n   n = A.size(); n   while (1) { n     int i = 0, j = n - 1; n     vector<int> v1, v2; n     while (i < j) { n       while (i < j && (A[i] ==  )  || dead[i])) i++; n       while (i < j && (A[j] ==  (  || dead[j])) j--; n       if (i >= j || A[i] ==  )  || A[j] ==  ( ) break; n       v1.push_back(i++); n       v2.push_back(j--); n     } n     if (v1.empty()) break; n     reverse(v2.begin(), v2.end()); n     for (int i : v2) v1.push_back(i); n     for (int i : v1) dead[i] = 1; n     out.push_back(v1); n   } n   cout << out.size() <<   n ; n   for (auto vec : out) { n     cout << vec.size() <<   n ; n     for (int i : vec) cout << i + 1 <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   register T c = getchar(); n   x = 0; n   int t = 0; n   if (c ==  - ) t = 1, c = getchar(); n   for (; (c < 48 || c > 57); c = getchar()) n     ; n   for (; c > 47 && c < 58; c = getchar()) { n     x = (x << 1) + (x << 3) + c - 48; n   } n   if (t) x = -x; n } n int main() { n   int n, Max = 0; n   read(n); n   string str; n   cin >> str; n   bool visited[250] = {0}; n   for (int i = 0; i < n; i++) { n     if (str[i] >=  a  && !visited[i]) { n       set<char> s; n       visited[i] = 1; n       s.insert(str[i]); n       for (int j = i + 1; j < n; j++) { n         if (str[j] <=  Z ) break; n         visited[j] = 1; n         s.insert(str[j]); n       } n       if (Max < s.size()) Max = s.size(); n     } n   } n   cout << Max; n } n


#include <bits/stdc++.h> n const int INF = 1e9; n using namespace std; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n bool vis[1005][1005], cvis[50]; n vector<pair<int, int> > cc[50]; n int m, n, k, a[1005][1005], D[45][1005][1005]; n void DFS(int c) { n   memset(vis, 0, sizeof(vis)); n   memset(cvis, 0, sizeof(cvis)); n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) D[c][i][j] = 4000; n   queue<pair<int, int> > Q; n   for (int i = 0; i < cc[c].size(); i++) { n     Q.push(cc[c][i]); n     vis[cc[c][i].first][cc[c][i].second] = 1; n     D[c][cc[c][i].first][cc[c][i].second] = 0; n   } n   cvis[c] = 1; n   int first, second, xx, yy; n   while (!Q.empty()) { n     first = Q.front().first; n     second = Q.front().second; n     Q.pop(); n     for (int j = 0; j < 4; j++) { n       xx = first + dx[j]; n       yy = second + dy[j]; n       if (xx < 1 || yy < 1 || xx > m || yy > n) continue; n       if (!vis[xx][yy]) { n         vis[xx][yy] = 1; n         D[c][xx][yy] = D[c][first][second] + 1; n         Q.push(pair<int, int>(xx, yy)); n       } n     } n     if (!cvis[a[first][second]]) { n       cvis[a[first][second]] = 1; n       for (int i = 0; i < cc[a[first][second]].size(); i++) { n         xx = cc[a[first][second]][i].first; n         yy = cc[a[first][second]][i].second; n         if (!vis[xx][yy]) { n           vis[xx][yy] = 1; n           D[c][xx][yy] = D[c][first][second] + 1; n           Q.push(pair<int, int>(xx, yy)); n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> m >> n >> k; n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) { n       cin >> a[i][j]; n       cc[a[i][j]].push_back(pair<int, int>(i, j)); n     } n   for (int i = 1; i <= k; i++) DFS(i); n   int ans = INF, q, r1, r2, c1, c2; n   cin >> q; n   while (q--) { n     cin >> r1 >> c1 >> r2 >> c2; n     ans = abs(r2 - r1) + abs(c1 - c2); n     for (int i = 1; i <= k; i++) { n       ans = min(ans, D[i][r1][c1] + D[i][r2][c2] + 1); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n char in[4][4]; n int oc = 0, xc = 0; n int whowin() { n   int i, j; n   int ret = 0; n   for (i = 0; i < 3; i++) { n     for (j = 0; j < 3; j++) { n       if (in[i][j] != in[i][0]) break; n     } n     if (j == 3 && in[i][0] !=  . ) { n       if (in[i][0] ==  0 ) n         ret |= 2; n       else n         ret |= 1; n     } n     for (j = 0; j < 3; j++) { n       if (in[j][i] != in[0][i]) break; n     } n     if (j == 3 && in[0][i] !=  . ) { n       if (in[0][i] ==  0 ) n         ret |= 2; n       else n         ret |= 1; n     } n   } n   for (j = 0; j < 3; j++) { n     if (in[j][j] != in[0][0]) break; n   } n   if (j == 3 && in[0][0] !=  . ) { n     if (in[0][0] ==  0 ) n       ret |= 2; n     else n       ret |= 1; n   } n   for (j = 0; j < 3; j++) { n     if (in[j][2 - j] != in[0][2]) break; n   } n   if (j == 3 && in[0][2] !=  . ) { n     if (in[0][2] ==  0 ) n       ret |= 2; n     else n       ret |= 1; n   } n   return ret; n } n int main() { n   int i, j; n   for (i = 0; i < 3; i++) scanf( %s , in[i]); n   for (i = 0; i < 3; i++) { n     for (j = 0; j < 3; j++) { n       if (in[i][j] ==  0 ) n         oc++; n       else if (in[i][j] ==  X ) n         xc++; n     } n   } n   try { n     bool flag = 0; n     bool cand[2] = {0, 0}; n     if (xc != oc && xc != oc + 1) throw  illegal ; n     if (xc == 0 && oc == 0) throw  first ; n     for (i = 0; i < 3; i++) { n       for (j = 0; j < 3; j++) { n         char c = in[i][j]; n         in[i][j] =  . ; n         int f = whowin(); n         in[i][j] = c; n         if (!f) { n           flag = 1; n           if (c ==  0 ) n             cand[1] = 1; n           else if (c ==  X ) n             cand[0] = 1; n         } n       } n     } n     if (!flag) throw  illegal ; n     if (xc == oc) { n       if (cand[1]) { n         if (whowin() == 2) throw  the second player won ; n         throw  first ; n       } else n         throw  illegal ; n     } else { n       if (cand[0]) { n         if (whowin() == 1) throw  the first player won ; n         if (xc + oc == 9) n           throw  draw ; n         else n           throw  second ; n       } else n         throw  illegal ; n     } n   } catch (const char* s) { n     puts(s); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = (long double)1e-7; n const long double PI = acos(0) * 2; n bool isZero(const long double& x) { return abs(x) <= EPS; } n int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); } n long long gcd(long long a, long long b) { n   for (; b; a %= b, swap(a, b)) { n   } n   return abs(a); n } n pair<long long, long long> operator+(const pair<long long, long long>& a, n                                      const pair<long long, long long>& b) { n   return pair<long long, long long>(a.first + b.first, a.second + b.second); n } n pair<long long, long long> operator-(const pair<long long, long long>& a, n                                      const pair<long long, long long>& b) { n   return pair<long long, long long>(a.first - b.first, a.second - b.second); n } n pair<long long, long long> operator*(const pair<long long, long long>& a, n                                      const long long& b) { n   return pair<long long, long long>(a.first * b, a.second * b); n } n long long operator*(const pair<long long, long long>& a, n                     const pair<long long, long long>& b) { n   return a.first * b.second - b.first * a.second; n } n long long ccw(const pair<long long, long long>& a, n               const pair<long long, long long>& b, n               const pair<long long, long long>& c) { n   return a * b + b * c + c * a; n } n void fg(vector<int> G[], int a, int b) { n   G[a].emplace_back(b); n   G[b].emplace_back(a); n } n void fg(vector<pair<int, int> > G[], int a, int b, int c) { n   G[a].emplace_back(b, c); n   G[b].emplace_back(a, c); n } n long long A, B, C; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> A >> B >> C; n   if (!A && !B) { n     puts(C ?  0  :  -1 ); n     exit(0); n   } n   if (!A) { n     printf( 1 n%.10lf n , double(-C) / B); n     exit(0); n   } n   long long D = B * B - 4 * A * C; n   if (D < 0) { n     puts( 0 ); n     exit(0); n   } n   if (!D) { n     printf( 1 n%.10lf n , double(-B) / A / 2); n     exit(0); n   } n   if (A < 0) { n     A = -A; n     B = -B; n     C = -C; n   } n   printf( 2 n%.10lf n%.10lf n , double(-B - sqrt(D)) / A / 2, n          double(-B + sqrt(D)) / A / 2); n   return 0; n } n


#include <bits/stdc++.h> n const int INT_INF = 0x3f3f3f3f; n const int INT_M_INF = 0x7f7f7f7f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const long long LL_M_INF = 0x7f7f7f7f7f7f7f7f; n const double pi = acos(-1.0); n using namespace std; n inline void debug(char ch) { n   for (int __ii = 0; __ii < 20; ++__ii) putchar(ch); n   printf(  n ); n } n const double eps = 1e-8; n inline int dcmp(double a, double b) { n   if (fabs(a - b) < eps) return 0; n   return a < b ? -1 : 1; n } n const long long MOD = 1e9 + 7; n const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; n const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; n const int MAXN = 1000 + 10; n const int MAXT = 10000 + 10; n char s[MAXN], t[MAXN]; n int len; n char sta[100]; n bool vis[100]; n bool solve() { n   for (int i = 0; i < len; ++i) n     if (s[i] != t[i]) { n       if (vis[s[i] -  a ] || vis[t[i] -  a ]) return false; n       if (sta[s[i] -  a ] != 0) { n         if (sta[s[i] -  a ] != t[i]) return false; n       } else if (sta[t[i] -  a ] != 0) { n         if (sta[t[i] -  a ] != s[i]) return false; n       } else { n         sta[s[i] -  a ] = t[i]; n         sta[t[i] -  a ] = s[i]; n       } n     } else { n       if (sta[s[i] -  a ] != 0) return false; n       vis[s[i] -  a ] = true; n     } n   return true; n } n vector<pair<char, char> > vec; n int main() { n   while (scanf( %s%s , s, t) == 2) { n     memset(sta, 0, sizeof(sta)); n     memset(vis, false, sizeof vis); n     vec.clear(); n     len = strlen(s); n     bool ans = solve(); n     if (ans) { n       for (int i = 0; i < 26; ++i) n         if (sta[i] != 0) { n           vec.push_back(pair<char, char>( a  + i, sta[i])); n           sta[sta[i] -  a ] = 0; n         } n       printf( %d n , (int)((vec).size())); n       for (int i = 0; i < (int)((vec).size()); ++i) n         printf( %c %c n , ((vec[i]).first), ((vec[i]).second)); n     } else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n int tt[1016][1016]; n int hh[1016][1016]; n int n, m; n int ex, ey; n queue<int> q; n queue<int> p; n void bfs() { n   while (!p.empty()) { n     int x = p.front(); n     int y = q.front(); n     p.pop(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int nx = x + dx[i]; n       int ny = y + dy[i]; n       if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && tt[nx][ny] == 0 && n           !(nx == ex && ny == ey)) { n         p.push(nx); n         q.push(ny); n         tt[nx][ny] = tt[x][y] + 1; n       } n     } n   } n } n int main() { n   scanf( %d %d , &n, &m); n   getchar(); n   int ans = 0; n   memset(tt, 0, sizeof(tt)); n   memset(hh, 0, sizeof(hh)); n   char c; n   int zux, zuy; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %c , &c); n       if (c >=  0  && c <=  9 ) { n         hh[i][j] = c -  0 ; n       } n       if (c ==  S ) { n         zux = i; n         zuy = j; n       } n       if (c ==  E ) { n         tt[i][j] = 1; n         ex = i; n         ey = j; n       } n       if (c ==  T ) { n         tt[i][j] = -1; n       } n     } n     getchar(); n   } n   p.push(ex); n   q.push(ey); n   bfs(); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (i == zux && j == zuy) continue; n       if (i == ex && j == ey) continue; n       if (tt[i][j] <= tt[zux][zuy] && tt[i][j] != 0 && tt[i][j] != -1) { n         ans += hh[i][j]; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   map<int, Node *> go; n   int l, r; n   Node *p, *link; n   Node(int _l, int _r, Node *_p) : l(_l), r(_r), p(_p), link(0) {} n }; n const int MAXN = 100 * 1000 + 5, S = 1000; n string s[MAXN]; n int c[MAXN], rr[MAXN], nn, curSuf; n long long ans = 0; n vector<int> a, sid; n Node *splitEdge(Node *v, int pos) { n   Node *res = new Node(v->l, pos, v->p); n   v->p->go[a[v->l]] = res; n   res->go[a[pos]] = v; n   v->p = res; n   v->l = pos; n   return res; n } n pair<Node *, int> go(Node *v, int l, int len) { n   while (len > 0) { n     v = v->go[a[l]]; n     len -= v->r - v->l; n     l += v->r - v->l; n   } n   return make_pair(v, v->r + len); n } n void add(int i, pair<Node *, int> &p) { n   Node *w = 0; n   while (curSuf <= i) { n     Node *v = p.first; n     int pos = p.second; n     if (pos != v->r) { n       if (a[i] == a[pos]) { n         p.second++; n         return; n       } n       v = splitEdge(v, pos); n       Node *u = new Node(i, nn, v); n       v->go[a[i]] = u; n       if (w) w->link = v; n       w = v; n     } else { n       if (w) w->link = v; n       w = 0; n       if (v->go.find(a[i]) != v->go.end()) { n         p.first = v->go[a[i]]; n         p.second = p.first->l + 1; n         return; n       } n       Node *u = new Node(i, nn, v); n       v->go[a[i]] = u; n     } n     int len = 0, l = v->r; n     Node *q = v; n     while (!q->link && q->p) { n       len += q->r - q->l; n       l -= q->r - q->l; n       q = q->p; n     } n     if (!q->p) { n       if (len) p = go(q, l + 1, len - 1); n     } else n       p = go(q->link, l, len); n     curSuf++; n   } n } n Node *buildSufTree() { n   nn = a.size(); n   Node *root = new Node(0, 0, 0); n   pair<Node *, int> p = make_pair(root, 0); n   curSuf = 0; n   for (int i = 0; i < nn; i++) add(i, p); n   return root; n } n long long dfs(Node *v, int dep = 0) { n   long long res = 0; n   if (v->go.empty()) res += c[sid[nn - dep]]; n   for (map<int, Node *>::iterator it = v->go.begin(); it != v->go.end(); it++) n     res += dfs(it->second, dep + it->second->r - it->second->l); n   if (v->go.empty()) { n     if (a[v->l] < S) ans = max(ans, res * (rr[sid[nn - dep]] - (nn - dep))); n   } else n     ans = max(ans, res * dep); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   for (int i = 0; i < n; i++) { n     for (size_t j = 0; j < s[i].length(); j++) { n       a.push_back(s[i][j]); n       sid.push_back(i); n     } n     rr[i] = a.size(); n     a.push_back(S + i); n     sid.push_back(i); n   } n   dfs(buildSufTree()); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcounll(s); n } n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n int k, n, m; n int a[200005], b[200005]; n vector<pair<int, int> > V[200005]; n vector<pair<int, int> > VV; n int M[200005], vt[200005]; n vector<pair<long double, int> > T[200005]; n vector<pair<long double, int> > TT; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   memset(M, -1, sizeof(M)); n   memset(vt, -1, sizeof(vt)); n   cin >> k >> n >> m; n   for (int i = (1); i <= (k); ++i) { n     cin >> a[i]; n   } n   for (int run = 0; run < (n); ++run) { n     int id, u, v; n     cin >> id >> u >> v; n     b[run + 1] = id; n     if (id == 1) { n       if (M[u] < v) { n         M[u] = v; n         vt[u] = run + 1; n       } n     } else if (id == 2) { n       V[u].push_back(make_pair(v, run + 1)); n     } else { n       VV.push_back(make_pair(v, run + 1)); n     } n   } n   sort((VV).begin(), (VV).end()); n   priority_queue<pair<pair<long double, int>, pair<int, int> > > que; n   for (int i = (1); i <= (k); ++i) { n     if (M[i] > a[i]) { n       V[i].push_back(make_pair(M[i] - a[i], vt[i])); n     } n     sort((V[i]).begin(), (V[i]).end()); n     long long run = a[i], temp; n     for (int j = ((int)(V[i]).size()) - 1; j >= 0; j--) { n       temp = run + V[i][j].first; n       T[i].push_back(make_pair(log(temp) - log(run), V[i][j].second)); n       run = temp; n     } n     if (((int)(V[i]).size())) { n       que.push( n           make_pair(make_pair(T[i][0].first, T[i][0].second), make_pair(i, 0))); n     } n   } n   long double RR = 0; n   while (!que.empty()) { n     pair<long double, int> PP = que.top().first; n     long double x = PP.first; n     int t = PP.second; n     pair<int, int> P = que.top().second; n     int u = P.first, v = P.second; n     que.pop(); n     if (((int)(T[u]).size()) >= v + 2) { n       que.push(make_pair(make_pair(T[u][v + 1].first, T[u][v + 1].second), n                          make_pair(u, v + 1))); n     } n     RR += x; n     TT.push_back(make_pair(RR, t)); n   } n   RR = 0; n   long double MAX = -1; n   int vt = -1; n   for (int i = 0; i <= ((int)(VV).size()); i++) { n     if (i > m) break; n     int u = min(m - i, ((int)(TT).size())); n     u--; n     long double x; n     if (u >= 0) n       x = TT[u].first; n     else n       x = 0; n     if (MAX < x + RR) { n       MAX = x + RR; n       vt = i; n     } n     if (i < ((int)(VV).size())) RR += log(VV[((int)(VV).size()) - i - 1].first); n   } n   int u = min(m - vt, ((int)(TT).size())); n   cout << u + vt << endl; n   vector<pair<int, int> > res; n   for (int i = 1; i <= vt; i++) n     res.push_back(make_pair(b[VV[((int)(VV).size()) - i].second], n                             VV[((int)(VV).size()) - i].second)); n   for (int i = 0; i < u; i++) n     res.push_back(make_pair(b[TT[i].second], TT[i].second)); n   sort((res).begin(), (res).end()); n   for (int i = 0; i < (((int)(res).size())); ++i) cout << res[i].second <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   bool flag = true; n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       int l = a[1]; n       int r = a[2]; n       if (a[i] >= l + r) flag = false; n     } else if (i == n - 1) { n       int l = a[n - 3]; n       int r = a[n - 2]; n       if (a[i] >= l + r) flag = false; n     } else { n       int l, r; n       if (i + 2 > n - 1) n         r = a[n - 1]; n       else n         r = a[i + 2]; n       if (i - 2 < 0) n         l = a[0]; n       else n         l = a[i - 2]; n       if (a[i] >= l + r) flag = false; n     } n   } n   if (flag == false) n     cout <<  NO n ; n   else { n     cout <<  YES n ; n     cout << a[0] <<    ; n     int i = 1; n     while (i < n) { n       cout << a[i] <<    ; n       i = i + 2; n     } n     if (n % 2) n       i = n - 1; n     else n       i = n - 2; n     while (i >= 2) { n       cout << a[i] <<    ; n       i = i - 2; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int c1 = 0, c2 = 0, c3 = 0; n   for (int i = 0; i < n; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     c1 += x, c2 += y, c3 += z; n   } n   if (c1 == 0 && c2 == 0 && c3 == 0) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long ll; n  n const int Maxn = 1000005; n const int mod = 1000000007; n  n int fac[Maxn], inv[Maxn]; n int n; n int res; n  n int toPower(int a, int p) n { n  int res = 1; n  while (p) { n   if (p & 1) res = ll(res) * a % mod; n   p >>= 1; a = ll(a) * a % mod; n  } n  return res; n } n  n int Inv(int x) { return toPower(x, mod - 2); } n  n int C(int n, int k) n { n  if (n < 0 || k < 0 || k > n) return 0; n  return ll(fac[n]) * inv[k] % mod * inv[n - k] % mod; n } n  n int main() n { n  fac[0] = inv[0] = 1; n  for (int i = 1; i < Maxn; i++) { n   fac[i] = ll(i) * fac[i - 1] % mod; n   inv[i] = Inv(fac[i]); n  } n  scanf( %d , &n); n  for (int r = 1; r < n; r += 2) n   res = (res + ll(C(r + 1, n - r - 1)) * fac[r]) % mod; n  res = ll(res) * 2 % mod * n % mod; n  printf( %d n , res); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 5; n vector<long long> f; n vector<long long> s; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long a, b; n   cin >> a >> b; n   while (a > 0) { n     long long rem = a % 10; n     a /= 10; n     f.push_back(rem); n   } n   while (b > 0) { n     long long rem = b % 10; n     b /= 10; n     s.push_back(rem); n   } n   reverse(f.begin(), f.end()); n   reverse(s.begin(), s.end()); n   long long dp[10]; n   memset(dp, 0, sizeof(dp)); n   for (auto x : f) { n     dp[x]++; n   } n   if (f.size() < s.size()) { n     sort(f.begin(), f.end(), greater<long long>()); n     for (auto x : f) { n       cout << x; n     } n     cout << endl; n     return 0; n   } n   bool done = 0; n   for (long long i = 0; i < s.size(); ++i) { n     long long x = s[i]; n     for (long long num = x; num >= 0; --num) { n       if (dp[num] > 0) { n         if (num == x) { n           dp[num]--; n           vector<long long> rem; n           for (long long j = 0; j < 10; ++j) { n             long long tmp = dp[j]; n             while (tmp--) { n               rem.push_back(j); n             } n           } n           sort(rem.begin(), rem.end()); n           bool val = 1; n           for (long long j = i + 1; j < s.size(); ++j) { n             if (rem[j - (i + 1)] > s[j]) val = 0; n             if (rem[j - (i + 1)] < s[j]) break; n           } n           if (!val) { n             dp[num]++; n             continue; n           } n           dp[num]++; n         } n         cout << num; n         if (num < x) done = 1; n         dp[num]--; n         break; n       } n     } n     if (done) break; n   } n   vector<long long> rem; n   for (long long i = 0; i < 10; ++i) { n     while (dp[i] > 0) { n       rem.push_back(i); n       dp[i]--; n     } n   } n   sort(rem.begin(), rem.end(), greater<long long>()); n   for (auto x : rem) { n     cout << x; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   for (auto v : V) os << v <<    ; n   return cout <<   ; n } n template <class T> n ostream& operator<<(ostream& os, set<T> S) { n   for (auto s : S) os << s <<    ; n   return cout <<   ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os << P.first <<     << P.second; n } n vector<long long> vis; n vector<vector<long long>> adj; n int main() { n   long long i, j, n, f1, f2, t, k, a, b, x1, x2, m, w, z; n   t = 1; n   while (t--) { n     cin >> n >> z; n     vector<pair<long long, long long>> v(n + 1); n     vis.assign(n + 1, 0); n     adj.assign(n + 1, {}); n     for (i = 1; i <= n - 1; i++) { n       cin >> a >> b; n       adj[a].push_back(b); n       adj[b].push_back(a); n     } n     long long s; n     queue<long long> q; n     vector<long long> used(n + 1); n     vector<long long> x(n + 1), y(n + 1); n     vector<long long> p(n + 1); n     s = z; n     q.push(s); n     used[s] = 1; n     p[s] = -1; n     while (!q.empty()) { n       long long v = q.front(); n       q.pop(); n       for (long long u : adj[v]) { n         if (!used[u]) { n           used[u] = 1; n           q.push(u); n           x[u] = x[v] + 1; n           p[u] = v; n         } n       } n     } n     used.assign(n + 1, 0); n     s = 1; n     q.push(s); n     used[s] = 1; n     p[s] = -1; n     while (!q.empty()) { n       long long v = q.front(); n       q.pop(); n       for (long long u : adj[v]) { n         if (!used[u]) { n           used[u] = 1; n           q.push(u); n           y[u] = y[v] + 1; n           p[u] = v; n         } n       } n     } n     long long ans = 0; n     for (i = 1; i <= n; i++) { n       if (y[i] > x[i]) { n         ans = max(ans, 2 * y[i]); n       } n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n void seriy() { n   cin.tie(0); n   cout.tie(0); n } n signed main() { n   seriy(); n   long long n, m; n   cin >> n >> m; n   long long mn = 2e9 + 8; n   for (long long i = 0; i < m; i++) { n     long long l, r; n     cin >> l >> r; n     mn = min(mn, r - l + 1); n   } n   cout << mn << endl; n   vector<long long> ans(n); n   for (long long i = 0; i < n; i++) { n     ans[i] = i % mn; n   } n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int a = 0, b = 0; n   int n, d, t; n   cin >> n >> d; n   int sol = 0; n   for (int i = 0; i < n; ++i) { n     cin >> t; n     if (t == 0) { n       if (b + t < 0) { n         a = 0; n         b = d; n         sol++; n       } else { n         a = max(0, a); n       } n     } else if (a + t > d) { n       sol = -1; n       break; n     } n     a = min(d, a + t); n     b = min(d, b + t); n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, m, n, a, u, v; n void printAnswer(vector<int> ans) { n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n } n int main() { n   cin >> t; n   for (int j = 0; j < t; j++) { n     cin >> n >> m; n     vector<vector<int> > G(n + 1); n     for (int i = 0; i < m; i++) { n       cin >> v >> u; n       G[v].push_back(u); n       G[u].push_back(v); n     } n     vector<int> odd; n     vector<int> even; n     bool seen[n + 1]; n     for (int i = 0; i <= n; i++) { n       seen[i] = false; n     } n     bool oddLevel = true; n     vector<int> currLevel; n     currLevel.push_back(1); n     seen[1] = true; n     while (currLevel.size() > 0) { n       if (oddLevel) { n         odd.insert(odd.end(), currLevel.begin(), currLevel.end()); n       } else { n         even.insert(even.end(), currLevel.begin(), currLevel.end()); n       } n       vector<int> nextLevel; n       for (int i = 0; i < currLevel.size(); i++) { n         int n = currLevel[i]; n         for (int k = 0; k < G[n].size(); k++) { n           if (!seen[G[n][k]]) { n             seen[G[n][k]] = true; n             nextLevel.push_back(G[n][k]); n           } n         } n       } n       currLevel = nextLevel; n       oddLevel = !oddLevel; n     } n     if (odd.size() >= even.size()) { n       printAnswer(even); n     } else { n       printAnswer(odd); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int len, a, b, c; n string s; n int main() { n   cin >> s; n   len = s.length(); n   for (int i = 0; i < len; i++) { n     if (s[i] ==  a ) n       a++, c++; n     else n       b++; n     a = max(a, b); n     b = max(b, c); n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 11; n const int Inf = 1e9; n vector<int> dx[3] = { n     {-1, 0, 0, 1}, {-1, -1, 1, 1}, {-1, 1, 2, 2, 1, -1, -2, -2}}; n vector<int> dy[3] = { n     {0, -1, 1, 0}, {-1, 1, -1, 1}, {-2, -2, -1, 1, 2, 2, -1, 1}}; n int n; n pair<int, int> g[3][3][N][N][N][N]; n int a[N][N]; n bool ok(int x, int y) { return 0 <= x && x < n && 0 <= y && y < n; } n void build() { n   for (int x = 0; x < n; ++x) { n     for (int y = 0; y < n; ++y) { n       for (int t = 0; t < 3; ++t) { n         for (int i = 0; i < dx[t].size(); ++i) { n           if (t == 2) { n             int cx = x + dx[t][i]; n             int cy = y + dy[t][i]; n             if (ok(cx, cy)) { n               g[t][t][x][y][cx][cy] = {1, 0}; n             } n           } else { n             for (int k = 1; k <= n; ++k) { n               int cx = x + dx[t][i] * k; n               int cy = y + dy[t][i] * k; n               if (ok(cx, cy)) { n                 g[t][t][x][y][cx][cy] = {1, 0}; n               } n             } n           } n         } n       } n     } n   } n } n pair<int, int> sm(pair<int, int> a, pair<int, int> b) { n   return {a.first + b.first, a.second + b.second}; n } n void count(int x, int y, vector<pair<int, int>> &t) { n   if (a[x][y] == n * n) { n     return; n   } n   int ix = -1, jx = -1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (a[x][y] + 1 == a[i][j]) { n         ix = i; n         jx = j; n       } n     } n   } n   vector<pair<int, int>> nt(3, {Inf, Inf}); n   for (int t1 = 0; t1 < 3; ++t1) { n     for (int t2 = 0; t2 < 3; ++t2) { n       nt[t2] = min(nt[t2], sm(t[t1], g[t1][t2][x][y][ix][jx])); n     } n   } n   t = nt; n   count(ix, jx, t); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       for (int k = 0; k < n; ++k) { n         for (int t = 0; t < n; ++t) { n           for (int t1 = 0; t1 < 3; ++t1) { n             for (int t2 = 0; t2 < 3; ++t2) { n               g[t1][t2][i][j][k][t] = {Inf, Inf}; n             } n           } n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       for (int t1 = 0; t1 < 3; ++t1) { n         for (int t2 = 0; t2 < 3; ++t2) { n           if (t1 == t2) { n             g[t1][t2][i][j][i][j] = {0, 0}; n           } else { n             g[t1][t2][i][j][i][j] = {1, 1}; n           } n         } n       } n     } n   } n   build(); n   for (int t3 = 0; t3 < 3; ++t3) { n     for (int kx = 0; kx < n; ++kx) { n       for (int ky = 0; ky < n; ++ky) { n         for (int t1 = 0; t1 < 3; ++t1) { n           for (int ix = 0; ix < n; ++ix) { n             for (int iy = 0; iy < n; ++iy) { n               for (int t2 = 0; t2 < 3; ++t2) { n                 for (int jx = 0; jx < n; ++jx) { n                   for (int jy = 0; jy < n; ++jy) { n                     g[t1][t2][ix][iy][jx][jy] = n                         min(g[t1][t2][ix][iy][jx][jy], n                             sm(g[t1][t3][ix][iy][kx][ky], n                                g[t3][t2][kx][ky][jx][jy])); n                   } n                 } n               } n             } n           } n         } n       } n     } n   } n   int ix = -1, jx = -1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (a[i][j] == 1) { n         ix = i; n         jx = j; n       } n     } n   } n   vector<pair<int, int>> t(3); n   count(ix, jx, t); n   pair<int, int> ans = {Inf, Inf}; n   for (int i = 0; i < 3; ++i) { n     ans = min(ans, t[i]); n   } n   cout << ans.first <<     << ans.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = LONG_LONG_MAX; n const long long int NINF = LONG_LONG_MIN; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long int n, l, k; n   cin >> n >> l >> k; n   string s; n   cin >> s; n   sort((s).begin(), (s).end()); n   vector<string> list(n); n   long long int p = 0; n   long long int start = 0; n   for (long long int i = 0; i < l; i++) { n     for (long long int j = start; j < k; j++) { n       list[j] += s[p++]; n       if (j != 0) { n         if (list[j].back() != list[j - 1].back()) start = j; n       } n     } n   } n   for (long long int i = 0; i < n; i++) { n     for (long long int j = (long long int)list[i].size(); j < l; j++) { n       list[i] += s[p++]; n     } n   } n   for (string& ss : list) cout << ss << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> dan, ans; n vector<pair<long long, long long> > check; n long long calc(long long st) { n   multiset<long long> otr; n   long long otv = 0; n   for (long long i = 0; i < dan.size(); i++) { n     st += dan[i]; n     if (dan[i] < 0) otr.insert(dan[i]); n     if (st < 0) { n       st -= *otr.begin(); n       otr.erase(otr.begin()); n       otv++; n     } n   } n   return otv; n } n int main() { n   long long n, m; n   cin >> n >> m; n   dan.resize(n); n   ans.resize(m, -1); n   check.resize(m, {0, 0}); n   for (long long i = 0; i < n; i++) { n     cin >> dan[i]; n   } n   for (long long i = 0; i < m; i++) { n     cin >> check[i].first; n     check[i].second = i + 1; n   } n   sort(check.begin(), check.end()); n   reverse(check.begin(), check.end()); n   long long l = 0; n   while (l < m) { n     long long l1 = l, r1 = m, mid, mida; n     long long now; n     if (ans[l1] != -1) n       now = ans[l1]; n     else { n       now = calc(check[l1].first); n       ans[l1] = now; n     } n     while (l1 + 1 != r1) { n       mid = (l1 + r1) / 2; n       if (ans[mid] != -1) n         mida = ans[mid]; n       else { n         mida = calc(check[mid].first); n         ans[mid] = mida; n       } n       if (mida > now) { n         r1 = mid; n       } else { n         l1 = mid; n       } n     } n     if (ans[l1] == -1) { n       ans[l1] = calc(check[l1].first); n     } n     l = l1 + 1; n   } n   long long last = -1; n   for (long long i = 0; i < m; i++) { n     if (ans[i] != -1) n       last = ans[i]; n     else n       ans[i] = last; n   } n   for (long long i = 0; i < m; i++) { n     swap(check[i].first, check[i].second); n     check[i].second = ans[i]; n   } n   sort(check.begin(), check.end()); n   for (long long i = 0; i < m; i++) { n     cout << check[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500; n int d[maxn * maxn]; n map<int, int> id; n int idcnt; n map<pair<int, int>, int> id2; n int id2cnt; n pair<int, int> status[maxn * maxn]; n int ans[maxn * maxn]; n int first[maxn * maxn], nxt[1750000], des[1750000], tot; n int W[6]; n int T, f; n queue<int> Q; n inline int get(int status, int x_) { return status % W[x_ + 1] / W[x_]; } n void dfs(int dep, int rest, int val) { n   if (dep == 4) { n     int temp = val + rest * W[4]; n     if (!id.count(temp)) { n       id.insert(make_pair(temp, ++idcnt)); n     } n     return; n   } n   for (int i = 0; i <= rest; i++) { n     dfs(dep + 1, rest - i, val + W[dep] * i); n   } n } n inline void AE(int id1, int id2) { n   tot++; n   des[tot] = id2; n   nxt[tot] = first[id1]; n   first[id1] = tot; n   d[id2]++; n } n void addEdge(int fi, int se, int idd) { n   for (int i = 1; i <= 4; i++) { n     for (int j = 1; j <= 4; j++) { n       int f = get(fi, i); n       int s = get(se, j); n       if (f > 0 && s > 0) { n         AE(id2[make_pair(se, fi - W[i] + W[(i + j) % 5])], idd); n       } n     } n   } n } n void search() { n   for (int i = 1; i <= id2cnt; i++) { n     if (d[i] == 0) { n       if (status[i].first == 8) { n         ans[i] = 1; n       } else { n         ans[i] = -1; n       } n       Q.push(i); n     } n   } n   while (!Q.empty()) { n     int q = Q.front(); n     Q.pop(); n     if (ans[q] == 1) { n       for (int t = first[q]; t; t = nxt[t]) { n         int v = des[t]; n         d[v]--; n         if (d[v] == 0 && ans[v] == 0) { n           ans[v] = -1; n           Q.push(v); n         } n       } n     } else { n       for (int t = first[q]; t; t = nxt[t]) { n         int v = des[t]; n         d[v]--; n         if (ans[v] == 0) { n           ans[v] = 1; n           Q.push(v); n         } n       } n     } n   } n } n void init() { n   W[0] = 1; n   for (int i = 1; i <= 5; i++) W[i] = W[i - 1] * 10; n   dfs(0, 8, 0); n   for (pair<int, int> fi : id) { n     for (pair<int, int> se : id) { n       id2.insert(make_pair(make_pair(fi.first, se.first), ++id2cnt)); n       status[id2cnt] = make_pair(fi.first, se.first); n     } n   } n   memset(d, 0, sizeof d); n   for (pair<pair<int, int>, int> e : id2) { n     addEdge(e.first.first, e.first.second, e.second); n   } n   search(); n } n void solve() { n   scanf( %d , &f); n   int ast = 0, bst = 0; n   for (int i = 0; i < 8; i++) { n     int t; n     scanf( %d , &t); n     ast += W[t]; n   } n   for (int i = 0; i < 8; i++) { n     int t; n     scanf( %d , &t); n     bst += W[t]; n   } n   if (f) { n     int res = ans[id2[make_pair(bst, ast)]]; n     if (res == 1) { n       printf( Bob n ); n     } else if (res == -1) { n       printf( Alice n ); n     } else { n       printf( Deal n ); n     } n   } else { n     int res = ans[id2[make_pair(ast, bst)]]; n     if (res == 1) { n       printf( Alice n ); n     } else if (res == -1) { n       printf( Bob n ); n     } else { n       printf( Deal n ); n     } n   } n } n int main() { n   init(); n   scanf( %d , &T); n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, cnt[60], have, r = 1; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> s; n   n = s.size(); n   s =     + s; n   for (int i = (1); i <= (n); ++i) { n     while (r <= n && have < 26) { n       if (s[r] ==  ? ) n         ++have; n       else { n         int temp = s[r] -  A ; n         if (!cnt[temp]) ++have; n         ++cnt[temp]; n       } n       ++r; n     } n     if (have == 26 && r - i == 26) { n       int q[50], top = 0; n       for (int j = (0); j <= (25); ++j) n         if (!cnt[j]) q[++top] = j; n       for (int j = (1); j <= (i - 1); ++j) n         if (s[j] ==  ? ) n           cout <<  A ; n         else n           cout << s[j]; n       for (int j = (i); j <= (min(r, n)); ++j) n         if (s[j] ==  ? ) { n           if (top) n             cout << (char)( A  + q[top--]); n           else n             cout <<  A ; n         } else n           cout << s[j]; n       for (int j = (r + 1); j <= (n); ++j) n         if (s[j] ==  ? ) n           cout <<  A ; n         else n           cout << s[j]; n       return 0; n     } n     if (s[i] ==  ? ) n       --have; n     else { n       int temp = s[i] -  A ; n       --cnt[temp]; n       if (!cnt[temp]) --have; n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n const int MAX = 1e5 + 55; n const int inf = 1e9 + 77; n const int MOD = 1e9 + 7; n const double PI = acos(-1.0); n const double eps = 1e-7; n int main() { n   int n, k; n   scanf( %d , &n); n   scanf( %d , &k); n   if (n > (long long)k * (long long)(k - 1)) { n     printf( NO ); n     return 0; n   } n   printf( YES n ); n   int need = n; n   for (int i = 1; i <= k && need; ++i) { n     for (int j = i + 1; j <= k && need; ++j) { n       printf( %d %d n , i, j); n       --need; n       if (need) { n         printf( %d %d n , j, i); n         --need; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long spf[1000005]; n vector<long long> primes, prev[1000005], next[1000005]; n void sieve() { n   for (long long i = 2; i < 1000005; ++i) { n     if (spf[i] == 0) { n       spf[i] = i; n       for (long long j = 2 * i; j <= 1000005; j += i) { n         if (spf[j] == 0) spf[j] = i; n       } n     } n   } n } n bool cmp(const pair<long long, long long>& a, n          const pair<long long, long long>& b) { n   if (a.first == b.first) n     return a.second < b.second; n   else n     return a.first > b.first; n } n string kmp(string s) { n   long long n = s.size(); n   vector<long long> p(n); n   long long i = 1, len = 0; n   while (i < n) { n     if (s[i] == s[len]) { n       len++; n       p[i] = len; n       i++; n     } else { n       if (len != 0) { n         len = p[len - 1]; n       } else { n         p[i] = 0; n         i++; n       } n     } n   } n   long long ans = p[n - 1]; n   string t = s.substr(0, ans); n   return t; n } n string pal(string t) { n   string r = t; n   reverse(r.begin(), r.end()); n   string t1 = kmp(r +  #  + t); n   string t2 = kmp(t +  #  + r); n   return t1.size() > t2.size() ? t1 : t2; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   srand(static_cast<unsigned>(time(0))); n   long long t = 1; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     long long n = s.size(); n     long long i = 0; n     while (i <= n - i - 1 && s[i] == s[n - i - 1]) { n       i++; n     } n     if (i > n - i - 1) { n       cout << s <<   n ; n       continue; n     } n     string t1 = s.substr(i, n - 2 * i); n     string ans = pal(t1); n     string temp = s.substr(0, i); n     ans = temp + ans; n     reverse(temp.begin(), temp.end()); n     ans = ans + temp; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n vector<long long int> graph[100007]; n long long int vis[100007]; n long long int mxm; n void dfs(long long int u, long long int p, long long int d) { n   vis[u] = 1; n   mxm = max(mxm, d); n   for (long long int v : graph[u]) { n     dfs(v, u, d + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int tc = 1; n   long long int cs = 0; n   while (tc--) { n     long long int n; n     cin >> n; n     mxm = 0; n     for (long long int i = 1; i <= n; i++) { n       long long int a; n       cin >> a; n       if (a != -1) graph[a].push_back(i); n     } n     long long int ans = 0; n     for (long long int i = 1; i <= n; i++) { n       if (!vis[i]) { n         dfs(i, -1, 1); n         ans = max(ans, mxm); n         mxm = 0; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 31400; n struct point { n   int x, y; n }; n int n; n pair<point, point> p[5]; n bool used[5]; n bool func() { n   long long s = 0; n   point MIN, MAX; n   MIN.x = MIN.y = NMAX; n   MAX.x = MAX.y = 0; n   for (int i = 0; i < n; i++) { n     s += (p[i].second.x - p[i].first.x) * (p[i].second.y - p[i].first.y); n     MIN.x = min(MIN.x, p[i].first.x); n     MIN.y = min(MIN.y, p[i].first.y); n     MAX.x = max(MAX.x, p[i].second.x); n     MAX.y = max(MAX.y, p[i].second.y); n   } n   if ((MAX.x - MIN.x) == (MAX.y - MIN.y) && s == pow(MAX.x - MIN.x, 2)) { n     return true; n   } n   return false; n } n int main() { n   cin >> n; n   pair<int, int> a, b, MAX, MIN; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first.x >> p[i].first.y >> p[i].second.x >> p[i].second.y; n   } n   if (func()) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int power(unsigned long long int n, n                              unsigned long long int m) { n   if (n == 0LL) n     return 1LL % m; n   else if (n & 1LL) n     return ((power(n / 2, m) % m) * (power(n / 2, m) % m) * (3LL % m)) % m; n   else n     return ((power(n / 2, m) % m) * (power(n / 2, m) % m)) % m; n } n void solve() { n   unsigned long long int n, m, ans; n   cin >> n >> m; n   ans = (power(n, m) + m - 1) % m; n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   ; n   unsigned long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const long long mod = 1000000007; n int A[N], rmq[N][25], getL[N]; n void go(int n) { n   for (int i = 0; i < n; i++) rmq[i][0] = i; n   for (int i = 1; i < N; i++) getL[i] = log2(i); n   for (int j = 1; (1 << j) <= n; j++) { n     for (int i = 0; i + (1 << j) - 1 < n; i++) { n       if (A[rmq[i][j - 1]] < A[rmq[i + (1 << (j - 1))][j - 1]]) { n         rmq[i][j] = rmq[i][j - 1]; n       } else { n         rmq[i][j] = rmq[i + (1 << (j - 1))][j - 1]; n       } n     } n   } n } n int getmin(int i, int j) { n   int k = getL[j - i + 1]; n   if (A[rmq[i][k]] < A[rmq[j - (1 << k) + 1][k]]) { n     return A[rmq[i][k]]; n   } n   return A[rmq[j - (1 << k) + 1][k]]; n } n vector<pair<int, int> > v; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, d, m; n   cin >> d >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     v.push_back(make_pair(x, y)); n   } n   v.push_back(make_pair(d, 0)); n   sort(v.begin(), v.end()); n   int st = 0, fuel = n; n   for (int i = 0; i < v.size(); i++) A[i] = v[i].second; n   go(m + 1); n   long long cst = 0; n   for (int i = 0; i < v.size(); i++) { n     if (v[i].first - st > n) { n       cst = -1; n       break; n     } n     fuel -= (v[i].first - st); n     int l = i + 1, r = v.size() - 1, res = i; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (v[mid].first - v[i].first <= fuel) { n         l = mid + 1; n         res = mid; n       } else n         r = mid - 1; n     } n     int nxtmin = i; n     l = i + 1, r = v.size() - 1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (getmin(i + 1, mid) <= v[i].second) { n         r = mid - 1; n         nxtmin = mid; n       } else n         l = mid + 1; n     } n     st = v[i].first; n     if (res >= nxtmin) { n       continue; n     } n     cst += (min(n, v[nxtmin].first - v[i].first) - fuel) * 1LL * v[i].second; n     fuel = min(n, v[nxtmin].first - v[i].first); n   } n   cout << cst; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n struct edge { n   int to, next; n } e[N * 2]; n int head[N], tot, n; n int sz[N], mxv, mxrt; n void add(int x, int y) { n   e[++tot] = (edge){y, head[x]}; n   head[x] = tot; n } n void dfs(int x, int fa, int n) { n   int mx = 0; n   sz[x] = 1; n   for (int i = head[x]; i; i = e[i].next) n     if (e[i].to != fa) { n       dfs(e[i].to, x, n); n       sz[x] += sz[e[i].to]; n       mx = max(mx, sz[e[i].to]); n     } n   mx = max(mx, n - sz[x]); n   if (mx < mxv) mxv = mx, mxrt = x; n } n pair<int, int> q[N]; n int f[N][N]; n int top, be[N], mnv, mnid, dif; n void insert(int x, int y) { n   q[++top] = pair<int, int>(x, y); n   for (int i = (int)(0); i <= (int)(n); i++) f[top][i] = f[top - 1][i]; n   for (int i = (int)(y); i <= (int)(n); i++) f[top][i] |= f[top - 1][i - y]; n } n int s1, s2, dep[N]; n void getans(int x, int fa, int type) { n   dep[x] = (type == 1 ? (s1 += 1) : (s2 += dif)); n   printf( %d %d %d n , x, fa, dep[x] - dep[fa]); n   for (int i = head[x]; i; i = e[i].next) n     if (e[i].to != fa) getans(e[i].to, x, type); n } n int main() { n   scanf( %d , &n); n   for (int i = (int)(1); i <= (int)(n - 1); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   mxv = 1 << 30; n   dfs(1, 0, n); n   f[0][0] = 1; n   for (int i = head[mxrt]; i; i = e[i].next) n     insert(e[i].to, sz[e[i].to] > sz[mxrt] ? n - sz[mxrt] : sz[e[i].to]); n   insert(mxrt, 1); n   mnv = 1 << 30; n   for (int i = (int)(0); i <= (int)(n); i++) n     if (abs(n - 2 * i) < mnv && f[top][i]) mnv = n - 2 * i, mnid = i; n   dif = mnid; n   for (int i = (int)(top); i >= (int)(1); i--) { n     if (mnid >= q[i].second && f[i - 1][mnid - q[i].second]) n       be[i] = 1, mnid -= q[i].second; n     else n       be[i] = 0; n   } n   for (int i = (int)(1); i <= (int)(top - 1); i++) n     getans(q[i].first, mxrt, be[i]); n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   vector<int> children; n   int value, bit; n   Node(int b) { n     children.assign(2, -1); n     value = 0; n     bit = b; n   } n }; n vector<Node> tree; n void add(int id, int amt, int n) { n   tree[n].value += amt; n   if (tree[n].bit == 0) return; n   int next = id / tree[n].bit; n   if (tree[n].children[next] == -1) { n     tree[n].children[next] = tree.size(); n     tree.emplace_back(tree[n].bit / 2); n   } n   add(id & (tree[n].bit - 1), amt, tree[n].children[next]); n } n int query(int id, int l, int n) { n   if (tree[n].bit == 0) return 0; n   int nextId = id & (tree[n].bit - 1); n   int nextL = l & (tree[n].bit - 1); n   int child = (id ^ l) / tree[n].bit; n   int result = 0; n   if (l & tree[n].bit) { n     int rChild = tree[n].children[1 - child]; n     if (rChild != -1) result += tree[rChild].value; n   } n   if (tree[n].children[child] != -1) n     result += query(nextId, nextL, tree[n].children[child]); n   return result; n } n int main() { n   tree.emplace_back(1 << 30); n   int q; n   cin >> q; n   for (int qa = 0; qa < q; ++qa) { n     int type; n     cin >> type; n     if (type < 3) { n       int p; n       cin >> p; n       add(p, type == 1 ? 1 : -1, 0); n     } else { n       int p, l; n       cin >> p >> l; n       cout << query(p, l, 0) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n double PI = 3.14159; n int dX[] = {1, -1, 0, 0}; n int dY[] = {0, 0, 1, -1}; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   double h, m, angle_h = 0, angle_m = 0; n   char c; n   cin >> h >> c >> m; n   h = fmod(h, 12); n   angle_h = (h + m / 60) * 5; n   angle_h *= 6.0; n   angle_m = m * 6; n   cout << fixed << setprecision(9) << angle_h <<     << angle_m <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, pos, cnt = 0; n   cin >> n >> a >> b; n   for (int pos = 1; pos <= n; pos++) { n     if (n - pos <= b && pos - 1 >= a) cnt++; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, q; n   cin >> n >> q; n   while (q--) { n     long long a, b; n     cin >> a >> b; n     if ((a + b) % 2 == 0) { n       long long ans = 0; n       ans += (a) / 2 * ((n + 1) / 2); n       ans += ((a - 1) - (a / 2)) * (n / 2); n       if (a % 2) { n         ans += (b + 1) / 2; n       } else { n         ans += b / 2; n       } n       cout << ans <<   n ; n     } else { n       long long ans = (n * n + 1) / 2; n       ans += (a) / 2 * (n / 2); n       ans += ((a - 1) - (a / 2)) * ((n + 1) / 2); n       if (a % 2) { n         ans += (b) / 2; n       } else { n         ans += (b + 1) / 2; n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, c, ans = 0; n   string s; n   cin >> s; n   bool sol = 1; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  1  && s[i + 1] ==  4  && s[i + 2] ==  4 ) { n       i += 2; n       continue; n     } n     if (s[i] ==  1  || (s[i] ==  4  && s[i - 1] ==  1 )) n       continue; n     else n       sol = 0; n   } n   if (sol) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 100; n int a[max_n], tmp[max_n]; n bool vis[max_n]; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < (n); i++) { n     cin >> a[i]; n     a[i]--; n     tmp[i] = a[i]; n   } n   sort(tmp, tmp + n); n   for (int i = 0; i < (n); i++) n     if (i != tmp[i]) { n       cout << -1 << endl; n       return 0; n     } n   long long ans = 1; n   memset(vis, false, sizeof vis); n   for (int i = 0; i < (n); i++) n     if (!vis[i]) { n       int cnt = 0; n       for (int k = i; !vis[k]; k = a[k]) { n         vis[k] = true; n         cnt++; n       } n       if (cnt % 2 == 0) cnt /= 2; n       ans *= cnt / gcd(ans, cnt); n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long p; n long long a[1000005]; n long long fastpow(long long a, int x) { n   if (x < 0) return 1; n   if (x == 0) return 1; n   if (x == 1) return a; n   long long tmp = fastpow(a, x >> 1); n   tmp = (tmp * tmp) % 1000000007ll; n   if (x & 1) { n     tmp = (tmp * a) % 1000000007ll; n   } n   return tmp; n } n void solve() { n   scanf( %d%lld , &n, &p); n   for (int i = 0; i < n; i++) { n     scanf( %lld , a + i); n   } n   if (p == 1) { n     printf( %d n , n % 2); n     return; n   } n   sort(a, a + n); n   long long ans = 0; n   long long res = 0; n   int last = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (i != n - 1) { n       ans *= fastpow(p, last - a[i]); n       ans %= 1000000007ll; n     } n     if (i != n - 1 && res <= 1e6 + 100) { n       for (int j = 0; j < 20 && j < last - a[i]; j++) { n         res *= p; n         if (res > 1e6 + 100) { n           res = 1e18; n           break; n         } n       } n     } n     if (res == 0) { n       ans++; n       res++; n     } else { n       ans += 1000000007ll - 1; n       res--; n     } n     ans %= 1000000007ll; n     last = a[i]; n   } n   ans *= fastpow(p, last); n   printf( %lld n , ans % 1000000007ll); n } n int main() { n   ios_base::sync_with_stdio(false); n   int T; n   scanf( %d , &T); n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool remin(T &a, const T &b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n bool remax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n int nxt() { n   int first; n   scanf( %d , &first); n   return first; n } n long long gcd(long long a, long long b) { n   a = abs(a); n   b = abs(b); n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n struct pt { n   long double first, second, z; n   pt() {} n   pt(long double first, long double second, long double z) n       : first(first), second(second), z(z) {} n   inline pt operator-(const pt &r) const { n     return pt(first - r.first, second - r.second, z - r.z); n   } n   inline pt operator+(const pt &r) const { n     return pt(first + r.first, second + r.second, z + r.z); n   } n   inline pt operator*(const long double &r) const { n     return pt(first * r, second * r, z * r); n   } n   inline long double sqlen() const { n     return abs(first * first + second * second + z * z); n   } n   long double len() const { return sqrtl(sqlen()); } n   inline bool operator<(const pt &r) const { n     if (first != r.first) return first < r.first; n     return second < r.second; n   } n   inline bool operator==(const pt &r) const { n     return first == r.first && second == r.second; n   } n }; n ostream &operator<<(ostream &os, const pt &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n inline pt cross(const pt &l, const pt &r) { n   return pt(l.second * r.z - l.z * r.second, l.z * r.first - l.first * r.z, n             l.first * r.second - l.second * r.first); n } n inline long double dot(const pt &l, const pt &r) { n   return l.first * r.first + l.second * r.second + l.z * r.z; n } n long long pwmod(long long a, long long n, long long mod) { n   long long ret = 1; n   while (n) { n     if (n & 1) ret = ret * a % mod; n     a = a * a % mod; n     n >>= 1; n   } n   return ret; n } n template <typename T> n inline T sqr(T first) { n   return first * first; n } n bool remin(long long &first, long long second) { n   if (first > second) { n     first = second; n     return 1; n   } n   return 0; n } n const int N = 300 * 1000; n vector<int> g[N]; n int size[N]; n int parent[N]; n int answer[N]; n int maxSize[N]; n vector<int> childs_[N]; n void dfs(int v, int p = -1) { n   size[v] = 1; n   parent[v] = p; n   for (int to : g[v]) { n     if (to == p) continue; n     dfs(to, v); n     size[v] += size[to]; n   } n   maxSize[v] = 0; n   for (int to : g[v]) { n     if (to == p) continue; n     remax(maxSize[v], size[to]); n   } n } n void dfs2(int v, int p = -1) { n   vector<int> &ch = childs_[v]; n   for (int to : g[v]) { n     if (to == p) continue; n     dfs2(to, v); n     ch.push_back(answer[to]); n   } n   if (ch.empty()) { n     answer[v] = v; n     return; n   } n   for (int &u : ch) { n     int s = size[v] / 2; n     while (u != v && maxSize[parent[u]] <= s) { n       u = parent[u]; n     } n   } n   for (int u : ch) { n     int s = size[v] / 2; n     int top = size[v] - size[u]; n     if (maxSize[u] <= s && top <= s) { n       answer[v] = u; n     } n   } n } n void solve() { n   int n = nxt(); n   int q = nxt(); n   for (int i = 1; i < n; ++i) { n     int p = nxt() - 1; n     g[p].push_back(i); n     g[i].push_back(p); n   } n   dfs(0); n   dfs2(0); n   for (int i = 0; i < (int)(q); i++) { n     int v = nxt() - 1; n     cout << answer[v] + 1 <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   for (int i = 0; i < (int)(t); i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int mod = 1000000007; n const long long linf = 0x3f3f3f3f3f3f3f3f; n char c[200100]; n int qnt[200100][26]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, k; n     scanf( %d%d , &n, &k); n     scanf(  %s , c); n     int m = n / k; n     int res = 0; n     if (k % 2) { n       for (int i = 0; i <= k / 2; i++) { n         for (int j = 0; j < m; j++) { n           int a = i + j * k; n           int b = k - 1 - i + j * k; n           qnt[i][c[a] -  a ]++; n           qnt[i][c[b] -  a ]++; n         } n       } n       for (int i = 0; i < k / 2; i++) { n         int maior = 0; n         for (int j = 0; j < 26; j++) maior = max(maior, qnt[i][j]); n         res += 2 * m - maior; n       } n       int maior = 0; n       for (int j = 0; j < 26; j++) maior = max(maior, qnt[k / 2][j]); n       res += (2 * m - maior) / 2; n     } else { n       for (int i = 0; i < k / 2; i++) { n         for (int j = 0; j < m; j++) { n           int a = i + j * k; n           int b = k - 1 - i + j * k; n           qnt[i][c[a] -  a ]++; n           qnt[i][c[b] -  a ]++; n         } n       } n       for (int i = 0; i < k / 2; i++) { n         int maior = 0; n         for (int j = 0; j < 26; j++) maior = max(maior, qnt[i][j]); n         res += 2 * m - maior; n       } n     } n     printf( %d n , res); n     for (int i = 0; i <= n; i++) { n       for (int j = 0; j < 26; j++) qnt[i][j] = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   bool x = false; n   string s; n   cin >> s; n   string b; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  d  && s[i + 1] ==  o  && s[i + 2] ==  t  && i != 0 && n         i + 2 < s.size() - 1) { n       b +=  . ; n       s[i + 1] =    ; n       s[i + 2] =    ; n     } else if (x == false && s[i] ==  a  && s[i + 1] ==  t  && i != 0 && n                i + 1 < s.size() - 1) { n       b +=  @ ; n       s[i + 1] =    ; n       x = true; n     } else if (s[i] !=    ) { n       b += s[i]; n     } n   } n   cout << b; n   return 0; n } n


#include <bits/stdc++.h> n long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; n using namespace std; n class pa3 { n  public: n   long long x; n   long long y, z; n   pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {} n   bool operator<(const pa3 &p) const { n     if (x != p.x) return x < p.x; n     if (y != p.y) return y < p.y; n     return z < p.z; n   } n   bool operator>(const pa3 &p) const { n     if (x != p.x) return x > p.x; n     if (y != p.y) return y > p.y; n     return z > p.z; n   } n   bool operator==(const pa3 &p) const { n     return x == p.x && y == p.y && z == p.z; n   } n   bool operator!=(const pa3 &p) const { n     return !(x == p.x && y == p.y && z == p.z); n   } n }; n class pa4 { n  public: n   long long x; n   long long y, z, w; n   pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0) n       : x(x), y(y), z(z), w(w) {} n   bool operator<(const pa4 &p) const { n     if (x != p.x) return x < p.x; n     if (y != p.y) return y < p.y; n     if (z != p.z) return z < p.z; n     return w < p.w; n   } n   bool operator>(const pa4 &p) const { n     if (x != p.x) return x > p.x; n     if (y != p.y) return y > p.y; n     if (z != p.z) return z > p.z; n     return w > p.w; n   } n   bool operator==(const pa4 &p) const { n     return x == p.x && y == p.y && z == p.z && w == p.w; n   } n }; n class pa2 { n  public: n   long long x, y; n   pa2(long long x = 0, long long y = 0) : x(x), y(y) {} n   pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } n   pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } n   bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } n   bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } n   bool operator==(const pa2 &p) const { n     return abs(x - p.x) == 0 && abs(y - p.y) == 0; n   } n   bool operator!=(const pa2 &p) const { n     return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); n   } n }; n string itos(long long i) { n   ostringstream s; n   s << i; n   return s.str(); n } n long long gcd(long long v, long long b) { n   if (v > b) return gcd(b, v); n   if (v == b) return b; n   if (b % v == 0) return v; n   return gcd(v, b % v); n } n long long mod; n long long extgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = extgcd(b, a % b, y, x); n   y -= a / b * x; n   return d; n } n pair<long long, long long> operator+(const pair<long long, long long> &l, n                                      const pair<long long, long long> &r) { n   return {l.first + r.first, l.second + r.second}; n } n pair<long long, long long> operator-(const pair<long long, long long> &l, n                                      const pair<long long, long long> &r) { n   return {l.first - r.first, l.second - r.second}; n } n long long pr[200010]; n long long inv[200010]; n long long beki(long long wa, long long rr, long long warukazu) { n   if (rr == 0) return 1 % warukazu; n   if (rr == 1) return wa % warukazu; n   wa %= warukazu; n   if (rr % 2 == 1) n     return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu; n   long long zx = beki(wa, rr / 2, warukazu); n   return (zx * zx) % warukazu; n } n long long comb(long long nn, long long rr) { n   if (rr < 0 || rr > nn || nn < 0) return 0; n   long long r = pr[nn] * inv[rr]; n   r %= mod; n   r *= inv[nn - rr]; n   r %= mod; n   return r; n } n void gya(long long ert) { n   pr[0] = 1; n   for (long long i = 1; i <= ert; i++) { n     pr[i] = (pr[i - 1] * i) % mod; n   } n   inv[ert] = beki(pr[ert], mod - 2, mod); n   for (long long i = ert - 1; i >= 0; i--) { n     inv[i] = inv[i + 1] * (i + 1) % mod; n   } n } n long long ko[11] = {}; n vector<long long> p[11][11], q[11][11]; n long long ju[40]; n long long solve(pair<string, long long> X) { n   string s = X.first; n   reverse(s.begin(), s.end()); n   long long l = X.second; n   long long ans = 0; n   for (long long i = 1; i <= 10; i++) { n     long long e = 0; n     for (long long j = 0; j < l; j++) { n       e += ju[q[i][l][j]] * (s[j] -  0 ) % mod; n       e += ju[p[l][i][j]] * (s[j] -  0 ) % mod; n       e %= mod; n     } n     e *= ko[i]; n     e %= mod; n     ans += e; n     ans %= mod; n   } n   return ans; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   mod = 998244353; n   vector<pair<string, long long>> ve; n   ju[0] = 1; n   for (long long i = 1; i < 40; i++) { n     ju[i] = ju[i - 1] * 10 % mod; n   } n   for (long long l1 = 1; l1 <= 10; l1++) n     for (long long r1 = 1; r1 <= 10; r1++) { n       long long f = 0; n       for (long long i = 0;; i++) { n         if (i >= l1 && i >= r1) break; n         if (i < l1) { n           p[l1][r1].push_back(f); n           f++; n         } n         if (i < r1) { n           q[l1][r1].push_back(f); n           f++; n         } n       } n     } n   for (long long i = 0; i < n; i++) { n     long long y; n     cin >> y; n     string s = itos(y); n     ve.push_back(make_pair(s, s.length())); n     ko[s.length()]++; n   } n   long long ans = 0; n   for (auto v : ve) { n     long long r = solve(v); n     ans += r; n     ans %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e3 + 100, M = 1e9 + 7; n long long int n, m, d[mx][mx]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   d[0][0] = 1; n   for (int i = 1; i < mx; i++) { n     d[i][0] = 1; n     for (int j = 1; j < i + 1; j++) n       d[i][j] = (d[i - 1][j - 1] + d[i - 1][j]) % M; n   } n   cout << d[(m * 2) + (n - 1)][(n - 1)] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n int upmin(T1 &x, T2 v) { n   if (x <= v) { n     return 0; n   } n   x = v; n   return 1; n } n template <class T1, class T2> n int upmax(T1 &x, T2 v) { n   if (x >= v) { n     return 0; n   } n   x = v; n   return 1; n } n const int INF = 0x3f3f3f3f; n void init() { n   int N; n   cin >> N; n   map<string, string> mp, rvmp; n   for (int i = 0; i < N; ++i) { n     string a, b; n     cin >> a >> b; n     if (not rvmp.count(a)) { n       mp[a] = b; n       rvmp[b] = a; n     } else { n       mp[rvmp[a]] = b; n       rvmp[b] = rvmp[a]; n       rvmp.erase(a); n     } n   } n   cout << mp.size() << endl; n   for (auto it = mp.begin(); it != mp.end(); ++it) { n     cout << it->first <<     << it->second << endl; n   } n } n void preprocess() {} n void solve() {} n signed main() { n   ios::sync_with_stdio(0); n   init(); n   preprocess(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int abs(int x) { return x > 0 ? x : -x; } n int max(int x, int y) { return x > y ? x : y; } n int min(int x, int y) { return x < y ? x : y; } n long long read() { n   long long ans = 0; n   char ch = getchar(), t = ch; n   while (ch <  0  || ch >  9 ) { n     t = ch; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     ans = ans * 10 + ch -  0 ; n     ch = getchar(); n   } n   if (t ==  - ) ans = -ans; n   return ans; n } n using namespace std; n int main() { n   long long n = read(); n   while (n--) { n     long long l = read(), r = read(); n     long long bit = 0; n     if (l == 0 && r == 0) { n       printf( 0 n ); n       continue; n     } n     while ((1ll * 1 << bit) <= r) { n       if ((l | (1ll * 1 << bit)) > r) { n         printf( %I64d n , l); n         break; n       } n       if ((l | (1ll * 1 << bit)) == r) { n         printf( %I64d n , l | (1ll * 1 << bit)); n         break; n       } n       l = l | (1ll * 1 << bit); n       bit++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> C; n long long B[1100000], pre[1100000], suf[1100000], A[1100000], tot, i, n, n     c[1100000], ans; n void add(long long x) { n   for (; x <= n; x += (x & (-x))) c[x]++; n } n long long getsum(long long x) { n   long long sum = 0; n   for (; x >= 1; x -= (x & (-x))) sum += c[x]; n   return sum; n } n int main() { n   scanf( %I64d , &n); n   for (i = 1; i <= n; i++) cin >> A[i]; n   for (i = 1; i <= n; i++) C[A[i]]++, pre[i] = C[A[i]]; n   C.clear(); n   for (i = n; i >= 1; i--) C[A[i]]++, suf[i] = C[A[i]]; n   for (i = n; i >= 1; i--) { n     ans += getsum(pre[i] - 1); n     add(suf[i]); n   } n   printf( %I64d n , ans); n } n