#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > g[110]; n char s[2]; n int n, m, x, y, f[110][110][27]; n int dp(int x, int y, int k) { n   if (f[x][y][k] != 0) return f[x][y][k]; n   int ret = 1; n   for (auto e : g[x]) n     if (e.second >= k) ret = min(ret, dp(y, e.first, e.second)); n   f[x][y][k] = -ret; n   return f[x][y][k]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%s , &x, &y, s); n     g[x].push_back(pair<int, int>(y, s[0] -  a )); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       for (int k = 0; k < 26; k++) f[i][j][k] = dp(i, j, k); n       if (f[i][j][0] > 0) n         cout <<  A ; n       else n         cout <<  B ; n     } n     cout << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n char s[500010]; n int n, m, ver[500010], hor[500010]; n bool b; n void calc(int i, int j, int k) { n   if (!b) return; n   int x = (k < 3) ? 1 : 2; n   if (j % 2) x = 3 - x; n   if (hor[i] == 0) n     hor[i] = x; n   else if (hor[i] != x) { n     b = false; n     return; n   } n   x = (k == 1 || k == 4) ? 1 : 2; n   if (i % 2) x = 3 - x; n   if (ver[j] == 0) n     ver[j] = x; n   else if (ver[j] != x) { n     b = false; n     return; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   memset(ver, 0, sizeof(ver)); n   memset(hor, 0, sizeof(hor)); n   b = true; n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     for (int j = 0; j < m; j++) n       if (s[j] !=  . ) calc(i, j, s[j] -  0 ); n   } n   if (!b) n     printf( 0 n ); n   else { n     int ans = 1; n     for (int i = 0; i < n; i++) n       if (hor[i] == 0) ans = ans * 2 % 1000003; n     for (int i = 0; i < m; i++) n       if (ver[i] == 0) ans = ans * 2 % 1000003; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma optimize( -O3 ) n int cnt; n int t[1200005]; n int lazy[1200005]; n long long int ans[300005]; n void push(int v, int tl, int tr) { n   int tm = (tl + tr) / 2; n   if (lazy[v] == 2) { n     t[v * 2] = tm - tl + 1; n     t[2 * v + 1] = tr - tm; n     lazy[2 * v] = 2; n     lazy[2 * v + 1] = 2; n     lazy[v] = 0; n   } else if (lazy[v] == 3) { n     t[2 * v] = 0; n     t[2 * v + 1] = 0; n     lazy[2 * v] = 3; n     lazy[2 * v + 1] = 3; n     lazy[v] = 0; n   } else if (lazy[v]) { n     t[2 * v] = (tm - tl + 1) - t[2 * v]; n     t[2 * v + 1] = (tr - tm) - t[2 * v + 1]; n     lazy[2 * v] ^= 1; n     lazy[2 * v + 1] ^= 1; n     lazy[v] = 0; n   } n } n void update(int v, int tl, int tr, int l, int r, int type) { n   if (l > r) return; n   if (l == tl && tr == r) { n     push(v, tl, tr); n     if (type == 1) { n       t[v] = (tr - tl + 1); n       lazy[v] = 2; n     } else if (type == 2) { n       t[v] = 0; n       lazy[v] = 3; n     } else if (type == 3) { n       t[v] = (tr - tl + 1) - t[v]; n       lazy[v] ^= 1; n     } n   } else { n     push(v, tl, tr); n     int tm = (tl + tr) / 2; n     update(v * 2, tl, tm, l, min(r, tm), type); n     update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, type); n     t[v] = (t[v * 2] + t[v * 2 + 1]); n   } n } n int first(int v, int tl, int tr) { n   if (tl == tr) { n     return tl; n   } n   int tm = (tl + tr) / 2; n   push(v, tl, tr); n   if (t[v * 2] != (tm - tl + 1)) { n     return first(v * 2, tl, tm); n   } else { n     return first(v * 2 + 1, tm + 1, tr); n   } n } n void solve() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   vector<pair<int, pair<long long int, long long int>>> v; n   set<long long int> se; n   for (int i = 0; i < n; i++) { n     long long int t, l, r; n     cin >> t >> l >> r; n     v.push_back({t, {l, r}}); n     se.insert(l); n     se.insert(r + 1); n   } n   se.insert(1); n   map<long long int, int> m; n   int pt = 0; n   for (auto u : se) { n     m[u] = pt; n     ans[pt] = u; n     pt++; n   }; n   for (auto u : v) { n     cnt++; n     int ti = u.first; n     int l = m[u.second.first]; n     int r = m[u.second.second + 1] - 1; n     ; n     update(1, 0, 200001, l, r, ti); n     int val = first(1, 0, 200001); n     cout << ans[val] <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:256000000 ) n FILE *stream; n const int INF = 1e9 + 7; n const long long INFL = (long long)1e18 + 1; n const int SIZE = 2e5 + 10; n const int TSIZE = 1e3 + 10; n const long double eps = 0.000001; n const long long MOD = 1e9 + 7; n char POW(char a, char b) { n   if (b == 0) return 1; n   char cur = POW(a, b / 2); n   if (b % 2 == 0) n     return cur * cur; n   else n     return cur * cur * a; n } n long long gcd(long long &x, long long &y, long long a, long long b) { n   if (a == 0) { n     y = 1; n     x = 0; n     return abs(b); n   } n   long long x1 = 0, y1 = 0; n   long long d = gcd(x1, y1, b % a, a); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n long long gcd(long long a, long long b) { n   if (a == 0) { n     return abs(b); n   } n   long long d = gcd(b % a, a); n   return d; n } n bool cmp(pair<int, int> a, pair<int, int> b) { n   return (long long)a.first * b.second > (long long)a.second * b.first; n } n void accept() { n   int n; n   long double ans = INFL; n   cin >> n; n   vector<pair<long long, long long> > c(n + 2); n   for (long long i = 0; i < n; i++) { n     cin >> c[i].first >> c[i].second; n     if (i < 2) { n       c[n + i] = c[i]; n     } n   } n   for (int i = 1; i <= n + 1; i++) { n     long long x = c[i].first, y = c[i].second; n     long long xl = c[i - 1].first, yl = c[i - 1].second; n     long long xr = c[i + 1].first, yr = c[i + 1].second; n     long long a = yr - yl; n     long long b = xl - xr; n     long long z = xl * (yl - yr) + yl * (xr - xl); n     long double cans = (a * x + b * y + z) / sqrt(a * a + b * b) / 2; n     if (cans < 0) cans *= -1; n     if (i == 1) ans = cans; n     if (cans < ans) ans = abs(a * x + b * y + z) / sqrt(a * a + b * b) / 2; n   } n   cout << fixed << setprecision(10) << ans; n } n void solution() { n   long long t = 1; n   for (long long i = 0; i < t; i++) { n     accept(); n   } n   return; n } n void test() { n   cout <<  100000 n ; n   for (int k = 0; k < 100000 - 1; k++) { n     cout << k + 1 <<    ; n   } n   cout <<  100000 n ; n } n int main() { n   srand(time(0)); n   solution(); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n const int maxn=200010; n int a[maxn],f[maxn]; n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     int t,n; n     cin>>t; n     while(t--){ n         memset(f,0,sizeof(f)); n         cin>>n; n         for(int i=0;i<n;i++)cin>>a[i]; n         sort(a,a+n); n         f[a[0]]=1; n         for(int i=1;i<n;i++){ n             for(int j=1;j*j<=a[i];j++){ n                 if(a[i]%j)continue; n                 f[a[i]]=max({f[a[i]],f[j]+1,f[a[i]/j]+1}); n             } n         } n         int ans=0; n         for(int i=1;i<=200000;i++)ans=max(ans,f[i]); n         cout<<n-ans<<  n ; n          n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, m, a[100005] = {}; n   cin >> n; n   m = n; n   while (n--) { n     cin >> x; n     a[x] = 1; n     while (a[m]) { n       cout << m <<    ; n       m--; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int i, a, v, max, min, min1; n   cin >> a; n   max = min = a; n   for (i = 1; i < n; i++) { n     cin >> v; n     if (v > max) max = v; n     if (v < min) min = v; n   } n   cin >> a; n   min1 = a; n   for (i = 1; i < m; i++) { n     cin >> v; n     if (v < min1) min1 = v; n   } n   if ((2 * min < max) && (min1 > max)) { n     cout << max; n     return 0; n   } n   if ((2 * min) < min1 && min1 > max) { n     cout << (2 * min); n     return 0; n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string dp_func(string str) { n   if (str.size() & 1) return str; n   string gf, my; n   for (int i = 0; i < str.size() / 2; i++) my += str[i]; n   for (int i = str.size() / 2; str[i]; i++) gf += str[i]; n   string pts = dp_func(gf); n   string rts = dp_func(my); n   if (pts > rts) swap(pts, rts); n   return pts + rts; n } n int main() { n   string str, str2; n   cin >> str >> str2; n   str = dp_func(str); n   str2 = dp_func(str2); n   if (str == str2) n     printf( YES n ); n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c <=  9  && c >=  0 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n const int maxn = 500007 * 30; n int rt = 0; n struct Segtree { n   int tot; n   int ls[maxn], rs[maxn], tag[maxn], sum[maxn]; n   void pushdown(int x, int l, int r) { n     int mid = l + r >> 1; n     if (l != r) { n       if (!ls[x]) ls[x] = ++tot; n       if (!rs[x]) rs[x] = ++tot; n       sum[ls[x]] = (mid - l + 1) * tag[x]; n       sum[rs[x]] = (r - mid) * tag[x]; n       tag[ls[x]] = tag[x]; n       tag[rs[x]] = tag[x]; n     } n     tag[x] = -1; n   } n   void modify(int &x, int l, int r, int L, int R, int w) { n     if (!x) x = ++tot; n     if (l >= L && r <= R) { n       sum[x] = (r - l + 1) * w; n       tag[x] = w; n       return; n     } n     if (tag[x] >= 0) pushdown(x, l, r); n     int mid = l + r >> 1; n     if (L <= mid) modify(ls[x], l, mid, L, R, w); n     if (R > mid) modify(rs[x], mid + 1, r, L, R, w); n     sum[x] = sum[ls[x]] + sum[rs[x]]; n   } n } t; n int main() { n   int n = read(); n   int m = read(); n   memset(t.tag, -1, sizeof t.tag); n   for (int type, l, r, i = 1; i <= m; ++i) { n     l = read(), r = read(), type = read(); n     if (type == 1) n       t.modify(rt, 1, n, l, r, 1); n     else n       t.modify(rt, 1, n, l, r, 0); n     printf( %d n , n - t.sum[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[1010][1010]; n bool got[1010][1010]; n bool ok(int x) { n   memset(got, 0, sizeof(got)); n   for (int j = 0; j < m; j++) { n     vector<int> vt; n     for (int i = 0; i < n; i++) n       if (a[i][j] >= x) vt.push_back(i); n     for (int o = 0; o < vt.size(); o++) n       for (int p = o + 1; p < vt.size(); p++) n         if (got[vt[o]][vt[p]]) n           return true; n         else n           got[vt[o]][vt[p]] = true; n   } n   return false; n } n int t[1000010]; n int main() { n   int cnt = 0; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       scanf( %d , &a[i][j]); n       t[cnt++] = a[i][j]; n     } n   sort(t, t + n * m); n   int l = 0, r = unique(t, t + n * m) - t - 1; n   while (l < r) { n     int m = (l + r + 1) >> 1; n     if (ok(t[m])) n       l = m; n     else n       r = m - 1; n   } n   printf( %d n , t[l]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = (long long)5e3 + 555; n void ndr() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); } n int divi(long long N) { n   int cnt(0); n   for (long long i = 1; i <= sqrt(N); i++) { n     if (N % i == 0) { n       if (N / i == i) n         cnt++; n       else n         cnt += 2; n     } n   } n   return cnt; n } n bool cp(int x, int y) { return x > y; } n unsigned long long numberOfSetBits(long long i) { n   i = i - ((i >> 1) & 0x55555555); n   i = (i & 0x33333333) + ((i >> 2) & 0x33333333); n   return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; n } n bool Taxi(string X) { n   for (long long i = (long long)1; i < (long long)X.size(); i++) { n     if (X[i] != X[i - 1]) return false; n   } n   return true; n } n bool Pizza(string X) { n   for (long long i = (long long)1; i < (long long)X.size(); i++) { n     if (X[i] >= X[i - 1]) return false; n   } n   return true; n } n signed main() { n   ndr(); n   vector<multimap<int, string>> Phone_Numbers(3); n   multimap<int, string>::iterator it, it1; n   int Friends; n   cin >> Friends; n   while (Friends--) { n     int N; n     cin >> N; n     string S; n     cin >> S; n     int taxi = 0, pizza = 0, girl = 0; n     while (N--) { n       string s; n       cin >> s; n       for (long long i = (long long)0; i < (long long)s.size(); i++) n         if (s[i] ==  - ) { n           s.erase(s.begin() + i); n           i--; n         } n       if (Taxi(s)) n         taxi++; n       else if (Pizza(s)) n         pizza++; n       else n         girl++; n     } n     Phone_Numbers[0].insert({taxi, S}); n     Phone_Numbers[1].insert({pizza, S}); n     Phone_Numbers[2].insert({girl, S}); n   } n   for (long long i = (long long)0; i < (long long)3; i++) { n     if (i == 0) n       cout <<  If you want to call a taxi, you should call: ; n     else if (i == 1) n       cout <<  If you want to order a pizza, you should call: ; n     else n       cout <<  If you want to go to a cafe with a wonderful girl, you should   n                call: ; n     it1 = Phone_Numbers[i].end(); n     --it1; n     for (it = Phone_Numbers[i].lower_bound(it1->first);; it++) { n       if (it == it1) { n         cout <<     << it->second <<  .  << endl; n         break; n       } else n         cout <<     << it->second <<  , ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 5e3 + 10; n const int MOD = 1e9 + 7; n int n; n char s[MAX_N]; n int dp[26][MAX_N]; n int sum[MAX_N]; n int cb[MAX_N][MAX_N]; n int main() { n   int i, l, j; n   scanf( %d , &n); n   scanf( %s , s + 1); n   cb[0][0] = 1; n   for (i = 1; i <= n; i++) { n     cb[i][0] = 1; n     for (j = 1; j <= i; j++) { n       cb[i][j] = cb[i - 1][j - 1] + cb[i - 1][j]; n       if (cb[i][j] >= MOD) cb[i][j] -= MOD; n     } n   } n   s[i] =  # ; n   for (i = 1; i <= n; i++) { n     if (s[i] == s[i - 1]) continue; n     if (!dp[s[i] -  a ][1]) { n       dp[s[i] -  a ][1] = 1; n       sum[1]++; n     } n     for (l = 2; l <= n; l++) { n       sum[l] -= dp[s[i] -  a ][l]; n       if (sum[l] < 0) sum[l] += MOD; n       dp[s[i] -  a ][l] = sum[l - 1] - dp[s[i] -  a ][l - 1]; n       if (dp[s[i] -  a ][l] < 0) dp[s[i] -  a ][l] += MOD; n       sum[l] += dp[s[i] -  a ][l]; n       if (sum[l] >= MOD) sum[l] -= MOD; n     } n   } n   int ans = 0; n   for (i = 1; i <= n; i++) { n     ans += (long long)((long long)sum[i] * (long long)cb[n - 1][i - 1]) % MOD; n     if (ans >= MOD) ans -= MOD; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string simplify(string s) { n   string a; n   for (int i = 0; i < s.length(); i++) { n     if (isalpha(s[i])) { n       s[i] = tolower(s[i]); n       a.push_back(s[i]); n     } n   } n   return a; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   string s1, s2, s3; n   cin >> s1 >> s2 >> s3; n   s1 = simplify(s1); n   s2 = simplify(s2); n   s3 = simplify(s3); n   int x = s1.length(), y = s2.length(), z = s3.length(); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     s = simplify(s); n     if (s.length() == x + y + z && n         (s == s1 + s2 + s3 || s == s1 + s3 + s2 || s == s2 + s1 + s3 || n          s == s2 + s3 + s1 || s == s3 + s1 + s2 || s == s3 + s2 + s1)) n       cout <<  ACC n ; n     else n       cout <<  WA n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int i, x, y; n int main() { n   cin >> x >> y; n   i = 1; n   while (1 != 0) { n     if (i % 2 == 1) { n       if (x >= 2 && y >= 2) { n         x -= 2; n         y -= 2; n         i++; n       } else if (y >= 12 && x >= 1) { n         y -= 12; n         x -= 1; n         i++; n       } else if (y >= 22) { n         y -= 22; n         i++; n       } else { n         cout <<  Hanako ; n         return 0; n       } n     } n     if (i % 2 == 0) { n       if (y >= 22) { n         y -= 22; n         i++; n       } else if (y >= 12 && x >= 1) { n         y -= 12; n         x -= 1; n         i++; n       } else if (y >= 2 && x >= 2) { n         y -= 2; n         x -= 2; n         i++; n       } else { n         cout <<  Ciel ; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int arr[123456]; n int prW[123556]; n int leNM[123456]; n int main() { n   cin >> n >> q; n   n--; n   int a, b; n   cin >> a; n   for (int i = 1; i <= (n); ++i) { n     cin >> b; n     arr[i] = abs(a - b); n     a = b; n   } n   vector<pair<int, int> > stack; n   for (int i = 1; i <= (n); ++i) { n     pair<int, int> elt = make_pair(arr[i], -i); n     while (!stack.empty() && elt > stack.back()) { n       prW[-stack.back().second] = i; n       stack.pop_back(); n     } n     stack.push_back(elt); n   } n   while (!stack.empty()) { n     prW[-stack.back().second] = n + 1; n     stack.pop_back(); n   } n   stack.clear(); n   for (int i = n; i >= (1); --i) { n     pair<int, int> elt = make_pair(arr[i], -i); n     while (!stack.empty() && elt > stack.back()) { n       leNM[-stack.back().second] = i; n       stack.pop_back(); n     } n     stack.push_back(elt); n   } n   while (!stack.empty()) { n     leNM[-stack.back().second] = 0; n     stack.pop_back(); n   } n   int l, r; n   long long ans, cur; n   long long modl, modp; n   for (int x = 0; x < (q); ++x) { n     cin >> l >> r; n     --r; n     ans = 0; n     for (int i = l; i <= (r); ++i) { n       cur = arr[i]; n       if (leNM[i] < l) n         modl = i - l + 1; n       else n         modl = i - leNM[i]; n       if (prW[i] > r) n         modp = r - i + 1; n       else n         modp = prW[i] - i; n       cur *= modl; n       cur *= modp; n       ans += cur; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n string s1, s2; n vector<pair<string, string> > ans; n map<string, int> u; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s1 >> s2; n     map<string, int>::iterator it = u.find(s1); n     if (it == u.end()) { n       u[s1] = k, u[s2] = k; n       ans.push_back(make_pair(s1, s2)); n       k++; n     } else { n       u[s2] = it->second; n       ans[it->second].second = s2; n     } n   } n   cout << ans.size() << endl; n   for (size_t i = 0; i < ans.size(); i++) { n     cout << ans[i].first <<     << ans[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nr, K, mod, ans[33], v[70]; n long long N; n struct matrix { n   int a[33][33]; n   matrix() { memset(a, 0, sizeof(a)); } n   matrix(bool type) { n     memset(a, 0, sizeof(a)); n     if (type == 1) n       for (int i = 0; i <= K; i++) a[i][i] = 1; n   } n } dp[70][33], pref[33], suf[33]; n matrix operator*(matrix a, matrix b) { n   matrix c; n   for (int i = 0; i <= K; i++) n     for (int j = 0; j <= K; j++) { n       c.a[i][j] = 0; n       for (int k = 0; k <= K; k++) n         c.a[i][j] = ((long long)c.a[i][j] + 1LL * a.a[i][k] * b.a[k][j]) % mod; n     } n   return c; n } n void Prod(matrix a) { n   int old[33]; n   for (int i = 0; i <= K; i++) old[i] = ans[i]; n   for (int i = 0; i <= K; i++) { n     ans[i] = 0; n     for (int j = 0; j <= K; j++) n       ans[i] = ((long long)ans[i] + 1LL * old[j] * a.a[j][i]) % mod; n   } n } n void Finish() { n   int sum = 0; n   for (int i = 0; i <= K; i++) sum = ((long long)sum + ans[i]) % mod; n   printf( %d n , sum); n } n int main() { n   scanf( %I64d %d , &N, &K), mod = 1e9 + 7; n   long long aux = N; n   while (aux) v[nr++] = aux % K, aux /= K; n   for (int i = 0; i < K; i++) { n     for (int j = 0; j <= K; j++) n       if (j != i) dp[0][i].a[j][j] = 1; n     for (int j = 0; j <= K; j++) dp[0][i].a[j][i] = 1; n   } n   for (int i = 1; i < nr; i++) { n     pref[0] = dp[i - 1][0]; n     for (int j = 1; j < K; j++) pref[j] = pref[j - 1] * dp[i - 1][j]; n     suf[K] = matrix(1); n     for (int j = K - 1; j >= 0; j--) suf[j] = dp[i - 1][j] * suf[j + 1]; n     dp[i][0] = suf[0]; n     for (int j = 1; j < K; j++) dp[i][j] = suf[j] * pref[j - 1]; n   } n   ans[K] = 1; n   int curr = 0; n   for (int i = nr - 1; i >= 0; i--) n     while (v[i]--) Prod(dp[i][curr]), curr = (curr + 1) % K; n   Finish(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200000]; n map<long long, bool> mm; n int main() { n   int n, t, k, d; n   cin >> n >> t >> k >> d; n   int res0 = 0, res1 = 0, sum0 = 0, sum1 = 0; n   while (sum0 < n) res0 += t, sum0 += k; n   for (int i = 1; i <= 10000; i++) { n     if (i % t == 0) sum1 += k, res1 = i; n     if (sum1 >= n) break; n     if (i % (d + t) == 0) sum1 += k, res1 = i; n     if (sum1 >= n) break; n   } n   if (res1 < res0) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 10, mod = 1e9 + 7; n long long ans = 1, t, n, a, b, f[N]; n int main() { n   scanf( %lld , &n); n   f[0] = f[1] = 1; n   for (long long i = 2; i <= n; i++) n     f[i] = (f[i - 1] + f[i - 2] * (i - 1) % mod) % mod; n   for (long long i = 1; i <= n; i++) { n     long long x; n     scanf( %lld , &x); n     if (x == 1) n       a++; n     else n       b++; n   } n   for (long long i = n; i >= n - b + 1; i--) ans = (ans * i) % mod; n   printf( %lld n , ans * f[a] % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t; n bool B1[1000001]; n bool B2[1000001]; n vector<int> R; n int main() { n   int n; n   getline(cin, s); n   getline(cin, t); n   int i = 0; n   bool b = true; n   while (i < t.size()) { n     if (t[i] != s[i]) b = false; n     B1[i] = b; n     i++; n   } n   i = 0; n   b = true; n   while (i < t.size()) { n     if (t[t.size() - i - 1] != s[s.size() - i - 1]) b = false; n     B2[s.size() - i - 1] = b; n     i++; n   } n   for (int i = 0; i < s.size() + 1; i++) { n     bool b1 = true, b2 = true; n     if (i > 0) b1 = B1[i - 1]; n     if (i + 1 < s.size()) b2 = B2[i + 1]; n     if (b1 && b2) R.push_back(i); n   } n   cout << R.size() << endl; n   for (int i = 0; i < R.size(); i++) cout << R[i] + 1 <<    ; n   cin >> n; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000001]; n int main() { n   int n, v = 0; n   cin >> n; n   sprintf(s,  %X , n); n   for (int i = 0; i < strlen(s); i++) { n     if (s[i] ==  0  || s[i] ==  4  || s[i] ==  6  || s[i] ==  9  || n         s[i] ==  A  || s[i] ==  D ) n       v++; n     else if (s[i] ==  8  || s[i] ==  B ) n       v += 2; n   } n   cout << v << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> errors; n void get_codes(unordered_map<int, int>& quantity, int cnt, int inc) { n   for (int i = 0; i < cnt; i++) { n     int code; n     cin >> code; n     quantity[code] += inc; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int bugs; n   cin >> bugs; n   get_codes(errors, bugs, 1); n   get_codes(errors, bugs - 1, -3); n   for (auto it = errors.begin(); it != errors.end(); ++it) { n     if ((it->second) % 2 != 0) { n       cout << (it->first) <<   n ; n       (it->second) -= 1; n     } n   } n   get_codes(errors, bugs - 2, 2); n   for (auto it = errors.begin(); it != errors.end(); ++it) n     if ((it->second) != 0) cout << (it->first) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int head[10010], cnt = 1, cur[10010], dis[N], s, t; n struct edge { n   int next, to, w; n } e[N << 1]; n struct query { n   int u, t; n } q[N]; n int main() { n   auto add = [&](int u, int v, int w) { n     e[++cnt] = {head[u], v, w}; n     head[u] = cnt; n   }; n   auto add_edge = [&](int u, int v, int w) { n     add(u, v, w); n     add(v, u, 0); n   }; n   auto bfs = [&]() { n     for (int i = 1; i <= t; i++) dis[i] = -1; n     dis[s] = 0; n     queue<int> q; n     q.push(s); n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       for (int i = head[u]; i; i = e[i].next) { n         int v = e[i].to; n         if (e[i].w && dis[v] == -1) { n           dis[v] = dis[u] + 1; n           q.push(v); n         } n       } n     } n     return dis[t] != -1; n   }; n   function<int(int, int)> dfs = [&](int u, int flo) { n     if (u == t) return flo; n     int detla = flo; n     for (int &i = cur[u]; i; i = e[i].next) { n       int v = e[i].to; n       if (e[i].w && dis[v] == dis[u] + 1) { n         int d = dfs(v, min(detla, e[i].w)); n         e[i].w -= d; n         e[i ^ 1].w += d; n         detla -= d; n         if (detla == 0) break; n       } n     } n     return flo - detla; n   }; n   auto dini = [&]() { n     int ans = 0; n     while (bfs()) memcpy(cur, head, sizeof cur), ans += dfs(s, INF); n     return ans; n   }; n   auto cal = [&](int l, int r, int k) { n     int L = l - k + 5, R = r - k + 5; n     return R / 5 - L / 5; n   }; n   int n, b, Q; n   scanf( %d %d %d , &n, &b, &Q); n   for (int i = 1; i <= Q; i++) scanf( %d %d , &q[i].u, &q[i].t); n   q[++Q] = query{0, 0}; n   q[++Q] = query{b, n}; n   sort(q + 1, q + Q + 1, [](const query a, const query b) { n     return a.u == b.u ? a.t < b.t : a.u < b.u; n   }); n   for (int i = 2; i <= Q; i++) n     if ((q[i - 1].u == q[i].u && q[i - 1].t < q[i].t) || q[i - 1].t > q[i].t) n       return puts( unfair ); n   int p = 0; n   s = Q + 5 + 1, t = s + 1; n   for (int i = 1; i <= Q; i++) { n     add_edge(s, i, q[i].t - q[i - 1].t); n     p += q[i].t - q[i - 1].t; n     for (int j = 0; j < 5; j++) { n       add_edge(i, Q + j + 1, cal(q[i - 1].u, q[i].u, j)); n     } n   } n   for (int i = 0; i < 5; i++) add_edge(Q + i + 1, t, n / 5); n   puts(dini() == n ?  fair  :  unfair ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, string> a[3005]; n int h[3005]; n vector<pair<string, int> > ans; n int main() { n   int n, m; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i].second >> a[i].first; n   sort(a, a + n); n   for (int i = 0; i < n; i++) n     if (a[i].first > i) { n       cout <<  -1 n ; n       return 0; n     } n   m = h[0] = 0; n   for (int i = 1; i < n; i++) n     m = max(m, h[i] = h[i - 1] + (a[i].first > a[i - 1].first)); n   for (int i = 0; i < n; i++) n     ans.insert(ans.begin() + a[i].first, make_pair(a[i].second, h[i])); n   for (int i = 0; i < n; i++) n     cout << ans[i].first <<     << m - ans[i].second + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int a[3]; n     cin >> a[0] >> a[1] >> a[2]; n     sort(a, a + 3, greater<long long int>()); n     if (a[0] != a[1]) { n       cout <<  NO  n            <<   n ; n     } else { n       cout <<  YES  n            <<   n ; n       cout << a[0] <<     << a[2] <<     << a[2] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int p, pre, k, cnt; n vector<long long int> ans; n int main() { n   scanf( %lld %lld , &p, &k); n   if (p < k) { n     printf( 1 n%lld n , p); n     return 0; n   } n   pre = p; n   while (1) { n     cnt = (k - pre); n     if (cnt <= 0) { n       cnt = cnt / k; n       cnt -= 1; n     } else { n       cnt = cnt / k; n       if (cnt * k == (k - pre)) { n         cnt -= 1; n       } n     } n     pre = cnt; n     if (pre == 0) { n       printf( -1 n ); n       return 0; n     } n     ans.push_back(pre); n     if (pre > 0 && pre < k) { n       break; n     } n   } n   reverse(ans.begin(), ans.end()); n   pre = p; n   for (int i = ans.size() - 1; i >= 0; i--) { n     long long int temp = ans[i] * k + pre; n     pre = ans[i]; n     ans[i] = temp; n   } n   reverse(ans.begin(), ans.end()); n   ans.push_back(pre); n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long v1, v2; n vector<int> sz; n vector<vector<pair<int, int>>> g; n void dfs(int u, int p, int c) { n   sz[u] = 1; n   for (auto &e : g[u]) { n     int v = e.first; n     if (v == p) continue; n     dfs(v, u, e.second); n     sz[u] += sz[v]; n   } n   v1 += 1LL * (sz[u] & 1) * c; n   v2 += 1LL * min(sz[u], 2 * n - sz[u]) * c; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     g.assign(2 * n + 1, vector<pair<int, int>>()); n     for (int i = 1, a, b, c; i <= 2 * n - 1; ++i) { n       cin >> a >> b >> c; n       g[a].push_back({b, c}); n       g[b].push_back({a, c}); n     } n     v1 = v2 = 0; n     sz.assign(2 * n + 1, 0); n     dfs(1, 0, 0); n     cout << v1 <<     << v2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[205], b[205]; n long long dp[205][205], k; n int n, m; n char ans[205]; n bool cmp(int x, int y) { return a[x] < a[y]; } n int main() { n   scanf( %d%d , &n, &m); n   cin >> k; n   int t = n + m - 1; n   for (int i = 0; i < t; i++) { n     a[i] = n * m; n     b[i] = i; n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int tmp; n       scanf( %d , &tmp); n       if (tmp < a[i + j]) a[i + j] = tmp; n     } n   sort(b, b + t, cmp); n   for (int q = 0; q < t; q++) { n     ans[b[q]] =  ( ; n     dp[0][0] = 1; n     for (int i = 0; i < t; i++) n       for (int j = i & 1; j <= i && j <= t - i; j += 2) n         if (dp[i][j]) { n           if (dp[i][j] > k) dp[i][j] = k; n           if (ans[i] !=  ) ) dp[i + 1][j + 1] += dp[i][j]; n           if (ans[i] !=  (  && j) dp[i + 1][j - 1] += dp[i][j]; n           dp[i][j] = 0; n         } n     if (dp[t][0] < k) { n       k -= dp[t][0]; n       ans[b[q]] =  ) ; n     } n     dp[t][0] = 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout.put(ans[i + j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int base = 1000000000; n const int base_digits = 9; n struct bigint { n   std::vector<int> z; n   int sign; n   bigint() : sign(1) {} n   bigint(long long v) { *this = v; } n   bigint(const std::string &s) { read(s); } n   void operator=(const bigint &v) { n     sign = v.sign; n     z = v.z; n   } n   void operator=(long long v) { n     sign = 1; n     if (v < 0) sign = -1, v = -v; n     z.clear(); n     for (; v > 0; v = v / base) z.push_back(v % base); n   } n   bigint operator+(const bigint &v) const { n     if (sign == v.sign) { n       bigint res = v; n       for (int i = 0, carry = 0; n            i < (int)std::max(z.size(), v.z.size()) || carry; ++i) { n         if (i == (int)res.z.size()) res.z.push_back(0); n         res.z[i] += carry + (i < (int)z.size() ? z[i] : 0); n         carry = res.z[i] >= base; n         if (carry) res.z[i] -= base; n       } n       return res; n     } n     return *this - (-v); n   } n   bigint operator-(const bigint &v) const { n     if (sign == v.sign) { n       if (abs() >= v.abs()) { n         bigint res = *this; n         for (int i = 0, carry = 0; i < (int)v.z.size() || carry; ++i) { n           res.z[i] -= carry + (i < (int)v.z.size() ? v.z[i] : 0); n           carry = res.z[i] < 0; n           if (carry) res.z[i] += base; n         } n         res.trim(); n         return res; n       } n       return -(v - *this); n     } n     return *this + (-v); n   } n   void operator*=(int v) { n     if (v < 0) sign = -sign, v = -v; n     for (int i = 0, carry = 0; i < (int)z.size() || carry; ++i) { n       if (i == (int)z.size()) z.push_back(0); n       long long cur = z[i] * (long long)v + carry; n       carry = (int)(cur / base); n       z[i] = (int)(cur % base); n     } n     trim(); n   } n   bigint operator*(int v) const { n     bigint res = *this; n     res *= v; n     return res; n   } n   friend std::pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) { n     int norm = base / (b1.z.back() + 1); n     bigint a = a1.abs() * norm; n     bigint b = b1.abs() * norm; n     bigint q, r; n     q.z.resize(a.z.size()); n     for (int i = a.z.size() - 1; i >= 0; i--) { n       r *= base; n       r += a.z[i]; n       int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : 0; n       int s2 = b.z.size() - 1 < r.z.size() ? r.z[b.z.size() - 1] : 0; n       int d = ((long long)s1 * base + s2) / b.z.back(); n       r -= b * d; n       while (r < 0) r += b, --d; n       q.z[i] = d; n     } n     q.sign = a1.sign * b1.sign; n     r.sign = a1.sign; n     q.trim(); n     r.trim(); n     return std::make_pair(q, r / norm); n   } n   friend bigint sqrt(const bigint &a1) { n     bigint a = a1; n     while (a.z.empty() || a.z.size() % 2 == 1) a.z.push_back(0); n     int n = a.z.size(); n     int firstDigit = (int)sqrt((double)a.z[n - 1] * base + a.z[n - 2]); n     int norm = base / (firstDigit + 1); n     a *= norm; n     a *= norm; n     while (a.z.empty() || a.z.size() % 2 == 1) a.z.push_back(0); n     bigint r = (long long)a.z[n - 1] * base + a.z[n - 2]; n     firstDigit = (int)sqrt((double)a.z[n - 1] * base + a.z[n - 2]); n     int q = firstDigit; n     bigint res; n     for (int j = n / 2 - 1; j >= 0; j--) { n       for (;; --q) { n         bigint r1 = n             (r - (res * 2 * base + q) * q) * base * base + n             (j > 0 ? (long long)a.z[2 * j - 1] * base + a.z[2 * j - 2] : 0); n         if (r1 >= 0) { n           r = r1; n           break; n         } n       } n       res *= base; n       res += q; n       if (j > 0) { n         int d1 = res.z.size() + 2 < r.z.size() ? r.z[res.z.size() + 2] : 0; n         int d2 = res.z.size() + 1 < r.z.size() ? r.z[res.z.size() + 1] : 0; n         int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()] : 0; n         q = ((long long)d1 * base * base + (long long)d2 * base + d3) / n             (firstDigit * 2); n       } n     } n     res.trim(); n     return res / norm; n   } n   bigint operator/(const bigint &v) const { return divmod(*this, v).first; } n   bigint operator%(const bigint &v) const { return divmod(*this, v).second; } n   void operator/=(int v) { n     if (v < 0) sign = -sign, v = -v; n     for (int i = (int)z.size() - 1, rem = 0; i >= 0; --i) { n       long long cur = z[i] + rem * (long long)base; n       z[i] = (int)(cur / v); n       rem = (int)(cur % v); n     } n     trim(); n   } n   bigint operator/(int v) const { n     bigint res = *this; n     res /= v; n     return res; n   } n   int operator%(int v) const { n     if (v < 0) v = -v; n     int m = 0; n     for (int i = z.size() - 1; i >= 0; --i) n       m = (z[i] + m * (long long)base) % v; n     return m * sign; n   } n   void operator+=(const bigint &v) { *this = *this + v; } n   void operator-=(const bigint &v) { *this = *this - v; } n   void operator*=(const bigint &v) { *this = *this * v; } n   void operator/=(const bigint &v) { *this = *this / v; } n   bool operator<(const bigint &v) const { n     if (sign != v.sign) return sign < v.sign; n     if (z.size() != v.z.size()) return z.size() * sign < v.z.size() * v.sign; n     for (int i = z.size() - 1; i >= 0; i--) n       if (z[i] != v.z[i]) return z[i] * sign < v.z[i] * sign; n     return false; n   } n   bool operator>(const bigint &v) const { return v < *this; } n   bool operator<=(const bigint &v) const { return !(v < *this); } n   bool operator>=(const bigint &v) const { return !(*this < v); } n   bool operator==(const bigint &v) const { n     return !(*this < v) && !(v < *this); n   } n   bool operator!=(const bigint &v) const { return *this < v || v < *this; } n   void trim() { n     while (!z.empty() && z.back() == 0) z.pop_back(); n     if (z.empty()) sign = 1; n   } n   bool isZero() const { return z.empty() || (z.size() == 1 && !z[0]); } n   bigint operator-() const { n     bigint res = *this; n     res.sign = -sign; n     return res; n   } n   bigint abs() const { n     bigint res = *this; n     res.sign *= res.sign; n     return res; n   } n   long long longValue() const { n     long long res = 0; n     for (int i = z.size() - 1; i >= 0; i--) res = res * base + z[i]; n     return res * sign; n   } n   friend bigint gcd(const bigint &a, const bigint &b) { n     return b.isZero() ? a : gcd(b, a % b); n   } n   friend bigint lcm(const bigint &a, const bigint &b) { n     return a / gcd(a, b) * b; n   } n   void read(const std::string &s) { n     sign = 1; n     z.clear(); n     int pos = 0; n     while (pos < (int)s.size() && (s[pos] ==  -  || s[pos] ==  + )) { n       if (s[pos] ==  - ) sign = -sign; n       ++pos; n     } n     for (int i = s.size() - 1; i >= pos; i -= base_digits) { n       int x = 0; n       for (int j = std::max(pos, i - base_digits + 1); j <= i; j++) n         x = x * 10 + s[j] -  0 ; n       z.push_back(x); n     } n     trim(); n   } n   friend std::istream &operator>>(std::istream &stream, bigint &v) { n     std::string s; n     stream >> s; n     v.read(s); n     return stream; n   } n   friend std::ostream &operator<<(std::ostream &stream, const bigint &v) { n     if (v.sign == -1) stream <<  - ; n     stream << (v.z.empty() ? 0 : v.z.back()); n     for (int i = (int)v.z.size() - 2; i >= 0; --i) n       stream << std::setw(base_digits) << std::setfill( 0 ) << v.z[i]; n     return stream; n   } n   static std::vector<int> convert_base(const std::vector<int> &a, n                                        int old_digits, int new_digits) { n     std::vector<long long> p(std::max(old_digits, new_digits) + 1); n     p[0] = 1; n     for (int i = 1; i < (int)p.size(); i++) p[i] = p[i - 1] * 10; n     std::vector<int> res; n     long long cur = 0; n     int cur_digits = 0; n     for (int i = 0; i < (int)a.size(); i++) { n       cur += a[i] * p[cur_digits]; n       cur_digits += old_digits; n       while (cur_digits >= new_digits) { n         res.push_back(int(cur % p[new_digits])); n         cur /= p[new_digits]; n         cur_digits -= new_digits; n       } n     } n     res.push_back((int)cur); n     while (!res.empty() && res.back() == 0) res.pop_back(); n     return res; n   } n   static std::vector<long long> karatsubaMultiply( n       const std::vector<long long> &a, const std::vector<long long> &b) { n     int n = a.size(); n     std::vector<long long> res(n + n); n     if (n <= 32) { n       for (int i = 0; i < n; i++) n         for (int j = 0; j < n; j++) res[i + j] += a[i] * b[j]; n       return res; n     } n     int k = n >> 1; n     std::vector<long long> a1(a.begin(), a.begin() + k); n     std::vector<long long> a2(a.begin() + k, a.end()); n     std::vector<long long> b1(b.begin(), b.begin() + k); n     std::vector<long long> b2(b.begin() + k, b.end()); n     std::vector<long long> a1b1 = karatsubaMultiply(a1, b1); n     std::vector<long long> a2b2 = karatsubaMultiply(a2, b2); n     for (int i = 0; i < k; i++) a2[i] += a1[i]; n     for (int i = 0; i < k; i++) b2[i] += b1[i]; n     std::vector<long long> r = karatsubaMultiply(a2, b2); n     for (int i = 0; i < (int)a1b1.size(); i++) r[i] -= a1b1[i]; n     for (int i = 0; i < (int)a2b2.size(); i++) r[i] -= a2b2[i]; n     for (int i = 0; i < (int)r.size(); i++) res[i + k] += r[i]; n     for (int i = 0; i < (int)a1b1.size(); i++) res[i] += a1b1[i]; n     for (int i = 0; i < (int)a2b2.size(); i++) res[i + n] += a2b2[i]; n     return res; n   } n   bigint operator*(const bigint &v) const { n     std::vector<int> a6 = convert_base(this->z, base_digits, 6); n     std::vector<int> b6 = convert_base(v.z, base_digits, 6); n     std::vector<long long> a(a6.begin(), a6.end()); n     std::vector<long long> b(b6.begin(), b6.end()); n     while (a.size() < b.size()) a.push_back(0); n     while (b.size() < a.size()) b.push_back(0); n     while (a.size() & (a.size() - 1)) a.push_back(0), b.push_back(0); n     std::vector<long long> c = karatsubaMultiply(a, b); n     bigint res; n     res.sign = sign * v.sign; n     for (int i = 0, carry = 0; i < (int)c.size(); i++) { n       long long cur = c[i] + carry; n       res.z.push_back((int)(cur % 1000000)); n       carry = (int)(cur / 1000000); n     } n     res.z = convert_base(res.z, 6, base_digits); n     res.trim(); n     return res; n   } n }; n bigint lastans; n const int maxn = 6e5 + 222; n const int mask = (1 << 30); n using ll = long long; n int q, s[maxn], nxt[maxn], pre[maxn], w[maxn]; n std::map<int, int> cnt; n std::vector<int> stk; n char t[10]; n ll sum = 0; n int query(int x) { return w[*std::lower_bound(stk.begin(), stk.end(), x)]; } n int main() { n   scanf( %d , &q); n   scanf( %s%d , t + 1, &w[1]); n   s[1] = t[1] -  a ; n   lastans = w[1]; n   stk.push_back(1); n   std::cout << lastans <<   n ; n   for (int n = 2, j = 0; n <= q; ++n) { n     scanf( %s%d , t + 1, &w[n]); n     int c = (lastans + t[1] -  a ) % 26; n     s[n] = c; n     w[n] = w[n] ^ (lastans % mask); n     while (j && s[j + 1] != s[n]) j = nxt[j]; n     if (s[j + 1] == s[n]) j++; n     nxt[n] = j; n     if (s[nxt[n - 1] + 1] == s[n]) n       pre[n - 1] = pre[nxt[n - 1]]; n     else n       pre[n - 1] = nxt[n - 1]; n     int x = nxt[n - 1]; n     while (x) { n       if (s[x + 1] == s[n]) n         x = pre[x]; n       else { n         int w = query(n - x); n         cnt[w]--; n         sum -= w; n         x = nxt[x]; n       } n     } n     while (stk.size() && w[stk.back()] >= w[n]) stk.pop_back(); n     stk.push_back(n); n     int cw = 0; n     for (auto it = cnt.upper_bound(w[n]); it != cnt.end();) { n       sum -= (ll)it->second * (it->first - w[n]); n       cw += it->second; n       auto nw = std::next(it); n       cnt.erase(it); n       it = nw; n     } n     cnt[w[n]] += cw; n     if (s[1] == s[n]) { n       cnt[w[n]]++; n       sum += w[n]; n     } n     lastans += sum + w[stk[0]]; n     std::cout << lastans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 16, MX = 16 * 2000 + 42; n int n, k, inp[nmax]; n int outp[nmax]; n void print() { n   vector<pair<int, int> > active; n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) active.push_back({inp[i], outp[i]}); n   for (int t = 1; t < n; t++) { n     for (int i = 0; i < active.size(); i++) n       for (int j = i + 1; j < active.size(); j++) n         if (active[i].second == active[j].second) { n           cout << active[i].first <<     << active[j].first << endl; n           active[i].first += active[j].first; n           while (active[i].first % k == 0) { n             active[i].first = active[i].first / k; n             active[i].second--; n           } n           active.erase(active.begin() + j, active.begin() + j + 1); n           i = n; n           j = n; n         } n   } n   exit(0); n } n bitset<MX> on[1 << nmax]; n int sum[1 << nmax]; n void go(int aim, int deg, int mask) { n   if (aim == 0 && mask == 0) print(); n   if (aim > sum[mask] || aim < 0) return; n   if (on[mask][aim]) return; n   on[mask][aim] = 1; n   go(aim * k, deg + 1, mask); n   for (int i = 0; i < n; i++) n     if ((mask & (1 << i))) { n       outp[i] = deg; n       go(aim - inp[i], deg, mask - (1 << i)); n     } n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> inp[i]; n   for (int mask = 0; mask < (1 << n); mask++) n     for (int i = 0; i < n; i++) n       if ((mask & (1 << i))) sum[mask] += inp[i]; n   go(1, 0, (1 << n) - 1); n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[2000006], pp[2000006]; n long long first[2000006], cur = 0, w[2000006], n, m, i, xx, second, d, h, ans, n                           w1, p, tt, o, temp, t; n void dfs(long long node, long long l) { n   if (node > n) return; n   long long i, j; n   first[node] = l; n   v[node].push_back(l); n   dfs(2 * node, l + w[2 * node]); n   dfs(2 * node + 1, l + w[2 * node + 1]); n   for (j = 0; j < v[2 * node].size(); ++j) v[node].push_back(v[2 * node][j]); n   for (j = 0; j < v[2 * node + 1].size(); ++j) n     v[node].push_back(v[2 * node + 1][j]); n   sort(v[node].begin(), v[node].end()); n   pp[node].resize(v[node].size()); n   pp[node][0] = v[node][0]; n   for (i = 1; i < v[node].size(); ++i) n     pp[node][i] = v[node][i] + pp[node][i - 1]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (i = 2; i <= n; ++i) cin >> w[i]; n   dfs(1, 0); n   for (i = 0; i < m; ++i) { n     ans = 0; n     cin >> d >> h; n     t = upper_bound(v[d].begin(), v[d].end(), h + first[d]) - v[d].begin(); n     if (t) ans = (h + first[d]) * t - pp[d][t - 1]; n     while (d != 1) { n       w1 = w[d]; n       p = d / 2; n       if (h > w1) ans += h - w1; n       o = d - (d % 2) + !(d % 2); n       if (o <= n) { n         temp = h - (w[o] + w1) + first[o]; n         tt = upper_bound(v[o].begin(), v[o].end(), temp) - v[o].begin(); n         if (tt) ans += tt * temp - pp[o][tt - 1]; n       } n       h -= w[d]; n       d >>= 1; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n map<int, long long> ans; n inline long long go(int n) { n   if (ans.find(n) != ans.end()) return ans[n]; n   long long cur = n + 1; n   for (int i = 2; i * i <= n; ++i) { n     if (n % i == 0) { n       cur = max(cur, n + go(i)); n       cur = max(cur, n + go(n / i)); n     } n   } n   return ans[n] = cur; n } n int main() { n   cin >> n; n   ans[1] = 1; n   cout << go(n) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d; n bool ck[100005]; n vector<int> adj[100005]; n int dep[100005], ans; n void dfs(int u, int p) { n   dep[u] = (ck[u] ? 0 : -(1 << 20)); n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (v == p) continue; n     dfs(v, u); n     if (dep[v] != -(1 << 20)) dep[u] = max(dep[u], dep[v] + 1); n   } n } n void get(int u, int p, int bef) { n   if (ck[u]) bef = max(0, bef); n   ans += (bef <= d && dep[u] <= d); n   int m1 = -1, m2 = -1, c1 = 0; n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (v == p) continue; n     int dd = dep[v] + 1; n     if (dd > m1) n       m2 = m1, m1 = dd; n     else if (dd > m2) n       m2 = dd; n   } n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (v == p) continue; n     int dd = dep[v] + 1; n     if (dd == m1) c1++; n   } n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (v == p) continue; n     int dd = dep[v] + 1; n     if (dd == m1) { n       if (c1 > 1) n         get(v, u, max(bef + 1, m1 + 1)); n       else if (m2 != -1) n         get(v, u, max(bef + 1, m2 + 1)); n       else n         get(v, u, bef + 1); n     } else n       get(v, u, max(bef + 1, m1 + 1)); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &d); n   for (int i = 0; i < m; ++i) { n     int u; n     scanf( %d , &u), u--; n     ck[u] = 1; n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d%d , &u, &v), u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(0, -1), get(0, -1, ck[0] ? 0 : -(1 << 20)); n   printf( %d , ans); n } n


#include<bits/stdc++.h> n #define ll long long n using namespace std; n  n int x[100005]; n int idx=0; n int d[100005]; n int main() n { n     int t; n     cin>>t; n     while(t--){ n         int n; n         cin>>n; n         idx=0; n         for(int i=1;i<=n;i++){ n             scanf( %d ,&x[i]); n         } n         for(int i=n;i>=1;i--){ n             if(idx==0){ n                 d[++idx]=i; n             } n             else{ n                 while(idx>0&&x[i]>x[d[idx]]){ n                     idx--; n                 } n                 d[++idx]=i; n             } n         } n         for(int i=1;i<=idx;i++){ n             int ma; n             if(i==1) ma=n+1; n             else ma=d[i-1]; n             for(int j=d[i];j<ma;j++){ n                 cout<<x[j]<<   ; n             } n         } n         cout<<endl; n     } n     return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 1; n   scanf( %d , &n); n   for (int i = 2; i <= n / 2; i++) { n     if (n % i == 0) 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 ostream &operator<<(ostream &os, const vector<T> &v) { n   for (T i : v) os << i <<  ,  ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const set<T> &v) { n   for (T i : v) os << i <<    ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const multiset<T> &v) { n   for (T i : v) os << i <<    ; n   return os; n } n template <class T, class second> n ostream &operator<<(ostream &os, const pair<T, second> &v) { n   os << v.first <<     << v.second; n   return os; n } n template <class T, class second> n ostream &operator<<(ostream &os, const map<T, second> &v) { n   for (auto i : v) os <<  (  << i.first <<   =>   << i.second <<  )  <<    ; n   return os; n } n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void f1(string &s) { n   int n = s.length() + 1; n   vector<int> ans(n); n   int pos = 0, c = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  > ) pos = i + 1; n   } n   ans[pos] = ++c; n   if (pos > 1) { n     for (int i = pos - 2; i >= 0; i--) { n       if (s[i] ==  > ) { n         ans[i + 1] = ++c; n         if (i == 0) ans[i] = ++c; n       } n     } n   } n   set<int> to_do; n   while (c < n) to_do.insert(++c); n   for (int i = 1; i < n; i++) { n     if (s[i - 1] ==  >  || ans[i] != 0) continue; n     int c = i; n     while (c < n && s[c - 1] ==  < ) c++; n     c--; n     while (c >= i) { n       ans[c] = *to_do.rbegin(); n       to_do.erase(--to_do.end()); n       c--; n     } n   } n   for (int i = 0; i < n; i++) n     if (ans[i] == 0) { n       ans[i] = *to_do.rbegin(); n       to_do.erase(--to_do.end()); n     } n   for (int i : ans) cout << i <<    ; n   cout <<   n ; n } n void f2(string &s) { n   int n = s.length() + 1; n   vector<int> ans(n); n   reverse(s.begin(), s.end()); n   int pos = 0, c = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  < ) pos = i + 1; n   } n   ans[pos] = ++c; n   if (pos > 1) { n     for (int i = pos - 2; i >= 0; i--) { n       if (s[i] ==  < ) { n         ans[i + 1] = ++c; n       } n     } n   } n   c = n; n   for (int i = n - 1; i >= 0; i--) n     if (ans[i] == 0) ans[i] = c--; n   reverse(ans.begin(), ans.end()); n   for (int i : ans) cout << i <<    ; n   cout <<   n ; n } n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   f1(s); n   f2(s); n } n int main() { n   fast(); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n const int maxm = 100 + 10; n const int inf = 0x3f; n long long f[maxm][maxn], D[maxn], H[maxn], T[maxn], q[maxn]; n long long g[maxn]; n int N, M, P; n long long A[maxn], S[maxn]; n void init() { n   sort(A + 1, A + 1 + M); n   for (int i = (1); i <= (M); i++) { n     S[i] = S[i - 1] + A[i]; n   } n   memset(f, inf, sizeof(f)); n   memset(g, 0, sizeof(g)); n   memset(q, 0, sizeof(q)); n   f[0][0] = 0; n } n void solve() { n   for (int i = (1); i <= (P); i++) { n     for (int k = (1); k <= (M); k++) g[k] = f[i - 1][k] + S[k]; n     int l = 1, r = 1; n     q[1] = 0; n     for (int j = (1); j <= (M); j++) { n       while (l < r && (g[q[l + 1]] - g[q[l]]) <= (A[j] * (q[l + 1] - q[l]))) n         l++; n       f[i][j] = min(f[i - 1][j], g[q[l]] + A[j] * (j - q[l]) - S[j]); n       while (l < r && ((g[q[r]] - g[q[r - 1]]) * (j - q[r]) >= n                        (g[j] - g[q[r]]) * (q[r] - q[r - 1]))) n         r--; n       q[++r] = j; n     } n   } n   cout << f[P][M] << endl; n } n int main() { n   cin >> N >> M >> P; n   for (int i = (2); i <= (N); i++) { n     int x; n     scanf( %d , &x); n     D[i] = D[i - 1] + x; n   } n   for (int i = (1); i <= (M); i++) { n     int h, t; n     scanf( %d%d , &h, &t); n     A[i] = t - D[h]; n   } n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5, M = 1e9 + 7; n const unsigned long long base = 13331; n const double Pi = acos(-1.0); n const long long C = 299792458; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n char a[505][505]; n int n, m; n bool check() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i][j] ==  * ) { n         if (i - 1 < 1 || i + 1 > n || j - 1 < 1 || j + 1 > m) continue; n         if (a[i - 1][j] ==  *  && a[i + 1][j] ==  *  && a[i][j - 1] ==  *  && n             a[i][j + 1] ==  * ) { n           a[i][j] =  . ; n           for (int k = i - 1; k >= 1; k--) { n             if (a[k][j] ==  * ) n               a[k][j] =  . ; n             else n               break; n           } n           for (int k = i + 1; k <= n; k++) { n             if (a[k][j] ==  * ) n               a[k][j] =  . ; n             else n               break; n           } n           for (int k = j - 1; k >= 1; k--) { n             if (a[i][k] ==  * ) n               a[i][k] =  . ; n             else n               break; n           } n           for (int k = j + 1; k <= m; k++) { n             if (a[i][k] ==  * ) n               a[i][k] =  . ; n             else n               break; n           } n           return true; n         } n       } n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   if (!check()) { n     cout <<  NO  << endl; n   } else { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (a[i][j] ==  * ) { n           cout <<  NO  << endl; n           return 0; n         } n       } n     } n     cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 25; n inline int getDis(pair<int, int> a, pair<int, int> b) { n   return (b.first - a.first) * (b.first - a.first) + n          (b.second - a.second) * (b.second - a.second); n } n pair<int, int> G[maxn]; n int dp[1 << maxn]; n int dis[maxn][maxn], route[1 << maxn]; n int n; n int main() { n   scanf( %d%d , &G[0].first, &G[0].second); n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) n     scanf( %d%d , &G[i].first, &G[i].second); n   for (register int i = 0; i <= n; i++) n     for (register int j = i; j <= n; j++) n       dis[i][j] = dis[j][i] = getDis(G[i], G[j]); n   memset(route, -1, sizeof route); n   for (register int i = 0; i < (1 << maxn); i++) dp[i] = 0x3f3f3f3f; n   dp[0] = 0; n   for (register int i = 0; i < (1ll << n); i++) { n     if (dp[i] == 0x3f3f3f3f) continue; n     for (register int j = 1; j <= n; j++) { n       if (i & (1ll << (j - 1))) continue; n       for (register int k = 1; k <= n; k++) { n         if (i & (1ll << (k - 1))) continue; n         if (dp[i] + dis[0][j] + dis[j][k] + dis[k][0] < n             dp[i | (1ll << (j - 1)) | (1ll << (k - 1))]) { n           route[i | (1ll << (j - 1) | (1ll << (k - 1)))] = i; n           dp[i | (1ll << (j - 1)) | (1ll << (k - 1))] = n               min(dp[i | (1ll << (j - 1)) | (1ll << (k - 1))], n                   dp[i] + dis[0][j] + dis[j][k] + dis[k][0]); n         } n       } n       break; n     } n   } n   printf( %d n , dp[(1 << n) - 1]); n   int now = (1 << n) - 1, last = -1; n   printf( 0  ); n   while (true) { n     if (last == -1) { n       last = now; n       now = route[now]; n       continue; n     } else { n       int change = now ^ last; n       for (register int i = 1; i <= n; i++) { n         if (change & (1ll << (i - 1))) { n           printf( %d  , i); n         } n       } n       last = now; n       now = route[now]; n       if (now == -1) { n         for (register int i = 1; i <= n; i++) { n           if (last & (1 << (i - 1))) { n             printf( %d  , i); n           } n         } n         break; n       } n     } n     printf( 0  ); n   } n   printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4786) n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int maxn = 2e5 + 5; n int isu[maxn], sz[maxn], dis[maxn], kk; n long long ans; n vector<int> G[maxn]; n void dfs1(int root, int fa, long long depth) { n   int siz = G[root].size(), nt; n   dis[root] = depth; n   if (isu[root]) { n     sz[root] = 1; n     ans += depth; n   } n   for (int i = 0; i < siz; i++) { n     nt = G[root][i]; n     if (nt == fa) continue; n     dfs1(nt, root, depth + 1LL); n     sz[root] += sz[nt]; n   } n } n void solve(int root, int fa) { n   if (kk <= 0) return; n   priority_queue<pair<int, int> > Q; n   int siz = G[root].size(), nt; n   for (int i = 0; i < siz; i++) { n     nt = G[root][i]; n     if (nt == fa) continue; n     if (sz[nt] > 0) { n       Q.push(make_pair(sz[nt], nt)); n     } n   } n   if (isu[root]) { n     Q.push(make_pair(1, root)); n   } n   pair<int, int> f1 = Q.top(), f2; n   int fir = f1.first, sec = f1.second, sum = 0; n   if (Q.size() == 1) { n     solve(sec, root); n   } else { n     Q.pop(); n     while (!Q.empty()) { n       f2 = Q.top(); n       Q.pop(); n       sum += f2.first; n     } n     sum = min(sum, kk); n     kk -= sum; n     ans -= 2LL * dis[root] * sum; n     if (kk > 0) solve(sec, root); n   } n } n int main() { n   int n, k, x, y; n   scanf( %d %d , &n, &k); n   kk = k; n   for (int i = 1; i <= 2 * k; i++) { n     scanf( %d , &x); n     isu[x] = 1; n   } n   for (int 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   ans = 0; n   dfs1(1, 0, 0); n   solve(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, i; n   cin >> n; n   long long int arr1[n]; n   for (i = 0; i < n; i++) cin >> arr1[i]; n   cin >> m; n   long long int arr2[m]; n   for (i = 0; i < m; i++) cin >> arr2[i]; n   sort(arr1, arr1 + n); n   sort(arr2, arr2 + m); n   cout << arr1[n - 1] <<     << arr2[m - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int sg[100005], sgp[100005]; n int main() { n   int n; n   cin >> n; n   if (n <= 2) { n     cout <<  -1 ; n     return 0; n   } n   sg[0] = sg[1] = sg[2] = 0; n   for (int i = 3; i <= n; ++i) { n     int cnt[105] = {0}; n     for (int d = 2; d <= i; ++d) { n       int fr, ls; n       if (d % 2 == 0) { n         fr = i / d - d / 2 + 1, ls = i / d + d / 2; n         if (ls >= i || fr <= 0) break; n         if ((fr + ls) * d / 2 != i) continue; n       } n       if (d % 2 == 1) { n         fr = i / d - (d - 1) / 2, ls = i / d + (d - 1) / 2; n         if (ls >= i || fr <= 0) break; n         if (i % d != 0) continue; n       } n       cnt[sgp[fr - 1] ^ sgp[ls]] = 1; n       if (i == n && (sgp[fr - 1] ^ sgp[ls]) == 0) { n         cout << d; n         return 0; n       } n     } n     for (int j = 0; j < 100000; ++j) n       if (cnt[j] == 0) { n         sg[i] = j; n         sgp[i] = sgp[i - 1] ^ sg[i]; n         break; n       } n   } n   cout <<  -1 ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, n, a, b, c, ret = 0, t; n   scanf( %d %d %d %d , &n, &a, &b, &c); n   n *= 10; n   for (i = 0; i <= a; i++) n     for (j = 0; j <= b; j++) { n       t = (i * 5); n       t += (j * 10); n       if (n - t < 0) continue; n       if ((n - t) % 20 == 0 && (n - t) / 20 <= c) ret++; n     } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long parse(string s) { n   long long k = 1, ret = 0; n   for (int i = 0; i < s.size(); i++) n     ret += k * (long long)(s[s.size() - i - 1] -  0 ), k *= 4; n   return ret; n } n string change(string s) { n   string ret = s; n   for (int i = 0; i < ret.size(); i++) { n     if (ret[i] ==  1 ) n       ret[i] =  2 ; n     else if (ret[i] ==  2 ) n       ret[i] =  3 ; n     else if (ret[i] ==  3 ) n       ret[i] =  1 ; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long idx = (n + 2) / 3, m = n % 3; n     long long k = 0, sum = 0; n     while (sum < idx) sum += (1ll << (2 * k)), k++; n     k--; n     long long buf = (1ll << (2 * k)), c = sum - buf + 1; n     string ans =  1 ; n     for (int i = k - 1; i >= 0; i--) { n       long long div = (1ll << (2 * i)); n       long long d = (idx - c) / div; n       ans += (char)( 0  + d); n       c += div * d; n     } n     long long f = parse(ans); n     long long s = parse(change(ans)); n     if (m == 1) n       cout << f <<   n ; n     else if (m == 2) n       cout << s <<   n ; n     else n       cout << (f ^ s) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline void sub(long long &a, long long b) { n   a -= b; n   if (a < 0) a += 1000000007; n } n inline void add(long long &a, long long b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n } n template <typename T> n inline T const &MAX(T const &a, T const &b) { n   return a > b ? a : b; n } n template <typename T> n inline T const &MIN(T const &a, T const &b) { n   return a < b ? a : b; n } n inline long long qp(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % 1000000007; n     a = a * a % 1000000007, b >>= 1; n   } n   return ans; n } n inline long long qp(long long a, long long b, long long c) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % c; n     a = a * a % c, b >>= 1; n   } n   return ans; n } n using namespace std; n const unsigned long long ba = 233; n const double eps = 1e-6; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int N = 100000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f; n long long h[N], a[N]; n int cnt, pre[N]; n struct sgt { n   pair<int, int> ma[N << 2]; n   void update(int pos, pair<int, int> v, int l, int r, int rt) { n     if (l == r) { n       ma[rt] = v; n       return; n     } n     int m = (l + r) >> 1; n     if (pos <= m) n       update(pos, v, l, m, rt << 1); n     else n       update(pos, v, m + 1, r, rt << 1 | 1); n     ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]); n   } n   pair<int, int> query(int L, int R, int l, int r, int rt) { n     if (L <= l && r <= R) return ma[rt]; n     int m = (l + r) >> 1; n     pair<int, int> ans = make_pair(0, 0); n     if (L <= m) ans = max(ans, query(L, R, l, m, rt << 1)); n     if (m < R) ans = max(ans, query(L, R, m + 1, r, rt << 1 | 1)); n     return ans; n   } n } sg; n int main() { n   int n, d; n   scanf( %d%d , &n, &d); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &h[i]); n     a[cnt++] = h[i]; n   } n   sort(a, a + cnt); n   cnt = unique(a, a + cnt) - a; n   for (int i = 1; i <= n; i++) { n     pair<int, int> x = make_pair(0, 0), y; n     int p = upper_bound(a, a + cnt, h[i] - d) - a; n     p--; n     if (p >= 0) x = max(x, sg.query(1, p + 1, 1, cnt, 1)); n     p = lower_bound(a, a + cnt, h[i] + d) - a; n     if (p < cnt) x = max(x, sg.query(p + 1, cnt, 1, cnt, 1)); n     p = lower_bound(a, a + cnt, h[i]) - a; n     y = sg.query(p + 1, p + 1, 1, cnt, 1); n     if (x.first + 1 > y.first) { n       sg.update(p + 1, make_pair(x.first + 1, i), 1, cnt, 1); n       pre[i] = x.second; n     } n   } n   pair<int, int> x = sg.query(1, cnt, 1, cnt, 1); n   printf( %d n , x.first); n   stack<int> st; n   for (int now = x.second; now != 0; now = pre[now]) st.push(now); n   while (!st.empty()) printf( %d  , st.top()), st.pop(); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 10; n const int MOD = 1e9 + 7; n const int INF = 2e9; n const long long INFLL = 1e18; n const double PI = acos((double)-1); n inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; } n inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; } n inline int fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   int n = s.length(); n   int x = 0, y = 0; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  - ) { n       y++; n       if (x > 0) n         x--; n       else n         ans++; n     } else { n       x++; n       if (y > 0) n         y--; n       else n         ans++; n     } n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n int main() { n   ios::sync_with_stdio(false); n   long long k, d, t; n   cin >> k >> d >> t; n   t *= 2; n   long long p = (k + d - 1) / d * d; n   long double res = t / (p + k) * p; n   t %= (p + k); n   if (t > 2 * k) { n     res += k; n     t -= 2 * k; n     res += t; n   } else n     res += (long double)t / 2; n   cout << setprecision(10) << fixed << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double arr[550]; n int m; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   double ans = 0; n   for (int i = 1; i <= m; i++) { n     int u, v; n     double w; n     cin >> u >> v; n     cin >> w; n     double rr = (arr[u] + arr[v]) / w; n     if (rr > ans) ans = rr; n   } n   printf( %.9lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const long double PI = acos(-1.0); n long long diff[200010]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   ; n   int T, j, t, i, buckN = 1; n   int N = 1; n   scanf( %d , &T); n   long long sum = 0, a, x, lE = 0; n   while (T--) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %lld%lld , &a, &x); n       sum += a * x; n       diff[a] -= x; n       if (a == N) { n         diff[a] += x; n         lE += x; n       } n     } else if (t == 2) { n       scanf( %lld , &a); n       diff[N] = a - lE; n       lE = a; n       N++; n       sum += a; n     } else { n       sum -= lE; n       --N; n       lE -= diff[N]; n       diff[N] = 0; n     } n     printf( %.10f n , (1.0 * sum) / N); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string arr[3]; n   for (int i = 0; i < 3; i++) { n     cin >> arr[i]; n     for (int j = 0; j < arr[i].size(); j++) n       if (arr[i][j] ==  ;  || arr[i][j] ==  -  || arr[i][j] ==  _ ) { n         arr[i].erase(j, 1); n         j--; n       } n     for (int j = 0; j < arr[i].size(); j++) n       if (arr[i][j] >=  A  && arr[i][j] <=  Z ) arr[i][j] += 32; n   } n   string s1 = arr[0] + arr[1] + arr[2], s2 = arr[0] + arr[2] + arr[1], n          s3 = arr[1] + arr[0] + arr[2], s4 = arr[1] + arr[2] + arr[0], n          s5 = arr[2] + arr[0] + arr[1], s6 = arr[2] + arr[1] + arr[0]; n   string s; n   int n; n   cin >> n; n   while (n--) { n     cin >> s; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  ;  || s[i] ==  -  || s[i] ==  _ ) { n         s.erase(i, 1); n         i--; n       } n       if (s[i] >=  A  && s[i] <=  Z ) s[i] += 32; n     } n     if (s == s1 || s == s2 || s == s3 || s == s4 || s == s5 || s == s6) n       cout <<  ACC  << endl; n     else n       cout <<  WA  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fir[201], tot, in[201], In[201]; n int nxt[400201], to[400201]; n inline void add_edge(int u, int v) { n   to[++tot] = v; n   nxt[tot] = fir[u]; n   fir[u] = tot; n } n int n, c[201], cc[4][201]; n queue<int> q[5]; n inline int solve(int x) { n   int ans = 0; n   for (register int i = 1; i <= 3; i++) { n     for (register int j = 1; j <= cc[i][0]; j++) { n       if (!in[cc[i][j]]) q[i].push(cc[i][j]); n     } n   } n   int now = x - 1; n   while (1) { n     now++; n     if (now == 4) now = 1; n     while (!q[now].empty()) { n       ans++; n       int u = q[now].front(); n       q[now].pop(); n       for (register int i = fir[u]; i; i = nxt[i]) { n         int v = to[i]; n         in[v]--; n         if (!in[v]) { n           q[c[v]].push(v); n         } n       } n     } n     if (q[1].empty() && q[2].empty() && q[3].empty()) return ans; n     ans++; n   } n } n int main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) { n     scanf( %d , c + i); n     cc[c[i]][++cc[c[i]][0]] = i; n   } n   for (register int i = 1; i <= n; i++) { n     int sze = 0, tmp = 0; n     scanf( %d , &sze); n     for (register int j = 1; j <= sze; j++) { n       scanf( %d , &tmp); n       add_edge(tmp, i); n       In[i]++; n       in[i]++; n     } n   } n   int ans = 0x7fffffff; n   ans = min(ans, solve(1)); n   for (register int i = 1; i <= n; i++) in[i] = In[i]; n   ans = min(ans, solve(2)); n   for (register int i = 1; i <= n; i++) in[i] = In[i]; n   ans = min(ans, solve(3)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const int inf = 0x3f3f3f3f; n int main() { n   long long n, m, k, p[maxn], ans = 0, sub = 0; n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     scanf( %lld , &p[i]); n     p[i]--; n   } n   for (int i = 1; i <= m; i++) { n     long long now = (p[i] - sub) / k; n     for (int j = 1;; j++) { n       long long a = (p[i + j] - sub) / k; n       if (i + j > m || a != now) { n         sub += j; n         i += j - 1; n         ans++; n         break; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1 * 1000 + 10; n int ans[MAXN][MAXN]; n int main() { n   ios::sync_with_stdio(false); n   int n, d, k; n   cin >> n; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(a.begin(), a.end(), [&](int a, int b) { return a <= b; }); n   int ct = 0; n   for (int i = 0; i < n; i++) ct = (a[i] == b[i]) ? ct : ct + 1; n   if (ct == 0 || ct == 2) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2010][2010], a[2010]; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int n, i, j, ans, mn, cnt; n   scanf( %d , &n); n   cnt = 0; n   for (i = 0; i < n; i++) { n     scanf( %d , a + i); n     if (a[i] == 1) cnt++; n   } n   if (cnt) { n     ans = n - cnt; n   } else { n     mn = 0x3fffffff; n     for (i = 0; i < n; i++) { n       for (j = i; j < n; j++) { n         dp[i][j] = (i == j ? a[i] : gcd(dp[i][j - 1], a[j])); n         if (dp[i][j] == 1) mn = min(mn, j - i); n       } n     } n     if (mn == 0x3fffffff) n       ans = -1; n     else n       ans = mn + n - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1000005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   sort(a + 1, a + 1 + n); n   int x = 1; n   int y = n; n   long long ans = 0; n   while (x < y) { n     ans += abs(a[y] - a[x]) * 2; n     x += m; n     y -= m; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1e9 + 7; n const int mod = 998244353; n namespace __________ { n namespace _in { n template <class T> n inline T read() { n   char ch; n   bool flag = 0; n   T x = 0; n   while (ch = getchar(), !isdigit(ch)) n     if (ch ==  - ) flag = 1; n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n   return flag ? -x : x; n } n struct { n   inline operator long long() { return read<long long>(); } n   inline operator int() { return read<int>(); } n   inline operator bool() { return read<bool>(); } n   template <class T> n   inline void operator()(T &x) { n     x = *this; n   } n   template <class T, class... A> n   inline void operator()(T &x, A &...a) { n     x = *this, this->operator()(a...); n   } n } IN; n }  // namespace _in n namespace _std { n template <class T> n inline T abs(T x) { n   if (x < 0) x = -x; n   return x; n } n template <class T> n inline T max(T x, T y) { n   if (x < y) x = y; n   return x; n } n template <class T> n inline T min(T x, T y) { n   if (x > y) x = y; n   return x; n } n template <class T> n inline void swap(T &x, T &y) { n   x ^= y ^= x ^= y; n } n template <class T> n inline void chkmax(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n inline void chkmin(T &x, T y) { n   if (x > y) x = y; n } n }  // namespace _std n namespace _mod { n inline int mul(int x, int y) { return 1ll * x * y % mod; } n inline int dec(int x, int y) { n   x -= y; n   if (x < 0) x += mod; n   return x; n } n inline int add(int x, int y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n inline void pls(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n inline void sub(int &x, int y) { n   x -= y; n   if (x < 0) x += mod; n } n inline int modpow(int x, int y, int res = 1) { n   if (y == -1) y = mod - 2; n   for (; y; y >>= 1, x = mul(x, x)) n     if (y & 1) res = mul(res, x); n   return res; n } n }  // namespace _mod n using namespace _in; n using namespace _std; n using namespace _mod; n }  // namespace __________ n using namespace __________; n const int N = 2e3 + 5; n int n, k, L[N], R[N], a[N]; n long long dp[N], sum; n inline void calc(int &tot, int p, int &tim) { n   int _, a = ::a[p], L = ::L[p]; n   if (a <= tot) n     tot -= a, tim = L; n   else n     _ = a - tot, tot = (k - (_ % k)) % k, tim = L + (_ / k - (_ % k == 0)) + 1; n } n inline void solve() { n   IN(n, k); n   for (int i = 1; i <= n; ++i) IN(L[i], R[i], a[i]), sum += a[i]; n   dp[n + 1] = 0; n   for (int i = n; i >= 1; --i) { n     dp[i] = 1e18; n     int tot = k, tim; n     for (int j = i; j <= n; ++j) { n       calc(tot, j, tim); n       if (tim > R[j]) break; n       if (j == n) dp[i] = 0; n       if (L[j + 1] > tim) chkmin(dp[i], dp[j + 1] + tot); n     } n   } n   printf( %lld n , dp[1] > 1e14 ? -1 : dp[1] + sum); n } n int main() { n   int T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long T, N, M; n   cin >> T; n   while (T--) { n     cin >> N >> M; n     N /= M; n     long long Sum = 0, Ans = 0; n     long long digits[20]; n     for (int i = 1; i <= 10; i++) Sum += (digits[i] = (i * M % 10)); n     for (int i = 1; i <= N % 10; i++) Ans += digits[i]; n     Ans += N / 10 * Sum; n     cout << Ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int NINF = -INF - 1; n const int N = 2e5 + 5; n using namespace std; n char a[N]; n int c[N]; n int b[26]; n int main() { n   int n, ans = 0, maxn = -1000000; n   scanf( %d , &n); n   scanf( %s , a); n   for (int i = 0; i < n; i++) { n     ans = 1; n     for (int j = a[i] -  a  + 1; j < 26; j++) { n       ans = max(ans, b[j] + 1); n     } n     b[a[i] -  a ] = ans; n     c[i] = ans; n     maxn = max(maxn, c[i]); n   } n   cout << maxn << endl; n   for (int i = 0; i < n; i++) printf( %d  , c[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, finall = 0, t1 = 0; n   vector<string> v; n   cin >> n; n   int cop = n; n   while (n--) { n     string s; n     cin >> s; n     v.push_back(s); n   } n   for (int j = 0; j < 7; j++) { n     t1 = 0; n     for (int k = 0; k < cop; k++) t1 += v[k][j] -  0 ; n     if (t1 > finall) finall = t1; n   } n   cout << finall; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f, N = 2e5 + 5; n inline int read() { n   int sum = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) sum = sum * 10 + c - 48, c = getchar(); n   return sum * f; n } n int n, d, x[N], minn = INF; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> d; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = 1; i <= 100; i++) { n     int j = i + d, cnt = 0; n     for (int k = 1; k <= n; k++) { n       if (x[k] < i || x[k] > j) cnt++; n     } n     minn = min(minn, cnt); n     ; n   } n   cout << minn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n void yes() { n   cout <<  Yes  n        <<   n ; n } n void no() { n   cout <<  No  n        <<   n ; n } n struct Sync_stdio { n   Sync_stdio() { n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n   } n } _sync_stdio; n struct FAIL { n   FAIL() { n     cout <<  CHANGE!!!  n          <<   n ; n   } n }; n class BipartiteMatching { n  public: n   struct Edge { n     int first_part_vertex; n     int second_part_vertex; n   }; n   struct Vertex { n     int vertex_id; n     int part; n   }; n   BipartiteMatching(int size_first_, int size_second_) n       : size_first(size_first_), size_second(size_second_), graph(size_first) {} n   void add_edge(int first_part_vertex, int second_part_vertex) { n     graph[first_part_vertex].push_back(second_part_vertex); n   } n   vector<Edge> find_matching() { n     if (!calculated_matching) { n       calculate_matching(); n     } n     vector<Edge> answer; n     for (int i = 0; i < size_second; ++i) { n       if (mt[i] != -1) { n         answer.push_back({mt[i], i}); n       } n     } n     return answer; n   } n   vector<Vertex> find_minimum_vertex_cover() { n     if (!calculated_matching) { n       calculate_matching(); n     } n     cerr <<  Doesn t work  n          <<   n ; n     return {}; n   } n  n  private: n   bool try_kuhn(int vertex) { n     if (used[vertex]) { n       return false; n     } n     used[vertex] = true; n     for (int to : graph[vertex]) { n       if (mt[to] == -1 || try_kuhn(mt[to])) { n         mt[to] = vertex; n         return true; n       } n     } n     return false; n   } n   void calculate_matching() { n     mt.assign(size_second, -1); n     for (int i = 0; i < size_first; ++i) { n       used.assign(size_first, false); n       try_kuhn(i); n     } n     calculated_matching = true; n   } n   int size_first, size_second; n   int calculated_matching = false; n   vector<vector<int>> graph; n   vector<int> mt; n   vector<char> used; n }; n bool can(int n, int m) { n   BipartiteMatching matching(n * m, n * m); n   for (int i1 = (0); i1 < (n); ++i1) { n     for (int j1 = (0); j1 < (m); ++j1) { n       for (int i2 = (0); i2 < (n); ++i2) { n         for (int j2 = (0); j2 < (m); ++j2) { n           if ((i1 + j1) % 2 && abs(i1 - i2) + abs(j1 - j2) == 3) { n             matching.add_edge(i1 * m + j1, i2 * m + j2); n           } n         } n       } n     } n   } n   return matching.find_matching().size() == n * m / 2; n } n void build_table(int n, int m) { n   vector<vector<int>> ans(n, vector<int>(m)); n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (m); ++j) { n       ans[i][j] = can(i + 1, j + 1); n     } n   } n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (m); ++j) { n       cout << ans[i][j]; n     } n     cout <<   n ; n   } n } n int main() { n   long long n, m; n   cin >> n >> m; n   if (n > m) { n     swap(n, m); n   } n   if (n >= 3) { n     cout << n * m / 2 * 2; n     return 0; n   } n   if (n == 2) { n     if (m == 7) { n       cout << 12; n     } else if (m >= 4) { n       cout << n * m / 2 * 2; n     } else if (m == 3) { n       cout << 4; n     } else { n       cout << 0; n     } n     return 0; n   } n   cout << m / 6 * 6 + max((m % 6 - 3) * 2, 0LL); n } n


#include <bits/stdc++.h> n int main() { n   char abc[105]; n   scanf( %s , abc); n   int n = strlen(abc); n   int i = 0; n   int max = 1, count = 0; n   while (i < n) { n     if (abc[i] ==  A  || abc[i] ==  E  || abc[i] ==  I  || abc[i] ==  O  || n         abc[i] ==  U  || abc[i] ==  Y ) { n       if (max < count + 1) max = count + 1; n       count = 0; n     } else n       count++; n     i++; n   } n   if (max < count + 1) max = count + 1; n   printf( %d , max); n } n


#include <bits/stdc++.h> n using namespace std; n int find(int a[], int n) { n   int max = 1; n   int m = a[1]; n   for (int i = 2; i < n; i++) { n     if (m <= a[i]) { n       max = i; n       m = a[i]; n     } n   } n   return max; n } n int main() { n   int c = 0; n   int n; n   cin >> n; n   int a[n]; n   cin >> a[0]; n   for (int i = 1; i < n; i++) { n     int temp; n     cin >> a[i]; n   } n   int m = 0; n   do { n     m = find(a, n); n     if (a[m] >= a[0]) { n       a[m]--; n       a[0]++; n       c++; n     } else n       break; n   } while (true); n   cout << c <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   while (cin >> n >> m) { n     int i; n     for (i = 1; i <= n; i++) { n       int k = m - 2, t = m; n       if (i % 2) n         while (t--) cout <<  # ; n       else { n         cout << (i % 4 ?  .  :  # ); n         while (k--) cout <<  . ; n         cout << (i % 4 ?  #  :  . ); n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400005; n vector<int> adj[N]; n vector<int> st; n int cTime = 1; n int in[N]; n int low[N]; n vector<vector<int>> comps; n map<array<int, 2>, int> edgeToI; n array<int, 2> res[N]; n int edgeToIndex(int a, int b) { return edgeToI[{min(a, b), max(a, b)}]; } n void addRes(int a, int b) { res[edgeToIndex(a, b)] = {a, b}; } n void dfs(int node, int par) { n   in[node] = cTime++; n   low[node] = in[node]; n   st.push_back(node); n   for (int next : adj[node]) { n     if (next == par) continue; n     if (in[next]) { n       low[node] = min(low[next], low[node]); n     } else { n       dfs(next, node); n       low[node] = min(low[node], low[next]); n     } n   } n   if (in[node] == low[node]) { n     vector<int> cur; n     while (true) { n       int last = st.back(); n       st.pop_back(); n       cur.push_back(last); n       if (last == node) break; n     } n     comps.push_back(cur); n   } n } n bool onCircle[N]; n bool vis[N]; n void getEdges(int node) { n   vis[node] = true; n   for (int next : adj[node]) { n     if (res[edgeToIndex(node, next)][0] != 0) continue; n     addRes(next, node); n     if (!vis[next]) getEdges(next); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n     edgeToI[{min(a, b), max(a, b)}] = i; n   } n   dfs(1, -1); n   vector<int> best; n   for (auto comp : comps) { n     if (comp.size() > best.size()) best = comp; n   } n   for (int i : best) onCircle[i] = true; n   vector<array<int, 2>> edges; n   cout << best.size() <<   n ; n   if (best.size() != 1) { n   } n   getEdges(best[0]); n   for (int i = 0; i < m; i++) { n     cout << res[i][0] <<     << res[i][1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int hed[600010], nxt[600010], to[600010], cnt; n inline void addedge(int u, int v) { n   nxt[++cnt] = hed[u]; n   hed[u] = cnt; n   to[cnt] = v; n } n int siz[300010], root[300010], ans[300010]; n int mx[300010]; n int id[6000010], son[6000010][2], tot; n int n, q; n int merge(int x, int y) { n   if (!x) return y; n   if (!y) return x; n   if (mx[id[y]] < mx[id[x]]) id[x] = id[y]; n   son[x][0] = merge(son[x][0], son[y][0]); n   son[x][1] = merge(son[x][1], son[y][1]); n   return x; n } n void update(int &x, int l, int r, int p, int v, int i) { n   if (!x) x = ++tot; n   if (v < mx[id[x]]) id[x] = i; n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (p <= mid) n     update(son[x][0], l, mid, p, v, i); n   else n     update(son[x][1], mid + 1, r, p, v, i); n } n int query(int a, int b, int k, int l, int r) { n   if (a > b || l > r || !k) return 0; n   if (a == l && b == r) return id[k]; n   int mid = (l + r) >> 1; n   if (b <= mid) n     return query(a, b, son[k][0], l, mid); n   else if (a > mid) n     return query(a, b, son[k][1], mid + 1, r); n   else { n     int L = query(a, mid, son[k][0], l, mid), n         R = query(mid + 1, b, son[k][1], mid + 1, r); n     if (!L) return R; n     if (!R) return L; n     if (mx[L] < mx[R]) n       return L; n     else n       return R; n   } n } n void dfs(int x) { n   siz[x] = 1; n   for (int i = hed[x]; i; i = nxt[i]) { n     int y = to[i]; n     dfs(y); n     siz[x] += siz[y]; n     root[x] = merge(root[x], root[y]); n     mx[x] = max(mx[x], siz[y]); n   } n   update(root[x], 1, n, siz[x], mx[x], x); n   ans[x] = query((siz[x] + 1) / 2, siz[x], root[x], 1, n); n } n int main() { n   scanf( %d%d , &n, &q); n   mx[0] = 1e9; n   for (int i = 2; i <= n; i++) { n     int p; n     scanf( %d , &p); n     addedge(p, i); n   } n   dfs(1); n   while (q--) { n     int x; n     scanf( %d , &x); n     printf( %d n , ans[x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, temp, prev, A[100005]; n   bool f = false; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n   } n   for (int i = 0; i < k; i++) { n     cin >> temp; n   } n   if (k == 1) { n     if (n == 1) { n       cout <<  Yes ; n       f = true; n     } else { n       int prevMax; n       for (int i = 0; i < n; i++) { n         if (A[i] == 0) { n           A[i] = temp; n         } n         if (i != 0) { n           if (A[i] <= prevMax) { n             cout <<  Yes ; n             f = true; n             break; n           } n           prevMax = A[i]; n         } else { n           prevMax = A[0]; n         } n       } n     } n   } else { n     cout <<  Yes ; n     f = true; n   } n   if (!f) { n     cout <<  No ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, pair<int, int> > ins; n void operator+=(pair<int, int> &i, pair<int, int> &j) { n   i.first += j.first, i.second += j.second; n } n int main() { n   ins[ U ] = pair<int, int>(0, 1), ins[ D ] = pair<int, int>(0, -1), n   ins[ L ] = pair<int, int>(-1, 0), ins[ R ] = pair<int, int>(1, 0); n   int n, m, w = 0; n   pair<int, int> p; n   char s[205]; n   scanf( %d %s , &n, s), m = strlen(s); n   for (int i = 0; i < m - 1; ++i) { n     p = ins[s[i]]; n     for (int j = i + 1; j < m; ++j) { n       p += ins[s[j]]; n       if (p == pair<int, int>(0, 0)) w++; n     } n   } n   printf( %d n , w); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long a[n]; n   vector<long long> pref(n + 1, 0); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     pref[i + 1] = pref[i] + a[i]; n   } n   map<long long, int> m; n   m[0] = 0; n   long long ans = 0; n   int prev = -1; n   for (int i = 1; i <= n; i++) { n     if (m.find(pref[i]) != m.end()) { n       if (m[pref[i]] >= prev - 1) { n         ans++; n         prev = i; n       } n     } n     m[pref[i]] = i; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n namespace ZDY { n #pragma GCC optimize(3) n template <class T> n __inline__ __attribute__((always_inline)) T ABS(T x) { n   return x > 0 ? x : -x; n } n template <class T> n __inline__ __attribute__((always_inline)) T MAX(T x, T y) { n   return x > y ? x : y; n } n template <class T> n __inline__ __attribute__((always_inline)) T MIN(T x, T y) { n   return x < y ? x : y; n } n template <class T> n __inline__ __attribute__((always_inline)) T GCD(T x, T y) { n   return y ? GCD(y, x % y) : x; n } n template <class T> n __inline__ __attribute__((always_inline)) void SWAP(T& x, T& y) { n   T t = x; n   x = y; n   y = t; n } n }  // namespace ZDY n using namespace ZDY; n using namespace std; n namespace IO { n const char* ln =   n ; n const int str = 1 << 20; n struct IN { n   char buf[str], *s, *t; n   bool _; n   IN() : s(buf), t(buf), _(0) {} n   __inline__ __attribute__((always_inline)) char gc() { n     return s == t && ((t = (s = buf) + fread(buf, 1, str, stdin)) == s) n                ? EOF n                : (*s++); n   } n   IN& operator>>(char& ch) { n     if (_) return *this; n     char c; n     while ((c = gc()) != EOF && isspace(c)) n       ; n     if (c == EOF) n       _ = 1; n     else n       ch = c; n     return *this; n   } n   IN& operator>>(char* ch) { n     memset(ch, 0, sizeof(ch)); n     if (_) return *this; n     char c; n     while ((c = gc()) != EOF && isspace(c)) n       ; n     if (c == EOF) return _ = 1, *this; n     *ch = c; n     ch++; n     while ((c = gc()) != EOF && !isspace(c)) *ch = c, ch++; n     if (c == EOF) _ = 1; n     return *this; n   } n   IN& operator>>(string& ch) { n     if (_) return *this; n     char c; n     while ((c = gc()) != EOF && isspace(c)) n       ; n     if (c == EOF) return _ = 1, *this; n     ch += c; n     while ((c = gc()) != EOF && !isspace(c)) ch += c; n     if (c == EOF) _ = 1; n     return *this; n   } n   template <typename T> n   IN& operator>>(T& x) { n     if (_) return *this; n     char c = gc(); n     bool ff = 0; n     while (c != EOF && (c <  0  || c >  9 )) ff ^= (c ==  - ), c = gc(); n     if (c == EOF) { n       _ = 1; n       return *this; n     } n     x = 0; n     while (c != EOF &&  0  <= c && c <=  9 ) n       x = (x << 3) + (x << 1) + c - 48, c = gc(); n     if (c == EOF) _ = 1; n     if (ff) x = -x; n     return *this; n   } n } in; n struct OUT { n   char buf[str], *s, *t; n   OUT() : s(buf), t(buf + str) {} n   ~OUT() { fwrite(buf, 1, s - buf, stdout); } n   void pt(char c) { n     (s == t) ? (fwrite(s = buf, 1, str, stdout), *s++ = c) : (*s++ = c); n   } n   OUT& operator<<(const char* s) { n     while (*s) pt(*s++); n     return *this; n   } n   OUT& operator<<(char* s) { n     while (*s) pt(*s++); n     return *this; n   } n   OUT& operator<<(string s) { n     for (int i = 0; s[i]; i++) pt(s[i]); n     return *this; n   } n   template <typename T> n   OUT& operator<<(T x) { n     if (!x) return pt( 0 ), *this; n     if (x < 0) pt( - ), x = -x; n     char a[30], t = 0; n     while (x) a[t++] = x % 10, x /= 10; n     while (t--) pt(a[t] +  0 ); n     return *this; n   } n } out; n }  // namespace IO n using namespace IO; n int n, b[1000011], a[1000011], cnt = 0, head[1000011]; n bool cmp(int x, int y) { return a[x] > a[y]; } n bool cmp2(int x, int y) { return a[x] < a[y]; } n struct edge { n   int to, nxt; n } e[1000011 * 2]; n void add(int x, int y) { n   e[++cnt].to = y; n   e[cnt].nxt = head[x]; n   head[x] = cnt; n } n int f[1000011], sz[1000011]; n int gf(int x) { return (x == f[x]) ? x : (f[x] = gf(f[x])); } n bool v[1000011]; n int main() { n   in >> n; n   for (int i = 1; i <= n; ++i) in >> a[i], b[i] = i; n   sort(b + 1, b + n + 1, cmp2); n   int x, y; n   for (int i = 1; i <= n - 1; ++i) in >> x >> y, add(x, y), add(y, x); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) f[i] = i, sz[i] = 1; n   for (int i = 1; i <= n; ++i) { n     x = b[i]; n     v[x] = 1; n     int t = 0; n     for (int i = head[x], to; to = e[i].to, i; i = e[i].nxt) n       if (v[to]) { n         ans += 1ll * sz[x] * sz[gf(to)] * a[x]; n         sz[x] += sz[gf(to)]; n         f[gf(to)] = x; n       } n   } n   memset(v, 0, sizeof(v)); n   for (int i = 1; i <= n; ++i) f[i] = i, sz[i] = 1; n   sort(b + 1, b + n + 1, cmp); n   for (int i = 1; i <= n; ++i) { n     x = b[i]; n     v[x] = 1; n     int t = 0; n     for (int i = head[x], to; to = e[i].to, i; i = e[i].nxt) n       if (v[to]) { n         ans -= 1ll * sz[x] * sz[gf(to)] * a[x]; n         sz[x] += sz[gf(to)]; n         f[gf(to)] = x; n       } n   } n   out << ans << ln; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[100007]; n pair<int, int> b[100007]; n pair<int, int> p, r; n double t, q, x, y, w; n int fl = 1; n int main() { n   int n; n   int d1, d2, res = 0; n   scanf( %d , &n); n   ; n   scanf( %d , &d1); n   ; n   scanf( %d , &d2); n   ; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i].first); n     ; n     scanf( %d , &a[i].second); n     ; n     scanf( %d , &b[i].first); n     ; n     scanf( %d , &b[i].second); n     ; n   } n   for (int i = 1; i < n; ++i) { n     p.first = b[i + 1].first - a[i + 1].first; n     p.second = b[i + 1].second - a[i + 1].second; n     r.first = b[i].first - a[i].first; n     r.second = b[i].second - a[i].second; n     t = 2.0 * r.first * (p.first - r.first); n     t += 2.0 * r.second * (p.second - r.second); n     q = 2.0 * (r.first - p.first) * (r.first - p.first); n     q += 2.0 * (r.second - p.second) * (r.second - p.second); n     t = t / q; n     t = -t; n     x = r.first * r.first + r.second * r.second; n     y = p.first * p.first + p.second * p.second; n     if (t <= 0.0 || t >= 1.0) { n       if (y <= 1.0 * d1 * d1 && fl == 1) { n         res += 1; n         fl = 0; n       } else if (y > 1.0 * d2 * d2 && fl == 0) n         fl = 1; n     } else { n       w = (t * (p.first - r.first) + r.first) * n           (t * (p.first - r.first) + r.first); n       w += (t * (p.second - r.second) + r.second) * n            (t * (p.second - r.second) + r.second); n       if (w < x) { n         if (w <= 1.0 * d1 * d1 && fl == 1) { n           res += 1; n           fl = 0; n         } n         if (y > 1.0 * d2 * d2 && fl == 0) fl = 1; n       } else { n         if (w > 1.0 * d2 * d2 && fl == 0) fl = 1; n         if (y <= 1.0 * d1 * d1 && fl == 1) { n           res += 1; n           fl = 0; n         } n       } n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400000 + 5; n int n, m, tr[N << 2][3], q; n set<int> ssp[N][2]; n void modify(int x, int who, int v, int l = 1, int r = n, int k = 1) { n   if (l == r) { n     if (ssp[l][who].count(v)) n       ssp[l][who].erase(v); n     else n       ssp[l][who].insert(v); n     tr[k][0] = ssp[l][0].empty() ? 0x3f3f3f3f : *ssp[l][0].begin(); n     tr[k][1] = ssp[l][1].empty() ? 0 : *ssp[l][1].rbegin(); n     tr[k][2] = tr[k][1] >= tr[k][0]; n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) n     modify(x, who, v, l, mid, k << 1); n   else n     modify(x, who, v, mid + 1, r, k << 1 | 1); n   tr[k][1] = max(tr[k << 1][1], tr[k << 1 | 1][1]); n   tr[k][0] = min(tr[k << 1][0], tr[k << 1 | 1][0]); n   tr[k][2] = n       (tr[k << 1 | 1][1] >= tr[k << 1][0]) | tr[k << 1][2] | tr[k << 1 | 1][2]; n } n void build(int l = 1, int r = n, int k = 1) { n   tr[k][0] = 0x3f3f3f3f, tr[k][1] = tr[k][2] = 0; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(l, mid, k << 1), build(mid + 1, r, k << 1 | 1); n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   n <<= 1, m <<= 1, build(); n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x & 1) { n       modify(x, 0, y); n     } else { n       --x, --y; n       modify(x, 1, y); n     } n     printf( %s n , !tr[1][2] ?  YES  :  NO ); n   } n   return 0; n } n