#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n string conv(int n) { n   string kq =   ; n   while (n != 0) { n     int r = n % 26; n     if (r == 0) r = 26; n     kq = char(r - 1 +  A ) + kq; n     n = (n - r) / 26; n   } n   return kq; n } n int conv(string s) { n   int kq = 0; n   int n = s.size(); n   int tmp = 1; n   for (int i = n - 1; i >= 0; i--) { n     int k = int(s[i] -  A ) + 1; n     kq += tmp * k; n     tmp *= 26; n   } n   return kq; n } n int type(string s) { n   if (s[0] !=  R ) return 1; n   if (s[1] >=  A  && s[1] <=  Z ) return 1; n   bool kt = false; n   int n = s.size(); n   for (int i = 0; i <= n - 1; i++) n     if (s[i] ==  C ) { n       kt = true; n       break; n     } n   if (!kt) return 1; n   return 2; n } n void chuyen(string s) { n   if (type(s) == 1) { n     int n = s.size(); n     string x1 =   ; n     int j = 0; n     while (s[j] >=  A  && s[j] <=  Z ) { n       x1 += s[j]; n       j++; n     } n     cout <<  R ; n     for (int i = j; i <= n - 1; i++) cout << s[i]; n     cout <<  C  << conv(x1) <<   n ; n   } n   if (type(s) == 2) { n     int kq = 0; n     int tmp = 1; n     int j = s.size() - 1; n     while (s[j] >=  0  && s[j] <=  9 ) { n       kq += (s[j] -  0 ) * tmp; n       tmp *= 10; n       j--; n     } n     cout << conv(kq); n     for (int i = 1; i <= j - 1; i++) cout << s[i]; n     cout <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     chuyen(s); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solution() { n   long long n; n   cin >> n; n   vector<vector<long long>> v(n, vector<long long>(n, 0)); n   if (n % 2 != 0) { n     v[n - 1][(n - 1) / 2] = 1; n     v[(n - 1) / 2][n - 1] = 1; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       if (i == j || i == n - j - 1) v[i][j] = 1; n       cout << v[i][j] <<    ; n     } n     cout <<   n ; n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) solution(); n } n


#include <bits/stdc++.h> n std::vector<int> V[200010], V2[200010]; n int check[200010]; n std::stack<int> St; n void init(int k) { n   if (check[k] == 1) return; n   check[k] = 1; n   for (int i = 0; i < V[k].size(); i++) init(V[k][i]); n   St.push(k); n } n int color[200010]; n void makeSCC(int k, int c) { n   if (color[k] > 0) return; n   color[k] = c; n   for (int i = 0; i < V2[k].size(); i++) makeSCC(V2[k][i], c); n } n int x[200010], cost[200010]; n std::vector<int> V3[200010]; n int main() { n   int a; n   scanf( %d , &a); n   for (int i = 1; i <= a; i++) scanf( %d , &x[i]); n   for (int i = 1; i <= a; i++) { n     int b; n     scanf( %d , &b); n     V[i].push_back(b); n     V2[b].push_back(i); n   } n   for (int i = 1; i <= a; i++) init(i); n   int c = 1; n   while (!St.empty()) { n     int k = St.top(); n     St.pop(); n     if (color[k] == 0) makeSCC(k, c++); n   } n   for (int i = 1; i <= a; i++) cost[i] = 123456789; n   for (int i = 1; i <= a; i++) n     cost[color[i]] = cost[color[i]] < x[i] ? cost[color[i]] : x[i]; n   for (int i = 1; i <= a; i++) n     if (color[i] != color[V[i][0]]) V3[color[i]].push_back(color[V[i][0]]); n   long long int ans = 0; n   for (int i = 1; i <= a; i++) n     if (V3[i].size() == 0 && cost[i] < 123456789) ans += cost[i]; n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n int n, ust, x, y, mark[10]; n int main() { n   scanf( %d , &n); n   scanf( %d , &ust); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x, &y); n     mark[x] = 1; n     mark[y] = 1; n     mark[7 - x] = 1; n     mark[7 - y] = 1; n     if (!mark[7 - ust]) n       for (int j = 0; j < 7; j++) mark[j] = 0; n     else { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   cout << n / (2520LL); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000001; n int n, kt[N], a[N], cnt, x[N]; n long long k, s; n long long cal(int i) { n   long long res = i; n   return res * (res + 1) / 2; n } n int main() { n   scanf( %d%lld , &n, &k); n   if (k < cal(n)) { n     printf( -1 ); n     return 0; n   } n   for (int i = n; i > 0; i--) { n     int h = n - (n - i) / 2; n     if (k - h < cal(i - 1)) h = k - cal(i - 1); n     k -= h; n     kt[h]++; n     if (kt[h] == 2) a[++cnt] = h; n   } n   for (int i = 1; i <= n; i++) s += kt[i] * i; n   printf( %lld n , s); n   for (int i = 1; i <= n; i++) { n     if (kt[i] == 1) n       x[i] = i; n     else if (kt[i] == 0) { n       x[a[cnt]] = i; n       x[i] = a[cnt]; n       cnt--; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , i); n   printf(  n ); n   for (int i = 1; i <= n; i++) printf( %d  , x[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, m, q; n   cin >> n >> m >> q; n   vector<vector<int>> r(n), c(m); n   for (int i = 0; i < q; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     r[x].push_back(y); n     c[y].push_back(x); n   } n   if (n == 1) { n     cout << m - q; n     return 0; n   } n   if (m == 1) { n     cout << n - q; n     return 0; n   } n   vector<char> used_row(n, false); n   vector<char> used_col(m, false); n   int ans = 0; n   vector<int> cols_to_add(r[0]), next_cols_to_add; n   used_row[0] = true; n   for (int i = 0; i < m; ++i) { n     if (used_col[i]) continue; n     while (!cols_to_add.empty()) { n       next_cols_to_add.clear(); n       for (int col : cols_to_add) { n         if (used_col[col]) continue; n         used_col[col] = true; n         for (int row : c[col]) { n           if (used_row[row]) continue; n           used_row[row] = true; n           for (int new_col : r[row]) { n             next_cols_to_add.push_back(new_col); n           } n         } n       } n       cols_to_add = next_cols_to_add; n     } n     if (!used_col[i]) { n       ++ans; n       cols_to_add.push_back(i); n     } n   } n   while (!cols_to_add.empty()) { n     next_cols_to_add.clear(); n     for (int col : cols_to_add) { n       if (used_col[col]) continue; n       used_col[col] = true; n       for (int row : c[col]) { n         if (used_row[row]) continue; n         used_row[row] = true; n         for (int new_col : r[row]) { n           next_cols_to_add.push_back(new_col); n         } n       } n     } n     cols_to_add = next_cols_to_add; n   } n   for (int i = 1; i < n; ++i) { n     if (!used_row[i]) { n       ++ans; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n bool canWin(long long b, long long e) { n   if (e % 2 == 1) { n     return b % 2 == 0; n   } else if (e < 2 * b) { n     return b % 2 == 1; n   } else if (e < 4 * b) { n     return 1; n   } else { n     return canWin(b, e / 4); n   } n } n bool canLose(long long b, long long e) { n   if (e < 2 * b) { n     return 1; n   } else if (e / 2 % 2 == 1) { n     return b % 2 == 0; n   } else if (e < 4 * b) { n     return b % 2 == 1; n   } else { n     return canLose(b, e / 4); n   } n } n int main() { n   int t; n   scanf( %d , &t); n   bool canBeWinner, canBeLoser; n   bool canBeFirst = true; n   bool canBeSecond = false; n   for (int i = 0; i < t; i++) { n     long long b, e; n     scanf( %lld%lld , &b, &e); n     bool cWin = canWin(b, e); n     bool cLose = canLose(b, e); n     canBeWinner = (canBeFirst && cWin) || (canBeSecond && !cWin); n     canBeLoser = (canBeFirst && cLose) || (canBeSecond && !cLose); n     canBeFirst = canBeLoser; n     canBeSecond = canBeWinner; n   } n   printf( %d %d n , canBeWinner, canBeLoser); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const long long N = 4e7 + 5, N_ = 4e6 + 5; n int cnt = 0, prime[N_]; n bitset<N> vis; n void pre() { n   for (int i = 2; i < N; ++i) { n     if (!vis[i]) prime[cnt++] = i; n     for (int j = 0; i * prime[j] < N; ++j) { n       vis[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n; n   if (n == 2) { n     cout <<  1 1  << endl; n     return 0; n   } n   if (n == 3) { n     cout <<  1 1 2  << endl; n     return 0; n   } n   if (n == 4) { n     cout <<  1 1 2 1  << endl; n     return 0; n   } n   if (n == 5) { n     cout <<  1 2 1 1 1  << endl; n     return 0; n   } n   if (n == 6) { n     cout <<  1 2 1 1 1 1  << endl; n     return 0; n   } n   pre(); n   m = n * (n + 1) / 2; n   bool sign = 0; n   vector<int> res(n + 1, 0); n   int flag = 0; n   if (m & 1) { n     if (!vis[m - 2]) { n       ++res[2]; n       res.erase(res.begin()); n       for (auto i : res) cout << i + 1 <<    ; n       cout << endl; n       return 0; n     } n     m -= 3; n     flag = 3; n     res[3] = 2; n   } n   for (auto i : prime) { n     int t = m - i; n     if (!i || t < 0) break; n     if (!vis[t]) { n       for (int i = n; t && i > flag; --i) { n         if (i == flag) continue; n         if (i <= t) { n           t -= i; n           ++res[i]; n         } n       } n       if (t) { n         res = vector<int>(n + 1, 0); n         res[flag] = 2; n       } else { n         sign = 1; n         break; n       } n     } n   } n   if (!sign) { n     cout << -1 << endl; n     return 0; n   } n   res.erase(res.begin()); n   for (auto i : res) cout << i + 1 <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n inline long long read() { n   long long 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 << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long qpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = 1ll * ans * a % 1000000007; n     a = 1ll * a * a % 1000000007; n     b >>= 1; n   } n   return ans % 1000000007; n } n signed main() { n   cin >> n >> m; n   long long c = n - m + 1; n   c *= 2; n   n++; n   n *= 2; n   m--; n   long long tot = qpow(n, m); n   long long amo = (c * tot) % 1000000007; n   amo %= 1000000007; n   cout << amo; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int pr(int i) { return (i - 1 + n) % n; } n int nx(int i) { return (i + 1) % n; } n int get(int i, int j) { n   if (i > j) swap(i, j); n   return min(j - i, n - j + i); n } n char no(char el) { n   if (el ==  B ) n     return  W ; n   else n     return  B ; n } n int gg(string s) { n   int b = 1; n   for (char i : s) { n     if (i !=  B ) b = 0; n   } n   int w = 1; n   for (char i : s) { n     if (i !=  W ) w = 0; n   } n   return max(w, b); n } n signed main() { n   string s; n   cin >> n >> k >> s; n   if (gg(s)) { n     cout << s; n     return 0; n   } n   vector<int> used(n, 0); n   int l = 0, r = 0, op = 1e6; n   bool ch = false; n   while (l < n && op > 0) { n     --op; n     if (used[l]) { n       ++l; n     } else if (s[l] == s[pr(l)]) { n       ch = true; n       while (s[l] == s[nx(l)]) used[l] = 1, l = nx(l); n       r = nx(l); n       while (s[r] != s[nx(r)]) used[r] = 1, r = nx(r); n       int cr = r; n       while (s[nx(cr)] == s[cr]) used[cr] = 1, cr = nx(cr); n       char last = s[l]; n       for (int i = nx(l); i != r; i = nx(i)) { n         if (get(l, i) > k && get(r, i) > k) n           s[i] = no(last), last = no(last); n         else if (get(l, i) < get(r, i)) n           s[i] = s[l], last = s[l]; n         else if (get(l, i) >= get(r, i)) n           s[i] = s[r], last = s[r]; n       } n       ++l; n     } else { n       ++l; n     } n   } n   if (!ch) { n     if (k % 2 == 0) n       cout << s; n     else n       for (char i : s) cout << no(i); n     return 0; n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100100]; n int aa[100100]; n long long dfs(int u, int fa) { n   vector<long long> tmp; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v != fa && aa[v] - 1 >= 0) { n       aa[v] -= 1; n       tmp.push_back(dfs(v, u)); n     } n   } n   sort(tmp.begin(), tmp.end()); n   int cc = tmp.size(); n   long long ans = 0; n   for (int i = aa[u]; i >= 1; i--) { n     if (cc > 0) { n       ans += tmp[--cc] + 2; n       aa[u]--; n     } else { n       break; n     } n   } n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v != fa) { n       int tmp = min(aa[u], aa[v]); n       ans += tmp * 2; n       aa[u] -= tmp; n       aa[v] -= tmp; n     } n   } n   return ans; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &aa[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   int s; n   scanf( %d , &s); n   printf( %I64d n , dfs(s, -1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int TESTS, k, a, n, b, c, d, m, x, i, y, z; n   cin >> TESTS; n   while (TESTS--) { n     cin >> n; n     cin >> s; n     long long int ans = n; n     for (long long int i = 0; i <= n - 1; i++) { n       if (s[i] ==  1 ) { n         a = max(i + 1, n - i); n         ans = max(ans, 2 * a); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int limit = 10000; n int query(vector<long long> &v) { n   int n = (int)v.size(); n   cout << n <<    ; n   for (int i = 0; i < n; i++) { n     cout << v[i] << (i == n - 1 ?   n  :    ); n   } n   int k; n   cin >> k; n   if (k == -1) { n     exit(0); n   } else if (k == -2) { n     assert(false); n   } n   return k; n } n int main() { n   vector<vector<long long>> dp(6, vector<long long>(10010)); n   for (int q = 1; q <= 5; q++) { n     for (long long p = 1; p <= limit; p++) { n       if (q == 5 && p == 2) { n         break; n       } n       long long x = p; n       for (int i = 0; i < p; i++) { n         x += dp[q - 1][min(10000LL, x)]; n         x++; n       } n       x += dp[q - 1][min(10000LL, x)]; n       dp[q][p] = x - p; n     } n   } n   long long lb = 1, ub = 10004205361450474LL; n   for (int q = 5; q >= 1; q--) { n     vector<long long> hoge; n     long long x = lb; n     for (int i = 0; i < min(10000LL, lb); i++) { n       x += dp[q - 1][min((long long)10000, x)]; n       hoge.push_back(x); n       x++; n     } n     x += dp[q - 1][min(10000LL, x)]; n     int k = query(hoge); n     if (k == 0) { n       ub = hoge[0] - 1; n     } else if (k == (int)hoge.size()) { n       lb = hoge.back() + 1; n     } else { n       lb = hoge[k - 1] + 1; n       ub = hoge[k] - 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)1e5 + 10; n int sx[MAXN], sy[MAXN], sz[MAXN]; n char s[MAXN]; n void solve(int x, int y, int z) { n   if (x + y + z < 3) { n     printf( YES n ); n     return; n   } n   int e = (x + y + z) / 3; n   vector<int> v; n   v.push_back(x); n   v.push_back(y); n   v.push_back(z); n   sort(v.begin(), v.end()); n   if ((x + y + z) % 3 == 0) { n     if (x == e && y == e && z == e) n       printf( YES n ); n     else n       printf( NO n ); n     return; n   } else if ((x + y + z) % 3 == 1) { n     if (v[2] == e + 1 && v[1] == e) n       printf( YES n ); n     else n       printf( NO n ); n     return; n   } else { n     if (v[2] == e + 1 && v[1] == e + 1) n       printf( YES n ); n     else n       printf( NO n ); n     return; n   } n } n int main() { n   scanf( %s , s); n   int n = strlen(s); n   for (int i = 1; i <= n; i++) { n     sx[i] = sx[i - 1] + (s[i - 1] ==  x ); n     sy[i] = sy[i - 1] + (s[i - 1] ==  y ); n     sz[i] = sz[i - 1] + (s[i - 1] ==  z ); n   } n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     int x = sx[r] - sx[l - 1], y = sy[r] - sy[l - 1], z = sz[r] - sz[l - 1]; n     solve(x, y, z); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int siz = 3e5 + 5; n map<int, vector<int>> adjList; n long long int ans[siz] = {0}; n long long int cum[siz] = {0}; n map<long long int, long long int> q[siz]; n void dfs(int cur, int parent, int level, long long int val) { n   for (pair<long long int, long long int> temp : q[cur]) { n     cum[level] += temp.second; n     cum[min(level + temp.first + 1, siz - 1)] -= temp.second; n   } n   val += cum[level]; n   ans[cur] = val; n   for (int child : adjList[cur]) { n     if (child != parent) { n       dfs(child, cur, level + 1, val); n     } n   } n   for (pair<long long int, long long int> temp : q[cur]) { n     cum[level] -= temp.second; n     cum[min(level + temp.first + 1, siz - 1)] += temp.second; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int n; n   cin >> n; n   int y; n   long long int x; n   for (int i = 0; i < (int)(n - 1); i++) { n     cin >> x >> y; n     adjList[x].push_back(y); n     adjList[y].push_back(x); n   } n   int m; n   cin >> m; n   int d, v; n   for (int i = 0; i < (int)(m); i++) { n     cin >> v >> d >> x; n     q[v][d] += x; n   } n   dfs(1, -1, 1, 0); n   for (int i = 1; i <= (int)(n); i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long n, a[130], i, b = 1, f[10002]; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     f[a[i]]++; n   } n   sort(a + 1, a + n + 1); n   cout << a[n] <<    ; n   for (i = 1; i < n; i++) { n     if (f[a[i]] == 1 && a[n] % a[i] != 0) { n       b = a[i]; n     } else if (f[a[i]] == 2) { n       b = a[i]; n     } n   } n   cout << b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 998244353; n inline long long Msum(long long x) { return x; } n template <typename... Rest> n inline long long Msum(long long x, Rest... rest) { n   return (x + Msum(rest...)) % MOD; n } n inline long long Mprod(long long x) { return x; } n template <typename... Rest> n inline long long Mprod(long long x, Rest... rest) { n   return x * Mprod(rest...) % MOD; n } n inline long long Mnorm(long long x) { return (x % MOD + MOD) % MOD; } n long long Msq(long long x) { return x * x % MOD; } n long long Mpow(long long b, long long e) { n   return e ? Mprod(Msq(Mpow(b, e >> 1)), (e & 1 ? b : 1)) : 1; n } n long long Minv(long long x) { return Mpow(x, MOD - 2); } n const int MAXN = 51; n int sns[MAXN], w[MAXN]; n int wi, sn; n int sl, sd, S; n long long dp[MAXN][MAXN][MAXN]; n long long f(int h, int hl, int hd, int t) { n   if (!t) return 0; n   long long &ret = dp[h][hl][hd]; n   if (ret != -1) return ret; n   ret = 0; n   long long cS = S + hl - hd + sn * h; n   ret += Mprod(wi + sn * h, Minv(cS), 1 + f(h + 1, hl, hd, t - 1)); n   ret = Mnorm(ret); n   ret += Mprod(sl + hl, Minv(cS), f(h, hl + 1, hd, t - 1)); n   ret = Mnorm(ret); n   ret += Mprod(sd - hd, Minv(cS), f(h, hl, hd + 1, t - 1)); n   ret = Mnorm(ret); n   assert(sd - hd + sl + hl + wi + sn * h == cS); n   return ret; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &sns[i]); n     sns[i] = sns[i] * 2 - 1; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &w[i]); n   } n   for (int i = 0; i < n; i++) { n     S = sl = sd = 0; n     wi = w[i]; n     sn = sns[i]; n     for (int j = 0; j < (int)n; j++) { n       S += w[j]; n       if (j != i) { n         if (sns[j] == 1) n           sl += w[j]; n         else n           sd += w[j]; n       } n     } n     memset(dp, -1, sizeof(dp)); n     printf( %lld n , Mnorm(wi + sn * f(0, 0, 0, m))); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1111111; n int a[N]; n int n, k; n vector<int> prime, p; n void Getprime() { n   for (int i = 2; i <= 1000; i++) { n     bool bo = 1; n     for (int j = 2; j * j <= i; j++) { n       if (i % j == 0) { n         bo = 0; n         break; n       } n     } n     if (bo) { n       prime.push_back(i); n     } n   } n } n int main() { n   Getprime(); n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n   } n   int K = k; n   for (int i = 0; i < prime.size() && prime[i] * prime[i] <= k; ++i) { n     int t = prime[i]; n     if (k % t == 0) { n       int m = 1; n       while (K % t == 0 && K) { n         m *= t; n         K /= t; n       } n       p.push_back(m); n     } n   } n   if (K != 1) { n     p.push_back(K); n   } n   for (int i = 0; i < p.size(); ++i) { n     int pr = p[i]; n     bool flag = false; n     for (int j = 1; j <= n; ++j) { n       if (a[j] % pr == 0) { n         flag = true; n         break; n       } n     } n     if (!flag) { n       puts( No ); n       return 0; n     } n   } n   puts( Yes ); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void smn(T &a, const T &b) { n   if (b < a) a = b; n } n template <class T> n inline void smx(T &a, const T &b) { n   if (b > a) a = b; n } n template <class T> n inline T gcd(T x, T y) { n   return y ? gcd(y, x % y) : x; n } n namespace std { n template <class T, class U> n struct hash<pair<T, U>> { n   inline size_t operator()(const pair<T, U> &p) const { n     return hash<T>()(p.first) * 701 + hash<U>()(p.second); n   } n }; n }  // namespace std n const double eps = 1e-8; n const int key = 701; n const int MN = 1000 * 1000 + 1000; n string s; n long long h1[MN], pw1[MN], h2[MN], pw2[MN]; n const int MOD = 1000 * 1000 * 1000 + 7; n long long add[MN]; n int n, k; n pair<long long, long long> get(int s, int e) { n   long long res1 = h1[e]; n   if (s != 0) res1 -= pw1[e - s + 1] * h1[s - 1]; n   long long res2 = h2[e]; n   if (s != 0) res2 -= pw2[e - s + 1] * h2[s - 1], res2 %= MOD; n   if (res2 < 0) res2 += MOD; n   return make_pair(res1, res2); n } n bool check(int l) { n   auto v = get(0, l - 1); n   int now = l; n   for (int i = 1; i < k; i++) { n     if (get(now, now + l - 1) != v) return 0; n     now += l; n   } n   return 1; n } n int fnd(int p) { n   int s = 1, e = n - p, res = 0; n   while (s <= e) { n     int m = (s + e) / 2; n     if (get(0, m - 1) == get(p, p + m - 1)) n       res = m, s = m + 1; n     else n       e = m - 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k >> s; n   long long now = 0; n   for (int i = 0; i < n; i++) now = now * 701 + s[i], h1[i] = now; n   pw1[0] = 1; n   for (int i = 1; i <= n; i++) pw1[i] = 701 * pw1[i - 1]; n   now = 0; n   for (int i = 0; i < n; i++) now = (now * 701 + s[i]) % MOD, h2[i] = now; n   pw2[0] = 1; n   for (int i = 1; i <= n; i++) pw2[i] = (701 * pw2[i - 1]) % MOD; n   for (int i = 1; i <= n; i++) n     if (n / i >= k && check(i)) { n       int t = i * k; n       int b = fnd(t); n       smn(b, i); n       add[t - 1] += 1; n       add[t + b] += -1; n     } n   now = 0; n   for (int i = 0; i < n; i++) { n     now += add[i]; n     cout << (now ? 1 : 0); n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int upd(int& a, int b) { return a = min(a, b); } n inline int _abs(int x) { return x > 0 ? x : -x; } n char c; n int n, cnt[3], type[80]; n int pos[3][80], leq[3][80]; n int dp[80][80][80][80]; n void getpos(void) { n   for (int p = 0; p <= 2; p++) { n     cnt[p] = 0; n     for (int y = 1; y <= n; y++) { n       while (y <= n && type[y] != p) y++; n       if (y <= n) pos[p][++cnt[p]] = y; n     } n     leq[p][0] = 0; n     for (int i = 1; i <= n; i++) leq[p][i] = leq[p][i - 1] + (type[i] == p); n   } n   return; n } n void trans(int i, int j, int k, int lst) { n   int p0 = pos[0][i], p1 = pos[1][j], p2 = pos[2][k]; n   if (i > 0) n     upd(dp[0][i][j][k], n         dp[lst][i - 1][j][k] + _abs(j - leq[1][p0]) + _abs(k - leq[2][p0])); n   if (j > 0) n     upd(dp[1][i][j][k], n         dp[lst][i][j - 1][k] + _abs(i - leq[0][p1]) + _abs(k - leq[2][p1])); n   if (k > 0 && lst != 1) n     upd(dp[2][i][j][k], n         dp[lst][i][j][k - 1] + _abs(i - leq[0][p2]) + _abs(j - leq[1][p2])); n   return; n } n int main(void) { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf(  %c , &c); n     type[i] = (c ==  V  ? 1 : (c ==  K  ? 2 : 0)); n   } n   getpos(); n   memset(dp, 0x1f, sizeof(dp)); n   dp[0][0][0][0] = 0; n   for (int i = 0; i <= cnt[0]; i++) n     for (int j = 0; j <= cnt[1]; j++) n       for (int k = 0; k <= cnt[2]; k++) n         for (int lst = 0; lst <= 2; lst++) trans(i, j, k, lst); n   int ans = n * n; n   for (int i = 0; i <= 2; i++) upd(ans, dp[i][cnt[0]][cnt[1]][cnt[2]]); n   printf( %d n , ans >> 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000000; n int xx[1111111], yy[1111111]; n int pos[1111111], n, m, op[1111111], np[1111111], ff; n int owner[3333333], first[3333333]; n int ans[1111111]; n bool used[1111111]; n void imp() { n   puts( -1 ); n   exit(0); n } n inline int sum(int x) { n   int r = 0; n   while (x >= 0) { n     r += first[x]; n     x = (x & (x + 1)) - 1; n   } n   return r; n } n inline void add(int x, int d) { n   while (x <= maxn) { n     first[x] += d; n     x = (x | (x + 1)); n   } n } n inline int getFirst(int x) { n   int l = -1, r = maxn; n   while (r - l > 1) { n     int mid = ((l + r) >> 1); n     if (sum(mid) >= x) { n       r = mid; n     } else { n       l = mid; n     } n   } n   return r; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &xx[i], &yy[i]); n     --xx[i]; n   } n   memset(owner, -1, sizeof(owner)); n   memset(pos, -1, sizeof(pos)); n   int ff = m; n   for (int i = 1; i <= n; ++i) { n     add(ff + i, 1); n   } n   for (int i = 0; i < m; ++i) { n     int &oldPlace = op[i]; n     int &newPlace = np[i]; n     int &x = xx[i]; n     int &y = yy[i]; n     oldPlace = getFirst(y); n     if ((pos[x] != -1 && oldPlace != pos[x]) || n         (owner[oldPlace] != -1 && owner[oldPlace] != x)) { n       imp(); n     } n     newPlace = ff--; n     owner[oldPlace] = -1; n     add(oldPlace, -1); n     add(newPlace, 1); n     pos[x] = newPlace; n     owner[newPlace] = x; n   } n   for (int i = m - 1; i >= 0; --i) { n     int &oldPlace = op[i]; n     int &newPlace = np[i]; n     int &x = xx[i]; n     int &y = yy[i]; n     pos[x] = oldPlace; n     owner[oldPlace] = x; n     add(oldPlace, 1); n     owner[newPlace] = -1; n     add(newPlace, -1); n   } n   memset(ans, -1, sizeof(ans)); n   for (int i = 0; i < n; ++i) n     if (pos[i] != -1) { n       int p = sum(pos[i]); n       ans[p - 1] = i; n       used[i] = true; n     } n   int fx = 0; n   for (int i = 0; i < n; ++i) n     if (ans[i] == -1) { n       while (used[fx]) { n         ++fx; n       } n       ans[i] = fx; n       used[fx] = true; n     } n   for (int i = 0; i < n; ++i) { n     if (i > 0) { n       putchar(   ); n     } n     printf( %d , ans[i] + 1); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long spf[100001]; n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long power(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a); n     a = (a * a); n     b >>= 1; n   } n   return res; n } n long long modpow(long long a, long long b, long long x) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % x; n     a = (a * a) % x; n     b >>= 1; n   } n   return res; n } n void sieve() { n   spf[1] = 1; n   for (long long i = 2; i < 100001; i++) spf[i] = i; n   for (long long i = 4; i < 100001; i += 2) spf[i] = 2; n   for (long long i = 3; i * i < 100001; i++) n     if (spf[i] == i) n       for (long long j = i * i; j < 100001; j += i) n         if (spf[j] == j) spf[j] = i; n } n vector<long long> getFactorization(long long x) { n   vector<long long> ret; n   while (x != 1) { n     ret.push_back(spf[x]); n     x = x / spf[x]; n   } n   return ret; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> adj[n + 1]; n   for (int i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   vector<pair<long long, long long>> topic(n); n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> a; n     topic[i] = {a, i + 1}; n   } n   sort(topic.begin(), topic.end()); n   vector<long long> res; n   vector<set<long long>> around(n + 1); n   for (int i = 0; i < topic.size(); i++) { n     long long u = topic[i].second; n     res.push_back(u); n     if (around[u].find(topic[i].first) != around[u].end() || n         around[u].size() != topic[i].first - 1) { n       cout << -1 <<   n ; n       return; n     } n     for (auto j : adj[u]) { n       around[j].insert(topic[i].first); n     } n   } n   for (auto i : res) cout << i <<    ; n   cout <<   n ; n   ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n vector<long long> v[N]; n long long vis[N]; n long long col[N]; n set<long long> che; n void dfs(long long x) { n   vis[x] = 1; n   che.insert(x); n   for (auto it : v[x]) { n     if (!vis[it]) { n       dfs(it); n     } n   } n } n void solve() { n   long long n; n   cin >> n; n   long long i, x, y; n   vector<pair<long long, long long> > e, spe; n   for (i = 0; i < n - 1; i++) { n     cin >> x >> y; n     x--, y--; n     e.push_back(make_pair(x, y)); n   } n   for (i = 0; i < n; i++) { n     cin >> col[i]; n   } n   for (auto it : e) { n     if (col[it.first] != col[it.second]) { n       spe.push_back(make_pair(it.first, it.second)); n     } n   } n   long long m = spe.size(); n   long long f = 0, ind = 0; n   long long cnt[n]; n   memset(cnt, 0, sizeof(cnt)); n   if (m == 0) { n     cout <<  YES n1 ; n     return; n   } n   for (auto it : spe) { n     cnt[it.first]++; n     cnt[it.second]++; n   } n   for (i = 0; i < n; i++) { n     if (cnt[i] == m) { n       cout <<  YES n  << i + 1; n       return; n     } n   } n   cout <<  NO n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   int zD = 1; n   int zABC = 0; n   for (int i = 1; i <= n; i++) { n     int nzD = zABC * 3LL % 1000000007; n     int nzABC = (zABC * 2LL + zD) % 1000000007; n     zD = nzD; n     zABC = nzABC; n   } n   cout << zD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     auto chk = [&](int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }; n     vector<int> nxt(n * m, -1), deg(n * m), dp(n * m), dp2(n * m); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         char c; n         cin >> c; n         int x = i, y = j; n         if (c ==  L ) --y; n         if (c ==  R ) ++y; n         if (c ==  U ) --x; n         if (c ==  D ) ++x; n         if (chk(x, y)) { n           nxt[i * m + j] = x * m + y; n           deg[x * m + y] += 1; n         } n       } n     } n     for (int i = 0; i < n * m; ++i) dp2[i] = i; n     queue<int> q; n     for (int i = 0; i < n * m; ++i) n       if (deg[i] == 0) q.push(i); n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       if (nxt[u] != -1) { n         int v = nxt[u]; n         if (dp[v] < dp[u] + 1) { n           dp[v] = dp[u] + 1; n           dp2[v] = dp2[u]; n         } n         if (--deg[v] == 0) q.push(v); n       } n     } n     int ans = 0; n     for (int i = 0; i < n * m; ++i) { n       if (deg[i] == 0) continue; n       int p = nxt[i]; n       vector<int> loop; n       while (p != i) { n         loop.push_back(p); n         p = nxt[p]; n       } n       loop.push_back(i); n       for (int j : loop) dp[j] += loop.size() - 1, deg[j] = 0; n     } n     int mx = -1, x = -1, y = -1; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         if (dp[i * m + j] > mx) { n           mx = dp[i * m + j]; n           x = dp2[i * m + j] / m; n           y = dp2[i * m + j] % m; n         } n       } n     } n     cout << x + 1 <<     << y + 1 <<     << mx + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void addmod(int &a, long long b) { n   a = (a + b); n   if (a >= 1000000007) a -= 1000000007; n } n void mulmod(int &a, long long b) { a = (a * b) % 1000000007; } n int gi() { n   int a; n   scanf( %d , &a); n   return a; n } n long long gll() { n   long long a; n   scanf( %lld , &a); n   return a; n } n class rangesum { n   vector<long long> s; n  n  public: n   rangesum(const vector<int> &inp, const int &n) { n     s.resize(n + 1); n     for (int i = 0; i < n; i++) s[i + 1] = s[i] + inp[i]; n   } n   long long get(int x, int y) { return s[y + 1] - s[x]; } n }; n int main() { n   int n = gi(), l, u; n   vector<int> a(n); n   for (auto i = 0; i < n; i++) { n     a[i] = gi(); n   } n   rangesum rs(a, n); n   set<int> rm; n   multiset<long long, greater<long long>> v; n   rm.insert(-1); n   rm.insert(n); n   v.insert(rs.get(0, n - 1)); n   for (int i = 0; i < n; i++) { n     int ix = gi() - 1; n     auto it = rm.upper_bound(ix); n     u = (*it) - 1; n     it--; n     l = (*it) + 1; n     auto mit = v.find(rs.get(l, u)); n     assert(mit != v.end()); n     v.erase(mit); n     if (ix > l) v.insert(rs.get(l, ix - 1)); n     if (ix < u) v.insert(rs.get(ix + 1, u)); n     rm.insert(ix); n     printf( %lld n , (v.size() ? *v.begin() : 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long BIG = 1000000000000000000LL; n void solve(); n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n long long nbBoxes; n long long reqSom = 0; n bool cycor[1 << 15]; n pair<long long, pair<long long, long long> > cymem[1 << 15]; n long long dpGo[1 << 15]; n long long origSom[15]; n vector<long long> yolo[15]; n map<long long, long long> loc; n map<long long, long long> ds; n vector<long long> maskOk; n pair<long long, long long> finRes[15]; n long long osef[15]; n long long getMap(long long x) { n   auto it = loc.find(x); n   if (it == loc.end()) n     return -1; n   else n     return it->second; n } n void tester(long long dep, long long valEnvoi, long long suiv, n             bool act = false) { n   if (dep == suiv) { n     if (origSom[dep] == reqSom) { n       cycor[1LL << dep] = true; n       cymem[1LL << dep] = {dep, {valEnvoi, suiv}}; n       if (act) finRes[dep] = {dep, osef[dep]}; n     } n     return; n   } n   long long gauche = dep; n   long long cg = reqSom - (origSom[dep] - valEnvoi); n   long long mask = (1LL << gauche); n   if (act) finRes[dep] = {suiv, valEnvoi}; n   while (true) { n     long long prev = gauche; n     gauche = getMap(cg); n     if (gauche != -1 && act) finRes[gauche] = {prev, cg}; n     if (gauche == -1 || gauche == dep) break; n     if (mask & (1LL << gauche)) break; n     cg = reqSom - (origSom[gauche] - cg); n     mask |= (1LL << gauche); n   } n   if (gauche == dep && cg == valEnvoi) { n     cycor[mask] = true; n     cymem[mask] = {dep, {valEnvoi, suiv}}; n   } n } n void solve() { n   cin >> nbBoxes; n   for (long long i = (0); i < (nbBoxes); ++i) { n     long long ni; n     cin >> ni; n     for (long long i00 = (0); i00 < (ni); ++i00) { n       long long x; n       cin >> x; n       yolo[i].push_back(x); n       reqSom += x; n       origSom[i] += x; n       loc[x] = i; n       osef[i] = x; n     } n   } n   if (reqSom % nbBoxes != 0) { n     cout <<  No n ; n     return; n   } n   reqSom /= nbBoxes; n   for (long long i = (0); i < (nbBoxes); ++i) { n     for (long long x : yolo[i]) { n       tester(i, x, -1); n       tester(i, x, i); n     } n   } n   for (long long i = (0); i < ((1 << 15)); ++i) { n     dpGo[i] = -1; n   } n   dpGo[0] = 0; n   for (long long mask = 1; mask < (1 << 15); ++mask) { n     for (long long s = mask; s > 0; s = ((s - 1) & mask)) { n       if (cycor[s] && dpGo[mask ^ s] != -1) { n         dpGo[mask] = s; n         break; n       } n     } n   } n   long long cur = (1LL << nbBoxes) - 1; n   while (dpGo[cur] != 0) { n     if (dpGo[cur] == -1) { n       cout <<  No n ; n       return; n     } n     long long ret = dpGo[cur]; n     long long dep = cymem[ret].first, valEnv = cymem[ret].second.first, n               suiv = cymem[ret].second.second; n     tester(dep, valEnv, suiv, true); n     cur ^= ret; n   } n   cout <<  Yes n ; n   for (long long i = (0); i < (nbBoxes); ++i) n     cout << finRes[i].second <<     << finRes[i].first + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a[300010], sum[300010], ans; n int main() { n   scanf( %lld%lld , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; n   ans += k * sum[n]; n   sort(sum + 1, sum + n); n   for (int i = 1; i < k; i++) ans -= sum[i]; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using LD = long double; n using ULL = unsigned long long; n template <class T1, class T2> n using P = pair<T1, T2>; n using PII = P<int, int>; n using PLL = P<LL, LL>; n using PLD = P<LD, LD>; n template <class T> n using V = vector<T>; n using VI = V<int>; n using VLL = V<LL>; n using VLD = V<LD>; n using VB = V<bool>; n using VS = V<string>; n template <class T, class Comp = greater<T>> n using PQ = priority_queue<T, V<T>, Comp>; n template <class T> n int sz(const T& x) { n   return (int)size(x); n } n template <class T> n bool amin(T& a, const T& b) { n   if (b < a) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n bool amax(T& a, const T& b) { n   if (b > a) { n     a = b; n     return true; n   } n   return false; n } n const size_t rseed = n     std::chrono::high_resolution_clock::now().time_since_epoch().count(); n mt19937 rnd(rseed); n int randint(int lo, int hi) { n   return uniform_int_distribution<int>{lo, hi}(rnd); n } n int main() { n   int T; n   scanf( %d , &T); n   for (int t = (1); t <= (T); t++) { n     int n; n     scanf( %d , &n); n     string s(n + 1,    ); n     scanf( %s , &s[0]); n     string a(n,    ), b(n,    ); n     bool f = false; n     for (int i = (0); i <= (n - 1); i++) { n       if (f) { n         a[i] =  0 ; n         b[i] = s[i]; n       } else { n         if (s[i] ==  1 ) { n           f = true; n           a[i] =  1 ; n           b[i] =  0 ; n         } else if (s[i] ==  2 ) n           a[i] = b[i] =  1 ; n         else n           a[i] = b[i] =  0 ; n       } n     } n     printf( %s n%s n , a.c_str(), b.c_str()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int seq[100]; n int main() { n   int n; n   int acc, t, pen; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , seq + i); n   } n   sort(seq, seq + n); n   acc = 10; n   t = 0; n   pen = 0; n   for (int i = 0; i < n; i++) { n     acc += seq[i]; n     if (acc > 720) { n       break; n     } n     t++; n     if (acc > 360) { n       pen += ((acc - 360) < 0 ? -(acc - 360) : (acc - 360)); n     } n   } n   printf( %d %d n , t, pen); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long maxx = 1e5 + 20, sqr = 150, mod = 1e9 + 7; n long long len, n, dp[maxx][sqr][2], q, pw25[maxx], pw26[maxx], pwmod[maxx], n     fac[maxx], tmp[maxx]; n string s; n long long pw(long long a, long long b) { n   if (!b) return 1ll; n   long long x = pw(a, b / 2); n   x = (x * x) % mod; n   if (b % 2) x = (x * a) % mod; n   return x; n } n long long sel(long long a, long long b) { n   long long x = 1ll; n   x = (x * fac[a]) % mod; n   x = (x * pwmod[b]) % mod; n   x = (x * pwmod[a - b]) % mod; n   return x; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   pw25[0] = pw26[0] = 1; n   for (long long i = 1; i < maxx; i++) n     pw25[i] = (pw25[i - 1] * 25) % mod, pw26[i] = (pw26[i - 1] * 26) % mod; n   fac[0] = 1; n   for (long long i = 1; i < maxx; i++) fac[i] = (fac[i - 1] * i) % mod; n   for (long long i = 0; i < maxx; i++) pwmod[i] = pw(fac[i], mod - 2); n   for (long long j = 0; j < sqr; j++) { n     dp[j][j][1] = dp[j][j][0] = 1; n     for (long long i = j + 1; i < maxx; i++) { n       dp[i][j][0] = (((dp[i - 1][j][0] * 26) % mod) + n                      (j >= 1 ? dp[i - 1][j - 1][1] : 0)) % n                     mod; n       dp[i][j][1] = (((dp[i - 1][j][1] * 25) % mod) + n                      (j >= 1 ? dp[i - 1][j - 1][1] : 0)) % n                     mod; n     } n   } n   long long c = 0; n   cin >> n >> s; n   len = s.length(); n   if (len >= sqr) { n     fill(tmp, tmp + maxx, 0); n     tmp[len - 1] = 1; n     for (long long j = len; j < maxx; j++) n       tmp[j] = (((tmp[j - 1] * 26ll) % mod) + n                 (((long long)sel(j, len - 1) * pw25[j + 1 - len]) % mod)) % n                mod; n   } n   for (long long i = 0; i < n; i++) { n     long long type; n     cin >> type; n     if (type == 1) { n       cin >> s, len = s.length(); n       if (len >= sqr) { n         fill(tmp, tmp + maxx, 0); n         tmp[len - 1] = 1; n         for (long long j = len; j < maxx; j++) n           tmp[j] = (((tmp[j - 1] * 26ll) % mod) + n                     (((long long)sel(j, len - 1) * pw25[j + 1 - len]) % mod)) % n                    mod; n       } n     } else { n       c++; n       cin >> q; n       if (len < sqr) { n         if (len > q) n           cout << 0 <<   n ; n         else n           cout << dp[q][len][0] <<   n ; n         continue; n       } n       long long ans = 0ll; n       cout << tmp[q - 1] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<array<int, 2>> tree(1); n int terminal[110]; n unsigned long long inf = (1ull << 63) - 1; n int s; n struct Mat { n   vector<unsigned long long> data; n   Mat() { data.assign(s * s, inf); } n }; n Mat operator*(const Mat& a, const Mat& b) { n   Mat r; n   for (int i = 0; i < s; i++) { n     for (int k = 0; k < s; k++) { n       for (int j = 0; j < s; j++) { n         r.data[i * s + j] = n             min(r.data[i * s + j], a.data[i * s + k] + b.data[k * s + j]); n       } n     } n   } n   return r; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int g, n, m; n   cin >> g >> n >> m; n   vector<pair<int, vector<int>>> mut; n   vector<vector<vector<int>>> node(g); n   for (int i = 0; i < n; i++) { n     int a, k; n     cin >> a >> k; n     node[a].push_back(vector<int>(k)); n     mut.emplace_back(a, vector<int>()); n     for (int& v : node[a].back()) { n       cin >> v; n       mut.back().second.push_back(v); n     } n   } n   for (int i = 0; i < m; i++) { n     int k; n     cin >> k; n     int p = 0; n     while (k--) { n       int c; n       cin >> c; n       if (!tree[p][c]) { n         tree[p][c] = tree.size(); n         tree.push_back({0, 0}); n       } n       p = tree[p][c]; n     } n     terminal[p] = 1; n   } n   { n     vector<int> link(tree.size()); n     queue<int> q; n     q.push(0); n     while (q.size()) { n       int p = q.front(); n       q.pop(); n       int par = link[p]; n       terminal[p] |= terminal[par]; n       for (int c : {0, 1}) { n         if (!tree[p][c]) { n           tree[p][c] = tree[par][c]; n         } else { n           link[tree[p][c]] = p ? tree[par][c] : 0; n           q.push(tree[p][c]); n         } n       } n     } n   } n   s = tree.size(); n   for (int p = 0; p < s; p++) n     if (terminal[p]) tree[p][0] = tree[p][1] = p; n   vector<Mat> mat(g); n   for (int c : {0, 1}) { n     for (int i = 0; i < s; i++) { n       mat[c].data[i * s + tree[i][c]] = 1; n     } n   } n   vector<int> changed(g, 1); n   while (1) { n     int found = 0; n     vector<int> nchanged(g); n     for (auto& [a, v] : mut) { n       int need = 0; n       for (int b : v) n         if (changed[b]) need = 1; n       if (!need) continue; n       Mat prod = mat[v[0]]; n       for (int i = 1; i < (int)v.size(); i++) { n         prod = prod * mat[v[i]]; n       } n       for (int i = 0; i < s * s; i++) { n         if (prod.data[i] < mat[a].data[i]) { n           mat[a].data[i] = prod.data[i]; n           nchanged[a] = 1; n           found = 1; n         } n       } n     } n     changed = nchanged; n     if (!found) break; n   } n   for (int a = 2; a < g; a++) { n     unsigned long long mi = inf; n     for (int i = 0; i < s; i++) { n       if (!terminal[i]) mi = min(mi, mat[a].data[i]); n     } n     if (mi < inf) n       cout <<  NO   << mi << endl; n     else n       cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize  -O3  n using namespace std; n const int MAXN = 310000; n int n; n int a[MAXN]; n vector<int> pr; n int bb[MAXN]; n int go[MAXN]; n int cc[MAXN]; n int ans[MAXN]; n vector<int> fc[MAXN]; n int gcd(int a, int b) { n   while (b) { n     int q = a % b; n     a = b; n     b = q; n   } n   return a; n } n int dp[1 << 7]; n int gg; n int gog[1 << 7]; n void run(int x, int msk, int ml, int k) { n   if (x == k) { n     dp[msk] = cc[ml]; n     gog[msk] = ml; n     return; n   } n   run(x + 1, msk, ml, k); n   run(x + 1, msk + (1 << x), ml * fc[gg][x], k); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   for (int i = 0; i < MAXN; ++i) bb[i] = i; n   for (int i = 2; i < MAXN; ++i) { n     if (bb[i] == i) pr.push_back(i); n     for (int j : pr) { n       if (j * i >= MAXN || bb[i] < j) break; n       bb[j * i] = j; n     } n   } n   for (int i = 1; i < MAXN; ++i) { n     int cur = 1; n     int x = i; n     while (x != 1) { n       int d = bb[x]; n       while (x % d == 0) x /= d; n       cur *= d; n       fc[i].push_back(d); n     } n     go[i] = cur; n   } n   cin >> n; n   int now = 0; n   fill(ans, ans + MAXN, 1000); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     now = gcd(now, a[i]); n     a[i] = go[a[i]]; n     ++cc[a[i]]; n     ans[a[i]] = 1; n   } n   if (now != 1) { n     cout << -1 <<   n ; n     return 0; n   } n   for (int i = 1; i < MAXN; ++i) { n     for (int j = 2 * i; j < MAXN; j += i) cc[i] += cc[j]; n   } n   for (int i = MAXN - 1; i > 1; --i) { n     if (go[i] != i) continue; n     int k = fc[i].size(); n     gg = i; n     run(0, 0, 1, k); n     for (int i = (1 << k) - 1; i > 0; --i) { n       for (int j = (i - 1) & i; j > 0; j = (j - 1) & i) dp[j] -= dp[i]; n       dp[0] -= dp[i]; n     } n     for (int j = 0; j < (1 << k); ++j) n       if (dp[j]) ans[gog[j]] = min(ans[gog[j]], ans[i] + 1); n   } n   cout << ans[1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, m; n priority_queue<int> s; n int main() { n   scanf( %d , &n); n   m = n; n   for (int i = 0; i < int(n); i++) { n     scanf( %d , &x); n     if (x < m) { n       s.push(x); n       cout <<    ; n     } else if (x == m) { n       cout << x <<    ; n       m--; n       while (!s.empty() && m == s.top()) { n         cout << s.top() <<    ; n         s.pop(); n         m--; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[12000]; n int n, i, j; n vector<int> ans; n int cek(int v, int t, string e) { n   if (v >= strlen(s) - 1) return v; n   int i = v; n   do { n     string tmp =   ; n     for (i = i;; i++) { n       tmp.push_back(s[i]); n       if (s[i] ==  > ) break; n     } n     i++; n     if (e ==  <table>  && tmp ==  </table> ) break; n     if (e ==  <tr>  && tmp ==  </tr> ) break; n     if (e ==  <td>  && tmp ==  </td> ) { n       ans[t]++; n       break; n     } n     if (tmp ==  <table> ) { n       ans.push_back(0); n       i = cek(i, ans.size() - 1, tmp); n     } else if (tmp ==  <tr> ) { n       i = cek(i, t, tmp); n     } else if (tmp ==  <td> ) { n       i = cek(i, t, tmp); n     } n   } while (i < strlen(s)); n   return i; n } n int main() { n   n = 0; n   bool f = 1; n   while (scanf( %c , &s[n]) != EOF) { n     if (s[n] !=   n ) n++; n   } n   int k = cek(0, -1,    ); n   sort(ans.begin(), ans.end()); n   for (i = 0; i < ans.size(); i++) { n     if (f) n       f = 0; n     else n       printf(   ); n     cout << ans[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, k; n int a[333333]; n long long ans; n map<int, int> mp, mpb; n int main() { n   scanf( %d%d%d , &n, &p, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     mp[a[i]]++; n   } n   for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { n     if (1ll * ((it->first) + (it->first)) * n             (2ll * (it->first) * (it->first) % p) % p != n         k) n       ans -= (1ll * (it->second) * ((it->second) - 1)); n   } n   for (int i = 1; i <= n; i++) { n     mpb[((1ll * a[i] * a[i] % p * a[i] % p * a[i] - 1ll * k * a[i]) % p + p) % n         p]++; n   } n   for (map<int, int>::iterator it = mpb.begin(); it != mpb.end(); it++) { n     ans += (1ll * (it->second) * ((it->second) - 1)); n   } n   printf( %lld n , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n long long fac[1000006]; n inline long long exp(long long x, long long n) { n   long long r = 1; n   x = x; n   while (n) { n     if (n % 2) r = (r * x) % 10000000000000007; n     x = (x * x) % 10000000000000007; n     n = n / 2; n   } n   return r; n } n inline long long mmi(long long a) { n   return exp(a, 10000000000000007 - 2) % 10000000000000007; n } n inline long long fact(long long n) { n   long long res = 1; n   for (long long i = 1; i < (n + 1); ++i) { n     res = (res * i) % 10000000000000007; n   } n   return res; n } n inline void fact_a() { n   fac[0] = 1; n   fac[1] = 1; n   for (long long i = 1; i < (105); ++i) { n     fac[i] = (fac[i - 1] * i) % 10000000000000007; n   } n } n inline long long inv_fact(long long n) { n   long long par = fac[n]; n   long long res = mmi(par); n   return res; n } n inline long long comb(long long n, long long r) { n   if (n == r && r == 0) return 1; n   if (n < r) return 0; n   return ((fac[n] * inv_fact(r)) % 10000000000000007 * inv_fact(n - r)) % n          10000000000000007; n } n struct triplet { n   long long a, b, c; n }; n bool operator<(const triplet &t1, const triplet &t2) { n   if (t1.a < t2.a) return true; n   if (t1.a == t2.a && t1.b < t2.b) return true; n   if (t1.a == t2.a && t1.b == t2.b && t1.c < t2.c) return true; n   return false; n } n pair<long long, pair<long long, long long> > ex_gcd(long long a, long long b) { n   if (b == 0) { n     return make_pair(a, make_pair(1, 0)); n   } n   pair<long long, pair<long long, long long> > p = ex_gcd(b, a % b); n   long long gcd = p.first; n   long long x1 = p.second.first; n   long long y1 = p.second.second; n   long long x = y1; n   long long y = x1 - (a / b) * y1; n   return make_pair(gcd, make_pair(x, y)); n } n long long prime[3000006]; n long long spf_prime[3000006]; n void sieve() { n   prime[1] = 1; n   for (long long i = 2; i * i <= 3000000; i++) n     if (prime[i] == 0) n       for (long long j = i * i; j <= 3000000; j += i) prime[j] = 1; n } n void spf() { n   for (long long i = 2; i * i <= 3000000; i++) n     if (!spf_prime[i]) n       for (long long j = i * i; j <= 3000000; j += i) n         if (!spf_prime[j]) spf_prime[j] = i; n   for (long long i = 2; i <= 3000000; i++) n     if (!spf_prime[i]) spf_prime[i] = i; n } n long long getparent_BIT(long long idx) { return idx - (idx & -idx); } n long long getnext_BIT(long long idx) { return idx + (idx & -idx); } n long long getsum_BIT(long long idx, long long BIT[], long long n) { n   long long sum = 0; n   while (idx > 0) { n     sum += BIT[idx]; n     idx = getparent_BIT(idx); n   } n   return sum; n } n void update_BIT(long long idx, long long BIT[], long long val, long long n) { n   while (idx <= n) { n     BIT[idx] += val; n     idx = getnext_BIT(idx); n   } n } n void build_BIT(long long BIT[], long long a[], long long n) { n   for (long long i = 0; i < (n); ++i) { n     update_BIT(i, BIT, a[i], n); n   } n } n void comp_lps(string s, long long lps[], long long n) { n   long long i = 1, j = 0; n   while (i < n) { n     if (s[i] == s[j]) { n       lps[i++] = ++j; n     } else { n       if (j != 0) n         j = lps[j - 1]; n       else n         lps[i++] = 0; n     } n   } n } n void dfs_l(long long u, vector<vector<long long> > &adj, long long status[]) { n   if (status[u] == 1) return; n   status[u] = 1; n   for (long long i = 0; i < (adj[u].size()); ++i) dfs_l(adj[u][i], adj, status); n } n long long dij(long long u, long long n, n               vector<vector<pair<long long, long long> > > &adj, n               long long status[]) { n   long long dist[n]; n   for (long long i = 0; i < (n); ++i) dist[i] = 1000000000; n   dist[0] = 0; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       q; n   q.push(make_pair(0, 0)); n   while (!q.empty()) { n     pair<long long, long long> p; n     p = q.top(); n     q.pop(); n     long long x = p.second; n     long long w = p.first; n     if (status[x] == 1) continue; n     status[x] = 1; n     for (long long i = 0; i < (adj[x].size()); ++i) { n       long long ww = adj[x][i].first; n       long long y = adj[x][i].second; n       if (dist[x] + ww < dist[y]) { n         dist[y] = dist[x] + ww; n         q.push(make_pair(dist[y], y)); n       } n     } n   } n   if (dist[n - 1] >= 1000000000) return -1; n   return dist[n - 1]; n } n long long phi(long long n) { n   vector<long long> v; n   long long k = n; n   ; n   for (long long i = 2; i * i <= n; i++) { n     if (k % i == 0) { n       v.push_back(i); n       while (k % i == 0) k /= i; n     } n   } n   if (k > 1) v.push_back(k); n   long long ans = n; n   for (long long i = 0; i < (v.size()); ++i) ans -= (ans / v[i]); n   return ans; n } n struct trie { n   struct trie *left, *right; n   bool isend; n }; n trie *get_trie() { n   trie *ptr = new trie; n   ptr->left = NULL; n   ptr->right = NULL; n   ptr->isend = false; n   return ptr; n } n bool isempty(trie *root) { n   if (root->left == NULL && root->right == NULL) return true; n   return false; n } n void insert_trie(trie *root, string key) { n   trie *ptr = root; n   for (long long i = 0; i < (key.size()); ++i) { n     if (key[i] ==  0 ) { n       if (ptr->left == NULL) { n         ptr->left = get_trie(); n       } n       ptr = ptr->left; n     } else { n       if (ptr->right == NULL) ptr->right = get_trie(); n       ptr = ptr->right; n     } n   } n   ptr->isend = true; n } n trie *remove_trie(trie *root, string key, long long idx = 0) { n   if (root == NULL) return NULL; n   if (idx == key.size()) { n     if (isempty(root)) { n       delete (root); n       root = NULL; n     } n     return root; n   } n   if (key[idx] ==  0 ) n     root->left = remove_trie(root->left, key, idx + 1); n   else n     root->right = remove_trie(root->right, key, idx + 1); n   if (isempty(root)) { n     delete (root); n     root = NULL; n   } n   return root; n } n long long search_trie(trie *root, string key) { n   trie *ptr = root; n   string s =   ; n   for (long long i = 0; i < (key.size()); ++i) { n     if (key[i] ==  0 ) { n       if (ptr->right != NULL) { n         ptr = ptr->right; n         s +=  1 ; n       } else { n         ptr = ptr->left; n         s +=  0 ; n       } n     } else { n       if (ptr->left != NULL) { n         ptr = ptr->left; n         s +=  1 ; n       } else { n         ptr = ptr->right; n         s +=  0 ; n       } n     } n   } n   long long res = 0; n   reverse(s.begin(), s.end()); n   for (long long i = s.length() - 1; i >= (0); --i) n     res = res * 2 + (s[i] -  0 ); n   return res; n } n struct Comp { n   bool operator()(const std::pair<int, int> &a, const std::pair<int, int> &b) { n     if (a.first != b.first) { n       return a.first < b.first; n     } n     return a.second > b.second; n   } n }; n void dearr() { n   long long dar[1005]; n   dar[0] = 1; n   dar[1] = 0; n   dar[2] = 1; n   for (long long i = 3; i < (1002); ++i) { n     dar[i] = (i - 1) * (dar[i - 1] + dar[i - 2]); n   } n } n int main() { n   long long n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  white  n          <<   n ; n     cout << 1 <<     << 2 <<   n ; n   } else n     cout <<  black  n          <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i % 2 == 0) { n         if (j % 2 == 0) { n           cout <<  W ; n         } else { n           cout <<  B ; n         } n       } else { n         if (j % 2 != 0) { n           cout <<  W ; n         } else { n           cout <<  B ; n         } n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int first, second; n   cin >> second >> first; n   if ((second == 1 && first == 0) || first == 0) { n     cout <<  NO ; n     return 0; n   } else if (first == 1 && second == 0) { n     cout <<  YES ; n     return 0; n   } n   int tot = first; n   int co = first - 1; n   if (first == 1 && second >= 0) { n     cout <<  NO ; n     return 0; n   } n   if ((second - co) % 2 == 0 && (second - co) >= 0) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, k, lb, ub, ans = -1; n int main() { n   long long l, r; n   scanf( %lld%lld%lld%lld , &n, &l, &r, &k); n   x = (r >= l ? r - l + 1 : r + n - l + 1) % n; n   for (long long i = k, j, r; i; i = j) { n     j = k / (k / i + 1); n     r = k / i; n     ub = min(2 * n, i); n     lb = max(n, j + 1); n     ub = min(ub, min((2 * n + k - 2 * x + 1) / (r + 1), (k - x) / r)); n     lb = max(lb, (k - 2 * x + r - 1) / r); n     if (ub >= lb) ans = max(ans, ub); n   } n   if (x <= k && k <= x * 2) ans = max(ans, n + min(k - x, x - 1) + n - x + 1); n   if (!x && k == n) ans = max(ans, n + 1); n   printf( %lld n , ~ans ? ans - n : ans); n } n


#include <bits/stdc++.h> n using namespace std; n char p[][20] = { vaporeon ,  jolteon ,  flareon ,  espeon , n                  umbreon ,   leafeon ,  glaceon ,  sylveon }; n char s[20]; n int main() { n   int n, k, i; n   scanf( %d , &n); n   scanf( %s , s); n   for (k = 0; k < 8; k++) { n     int fl = 1; n     for (i = 0; fl && i < n; i++) { n       if (p[k][i] ==   0  || (p[k][i] != s[i] && s[i] !=  . )) fl = 0; n     } n     if (fl && p[k][i] ==   0 ) printf( %s n , p[k]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double epsilon = 1e-9; n int main() { n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   vector<pair<int, int> > moves; n   for (int i = 0; i < n; i++) { n     if (b[i] != a[i]) { n       for (int j = i + 1; j < n; j++) { n         if (b[j] == a[i]) { n           for (int k = j; k > i; k--) { n             moves.push_back(make_pair(k - 1, k)); n             swap(b[k], b[k - 1]); n           } n           break; n         } n       } n     } n   } n   cout << moves.size() << endl; n   for (int i = 0; i < moves.size(); i++) n     cout << moves[i].first + 1 <<     << moves[i].second + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long a, b, c, d, e, first; n   cin >> a >> b >> c >> d >> e >> first; n   b = min(c, b); n   if (d < a && d < b) n     cout << d * max(e, first); n   else { n     long long p = min(a, d) * e + min(d - min(d, a), b) * first; n     long long q = min(b, d) * first + min(d - min(d, b), a) * e; n     cout << max(p, q); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   for (long long i = 0; i < n - 1; i++) { n     if (s[i] > s[i + 1]) { n       cout <<  YES  << endl; n       cout << i + 1 <<     << i + 2 << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 21; n int a[1 << maxn], n; n long long ans[maxn][2]; n void mergesort(int l, int r, int dep) { n   if (r - l <= 1) return; n   int m = (l + r) / 2; n   mergesort(l, m, dep - 1); n   mergesort(m, r, dep - 1); n   for (int i = l; i < m; i++) { n     ans[dep][0] += lower_bound(a + m, a + r, a[i]) - a - m; n   } n   for (int i = m; i < r; i++) { n     ans[dep][1] += lower_bound(a + l, a + m, a[i]) - a - l; n   } n   inplace_merge(a + l, a + m, a + r); n } n long long solve(int q) { n   long long res = 0; n   while (q) swap(ans[q][0], ans[q][1]), q--; n   for (int i = 1; i <= n; i++) { n     res += ans[i][0]; n   } n   return res; n } n int main() { n   int m, q; n   cin >> n; n   for (int i = 0; i < 1 << n; i++) scanf( %d , &a[i]); n   cin >> m; n   mergesort(0, 1 << n, n); n   for (int i = 0; i < m; i++) { n     scanf( %d , &q); n     cout << solve(q) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1.0); n const int INF = 0x3f3f3f3f; n const long long inf = (((long long)1) << 61) + 5; n const int N = 100005; n int f[N]; n int g[N]; n int a[N]; n int bit[N]; n int ans[N]; n int sum1(int x) { n   int ans = 0; n   for (int i = x; i > 0; i -= i & -i) ans = max(bit[i], ans); n   return ans; n } n void add1(int x, int val) { n   for (int i = x; i <= N; i += i & -i) bit[i] = max(bit[i], val); n } n int sum2(int x) { n   int ans = 0; n   for (int i = x; i <= N; i += i & -i) ans = max(bit[i], ans); n   return ans; n } n void add2(int x, int val) { n   for (int i = x; i > 0; i -= i & -i) bit[i] = max(bit[i], val); n } n int main() { n   int n, res = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     int x = sum1(a[i] - 1); n     f[i] = x + 1; n     if (f[i] > res) res = f[i]; n     add1(a[i], f[i]); n   } n   memset(bit, 0, sizeof bit); n   for (int i = n; i >= 1; i--) { n     int x = sum2(a[i] + 1); n     g[i] = x + 1; n     add2(a[i], g[i]); n   } n   vector<int> q[N]; n   for (int i = 1; i <= n; i++) { n     if (f[i] + g[i] - 1 < res) n       ans[i] = 1; n     else n       q[f[i]].push_back(i); n   } n   for (int i = 1; i <= res; i++) { n     int sz = q[i].size(); n     for (int j = 0; j < sz; j++) { n       int v = q[i][j]; n       if (sz == 1) n         ans[v] = 3; n       else n         ans[v] = 2; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n template <typename T> n inline void read(T &x) { n   x = 0; n   T fl = 1; n   char ch = 0; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) fl = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     x = (x << 1) + (x << 3) + (ch ^ 48); n   x *= fl; n } n template <typename T, typename... Args> n inline void read(T &x, Args &...args) { n   read(x); n   read(args...); n } n const int N = 1e5 + 5; n struct data { n   int s, m, r, t; n   data(int x = 0) { t = x; } n   bool operator<(const data &b) const { return t < b.t; } n   long long calc(long long tim) { return min(s + tim * r, m * 1ll); } n } a[18][N]; n struct seg { n   int trt[N << 2]; n   long long trm[18][N << 2], trr[18][N << 2]; n   void pushdown(int nod) { n     if (trt[nod] >= 0) trt[(nod << 1)] = trt[(nod << 1 | 1)] = trt[nod]; n   } n   void pushup(int nod) { n     if (trt[(nod << 1)] != trt[(nod << 1 | 1)]) n       trt[nod] = -1; n     else n       trt[nod] = trt[(nod << 1)]; n   } n   void build(int nod, int l, int r, int dep) { n     trt[nod] = -2; n     if (l == r) { n       read(a[dep][l].s, a[dep][l].m, a[dep][l].r); n       if (a[dep][l].r == 0) n         a[dep][l].t = inf; n       else n         a[dep][l].t = a[dep][l].m % a[dep][l].r == 0 n                           ? a[dep][l].m / a[dep][l].r n                           : a[dep][l].m / a[dep][l].r + 1; n       trm[dep][l] = trm[dep][l - 1] + a[dep][l].m; n       trr[dep][l] = trr[dep][l - 1] + a[dep][l].r; n       return; n     } n     int mid = (l + r) >> 1; n     build((nod << 1), l, mid, dep + 1); n     build((nod << 1 | 1), mid + 1, r, dep + 1); n     for (int i = (l); i <= (r); i++) a[dep][i] = a[dep + 1][i]; n     sort(a[dep] + l, a[dep] + r + 1); n     for (int i = (l); i <= (r); i++) { n       trm[dep][i] = trm[dep][i - 1] + a[dep][i].m; n       trr[dep][i] = trr[dep][i - 1] + a[dep][i].r; n     } n   } n   long long query(int tim, int nod, int l, int r, int ql, int qr, int dep) { n     long long res = 0; n     if (ql <= l && r <= qr && trt[nod] != -1) { n       if (trt[nod] == -2) n         for (int i = (l); i <= (r); i++) res += a[dep][i].calc(tim); n       else { n         int i = upper_bound(a[dep] + l, a[dep] + 1 + r, data(tim - trt[nod])) - n                 a[dep] - 1; n         res = (tim - trt[nod]) * (trr[dep][r] - trr[dep][i]) + trm[dep][i] - n               trm[dep][l - 1]; n       } n       trt[nod] = tim; n       return res; n     } n     pushdown(nod); n     int mid = (l + r) >> 1; n     if (ql <= mid) res += query(tim, (nod << 1), l, mid, ql, qr, dep + 1); n     if (qr > mid) n       res += query(tim, (nod << 1 | 1), mid + 1, r, ql, qr, dep + 1); n     pushup(nod); n     return res; n   } n } sgt; n int n, m; n int main() { n   read(n); n   sgt.build(1, 1, n, 0); n   read(m); n   for (int _ = (1); _ <= (m); _++) { n     int t, l, r; n     read(t); n     read(l); n     read(r); n     printf( %lld n , sgt.query(t, 1, 1, n, l, r, 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, i, j, n, aux, cnt, ans, div1[10000010], v[10000010]; n int main() { n   cin >> a >> n; n   v[1] = 1; n   for (i = 3; i * i <= a + n - 1; i += 2) n     if (!div1[i]) { n       div1[i] = i; n       for (j = i * i; j <= a + n - 1; j += 2 * i) div1[j] = i; n     } n   for (i = 2; i <= a + n - 1; i += 2) div1[i] = 2; n   for (i = 2; i <= a + n - 1; i++) { n     aux = i; n     cnt = 0; n     if (!div1[i]) div1[i] = i; n     while (aux % div1[i] == 0) aux /= div1[i], cnt++; n     v[i] = v[aux]; n     if (cnt & 1) v[i] *= div1[i]; n   } n   for (i = a; i <= a + n - 1; i++) ans += v[i]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n vector<long long> v; n unordered_map<long long, long long> m; n long long falla(long long n) { n   if (m.count(n)) { n     return m[n]; n   } n   long long ans; n   long long k = 0; n   for (int j = n - 1; j >= 1; j--) { n     if (n % j == 0) { n       ans = j; n       break; n     } n     if (n % (n - j + 1) == 0) { n       ans = n / (n - j + 1); n       break; n     } n   } n   m[ans] = ans; n   return ans; n } n int main() { n   long long casos; n   cin >> casos; n   long long num = pow(2, 25) - 1; n   for (int i = 1; i < 30; i++) { n     v.push_back((1 << i) - 1); n   } n   long long flag = 0; n   while (casos--) { n     long long num; n     long long ans1; n     cin >> num; n     if (m.count(num)) { n       cout << m[num] << endl; n       flag = 1; n     } n     if (flag == 0) { n       ans1 = lower_bound((v).begin(), (v).end(), num) - v.begin(); n       if (num == v[ans1]) { n         ans1 = falla(num); n         m[num] = ans1; n         cout << ans1 << endl; n       } else { n         m[num] = v[ans1]; n         cout << v[ans1] << endl; n       } n     } n     flag = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[200005][2]; n bool vst[200005][2]; n long long A[200005], n; n long long go(long long v, bool b) { n   if (vst[v][b]) return dp[v][b]; n   vst[v][b] = true; n   long long val = 0; n   if (!b) { n     if ((v - A[v]) > 1) n       val = go(v - A[v], b ^ 1); n     else if ((v - A[v]) == 1) n       val = -1; n   } else { n     if ((v + A[v]) <= n) val = go(v + A[v], b ^ 1); n   } n   if (val != -1) n     dp[v][b] = A[v] + val; n   else n     dp[v][b] = -1; n   return dp[v][b]; n } n int main() { n   long long i; n   cin >> n; n   for (i = (2); i <= (n); i++) { n     cin >> A[i]; n   } n   memset((vst), (0), sizeof(vst)); n   long long j; n   memset((dp), (-1), sizeof(dp)); n   for (i = (2); i <= (n); i++) { n     go(i, 0); n     if (dp[i][0] != -1) n       cout << dp[i][0] + i - 1 << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const double Pi = 3.1415926535897932; n const int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; n const int maxn = 1e4 + 5; n struct EDGE { n   int a, b; n   int A, B; n } edge[maxn]; n int n, m; n int last[maxn]; n vector<int> G[maxn], tree[maxn]; n int d[maxn]; n int A[maxn], B[maxn]; n bool vis[maxn]; n vector<int> ans; n int in[maxn]; n void dfs_tree(int a, int fa) { n   vis[a] = true; n   for (int i = 0; i < G[a].size(); i++) { n     int p = G[a][i]; n     if (p == last[a]) continue; n     int b = edge[p].a ^ edge[p].b ^ a; n     if (!vis[b]) { n       tree[a].push_back(p); n       in[p] = true; n       d[b] = d[a] + 1; n       last[b] = p; n       dfs_tree(b, a); n     } else if (d[a] > d[b]) { n       if (d[a] - d[b] + 1 & 1) { n         ans.push_back(p); n         B[a]++; n         B[b]--; n       } else { n         A[a]++; n         A[b]--; n       } n     } n   } n } n void dfs(int a) { n   vis[a] = true; n   for (int i = 0; i < tree[a].size(); i++) { n     int p = tree[a][i]; n     int b = edge[p].a ^ edge[p].b ^ a; n     if (!vis[b]) dfs(b); n     A[a] += A[b]; n     B[a] += B[b]; n   } n   edge[last[a]].A = A[a]; n   edge[last[a]].B = B[a]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     edge[i] = EDGE{a, b}; n     G[a].push_back(i); n     G[b].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) dfs_tree(i, -1); n   } n   memset(vis, false, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) dfs(i); n   } n   if (ans.size() == 0) { n     for (int i = 1; i <= m; i++) ans.push_back(i); n   } else if (ans.size() == 1) { n     for (int i = 1; i <= m; i++) n       if (in[i]) { n         if (edge[i].A == 0 && edge[i].B) ans.push_back(i); n       } n   } else { n     int t = ans.size(); n     ans.clear(); n     for (int i = 1; i <= m; i++) n       if (in[i]) { n         if (edge[i].B) { n           if (edge[i].B == t && edge[i].A == 0) ans.push_back(i); n         } n       } n   } n   sort(ans.begin(), ans.end()); n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) n     printf( %d , ans[i]), putchar(i == ans.size() - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[200]; n int n; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].second; n     long long tmp = a[i].second; n     while (tmp % 3 == 0) { n       a[i].first--; n       tmp /= 3; n     } n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) cout << a[i].second <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long arr[3]; n   scanf( %I64d%I64d%I64d , &arr[0], &arr[1], &arr[2]); n   sort(arr, arr + 3); n   if (arr[0] == arr[1] && arr[1] == arr[2]) n     printf( 0 n ); n   else if (arr[2] == arr[1] && arr[1] > arr[0]) { n     printf( %I64d n , arr[2] - arr[0] - 1); n   } else if (arr[2] > arr[1] && arr[1] == arr[0]) { n     printf( %I64d n , (arr[2] - arr[1] - 1) + (arr[2] - arr[0] - 1)); n   } else if (arr[2] > arr[1] && arr[1] > arr[0]) { n     printf( %I64d n , (arr[2] - arr[1] - 1) + (arr[2] - arr[0] - 1)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long const modul = 1; n long long mod_sum(long long x, long long y) { return (x + y) % modul; } n long long mod_sub(long long x, long long y) { n   if (x > y) { n     return (x - y) % modul; n   } else { n     return (modul + x - y) % modul; n   } n } n long long mod_mpl(long long x, long long y) { return (x * y) % modul; } n void set_precise(int n) { n   cout << fixed; n   cout << setprecision(n); n } n int const N = 200000; n class Tree { n  private: n   long long tree[N * 4]; n   int start; n   int size; n   void rebuild(int id) { n     if (id < 1) { n       return; n     } n     tree[id] = tree[id * 2] + tree[id * 2 + 1]; n     rebuild(id / 2); n   }; n   long long count_sum(int cur, int left, int right, int cur_l, int cur_r) { n     int mid = (cur_l + cur_r) / 2; n     if ((left == cur_l) && (right == cur_r)) { n       return tree[cur]; n     } n     if (right <= mid) { n       return count_sum(cur * 2, left, right, cur_l, mid); n     } else if (left >= mid + 1) { n       return count_sum(cur * 2 + 1, left, right, mid + 1, cur_r); n     } else { n       return count_sum(cur * 2, left, mid, cur_l, mid) + n              count_sum(cur * 2 + 1, mid + 1, right, mid + 1, cur_r); n     } n   }; n  n  public: n   Tree() { n     size = 0; n     start = 0; n   }; n   void initialize(int n) { n     int cur = 1; n     int sum = 0; n     while (cur < n) { n       sum += cur; n       cur *= 2; n     } n     start = sum + 1; n     size = cur; n     for (int i = 1; i < start + cur; ++i) { n       tree[i] = 0; n     } n   } n   Tree(int n) { n     int cur = 1; n     int sum = 0; n     while (cur < n) { n       sum += cur; n       cur *= 2; n     } n     start = sum + 1; n     size = cur; n     for (int i = 1; i < start + cur; ++i) { n       tree[i] = 0; n     } n   }; n   void update(int pos, long long q, long long no_more) { n     long long cur = tree[start + pos]; n     cur += q; n     if (cur > no_more) { n       cur = no_more; n     } n     tree[start + pos] = cur; n     rebuild((start + pos) / 2); n   }; n   long long get_sum(int left, int right) { n     if (left > right) { n       return 0; n     } n     return count_sum(1, left, right, 0, size - 1); n   } n }; n Tree max_a; n Tree max_b; n int main() { n   int n, k; n   long long a, b; n   int q; n   cin >> n >> k >> a >> b >> q; n   max_a.initialize(n); n   max_b.initialize(n); n   for (int i = 0; i < q; ++i) { n     int type; n     cin >> type; n     if (type == 1) { n       int day; n       long long x; n       cin >> day >> x; n       --day; n       max_a.update(day, x, a); n       max_b.update(day, x, b); n     } else { n       int p; n       cin >> p; n       --p; n       long long ans = 0; n       ans += max_b.get_sum(0, p - 1); n       ans += max_a.get_sum(p + k, n - 1); n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sove() {} n int main() { n   int n; n   cin >> n; n   int nums[n]; n   for (int i = 0; i < n; ++i) { n     cin >> nums[i]; n   } n   for (int i = 0; i < n / 2; i += 2) { n     int temp = nums[i]; n     nums[i] = nums[n - i - 1]; n     nums[n - i - 1] = temp; n   } n   for (int num : nums) { n     cout << num <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n vector<int> e[maxn]; n int dp[maxn], root[maxn], d[maxn], sz[maxn]; n pair<int, int> dfs(int v, int st, int par) { n   if (root[v] == -1) sz[st]++; n   root[v] = st; n   pair<int, int> p = make_pair(0, -1); n   for (int i = 0; i < e[v].size(); i++) n     if (e[v][i] != par) p = max(p, dfs(e[v][i], st, v)); n   if (par != -1) p.first++; n   if (p.second == -1) p.second = v; n   return p; n } n int get_root(int v) { return root[v] == v ? v : root[v] = get_root(root[v]); } n void merge(int v, int u) { n   v = get_root(v); n   u = get_root(u); n   if (sz[v] > sz[u]) swap(v, u); n   if (v == u) return; n   root[v] = u; n   sz[u] += sz[v]; n   d[u] = max((d[v] + 1) / 2 + (d[u] + 1) / 2 + 1, max(d[v], d[u])); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, q; n   cin >> n >> m >> q; n   memset(root, -1, sizeof root); n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> v >> u; n     v--; n     u--; n     e[v].push_back(u); n     e[u].push_back(v); n   } n   for (int i = 0; i < n; i++) n     if (root[i] == -1) d[i] = dfs(dfs(i, i, -1).second, i, -1).first; n   for (int i = 0; i < q; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int x; n       cin >> x; n       x--; n       cout << d[get_root(x)] <<   n ; n     } else { n       int x, y; n       cin >> x >> y; n       x--; n       y--; n       merge(x, y); n     } n   } 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(0); n   cout.tie(0); n   long long int t; n   t = 1; n   for (long long int _ = 0; _ < t; _++) { n     long long int n, m, b, mod; n     cin >> n >> m >> b >> mod; n     vector<long long int> v(n + 1); n     for (long long int i = 1; i <= n; i++) cin >> v[i]; n     long long int dp[2][m + 1][b + 1]; n     for (long long int i = 0; i <= 1; i++) { n       for (long long int j = 0; j <= m; j++) { n         for (long long int k = 0; k <= b; k++) dp[i][j][k] = 0; n       } n     } n     dp[0][0][0] = 1; n     for (long long int i = 1; i <= n; i++) { n       for (long long int j = 0; j <= m; j++) { n         for (long long int k = 0; k <= b; k++) { n           if (k >= v[i] && j > 0) n             dp[i % 2][j][k] = n                 (dp[1 - (i % 2)][j][k] + dp[i % 2][j - 1][k - v[i]]) % mod; n           else n             dp[i % 2][j][k] = dp[1 - (i % 2)][j][k]; n         } n       } n     } n     long long int ans = 0; n     for (long long int k = 0; k <= b; k++) ans = (ans + dp[n % 2][m][k]) % mod; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n set<string> a, b, c; n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     a.insert(s); n   } n   for (int i = 1; i <= m; i++) { n     string s; n     cin >> s; n     if (a.count(s)) c.insert(s); n     b.insert(s); n   } n   while (1) { n     if (a.empty()) { n       cout <<  NO n ; n       return; n     } n     if (!c.empty()) { n       a.erase(*c.begin()); n       b.erase(*c.begin()); n       c.erase(c.begin()); n     } else { n       if (b.count(*a.begin())) b.erase(*a.begin()); n       a.erase(a.begin()); n     } n     if (b.empty()) { n       cout <<  YES n ; n       return; n     } n     if (!c.empty()) { n       a.erase(*c.begin()); n       b.erase(*c.begin()); n       c.erase(c.begin()); n     } else { n       if (a.count(*b.begin())) a.erase(*b.begin()); n       b.erase(b.begin()); n     } n   } n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, res = 0, a = 0, b; n   cin >> n >> d; n   for (int i = 0; i < n; i++) { n     cin >> b; n     if (a >= b) { n       res += ceil((a - b + 1) / (double)d); n       b += ceil((a - b + 1) / (double)d) * d; n     } n     a = b; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > g[100010]; n int N, K, in[100010], dp[100010][2]; n vector<pair<int, int> > num[100010]; n void dfs(int u, int fa) { n   for (pair<int, int>& e : g[u]) { n     int v = e.first, cost = e.second; n     if (v == fa) continue; n     dfs(v, u); n     num[u].push_back(pair<int, int>(dp[v][1] + cost, v)); n   } n   sort(num[u].begin(), num[u].end(), greater<pair<int, int> >()); n   dp[u][1] = 0; n   int sum = 0; n   for (size_t i = 0; (int)i < K - 1 && i < num[u].size(); i++) { n     dp[u][1] += num[u][i].first; n     sum += num[u][i].first; n   } n   if ((int)num[u].size() <= K - 1) { n     for (size_t i = 0; i < num[u].size(); i++) { n       int v = num[u][i].second; n       dp[u][0] = max(dp[u][0], sum - dp[v][1] + dp[v][0]); n     } n   } else { n     for (size_t i = 0; i < (int)K - 1; i++) { n       int v = num[u][i].second; n       dp[u][0] = max(dp[u][0], sum + num[u][K - 1].first - dp[v][1] + dp[v][0]); n     } n     for (size_t i = K - 1; i < num[u].size(); i++) { n       int v = num[u][i].second; n       dp[u][0] = max(dp[u][0], sum + num[u][i].first - dp[v][1] + dp[v][0]); n     } n   } n } n int main() { n   int u, v, c; n   scanf( %d%d , &N, &K); n   for (int i = 1; i < N; i++) { n     scanf( %d%d%d , &u, &v, &c); n     g[u].push_back(pair<int, int>(v, c)); n     g[v].push_back(pair<int, int>(u, c)); n   } n   dfs(0, -1); n   printf( %d n , max(dp[0][0], dp[0][1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { n   while (b) b ^= a ^= b ^= a %= b; n   return a; n } n long long LCM(long long a, long long b) { return a * (b / GCD(a, b)); } n const double EPS = 10e-9; n const long long INF = (1LL << 30); n using namespace std; n template <typename T> n string toString(T t) { n   stringstream ss; n   ss << t; n   return ss.str(); n } n long long isSquare(long long N) { n   long long lo = 1, hi = 2 * sqrt(N); n   while (hi - lo > 1) { n     long long mid = (hi + lo) >> 1; n     long long res = mid * mid; n     if (res < N) { n       lo = mid; n     } else if (res > N) { n       hi = mid; n     } else { n       return mid; n     } n   } n   return lo; n } n int main() { n   long long N; n   cin >> N; n   if (N % 3 != 0) { n     cout << 0 << endl; n   } else { n     N /= 3; n     int options = 0; n     for (long long i = 1; i * i * i <= N; i++) { n       if (N % i == 0) { n         long long left = N / i, to = i / 2; n         for (long long a = 1; a <= to; a++) { n           long long c = i - a; n           long long d = (a - c) * (a - c); n           long long sqr = isSquare(d + 4 * left); n           if (sqr * sqr == d + 4 * left) { n             long long b = (-(a + c) + sqr) >> 1; n             if (c <= b) { n               if (a == c) { n                 if (b == c) { n                   options += 1; n                 } else { n                   options += 3; n                 } n               } else { n                 if (b == c) { n                   options += 3; n                 } else { n                   options += 6; n                 } n               } n             } n           } n         } n       } n     } n     cout << options << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n const long long int inf = 1e17; n const long long int N = 5005; n long long int n; n long long int a[N]; n long long int dp[N][N]; n long long int go(long long int idx, long long int flag) { n   long long int &ans = dp[idx][flag]; n   if (ans != -1) return ans; n   if (idx > n) { n     return 0; n   } n   if (flag == 0 or flag == 2) { n     ans = max(go(idx + 1, flag), go(idx + 1, flag + 1)) + a[idx]; n   } else { n     ans = max(go(idx + 1, flag), go(idx + 1, flag + 1)) - a[idx]; n   } n   return ans; n } n vector<long long int> res; n void backtrack(long long int idx, long long int flag) { n   if (idx > n) { n     return; n   } n   if (flag == 0 or flag == 2) { n     long long int tmp1 = go(idx + 1, flag) + a[idx]; n     long long int tmp2 = go(idx + 1, flag + 1) + a[idx]; n     res.push_back(1); n     if (tmp1 >= tmp2) { n       backtrack(idx + 1, flag); n     } else { n       backtrack(idx + 1, flag + 1); n     } n   } else { n     long long int tmp1 = go(idx + 1, flag) - a[idx]; n     long long int tmp2 = go(idx + 1, flag + 1) - a[idx]; n     res.push_back(0); n     if (tmp1 >= tmp2) { n       backtrack(idx + 1, flag); n     } else { n       backtrack(idx + 1, flag + 1); n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(dp, -1, sizeof(dp)); n   cin >> n; n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long int ans = go(0, 0); n   backtrack(0, 0); n   res.erase(res.begin()); n   long long int cnt = 0, idx = 0; n   while (res[idx] and idx < n) { n     cnt++; n     idx++; n   } n   vector<long long int> vec; n   vec.push_back(cnt); n   cnt = 0; n   idx = vec.back(); n   while (idx < n and !res[idx]) { n     cnt++; n     idx++; n   } n   vec.push_back(cnt); n   idx = vec[0] + vec[1]; n   cnt = 0; n   while (idx < n and res[idx]) { n     cnt++; n     idx++; n   } n   vec.push_back(cnt); n   idx = 0, cnt = 0; n   for (auto to : vec) { n     cnt = to; n     if (cnt == 0LL) { n       cout << idx <<    ; n     } else { n       cout << idx + cnt <<    ; n       idx += cnt; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n bool chk(string s) { n   int l = s.size(); n   for (int i = 0, j = l - 1; i <= j; i++, j--) { n     if (s[i] != s[j]) return false; n   } n   return true; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     string ans; n     int mx = 0; n     string res; n     for (int i = 0; i < s.size(); i++) { n       ans += s[i]; n       if (chk(ans)) { n         int l = ans.size(); n         if (l > mx) { n           mx = ans.size(); n           res = ans; n         } n       } n     } n     ans =   ; n     for (int i = s.size() - 1; i >= 0; i--) { n       ans += s[i]; n       if (chk(ans)) { n         int l = ans.size(); n         if (l > mx) { n           mx = ans.size(); n           res = ans; n         } n       } n     } n     int p1 = 0; n     int p2 = 0; n     for (int i = 0, j = s.size() - 1; i < j; i++, j--) { n       if (s[i] != s[j]) { n         p1 = i - 1; n         p2 = j + 1; n         break; n       } n     } n     ans =   ; n     string rev =   ; n     for (int i = 0; i <= p1; i++) ans += s[i]; n     for (int i = p2; i < s.size(); i++) rev += s[i]; n     string ss =   ; n     for (int i = p1 + 1; i < p2; i++) { n       ss += s[i]; n       string tmp = ans + ss + rev; n       int l = tmp.size(); n       if (l <= mx) continue; n       if (chk(tmp)) { n         if (l > mx) { n           mx = l; n           res = tmp; n         } n       } n     } n     ss =   ; n     for (int j = p2 - 1; j > p1; j--) { n       ss = s[j] + ss; n       string tmp = ans + ss + rev; n       int l = tmp.size(); n       if (chk(tmp)) { n         if (l > mx) { n           mx = l; n           res = tmp; n         } n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int num = n + m; n   int a = m / 2, aa = m % 2, aaa = a + aa; n   if (n == m) { n     for (int i = 0; i < n; i++) { n       printf( 01 ); n     } n   } else if (n < m) { n     if (aaa <= n + 1) { n       a = m - (n + 1); n       m -= a; n       for (int i = 0; n; i++) { n         if (a) { n           printf( 11 ); n           a--; n         } else if (m) { n           printf( 1 ); n           m--; n         } n         if (n) { n           printf( 0 ); n           n--; n         } n       } n       if (a) n         printf( 11 ); n       else if (m) n         printf( 1 ); n     } else n       printf( -1 ); n   } else if (n > m) { n     if (n <= m + 1) { n       for (int i = 0; i < m; i++) { n         printf( 01 ); n       } n       printf( 0 ); n     } else n       printf( -1 ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long k, d, t; n int main() { n   scanf( %lld%lld%lld , &k, &d, &t); n   t *= 2; n   long long round = ceil((double)k / d) * d; n   long long num = k * 2 + (round - k); n   long long cnt = t / num; n   long long left = t - cnt * num; n   double ans = 0.0; n   if (left <= k * 2) { n     ans = cnt * round + left / 2; n     if (left % 2) ans += 0.5; n   } else n     ans = cnt * round + k + (left - k * 2); n   printf( %.1lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ans = 0, last = 0, current = 0; n   while (n > 0) { n     ans++; n     current = last + ans; n     last = current; n     n -= current; n     if (n < 0) { n       ans--; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int head, tail, n, q[100100]; n long long f[100100], a[100100], b[100010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %I64d , &b[i]); n   f[1] = 0; n   head = tail = 1; n   q[head] = 1; n   for (int i = 2; i <= n; i++) { n     while (head < tail) { n       if ((1.0 * f[q[head]] + 1.0 * a[i] * b[q[head]]) > n           (1.0 * f[q[head + 1]] + 1.0 * a[i] * b[q[head + 1]])) n         head++; n       else n         break; n     } n     f[i] = f[q[head]] + b[q[head]] * a[i]; n     while (head < tail) { n       if (1.0 * (f[q[tail]] - f[q[tail - 1]]) * (b[q[tail]] - b[i]) > n           1.0 * (f[i] - f[q[tail]]) * (b[q[tail - 1]] - b[q[tail]])) n         tail--; n       else n         break; n     } n     q[++tail] = i; n   } n   printf( %I64d , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int N = 5005; n void update(int &a, int b) { n   a += b; n   a %= MOD; n } n int add(int a, int b) { return (a + b) % MOD; } n int multiply(int a, int b) { return (a * 1LL * b) % MOD; } n int quick_power(int a, int n) { n   int res = 1; n   for (; n > 0; n >>= 1) { n     if (n & 1) res = multiply(res, a); n     a = multiply(a, a); n   } n   return res; n } n int get_inv(int a) { return quick_power(a, MOD - 2); } n int n, a; n int cnt[N], sum[N][N], dp[N][N]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     cnt[a]++; n   } n   for (int i = 0; i <= n; i++) { n     sum[0][i] = 1; n   } n   int answer = 0; n   for (int i = 1; i <= n; i++) { n     int inv = get_inv(n - i + 1); n     for (int j = 1; j <= n; j++) { n       if (cnt[j]) { n         dp[i][j] = multiply(multiply(sum[i - 1][j - 1], cnt[j]), inv); n         update(answer, multiply(multiply(dp[i - 1][j], cnt[j] - 1), inv)); n       } n       sum[i][j] = add(sum[i][j - 1], dp[i][j]); n     } n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long double n, h; n   cin >> n >> h; n   cout << setprecision(20); n   for (long double i = 0; i < n - 1; i++) { n     cout << h * sqrt((i + 1) / n) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n const long long N = 1e5 + 5, M = 1e9 + 7; n char ulta[500]; n set<char> ache; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ulta[ U ] =  D ; n   ulta[ D ] =  U ; n   ulta[ R ] =  L ; n   ulta[ L ] =  R ; n   string s; n   long long n, i, j, k, ans = 2; n   cin >> n >> s; n   for (auto ch : s) { n     auto x = ache.find(ulta[ch]); n     if (x != ache.end()) { n       ans++; n       ache.clear(); n     } n     ache.insert(ch); n   } n   cout << ans - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct State { n   int len, link; n   map<char, int> nxtc; n } SA[2 * 2006]; n bool vis[2 * 2006]; n char str[2006], pref[2 * 2006], suf[2006]; n class SuffixAutomata { n   int sz, last; n   void init(int idx) { n     SA[idx].len = 0, SA[idx].link = -1; n     SA[idx].nxtc.clear(); n   } n   bool traverse(int cur, char *ch) { n     if (*ch == 0) return true; n     if (SA[cur].nxtc.count(*ch) == 0) return false; n     int p = SA[cur].nxtc[*ch]; n     return traverse(p, ++ch); n   } n   int findState(int cur, char *ch) { n     if (*ch == 0) return cur; n     int p = SA[cur].nxtc[*ch]; n     return findState(p, ++ch); n   } n  n  public: n   SuffixAutomata() { n     sz = last = 0; n     init(0); n     ++sz; n   } n   int size() { return sz; } n   void extend(char ch) { n     int p, q, clone, cur = sz++; n     init(cur); n     SA[cur].len = SA[last].len + 1; n     for (p = last; p != -1 && SA[p].nxtc.count(ch) == 0; p = SA[p].link) n       SA[p].nxtc[ch] = cur; n     if (p == -1) n       SA[cur].link = 0; n     else { n       q = SA[p].nxtc[ch]; n       if (SA[p].len + 1 == SA[q].len) n         SA[cur].link = q; n       else { n         clone = sz++; n         init(clone); n         SA[clone] = SA[q]; n         SA[clone].len = SA[p].len + 1; n         for (; p != -1 && SA[p].nxtc[ch] == q; p = SA[p].link) n           SA[p].nxtc[ch] = clone; n         SA[cur].link = SA[q].link = clone; n       } n     } n     last = cur; n   } n   bool traverse(char *str) { return traverse(0, str); } n   int findState(char *str) { return findState(0, str); } n }; n int lenp, lens, dp[2 * 2006][2006]; n int rec(int cur, int idx) { n   if (suf[idx] == 0) return 1; n   if (dp[cur][idx] != -1) return dp[cur][idx]; n   int &ret = dp[cur][idx]; n   ret = 0; n   char ch; n   int p; n   if (idx) { n     ch = suf[idx]; n     if (SA[cur].nxtc.count(ch) == 0) return ret = 0; n     p = SA[cur].nxtc[ch]; n     return ret = rec(p, idx + 1); n   } n   map<char, int> &M = SA[cur].nxtc; n   map<char, int>::iterator im; n   for (im = M.begin(); im != M.end(); ++im) { n     ch = im->first; n     p = im->second; n     ret += rec(p, idx); n     if (ch == suf[idx]) ret += rec(p, idx + 1); n   } n   return ret; n } n int main() { n   int i, j, ans, n; n   while (scanf( %s , str) != EOF) { n     SuffixAutomata sa; n     for (i = 0; str[i]; ++i) sa.extend(str[i]); n     ans = 0; n     scanf( %s %s , pref, suf); n     lenp = strlen(pref), lens = strlen(suf); n     if (!sa.traverse(pref)) { n       puts( 0 ); n       continue; n     } n     for (i = 0; i < lenp; ++i) { n       if (i + lens < lenp) continue; n       for (j = 0; j < lens; ++j) { n         if (pref[i + j] != suf[j]) break; n       } n       if (i + j == lenp) { n         strcat(pref, &suf[lenp - i]); n         ans += sa.traverse(pref); n         pref[lenp] = 0; n       } n     } n     n = sa.findState(pref); n     memset(dp, -1, sizeof(dp)); n     ans += rec(n, 0); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200005; n int main() { n   int n, k, q, i, a, b, p[MAX] = {0}, c[MAX] = {0}; n   scanf( %d %d %d , &n, &k, &q); n   while (n--) { n     scanf( %d %d , &a, &b); n     p[a]++; n     p[b + 1]--; n   } n   for (i = 1; i < MAX; i++) { n     p[i] += p[i - 1]; n     c[i] += c[i - 1] + (p[i] >= k); n   } n   while (q--) { n     scanf( %d %d , &a, &b); n     printf( %d n , c[b] - c[a - 1]); n   } n   return 0; n } n