#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, ans = 0; n   cin >> n; n   vector<int> v; n   int dat[101] = {}; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     if (a == 1) break; n     v.push_back(a); n   } n   if (a != 1) { n     sort(v.begin(), v.end()); n     for (int i = 0; i < v.size(); ++i) { n       if (dat[v[i]] == 0) { n         for (int j = v[i]; j < 101; j += v[i]) { n           dat[j] = 1; n         } n         ++ans; n       } n     } n   } else n     ans = 1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long p = 998244353; n long long po(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   if (b % 2 == 0) { n     long long u = po(a, b / 2); n     return (u * u) % p; n   } else { n     long long u = po(a, b - 1); n     return (a * u) % p; n   } n } n long long inv(long long x) { return po(x, p - 2); } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   sort(a, a + n); n   reverse(a, a + n); n   long long pr[n + 1]; n   pr[0] = 0; n   for (long long i = 0; i < n; ++i) pr[i + 1] = pr[i] + a[i]; n   for (long long k = 1; k <= n; ++k) { n     long long ans = 0; n     for (long long l = k; l <= n; l += k) { n       ans += (pr[l] - pr[((l - 1) / k) * k]) * ((l - 1) / k); n       ans %= p; n       if (l > (n - k) && l < n) l = (n - k); n     } n     ans *= inv(n); n     ans %= p; n     ans += p; n     ans %= p; n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solve(vector<int> ps, vector<int> v) { n   if (ps.empty()) return v[0] == 0; n   int n = v.size(); n   vector<int> nps(ps.begin() + 1, ps.end()); n   vector<vector<int> > g(ps[0], vector<int>(n / ps[0])); n   bool ans = true; n   for (int i = 0; i < n; ++i) { n     g[i % ps[0]][i % (n / ps[0])] = v[i]; n   } n   for (int i = 1; i < ps[0]; ++i) { n     for (int j = 0; j < n / ps[0]; ++j) { n       g[i][j] -= g[0][j]; n     } n     if (!solve(nps, g[i])) ans = false; n   } n   return ans; n } n int main() { n   int N; n   string S; n   cin >> N >> S; n   vector<int> ps; n   int rad = 1, x = N; n   for (int i = 2; i * i <= x; ++i) { n     if (x % i == 0) { n       rad *= i; n       ps.push_back(i); n       while (x % i == 0) x /= i; n     } n   } n   if (x > 1) rad *= x, ps.push_back(x); n   bool ans = true; n   for (int i = 0; i < N / rad; ++i) { n     vector<int> v; n     for (int j = i; j < N; j += N / rad) { n       v.push_back(S[j] -  0 ); n     } n     bool res = solve(ps, v); n     if (!res) ans = false; n   } n   cout << (ans ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int N = 77; n int n; n char s[N]; n vector<int> v, k, x; n int dp[N][N][N][2]; n int count(vector<int> &list, int from, int w) { n   int cnt = 0; n   for (int i = from; i < list.size(); i++) { n     if (list[i] < w) cnt++; n   } n   return cnt; n } n int cost(int vv, int kk, int xx, int w) { n   return count(v, vv, w) + count(k, kk, w) + count(x, xx, w); n } n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   for (int i = 0; i < n; i++) { n     if (s[i] ==  V ) n       v.push_back(i); n     else if (s[i] ==  K ) n       k.push_back(i); n     else n       x.push_back(i); 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 l = 0; l < 2; l++) dp[i][j][k][l] = 1e9; n   dp[0][0][0][0] = 0; n   for (int i = 0; i <= v.size(); i++) { n     for (int j = 0; j <= k.size(); j++) { n       for (int p = 0; p <= x.size(); p++) { n         for (int c = 0; c < 2; c++) { n           if (i < v.size()) n             dp[i + 1][j][p][1] = n                 min(dp[i + 1][j][p][1], cost(i, j, p, v[i]) + dp[i][j][p][c]); n           if (c == 0 && j < k.size()) n             dp[i][j + 1][p][0] = n                 min(dp[i][j + 1][p][0], cost(i, j, p, k[j]) + dp[i][j][p][c]); n           if (p < x.size()) n             dp[i][j][p + 1][0] = n                 min(dp[i][j][p + 1][0], cost(i, j, p, x[p]) + dp[i][j][p][c]); n         } n       } n     } n   } n   printf( %d n , min(dp[v.size()][k.size()][x.size()][0], n                      dp[v.size()][k.size()][x.size()][1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, flag = 0; n   int a[5005]; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= n; i++) { n     if (a[a[a[i]]] == i) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n const double PI = 3.1415926535897932384626433; n const int KL = 1e4 + 10; n const long long MOD = 1e9 + 7; n using namespace std; n int n; n struct point { n   long double x, y, z; n }; n long double dis(point a, point b) { n   long double dx = a.x - b.x; n   long double dy = a.y - b.y; n   long double dz = a.z - b.z; n   return sqrt(dx * dx + dy * dy + dz * dz); n } n point a[KL], p; n long double t[KL], vp, vs; n bool ok(long double mid) { n   long double ttt = 0.0; n   int pos; n   for (int i = 1; i <= n; i++) { n     ttt += t[i]; n     if (ttt >= mid) { n       pos = i; n       break; n     } n   } n   long double dx = a[pos].x - a[pos - 1].x; n   long double dy = a[pos].y - a[pos - 1].y; n   long double dz = a[pos].z - a[pos - 1].z; n   long double d = dis(a[pos], a[pos - 1]); n   long double dc = (mid - (ttt - t[pos])) * vs; n   long double sm = dc / d; n   dx = dx * sm; n   dy = dy * sm; n   dz = dz * sm; n   point c = {dx + a[pos - 1].x, dy + a[pos - 1].y, dz + a[pos - 1].z}; n   long double ds = dis(c, p); n   return (ds / mid <= vp); n } n void prnt(long double nm) { cout << fixed << setprecision(10) << nm <<    ; } n int main() { n   cin >> n; n   for (int i = 0; i <= n; i++) { n     cin >> a[i].x >> a[i].y >> a[i].z; n   } n   cin >> vp >> vs >> p.x >> p.y >> p.z; n   long double tt = 0.0; n   for (int i = 1; i <= n; i++) { n     t[i] = dis(a[i - 1], a[i]) / vs; n     tt += t[i]; n   } n   if (!ok(tt)) n     cout <<  NO  << endl; n   else { n     long double lo = 0; n     long double hi = tt; n     for (int i = 0; i < 100; i++) { n       long double mid = (lo + hi) / 2; n       if (ok(mid)) n         hi = mid; n       else n         lo = mid; n     } n     cout <<  YES  << endl; n     cout << fixed << setprecision(10) << lo << endl; n     if (lo == 0) { n       prnt(a[0].x); n       prnt(a[0].y); n       prnt(a[0].z); n       cout << endl; n       return 0; n     } n     long double ttt = 0.0; n     int pos; n     for (int i = 0; i <= n; i++) { n       ttt += t[i]; n       if (ttt >= lo) { n         pos = i; n         break; n       } n     } n     long double dx = a[pos].x - a[pos - 1].x; n     long double dy = a[pos].y - a[pos - 1].y; n     long double dz = a[pos].z - a[pos - 1].z; n     long double d = dis(a[pos], a[pos - 1]); n     long double dc = (lo - (ttt - t[pos])) * vs; n     long double sm = dc / d; n     dx = dx * sm; n     dy = dy * sm; n     dz = dz * sm; n     point c = {dx + a[pos - 1].x, dy + a[pos - 1].y, dz + a[pos - 1].z}; n     prnt(c.x); n     prnt(c.y); n     prnt(c.z); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   vector<Node*> next; n   Node() : next(26, nullptr) {} n }; n Node* root = new Node; n void insert(string& s, int i, Node* c) { n   if (i >= s.length()) return; n   if (c->next[s[i] -  a ] == nullptr) { n     c->next[s[i] -  a ] = new Node; n   } n   insert(s, i + 1, c->next[s[i] -  a ]); n } n bool traverseWin(Node* c) { n   bool curWin = false; n   for (auto n : c->next) n     if (n != nullptr) { n       if (!traverseWin(n)) curWin = true; n     } n   return curWin; n } n bool traverseLose(Node* c) { n   bool curLose = false; n   int i = 0; n   for (auto n : c->next) n     if (n != nullptr) { n       ++i; n       if (!traverseLose(n)) curLose = true; n     } n   return curLose || (i == 0); n } n int main(int, char**) { n   std::ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     insert(s, 0, root); n   } n   bool w = traverseWin(root); n   bool l = traverseLose(root); n   if (!w) { n     cout <<  Second  << endl; n   } else { n     if (!l) { n       cout << (k % 2 == 0 ?  Second  :  First ) << endl; n     } else { n       cout <<  First  << endl; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long ax, ay, bx, by, cx, cy; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> ax >> ay >> bx >> by >> cx >> cy; n   if ((by - ay) * (cx - bx) == (cy - by) * (bx - ax)) n     cout <<  NO n ; n   else if ((by - ay) * (by - ay) + (bx - ax) * (bx - ax) == n            (by - cy) * (by - cy) + (bx - cx) * (bx - cx)) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n(0); n   scanf( %d , &n); n   char first =  x , last =  y ; n   int count(0); n   if (n % 2 == 0) { n     for (int k = 0; k < n / 2; k++) { n       printf( %c%c , (count) % 2 +  a , (count) % 2 +  a ); n       ++count; n     }; n     puts(  ); n     for (int k = 0; k < n / 2; k++) { n       printf( %c%c , (count) % 2 +  c , (count) % 2 +  c ); n       ++count; n     }; n     puts(  ); n     printf( %c , first); n     for (int k = 0; k < n / 2 - 1; k++) { n       printf( %c%c , (count) % 2 +  f , (count) % 2 +  f ); n       ++count; n     }; n     printf( %c n , last); n     printf( %c , first); n     for (int k = 0; k < n / 2 - 1; k++) { n       printf( %c%c , (count) % 2 +  m , (count) % 2 +  m ); n       ++count; n     }; n     printf( %c n , last); n   } else if (n % 2 == 1) { n     char count =  b ; n     printf( %c , first); n     for (int k = 0; k < n / 2; k++) { n       printf( %c%c , (count) % 2 +  a , (count) % 2 +  a ); n       ++count; n     }; n     puts(  ); n     printf( %c , first); n     for (int k = 0; k < n / 2; k++) { n       printf( %c%c , (count) % 2 +  c , (count) % 2 +  c ); n       ++count; n     }; n     puts(  ); n     for (int k = 0; k < n / 2; k++) { n       printf( %c%c , (count) % 2 +  f , (count) % 2 +  f ); n       ++count; n     }; n     printf( %c n , last); n     for (int k = 0; k < n / 2; k++) { n       printf( %c%c , (count) % 2 +  m , (count) % 2 +  m ); n       ++count; n     }; n     printf( %c n , last); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:102400000,102400000 ) n using namespace std; n const double PI = acos(-1.0); n int f_max(int a, int b) { return a > b ? a : b; } n int f_min(int a, int b) { return a < b ? a : b; } n int f_abs(int a) { return a > 0 ? a : -a; } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int n, m; n int u, d, l, r; n int x, y; n int main() { n   int q, w, e; n   int i, j, k; n   int ans, len; n   while (~scanf( %d%d , &n, &m)) { n     len = 0x7fffffff; n     r = u = -0x7fffffff; n     l = d = 0x7fffffff; n     scanf( %d , &n); n     for (i = 0; i < n; i++) { n       scanf( %d%d , &x, &y); n       u = f_max(u, y - x); n       d = f_min(d, y - x); n       l = f_min(l, x + y); n       r = f_max(r, x + y); n     } n     scanf( %d , &m); n     int mm; n     for (i = 1; i <= m; i++) { n       scanf( %d%d , &x, &y); n       mm = 0; n       mm = f_max(mm, u - (y - x)); n       mm = f_max(mm, (y - x) - d); n       mm = f_max(mm, r - (x + y)); n       mm = f_max(mm, (x + y) - l); n       if (mm < len) { n         ans = i; n         len = mm; n       } n     } n     printf( %d n%d n , len, ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n long long mod = 1e9 + 7; n long long fmod(long long b, long long exp) { n   long long res = 1; n   while (exp) { n     if (exp & 1ll) res = (res * b) % mod; n     b = (b * b) % mod; n     exp /= 2ll; n   } n   return res; n } n long long buc[101]; n long long fac[N], inv[N]; n long long dp[N], temp_dp[N]; n long long ans[55][55]; n string s, s1, s2; n int find(char c) { n   if (c >=  A  && c <=  Z ) n     return (int)(c -  A  + 26); n   else n     return (c -  a ); n } n inline void add(long long &a, long long b) { n   a += b; n   if (a >= mod) a -= mod; n } n inline void sub(long long &a, long long b) { n   a -= b; n   if (a < 0) a += mod; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1, n, i, j, m, q; n   cin >> s; n   n = s.length(); n   for (int i = 0; i < n; i++) buc[find(s[i])]++; n   fac[0] = 1; n   for (i = 1; i <= n; i++) fac[i] = (fac[i - 1] * 1ll * i) % mod; n   inv[n] = fmod(fac[n], mod - 2); n   for (int i = n - 1; i >= 0; i--) inv[i] = (inv[i + 1] * 1ll * (i + 1)) % mod; n   long long num = (fac[n / 2] * fac[n / 2]) % mod; n   for (int i = 0; i < 52; i++) num = (num * inv[buc[i]]) % mod; n   dp[0] = 1; n   for (int i = 0; i < 52; i++) { n     if (!buc[i]) continue; n     for (int j = n; j >= buc[i]; j--) add(dp[j], dp[j - buc[i]]); n   } n   for (i = 0; i < 52; i++) ans[i][i] = dp[n / 2]; n   for (i = 0; i < 52; i++) { n     if (!buc[i]) continue; n     for (int k = 0; k <= n; k++) temp_dp[k] = dp[k]; n     for (int k = buc[i]; k <= n; k++) sub(temp_dp[k], temp_dp[k - buc[i]]); n     for (int j = i + 1; j < 52; j++) { n       if (!buc[j]) continue; n       for (int k = buc[j]; k <= n; k++) sub(temp_dp[k], temp_dp[k - buc[j]]); n       ans[i][j] = (2ll * temp_dp[n / 2]) % mod; n       ans[j][i] = ans[i][j]; n       for (int k = n; k >= buc[j]; k--) add(temp_dp[k], temp_dp[k - buc[j]]); n     } n   } n   cin >> q; n   int x, y; n   while (q--) { n     cin >> x >> y; n     int l = find(s[x - 1]); n     int r = find(s[y - 1]); n     cout << (num * ans[l][r]) % mod <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastInOut(); n unsigned long long power(unsigned long long b, unsigned long long pow) { n   if (b == 0) n     return 0; n   else if (pow == 1) n     return b; n   else if (pow % 2 == 0) n     return power(b * b, pow / 2); n   else n     return b * power(b * b, pow / 2); n } n int power(int z, int i) { n   int zz = 1; n   for (int k = 0; k < i; k++) { n     zz *= z; n   } n   return zz; n } n long long power(long long x, long long y) { n   long long z = 1; n   for (long long i = 0; i < y; i++) z *= x; n   return z; n } n bool right(int arr[]) { n   int a[3]; n   a[0] = power(arr[3] - arr[1], 2) + power(arr[2] - arr[0], 2); n   a[1] = power(arr[5] - arr[1], 2) + power(arr[4] - arr[0], 2); n   a[2] = power(arr[5] - arr[3], 2) + power(arr[4] - arr[2], 2); n   sort(a, a + 3); n   if (a[0] + a[1] == a[2] && (sqrt(a[0]) * sqrt(a[1]) * sqrt(a[2])) / 2) n     return true; n   return false; n } n bool fed(int k, int f, int test) { n   if (f < test) swap(f, test); n   int c = 0; n   while (f) { n     if ((f & 1) != (test & 1)) c++; n     f = f >> 1; n     test = test >> 1; n     if (c > k) return false; n   } n   if (c > k) return false; n   return true; n } n unsigned long long fact(long long a) { n   unsigned long long res = 1; n   for (long long i = a; i > 0; i--) res *= i; n   return res; n } n unsigned long long comb(long long a, long long b) { n   if ((a - b) < b) b = a - b; n   if (a <= 19) return fact(a) / (fact(a - b) * fact(b)); n   if (a == b || b == 0) return 1; n   if (b == 1) return a; n   return comb(a - 1, b - 1) + comb(a - 1, b); n } n bool palind(string s) { n   for (unsigned int i = 0, f = s.size() - 1; i < s.size() / 2; i++, f--) n     if (s[i] != s[f]) return false; n   return true; n } n int div(long long x) { n   double SqrtOfnumber; n   SqrtOfnumber = sqrt(x); n   int counter = 1; n   int i; n   for (i = 2; i < SqrtOfnumber; i++) { n     if (x % i == 0) counter++; n   } n   counter = counter * 2; n   if (i * i == x) counter++; n   return counter; n } n bool isprim(int x) { n   for (int i = x - 1; i > 1; i--) { n     if (x % i == 0) return false; n   } n   return true; n } n set<long long> prim(int n) { n   n++; n   bool *arr = new bool[n]; n   for (int i = 0; i < n; i++) arr[i] = true; n   set<long long> se; n   for (int i = 2; i * i < n; i++) n     if (arr[i]) n       for (int k = i * i; k < n; k += i) arr[k] = false; n   for (int i = 2; i < n; i++) n     if (arr[i]) se.insert(i); n   return se; n } n int n, m; n char arr[1010][1010]; n bool pash(int x, int y) { n   int s; n   s = 1; n   if (x + 1 < n && arr[x + 1][y] ==  b ) s++; n   if (x + 1 < n && y + 1 < m && arr[x + 1][y + 1] ==  b ) s++; n   if (y + 1 < m && arr[x][y + 1] ==  b ) s++; n   if (s == 4) return true; n   s = 1; n   if (x - 1 >= 0 && arr[x - 1][y] ==  b ) s++; n   if (x - 1 >= 0 && y + 1 < m && arr[x - 1][y + 1] ==  b ) s++; n   if (y + 1 < m && arr[x][y + 1] ==  b ) s++; n   if (s == 4) return true; n   s = 1; n   if (x - 1 >= 0 && arr[x - 1][y] ==  b ) s++; n   if (x - 1 >= 0 && y - 1 >= 0 && arr[x - 1][y - 1] ==  b ) s++; n   if (y - 1 >= 0 && arr[x][y - 1] ==  b ) s++; n   if (s == 4) return true; n   s = 1; n   if (x + 1 < n && arr[x + 1][y] ==  b ) s++; n   if (x + 1 < n && y - 1 >= 0 && arr[x + 1][y - 1] ==  b ) s++; n   if (y - 1 >= 0 && arr[x][y - 1] ==  b ) s++; n   if (s == 4) return true; n   return false; n } n int main() { n   fastInOut(); n   int n, m, x; n   string res =  YES ; n   cin >> n >> m; n   vector<int> v; n   for (int i = 0; i < m; i++) { n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   if (v.size()) { n     if (*min_element(v.begin(), v.end()) == 1 || n         *max_element(v.begin(), v.end()) == n) n       res =  NO ; n     else n       for (int i = 0; i < m - 2; i++) n         if (v[i + 1] - v[i] == 1 && v[i + 2] - v[i] == 2) { n           res =  NO ; n           break; n         } n   } n   cout << res; n   return 0; n } n void fastInOut() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } n


#include <bits/stdc++.h> n const double PI = acos(-1.0); n const int INF = 0x3f3f3f3f; n const double eps = 1e-8; n const int MAXN = 1e3 + 10; n const long long MOD = 1e9 + 7; n int cases = 0; n using std::string; n const int dir[][2] = {{-1, 0}, {-1, -1}, {-1, 1}, {0, -1}, n                       {0, 1},  {1, -1},  {1, 0},  {1, 1}}; n int next[MAXN]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   while (m--) { n     int t, k, d; n     std::vector<int> tmp; n     scanf( %d%d%d , &t, &k, &d); n     int cnt = 0; n     int res = 0; n     for (int i = 1; i <= n; ++i) { n       if (next[i] <= t) { n         tmp.push_back(i); n       } n       if (tmp.size() == k) { n         for (int u : tmp) { n           next[u] = t + d; n           res += u; n         } n         break; n       } n     } n     printf( %d n , res == 0 ? -1 : res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const int maxn = 3e5 + 5; n int level[maxn * 2], num[maxn * 2], par[maxn * 2]; n char s[maxn * 2]; n stack<int> stk; n int main() { n   int n, cnt = 0; n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) { n     if (stk.empty()) n       stk.push(i); n     else { n       int c = stk.top(); n       if (s[i] ==  )  && s[c] ==  ( ) n         stk.pop(); n       else n         stk.push(i); n     } n     s[i + n] = s[i]; n     cnt = cnt + ((s[i] ==  ( ) ? (1) : (-1)); n   } n   if (cnt) return 0 & printf( 0 n1 1 n ); n   int j = 1; n   while (!stk.empty()) { n     int c = stk.top(); n     if (s[c] ==  ) ) break; n     stk.pop(); n     j = c; n   } n   for (int i = 1; i <= n * 2; i++) level[i] = -1, num[i] = 0; n   while (!stk.empty()) stk.pop(); n   for (int i = j; i <= j + n - 1; i++) { n     if (stk.empty()) { n       stk.push(i); n       level[i] = 1; n       cnt++; n     } else { n       int c = stk.top(); n       if (s[i] ==  )  && s[c] ==  ( ) { n         stk.pop(); n         par[c] = i; n       } else { n         stk.push(i); n         level[i] = stk.size(); n       } n     } n   } n   int i = j; n   while (i <= j + n - 1) { n     if (level[i] == 1) { n       int r = par[i], k = i; n       while (i < r) { n         if (level[i] == 2) num[k]++; n         i++; n       } n     } n     i++; n   } n   i = j; n   while (i <= j + n - 1) { n     if (level[i] == 2) { n       int r = par[i], k = i; n       while (i < r) { n         if (level[i] == 3) num[k]++; n         i++; n       } n     } n     i++; n   } n   int ans = 0, l, r; n   for (int i = j; i <= j + n - 1; i++) { n     if (level[i] == 1) { n       if (ans < (num[i] + 1)) { n         ans = num[i] + 1; n         l = i % n; n         r = par[i] % n; n       } n     } else if (level[i] == 2) { n       if (ans < (cnt + num[i] + 1)) { n         ans = cnt + num[i] + 1; n         l = i % n; n         r = par[i] % n; n       } n     } n   } n   if (ans < cnt) ans = cnt, l = 1, r = 1; n   if (l == 0) l = n; n   if (r == 0) r = n; n   printf( %d n%d %d n , ans, l, r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n int n, k, i, a, b, da, db, u, v, head[N], Next[N * 2], adj[N * 2], j, dp[N], n     ans, dd, t; n void Push(int u, int v) { n   Next[++k] = head[u]; n   head[u] = k; n   adj[k] = v; n } n void dfs(int i, int fa) { n   int j, mxx = 0; n   dp[i] = 0; n   for (j = head[i]; j; j = Next[j]) n     if (adj[j] != fa) { n       dfs(adj[j], i); n       if (dp[i] < dp[adj[j]] + 1) { n         mxx = max(mxx, dp[i]); n         dp[i] = dp[adj[j]] + 1; n       } else n         mxx = max(mxx, dp[adj[j]] + 1); n     } n   ans = max(ans, mxx + dp[i]); n } n void dis(int i, int fa, int d) { n   int j; n   for (j = head[i]; j; j = Next[j]) n     if (adj[j] != fa) dis(adj[j], i, d + 1); n   if (i == b) dd = d; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d %d %d %d , &n, &a, &b, &da, &db); n     for (i = 1; i <= n; ++i) head[i] = 0; n     k = 0; n     for (i = 1; i < n; ++i) { n       scanf( %d %d , &u, &v); n       Push(u, v); n       Push(v, u); n     } n     if (db <= da * 2) { n       puts( Alice ); n       continue; n     } n     ans = 0; n     dfs(1, 0); n     dis(a, 0, 0); n     if (dd <= da || ans <= da * 2) n       puts( Alice ); n     else n       puts( Bob ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cerr.tie(0); n   int n, tmp; n   vector<pair<int, int> > a; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> tmp, a.push_back(make_pair(tmp, i + 1)); n   sort(a.begin(), a.end()); n   int c1 = 0, c2 = 0, s1 = 0, s2 = 0; n   vector<int> t1, t2; n   int l = 0, r = n - 1; n   while (l <= r) { n     if (s1 > s2) { n       if (c2 - c1 <= 0) { n         c2++; n         s2 += a[r].first; n         t2.push_back(a[r].second); n         r--; n       } else { n         c1++; n         s1 += a[l].first; n         t1.push_back(a[l].second); n         l++; n       } n     } else { n       if (c2 - c1 <= 0) { n         c2++; n         s2 += a[l].first; n         t2.push_back(a[l].second); n         l++; n       } else { n         c1++; n         s1 += a[r].first; n         t1.push_back(a[r].second); n         r--; n       } n     } n   } n   cout << c1 << endl; n   for (int i = 0; i < c1; i++) cout << t1[i] <<    ; n   cout << endl; n   cout << c2 << endl; n   for (int i = 0; i < c2; i++) cout << t2[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n int c[maxn], A[maxn], B[maxn]; n int n, k, a, b, q, cmd; n void add(int *c, int p, int val) { n   for (; p <= n; p += p & -p) c[p] += val; n } n int sum(int *c, int p) { n   int ret = 0; n   for (; p; p -= p & -p) ret += c[p]; n   return ret; n } n int main() { n   cin >> n >> k >> a >> b >> q; n   while (q--) { n     int x, y; n     scanf( %d , &cmd); n     if (cmd == 1) { n       scanf( %d%d , &x, &y); n       add(A, x, -min(c[x], a)); n       add(B, x, -min(c[x], b)); n       c[x] += y; n       add(A, x, min(c[x], a)); n       add(B, x, min(c[x], b)); n     } else { n       scanf( %d , &x); n       int ans = 0; n       ans += sum(B, x - 1); n       ans += sum(A, n) - sum(A, x + k - 1); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2 * 1e6 + 10; n long long a[maxn], b[maxn], q[maxn], sz[maxn], n; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n long long b1, b2, n1; n signed main() { n   n = read(); n   long long p = 0; n   for (long long i = 1; i <= n; i++) { n     a[i] = read(); n     if (a[i] > a[p]) p = i; n     if (a[i] > b1) n       b2 = b1, b1 = a[i], n1 = 1; n     else if (a[i] == b1) n       n1++; n     else if (a[i] > b2) n       b2 = a[i]; n   } n   for (long long i = 1; i <= n; i++) b[i] = a[(i + p - 1) % n + 1]; n   long long l = 1, r = 0, ans = 0; n   for (long long i = 1; i <= n; i++) { n     while (l <= r && b[i] > b[q[r]]) r--, ans++; n     if (r >= l) { n       if (b[i] == b[q[r]]) n         ans += min(r, sz[r] + 1); n       else n         ans++; n     } n     q[++r] = i; n     if (b[i] == b[q[r - 1]]) n       sz[r] = sz[r - 1] + 1; n     else n       sz[r] = 1; n   } n   p = 0; n   for (long long i = 1; i < n; i++) n     if (b[i] >= b[p]) { n       p = i; n       if (n1 > 1 && b[p] == b1) n         ; n       else if (n1 == 1 && b[p] == b2) n         ; n       else n         ans++; n     } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max = 50; n int row[Max][Max] = {0}, dp[Max][Max][Max][Max] = {0}, a[Max][Max]; n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %1d , &a[i][j]); n       row[i][j] = row[i][j - 1] + 1; n       if (a[i][j] == 1) row[i][j] = 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       for (int k = i; k <= n; k++) { n         for (int l = j; l <= m; l++) { n           dp[i][j][k][l] = n               dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1]; n           int r = l - j + 1; n           for (int z = k; z >= i; z--) { n             r = min(r, row[z][l]); n             dp[i][j][k][l] += r; n           } n         } n       } n     } n   } n   while (q--) { n     int a, b, c, d; n     scanf( %d%d%d%d , &a, &b, &c, &d); n     cout << dp[a][b][c][d] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n, k; n int siz[100086]; n int c, x; n int id[100086]; n int mx; n char s[100086]; n inline int get(int l, int r) { n   printf( ? %d , r - l + 1), fflush(stdout); n   for (int i = l; i <= r; i++) printf(  %d , i), fflush(stdout); n   puts(  ); n   fflush(stdout); n   int x; n   scanf( %d , &x); n   return x; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= k; i++) siz[i] = 0; n     for (int i = 1; i <= n; i++) id[i] = 0; n     for (int i = 1; i <= k; i++) { n       scanf( %d , &c); n       while (c--) { n         scanf( %d , &x); n         id[x] = i; n         siz[i]++; n       } n     } n     mx = get(1, n); n     int l = 1, r = n, mid; n     while (l < r) { n       mid = l + r >> 1; n       x = get(l, mid); n       if (x == mx) n         r = mid; n       else n         l = mid + 1; n     } n     if (!id[l]) n       x = mx; n     else { n       printf( ? %d , n - siz[id[l]]), fflush(stdout); n       for (int i = 1; i <= n; i++) n         if (id[i] != id[l]) printf(  %d , i), fflush(stdout); n       puts(  ); n       fflush(stdout); n       scanf( %d , &x); n     } n     printf( ! ), fflush(stdout); n     for (int i = 1; i <= k; i++) { n       if (i == id[l]) n         printf(  %d , x); n       else n         printf(  %d , mx); n       fflush(stdout); n     } n     puts(  ); n     fflush(stdout); n     scanf( %s , s); n     if (s[0] ==  I ) return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long N = 3e5 + 5; n const long long INF = 1e14; n vector<long long> adj[33]; n int dirx[] = {-1, 1, 0, 0}; n int diry[] = {0, 0, -1, 1}; n long long ans[33][2]; n void dfs(int u, int p, int dir, long long dist) { n   int d = 0; n   for (long long i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == p) continue; n     if (dir == d) d++; n     ans[v][0] = ans[u][0] + dist * dirx[d]; n     ans[v][1] = ans[u][1] + dist * diry[d]; n     dfs(v, u, d ^ 1, (dist >> 1)); n     d++; n   } n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   int a, b; n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (long long i = 0; i < n; i++) n     if (adj[i].size() > 4) { n       cout <<  NO n ; n       return 0; n     } n   ans[0][0] = 0; n   ans[0][1] = 0; n   dfs(0, -1, -1, (1 << 30)); n   cout <<  YES n ; n   for (long long i = 0; i < n; i++) n     cout << ans[i][0] <<     << ans[i][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Max(int a, int b) { return (a > b ? a : b); } n inline int Min(int a, int b) { return (a > b ? b : a); } n int dp[45][65][65]; n int cnt[65], K; n void Init() { n   for (int i = 0; i < 65; i++) { n     int n = i; n     int x = 0; n     while (n) { n       x++; n       n &= (n - 1); n     } n     cnt[i] = x; n   } n } n int get(int num) { n   int k = K - 1; n   return (num | ((num >> 1)) | ((num << 1) & k)); n } n int main() { n   int i, j, k, t; n   int a, b, c, x, y, z; n   int n, m; n   Init(); n   while (scanf( %d%d , &n, &m) != EOF) { n     if (n > m) swap(n, m); n     K = (1 << n); n     if (m == 1 && n == 1) { n       puts( 0 ); n       continue; n     } n     for (i = 0; i < K; i++) n       for (j = 0; j < K; j++) { n         if ((i | get(j)) == K - 1) { n           dp[2][i][j] = cnt[i] + cnt[j]; n         } else n           dp[2][i][j] = 100000000; n       } n     for (i = 3; i <= m; i++) n       for (j = 0; j < K; j++) n         for (k = 0; k < K; k++) { n           dp[i][j][k] = 100000000; n           for (t = 0; t < K; t++) { n             int my = j | get(k) | t; n             if (dp[i - 1][k][t] == 100000000 || my != K - 1) continue; n             dp[i][j][k] = Min(dp[i][j][k], dp[i - 1][k][t] + cnt[j]); n           } n         } n     int ans = 100000000; n     for (j = 0; j < K; j++) n       for (k = 0; k < K; k++) n         for (t = 0; t < K; t++) { n           if ((get(t | j) | k) != K - 1) continue; n           ans = Min(ans, dp[m][j][k] + cnt[t]); n         } n     printf( %d n , n * m - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ms { n   string s; n   int id; n }; n bool operator<(const ms &a, const ms &b) { return a.s < b.s; } n vector<ms> sv, msv[11]; n string s; n int n, lenmin = 11, lenmax = 0; n bool used[10000] = {false}; n int main(void) { n   int i; n   cin >> n; n   sv.reserve(n); n   for (i = 0; i < n; ++i) { n     cin >> s; n     if ((int)s.length() > lenmax) lenmax = s.length(); n     if ((int)s.length() < lenmin) lenmin = s.length(); n     ms sid = {s, i}; n     sv.push_back(sid); n     msv[s.length()].push_back(sid); n   } n   cin >> s; n   for (i = 0; i < n; ++i) sv[i].s.append(s); n   sort(sv.begin(), sv.end()); n   for (i = 1; i <= 10; ++i) { n     sort(msv[i].begin(), msv[i].end()); n     reverse(msv[i].begin(), msv[i].end()); n   } n   lenmax = lenmax + lenmin; n   for (i = 0; i < n; ++i) n     if (!used[sv[i].id]) { n       cout << sv[i].s; n       used[sv[i].id] = true; n       lenmin = lenmax - sv[i].s.length() + 1; n       while (used[msv[lenmin].back().id]) msv[lenmin].pop_back(); n       cout << msv[lenmin].back().s <<   n ; n       used[msv[lenmin].back().id] = true; n       msv[lenmin].pop_back(); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Szukaj(vector<int>& moje, vector<bool>& jest, int x) { n   int nr = -1; n   for (int i = 0; i < moje.size(); ++i) { n     if (moje[i] >= x && jest[i]) { n       if (nr == -1) n         nr = i; n       else { n         if (moje[i] < moje[nr]) nr = i; n       } n     } n   } n   return nr; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   vector<int> atk, def; n   for (int i = 0; i < n; ++i) { n     string op; n     int s; n     cin >> op >> s; n     if (op ==  ATK ) n       atk.push_back(s); n     else n       def.push_back(s); n   } n   vector<int> moje(m); n   for (int i = 0; i < m; ++i) cin >> moje[i]; n   int odp = 0; n   sort(atk.begin(), atk.end()); n   sort(moje.begin(), moje.end()); n   reverse(moje.begin(), moje.end()); n   for (int i = 1; i <= min(atk.size(), moje.size()); ++i) { n     vector<int> a, b; n     for (int j = 0; j < i; ++j) a.push_back(moje[j]); n     for (int j = i - 1; j >= 0; --j) b.push_back(atk[j]); n     int s = 0; n     bool ok = true; n     for (int j = 0; j < i; ++j) { n       if (a[j] >= b[j]) n         s += a[j] - b[j]; n       else n         ok = false; n     } n     if (ok) odp = max(odp, s); n   } n   bool ok = true; n   int s = 0; n   vector<bool> jest(moje.size(), true); n   sort(def.begin(), def.end()); n   for (int i = 0; i < def.size(); ++i) { n     int nr = Szukaj(moje, jest, def[i] + 1); n     if (nr != -1) n       jest[nr] = false; n     else n       ok = false; n   } n   for (int i = 0; i < atk.size(); ++i) { n     int nr = Szukaj(moje, jest, atk[i]); n     if (nr != -1) { n       jest[nr] = false; n       s += moje[nr] - atk[i]; n     } else n       ok = false; n   } n   for (int i = 0; i < moje.size(); ++i) n     if (jest[i]) s += moje[i]; n   if (ok) odp = max(odp, s); n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int d, s; n   int id; n }; n node a[100009], b[100009]; n map<int, int> Map; n vector<int> e[100009], vec; n int match[100009]; n int vis[100009]; n int n, m; n bool cmp(node x, node y) { return x.d > y.d; } n bool find(int x) { n   for (int i = 0; i < e[x].size(); i++) { n     if (match[e[x][i]] == x) continue; n     if (match[e[x][i]] == 0 || find(match[e[x][i]])) { n       match[e[x][i]] = x; n       return true; n     } n   } n   return false; n } n int main() { n   int d, s; n   scanf( %d , &n); n   memset(match, 0, sizeof(match)); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i].d, &a[i].s); n     a[i].id = i; n     Map[a[i].s] = i; n   } n   cin >> m; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &d, &s); n     if (Map.find(s) != Map.end()) { n       if (a[Map[s]].d <= d) e[Map[s]].push_back(i); n     } n     if (Map.find(s + 1) != Map.end()) { n       if (a[Map[s + 1]].d <= d) e[Map[s + 1]].push_back(i); n     } n   } n   sort(a + 1, a + 1 + n, cmp); n   long long ans1 = 0, ans2 = 0; n   for (int i = 1; i <= n; i++) { n     if (find(a[i].id)) { n       ans1++; n       ans2 += a[i].d; n     } n   } n   cout << ans2 << endl; n   cout << ans1 << endl; n   for (int i = 1; i <= m; i++) { n     if (match[i] != 0) cout << i <<     << match[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = (1 << 18) + 5; n const int MAXM = 1005; n bitset<MAXM> STree[MAXN], Prime, mask; n int Lazy[MAXN], a[MAXN], in[MAXN], out[MAXN], time_tag, n, m, H[MAXN]; n vector<int> G[MAXN]; n void dfs(int i, int fa) { n   in[i] = ++time_tag; n   H[in[i]] = i; n   for (int v : G[i]) { n     if (v == fa) continue; n     dfs(v, i); n   } n   out[i] = time_tag; n } n void init(int k, int l, int r) { n   if (r == l) return; n   if (r - l == 1) { n     STree[k][a[H[l]]] = 1; n     return; n   } n   int m = l + r >> 1; n   init(k << 1, l, m); n   init(k << 1 | 1, m, r); n   STree[k] = STree[k << 1] | STree[k << 1 | 1]; n   return; n } n void inline push_down(int k) { n   if (Lazy[k]) { n     (Lazy[k << 1] += Lazy[k]) %= m; n     (Lazy[k << 1 | 1] += Lazy[k]) %= m; n     STree[k << 1] = ((STree[k << 1] << Lazy[k]) & mask) | n                     ((STree[k << 1] >> (m - Lazy[k])) & mask); n     STree[k << 1 | 1] = ((STree[k << 1 | 1] << Lazy[k]) & mask) | n                         ((STree[k << 1 | 1] >> (m - Lazy[k])) & mask); n     Lazy[k] = 0; n   } n } n void inline maintain(int k) { n   STree[k] = STree[k << 1] | STree[k << 1 | 1]; n   return; n } n void modify(int k, int l, int r, int ql, int qr, int val) { n   if (ql >= r || qr <= l) return; n   if (ql <= l && r <= qr) { n     (Lazy[k] += val) %= m; n     STree[k] = ((STree[k] << val) & mask) | ((STree[k] >> (m - val)) & mask); n     return; n   } n   push_down(k); n   int m = l + r >> 1; n   modify(k << 1, l, m, ql, qr, val); n   modify(k << 1 | 1, m, r, ql, qr, val); n   maintain(k); n } n bitset<MAXM> query(int k, int l, int r, int ql, int qr) { n   if (ql >= r || qr <= l) return bitset<MAXM>(); n   if (ql <= l && r <= qr) return STree[k]; n   push_down(k); n   int m = l + r >> 1; n   bitset<MAXM> ret = n       query(k << 1, l, m, ql, qr) | query(k << 1 | 1, m, r, ql, qr); n   maintain(k); n   return ret; n } n int main() { n   Prime = ~Prime; n   Prime[0] = Prime[1] = 0; n   for (int i = (2); i < (1001); i++) { n     if (Prime[i]) { n       for (int j = i + i; j <= 1000; j += i) { n         Prime[j] = 0; n       } n     } n   } n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (m); i++) mask[i] = 1; n   for (int i = (1); i < (n + 1); i++) scanf( %d , a + i), a[i] %= m; n   for (int i = (1); i < (n); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v), G[v].push_back(u); n   } n   dfs(1, -1); n   init(1, 1, time_tag + 1); n   int q; n   scanf( %d , &q); n   for (int cas = (0); cas < (q); cas++) { n     int op, v, x; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%d , &v, &x); n       modify(1, 1, time_tag + 1, in[v], out[v] + 1, x % m); n     } else { n       scanf( %d , &v); n       bitset<MAXM> res = Prime & query(1, 1, time_tag + 1, in[v], out[v] + 1); n       printf( %d n , res.count()); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 600 + 5; n const int M = 2e5 + 5; n template <class T> n inline void getin(T& num) { n   char c; n   bool flag = 0; n   num = 0; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag = 1; n   while (c >=  0  && c <=  9 ) { n     num = num * 10 + c - 48; n     c = getchar(); n   } n   if (flag) num = -num; n } n int n, m, u, v, In[N]; n int tot, x[N], y[N], Out[N]; n int cnt, fir[N], tar[M], nxt[M]; n long long p, f[N][N], a[N][N]; n inline void link(int a, int b) { n   tar[++cnt] = b, In[b]++, Out[a]++; n   nxt[cnt] = fir[a], fir[a] = cnt; n } n template <class T> n inline void add(T& a, T b) { n   a += b; n   if (a >= p) a -= p; n } n inline void Dp() { n   queue<int> q; n   int c = 0; n   for (int i = 1; i <= n; i++) { n     if (!In[i]) x[++tot] = i; n     if (!Out[i]) y[++c] = i; n   } n   for (int i = 1; i <= n; i++) f[i][i] = 1; n   for (int i = 1; i <= n; i++) n     if (!In[i]) q.push(i); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = fir[u]; i; i = nxt[i]) { n       int v = tar[i]; n       for (int i = 1; i <= n; i++) add(f[i][v], f[i][u]); n       In[v]--; n       if (!In[v]) q.push(v); n     } n   } n   for (int i = 1; i <= tot; i++) n     for (int j = 1; j <= tot; j++) a[i][j] = f[x[i]][y[j]]; n } n inline int calc(int n) { n   int ret = 1; n   for (int i = 1; i <= n; i++) { n     if (a[i][i] < 0) { n       ret = -ret; n       for (int k = i; k <= n; k++) a[i][k] = -a[i][k]; n     } n     for (int j = i + 1; j <= n; j++) { n       for (int k = i; k <= n; k++) a[i][k] %= p, a[j][k] %= p; n       while (a[j][i]) { n         if (a[j][i] < 0) { n           ret = -ret; n           for (int k = i; k <= n; k++) a[j][k] = -a[j][k]; n         } n         int t = a[i][i] / a[j][i]; n         for (int k = i; k <= n; k++) a[i][k] = (a[i][k] - t * a[j][k]) % p; n         for (int k = i; k <= n; k++) swap(a[i][k], a[j][k]); n         ret = -ret; n       } n     } n     if (a[i][i] == 0) return 0; n     ret = 1ll * ret * a[i][i] % p; n   } n   return (ret + p) % p; n } n int main() { n   getin(n), getin(m), getin(p); n   for (int i = 1; i <= m; i++) getin(u), getin(v), link(u, v); n   Dp(); n   cout << calc(tot) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool is_right(complex<long long> p1, complex<long long> p2, n               complex<long long> p3) { n   return (conj(p2 - p1) * (p3 - p1)).imag() < 0; n } n const int N = 1e5 + 5; n vector<complex<long long> > U; n complex<long long> a[N]; n int n; n void read_input() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     a[i] = complex<long long>(x, y - 1LL * x * x); n   } n } n void solve() { n   sort(a, a + n, [](complex<long long> a, complex<long long> b) { n     return make_pair(a.real(), a.imag()) < make_pair(b.real(), b.imag()); n   }); n   for (int i = 0; i < n; U.push_back(a[i++])) n     while (U.size() > 1 && !is_right(U[U.size() - 2], U.back(), a[i])) n       U.pop_back(); n   cout << U.size() - (U.size() > 1 && U[0].real() == U[1].real()) - 1; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   read_input(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chkmin(T& x, T y) { n   return x > y ? x = y, true : false; n } n template <class T> n inline bool chkmax(T& x, T y) { n   return x < y ? x = y, true : false; n } n inline int read(void) { n   int x, f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return x * f; n } n template <typename T> n struct Graph { n   enum { maxn = 305, maxm = 200000 }; n   struct Edge { n     int to, nxt; n     T w, cap, flow; n   } v[maxm]; n   int h[maxn], cnt; n   Graph() { memset(h, -1, sizeof(h)); } n   void add1(int x, int y, T w, T cap) { n     v[cnt] = Edge{y, h[x], w, cap, 0}; n     h[x] = cnt++; n   } n   void add(int x, int y, T w, T cap) { n     add1(x, y, w, cap); n     add1(y, x, -w, 0); n   } n }; n Graph<int> G; n int Ss, Se; n static int f[305]; n int dfs(int x, int re) { n   if (x == Se || re <= 0) return re; n   int s1 = 0, t; n   for (int i = G.h[x]; i != -1; i = G.v[i].nxt) { n     int to = G.v[i].to; n     if (f[to] != f[x] + 1) continue; n     t = dfs(to, min(re, G.v[i].cap - G.v[i].flow)); n     G.v[i].flow += t; n     G.v[i ^ 1].flow -= t; n     s1 += t; n     re -= t; n     if (re == 0) break; n   } n   return s1; n } n bool bfs(int x) { n   queue<int> Q; n   memset(f, -1, sizeof(f)); n   Q.push(x); n   f[x] = 0; n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int i = G.h[u]; i != -1; i = G.v[i].nxt) { n       if (G.v[i].flow >= G.v[i].cap) continue; n       int to = G.v[i].to; n       if (f[to] != -1) continue; n       f[to] = f[u] + 1; n       Q.push(to); n     } n   } n   return f[Se] != -1; n } n int dinic() { n   int ans = 0; n   while (bfs(Ss)) ans += dfs(Ss, 1000000000); n   return ans; n } n bool vi[305]; n int n, lk[305], v, x, ans, w; n vector<int> V[305]; n bool dfs(int x) { n   vi[x] = 1; n   for (int i = 0; i <= V[x].size() - 1; ++i) { n     int to = V[x][i]; n     if (!lk[to] || (!vi[lk[to]] && dfs(lk[to]))) { n       lk[to] = x; n       return 1; n     } n   } n   return 0; n } n int main(void) { n   n = read(); n   for (int i = 1; i <= n; ++i) { n     v = read(); n     while (v--) { n       x = read(); n       V[i].push_back(x); n     } n   } n   for (int i = 1; i <= n; ++i) { n     memset(vi, 0, sizeof(vi)); n     dfs(i); n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= V[i].size() - 1; ++j) n       G.add(i, lk[V[i][j]], 0, 1000000000); n   Ss = n + 1; n   Se = n + 2; n   for (int i = 1; i <= n; ++i) { n     w = -read(); n     if (w < 0) n       G.add(i, Se, 0, -w); n     else n       G.add(Ss, i, 0, w), ans += w; n   } n   printf( %d n , -(ans - dinic())); n   return 0; n } n


#include <bits/stdc++.h> n int valid(char prev, char next) { n   char s[] =  aeioun ; n   for (int i = 0; i < 6; i++) n     if (prev == s[i]) return 1; n   for (int i = 0; i < 5; i++) n     if (next == s[i]) return 1; n   return 0; n } n int main() { n   char s[300]; n   scanf( %s , s); n   int v = 1; n   for (int i = 0; s[i] !=   0 ; i++) v &= valid(s[i], s[i + 1]); n   printf( %s n , v ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<long long, long long>, pair<long long, long long> > mp; n long long n; n pair<long long, long long> solve(pair<long long, long long> a) { n   if (a.second < 10) n     return make_pair(a.first || a.second, a.second - max(a.first, a.second)); n   if (mp[a].first) return mp[a]; n   long long ans = 0, p = 1; n   while (a.second >= p * 10) p *= 10; n   pair<long long, long long> b = n       solve(make_pair(max(a.first, a.second / p), a.second % p)); n   ans += b.first; n   pair<long long, long long> c = n       solve(make_pair(a.first, a.second / p * p + b.second)); n   ans += c.first; n   return mp[a] = make_pair(ans, c.second); n } n int main() { n   cin >> n; n   cout << solve(make_pair(0, n)).first; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n int n1, n2, n3; n int t1, t2, t3; n int v1[3000]; n int v2[3030]; n int v3[3030]; n int main() { n   ios::sync_with_stdio(false); n   cin >> k >> n1 >> n2 >> n3 >> t1 >> t2 >> t3; n   int time = t1 + t2 + t3 - 1; n   int pos = 0; n   int res = 0; n   for (int i = 0; i <= time + 1; i++) { n     v1[i] = 0; n     v2[i] = 0; n     v3[i] = 0; n   } n   for (int i = 1; i <= k; i++) { n     int posnew = -1; n     for (int j = pos; j <= pos + time + 1; j++) { n       int kk = j - pos; n       if ((v1[kk] + 1 <= n1) && n           ((kk + t1 > time + 1) || (v2[kk + t1] + 1 <= n2)) && n           ((kk + t1 + t2 > time + 1) || (v3[kk + t1 + t2] + 1 <= n3))) { n         posnew = j; n         break; n       } n     } n     int t = min(pos + time + 1, posnew + time); n     for (int j = posnew; j <= t; j++) { n       if (j <= posnew + t1 - 1) n         v1[j - pos]++; n       else if (j <= posnew + t1 + t2 - 1) n         v2[j - pos]++; n       else n         v3[j - pos]++; n       v1[j - posnew] = v1[j - pos]; n       v2[j - posnew] = v2[j - pos]; n       v3[j - posnew] = v3[j - pos]; n     } n     for (int j = t + 1; j <= posnew + time; j++) { n       if (j <= posnew + t1 - 1) { n         v1[j - posnew] = 1; n         v2[j - posnew] = 0; n         v3[j - posnew] = 0; n       } else if (j <= posnew + t1 + t2 - 1) { n         v2[j - posnew] = 1; n         v1[j - posnew] = 0; n         v3[j - posnew] = 0; n       } else { n         v3[j - posnew] = 1; n         v1[j - posnew] = 0; n         v2[j - posnew] = 0; n       } n     } n     v1[time + 1] = 0; n     v2[time + 1] = 0; n     v3[time + 1] = 0; n     res = posnew + time; n     pos = posnew; n   } n   cout << res + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define int int64_t n #define MOD 998244353 n #define inf 9e18 n #define tikoo (int) (1e9 + 7) n  n template<typename T, typename T1> T max(T a, T1 b) { if (b > a) a = b; return a; } n template<typename T, typename T1> T min(T a, T1 b) { if (b < a) a = b; return a; } n int fastpow(int a, int b, int m = tikoo) { int res = 1; a %= m; n     while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; } n  n signed main() { n     ios_base::sync_with_stdio(0); n     cin.tie(0), cout.tie(0); n     int t = 1; n     cin >> t; n     while (t--) { n         int n, k; n         cin >> n >> k; n         cout << fastpow(n, k) <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void cppio() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n const int maxn = int(2e5) + 10; n int n; n int a[maxn]; n int p, q; n long long first, second; n int gcd(int a, int b) { n   if (!b) { n     first = 1, second = 0; n     return a; n   } n   int q = a / b; n   int ret = gcd(b, a % b); n   tie(first, second) = make_pair(second, first - second * q); n   return ret; n } n int ans[maxn][3]; n int main() { n   cppio(); n   cin >> n; n   for (int i = 1; i <= (n); ++i) cin >> a[i]; n   if (n == 1) { n     cout <<  1 1 n  << (-a[1]) <<   n1 1 n0 n1 1 n0 n ; n     return 0; n   } n   cout << 1 <<     << n <<   n0  ; n   for (int i = 2; i <= n; ++i) { n     cout << (-n * 1ll * a[i]) <<    ; n   } n   cout <<   n ; n   cout << 1 <<     << 1 <<   n ; n   cout << (-a[1]) <<   n ; n   cout << 2 <<     << n <<   n ; n   for (int i = 2; i <= n; ++i) { n     int first = a[i]; n     cout << ((n - 1) * 1ll * first) <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], dist[100005], sad[100005], subt[100005], ans = 0LL; n vector<pair<long long, long long> > adj[100005]; n void dfs(long long node, long long par, long long min_vertex) { n   if (par != -1) sad[node] = (a[node] < dist[node] - dist[min_vertex]); n   min_vertex = (dist[node] < dist[min_vertex]) ? node : min_vertex; n   for (auto i : adj[node]) n     if (par != i.first) { n       dist[i.first] += (dist[node] + i.second); n       dfs(i.first, node, min_vertex); n       subt[node] += subt[i.first]; n     } n   subt[node]++; n } n void dfs1(long long node, long long par) { n   if (sad[node]) { n     ans += subt[node]; n     return; n   } n   for (auto i : adj[node]) n     if (i.first != par) dfs1(i.first, node); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     long long n, i, j, k; n     cin >> n; n     for (i = 1; i <= n; i++) cin >> a[i]; n     for (i = 1; i <= n - 1; i++) { n       long long x, wt; n       cin >> x >> wt; n       adj[i + 1].push_back(make_pair(x, wt)); n       adj[x].push_back(make_pair(i + 1, wt)); n     } n     dfs(1, -1, 1); n     dfs1(1, -1); n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n const int M = 128; n const int inf = 0x7fffffff; n const long long INF = 9E18; n const int mod = 1e9 + 7; n int n, m; n int pos[M][maxn], len[M]; n int dp[M][maxn]; n char s[M][maxn]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n     for (int j = 1; j <= m; j++) { n       if (s[i][j] ==  1 ) pos[i][len[i]++] = j; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (len[i] == 0) { n         puts( -1 ); n         return 0; n       } n       int cnt = len[i]; n       int loc = lower_bound(pos[i], pos[i] + cnt, j) - pos[i]; n       int movl = inf, movr = inf; n       if (loc == len[i]) { n         loc--; n         movr = j - pos[i][loc]; n         movl = m - (j - pos[i][0]); n       } else if (loc == 0) { n         movl = pos[i][loc] - j; n         movr = m - (pos[i][len[i] - 1] - j); n       } else { n         movl = pos[i][loc] - j; n         movr = j - pos[i][--loc]; n       } n       dp[i][j] = min(movl, movr); n     } n   } n   int ans = inf; n   for (int i = 1; i <= m; i++) { n     int sum = 0; n     for (int j = 1; j <= n; j++) { n       sum += dp[j][i]; n     } n     ans = min(ans, sum); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   deque<char> vec; n   int c; n   bool flag; n   int max = 0; n   for (int i = 0; i < 8; i++) { n     cin >> x; n     for (int j = 0; j < x.length(); j++) vec.push_back(x[j]); n     if (i % 2 == 0) { n       c = 0; n       while (true) { n         c++; n         flag = true; n         for (int j = 1; j < x.length(); j++) { n           if (x[j] == x[j - 1]) { n             flag = false; n             char temp = vec.back(); n             vec.pop_back(); n             vec.push_front(temp); n             break; n           } n         } n         if (c == 9 || flag == true) break; n       } n     } else if (i % 2 != 0) { n       c = 0; n       while (true) { n         c++; n         flag = true; n         for (int j = 1; j < x.length(); j++) { n           if (x[j] == x[j - 1]) { n             flag = false; n             char temp = vec.back(); n             vec.pop_back(); n             vec.push_front(temp); n             break; n           } n         } n         if (c == 9 || flag == true) break; n       } n     } n     if (c > max) max = c; n   } n   if (flag == false || max == 9) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 3, i, j, a, b; n   vector<pair<int, int> > v, v2; n   for (i = 0; i < 3; i++) { n     scanf( %d%d , &a, &b); n     v.push_back(make_pair(a, b)); n     v2.push_back(make_pair(b, a)); n   } n   int cnt, c2, c3; n   sort(v.begin(), v.end()); n   sort(v2.begin(), v2.end()); n   if (v[0].first == v[1].first) { n     if (v[1].first == v[2].first) n       cnt = 1; n     else if (v[1].second == v[2].second) n       cnt = 2; n     else n       cnt = 3; n   } else { n     if (v[1].first == v[2].first) n       cnt = 2; n     else if (v[1].second == v[2].second) n       cnt = 3; n     else n       cnt = 3; n   } n   if (v[0].first == v[2].first) { n     if (v[1].first == v[2].first) n       c2 = 1; n     else if (v[1].second == v[2].second) n       c2 = 2; n     else n       c2 = 3; n   } else { n     if (v[1].first == v[2].first) n       c2 = 2; n     else if (v[1].second == v[2].second) n       c2 = 3; n     else n       c2 = 3; n   } n   if (v[0].first == v[2].first) { n     if (v[1].first == v[0].first) n       c3 = 1; n     else if (v[1].second == v[0].second) n       c3 = 2; n     else n       c3 = 3; n   } else { n     if (v[1].first == v[0].first) n       c3 = 2; n     else if (v[1].second == v[0].second) n       c3 = 3; n     else n       c3 = 3; n   } n   cnt = min(cnt, min(c2, c3)); n   int c1; n   if (v2[0].first == v2[1].first) { n     if (v2[1].first == v2[2].first) n       c1 = 1; n     else if (v2[1].second == v2[2].second) n       c1 = 2; n     else n       c1 = 3; n   } else { n     if (v2[1].first == v2[2].first) n       c1 = 2; n     else if (v2[1].second == v2[2].second) n       c1 = 3; n     else n       c1 = 3; n   } n   if (v2[0].first == v2[2].first) { n     if (v2[1].first == v2[2].first) n       c2 = 1; n     else if (v2[1].second == v2[2].second) n       c2 = 2; n     else n       c2 = 3; n   } else { n     if (v2[1].first == v2[2].first) n       c2 = 2; n     else if (v2[1].second == v2[2].second) n       c2 = 3; n     else n       c2 = 3; n   } n   if (v2[0].first == v2[2].first) { n     if (v2[1].first == v2[0].first) n       c3 = 1; n     else if (v2[1].second == v2[0].second) n       c3 = 2; n     else n       c3 = 3; n   } else { n     if (v2[1].first == v2[0].first) n       c3 = 2; n     else if (v2[1].second == v2[0].second) n       c3 = 3; n     else n       c3 = 3; n   } n   if (v[0].first == v[1].first) { n     if (v[2].second < v[1].second && v[2].second > v[0].second) { n       cout <<  3  << endl; n       exit(0); n     } else if (v[2].second < v[0].second && v[2].second > v[1].second) { n       cout <<  3  << endl; n       exit(0); n     } else { n       cnt = min(cnt, min(c1, min(c2, c3))); n     } n   } else if (v2[0].first == v2[1].first) { n     if (v2[2].second < v2[1].second && v2[2].second > v2[0].second) { n       cout <<  3  << endl; n       exit(0); n     } else if (v2[2].second < v2[0].second && v2[2].second > v2[1].second) { n       cout <<  3  << endl; n       exit(0); n     } else { n       cnt = min(cnt, min(c1, min(c2, c3))); n     } n   } else n     cnt = min(cnt, min(c1, min(c2, c3))); n   reverse(v.begin(), v.end()); n   reverse(v2.begin(), v2.end()); n   int cnt1; n   if (v[0].first == v[1].first) { n     if (v[1].first == v[2].first) n       cnt1 = 1; n     else if (v[1].second == v[2].second) n       cnt1 = 2; n     else n       cnt1 = 3; n   } else { n     if (v[1].first == v[2].first) n       cnt1 = 2; n     else if (v[1].second == v[2].second) n       cnt1 = 3; n     else n       cnt1 = 3; n   } n   if (v[0].first == v[2].first) { n     if (v[1].first == v[2].first) n       c2 = 1; n     else if (v[1].second == v[2].second) n       c2 = 2; n     else n       c2 = 3; n   } else { n     if (v[1].first == v[2].first) n       c2 = 2; n     else if (v[1].second == v[2].second) n       c2 = 3; n     else n       c2 = 3; n   } n   if (v[0].first == v[2].first) { n     if (v[1].first == v[0].first) n       c3 = 1; n     else if (v[1].second == v[0].second) n       c3 = 2; n     else n       c3 = 3; n   } else { n     if (v[1].first == v[0].first) n       c3 = 2; n     else if (v[1].second == v[0].second) n       c3 = 3; n     else n       c3 = 3; n   } n   cnt1 = min(cnt1, min(c2, c3)); n   if (v2[0].first == v2[1].first) { n     if (v2[1].first == v2[2].first) n       c1 = 1; n     else if (v2[1].second == v2[2].second) n       c1 = 2; n     else n       c1 = 3; n   } else { n     if (v2[1].first == v2[2].first) n       c1 = 2; n     else if (v2[1].second == v2[2].second) n       c1 = 3; n     else n       c1 = 3; n   } n   if (v2[0].first == v2[2].first) { n     if (v2[1].first == v2[2].first) n       c2 = 1; n     else if (v2[1].second == v2[2].second) n       c2 = 2; n     else n       c2 = 3; n   } else { n     if (v2[1].first == v2[2].first) n       c2 = 2; n     else if (v2[1].second == v2[2].second) n       c2 = 3; n     else n       c2 = 3; n   } n   if (v2[0].first == v2[2].first) { n     if (v2[1].first == v2[0].first) n       c3 = 1; n     else if (v2[1].second == v2[0].second) n       c3 = 2; n     else n       c3 = 3; n   } else { n     if (v2[1].first == v2[0].first) n       c3 = 2; n     else if (v2[1].second == v2[0].second) n       c3 = 3; n     else n       c3 = 3; n   } n   if (v[0].first == v[1].first) { n     if (v[2].second < v[1].second && v[2].second > v[0].second) { n       cout <<  3  << endl; n       exit(0); n     } else if (v[2].second < v[0].second && v[2].second > v[1].second) { n       cout <<  3  << endl; n       exit(0); n     } else { n       cnt1 = min(cnt1, min(c1, min(c2, c3))); n     } n   } else if (v2[0].first == v2[1].first) { n     if (v2[2].second < v2[1].second && v2[2].second > v2[0].second) { n       cout <<  3  << endl; n       exit(0); n     } else if (v2[2].second < v2[0].second && v2[2].second > v2[1].second) { n       cout <<  3  << endl; n       exit(0); n     } else { n       cnt1 = min(cnt1, min(c1, min(c2, c3))); n     } n   } else n     cnt1 = min(cnt1, min(c1, min(c2, c3))); n   cnt = min(cnt, cnt1); n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long ans = 0; n   bool flag = false; n   if (n == 2) { n     cout << 1 <<   n ; n     return 0; n   } n   for (int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       flag = true; n       break; n     } n   } n   if (!flag) { n     cout << 1 <<   n ; n     return 0; n   } n   if (n & 1) { n     flag = false; n     long long x = n - 2; n     for (int i = 2; i <= sqrt(x); i++) { n       if (x % i == 0) { n         flag = true; n         break; n       } n     } n     if (!flag) { n       cout << 2 <<   n ; n       return 0; n     } else n       cout << 3 <<   n ; n   } else { n     cout << 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 56; n int n, a[N]; n int cnt(int x) { n   int ans = 0; n   for (int i = 1; i <= n; ++i) ans += (a[i] == x); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   int k = *min_element(a + 1, a + n + 1); n   cout << (cnt(k) <= n / 2 ?  Alice  :  Bob ); n } n


#include <bits/stdc++.h> n using namespace std; n long long num[30], pre[100010], cnt; n int main() { n   int n, l, lr, ll; n   char s[100010]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s + 1; n     l = strlen(s + 1); n     for (int j = 0; j < 26; j++) n       if (num[j] && j +  a  != s[1] && j +  a  != s[l]) num[j] = 1; n     lr = 1; n     while (s[lr + 1] == s[lr]) lr++; n     ll = l; n     while (lr != l && s[ll] == s[ll - 1]) ll--; n     ll = l - ll + 1; n     if (s[1] == s[l]) n       if (lr == l) n         num[s[1] -  a ] = (l + 1) * num[s[1] -  a ] + l; n       else n         num[s[1] -  a ] = lr + ll + 1; n     else { n       num[s[1] -  a ] = num[s[1] -  a ] > 0 ? lr + 1 : lr; n       num[s[l] -  a ] = num[s[l] -  a ] > 0 ? ll + 1 : ll; n     } n     for (int j = lr + 1; j <= l - ll + 1; j++) n       if (s[j] == s[j - 1]) n         pre[j] = pre[j - 1] + 1; n       else { n         pre[j] = 1; n         num[s[j - 1] -  a ] = max(num[s[j - 1] -  a ], pre[j - 1]); n       } n   } n   cnt = 0; n   for (int j = 0; j < 26; j++) cnt = max(cnt, num[j]); n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n long long sub(long long a, long long b) { n   return (a - b + 1000000007) % 1000000007; n } n long long add(long long a, long long b) { return (a + b) % 1000000007; } n long long mul(long long a, long long b) { return (a * b) % 1000000007; } n vector<long long> adj[100005]; n bool vis[100005]; n long long siz, edges; n void dfs(long long i) { n   vis[i] = 1; n   for (long long j = 0; j < adj[i].size(); j++) { n     edges++; n     if (!vis[adj[i][j]]) dfs(adj[i][j]); n   } n } n long long mpow(long long base, long long exp) { n   base %= 1000000007; n   long long result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long)result * base) % 1000000007; n     base = ((long long)base * base) % 1000000007; n     exp >>= 1; n   } n   return result; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, sum = 0, t = 1, m, ans; n   while (t--) { n     string s1, s =   ; n     cin >> s1; n     for (long long i = 0; i < s1.length(); i++) n       if (s1[i] ==  a  || s1[i] ==  b ) s += s1[i]; n     n = s.length(); n     vector<long long> dp(n + 1, 1); n     dp[0] = 1; n     ans = 1; n     for (long long i = 1; i < n + 1; i++) { n       if (s[i - 1] ==  a ) n         dp[i] = dp[i - 1] + 1; n       else { n         dp[i] = 1; n         ans = (ans * (dp[i - 1])) % 1000000007; n       } n     } n     ans = (ans * dp[n]) % 1000000007; n     cout << ans - 1 <<   n ; n   } n   cerr <<   nTime elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v, v1; n   int a, b, c, d, e, f, g, h, i, j, k, l; n   cin >> a; n   for (i = 0; i < a; i++) { n     v.clear(); n     v1.clear(); n     cin >> c >> d >> b; n     map<int, int> m; n     for (j = 0; j < b; j++) { n       cin >> e; n       m[e]++; n       v.push_back(e); n     } n     f = 0; n     g = -1; n     h = -1; n     int min1 = INT_MAX; n     for (j = 0; j < v.size(); j++) { n       for (k = d; k <= c; k++) { n         if (!m.count(k)) { n           min1 = min(k - d, min1); n           break; n         } n       } n       for (k = d; k >= 1; k--) { n         if (!m.count(k)) { n           min1 = min(d - k, min1); n           break; n         } n       } n     } n     cout << min1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t, n; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     cin >> s; n     int mask = 0; n     n = s.size(); n     int ok100001 = (s[0] ==  1 ); n     for (int i = 1; i + 1 < n; ++i) ok100001 &= (s[i] ==  0 ); n     ok100001 &= (s.back() <=  1 ); n     if (ok100001) { n       for (int i = 0; i < n - 2; ++i) cout << 9; n       cout <<   n ; n       continue; n     } n     for (int i = 0; i < n; ++i) mask ^= 1 << (s[i] - 48); n     for (int i = n - 1; i >= 0; --i) { n       mask ^= 1 << (s[i] - 48); n       if (s[i] ==  0 ) continue; n       int popcnt = __builtin_popcount(mask); n       if (popcnt > n - i) continue; n       if (popcnt % 2 != (n - i) % 2) continue; n       if (popcnt < n - i || n           (popcnt == n - i && __builtin_ctz(mask) < s[i] - 48)) { n         int curdigit = s[i] - 48 - 1; n         if (popcnt == n - i) n           for (; curdigit >= 0; --curdigit) n             if (mask & (1 << curdigit)) break; n         s[i] = curdigit +  0 ; n         mask ^= 1 << curdigit; n         for (int j = i + 1; j < n; ++j) { n           curdigit = 9; n           if (__builtin_popcount(mask) == n - j) n             for (; curdigit >= 0; --curdigit) n               if (mask & (1 << curdigit)) break; n           s[j] = curdigit +  0 ; n           mask ^= 1 << curdigit; n         } n         break; n       } n     } n     cout << s <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long a[30], b[30], dp[30][30][30]; n struct node { n   long long dis[30][30]; n   node operator*(const node &t) const { n     node rep; n     memset(rep.dis, 0x3f, sizeof rep.dis); 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           rep.dis[i][j] = min(rep.dis[i][j], dis[i][k] + t.dis[k][j]); n     return rep; n   } n } key; n node pow(node rep, int n) { n   if (n == 1) return rep; n   node req = pow(rep, n / 2); n   if (n % 2 == 0) return req * req; n   return req * req * rep; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   memset(dp, 0x3f, sizeof dp); n   for (int i = 0; i <= n; i++) dp[i][i][0] = 0; n   for (int k = 1; k <= n; k++) n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= n; j++) { n         if (j - 1 >= 0) n           dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1] + a[k]); n         if (j + 1 <= n) n           dp[i][j][k] = min(dp[i][j][k], dp[i][j + 1][k - 1] + b[k]); n       } n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) key.dis[i][j] = dp[i][j][n]; n   key = pow(key, m); n   printf( %d n , key.dis[0][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p = 0, q = 0; n   cin >> n; n   string str; n   cin >> str; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  F  && str[i + 1] ==  S ) p = p + 1; n     if (str[i] ==  S  && str[i + 1] ==  F ) q = q + 1; n   } n   if (q > p) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n const int nMax = 200001; n struct Segment { n   int left; n   int right; n   long long sum; n   long long forced; n   Segment* lChild; n   Segment* rChild; n }; n Segment* initTree(int left, int right, long long forced) { n   Segment* segment = new Segment; n   segment->left = left; n   segment->right = right; n   segment->forced = forced; n   if (left == right) { n     segment->lChild = nullptr; n     segment->rChild = nullptr; n     segment->sum = left; n     segment->forced = left; n   } else { n     segment->lChild = initTree(left, (left + right) / 2, forced); n     segment->rChild = initTree((left + right) / 2 + 1, right, forced); n     segment->sum = ((segment->rChild == nullptr) ? 0 : segment->rChild->sum) + n                    ((segment->lChild == nullptr) ? 0 : segment->lChild->sum); n   } n   return segment; n } n int customMax(int fd, int sd) { return (fd > sd) ? fd : sd; } n int getValue(Segment* root, int item, int value = -1) { n   if (root->left == root->right) { n     if (value == -1) value = root->forced; n     return value; n   } n   if (value == -1) n     if (root->forced != -1) value = root->forced; n   if (item <= (root->left + root->right) / 2) n     return getValue(root->lChild, item, value); n   else n     return getValue(root->rChild, item, value); n } n int safeForceCheck(Segment* root, int left, int right, int threshold) { n   if (left > right) return -1; n   if (left == right) n     if (getValue(root, left) >= threshold) n       return -1; n     else n       return left; n   int result; n   if (getValue(root, (left + right) / 2) >= threshold) { n     result = safeForceCheck(root, left, (left + right) / 2 - 1, threshold); n   } else { n     result = safeForceCheck(root, (left + right) / 2 + 1, right, threshold); n     if (result == -1) result = (left + right) / 2; n   } n   return result; n } n void forceToSegment(Segment* root, int left, int right, long long forced, n                     long long was_forced = -1) { n   if (left < 0) return; n   if (right < left) return; n   if ((left == root->left) && (right == root->right)) { n     root->forced = forced; n     root->sum = (right - left + 1) * forced; n     return; n   } n   if (was_forced == -1) was_forced = root->forced; n   if (was_forced != -1) { n     if (was_forced >= forced) return; n     root->lChild->forced = was_forced; n     root->lChild->sum = n         (root->lChild->right - root->lChild->left + 1) * was_forced; n     root->rChild->forced = was_forced; n     root->rChild->sum = n         (root->rChild->right - root->rChild->left + 1) * was_forced; n     root->forced = -1; n   } n   if (right <= (root->left + root->right) / 2) { n     forceToSegment(root->lChild, left, right, forced, was_forced); n   } else if (left > (root->left + root->right) / 2) { n     forceToSegment(root->rChild, left, right, forced, was_forced); n   } else { n     forceToSegment(root->lChild, left, (root->left + root->right) / 2, forced, n                    was_forced); n     forceToSegment(root->rChild, (root->left + root->right) / 2 + 1, right, n                    forced, was_forced); n   } n   root->sum = ((root->rChild == nullptr) ? 0 : root->rChild->sum) + n               ((root->lChild == nullptr) ? 0 : root->lChild->sum); n } n int main() { n   long n; n   Segment* root; n   scanf( %ld , &n); n   std::vector<int> a(n); n   std::vector<int> used(nMax, -1); n   int maxA = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     used[a[i]] = i; n     if (a[i] > maxA) maxA = a[i]; n   } n   maxA++; n   std::vector<long long> downCount(maxA, 0); n   std::vector<int> rowTail(n, 0); n   std::vector<std::vector<int> > affective(maxA); n   downCount[maxA - 1] = ((long long)n * (n + 1)) / 2; n   for (int i = 1; i < maxA; ++i) n     for (int j = i; j < maxA; j += i) n       if (used[j] != -1) affective[i].push_back(used[j]); n   root = initTree(0, n - 1, -1); n   for (int i = maxA - 2; i >= 1; i--) { n     if (affective[i].size() <= 1) { n       downCount[i] = downCount[i + 1]; n     } else { n       std::sort(affective[i].begin(), affective[i].end()); n       forceToSegment(root, affective[i][1] + 1, n                      safeForceCheck(root, affective[i][1] + 1, n - 1, n), n); n       forceToSegment(root, affective[i][0] + 1, n                      safeForceCheck(root, affective[i][0] + 1, affective[i][1], n                                     affective[i][affective[i].size() - 1]), n                      affective[i][affective[i].size() - 1]); n       forceToSegment(root, 0, n                      safeForceCheck(root, 0, affective[i][0], n                                     affective[i][affective[i].size() - 2]), n                      affective[i][affective[i].size() - 2]); n       downCount[i] = ((long long)n * (n)) - root->sum; n     } n   } n   long long result = 0LL; n   for (int i = maxA - 2; i >= 1; i--) { n     result += (downCount[i + 1] - downCount[i]) * i; n   } n   printf( %I64d , result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 502; n pair<int, pair<int, int> > dp[N][N]; n int a[N]; n int b[N]; n map<int, int> a_val_to_pos; n int main() { n   int n, m; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   cin >> m; n   for (int i = 0; i < m; ++i) cin >> b[i]; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (b[j] == a[i]) { n         dp[i][j].first = 1; n         dp[i][j].second.first = -1; n         dp[i][j].second.second = -1; n         for (int k = j - 1; k >= 0; --k) { n           if (b[k] < b[j]) { n             if ((a_val_to_pos.find(b[k]) != a_val_to_pos.end()) && n                 dp[a_val_to_pos[b[k]]][k].first + 1 > dp[i][j].first) { n               dp[i][j].first = dp[a_val_to_pos[b[k]]][k].first + 1; n               dp[i][j].second.first = a_val_to_pos[b[k]]; n               dp[i][j].second.second = k; n             } n           } n         } n       } else { n         dp[i][j].first = 0; n         dp[i][j].second.first = -1; n         dp[i][j].second.second = -1; n       } n     } n     a_val_to_pos[a[i]] = i; n   } n   int ans_i = -1, ans_j = -1, mx = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) n       if (dp[i][j].first > mx) { n         mx = dp[i][j].first; n         ans_i = i; n         ans_j = j; n       } n   } n   vector<int> ans; n   cout << mx << endl; n   if (mx > 0) { n     for (;;) { n       ans.push_back(b[ans_j]); n       if (dp[ans_i][ans_j].second.first != -1) { n         int tmp = ans_i; n         ans_i = dp[tmp][ans_j].second.first; n         ans_j = dp[tmp][ans_j].second.second; n       } else { n         break; n       } n     } n     reverse(ans.begin(), ans.end()); n     for (int i = 0; i < ans.size(); ++i) cout << ans[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<long long, long long>; n using pcc = pair<char, char>; n using pdd = pair<double, double>; n using vi = vector<int>; n using vs = vector<string>; n using vll = vector<ll>; n using vpii = vector<pii>; n using vc = vector<char>; n using vd = vector<double>; n ll POW(ll a, ll b) { n   if (b == 0) return 1; n   ll x = POW(a, b / 2); n   x = (x * x); n   if (b & 1) x = (x * a); n   return x; n } n int main(int argc, char* argv[]) { n   ll n, a, b; n   scanf( %lld%*c , &(n)); n   while (n--) { n     scanf( %lld%*c , &(a)), scanf( %lld%*c , &(b)); n     int aa[100]{0}, bb[100]{0}, i; n     ll ans{0}, bbb = b; n     for (i = 0; i < 100; i++) { n       aa[i] = a % 2; n       a /= 2; n       bb[i] = b % 2; n       b /= 2; n     } n     for (i = 99; i >= 0; i--) { n       if (bb[i]) { n         if (!aa[i]) { n           ans += POW(2, i) - 1; n           if (ans + POW(2, i) <= bbb) ans += POW(2, i); n           break; n         } else n           ans += POW(2, i); n       } n     } n     printf( %lld , (ans)), printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int v[1000001]; n using namespace std; n int main() { n   int a, b, c, d; n   scanf( %d%d%d%d , &a, &b, &c, &d); n   while (b <= 1000000) { n     v[b] = 1; n     b += a; n   } n   while (d <= 1000000) { n     if (v[d] == 1) { n       printf( %d , d); n       return 0; n     } n     d += c; n   } n   printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   double t = 0; n   cin >> n; n   int i, a[n], b[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     b[i] = a[n - 1 - i]; n   } n   for (i = n - 1; i >= 0; i--) { n     if (i % 2 == 0) n       t += (b[i] * b[i]); n     else n       t -= (b[i] * b[i]); n   } n   t *= 3.141592653589793; n   printf( %.15f , t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5 * 1e5 + 1; n long long n, a[N], sum[N], s, f, ans; n int32_t main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (i) n       sum[i] = sum[i - 1] + a[i]; n     else n       sum[i] = a[i]; n   } n   if (sum[n - 1] % 3) return cout <<  0 n , 0; n   s = sum[n - 1] / 3; n   if (!s) { n     for (long long i = 0; i < n - 1; i++) { n       if (!sum[i]) f++; n     } n     cout << f * (f - 1) / 2 <<   n ; n     return 0; n   } n   for (long long i = 0; i < n - 1; i++) { n     if (sum[i] == s) n       f++; n     else if (sum[i] == s * 2) n       ans += f; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void run() { n   long long n; n   cin >> n; n   long long p[n]; n   for (long long i = 0; i < n; i++) cin >> p[i]; n   for (long long i = 0; i < n; i++) { n     if (p[i] == 0) p[i] = -1; n     if (p[i] != -1) p[i] %= 2; n   } n   long long ones = 0, zeros = 0; n   for (long long i = 1; i <= n; i++) { n     if (i % 2) n       ones++; n     else n       zeros++; n   } n   for (long long i = 0; i < n; i++) { n     if (p[i] == 0) n       zeros--; n     else if (p[i] == 1) n       ones--; n   } n   long long dp[n][zeros + 1][ones + 1][2]; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < zeros + 1; j++) { n       for (long long k = 0; k < ones + 1; k++) { n         for (long long l = 0; l < 2; l++) dp[i][j][k][l] = 1e9; n       } n     } n   } n   if (p[0] != -1) n     dp[0][0][0][p[0]] = 0; n   else { n     dp[0][1][0][0] = 0; n     dp[0][0][1][1] = 0; n   } n   for (long long i = 1; i < n; i++) { n     for (long long j = 0; j < zeros + 1; j++) { n       for (long long k = 0; k < ones + 1; k++) { n         if (p[i] == -1) { n           if (j + 1 <= zeros) n             dp[i][j + 1][k][0] = n                 min(dp[i - 1][j][k][0], dp[i - 1][j][k][1] + 1); n           if (k + 1 <= ones) n             dp[i][j][k + 1][1] = n                 min(dp[i - 1][j][k][1], dp[i - 1][j][k][0] + 1); n         } else n           dp[i][j][k][p[i]] = n               min(dp[i - 1][j][k][p[i]], dp[i - 1][j][k][1 - p[i]] + 1); n       } n     } n   } n   cout << min(dp[n - 1][zeros][ones][0], dp[n - 1][zeros][ones][1]); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void _do(T x) { n   cerr << x <<   n ; n } n template <typename T, typename... U> n void _do(T x, U... y) { n   cerr << x <<  ,  ; n   _do(y...); n } n const int MOD1 = 1e9 + 7; n const int MOD2 = 998244353; n const long long INF = 2e18; n const long double PI = 3.14159265358979323846; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long fpow(long long a, long long b, long long m) { n   if (!b) return 1; n   long long ans = fpow(a * a % m, b / 2, m); n   return (b % 2 ? ans * a % m : ans); n } n long long inv(long long a, long long m) { return fpow(a, m - 2, m); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   long long n = s.size(); n   string ans(n,  0 ); n   while (1) { n     long long id = -1; n     bool x = 0; n     for (long long i = 0; i < n; i++) { n       if (s[i] ==  0  && id != -1) { n         ans[id] =  1 , ans[i] =  0 ; n         s[id] = s[i] =  x ; n         id = -1, x = 1; n       } n       if (s[i] ==  1 ) id = i; n     } n     if (!x) break; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n const long long MX = 2e5 + 5; n const long long MOD = 1e9 + 7; n const long long INF = 1e18; n const long double PI = acos((long double)-1); n const long long xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n constexpr long long pct(long long x) { return __builtin_popcount(x); } n constexpr long long bits(long long x) { return 31 - __builtin_clz(x); } n template <class T> n T cdiv(T a, T b) { n   return a / b + !(a < 0 || a % b == 0); n } n long long fstTrue(function<bool(long long)> first, long long lo, long long hi) { n   hi++; n   assert(lo <= hi); n   while (lo < hi) { n     long long mid = (lo + hi) / 2; n     first(mid) ? hi = mid : lo = mid + 1; n   } n   return lo; n } n template <class T> n void remDup(vector<T>& v) { n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), end(v)); n } n void DBG() { cerr <<  ]  << endl; } n template <class H, class... T> n void DBG(H h, T... t) { n   cerr << h; n   if (sizeof...(t)) cerr <<  ,  ; n   DBG(t...); n } n void setIn(string S) { freopen(S.c_str(),  r , stdin); } n void setOut(string S) { freopen(S.c_str(),  w , stdout); } n void setIO(string S =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   if ((long long)(S).size()) { n     setIn(S +  .in ), setOut(S +  .out ); n   } n } n int32_t main() { n   setIO(); n   long long n; n   cin >> n; n   vector<long long> arr(n); n   for (auto& x : arr) cin >> x; n   if (n == 1) { n     for (long long i = (0); i < (3); ++i) { n       cout << 1 <<     << 1 <<   n ; n       cout << -arr[0] <<   n ; n       arr[i] = 0; n     } n   } else { n     cout << 1 <<     << 1 <<   n ; n     cout << -arr[0] <<   n ; n     cout << 2 <<     << n <<   n ; n     for (long long i = (1); i < (n); ++i) cout << arr[i] * (n - 1) <<    ; n     cout <<   n ; n     cout << 1 <<     << n <<   n ; n     cout << 0 <<    ; n     for (long long i = (1); i < (n); ++i) cout << -arr[i] * n <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n double fRand(double fMin, double fMax) { n   double f = (double)rand() / RAND_MAX; n   return fMin + f * (fMax - fMin); n } n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n long long power(long long x, int n) { n   if (n == 0) return 1; n   if (n & 1) return (x * power(x, n - 1)) % 1000000007; n   long long p = power(x, n / 2); n   return (p * p) % 1000000007; n } n int n, a[1000005]; n long long fact[1000005], invFact[1000005]; n long long C(long long n, long long k) { n   return ((fact[n] * invFact[k] % 1000000007) * invFact[n - k]) % 1000000007; n } n int main() { n   scanf( %d , &n); n   for (int u = (1); u <= (n); ++u) scanf( %d , &a[u]); n   fact[0] = invFact[0] = 1; n   for (int i = (1); i <= (1000005 - 1); ++i) n     fact[i] = (fact[i - 1] * i) % 1000000007; n   invFact[1000005 - 1] = power(fact[1000005 - 1], 1000000007 - 2); n   for (int i = (1000005 - 2); i >= (1); --i) n     invFact[i] = (invFact[i + 1] * (i + 1)) % 1000000007; n   sort(a + 1, a + n + 1); n   int j = n, ans = 0; n   for (int i = (n); i >= (1); --i) { n     while (j > 0 && a[j] >= a[i]) --j; n     if (a[i] != a[n]) { n       int k = n - j - 1; n       long long x = n           ((C(n, k + 1) * fact[k]) % 1000000007 * fact[n - k - 1]) % 1000000007; n       ans = (ans + a[i] * x) % 1000000007; n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n void _print(int *a, int n) {} n using namespace std; n const int N = 300; n int n, m; n int a[N][N], h[N], v[N]; n int ans[N][N]; n signed main() { n   scanf( %d , &n), scanf( %d , &m); n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) { n       scanf( %d , &a[i][j]); n       h[i] = max(h[i], a[i][j]); n       v[j] = max(v[j], a[i][j]); n     } n   sort(h, h + n, greater<int>()); n   sort(v, v + m, greater<int>()); n   int ph = 0, pv = 0; n   queue<pair<int, int> > q; n   for (int i = (n * m + 1) - 1; i >= (1); --i) { n     if (ph < n && i == h[ph + 1]) ph++; n     if (pv < m && i == v[pv + 1]) pv++; n     if (i == h[ph] || i == v[pv]) { n       ans[ph][pv] = i; n       if (i == h[ph]) n         for (int i = (pv)-1; i >= (0); --i) q.push(make_pair(ph, i)); n       if (i == v[pv]) n         for (int i = (ph)-1; i >= (0); --i) q.push(make_pair(i, pv)); n     } else { n       pair<int, int> p = q.front(); n       q.pop(); n       ans[p.first][p.second] = i; n     } n   } n   for (int i = (0); i < (n); ++i) { n     for (int _ = (0); _ < (m); ++_) n       printf( %d%c , ans[i][_],    n [_ == m - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0, l = -99999999, r = 99999999; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int rat, div; n     scanf( %d , &rat), scanf( %d , &div); n     if (div == 1) n       l = max(l, 1900 - sum); n     else n       r = min(r, 1899 - sum); n     sum += rat; n   } n   if (r >= 99999999) n     cout <<  Infinity n ; n   else if (l > r) n     cout <<  Impossible n ; n   else n     cout << sum + r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int r[105]; n   int i; n   double pi = acos(-1.0); n   for (i = 0; i < n; i++) { n     cin >> r[i]; n   } n   sort(r, r + n); n   int s = 0; n   for (i = 0; i < n; i++) { n     s = r[i] * r[i] - s; n   } n   printf( %.6lf n , s * pi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int M = 1e5 + 5; n bool dn = false; n int n; n bool check(int res[]) { n   for (int i = 1; i < n; i++) { n     if (abs(res[i] - res[i - 1]) != 1) { n       return false; n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) { n     cout << res[i] <<    ; n   } n   cout << endl; n   dn = true; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a > b + 1 || d > c + 1) { n     cout <<  NO n ; n     return 0; n   } n   n = a + b + c + d; n   int i, j, f; n   for (i = 0; i < 2; i++) { n     for (j = 0; j < 2; j++) { n       int ta, tb, tc, td; n       ta = a; n       tb = b; n       tc = c; n       td = d; n       int res[N]; n       int id1 = 0, id2 = n - 1; n       if (i == 0) { n         if (ta > 0) { n           if (tb < ta) n             continue; n           else { n             tb -= ta; n             for (id1 = 0; id1 < 2 * ta; id1++) { n               if (id1 % 2 == 0) n                 res[id1] = 1; n               else n                 res[id1] = 0; n             } n           } n         } n       } else { n         if (ta > 0) { n           if (tb < ta - 1) n             continue; n           else { n             tb -= ta - 1; n             for (id1 = 0; id1 < 2 * ta - 1; id1++) { n               if (id1 % 2 == 0) n                 res[id1] = 0; n               else n                 res[id1] = 1; n             } n           } n         } n       } n       if (j == 0) { n         if (td > 0) { n           if (tc < td) n             continue; n           else { n             tc -= td; n             for (id2 = 0; id2 < 2 * td; id2++) { n               if (id2 % 2 == 0) n                 res[n - 1 - id2] = 2; n               else n                 res[n - 1 - id2] = 3; n             } n           } n         } n       } else { n         if (td > 0) { n           if (tc < td - 1) n             continue; n           else { n             tc -= td - 1; n             for (id2 = 0; id2 < 2 * td - 1; id2++) { n               if (id2 % 2 == 0) n                 res[n - 1 - id2] = 3; n               else n                 res[n - 1 - id2] = 2; n             } n           } n         } n       } n       if (tc == tb + 1) { n         for (int f = 0; f < tc + tb; f++) { n           if (f % 2 == 0) n             res[f + id1] = 2; n           else n             res[f + id1] = 1; n         } n         check(res); n         if (dn) return 0; n       } n       if (tb == tc + 1) { n         for (int f = 0; f < tc + tb; f++) { n           if (f % 2 == 0) n             res[f + id1] = 1; n           else n             res[f + id1] = 2; n         } n         check(res); n         if (dn) return 0; n       } n       if (tb == tc) { n         for (int f = 0; f < tc + tb; f++) { n           if (f % 2 == 0) n             res[f + id1] = 2; n           else n             res[f + id1] = 1; n         } n         check(res); n         if (dn) return 0; n         for (int f = 0; f < tc + tb; f++) { n           if (f % 2 == 0) n             res[f + id1] = 1; n           else n             res[f + id1] = 2; n         } n         check(res); n         if (dn) return 0; n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 2e5; n const int MOD = 1e9 + 7; n template <typename ForwardIterator, typename T> n ForwardIterator first_less_than(ForwardIterator first, ForwardIterator last, n                                 T value) { n   auto it = std::lower_bound(first, last, value); n   return (it == first ? last : --it); n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n bool check(char ch) { n   if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ) n     return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   t = 1; n   while (t--) { n     long long n, i, j, k; n     string s, t; n     cin >> s >> t; n     if (s.size() != t.size()) { n       cout <<  NO  n            <<   n ; n       continue; n     } n     bool ok = true; n     for (i = 0; i < s.size(); i++) { n       bool f1 = check(s[i]); n       bool f2 = check(t[i]); n       if (f1 && !f2 || f2 && !f1) ok = false; n     } n     if (ok) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int mn = 1e5 + 1; n ll n, d, a[mn], b[mn], ans = 1, cnt = 1, pos = 1, ansid[mn]; n struct pairs { n   int val, id; n   pairs operator+(pairs A) { return (pairs){val + A.val, id + A.id}; } n } tree[mn * 4], f[mn]; n pairs maxp(pairs A, pairs B) { n   if (A.val == B.val) { n     if (A.id > B.id) return A; n     return B; n   } n   if (A.val < B.val) return B; n   return A; n } n void update(int l, int r, int id, int x, pairs p) { n   if (l > x || x > r) return; n   if (l == r) { n     tree[id] = p; n     return; n   } n   int mid = (l + r) / 2; n   update(l, mid, id * 2, x, p); n   update(mid + 1, r, id * 2 + 1, x, p); n   tree[id] = maxp(tree[id * 2], tree[id * 2 + 1]); n } n pairs get(int l, int r, int id, int x, int y) { n   if (l > y || x > r) return (pairs){0, 0}; n   if (x <= l && r <= y) return tree[id]; n   int mid = (l + r) / 2; n   return maxp(get(l, mid, id * 2, x, y), get(mid + 1, r, id * 2 + 1, x, y)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> d; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; ++i) { n     int u = upper_bound(b + 1, b + n + 1, a[i] - d) - b - 1; n     int v = lower_bound(b + 1, b + n + 1, a[i] + d) - b; n     int k = lower_bound(b + 1, b + n + 1, a[i]) - b; n     if (u) f[i] = maxp(f[i], get(1, n, 1, 1, u) + (pairs){1, 0}); n     if (v <= n) f[i] = maxp(f[i], get(1, n, 1, v, n) + (pairs){1, 0}); n     f[i] = maxp(f[i], (pairs){1, i}); n     update(1, n, 1, k, (pairs){f[i].val, i}); n     if (ans < f[i].val) { n       ans = f[i].val; n       pos = i; n     } n   } n   cout << ans <<   n ; n   ansid[ans] = pos, cnt = ans - 1; n   while (cnt) ansid[cnt--] = f[ansid[cnt + 1]].id; n   for (int i = 1; i <= ans; ++i) cout << ansid[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void incr(long long &st, long long &en, long long &k, long long val) { n   en++; n   if (val <= k) { n     k++; n   } n } n void decr(long long &st, long long &en, long long &k, long long val) { n   long long sz = en - st + 1; n   if (val < k) { n     k -= val; n     sz = sz - val; n   } else { n     sz = val; n   } n   st = 1; n   en = sz + st - 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k, m, t; n   cin >> n >> k >> m >> t; n   long long st = 1, en = n; n   while (t--) { n     long long w, val; n     cin >> w >> val; n     if (w) { n       incr(st, en, k, val); n       cout << (en - st + 1) <<     << k << endl; n     } else { n       decr(st, en, k, val); n       cout << (en - st + 1) <<     << k << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i128 = __int128_t; n using u128 = __uint128_t; n const long long MOD = 998244353; n const long double PI = 3.141592653589793238; n const long long pi = 31415926; n const long long inf = 1000000000000000000; n const long long small_inf = INT_MAX; n long long int modpow(long long int x, long long int n, n                      long long int mod = MOD) { n   long long int res = 1; n   while (n > 0) { n     if (n & 1) res = res * x % mod; n     x = x * x % MOD; n     n >>= 1; n   } n   return res; n } n long long int power(long long int x, long long int n) { n   long long int res = 1; n   while (n > 0) { n     if (n & 1) res = res * x; n     x = x * x; n     n >>= 1; n   } n   return res; n } n void init() {} n void solve() { n   int n, m; n   cin >> n >> m; n   std::vector<int> v[2][n + 1]; n   vector<pair<int, pair<long long int, long long int>>> v1[2 * n + 1]; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     v[0][x].push_back(y); n     v[1][y].push_back(x); n     v1[x].push_back(make_pair(y, make_pair(0, 1))); n     v1[n + y].push_back(make_pair(n + x, make_pair(0, 1))); n   } n   for (int i = 1; i <= n; i++) { n     v1[i].push_back(make_pair(n + i, make_pair(1, 0))); n     v1[n + i].push_back(make_pair(i, make_pair(1, 0))); n   } n   std::vector<std::vector<long long>> dist( n       2, std::vector<long long int>(n + 1, inf)); n   dist[0][1] = 0; n   for (int fu = 0; fu <= 30; fu++) { n     int f = fu % 2; n     set<pair<long long int, int>> s; n     for (int i = 1; i <= n; i++) { n       if (dist[f][i] != inf) s.insert(make_pair(dist[f][i], i)); n     } n     std::vector<int> vis(n + 1, 0); n     while (!s.empty()) { n       pair<long long int, int> p = *s.begin(); n       s.erase(s.begin()); n       int x = p.second; n       long long int d = p.first; n       if (vis[x]) continue; n       vis[x] = 1; n       for (auto y : v[f][x]) { n         if (!vis[y] && d + 1 < dist[f][y]) { n           dist[f][y] = d + 1; n           s.insert(make_pair(d + 1, y)); n         } n       } n     } n     for (int i = 1; i <= n; i++) { n       dist[f ^ 1][i] = min(dist[f ^ 1][i], dist[f][i] + power(2, fu)); n     } n   } n   if (dist[0][n] != inf || dist[1][n] != inf) { n     cout << min(dist[0][n], dist[1][n]) % MOD << endl; n     return; n   } n   vector<pair<long long int, long long int>> dis(2 * n + 1, n                                                  make_pair(inf, inf)); n   { n     set<pair<pair<long long int, long long int>, int>> s; n     for (int i = 1; i <= n; i++) { n       if (dist[0][i] < inf) dis[i] = make_pair(0, dist[0][i]); n       s.insert(make_pair(dis[i], i)); n     } n     std::vector<int> vis(2 * n + 1, 0); n     while (!s.empty()) { n       pair<pair<long long int, long long int>, int> p = *s.begin(); n       s.erase(s.begin()); n       if (vis[p.second]) continue; n       vis[p.second] = 1; n       int x = p.second; n       long long int num = p.first.first, d = p.first.second; n       for (auto y : v1[x]) { n         if (vis[y.first] || make_pair(num + y.second.first, n                                       d + y.second.second) >= dis[y.first]) n           continue; n         dis[y.first] = make_pair(num + y.second.first, d + y.second.second); n         s.insert(make_pair(dis[y.first], y.first)); n       } n     } n   } n   pair<long long int, long long int> p; n   if (dis[n].first < dis[2 * n].first) n     p = dis[n]; n   else if (dis[2 * n].first < dis[n].first) n     p = dis[2 * n]; n   else if (dis[2 * n].second < dis[n].second) n     p = dis[2 * n]; n   else n     p = dis[2 * n + 1]; n   long long int ans = p.second % MOD; n   for (int i = 30; i <= 30 + p.first - 1; i++) { n     ans += modpow(2, i); n     ans %= MOD; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int t23 = 1, tt23 = 1; n   init(); n   while (tt23 <= t23) { n     solve(); n     tt23++; n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct Medianer { n   priority_queue<int> low; n   priority_queue<int, vector<int>, greater<int> > high; n   Medianer() {} n   int getMedian() { return low.top(); } n   void insert(int a) { n     if (low.empty()) n       low.push(a); n     else if (a > low.top()) { n       high.push(a); n     } else n       low.push(a); n     while (low.size() > high.size()) { n       high.push(low.top()); n       low.pop(); n     } n     while (low.size() < high.size()) { n       low.push(high.top()); n       high.pop(); n     } n   } n   size_t size() { return low.size() + high.size(); } n }; n Medianer *merge(Medianer *a, Medianer *b) { n   if (a->size() < b->size()) swap(a, b); n   while (!b->low.empty()) { n     a->insert(b->low.top()); n     b->low.pop(); n   } n   while (!b->high.empty()) { n     a->insert(b->high.top()); n     b->high.pop(); n   } n   delete b; n   return a; n } n int main() { n   int N; n   cin >> N; n   vector<int> data(N, 0); n   copy_n(istream_iterator<int>(cin), N, data.begin()); n   vector<Medianer *> meds; n   for (int i = 0; i < N; ++i) { n     Medianer *cur = new Medianer(); n     cur->insert(data[i]); n     while (!meds.empty() && cur->getMedian() < meds.back()->getMedian()) { n       cur = merge(meds.back(), cur); n       meds.pop_back(); n     } n     meds.push_back(cur); n   } n   unsigned long long ret = 0; n   for (int i = 0, j = 0; i < (int)meds.size(); ++i) { n     for (int k = 0; k < (int)meds[i]->size(); ++k, ++j) { n       ret += abs(data[j] - meds[i]->getMedian()); n     } n     delete meds[i]; n   } n   cout << ret; n   return 0; n } n