#include <bits/stdc++.h> n using namespace std; n const int MAX = 1005; n int a[MAX][MAX], u[MAX][MAX], d[MAX][MAX], l[MAX][MAX], r[MAX][MAX]; n int main() { n   int n, m, q; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n   for (int j = 0; j < m; j++) n     if (a[0][j]) n       u[0][j] = -1; n     else n       u[0][j] = 0; n   for (int i = 1; i < n; i++) n     for (int j = 0; j < m; j++) n       if (a[i][j]) n         u[i][j] = u[i - 1][j]; n       else n         u[i][j] = i; n   for (int j = 0; j < m; j++) n     if (a[n - 1][j]) n       d[n - 1][j] = n; n     else n       d[n - 1][j] = n - 1; n   for (int i = n - 2; i >= 0; i--) n     for (int j = 0; j < m; j++) n       if (a[i][j]) n         d[i][j] = d[i + 1][j]; n       else n         d[i][j] = i; n   for (int i = 0; i < n; i++) n     if (a[i][0]) n       l[i][0] = -1; n     else n       l[i][0] = 0; n   for (int i = 0; i < n; i++) n     for (int j = 1; j < m; j++) n       if (a[i][j]) n         l[i][j] = l[i][j - 1]; n       else n         l[i][j] = j; n   for (int i = 0; i < n; i++) n     if (a[i][m - 1]) n       r[i][m - 1] = m; n     else n       r[i][m - 1] = m - 1; n   for (int i = 0; i < n; i++) n     for (int j = m - 2; j >= 0; j--) n       if (a[i][j]) n         r[i][j] = r[i][j + 1]; n       else n         r[i][j] = j; n   while (q--) { n     int op; n     scanf( %d , &op); n     int x, y; n     scanf( %d %d , &x, &y); n     x--; n     y--; n     if (op == 1) { n       if (a[x][y]) { n         a[x][y] = 0; n         l[x][y] = y; n         r[x][y] = y; n         u[x][y] = x; n         d[x][y] = x; n         for (int i = x - 1; i >= 0; i--) n           if (a[i][y]) n             d[i][y] = x; n           else n             break; n         for (int i = x + 1; i < n; i++) n           if (a[i][y]) n             u[i][y] = x; n           else n             break; n         for (int j = y - 1; j >= 0; j--) n           if (a[x][j]) n             r[x][j] = y; n           else n             break; n         for (int j = y + 1; j < m; j++) n           if (a[x][j]) n             l[x][j] = y; n           else n             break; n       } else { n         a[x][y] = 1; n         int ans; n         if (x == n - 1) n           ans = n; n         else n           ans = d[x + 1][y]; n         for (int i = x; i >= 0; i--) n           if (a[i][y]) n             d[i][y] = ans; n           else n             break; n         if (x == 0) n           ans = -1; n         else n           ans = u[x - 1][y]; n         for (int i = x; i < n; i++) n           if (a[i][y]) n             u[i][y] = ans; n           else n             break; n         if (y == m - 1) n           ans = m; n         else n           ans = r[x][y + 1]; n         for (int j = y; j >= 0; j--) n           if (a[x][j]) n             r[x][j] = ans; n           else n             break; n         if (y == 0) n           ans = -1; n         else n           ans = l[x][y - 1]; n         for (int j = y; j < m; j++) n           if (a[x][j]) n             l[x][j] = ans; n           else n             break; n       } n     } else { n       int ans = 0; n       int lo = d[x][y], hi = u[x][y]; n       for (int j = y; j < r[x][y]; j++) { n         lo = min(lo, d[x][j]); n         hi = max(hi, u[x][j]); n         ans = max(ans, (j - y + 1) * (lo - hi - 1)); n       } n       lo = d[x][y]; n       hi = u[x][y]; n       for (int j = y; j > l[x][y]; j--) { n         lo = min(lo, d[x][j]); n         hi = max(hi, u[x][j]); n         ans = max(ans, (y - j + 1) * (lo - hi - 1)); n       } n       int le = l[x][y], ri = r[x][y]; n       for (int i = x; i < d[x][y]; i++) { n         le = max(le, l[i][y]); n         ri = min(ri, r[i][y]); n         ans = max(ans, (i - x + 1) * (ri - le - 1)); n       } n       le = l[x][y]; n       ri = r[x][y]; n       for (int i = x; i > u[x][y]; i--) { n         le = max(le, l[i][y]); n         ri = min(ri, r[i][y]); n         ans = max(ans, (x - i + 1) * (ri - le - 1)); n       } 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 = 1e6 + 5; n const long long mod = 1e9 + 7; n const int inf = 0x7fffffff; n int a[maxn]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int r, g, b, w; n     cin >> r >> g >> b >> w; n     int f = 0; n     if (r % 2 == 1) f++; n     if (g % 2 == 1) f++; n     if (b % 2 == 1) f++; n     if (w % 2 == 1) f++; n     if (f <= 1) n       cout <<  Yes  <<   n ; n     else { n       if (r && g && b) r--, g--, b--, w += 3; n       f = 0; n       if (r % 2 == 1) f++; n       if (g % 2 == 1) f++; n       if (b % 2 == 1) f++; n       if (w % 2 == 1) f++; n       if (f <= 1) n         cout <<  Yes  <<   n ; n       else n         cout <<  No  <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, n, sum = 0, maxi, mini, start, end; n   ; n   cin >> a >> b >> n; n   mini = min(a, b); n   maxi = max(a, b); n   if (n <= min(a, b)) { n     cout << n + 1; n   } else { n     if (mini + maxi >= n) { n       int k = n - maxi; n       if (k < 0) { n         k = 0; n       } n       sum = mini - k + 1; n     } n     cout << sum; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n int main() { n   v.push_back(1); n   long long x, i, xx = 10, c = 2, cc = 0; n   while (1) { n     i = 1; n     cc = c - 1; n     xx = 0; n     while (i <= c) { n       xx = xx + pow(2, cc); n       cc++; n       i++; n     } n     c++; n     if (xx <= 100000) { n       v.push_back(xx); n     } else { n       break; n     } n   } n   int sz = v.size(); n   long long nn; n   cin >> nn; n   for (int ii = sz - 1; ii >= 0; ii--) { n     if (v[ii] <= nn && nn % v[ii] == 0) { n       cout << v[ii] << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int d = 998244353; n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<vector<long long int>> t(n + 1); n   vector<long long int> init(k + 1, 0); n   for (int i = 0; i <= n; i++) { n     t[i] = init; n   } n   t[1][0] = m; n   t[1][1] = 0; n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= min(i - 1, k); j++) { n       if (j >= 1) { n         t[i][j] = (t[i - 1][j] + (m - 1) * t[i - 1][j - 1]) % d; n       } n       if (j == 0) { n         t[i][0] = m; n       } n     } n   } n   cout << t[n][k]; n   return 0; n } n


#include <bits/stdc++.h> n static const int MAXLEN = 1e6 + 12; n int len = 0; n char s[MAXLEN]; n char *tokens[MAXLEN]; n int token_ct = 0; n int max_depth = 0; n std::vector<char *> nested[MAXLEN]; n int parse_comment_recursive(int start_token, int level) { n   if (max_depth < level) max_depth = level; n   nested[level].push_back(tokens[start_token++]); n   int children = strtol(tokens[start_token++], NULL, 10); n   while (children--) { n     start_token = parse_comment_recursive(start_token, level + 1); n   } n   return start_token; n } n int main() { n   while ((s[len] = getchar()) !=   n  && s[len] != EOF) ++len; n   s[len] =   0 ; n   tokens[token_ct++] = s; n   for (int i = 0; i < len; ++i) n     if (s[i] ==  , ) { n       tokens[token_ct++] = s + i + 1; n       s[i] =   0 ; n     } n   int parsed_tokens = 0; n   while (parsed_tokens < token_ct) n     parsed_tokens = parse_comment_recursive(parsed_tokens, 0); n   printf( %d n , max_depth + 1); n   for (int i = 0; i <= max_depth; ++i) { n     for (std::vector<char *>::iterator j = nested[i].begin(); n          j != nested[i].end(); ++j) { n       fputs(*j, stdout); n       putchar(   ); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 0; n int n, k, x; n string s; n vector<int> v; n int main() { n   int a, b, c, d, e, f; n   int cnt = 0; n   scanf( %d%d%d%d%d%d , &a, &b, &c, &d, &e, &f); n   if (a == d) cnt++; n   if (b == e) cnt++; n   if (c == f) cnt++; n   if (cnt >= 1) { n     printf( YES n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n char s[100100]; n int n, ans = 1, tmp; n int main() { n   scanf( %d %s , &n, s); n   for (int i = 1; i < n; i++) n     if (s[i] != s[i - 1]) n       ans++; n     else n       tmp++; n   if (tmp) ans++; n   if (tmp > 1) ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if (a > b) { n       if (a % 2 == b % 2) n         cout <<  1  << endl; n       else n         cout <<  2  << endl; n     } else if (a < b) { n       if (a % 2 == b % 2) n         cout <<  2  << endl; n       else n         cout <<  1  << endl; n     } else n       cout <<  0  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int keep[201]; n int tt[201]; n int first[201]; n int main() { n   while (cin >> n) { n     n *= 2; n     int sum = 0, n1 = 0, n2 = 0, s1 = 0, s2 = 0; n     memset(keep, 0, sizeof(keep)); n     memset(first, 0, sizeof(first)); n     for (int i = 0; i < n; i++) { n       int a; n       cin >> a; n       keep[a]++; n       tt[i] = a; n     } n     for (int i = 10; i < 100; i++) { n       if (keep[i] > 1) { n         s1++; n         s2++; n         if (n1 > n2) n           n1 += keep[i] / 2, n2 += (keep[i] - keep[i] / 2), first[i] = 2; n         else n           n2 += keep[i] / 2, n1 += (keep[i] - keep[i] / 2), first[i] = 1; n       } else if (keep[i] == 1) n         sum++; n     } n     int turn = 1; n     if (s1 > s2) { n       turn = 2; n       s1 += sum / 2; n       s2 += (sum - sum / 2); n     } else { n       turn = 2; n       s2 += sum / 2; n       s1 += (sum - sum / 2); n     } n     cout << s1 * s2 << endl; n     for (int i = 0; i < n; i++) { n       if (keep[tt[i]] > 1) { n         cout << first[tt[i]]; n         first[tt[i]] = (first[tt[i]] == 2 ? 1 : 2); n       } else if (keep[tt[i]] == 1) { n         cout << turn; n         turn = (turn == 2 ? 1 : 2); n       } n       if (i < n - 1) n         cout <<    ; n       else n         cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n template <typename T> n struct ScanfSpecifier {}; n template <> n struct ScanfSpecifier<char*> { n   static constexpr const char* value =  %s ; n }; n template <> n struct ScanfSpecifier<int> { n   static constexpr const char* value =  %d ; n }; n template <> n struct ScanfSpecifier<double> { n   static constexpr const char* value =  %lf ; n }; n template <> n struct ScanfSpecifier<float> { n   static constexpr const char* value =  %f ; n }; n template <> n struct ScanfSpecifier<char> { n   static constexpr const char* value =  %c ; n }; n template <> n struct ScanfSpecifier<const char*> { n   static constexpr const char* value =  %s ; n }; n template <> n struct ScanfSpecifier<unsigned long> { n   static constexpr const char* value =  %lu ; n }; n template <> n struct ScanfSpecifier<unsigned int> { n   static constexpr const char* value =  %u ; n }; n template <> n struct ScanfSpecifier<long long int> { n   static constexpr const char* value =  %lld ; n }; n template <typename T> n int RD(T& arg) { n   return std::scanf(ScanfSpecifier<T>::value, &arg); n } n template <int S> n int RD(char (&arg)[S]) { n   return std::scanf( %s , arg); n } n int RD(char* arg) { return std::scanf( %s , arg); } n template <> n int RD<char>(char& arg) { n   return std::scanf(  %c , &arg); n } n template <typename T, typename... Args> n int RD(T& arg1, Args&... args) { n   return RD(arg1) + RD(args...); n } n template <typename T> n T RD() { n   T ret; n   RD(ret); n   return ret; n } n template <typename It> n void RDV(It begin, It end) { n   while (begin != end) RD(*begin++); n } n template <typename C> n void RDV(C& c) { n   RDV(std::begin(c), std::end(c)); n } n template <typename T> n void WT(T arg) { n   std::printf(ScanfSpecifier<T>::value, arg); n } n template <typename T, typename U> n void WT(std::pair<T, U> arg) { n   std::printf( ( ); n   WT(arg.first); n   std::printf( ,  ); n   WT(arg.second); n   std::printf( ) ); n } n template <typename... Args> n void WT(Args... args) { n   int alc = 0; n   int dummy[] = {((alc++ ? std::printf(   ) : 0), WT(args), 0)...}; n } n template <typename... Args> n void WTL(Args... args) { n   WT(args...); n   std::printf(  n ); n } n template <typename It> n void WTV(It begin, It end) { n   int alc = 0; n   while (begin != end) (alc++ ? std::printf(   ) : 0), WT(*begin++); n } n template <typename C> n void WTV(const C& c) { n   WTV(std::begin(c), std::end(c)); n } n template <typename It> n void WTVL(It begin, It end) { n   WTV(begin, end); n   std::printf(  n ); n } n template <typename C> n void WTVL(const C& c) { n   WTVL(std::begin(c), std::end(c)); n } n struct Range { n   struct It { n     int num, step; n     int operator*() { return num; } n     void operator++() { num += step; } n     bool operator!=(const It& other) { return num != other.num; } n   }; n   Range(int ee) : b(0), e(ee) {} n   Range(int bb, int ee) : b(bb), e(ee) {} n   It begin() { return {b, (b < e ? 1 : -1)}; } n   It end() { return {e, 0}; } n   int b, e; n }; n template <typename T> n inline T& UMAX(T& x, T y) { n   if (x < y) x = y; n   return x; n } n template <typename T> n inline T& UMIN(T& x, T y) { n   if (y < x) x = y; n   return x; n } n template <typename T, typename... Args> n struct ArithmiticPromotion { n   typedef decltype(T() + typename ArithmiticPromotion<Args...>::type()) type; n }; n template <typename T, typename U> n struct ArithmiticPromotion<T, U> { n   typedef decltype(T() + U()) type; n }; n template <typename T> n struct ArithmiticPromotion<T, T> {}; n template <typename T> n struct ArithmiticPromotion<T> {}; n template <typename T, typename U> n typename ArithmiticPromotion<T, U>::T MAX(T a, U b) { n   return a < b ? b : a; n } n template <typename T, typename... Args> n typename ArithmiticPromotion<T, Args...>::T MAX(T a, Args... args) { n   return MAX(a, MAX(args...)); n } n template <typename T, typename U> n typename ArithmiticPromotion<T, U>::T MIN(T a, U b) { n   return a < b ? a : b; n } n template <typename T, typename... Args> n typename ArithmiticPromotion<T, Args...>::T MIN(T a, Args... args) { n   return MIN(a, MIN(args...)); n } n using RG = Range; n using namespace std; n char L[32], R[32]; n void pad(char* s) { n   int l = strlen(s); n   reverse(s, s + l); n   for (int i : RG(l, 19)) s[i] =  0 ; n   reverse(s, s + 19); n   for (int i : RG(19)) s[i] -=  0 ; n } n long long int ans; n int num[10]; n void dfs(int pos, int r, int total) { n   if (pos < 10) { n     for (int i = 0; i <= r; i++) { n       num[pos] = i; n       dfs(pos + 1, r - i, total + i); n     } n   } else { n     { n       bool check = true; n       int use = 0; n       bool b = false; n       int tmp[10] = {}; n       int mn = 1; n       for (int i = 0; i < 19; i++) { n         while (mn < 10 && tmp[mn] == num[mn]) mn++; n         if (!b) { n           for (int d = L[i] + 1; d < R[i]; d++) n             if ((!d || tmp[d] + 1 <= num[d]) && use + 19 - i - !d >= total) { n               ans++; n               return; n             } n         } else if (R[i]) { n           if (use + 19 - i - 1 >= total || n               (mn < R[i] && use + 19 - i >= total)) { n             ans++; n             return; n           } n         } n         if (R[i]) { n           ++use; n           if (++tmp[R[i]] > num[R[i]]) { n             check = false; n             break; n           } n         } n         if (L[i] < R[i]) b = true; n       } n       if (check && use == total) { n         ans++; n         return; n       } n     } n     { n       bool check = true; n       int use = 0; n       bool b = false; n       int tmp[10] = {}; n       int mx = 9; n       for (int i = 0; i < 19; i++) { n         while (mx > 0 && tmp[mx] == num[mx]) mx--; n         if (!b) { n           for (int d = L[i] + 1; d < R[i]; d++) n             if (tmp[d] + 1 <= num[d] && use + 19 - i - !d >= total) { n               ans++; n               return; n             } n         } else { n           if (mx > L[i] && use + 19 - i >= total) { n             ans++; n             return; n           } n         } n         if (L[i]) { n           ++use; n           if (++tmp[L[i]] > num[L[i]]) { n             check = false; n             break; n           } n         } n         if (L[i] < R[i]) b = true; n       } n       if (check && use == total) { n         ans++; n         return; n       } n     } n   } n } n int main() { n   RD(L, R); n   pad(L), pad(R); n   dfs(1, 18, 0); n   WTL(ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n char second[N]; n long long x, y, xx, yy; n int n; n bool check(long long days) { n   long long px = x, py = y; n   for (int i = 0; i < n; i++) { n     long long mult = days / n; n     if (i < (days % n)) mult++; n     if (second[i] ==  U ) py += mult; n     if (second[i] ==  D ) py -= mult; n     if (second[i] ==  L ) px -= mult; n     if (second[i] ==  R ) px += mult; n   } n   long long d = abs(xx - px) + abs(yy - py); n   return d <= days; n } n int main() { n   scanf( %lld%lld%lld%lld , &x, &y, &xx, &yy); n   scanf( %d , &n); n   scanf( %s , second); n   long long lo = 0, hi = 1e18, ans = -1; n   if (!check(hi)) { n     cout << -1 << endl; n     return 0; n   } n   while (hi - lo > 1) { n     long long mid = (lo + hi) / 2; n     if (check(mid)) { n       ans = mid; n       hi = mid; n     } else { n       lo = mid; n     } n   } n   cout << hi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, x, y; n   cin >> n; n   vector<pair<long long, long long>> a, b; n   for (long long i = 0; i < n; i++) { n     cin >> x >> y; n     a.push_back(make_pair(x, y)); n   } n   for (long long i = 0; i < n; i++) { n     cin >> x >> y; n     b.push_back(make_pair(x, y)); n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   cout << a[0].first + b[n - 1].first <<     << a[0].second + b[n - 1].second; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int a[1024], f[10010]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, l = INT_MAX, r = INT_MIN, cnt = 0, src, dst; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     ++f[a[i]]; n   } n   for (int i = 0; i < 10001; ++i) { n     if (f[i]) ++cnt; n     if (cnt > 3) { n       cout <<  Unrecoverable configuration. ; n       return 0; n     } n   } n   if (cnt == 1) { n     cout <<  Exemplary pages. ; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     if (l > a[i]) { n       l = a[i]; n       src = i + 1; n     } n     if (r < a[i]) { n       r = a[i]; n       dst = i + 1; n     } n   } n   int dif = r - l; n   if (dif % 2) { n     cout <<  Unrecoverable configuration. ; n     return 0; n   } n   dif /= 2; n   if (n == 2) { n     cout << dif <<   ml. from cup #  << src <<   to cup #  << dst <<  . n ; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     if (i != src - 1 && i != dst - 1) { n       if (l + dif != a[i] || r - dif != a[i]) { n         cout <<  Unrecoverable configuration. ; n         return 0; n       } n     } n   } n   cout << dif <<   ml. from cup #  << src <<   to cup #  << dst <<  . n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n const int MM = 998244353; n const long double PI = acos(-1); n long long power(long long b, long long e, long long m) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b % m, e / 2, m) % m; n   return power(b * b % m, e / 2, m); n } n long long power(long long b, long long e) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b, e / 2); n   return power(b * b, e / 2); n } n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (x < y) x = y; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n const int N = 500005; n long long c[N]; n map<long long, vector<pair<int, int>>> ed; n struct DSU { n   vector<int> p, r; n   int n; n   int components; n   DSU(int _) : n(_), components(_) { n     p.resize(n + 1); n     r.resize(n + 1); n     for (int i = 0; i <= _; ++i) p[i] = i, r[i] = 0; n   } n   int parent(int i) { return p[i] = (p[i] == i) ? i : parent(p[i]); } n   void merge(int x, int y) { n     x = parent(x); n     y = parent(y); n     if (x == y) return; n     if (r[x] > r[y]) n       p[y] = x; n     else n       p[x] = y; n     if (r[x] == r[y]) ++r[y]; n     --components; n   } n }; n int _runtimeTerror_() { n   long long ans = 0; n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) cin >> c[i]; n   ans += power(2, n, M) * ((1LL << k) % M) % M; n   for (int i = 1; i <= m; ++i) { n     int x, y; n     cin >> x >> y; n     ed[c[x] ^ c[y]].push_back({x, y}); n   } n   DSU dsu(n); n   for (auto &j : ed) { n     int cnt = 0; n     for (auto &k : j.second) n       dsu.p[k.first] = k.first, dsu.p[k.second] = k.second; n     dsu.components = n; n     for (auto &k : j.second) { n       dsu.merge(k.first, k.second); n     } n     ans -= power(2, n, M); n     ans %= M; n     ans += power(2, dsu.components, M); n     ans = (ans + M) % M; n   } n   cout << ans <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) _runtimeTerror_(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e5 + 5; n int di[] = {1, 1, -1, -1}; n int dj[] = {1, -1, 1, -1}; n long long n, k; n long long solv() { n   long long st = 0, ed = k, ans; n   while (st <= ed) { n     long long x = (k * (k + 1)) / 2; n     long long mid = (st + ed) / 2; n     long long xx = k - mid; n     x -= ((xx * (xx + 1)) / 2) + mid - 1; n     if (x < n) n       st = mid + 1; n     else if (x >= n) n       ans = mid, ed = mid - 1; n     else n       return mid; n   } n   return ans; n } n int main() { n   cin >> n >> k; n   if ((k * (k + 1)) / 2 - k + 1 < n) return cout << -1, 0; n   cout << solv(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2) { n     cout <<  black  << endl; n   } else { n     cout <<  white  n          <<   n1 2 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n int t, n, x[N], y[N]; n long long a[N], val[N]; n int main() { n   cin >> t; n   while (t--) { n     scanf( %d , &n); n     long long sum = 0; n     for (int i = 1; i <= n; ++i) { n       scanf( %lld , a + i); n       sum += a[i]; n     } n     if (sum % n) { n       puts( -1 ); n       continue; n     } n     sum /= n; n     int m = 0; n     for (int i = 2; i <= n; ++i) { n       long long req = a[i] % i; n       if (req) { n         req = i - req, ++m; n         x[m] = 1, y[m] = i, val[m] = req; n         a[1] -= req, a[i] += req; n       } n       ++m; n       x[m] = i, y[m] = 1, val[m] = a[i] / i; n       a[1] += a[i], a[i] = 0; n     } n     for (int i = 2; i <= n; ++i) { n       ++m; n       x[m] = 1, y[m] = i, val[m] = sum; n       a[1] -= sum, a[i] += sum; n     } n     printf( %d n , m); n     for (int i = 1; i <= m; ++i) printf( %d %d %lld n , x[i], y[i], val[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, i, x, y, best, w, cw, rt, a[200200]; n vector<int> g[200200]; n bool all; n void dfs(int i, int p, int cur, int dst) { n   if (dst > best) { n     best = dst; n     cw = cur; n     w = i; n   } n   for (int j : g[i]) n     if (j != p) { n       dfs(j, i, ((a[j] == 0) ? cur : a[j]), n           (dst + int(a[j] != 0 && a[j] != cur))); n     } n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     all = true; n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       if (a[i] != 0) { n         all = false; n         rt = i; n       } n       g[i].clear(); n     } n     for (i = 1; i < n; i++) { n       scanf( %d%d , &x, &y); n       g[x].push_back(y); n       g[y].push_back(x); n     } n     if (all) { n       puts( 1 ); n       continue; n     } n     best = 0; n     w = rt; n     cw = a[rt]; n     dfs(rt, 0, a[rt], 0); n     best = 0; n     dfs(w, 0, cw, 0); n     printf( %d n , (best + 3) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int64_t l; n   int64_t p, q; n   double x, w; n   std::cin >> l; n   std::cin >> p >> q; n   w = (q + p); n   x = l * p / w; n   std::cout << x; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[105]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   a[n] = a[0]; n   int k; n   int minn = 10005; n   for (int i = 1; i <= n; i++) { n     if (abs(a[i] - a[i - 1]) < minn) { n       minn = abs(a[i] - a[i - 1]); n       k = i; n     } n   } n   if (k >= n) { n     cout << 1 <<     << k << endl; n   } else { n     cout << k + 1 <<     << k << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n long long int n, x, y, b, a[3]; n int main() { n   long long int x, y; n   cin >> x >> y; n   long long int a = 0; n   while (true) { n     if (a % 2 == 0) { n       if (x >= 2 && y >= 2) { n         x -= 2; n         y -= 2; n       } else { n         if (x >= 1 && y >= 12) { n           x--; n           y -= 12; n         } else { n           if (x >= 0 && y >= 22) { n             y -= 22; n           } else { n             break; n           } n         } n       } n     } else { n       if (x >= 0 && y >= 22) { n         y -= 22; n       } else { n         if (x >= 1 && y >= 12) { n           x--; n           y -= 12; n         } else { n           if (x >= 2 && y >= 2) { n             x -= 2; n             y -= 2; n           } else { n             break; n           } n         } n       } n     } n     a++; n   } n   if (a % 2 == 1) { n     cout <<  Ciel ; n     return 0; n   } n   cout <<  Hanako ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int ci() { n   register char ch; n   int f = 1; n   while (!isdigit(ch = getchar())) f = ch ==  -  ? -1 : 1; n   register int x = ch ^  0 ; n   while (isdigit(ch = getchar())) x = (x * 10) + (ch ^  0 ); n   return f * x; n } n enum { N = 100024 }; n int a[N]; n int main() { n   int n = ci(); n   for (register int i = (1); i <= (n); i++) a[i] = ci(); n   sort(a + 1, a + n + 1); n   long long a1 = 0, a2 = 0; n   int m = n / 2; n   for (register int i = (1); i <= (m); i++) a1 += a[i]; n   for (register int i = (m + 1); i <= (n); i++) a2 += a[i]; n   printf( %I64d , a1 * a1 + a2 * a2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Dish { n   int ind; n   int cost; n   int cnt; n }; n int n; n bool dishComp(Dish a, Dish b) { n   if (a.cost == b.cost) { n     return a.ind < b.ind; n   } n   return a.cost < b.cost; n } n int main() { n   int m, a, c; n   cin >> n >> m; n   vector<Dish> dishes; n   for (int i = 0; i < n; i++) { n     cin >> a; n     Dish dish; n     dish.ind = i + 1; n     dish.cnt = a; n     dishes.push_back(dish); n   } n   int has[n + 1]; n   for (int i = 0; i < n; i++) { n     cin >> c; n     dishes[i].cost = c; n   } n   sort(dishes.begin(), dishes.end(), dishComp); n   for (int i = 0; i < n; i++) { n     has[dishes[i].ind] = i; n   } n   int t, d, btindex, cnti; n   long long int sm; n   int saveIndex = 0; n   for (int i = 0; i < m; i++) { n     cin >> t >> d; n     btindex = has[t]; n     if (dishes[btindex].cnt >= d) { n       sm = (long long int)dishes[btindex].cost * d; n       dishes[btindex].cnt = dishes[btindex].cnt - d; n     } else { n       sm = (long long int)dishes[btindex].cost * dishes[btindex].cnt; n       d = d - dishes[btindex].cnt; n       dishes[btindex].cnt = 0; n       cnti = 0; n       while (saveIndex < n) { n         if (dishes[saveIndex].cnt < d) { n           d = d - dishes[saveIndex].cnt; n           sm = sm + n                (long long int)dishes[saveIndex].cost * dishes[saveIndex].cnt; n           dishes[saveIndex].cnt = 0; n         } else { n           sm = sm + (long long int)dishes[saveIndex].cost * d; n           dishes[saveIndex].cnt = dishes[saveIndex].cnt - d; n           cnti = 1; n           break; n         } n         saveIndex++; n       } n       if (cnti == 0) sm = 0; n     } n     cout << sm <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 9; n int seg[MAX * 4][2], n, k, a, b, q, ar[MAX]; n void upd(int x, int s = 0, int e = n, int id = 0) { n   if (e - s == 1) { n     seg[id][0] = min(ar[s], a), seg[id][1] = min(ar[s], b); n     return; n   } n   int mid = s + e >> 1; n   if (x < mid) n     upd(x, s, mid, id * 2 + 1); n   else n     upd(x, mid, e, id * 2 + 2); n   seg[id][0] = seg[id * 2 + 1][0] + seg[id * 2 + 2][0]; n   seg[id][1] = seg[id * 2 + 1][1] + seg[id * 2 + 2][1]; n } n pair<int, int> gt(int l, int r, int s = 0, int e = n, int id = 0) { n   pair<int, int> ret = {0, 0}; n   if (r <= s || e <= l) return ret; n   if (l <= s && e <= r) return {seg[id][0], seg[id][1]}; n   int mid = s + e >> 1; n   pair<int, int> le = gt(l, r, s, mid, id * 2 + 1), n                  ri = gt(l, r, mid, e, id * 2 + 2); n   ret = {le.first + ri.first, le.second + ri.second}; n   return ret; n } n int main() { n   cin >> n >> k >> a >> b >> q, n++; n   while (q--) { n     int t, d, a; n     cin >> t >> d; n     if (t == 1) n       cin >> a, ar[d] += a, upd(d); n     else n       cout << gt(0, d).second + gt(d + k, n).first <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a; n   int r2 = 0; n   int ans; n   int prev = 0; n   long long res = 0; n   int pr2 = 0; n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &tmp); n     if (i == 0) { n       prev = tmp; n       continue; n     } n     if (tmp < prev) { n       res += prev - tmp; n     } n     prev = tmp; n   } n   printf( %I64d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Interval { n   int start, end; n }; n bool compare(Interval i1, Interval i2) { return (i1.start < i2.start); } n vector<long long int> factors; n void Divisors(long long int n) { n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         factors.push_back(i); n       else { n         factors.push_back(i); n         factors.push_back(n / i); n       } n     } n   } n } n long long int coun[100005] = {0}; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, k, m, t; n   cin >> n >> k >> m >> t; n   while (t--) { n     long long int task, in; n     cin >> task >> in; n     if (task == 0) { n       if (in < k) { n         k = k - in; n         n = n - in; n       } else { n         n = in; n       } n       cout << n <<     << k << endl; n     } else { n       if (in <= k) { n         k = k + 1; n         n = n + 1; n       } else { n         k = k; n         n = n + 1; n       } n       cout << n <<     << k << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 254; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n int n, t, K; n int a[N], d[N]; n long long A[N], D[N], lim[N]; n long long f[N][N][2], g[N][N][2]; n inline void down(long long &x, const long long y) { x > y ? x = y : 0; } n inline long long max(const long long x, const long long y) { n   return x < y ? y : x; n } n int main() { n   int i, j, k, r; n   long long times, time2, rem, need; n   scanf( %d%d%d , &n, &t, &K); n   for (i = 1; i <= n; ++i) scanf( %d%d%lld , a + i, d + i, lim + i); n   ++n, a[n] = d[n] = INT_MAX, lim[n] = INF; n   for (i = 1; i <= n; ++i) A[i] = A[i - 1] + a[i], D[i] = D[i - 1] + d[i]; n   memset(f, 63, sizeof f), memset(g, 63, sizeof g); n   memset(f, 0, sizeof *f), memset(g, 0, sizeof *g); n   for (i = 1; i <= n; ++i) n     for (j = 0; j <= t; ++j) n       for (k = 0; k < 2; ++k) { n         if ((long long)k * a[i] + (long long)j * d[i] <= lim[i] && n             f[i - 1][j][k] < INF) { n           down(f[i][j][k], f[i - 1][j][k]), n               times = (k * A[i - 1] + j * D[i - 1] + K - 1) / K; n           if (times * K <= k * A[i] + j * D[i]) down(g[i][j][k], times); n         } n         for (r = 0; r < j; ++r) n           if ((times = g[i][r][k]) < INF) { n             rem = k * A[i] + r * D[i] - K * times, assert(rem >= 0); n             need = n                 (max(0, rem + (long long)(j - r) * d[i] - lim[i]) + K - 1) / K; n             if (need * K <= rem && f[i - 1][j - r][0] < INF) { n               down(f[i][j][k], times + need + f[i - 1][j - r][0]); n               time2 = ((j - r) * D[i - 1] + K - 1) / K; n               if (time2 * K <= (j - r) * D[i] + (rem - need * K)) n                 down(g[i][j][k], times + need + time2); n             } n           } n       } n   printf( %lld n , f[n][t][1]); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 2e7 + 10; n const int mod = 998244853; n int n, m, qwq[maxn + 5], ifac[maxn + 5], f[maxn + 5]; n void zzh_NB(int &x, int y) { (x += y) >= mod && (x -= mod); } n int add(int x) { return x >= mod ? x - mod : x; } n int readINT() { n   int xx = 0, ch = getchar(), f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) xx = xx * 10 + ch -  0 , ch = getchar(); n   return xx * f; n } n int kehaixing(int x) { return x < 0 ? x + mod : x; } n int quick_pp(int x, int k) { n   int ans = 1; n   for (; k > 0; k >>= 1, x = 1LL * x * x % mod) { n     if ((k & 1) == 1) ans = 1LL * ans * x % mod; n   } n   return ans; n } n int inv(int x) { return quick_pp(x, mod - 2); } n void init(int n) { n   qwq[0] = 1; n   for (int i = 1; i <= n; i++) qwq[i] = 1LL * qwq[i - 1] * i % mod; n   ifac[n] = inv(qwq[n]); n   for (int i = n; i >= 1; i--) ifac[i - 1] = 1LL * ifac[i] * i % mod; n } n int wssb(int n, int m) { n   if (n < m) return 0; n   return 1LL * qwq[n] * ifac[m] % mod * ifac[n - m] % mod; n } n int ans = 0; n inline void duru() { n   n = readINT(), m = readINT(); n   init(n + m); n   for (int i = 0; i <= n; i++) { n     f[i] = (i <= n - m ? wssb(n + m, n) : wssb(n + m, n - i)); n   } n   for (int i = 0; i <= n; i++) { n     zzh_NB(ans, 1LL * i * kehaixing(f[i] - f[i + 1]) % mod); n   } n   printf( %d n , ans); n } n int main() { n   duru(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   cin >> s; n   long long int ans = 0; n   vector<string> v; n   string temp =   ; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  .  && v.size() != 0) { n       for (int i = 0; i < v.size(); i++) { n         if (temp == v[i]) ++ans; n       } n       if (temp ==   ) v.pop_back(); n       temp =   ; n       continue; n     } n     if (s[i] ==  : ) { n       for (int i = 0; i < v.size(); i++) { n         if (temp == v[i]) ++ans; n       } n       v.push_back(temp); n       temp =   ; n       continue; n     } n     if (s[i] !=  , ) temp += s[i]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:256000000 ) n template <class T> n T sqr(T a) { n   return a * a; n } n double a, d, s; n int n; n void init() { cin >> a >> d >> n; } n void solve() { n   double x = 0, y = 0; n   int dx[4] = {1, 0, -1, 0}; n   int dy[4] = {0, 1, 0, -1}; n   int t = d / (4 * a); n   d = d - t * (4 * a); n   int q = 0; n   for (int i = 0; i < n; ++i) { n     double t = d; n     if (!q) { n       if (a - x - t < 1e-5) { n         t -= a - x; n         x = a; n         q++; n       } n     } else if (q == 1) { n       if (a - y - t < 1e-5) { n         t -= a - y; n         y = a; n         q++; n       } n     } else if (q == 2) { n       if (x - t < 1e-5) { n         t -= x; n         x = 0; n         q++; n       } n     } else { n       if (y - t < 1e-5) { n         t -= y; n         y = 0; n         q = 0; n       } n     } n     while (t - a > 1e-5) { n       x += dx[q] * a; n       y += dy[q] * a; n       q++; n       if (q == 4) q = 0; n       t -= a; n     } n     x += dx[q] * t; n     y += dy[q] * t; n     printf( %0.4f %0.4f  n , x, y); n   } n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<char> line; n bool isPossible(int gLoc, int k); n int main() { n   int n, k; n   cin >> n; n   cin >> k; n   char curr; n   int grasshopperLocation; n   for (int i = 0; i < n; i++) { n     cin >> curr; n     line.push_back(curr); n     if (curr ==  G ) grasshopperLocation = i; n   } n   if (isPossible(grasshopperLocation, k)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n bool isPossible(int gLoc, int k) { n   for (int j = gLoc; j < line.size(); j += k) { n     if (line[j] ==  T ) n       return true; n     else if (line[j] ==  # ) n       break; n   } n   for (int j = gLoc; j >= 0; j -= k) { n     if (line[j] ==  T ) n       return true; n     else if (line[j] ==  # ) n       break; n   } n   return false; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n inline long long getint() { n   long long _x = 0, _tmp = 1; n   char _tc = getchar(); n   while ((_tc <  0  || _tc >  9 ) && _tc !=  - ) _tc = getchar(); n   if (_tc ==  - ) _tc = getchar(), _tmp = -1; n   while (_tc >=  0  && _tc <=  9 ) _x *= 10, _x += (_tc -  0 ), _tc = getchar(); n   return _x * _tmp; n } n inline long long add(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x += _y; n   return _x >= _mod ? _x - _mod : _x; n } n inline long long sub(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x -= _y; n   return _x < 0 ? _x + _mod : _x; n } n inline long long mul(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x *= _y; n   return _x >= _mod ? _x % _mod : _x; n } n long long mypow(long long _a, long long _x, long long _mod) { n   if (_x == 0) return 1LL; n   long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod); n   if (_x & 1) _ret = mul(_ret, _a, _mod); n   return _ret; n } n long long mymul(long long _a, long long _x, long long _mod) { n   if (_x == 0) return 0LL; n   long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod); n   if (_x & 1) _ret = add(_ret, _a, _mod); n   return _ret; n } n inline bool equal(double _x, double _y) { n   return _x > _y - 1e-9 && _x < _y + 1e-9; n } n void sleep(double sec = 1021) { n   clock_t s = clock(); n   while (clock() - s < CLOCKS_PER_SEC * sec) n     ; n } n int __ = 1, _cs; n void build() {} n int n; n vector<int> vx[100001], vy[100001]; n void init() { n   n = getint(); n   for (int i = 0, x, y; i < n; i++) { n     x = getint(); n     y = getint(); n     vx[x].push_back(y); n     vy[y].push_back(x); n   } n   for (int i = 0; i < 100001; i++) { n     sort(vx[i].begin(), vx[i].end()); n     sort(vy[i].begin(), vy[i].end()); n   } n } n int ans; n vector<int> cand[100001]; n void solve() { n   for (int x = 0; x < 100001; x++) n     for (int y : vx[x]) { n       size_t xptr = upper_bound(vx[x].begin(), vx[x].end(), y) - vx[x].begin(); n       size_t yptr = upper_bound(vy[y].begin(), vy[y].end(), x) - vy[y].begin(); n       while (xptr < vx[x].size() and yptr < vy[y].size()) { n         if (vx[x][xptr] - y == vy[y][yptr] - x) { n           if (not vx[vy[y][yptr]].empty()) n             cand[vy[y][yptr]].push_back(vx[x][xptr]); n           xptr++; n           yptr++; n         } else if (vx[x][xptr] - y < vy[y][yptr] - x) n           xptr = n               lower_bound(vx[x].begin(), vx[x].end(), y + (vy[y][yptr] - x)) - n               vx[x].begin(); n         else n           yptr = n               lower_bound(vy[y].begin(), vy[y].end(), x + (vx[x][xptr] - y)) - n               vy[y].begin(); n       } n     } n   for (int x = 0; x < 100001; x++) { n     sort(cand[x].begin(), cand[x].end()); n     size_t ptr = 0; n     for (int y : cand[x]) { n       while (ptr < vx[x].size() and vx[x][ptr] < y) ptr++; n       if (ptr == vx[x].size()) break; n       ans += (vx[x][ptr] == y); n     } n   } n   cout << ans << endl; n } n int main() { n   build(); n   while (__--) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize ( O2,unroll-loops ) n //#pragma GCC optimize( no-stack-protector,fast-math ) n //#pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n  n using namespace std; n typedef long long ll; n typedef long double ld; n typedef pair<int, int> pii; n typedef pair<pii, int> piii; n typedef pair<ll, ll> pll; n #define debug(x) cerr<<#x<< = <<(x)<<endl; n #define debugp(x) cerr<<#x<< = { <<(x.first)<< ,  <<(x.second)<< } <<endl; n #define debug2(x, y) cerr<< { <<#x<< ,  <<#y<< } = { <<(x)<< ,  <<(y)<< } <<endl; n #define debugv(v) {cerr<<#v<<  :  ;for (auto x:v) cerr<<x<<   ;cerr<<endl;} n #define all(x) x.begin(), x.end() n #define pb push_back n #define kill(x) return cout<<x<<  n , 0; n  n const int inf=1000000010; n const ll INF=1000000000000001000LL; n const int mod=1000000007; n const int MAXN=200010, LOG=18, SGM=26; n  n ll out, ans; n int n, m, k, u, v, x, y, t, a, b; n map<vector<int>, vector<string>> fuck; n int TR[MAXN][SGM], ts; n int par[LOG][MAXN]; n int inc[MAXN], dest[MAXN]; n vector<int> vec[MAXN]; n string S[MAXN]; n  n inline int Lcp(string &x, string &y){ n  int res=0; n  while (res<m && x[res]==y[res]) res++; n  return res; n } n int Count(vector<int> &vec, int l, int r){ // [l...r) n  return lower_bound(all(vec), r)-lower_bound(all(vec), l); n } n int GetPar(int v, int k){ n  for (int i=0; (1<<i)<=k; i++) if (k&(1<<i)) v=par[i][v]; n  return v; n } n ll Solve(){ n  if (m==1) return 1ll*n*(n-1)/2; n  sort(S, S+n); n  ll res=0;/* n  if (n<=2000){ n   for (int i=0; i<n; i++) for (int j=i+1; j<n; j++){ n    int l=Lcp(S[i], S[j]), r=m-1; n    if (l==m){ n     res++; n     continue ; n    } n    while (r && S[i][r]==S[j][r]) r--; n    int ok=1; n    while (l<r){ n     ok&=(S[i][l]<=S[i][l+1]); n     l++; n    } n    res+=ok; n   } n   return res; n  }*/ n  ts=0; n  memset(TR[0], 0, sizeof(TR[0])); n  vec[0].clear(); n  for (int i=0; i<n; i++){ n   vec[0].pb(i); n   int v=0; n   for (int j=m-1; ~j; j--){ n    int c=S[i][j]- a ; n    if (!TR[v][c]){ n     TR[v][c]=++ts; n     par[0][ts]=v; n     vec[ts].clear(); n     memset(TR[ts], 0, sizeof(TR[ts])); n    } n    v=TR[v][c]; n    vec[v].pb(i); n   } n   dest[i]=v; n  } n  for (int i=1; i<=ts; i++) sort(all(vec[i])); n  for (int j=1; j<LOG; ++j) for (int i=1; i<=ts; i++) par[j][i]=par[j-1][par[j-1][i]]; n  for (int i=0; i<n; i++){ n   inc[m-1]=m-1; n   for (int j=m-2; ~j; j--){ n    if (S[i][j]<=S[i][j+1]) inc[j]=inc[j+1]; n    else inc[j]=j; n   } n   int l=i+1; n   for (int lcp=m-1; ~lcp; lcp--){ n    int dwn=l-1, up=n; n    while (up-dwn>1){ n     int mid=(dwn+up)>>1; n     int tmp=Lcp(S[i], S[mid]); n     if (tmp<lcp) up=mid; n     else dwn=mid; n    } n    int r=up; n    if (l==r) continue ; n //   debug2(inc[lcp], GetPar(dest[i], inc[lcp])) n    res+=Count(vec[GetPar(dest[i], inc[lcp]+1)], l, r); n     n //   cerr<<S[i]<<   <<lcp<<     <<l<<   <<r<<  n ; n //   debug(res) n //   cerr<<  n ; n     n    l=r; n   } n  } n  return res; n } n  n int main(){ n  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); n  //freopen( input.txt ,  r , stdin); n  //freopen( output.txt ,  w , stdout); n  cin>>n; n  for (int i=0; i<n; i++){ n   cin>>S[i]; n   vector<int> shit(26, 0); n   for (char ch:S[i]) shit[ch- a ]++; n   fuck[shit].pb(S[i]); n  } n  m=S[0].size(); n  out=1337ll*n*(n-1)/2; n  for (auto p:fuck){ n   n=0; n   for (string s:p.second) S[n++]=s; n   out-=1337ll*n*(n-1)/2; n   out+=2ll*n*(n-1)/2; n   ans+=Solve(); n  } n  debug(ans) n  cout<<out-ans<<  n ; n   n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n 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 << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   return x * F; n } n int add(int x, int y) { n   return (x + y >= 998244353) ? x + y - 998244353 : x + y; n } n int dec(int x, int y) { return (x - y < 0) ? x - y + 998244353 : x - y; } n int mul(int x, int y) { return 1LL * x * y % 998244353; } n int n; n pair<int, int> a[800000 + 5]; n int dp[800000 + 5][7], pre[800000 + 5]; n int ans[800000 + 5], pt[800000 + 5]; n int cnt; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = pair<int, int>(read(), i); n   sort(a + 1, a + n + 1); n   memset(dp, 0x3f, sizeof(dp)); n   dp[1][1] = 0, pre[1] = 1; n   for (int i = 2; i <= n + 1; i++) { n     for (int j = 1; j <= 5; j++) { n       dp[i][j + 1] = n           min(dp[i][j + 1], dp[i - 1][j] - a[i - 1].first + a[i].first); n       if (j >= 3) { n         if (dp[i][1] > dp[i - 1][j]) { n           dp[i][1] = dp[i - 1][j]; n           pre[i] = j; n         } n       } n     } n     if (dp[i][1] > dp[i - 1][6]) { n       dp[i][1] = dp[i - 1][6]; n       pre[i] = 6; n     } n   } n   printf( %d  , dp[n + 1][1]); n   int p = n + 1; n   while (p >= 1) { n     int nxt = p - pre[p]; n     p = nxt; n     cnt++; n     ans[p] = cnt; n   } n   printf( %d n , cnt - 1); n   for (int i = 1; i <= n; i++) n     if (!ans[i]) ans[i] = ans[i - 1]; n   for (int i = 1; i <= n; i++) pt[a[i].second] = ans[i]; n   for (int i = 1; i <= n; i++) printf( %d  , pt[i]); n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s[400000]; n int bl[400000], S[400000], sz, n, m; n void dfs(int x, int y) { n   bl[x] = y; n   ++sz; n   for (int z : s[x]) n     if (z != 1 && !bl[z]) dfs(z, y); n } n void dft(int x) { n   S[x] = sz; n   for (int y : s[x]) n     if (y != 1 && !S[y]) dft(y); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     s[x].insert(y); n     s[y].insert(x); n   } n   if (s[1].count(n)) return printf( 1 n1 %d n , n), 0; n   for (int x : s[1]) n     if (s[x].count(n)) return printf( 2 n1 %d %d n , x, n), 0; n   for (int x : s[1]) n     for (int y : s[x]) n       if (s[y].count(n)) return printf( 3 n1 %d %d %d n , x, y, n), 0; n   for (int x : s[1]) n     for (int y : s[x]) n       if (y != 1 && !s[1].count(y)) n         return printf( 4 n1 %d %d 1 %d n , x, y, n), 0; n   if (!s[1].size()) return puts( -1 ), 0; n   for (int x : s[1]) n     if (!bl[x]) { n       sz = 0; n       dfs(x, x); n       dft(x); n     } n   int mn = n + 10, mid = 0; n   for (int x : s[1]) n     if (S[x] != s[x].size()) { n       if (s[x].size() < mn) { n         mn = s[x].size(); n         mid = x; n       } n     } n   if (mid) { n     puts( 5 ); n     for (int y : s[mid]) n       if (y != 1) n         for (int z : s[y]) n           if (z != 1 && z != mid && !s[mid].count(z)) n             return printf( 1 %d %d %d %d %d n , mid, y, z, mid, n), 0; n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mx = 19; n const long long mx1 = 262150; n long long s[mx]; n long long n, m, k; n long long extra[mx][mx]; n long long dp[mx][mx1]; n long long solve(long long prev, long long bitmask, long long m) { n   if (m == 0) return 0; n   if (dp[prev][bitmask] != -1) return dp[prev][bitmask]; n   long long ans = INT_MIN; n   for (long long j = 0; j < mx; j++) { n     if (bitmask & (1 << j)) n       ans = max(ans, extra[prev][j + 1] + s[j + 1] + n                          solve(j + 1, bitmask ^ (1 << j), m - 1)); n   } n   return dp[prev][bitmask] = ans; n } n signed main() { n   cin >> n >> m >> k; n   for (long long i = 1; i <= n; i++) cin >> s[i]; n   long long a, b, c; n   memset(extra, 0, sizeof(extra)); n   for (long long i = 0; i < k; i++) { n     cin >> a >> b >> c; n     extra[a][b] = c; n   } n   long long bitmask = (1 << n) - 1; n   long long prev = 0; n   memset(dp, -1, sizeof(dp)); n   cout << solve(prev, bitmask, m) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long LINF = 1e17; n const double DINF = numeric_limits<double>::max(); n const int ITER = 300; n const int MOD = 1e9 + 7; n const double EPS = 1e-10; n const int MAXN = 1e5 + 10; n int n; n vector<int> g[MAXN]; n int B[MAXN]; n int dp[MAXN][2]; n void mod(int v) { n   for (int i = 0; i < 2; i++) dp[v][i] %= MOD; n } n void dfs(int v) { n   dp[v][0] = 1 - B[v]; n   dp[v][1] = B[v]; n   for (int u : g[v]) { n     int old[2]; n     old[0] = dp[v][0]; n     old[1] = dp[v][1]; n     dp[v][0] = dp[v][1] = 0; n     dfs(u); n     dp[v][0] += (1ll * old[0] * dp[u][1]) % MOD; n     dp[v][1] += (1ll * old[1] * dp[u][1]) % MOD; n     mod(v); n     dp[v][1] += (1ll * old[1] * dp[u][0]) % MOD; n     mod(v); n     dp[v][1] += (1ll * old[0] * dp[u][1]) % MOD; n     mod(v); n     dp[v][0] += (1ll * old[0] * dp[u][0]) % MOD; n     mod(v); n   } n } n void solve() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int p; n     cin >> p; n     g[p].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     cin >> B[i]; n   } n   dfs(0); n   cout << dp[0][1] << endl; n } n int main() { n   cout << setprecision(12) << fixed; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[3000010], v[2 * 1000010 + 5]; n int main() { n   int n, k, i, mn, ans, j; n   scanf( %d%d , &n, &k); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   mn = a[0]; n   for (i = 1; i < n; i++) mn = min(mn, a[i]); n   if (mn <= k + 1) { n     cout << mn; n     return 0; n   } n   ans = k + 1; n   for (i = 0; i < n; i++) v[a[i]]++; n   for (i = 1; i <= 2 * 1000010; i++) v[i] += v[i - 1]; n   for (i = k + 2; i <= mn; i++) { n     int cnt = 0; n     for (j = i; j <= 1000010; j += i) cnt += (v[j + k] - v[j - 1]); n     if (cnt == n) ans = i; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> N; n   int x = 0; n   int res = 0; n   if (N == 1) { n     printf( 1 n ); n     printf( 1 n ); n     return 0; n   } n   for (int i = 1; i <= N; i++) { n     x += i; n     if (x > N) { n       res = i - 1; n       break; n     } n   } n   printf( %d n , res); n   for (int i = 1; i < res; i++) { n     printf( %d  , i); n     N -= i; n   } n   printf( %d n , N); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxint = -1u >> 1; n template <class T> n bool get_max(T& a, const T& b) { n   return b > a ? a = b, 1 : 0; n } n template <class T> n bool get_min(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n int k; n int num[26]; n string s; n int main() { n   cin >> k; n   cin >> s; n   memset(num, 0, sizeof(num)); n   for (int i = 0; i < s.size(); i++) num[s[i] -  a ]++; n   int flag = 1; n   for (int i = 0; i < 26; i++) n     if (num[i] % k != 0) { n       flag = 0; n     } n   if (flag == 0) n     puts( -1 ); n   else { n     string ans; n     for (int i = 0; i < k; i++) n       for (int j = 0; j < 26; j++) n         for (int g = 0; g < num[j] / k; g++) { n           ans = ans + char(j +  a ); n         } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sav, mn = 1e9; n struct point { n   long long x, y, uz; n } p[2005]; n string s; n int det(point A, point B, point C) { n   if (A.x * B.y + A.y * C.x + B.x * C.y - A.x * C.y - A.y * B.x - B.y * C.x < 0) n     return -1; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i].x >> p[i].y; n     if (p[i].y < mn) { n       mn = p[i].y; n       sav = i; n     } n   } n   cin >> s; n   p[sav].uz = 1; n   cout << sav <<    ; n   for (int i = 0; i < s.size(); i++) { n     int crt = 1, j, sgn; n     while (p[crt].uz) crt++; n     j = crt + 1; n     if (s[i] ==  R ) n       sgn = 1; n     else n       sgn = -1; n     while (j <= n) { n       while (j <= n && p[j].uz) j++; n       if (j > n) break; n       if (det(p[sav], p[crt], p[j]) == sgn) crt = j; n       j++; n     } n     sav = crt; n     p[crt].uz = 1; n     cout << sav <<    ; n   } n   for (int i = 1; i <= n; i++) n     if (!p[i].uz) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m; n   cin >> n >> m; n   vector<string> v, vec; n   for (long long i = 1; i <= n; i++) { n     string s; n     cin >> s; n     v.push_back(s); n   } n   for (long long i = 1; i <= m; i++) { n     string s; n     cin >> s; n     vec.push_back(s); n   } n   long long q; n   cin >> q; n   while (q--) { n     long long a; n     cin >> a; n     long long u = a % v.size(); n     cout << v[(u - 1 + v.size()) % v.size()]; n     u = a % vec.size(); n     cout << vec[(u - 1 + vec.size()) % vec.size()]; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void z_func(const string &s, vector<int> &z) { n   int L = 0, R = 0; n   int n = s.length(); n   z.resize(n); n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n int main() { n   string str; n   cin >> str; n   vector<int> z; n   z_func(str, z); n   int maxz = 0, res = 0; n   int n = (int)str.length(); n   for (int i = 0; i < (int)str.length(); i++) { n     if (z[i] == n - i) { n       if (maxz >= z[i]) res = max(res, z[i]); n     } n     maxz = max(maxz, z[i]); n   } n   if (res == 0) n     cout <<  Just a legend  << endl; n   else n     cout << str.substr(0, res) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ll n, a, b; n   cin >> n >> a >> b; n   ll tot = 6 * n; n   ll ans = 1e18; n   if (a * b >= tot) { n     cout << a * b <<   n  << a <<     << b <<   n ; n   } else { n     bool swp = false; n     if (a > b) { n       swp = true; n       swap(a, b); n     } n     ll na = a, nb = b; n     for (ll i = 1; i * i <= tot; i++) { n       ll j = tot / i; n       if (i * j < tot) j++; n       if (i >= a && j >= b) { n         if (i * j >= tot && i * j <= ans) { n           ans = i * j; n           na = i; n           nb = j; n         } n       } n     } n     if (swp) swap(na, nb); n     cout << na * nb <<   n  << na <<     << nb <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n char buf[1 << 23], *p1 = buf, *p2 = buf; n template <typename T> n inline void read(T &x) { n   x = 0; n   register char ch = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   while (!(ch >= 48 && ch <= 57)) n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   while ((ch >= 48 && ch <= 57)) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n }  // namespace IO n using namespace IO; n const int N = 1e5 + 10, inf = 0x3f3f3f3f; n struct ALTXDY { n   int tp, t, x, y, id; n   bool operator<(ALTXDY const &a) const { n     return x == a.x ? tp < a.tp : x < a.x; n   } n }; n ALTXDY q[N << 1]; n int n, m, k, tot, rt, bus[N], ans[N], c[N << 1], dat[N << 2], ls[N << 2], n     rs[N << 2]; n void update(int &i, int l, int r, int x, int k) { n   if (!i) i = ++tot; n   if (bus[dat[i]] < bus[k]) dat[i] = k; n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (x <= mid) n     update(ls[i], l, mid, x, k); n   else n     update(rs[i], mid + 1, r, x, k); n } n int query(int i, int l, int r, int x, int y, int val) { n   if (l == r) return dat[i]; n   int mid = (l + r) >> 1, t = -1; n   if (x <= mid && ls[i] && bus[dat[ls[i]]] >= val) n     t = query(ls[i], l, mid, x, y, val); n   if (t == -1 && y >= mid + 1 && rs[i] && bus[dat[rs[i]]] >= val) n     t = query(rs[i], mid + 1, r, x, y, val); n   return t; n } n int main() { n   read(n, m); n   for (int i = 1; i <= n; i++) { n     int s, f, t; n     read(s, f, t); n     q[i] = (ALTXDY){0, t, s, f, i}; n     c[++k] = t; n     bus[i] = f; n   } n   for (int i = 1; i <= m; i++) { n     int b, l, r; n     read(l, r, b); n     q[i + n] = (ALTXDY){1, b, l, r, i}; n     c[++k] = b; n   } n   sort(c + 1, c + 1 + k); n   k = unique(c + 1, c + 1 + k) - c - 1; n   for (int i = 1; i <= n + m; i++) n     q[i].t = lower_bound(c + 1, c + 1 + k, q[i].t) - c; n   sort(q + 1, q + 1 + n + m); n   for (int i = 1; i <= n + m; i++) n     if (q[i].tp == 0) n       update(rt, 1, k, q[i].t, q[i].id); n     else n       ans[q[i].id] = query(rt, 1, k, q[i].t, k, q[i].y); n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n double am, vm2, d, tant, ti, vm, t, s, daux, tfin; n int main() { n   scanf( %d %lf %lf , &n, &am, &d); n   tant = 0; n   for (i = 0; i < n; i++) { n     scanf( %lf %lf , &ti, &vm); n     t = (vm / am); n     s = 0.5 * (vm * vm / am); n     daux = d - s; n     if (daux < 0) { n       vm2 = sqrt(2 * d * am); n       tfin = vm2 / am; n       tfin += ti; n     } else { n       tfin = daux / vm; n       tfin += (ti + t); n     } n     if (tfin > tant) { n       tant = tfin; n     } n     printf( %.10lf n , tant); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace io { n const int L = (1 << 20) + 1; n char buf[L], *S, *T, c; n char getchar() { n   if (__builtin_expect(S == T, 0)) { n     T = (S = buf) + fread(buf, 1, L, stdin); n     return (S == T ? EOF : *S++); n   } n   return *S++; n } n int inp() { n   int x = 0, f = 1; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x * f; n } n unsigned inpu() { n   unsigned x = 0; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     ; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x; n } n long long inp_ll() { n   long long x = 0; n   int f = 1; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x * f; n } n char B[25], *outs = B + 20, *outr = B + 20; n template <class T> n inline void print(register T a, register char x = 0) { n   if (x) *--outs = x, x = 0; n   if (!a) n     *--outs =  0 ; n   else n     while (a) *--outs = (a % 10) + 48, a /= 10; n   if (x) *--outs = x; n   fwrite(outs, outr - outs, 1, stdout); n   outs = outr; n } n };  // namespace io n using io ::inp; n using io ::inp_ll; n using io ::inpu; n using io ::print; n using i32 = int; n using i64 = long long; n using u8 = unsigned char; n using u32 = unsigned; n using u64 = unsigned long long; n using f64 = double; n using f80 = long double; n long long power(long long a, long long b, long long p) { n   if (!b) return 1; n   long long t = power(a, b / 2, p); n   t = t * t % p; n   if (b & 1) t = t * a % p; n   return t; n } n template <class T> n inline void freshmin(T &a, const T &b) { n   if (a > b) a = b; n } n template <class T> n inline void freshmax(T &a, const T &b) { n   if (a < b) a = b; n } n const int MAXN = 510; n const int MAXP = 10000000; n const u64 MOD = 1000000007; n const f80 MI = f80(1) / MOD; n const long long INF = 10000000000000000LL; n const long long inf = 5000000000000LL; n int n; n struct node { n   long long a, b, k; n   void init() { scanf( %lld%lld%lld , &a, &b, &k); } n   bool operator<(const node &A) const { return b > A.b; } n } p[MAXN]; n long long F[MAXN][MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) p[i].init(); n   sort(p + 1, p + n + 1); n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= i; ++j) { n       F[i][j] = F[i - 1][j] + max(p[i].a - p[i].b * p[i].k, 0LL); n       if (j) freshmax(F[i][j], F[i - 1][j - 1] + p[i].a - p[i].b * (j - 1)); n     } n   cout << *max_element(F[n], F[n] + n + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long N = 2e5 + 5; n long long gcd(long long n, long long m) { n   if (n == 0) n     return m; n   else n     return gcd(m % n, n); n } n long long a[105], b[105]; n vector<pair<long long, long long> > v1, v2; n long long dp[103][60000]; n long long go(long long pos, long long r) { n   if (r < 0) return -MOD; n   if (pos == v2.size()) { n     return 0; n   } n   long long &ans = dp[pos][r]; n   if (ans != -1) return ans; n   ans = go(pos + 1, r); n   if (r >= a[v2[pos].second]) { n     ans = max(ans, go(pos + 1, r + b[v2[pos].second]) + 1); n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, r; n   cin >> n >> r; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i] >> b[i]; n     if (b[i] >= 0) { n       v1.push_back({a[i], b[i]}); n     } else { n       v2.push_back({-(a[i] + b[i]), i}); n     } n   } n   memset(dp, -1, sizeof(dp)); n   if (v1.size() > 0) { n     sort(v1.begin(), v1.end()); n     long long ans = 0; n     for (long long i = 0; i < v1.size(); i++) { n       if (r >= v1[i].first) { n         r += v1[i].second; n         ans++; n       } n     } n     if (v2.size() == 0) { n       cout << ans << endl; n       return 0; n     } n     sort(v2.begin(), v2.end()); n     long long k = go(0, r); n     cout << ans + k << endl; n   } else { n     sort(v2.begin(), v2.end()); n     cout << go(0, r) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n; n long long f[N][2], down[N], g[N], h[N], a[N]; n vector<int> e[N]; n template <typename T> n inline void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n inline void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= f; n } n inline void DFS(int u, int par) { n   f[u][0] = f[u][1] = down[u] = g[u] = a[u]; n   for (unsigned i = 0; i < e[u].size(); ++i) { n     int v = e[u][i]; n     if (v == par) continue; n     DFS(v, u); n     chkmax(f[u][0], f[v][0]); n     chkmax(f[u][0], f[u][1] + f[v][1]); n     chkmax(f[u][0], down[u] + g[v]); n     chkmax(f[u][0], down[v] + g[u]); n     chkmax(f[u][1], f[v][1]); n     chkmax(f[u][1], down[u] + down[v]); n     chkmax(g[u], g[v] + a[u]); n     chkmax(g[u], down[u] + f[v][1]); n     chkmax(g[u], down[v] + a[u] + h[u]); n     chkmax(h[u], f[v][1]); n     chkmax(down[u], a[u] + down[v]); n   } n } n int main() { n   read(n); n   for (int i = 1; i <= n; ++i) read(a[i]); n   for (int i = 1; i < n; ++i) { n     int u, v; n     read(u); n     read(v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   DFS(1, 0); n   printf( %lld n , f[1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x, y, i, j, a1 = 0, a2 = 0, b1 = 0, b2 = 0; n   cin >> n >> m; n   int a[n], b[m]; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < m; i++) cin >> b[i]; n   for (i = 0; i < n; i++) { n     if (a[i] % 2 == 1) n       a1++; n     else n       a2++; n   } n   for (i = 0; i < m; i++) { n     if (b[i] % 2 == 1) n       b1++; n     else n       b2++; n   } n   x = a1; n   if (b2 < x) x = b2; n   y = a2; n   if (b1 < y) y = b1; n   cout << x + y; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, i, j, k, l, x, y; n int a[200010]; n struct node { n   int i; n }; n bool operator<(node x, node y) { return a[x.i] < a[y.i]; } n multiset<node> S; n multiset<node>::iterator p, p2; n int li[200010]; n vector<pair<int, int> > v; n int main() { n   scanf( %d%d , &n, &s); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) S.insert((node){i}); n   while (!S.empty()) { n     if (a[(*S.begin()).i] == 0) { n       S.erase(S.begin()); n       continue; n     } n     p = --S.end(); n     x = (*p).i; n     S.erase(p); n     for (li[0] = 0; a[x]; a[x]--) n       if (S.empty()) n         return puts( No ), 0; n       else { n         p2 = --S.end(); n         y = (*p2).i; n         S.erase(p2); n         li[++li[0]] = y; n         a[y]--; n         v.push_back(make_pair(x, y)); n       } n     for (; li[0]; li[0]--) S.insert((node){li[li[0]]}); n   } n   if (S.empty()) { n     printf( Yes n ); n     printf( %d n , s / 2); n     for (int k = 0; k < v.size(); k++) n       printf( %d %d n , v[k].first, v[k].second); n   } else n     printf( No n ); n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int read() { n   char ch =    ; n   int x = 0; n   bool flag = false; n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = true; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return flag ? -x : x; n } n struct heap { n   vector<pair<int, int> > v; n   int loc[N]; n   heap() { n     v.push_back(make_pair(-1, -1)); n     memset(loc, -1, sizeof loc); n   } n   void push(pair<int, int> p) { n     int cur; n     cur = v.size(); n     loc[p.second] = cur; n     v.push_back(p); n     while (v[cur] < v[cur >> 1]) { n       swap(v[cur], v[cur >> 1]); n       swap(loc[v[cur].second], loc[v[cur >> 1].second]); n       cur >>= 1; n     } n   } n   void pop() { n     if (empty()) return; n     swap(v[1], v.back()); n     loc[v[1].second] = 1; n     loc[v.back().second] = -1; n     v.pop_back(); n     int cur = 1; n     while ((cur << 1) < v.size()) { n       if ((cur << 1) + 1 == v.size()) { n         if (v[cur] > v[cur << 1]) n           swap(v[cur], v[cur << 1]), n               swap(loc[v[cur].second], loc[v[cur << 1].second]); n         break; n       } n       if (v[cur] < min(v[cur << 1], v[(cur << 1) + 1])) break; n       if (v[cur << 1] < v[(cur << 1) + 1]) { n         swap(v[cur], v[cur << 1]); n         swap(loc[v[cur].second], loc[v[cur << 1].second]); n         cur <<= 1; n       } else { n         swap(v[cur], v[(cur << 1) + 1]); n         swap(loc[v[cur].second], loc[v[(cur << 1) + 1].second]); n         cur = (cur << 1) + 1; n       } n     } n   } n   pair<int, int> top() { return v[1]; } n   bool empty() { return v.size() == 1; } n }; n int n, m; n vector<int> bad[N]; n vector<pair<int, int> > v[N]; n int d[N]; n heap pq; n int main() { n   n = read(), m = read(); n   for (int i = 0; i < m; i++) { n     int a = read(), b = read(), c = read(); n     --a, --b; n     v[a].push_back(make_pair(b, c)); n     v[b].push_back(make_pair(a, c)); n   } n   for (int i = 0; i < n; i++) { n     int num = read(); n     while (num--) { n       bad[i].push_back(read()); n     } n   } n   for (int i = 0; i < n; i++) d[i] = 0x3f3f3f3f; n   d[0] = 0; n   pq.push(make_pair(0, 0)); n   while (!pq.empty()) { n     pair<int, int> tp = pq.top(); n     pq.pop(); n     int t = tp.first; n     int cur = tp.second; n     if (t != d[cur]) continue; n     int j = 0; n     while (j < bad[cur].size() && bad[cur][j] < t) j++; n     while (j < bad[cur].size() && bad[cur][j] == t) t++, j++; n     for (int i = 0; i < v[cur].size(); i++) { n       int to = v[cur][i].first, cost = v[cur][i].second; n       if (t + cost < d[to]) { n         d[to] = t + cost; n         pq.push(make_pair(d[to], to)); n       } n     } n   } n   if (d[n - 1] >= 0x3f3f3f3f) n     puts( -1 ); n   else n     cout << d[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int atoi(string s) { n   int sol = 0, st = 1; n   for (int i = 0; i < int(s.size()); i++) { n     sol += (s[i] - 48) * st; n     st *= 10; n   } n   return sol; n } n int main() { n   string a, b; n   cin >> a >> b; n   reverse(a.begin(), a.end()); n   cout << atoi(a) + atoi(b) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const long long ooo = 9223372036854775807ll; n const int _cnt = 1000 * 1000 + 7; n const int _p = 1000 * 1000 * 1000 + 7; n const int N = 1001005; n const double PI = acos(-1.0); n const double eps = 1e-9; n int o(int x) { return x % _p; } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n void file_put() { n   freopen( filename.in ,  r , stdin); n   freopen( filename.out ,  w , stdout); n } n int p[N], prime[N], phi[N], top, T, X, P, K, pp[N]; n long long l, r, mid, ans; n void init_prime(int N) { n   top = 0, phi[1] = 1; n   for (int i = 2; i <= N; ++i) { n     if (!p[i]) prime[++top] = i, phi[i] = i - 1; n     for (int j = 1; j <= top; ++j) { n       if ((long long)i * prime[j] > N) break; n       p[i * prime[j]] = 1; n       if (i % prime[j] == 0) { n         phi[i * prime[j]] = phi[i] * prime[j]; n         break; n       } n       phi[i * prime[j]] = phi[i] * phi[prime[j]]; n     } n   } n } n void init(int x) { n   pp[0] = 0; n   for (int i = 1; i <= top && (long long)prime[i] * prime[i] <= x; i++) n     if (x % prime[i] == 0) { n       pp[++pp[0]] = prime[i]; n       while (x % prime[i] == 0) x /= prime[i]; n       if (x == 1) return; n       if (!p[x]) { n         pp[++pp[0]] = x; n         return; n       } n     } n   if (x > 1) pp[++pp[0]] = x; n } n void dfs(int k, int op, long long s, long long n, long long &ans) { n   if (k > pp[0]) { n     ans += n / s * op; n     return; n   } n   dfs(k + 1, op, s, n, ans); n   if (s * pp[k] <= n) dfs(k + 1, -op, s * pp[k], n, ans); n } n long long Count(long long x) { n   long long ans = 0; n   dfs(1, 1, 1, x % P, ans); n   return x / P * phi[P] + ans; n } n int main() { n   scanf( %d , &T); n   init_prime(N - 5); n   while (T--) { n     scanf( %d%d%d , &X, &P, &K), init(P); n     K += Count(X), l = 1, r = 1e8; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (Count(mid) >= K) n         ans = mid, r = mid - 1; n       else n         l = mid + 1; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(long long x, long long y) { return x > y; } n int main() { n   long long n; n   cin >> n; n   string S; n   cin >> S; n   vector<long long> C; n   for (long long i = 0; i < n; i++) { n     long long x = S[i] -  0 ; n     if (x == 0 || x == 1) n       continue; n     else if (x == 2) n       C.push_back(2); n     else if (x == 3) n       C.push_back(3); n     else if (x == 4) n       C.push_back(2), C.push_back(2), C.push_back(3); n     else if (x == 5) n       C.push_back(5); n     else if (x == 6) n       C.push_back(5), C.push_back(3); n     else if (x == 7) n       C.push_back(7); n     else if (x == 8) n       C.push_back(7), C.push_back(2), C.push_back(2), C.push_back(2); n     else if (x == 9) n       C.push_back(7), C.push_back(3), C.push_back(3), C.push_back(2); n   } n   sort(C.begin(), C.end(), cmp); n   for (auto it : C) cout << it; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0; n   char c = getchar(); n   while (c <  0  ||  9  < c) c = getchar(); n   while ( 0  <= c && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return x; n } n int p[200000 + 5]; n int tp, stk[200000 + 5]; n int U[200000 + 5], V[200000 + 5]; n map<pair<int, int>, bool> Map; n int main() { n   int n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     U[i] = read(), V[i] = read(); n     Map[make_pair(U[i], V[i])] = 1; n     Map[make_pair(V[i], U[i])] = 1; n   } n   for (int i = 1; i <= n; i++) p[i] = i; n   for (int t = 100; t >= 0; t--) { n     random_shuffle(p + 1, p + n + 1); n     p[n + 1] = p[1]; n     tp = 0; n     for (int i = 1; i <= n && tp < m; i++) n       if (!Map.count(make_pair(p[i], p[i + 1]))) stk[++tp] = i; n     if (tp == m) { n       for (int i = 1; i <= m; i++) printf( %d %d n , p[stk[i]], p[stk[i] + 1]); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n #pragma GCC optimize( fast-math ) n #pragma GCC optimize  -O3  n long long INF = (long long)1e20; n int iINF = (int)1e20; n long long mod = 1e9 + 7; n long long mult(long long a, long long b) { n   if (a == 0) return 0; n   if (a == 1) return (b % mod); n   if (a % 2) { n     long long kek = mult(a / 2, b) % mod; n     kek *= 2; n     kek %= mod; n     kek += b; n     kek %= mod; n     return kek; n   } else { n     long long kek = mult(a / 2, b) % mod; n     kek *= 2; n     kek %= mod; n     return kek; n   } n } n long long f(long long r) { n   long long odd = 0, even = 0; n   long long now = 1; n   bool chet = false; n   while (r > 0) { n     long long num = min(r, now); n     if (chet) n       even += num; n     else n       odd += num; n     r -= now; n     now *= 2; n     chet = !chet; n   } n   return (mult(odd, odd) + mult(even, even) + even) % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long l, r; n   cin >> l >> r; n   cout << (f(r) - f(l - 1) + mod) % mod; n } n


#include <bits/stdc++.h> n int in[301]; n int c[301]; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n, i, j; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , in + i); n   for (i = 1; i <= n; i++) scanf( %d , c + i); n   for (i = 1; i <= n; i++) { n     for (j = 2; j * j <= in[i]; j++) { n       if (in[i] % j == 0) { n         while (in[i] % j == 0) in[i] /= j; n         in[i] *= j; n       } n     } n   } n   std::map<int, int> dp, dt; n   dp[0] = 0; n   for (i = 1; i <= n; i++) { n     for (auto t : dp) { n       int xx = gcd(t.first, in[i]); n       int xy = t.second + c[i]; n       if (dp.count(xx) == 0 || dp[xx] > xy) { n         if (dt.count(xx) == 0 || dt[xx] > xy) dt[xx] = xy; n       } n     } n     for (auto t : dt) { n       dp[t.first] = t.second; n     } n     dt.clear(); n   } n   if (dp.count(1) == 0) n     printf( -1 ); n   else n     printf( %d , dp[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int n = 0; n   bool f = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     n = (n << 1) + (n << 3) + (c ^ 48); n     c = getchar(); n   } n   return f ? -n : n; n } n int r, c, n, t, x, y, ans, res; n bool m[101][101]; n int main() { n   r = read(), c = read(), n = read(), t = read(); n   for (int i = 1; i <= n; i++) { n     x = read(), y = read(); n     m[x][y] = 1; n   } n   for (int i = 1; i <= r; i++) n     for (int j = 1; j <= c; j++) n       for (int k = i; k <= r; k++) n         for (int h = j; h <= c; h++) { n           res = 0; n           for (int a = i; a <= k; a++) n             for (int b = j; b <= h; b++) n               if (m[a][b]) res++; n           if (res >= t) ans++; n         } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool umax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n long long POW(long long a, long long p, long long M) { n   if (!p) return 1LL; n   long long T = POW(a, p / 2, M); n   T = T * T % M; n   if (p & 1) T = T * (a % M) % M; n   return T; n } n long long SQRT(long long a) { n   long long b = (long long)sqrtl(((double)a) + 0.5); n   while (b * b < a) ++b; n   while (b * b > a) --b; n   return (b * b == a) ? b : -1; n } n const long long MOD = 1e9 + 7; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(10); n   cout << fixed; n   long long n, m; n   cin >> n >> m; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   vector<vector<long long>> guess(n + 1); n   long long f = 0; n   long long N = 2e5; n   long long fq[4 * N]; n   memset(fq, 0, sizeof(fq)); n   for (long long i = 1; i <= n; i++) fq[N + i] = 1; n   for (long long i = 1; i <= n; i++) { n     long long mx = min(n, i + m + 1); n     f += 2 * (mx - i + 1) - 1; n   } n   long long st; n   for (long long i = 1; i <= m; i++) { n     long long a; n     cin >> a; n     guess[a].push_back(i); n   } n   for (long long i = 1; i <= n; i++) { n     if (guess[i].empty()) continue; n     long long ss = guess[i][0]; n     for (long long j = 1; j <= guess[i].size(); ++j) { n       if (j < guess[i].size() && guess[i][j] == guess[i][j - 1] + 1) n         continue; n       else { n         long long ct = 0; n         for (long long k = i - ss; k >= i - guess[i][j - 1]; --k) { n           long long org = min(n, k + m + 1); n           long long wt = min(n, k + m - (guess[i][j - 1] + (k - i) + 1) + 1); n           f -= fq[N + k] * (org - wt); n           ct += fq[N + k]; n           fq[N + k] = 0; n         } n         fq[N + i - guess[i][j - 1] - 1] += ct; n         if (j < guess[i].size()) ss = guess[i][j]; n       } n     } n   } n   memset(fq, 0, sizeof(fq)); n   for (long long i = 1; i <= n; i++) fq[i] = 1; n   for (long long i = n; i >= 1; i--) { n     if (guess[i].empty()) continue; n     long long ss = guess[i][0]; n     for (long long j = 1; j <= guess[i].size(); ++j) { n       if (j < guess[i].size() && guess[i][j] == guess[i][j - 1] + 1) n         continue; n       else { n         long long ct = 0; n         for (long long k = i + ss; k <= i + guess[i][j - 1]; ++k) { n           long long org = max(1LL, k - m - 1); n           long long wt = max(1LL, k - m + (guess[i][j - 1] - (k - i) + 1) - 1); n           f -= fq[k] * (wt - org); n           ct += fq[k]; n           fq[k] = 0; n         } n         fq[i + guess[i][j - 1] + 1] += ct; n         if (j < guess[i].size()) ss = guess[i][j]; n       } n     } n   } n   cout << f << endl; n   cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n inline bool check2(long long x, long long y3) { n   return (x * x + y3 * y3 * 3 <= n * n); n } n inline bool check(long long x, long long y3) { n   return check2(x - 2, y3) & check2(x - 1, y3 - 1) & check2(x + 1, y3 - 1) & n          check2(x + 2, y3) & check2(x + 1, y3 + 1) & check2(x - 1, y3 + 1); n } n long long calc(long long x, long long d) { n   long long l = 0, r = 3e6; n   while (l < r) { n     long long mid = (l + r + 1) / 2; n     long long y3 = d + mid * 2; n     if (check(x, y3)) n       l = mid; n     else n       r = mid - 1; n   } n   long long ret = r * 2; n   if (d == 0 && check(x, 0)) ret += 1; n   if (d == 1 && check(x, d)) ret += 2; n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   n *= 2; n   long long s = 0; n   int x = 3, d = 1; n   while (true) { n     long long t = calc(x, d); n     if (t == 0) break; n     s += t; n     x += 3, d ^= 1; n   } n   s *= 2; n   s += calc(0, 0); n   cout << s << endl; n } n


#include <bits/stdc++.h> n inline int getint() { n   register char ch; n   while (!isdigit(ch = getchar())) n     ; n   register int x = ch ^  0 ; n   while (isdigit(ch = getchar())) x = (((x << 2) + x) << 1) + (ch ^  0 ); n   return x; n } n const long long mod = 1e9 + 7; n const int N = 150001, M = 150002, K = 100001; n void exgcd(const long long &a, const long long &b, long long &x, long long &y) { n   if (!b) { n     x = 1; n     y = 0; n     return; n   } n   exgcd(b, a % b, y, x); n   y -= a / b * x; n } n inline long long inv(const long long &x) { n   long long tmp, ret; n   exgcd(x, mod, ret, tmp); n   return (ret + mod) % mod; n } n long long f[2][M]; n long long p[M], q[M]; n long long fact[K], tcaf[K]; n int k; n inline long long calc(const int &x) { n   return fact[k] * tcaf[k - x] % mod * tcaf[x] % mod * p[x] % mod * q[k - x] % n          mod; n } n signed main() { n   int n = getint(), m = getint(); n   long long a = getint(), b = getint(); n   k = getint(); n   fact[0] = 1; n   for (register int i = 1; i <= k; i++) { n     fact[i] = fact[i - 1] * i % mod; n   } n   tcaf[k] = inv(fact[k]); n   for (register int i = k - 1; ~i; i--) { n     tcaf[i] = tcaf[i + 1] * (i + 1) % mod; n   } n   p[0] = q[0] = 1; n   p[1] = a * inv(b) % mod; n   q[1] = (b - a) * inv(b) % mod; n   for (register int i = 2; i <= k; i++) { n     p[i] = p[i - 1] * p[1] % mod; n     q[i] = q[i - 1] * q[1] % mod; n   } n   f[0][m] = 1; n   for (register int i = 1; i <= n; i++) { n     long long s1 = 0, s2 = 0; n     for (register int j = 0; j <= m; j++) { n       f[i & 1][j] = (s1 * (f[!(i & 1)][m] - f[!(i & 1)][m - j]) - s2) % mod * n                     calc(m - j) % mod; n       s1 = (s1 + calc(j)) % mod; n       s2 = (s2 + f[!(i & 1)][j] * calc(j)) % mod; n     } n     for (register int j = 1; j <= m; j++) { n       f[i & 1][j] = (f[i & 1][j] + f[i & 1][j - 1]) % mod; n     } n   } n   printf( %I64d n , (f[n & 1][m] + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int x = 0, t = 1; n   register char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) t = -1, ch = getchar(); n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch - 48, ch = getchar(); n   return x * t; n } n struct Line { n   int v, next; n }; n struct Link { n   Line e[444444 << 1]; n   int h[444444], cnt; n   void Add(int u, int v) { n     e[++cnt] = (Line){v, h[u]}; n     h[u] = cnt++; n     e[++cnt] = (Line){u, h[v]}; n     h[v] = cnt++; n   } n } Gr, Tr; n multiset<int> MS[444444]; n int W[444444]; n int n, m, Q, Tot; n struct Graph { n   int dfn[444444], low[444444], S[444444], tim, top; n   void Tarjan(int u) { n     dfn[u] = low[u] = ++tim; n     S[++top] = u; n     for (int i = Gr.h[u]; i; i = Gr.e[i].next) { n       int v = Gr.e[i].v; n       if (!dfn[v]) { n         Tarjan(v); n         low[u] = min(low[u], low[v]); n         if (low[v] >= dfn[u]) { n           Tr.Add(++Tot, u); n           int x; n           do { n             x = S[top--]; n             Tr.Add(Tot, x); n           } while (v != x); n         } n       } else n         low[u] = min(low[u], dfn[v]); n     } n   } n } G; n struct SegMentTree { n   int t[444444 << 2]; n   void Modify(int now, int l, int r, int p, int w) { n     if (l == r) { n       t[now] = w; n       return; n     } n     int mid = (l + r) >> 1; n     if (p <= mid) n       Modify((now << 1), l, mid, p, w); n     else n       Modify((now << 1 | 1), mid + 1, r, p, w); n     t[now] = min(t[(now << 1)], t[(now << 1 | 1)]); n   } n   int Query(int now, int l, int r, int L, int R) { n     if (L <= l && r <= R) return t[now]; n     int ret = 1e9, mid = (l + r) >> 1; n     if (L <= mid) ret = min(ret, Query((now << 1), l, mid, L, R)); n     if (R > mid) ret = min(ret, Query((now << 1 | 1), mid + 1, r, L, R)); n     return ret; n   } n } SMT; n int fa[444444], tim, dfn[444444], low[444444]; n int size[444444], hson[444444], top[444444], dep[444444]; n void dfs1(int u, int ff) { n   fa[u] = ff; n   dep[u] = dep[ff] + 1; n   size[u] = 1; n   if (u <= n && ff) MS[ff].insert(W[u]); n   for (int i = Tr.h[u]; i; i = Tr.e[i].next) { n     int v = Tr.e[i].v; n     if (v == ff) continue; n     dfs1(v, u); n     size[u] += size[v]; n     if (size[v] > size[hson[u]]) hson[u] = v; n   } n } n void dfs2(int u, int tp) { n   top[u] = tp; n   dfn[u] = ++tim; n   low[tim] = u; n   if (hson[u]) dfs2(hson[u], tp); n   for (int i = Tr.h[u]; i; i = Tr.e[i].next) n     if (Tr.e[i].v != fa[u] && Tr.e[i].v != hson[u]) dfs2(Tr.e[i].v, Tr.e[i].v); n } n int Query(int u, int v) { n   int ret = 1e9; n   while (top[u] ^ top[v]) { n     if (dep[top[u]] < dep[top[v]]) swap(u, v); n     ret = min(ret, SMT.Query(1, 1, Tot, dfn[top[u]], dfn[u])); n     u = fa[top[u]]; n   } n   if (dep[u] > dep[v]) swap(u, v); n   ret = min(ret, SMT.Query(1, 1, Tot, dfn[u], dfn[v])); n   if (u <= n) n     return ret; n   else n     return min(ret, W[fa[u]]); n } n void Modify(int u, int w) { n   if (fa[u]) { n     MS[fa[u]].erase(MS[fa[u]].find(W[u])); n     MS[fa[u]].insert(w); n     SMT.Modify(1, 1, Tot, dfn[fa[u]], *MS[fa[u]].begin()); n   } n   W[u] = w; n   SMT.Modify(1, 1, Tot, dfn[u], w); n } n int main() { n   Tot = n = read(); n   m = read(); n   Q = read(); n   W[0] = 1e9; n   for (int i = 1; i <= n; ++i) W[i] = read(); n   for (int i = 1; i <= m; ++i) Gr.Add(read(), read()); n   G.Tarjan(1); n   dfs1(1, 0); n   dfs2(1, 1); n   for (int i = 1; i <= n; ++i) SMT.Modify(1, 1, Tot, dfn[i], W[i]); n   for (int i = n + 1; i <= Tot; ++i) n     SMT.Modify(1, 1, Tot, dfn[i], *MS[i].begin()); n   char ch[2]; n   while (Q--) { n     scanf( %s , ch); n     int a = read(), b = read(); n     if (ch[0] ==  C ) n       Modify(a, b); n     else n       printf( %d n , Query(a, b)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5005][5005], bad[30][5005], pre[5005]; n const long long MOD = 1e9 + 7; n long long fact[5005]; n long long qikpow(long long a, long long b) { n   if (!b) { n     return 1; n   } n   long long ret = qikpow(a, b / 2); n   ret *= ret; n   ret %= MOD; n   if (b & 1) { n     ret *= a; n     ret %= MOD; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int N; n   cin >> N; n   fact[0] = 1; n   dp[0][0] = 1; n   pre[0] = 1; n   for (int i = 1; i <= N; i++) { n     char c; n     cin >> c; n     fact[i] = (fact[i - 1] * i) % MOD; n     for (int k = 1; k <= i; k++) { n       dp[i][k] = pre[k - 1]; n       dp[i][k] = (dp[i][k] - bad[c -  a ][k - 1] + MOD) % MOD; n       dp[i][k] = (dp[i][k] - bad[c -  a ][k] + MOD) % MOD; n     } n     for (int k = 1; k <= i; k++) { n       bad[c -  a ][k] = (bad[c -  a ][k] + dp[i][k]) % MOD; n       pre[k] = (pre[k] + dp[i][k]) % MOD; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= N; i++) { n     ans += pre[i] * fact[N - 1] % MOD * n            qikpow(fact[i - 1] * fact[N - i] % MOD, MOD - 2) % MOD; n     ans %= MOD; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int n, s[3000 + 5], c[3000 + 5], f[3000 + 5][5]; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n int main() { n   n = read(); n   memset(f, inf, sizeof(f)); n   for (int i = 1; i <= n; i++) s[i] = read(); n   for (int i = 1; i <= n; i++) c[i] = read(); n   for (int i = 1; i <= n; i++) { n     f[i][1] = c[i]; n     for (int j = 1; j < i; j++) n       if (s[j] < s[i]) f[i][2] = min(f[j][1] + c[i], f[i][2]); n     for (int j = 1; j < i; j++) n       if (s[j] < s[i]) f[i][3] = min(f[j][2] + c[i], f[i][3]); n   } n   int ans = inf; n   for (int i = 1; i <= n; i++) n     if (f[i][3]) ans = min(ans, f[i][3]); n   if (ans == inf) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long a, sum, l, i, b; n   char ch[10]; n   while (scanf( %I64d , &a) != EOF) { n     getchar(); n     scanf( %s , ch); n     l = strlen(ch); n     b = 0; n     for (i = l - 1; i > 0; i--) b = (ch[i] -  0  + b) * 10; n     b = ch[0] -  0  + b; n     sum = a + b; n     printf( %I64d n , sum); n   } n } n


#include <bits/stdc++.h> n int x[5], j = 1, b, d, ans, t, i, u = 0; n void GCD(int a, int b) { n   if (a == b) { n     x[j] = a; n     j++; n   } else if (a < b) n     GCD(b - a, a); n   else n     GCD(a - b, b); n } n int main() { n   scanf( %d , &b); n   scanf( %d , &d); n   GCD(b - 1, d); n   GCD(b, d); n   GCD(b + 1, d); n   GCD(x[1], x[3]); n   t = (x[1] * x[2] * x[3] / x[4]) % d; n   for (i = 1; i <= 8; i++) { n     if (t == 0) { n       u = i; n       break; n     } else n       t = (t * x[2]) % d; n   } n   if (u == 0) n     ans = 7; n   else { n     if (x[1] == 1 && x[3] == 1) n       ans = 2; n     else if (x[2] == 1 && x[3] == 1) n       ans = 3; n     else if (x[1] == 1 && x[2] == 1) n       ans = 11; n     else if (x[1] == 1 || x[3] == 1) n       ans = 6; n     else if (x[2] > 1) n       ans = 6; n     else if (x[3] == 2) n       ans = 3; n     else if (x[1] == 2 && x[3] > 2) n       ans = 11; n     else n       ans = 6; n   } n   printf( %d-type , ans); n   if (ans == 2) printf(  n%d , u); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 50; n long long dp[N]; n void solve() { n   long long n; n   cin >> n; n   long long count = 0; n   while (n > 0) { n     long long quotient = n / 3; n     long long rem = n % 3; n     n = n / 3; n     dp[count] = rem; n     count++; n   } n   for (long long i = 0; i < N; i++) { n     if (dp[i] == 0 || dp[i] == 1) n       continue; n     else { n       dp[i] = -1; n       dp[i + 1] += 1; n     } n   } n   long long finalAns = 0; n   for (long long i = N - 1; i >= 0; i--) { n     if (dp[i] == 0) continue; n     if (dp[i] == -1) n       break; n     else { n       long long coeff = dp[i]; n       finalAns += pow(3, i); n     } n   } n   cout << finalAns << endl; n   for (long long i = 0; i < N; i++) dp[i] = 0; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = int(1e9 + 7), MAXN = int(1e3 + 50); n int n, s[MAXN], br, raz, z[MAXN]; n vector<int> v[MAXN]; n vector<pair<pair<int, int>, int>> rez; n void dfs(int i, int ot) { n   s[i] = 1; n   for (auto j : v[i]) n     if (j != ot) { n       dfs(j, i); n       s[i] += s[j]; n     } n } n int centroid() { n   dfs(1, -1); n   int c = 1, ot = -1; n   while (1) { n     int br = 1; n     for (auto j : v[c]) n       if (j != ot && 2 * s[j] >= n) { n         br = 0; n         ot = c; n         c = j; n         break; n       } n     if (br) break; n   } n   return c; n } n void dfs2(int i, int ot) { n   br += raz; n   z[i] = br; n   rez.push_back({{i, ot}, z[i] - z[ot]}); n   for (auto j : v[i]) n     if (j != ot) dfs2(j, i); 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 - 1; i++) { n     int p, q; n     cin >> p >> q; n     v[p].push_back(q); n     v[q].push_back(p); n   } n   if (n == 1) { n     exit(0); n   } n   if (n == 2) { n     printf( 1 2 1 n ); n     exit(0); n   } n   int c = centroid(); n   dfs(c, -1); n   vector<pair<int, int>> v2, g1, g2; n   multiset<pair<int, vector<int>>> sv; n   for (auto i : v[c]) { n     vector<int> vv{i}; n     sv.insert({s[i], vv}); n   } n   while (sv.size() > 2) { n     auto pom1 = *sv.begin(); n     sv.erase(sv.begin()); n     auto pom2 = *sv.begin(); n     sv.erase(sv.begin()); n     vector<int> vu; n     for (auto i : pom1.second) vu.push_back(i); n     for (auto i : pom2.second) vu.push_back(i); n     sv.insert({pom1.first + pom2.first, vu}); n   } n   auto a = *sv.begin(); n   sv.erase(sv.begin()); n   auto b = *sv.begin(); n   sv.erase(sv.begin()); n   z[c] = 0; n   br = 0; n   raz = 1; n   for (auto i : a.second) dfs2(i, c); n   br = 0; n   raz = a.first + 1; n   for (auto i : b.second) dfs2(i, c); n   for (auto i : rez) n     printf( %d %d %d n , i.first.first, i.first.second, i.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const uint64_t seed = n     std::chrono::system_clock::now().time_since_epoch().count(); n mt19937_64 rnd(seed); n const int MOD = 998244353; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   set<pair<long long, long long>> ss; n   int cnt = 0; n   long long x = 1, y = 0; n   auto has = [&](int v) { n     long long d = (v - y) / x; n     auto it = ss.upper_bound({d, LLONG_MAX}); n     if (it != ss.begin()) { n       it = prev(it); n       return it->first <= d && it->second >= d; n     } n     return false; n   }; n   for (int p : a) { n     if (p % 2 == 0 && has(p / 2)) { n       ss.clear(); n       ss.insert({p / 2, p / 2}); n       x = 1, y = 0; n       continue; n     }; n     x = -x; n     y = p - y; n     cnt++; n     if (p % 2 == 0) { n       long long d = (p / 2 - y) / x; n       ss.insert({d, d}); n     } n     long long r1 = (1 - y) / x; n     long long r2 = (p - 1 - y) / x; n     if (r1 > r2) swap(r1, r2); n     for (auto it = ss.begin(); it != ss.end();) { n       long long aa, bb; n       tie(aa, bb) = *it; n       aa = max(aa, r1); n       bb = min(bb, r2); n       if (aa > bb) n         it = ss.erase(it); n       else { n         ss.erase(it); n         ss.insert({aa, bb}); n         break; n       } n     } n     for (auto it = ss.rbegin(); it != ss.rend();) { n       long long aa, bb; n       tie(aa, bb) = *it; n       aa = max(aa, r1); n       bb = min(bb, r2); n       it = decltype(it){ss.erase(next(it).base())}; n       if (aa <= bb) { n         ss.insert({aa, bb}); n         break; n       } n     } n     if (ss.empty()) { n       cnt++; n       ss.insert({r1, r2}); n     } n   } n   cout << cnt <<   n ; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.precision(10); n   int T = 1; n   cin >> T; n   for (int i = 1; i <= T; i++) { n     solve(); n   } n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200005; n long long n, m, u, v, k; n long long flag; n long long a[N]; n long long x, y, z; n long long aa, bb, cc, dd; n long long ans[100005]; n long long sum; n void init() {} n long long gcd(long long x, long long y) { n   if (!y) return x; n   return gcd(y, x % y); n } n struct node { n   long long x, id; n   bool operator<(const node t) const { return x < t.x; } n }; n long long getn(long long x, long long y, long long z) { n   long long n; n   cout <<  ?   << x <<     << y <<     << z << endl; n   cin >> n; n   return n; n } n void solve() { n   init(); n   cin >> n; n   sum = 0; n   for (long long i = 1; i <= n; i++) { n     a[i] = 0; n   } n   for (long long i = 0; i < n / 3; i++) { n     ans[i] = getn(i * 3 + 1, i * 3 + 2, i * 3 + 3); n     if (!ans[i]) { n       u = i; n     } else { n       v = i; n     } n   } n   aa = getn(u * 3 + 1, u * 3 + 2, v * 3 + 1); n   bb = getn(u * 3 + 1, u * 3 + 2, v * 3 + 2); n   cc = getn(v * 3 + 1, v * 3 + 2, u * 3 + 1); n   dd = getn(v * 3 + 1, v * 3 + 2, u * 3 + 2); n   if (aa + bb == 0) { n     x = u * 3 + 1; n   } else { n     x = u * 3 + 3; n   } n   a[x] = 0; n   if (cc + dd == 2) { n     y = v * 3 + 1; n   } else { n     y = v * 3 + 3; n   } n   a[y] = 1; n   for (long long i = 0; i < n / 3; i++) { n     if (ans[i]) { n       aa = getn(i * 3 + 1, i * 3 + 2, x); n       bb = getn(i * 3 + 2, i * 3 + 3, x); n       if (aa + bb == 0) { n         a[i * 3 + 1] = 1; n         a[i * 3 + 2] = 0; n         a[i * 3 + 3] = 1; n       } else if (aa + bb == 2) { n         a[i * 3 + 1] = 1; n         a[i * 3 + 2] = 1; n         a[i * 3 + 3] = 1; n       } else if (aa == 1) { n         a[i * 3 + 1] = 1; n         a[i * 3 + 2] = 1; n         a[i * 3 + 3] = 0; n       } else { n         a[i * 3 + 1] = 0; n         a[i * 3 + 2] = 1; n         a[i * 3 + 3] = 1; n       } n     } else { n       aa = getn(i * 3 + 1, i * 3 + 2, y); n       bb = getn(i * 3 + 2, i * 3 + 3, y); n       if (aa + bb == 0) { n         a[i * 3 + 1] = 0; n         a[i * 3 + 2] = 0; n         a[i * 3 + 3] = 0; n       } else if (aa + bb == 2) { n         a[i * 3 + 1] = 0; n         a[i * 3 + 2] = 1; n         a[i * 3 + 3] = 0; n       } else if (aa == 1) { n         a[i * 3 + 1] = 1; n         a[i * 3 + 2] = 0; n         a[i * 3 + 3] = 0; n       } else { n         a[i * 3 + 1] = 0; n         a[i * 3 + 2] = 0; n         a[i * 3 + 3] = 1; n       } n     } n   } n   for (long long i = 1; i <= n; i++) { n     if (a[i] == 0) sum++; n   } n   cout <<  !   << sum <<    ; n   for (long long i = 1; i <= n; i++) { n     if (a[i] == 0) cout << i <<    ; n   } n   cout << endl; n } n signed main() { n   long long t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(const int a, const int b) { n   if (a <= b) { n     return false; n   } n   return true; n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     string s; n     cin >> s; n     int last_count = 0; n     vector<int> arr; n     for (int j = 0; j < s.length(); j++) { n       if (j == 0) { n         if (s[0] ==  1 ) { n           last_count++; n         } n       } else { n         if (s[j] ==  0 ) { n           if (last_count != 0) { n             arr.push_back(last_count); n           } n           last_count = 0; n         } else { n           last_count++; n         } n       } n     } n     if (last_count != 0) { n       arr.push_back(last_count); n     } n     if (arr.size() == 0) { n       cout << 0 << endl; n     } else { n       sort(arr.begin(), arr.end(), compare); n       int alice_Count = 0; n       for (int j = 0; j < arr.size(); j++) { n         if (j % 2 == 0) { n           alice_Count += arr[j]; n         } n       } n       cout << alice_Count << endl; n     } n   } n   return 0; n } n