#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n using ld = long double; n template <class T> n using Table = vector<vector<T>>; n const ld eps = 1e-9; n template <class S, class T> n ostream &operator<<(ostream &os, const pair<S, T> v) { n   os <<  (   << v.first <<  ,   << v.second <<  ) ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os <<    ; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<vector<T>> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os << endl; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<set<T>> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os << endl; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const set<T> &v) { n   int i = 0; n   for (auto it : v) { n     if (i > 0) { n       os <<    ; n     } n     os << it; n     i++; n   } n   return os; n } n const int INF = 2147483647; n const long long int L_INF = 9223372036854775807; n struct Edge { n   int src, dst; n   int weight; n   Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {} n }; n bool operator<(const Edge &e, const Edge &f) { n   return e.weight != f.weight ? e.weight > f.weight n          : e.src != f.src     ? e.src < f.src n                               : e.dst < f.dst; n } n int maximumFlow(const vector<vector<Edge>> &ag, int s, int t) { n   vector<vector<Edge>> g(ag); n   for (int i = 0; i < ag.size(); ++i) { n     for (int j = 0; j < ag[i].size(); ++j) { n       int d = ag[i][j].dst; n       int s = ag[i][j].src; n       bool ok = false; n       for (int k = 0; k < ag[d].size(); ++k) { n         if (ag[d][k].src == s) { n           ok = true; n           break; n         } n       } n       if (!ok) { n         g[d].push_back(Edge(d, s, 0)); n       } n     } n   } n   int n = g.size(); n   vector<vector<int>> flow(n, vector<int>(n)), capacity(n, vector<int>(n)); n   for (int u = 0; u < (int)n; ++u) n     for (auto e = (g[u]).begin(); e != (g[u]).end(); ++e) n       capacity[e->src][e->dst] += e->weight; n   int total = 0; n   while (1) { n     queue<int> Q; n     Q.push(s); n     vector<int> prev(n, -1); n     prev[s] = s; n     while (!Q.empty() && prev[t] < 0) { n       int u = Q.front(); n       Q.pop(); n       for (auto e = (g[u]).begin(); e != (g[u]).end(); ++e) n         if (prev[e->dst] < 0 && (capacity[u][e->dst] - flow[u][e->dst]) > 0) { n           prev[e->dst] = u; n           Q.push(e->dst); n         } n     } n     if (prev[t] < 0) break; n     int inc = INF; n     for (int j = t; prev[j] != j; j = prev[j]) n       inc = min(inc, (capacity[prev[j]][j] - flow[prev[j]][j])); n     for (int j = t; prev[j] != j; j = prev[j]) n       flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc; n     total += inc; n   } n   return total; n } n using ll = long long int; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, 1, 0, -1}; n int N, T; n vector<vector<int>> get_time(const vector<vector<int>> &canmove, int sy, int sx, n                              vector<vector<int>> c_times) { n   vector<vector<int>> times(N, vector<int>(N, 100000)); n   if (c_times.empty()) { n     c_times = times; n   } n   times[sy][sx] = 0; n   queue<pair<int, int>> que; n   que.emplace(sx, sy); n   while (!que.empty()) { n     auto p(que.front()); n     que.pop(); n     int x = p.first; n     int y = p.second; n     if (c_times[y][x] == times[y][x]) continue; n     for (int way = 0; way < 4; ++way) { n       int nx = x + dx[way]; n       int ny = y + dy[way]; n       if (nx >= 0 && nx < N && ny >= 0 && ny < N) { n         if (canmove[ny][nx] != false) { n           if (times[ny][nx] > times[y][x] + 1 && n               c_times[ny][nx] >= times[y][x] + 1) { n             times[ny][nx] = times[y][x] + 1; n             que.emplace(nx, ny); n           } n         } n       } n     } n   } n   return times; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cin >> N >> T; n   vector<vector<int>> canmove(N, vector<int>(N, true)); n   vector<vector<int>> ins(N, vector<int>(N)), outs(N, vector<int>(N)); n   vector<string> in_sts(N), out_sts(N); n   int collapt_x, collapt_y; n   for (int i = 0; i < N; ++i) { n     cin >> in_sts[i]; n     for (int j = 0; j < N; ++j) { n       if (in_sts[i][j] ==  Y ) { n         canmove[i][j] = false; n       } else if (in_sts[i][j] ==  Z ) { n         canmove[i][j] = false; n       } else { n         ins[i][j] = in_sts[i][j] -  0 ; n       } n     } n   } n   for (int i = 0; i < N; ++i) { n     cin >> out_sts[i]; n     for (int j = 0; j < N; ++j) { n       if (out_sts[i][j] ==  Y ) { n       } else if (out_sts[i][j] ==  Z ) { n         collapt_x = j; n         collapt_y = i; n       } else { n         outs[i][j] = out_sts[i][j] -  0 ; n       } n     } n   } n   vector<vector<int>> collapt_times = n       get_time(canmove, collapt_y, collapt_x, vector<vector<int>>(0)); n   vector<vector<vector<vector<int>>>> oks( n       N, n       vector<vector<vector<int>>>(N, vector<vector<int>>(N, vector<int>(N)))); n   const int start = 0; n   const int in_id = 1; n   const int out_id = in_id + N * N; n   const int goal = out_id + N * N; n   vector<vector<Edge>> g(goal + 1); n   for (int y = 0; y < N; ++y) { n     for (int x = 0; x < N; ++x) { n       g[start].push_back(Edge{start, in_id + y * N + x, ins[y][x]}); n       g[out_id + y * N + x].push_back( n           Edge{out_id + y * N + x, goal, outs[y][x]}); n     } n   } n   for (int y = 0; y < N; ++y) { n     for (int x = 0; x < N; ++x) { n       auto atimes = get_time(canmove, y, x, collapt_times); n       for (int ty = 0; ty < N; ++ty) { n         for (int tx = 0; tx < N; ++tx) { n           if (atimes[ty][tx] <= collapt_times[ty][tx] && atimes[ty][tx] <= T) { n             oks[y][x][ty][tx] = true; n             int aid = in_id + y * N + x; n             int bid = out_id + ty * N + tx; n             g[aid].push_back(Edge{aid, bid, 1000}); n           } n         } n       } n     } n   } n   int answer = maximumFlow(g, start, goal); n   cout << answer << endl; 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 pii = pair<int, int>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, k, t; n   cin >> n >> m >> k >> t; n   vector<pii> waste(k); n   for (int i = 0; i < k; ++i) { n     cin >> waste[i].first >> waste[i].second; n     --waste[i].first; n     --waste[i].second; n   } n   sort(begin(waste), end(waste)); n   vector<pair<pii, int>> queries(t); n   for (int i = 0; i < t; ++i) { n     cin >> queries[i].first.first >> queries[i].first.second; n     --queries[i].first.first; n     --queries[i].first.second; n     queries[i].second = i; n   } n   sort(begin(queries), end(queries)); n   vector<int> ans(t); n   int ptr = 0; n   for (auto& p : queries) { n     pii pos = p.first; n     int idx = p.second; n     while (ptr < k and waste[ptr] < pos) { n       ++ptr; n     } n     if (ptr < k and waste[ptr] == pos) { n       ans[idx] = 3; n     } else { n       int tot = m * pos.first + pos.second; n       tot -= ptr; n       ans[idx] = tot % 3; n     } n   } n   vector<string> res = { Carrots ,  Kiwis ,  Grapes ,  Waste }; n   for (int x : ans) cout << res[x] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> ad[100009]; n vector<int> idx; n deque<int> vec; n int cnt[100009]; n int ans[100009]; n vector<pair<int, int> > arr, arr2; n int val[100009]; n set<int> s; n int main() { n   int a, b, c, d, e, i, j, k, l, n, m, x, y, t, p, X; n   scanf( %d , &t); n   for (p = 1; p <= t; p++) { n     scanf( %d%d%d , &n, &x, &y); n     c = y - x; n     m = n + 1; n     arr.clear(); n     arr2.clear(); n     for (i = 0; i <= m; i++) { n       cnt[i] = 0; n       ad[i].clear(); n       ans[i] = -1; n     } n     s.clear(); n     for (i = 1; i <= n; i++) { n       scanf( %d , &a); n       val[i] = a; n       cnt[a]++; n       ad[a].push_back(i); n       s.insert(i); n     } n     for (i = 1; i <= m; i++) { n       if (cnt[i] == 0) { n         X = i; n         break; n       } n     } n     if (c == 0) { n       for (i = 1; i <= n; i++) { n         if (x) { n           ans[i] = val[i]; n           x--; n         } else { n           ans[i] = X; n         } n       } n       printf( YES n ); n       for (i = 1; i <= n; i++) { n         printf( %d  , ans[i]); n       } n       printf(  n ); n       continue; n     } n     for (i = 1; i <= m; i++) { n       if (cnt[i] > 0) { n         arr.push_back({cnt[i], i}); n       } n     } n     sort(arr.begin(), arr.end()); n     m = arr.size(); n     c = y - x; n     for (i = 0; i < m; i++) { n       if (c == 0) { n         break; n       } n       b = c / (m - i); n       if (c % (m - i) != 0) { n         b++; n       } n       b = min(b, arr[i].first); n       c -= b; n       arr2.push_back({b, arr[i].second}); n     } n     sort(arr2.begin(), arr2.end()); n     c = y - x; n     m = arr2.size(); n     if (m == 1) { n       a = arr2[0].second; n       l = arr2[0].first; n       if (l > (n - cnt[a])) { n         printf( NO n ); n         continue; n       } n       b = l; n       for (i = 1; i <= n; i++) { n         if (val[i] != a && b > 0) { n           ans[i] = a; n           b--; n         } n       } n       for (i = 0; i < l; i++) { n         b = ad[a][i]; n         ans[b] = X; n       } n     } else { n       l = 0; n       if (arr2[m - 1].first > (c - arr2[m - 1].first)) { n         l = arr2[m - 1].first - (c - arr2[m - 1].first); n         for (i = 0; i < m; i++) { n           a = arr2[i].second; n           b = arr2[i].first; n           for (j = 0; j < b; j++) { n             s.erase(ad[a][j]); n           } n         } n         a = arr2[m - 1].second; n         b = ad[a].size(); n         for (j = 0; j < b; j++) { n           s.erase(ad[a][j]); n         } n         b = s.size(); n         if (l > b) { n           printf( NO n ); n           continue; n         } n         arr2[m - 1].first -= l; n         a = arr2[m - 1].second; n         for (i = 0; i < l; i++) { n           d = *s.begin(); n           s.erase(d); n           ans[d] = a; n         } n         b = arr2[m - 1].first; n         d = b + l; n         for (i = b; i < d; i++) { n           k = ad[a][i]; n           ans[k] = X; n         } n       } n       vec.clear(); n       idx.clear(); n       for (i = 0; i < m; i++) { n         a = arr2[i].second; n         b = arr2[i].first; n         if (i == m - 1) { n           for (j = 0; j < b; j++) { n             vec.push_front(a); n           } n         } else { n           for (j = 0; j < b; j++) { n             vec.push_back(a); n           } n         } n         for (j = 0; j < b; j++) { n           idx.push_back(ad[a][j]); n         } n       } n       d = idx.size(); n       for (i = 0; i < d; i++) { n         a = idx[i]; n         ans[a] = vec[i]; n       } n     } n     for (i = 1; i <= n; i++) { n       if (ans[i] == -1) { n         if (x) { n           ans[i] = val[i]; n           x--; n         } else { n           ans[i] = X; n         } n       } n     } n     if (x > 0) { n       printf( NO n ); n       continue; n     } n     printf( YES n ); n     for (i = 1; i <= n; i++) { n       printf( %d  , ans[i]); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); 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 - 1; i++) { n     if (nums[i] < 0) { n       cout <<  NO ; n       return 0; n     } n     if (nums[i] % 2 == 1) { n       nums[i + 1] -= 1; n     } n   } n   if (nums[n - 1] >= 0 && nums[n - 1] % 2 == 0) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n void rd(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n void lrd(long long &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n const int INF = 1e9; n const long long LINF = 1e18; n const int mod = 998244353; n const int N = 3e5 + 10; n using namespace std; n int n; n int fi[N], nxt[N << 1], to[N << 1], tot; n void link(int x, int y) { n   nxt[++tot] = fi[x]; n   fi[x] = tot; n   to[tot] = y; n } n int f[N][3]; n void dfs(int x) { n   f[x][0] = f[x][1] = f[x][2] = 1; n   int cnt = 1; n   int ghb = 1; n   for (int i = fi[x]; i; i = nxt[i]) { n     if (f[to[i]][0]) continue; n     dfs(to[i]); n     cnt = cnt * 2 % mod; n     int tmp; n     tmp = (1ll * f[to[i]][0] * 2 + f[to[i]][1] + f[to[i]][2]) % mod; n     f[x][0] = 1ll * f[x][0] * tmp % mod; n     f[x][1] = 1ll * f[x][1] * (1ll * 2 * f[to[i]][0] + f[to[i]][1]) % mod; n     ghb = 1ll * ghb * (f[to[i]][0] + f[to[i]][1]) % mod; n     f[x][2] = 1ll * f[x][2] * (1ll * f[to[i]][0] * 2 + f[to[i]][1]) % mod; n   } n   f[x][1] = (f[x][1] - ghb + mod) % mod; n } n int main() { n   rd(n); n   for (int i = 1; i < n; i++) { n     int x, y; n     rd(x); n     rd(y); n     link(x, y); n     link(y, x); n   } n   dfs(1); n   printf( %d n , (f[1][0] + f[1][1] - 1) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1}; n int dr4[] = {0, 1, -1, 0}; n int dc4[] = {1, 0, 0, -1}; n int n; n int a[N]; n set<pair<int, int> > s; n set<int> end_idx; n int sz[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i + 1]; n   } n   int c = 1; n   for (int i = 2; i <= n; i++) { n     if (a[i] != a[i - 1]) { n       s.insert({-c, i - 1}); n       end_idx.insert(i - 1); n       sz[i - 1] = c; n       c = 1; n     } else n       c++; n   } n   s.insert({-c, n}); n   end_idx.insert(n); n   sz[n] = c; n   int ans = 0; n   while (!s.empty()) { n     auto it = *s.begin(); n     s.erase(s.begin()); n     end_idx.erase(it.second); n     ans++; n     auto idx = end_idx.lower_bound(it.second); n     if (idx != end_idx.begin() && idx != end_idx.end()) { n       int rgt = *idx; n       idx--; n       int lft = *idx; n       if (a[lft] == a[rgt]) { n         s.erase(s.find({-sz[lft], lft})); n         s.erase(s.find({-sz[rgt], rgt})); n         end_idx.erase(lft); n         sz[rgt] += sz[lft]; n         s.insert({-sz[rgt], rgt}); n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (107); n const int inf = (int)1e9 + 42; n struct aho_corasick { n   int is_end[MAXN], link[MAXN], psz; n   map<char, int> to[MAXN]; n   void clear() { n     for (int i = 0; i < psz; i++) is_end[i] = 0, link[i] = 0, to[i].clear(); n     psz = 1; n     is_end[0] = 1; n   } n   aho_corasick() { n     psz = MAXN - 2; n     clear(); n   } n   void add_word(string s) { n     int u = 0; n     for (char c : s) { n       if (!to[u].count(c)) to[u][c] = psz++; n       u = to[u][c]; n     } n     is_end[u] = 1; n   } n   void push_links() { n     queue<int> Q; n     int u, v, j; n     char c; n     Q.push(0); n     link[0] = -1; n     while (!Q.empty()) { n       u = Q.front(); n       Q.pop(); n       for (auto it : to[u]) { n         v = it.second; n         c = it.first; n         j = link[u]; n         while (j != -1 && !to[j].count(c)) j = link[j]; n         if (j != -1) n           link[v] = to[j][c]; n         else n           link[v] = 0; n         Q.push(v); n       } n     } n   } n }; n int n, m; n string s1, s2, virus; n void read() { n   cin >> s1; n   cin >> s2; n   cin >> virus; n   n = s1.size(); n   m = s2.size(); n } n aho_corasick aho; n bool is_bad[MAXN]; n int dp[MAXN][MAXN][MAXN]; n int nxt_i[MAXN][MAXN][MAXN]; n int nxt_j[MAXN][MAXN][MAXN]; n int nxt_state[MAXN][MAXN][MAXN]; n int rec(int i, int j, int state) { n   if (i >= n + 1) return -inf; n   if (j >= m + 1) return -inf; n   if (is_bad[state]) return -inf; n   if (i == n && j == m) return 0; n   int &memo = dp[i][j][state]; n   if (memo != -1) return memo; n   memo = -inf; n   if (i < n && j < m && s1[i] == s2[j]) { n     int u = state; n     while (u != -1 && !aho.to[u].count(s1[i])) u = aho.link[u]; n     if (u == -1) n       u = 0; n     else n       u = aho.to[u][s1[i]]; n     memo = max(memo, 1 + rec(i + 1, j + 1, u)); n     nxt_state[i][j][state] = u; n     nxt_i[i][j][state] = i + 1; n     nxt_j[i][j][state] = j + 1; n   } n   if (memo < rec(i + 1, j, state)) { n     memo = max(memo, rec(i + 1, j, state)); n     nxt_state[i][j][state] = state; n     nxt_i[i][j][state] = i + 1; n     nxt_j[i][j][state] = j; n   } n   if (memo < rec(i, j + 1, state)) { n     memo = max(memo, rec(i, j + 1, state)); n     nxt_state[i][j][state] = state; n     nxt_i[i][j][state] = i; n     nxt_j[i][j][state] = j + 1; n   } n   return memo; n } n void solve() { n   aho.clear(); n   aho.add_word(virus); n   aho.push_links(); n   int u = 0; n   for (char c : virus) u = aho.to[u][c]; n   is_bad[u] = true; n   for (int i = 0; i < aho.psz; i++) { n     int u = i; n     while (u != -1) { n       is_bad[i] |= is_bad[u]; n       u = aho.link[u]; n     } n   } n   memset(dp, -1, sizeof(dp)); n   int ret = rec(0, 0, 0); n   if (!ret) n     cout << 0 <<   n ; n   else { n     string ans =   ; n     int c_i = 0, c_j = 0, c_state = 0, last_i, last_j, last_state; n     while (!(c_i == n && c_j == m)) { n       last_i = c_i; n       last_j = c_j; n       last_state = c_state; n       c_i = nxt_i[last_i][last_j][last_state]; n       c_j = nxt_j[last_i][last_j][last_state]; n       c_state = nxt_state[last_i][last_j][last_state]; n       if (c_i - last_i && c_j - last_j) ans.push_back(s1[last_i]); n     } n     cout << ans <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n char s[100][100]; n int main() { n   int n, m, ans = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int f = 0, g = 0; n       for (int k = 1; k <= m; k++) n         if (s[i][k] !=  .  || s[j][k] !=  . ) { n           if (s[i][k] == s[j][k]) n             f = 1; n           else n             g = 1; n         } n       if (f == g && f) { n         printf( No ); n         return 0; n       } n     } n   } n   printf( Yes ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, d; n     cin >> n >> d; n     if (n >= d) n       cout <<  YES  << endl; n     else { n       double i = 1, cnt = 0; n       while (i <= n) { n         int y = ceil(d / (i + 1)) + i; n         if (y <= n) { n           cout <<  YES  << endl; n           ++cnt; n           break; n         } n         ++i; n       } n       if (cnt == 0) cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     sort(arr, arr + n); n     bool flag = 1; n     for (int i = 1; i < n; i++) { n       if (arr[i] - arr[i - 1] > 1) { n         flag = 0; n       } n     } n     if (flag) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100, PI = 3.14159265359, M = 1e9 + 7; n long long a[N], ans = 1; n int main() { n   long long n; n   cin >> n; n   long long x = 0, y = 0; n   for (int i = 0; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     long long t1 = a - x, t2 = b - x; n     if (x == y) { n       ans += min(a, b) - x; n     } else if (x < y) { n       if (y <= a) { n         x = y; n         ans += min(a, b) - x + 1; n       } n     } else if (x > y) { n       if (x <= b) { n         y = x; n         ans += min(a, b) - x + 1; n       } n     } n     x = a, y = b; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 40; n int a[maxN][maxN]; n int b[maxN][maxN]; n int flips[maxN][maxN]; n int n, x; n int check(int mask) { n   memset(flips, 0, sizeof(flips)); n   for (int i = 0; i < x; ++i) { n     if (mask & (1 << i)) { n       flips[x - 1][i] = 1; n     } n   } n   for (int i = 0; i + 1 < x; ++i) { n     for (int j = 0; j < x; ++j) { n       flips[i + x][j] = flips[i][j] ^ flips[x - 1][j]; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j + 1 < x; ++j) { n       flips[i][j + x] = flips[i][j] ^ flips[i][x - 1]; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       b[i][j] = a[i][j]; n       if (flips[i][j]) { n         b[i][j] = -b[i][j]; n       } n     } n   } n   int res = 0; n   for (int i = 0; i < n; ++i) { n     res += b[x - 1][i]; n   } n   for (int i = 0; i + 1 < x; ++i) { n     for (int j = 0; j < n; ++j) { n       b[i][j] += b[i + x][j]; n     } n   } n   for (int i = 0; i + 1 < x; ++i) { n     int cres1 = 0, cres2 = 0; n     for (int j = 0; j + 1 < x; ++j) { n       cres1 += abs(b[i][j] + b[i][j + x]); n       cres2 += abs(b[i][j] - b[i][j + x]); n     } n     res += max(cres1 + b[i][x - 1], cres2 - b[i][x - 1]); n   } n   return res; n } n int solve(vector<vector<int> > a) { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       ::a[i][j] = a[i][j]; n     } n   } n   x = (n + 1) / 2; n   int res = -1000000000LL; n   for (int i = 0; i < (1 << x); ++i) { n     res = max(res, check(i)); n   } n   return res; n } n int main() { n   scanf( %d , &n); n   vector<vector<int> > a(n, vector<int>(n)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       scanf( %d , &a[i][j]); n     } n   } n   cout << solve(a) << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long S[1000000] = {}; n int main() { n   cin >> n >> k; n   for (long long i = 0; i < n; i++) { n     string s; n     cin >> s; n     S[s.size()]++; n   } n   string s; n   cin >> s; n   long long pass = s.size(); n   long long sum1 = 1, sum2 = 0; n   for (long long i = 0; i < pass; i++) { n     sum1 += S[i]; n     sum2 += S[i]; n   } n   sum2 += S[pass]; n   cout << sum1 + ((sum1 - 1) / k) * 5 <<     << sum2 + ((sum2 - 1) / k) * 5 n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long exp(long long x, long long y, long long p) { n   long long res = 1; n   while (y) { n     if (y % 2) res = (res * x % p) % p; n     x = (x * x) % p; n     y /= 2; n   } n   return res; n } n long long exp(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y % 2) res = (res * x); n     x = (x * x); n     y /= 2; n   } n   return res; n } n long long a[100]; n int main() { n   long long n, m; n   cin >> n >> m; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n > m) n     cout << 0 << endl; n   else { n     long long sum = 1; n     for (long long i = 0; i < n; i++) { n       for (long long j = i + 1; j < n; j++) { n         sum = sum * abs(a[i] - a[j]); n         sum = sum % m; n       } n     } n     cout << sum % m; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n int A[N]; n const int S = 250; n int sum[S][S]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int Q; n   cin >> Q; n   while (Q--) { n     int t, x, y; n     cin >> t >> x >> y; n     if (t == 1) { n       A[x] += y; n       for (int m = 1; m < S; m++) { n         sum[m][x % m] += y; n       } n     } else { n       int ans = 0; n       if (x < S) { n         ans = sum[x][y]; n       } else { n         for (int i = y; i < N; i += x) { n           ans += A[i]; n         } n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n  public: n   int x, y; n   bool operator<(const Point &a) const { n     if (x != a.x) return x < a.x; n     return y < a.y; n   } n } tem; n set<Point> p; n set<int> xSet; n set<int> ySet; n bool f() { n   if (xSet.size() != 3 || ySet.size() != 3 || p.size() != 8) return false; n   set<int>::iterator p1; n   set<int>::iterator p2; n   int cn = 0; n   for (p1 = xSet.begin(); p1 != xSet.end(); p1++) { n     for (p2 = ySet.begin(); p2 != ySet.end(); p2++) { n       cn++; n       if (cn == 5) continue; n       tem.x = *p1; n       tem.y = *p2; n       if (p.find(tem) == p.end()) return false; n     } n   } n   return true; n } n int main(int argc, char **argv) { n   int i, a, b; n   for (i = 0; i < 8; i++) { n     scanf( %d%d , &a, &b); n     tem.x = a; n     tem.y = b; n     xSet.insert(a); n     ySet.insert(b); n     p.insert(tem); n   } n   printf( %s n , (f() ?  respectable  :  ugly )); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> b(n); n   string aans = string(n / 2,  1 ) + string((n + 1) / 2,  0 ); n   string bans = string(n / 2,  1 ) + string((n + 1) / 2,  0 ); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     cin >> b[i]; n   } n   if (n % 2) { n     if (a[n / 2] < b[n / 2]) n       aans[n / 2] =  1 ; n     else n       bans[n / 2] =  1 ; n   } n   int bpoint = 0; n   int apoint = 0; n   for (int i = 0; i < n; i++) { n     if (a[apoint] < b[bpoint]) { n       aans[apoint] =  1 ; n       apoint++; n     } else { n       bans[bpoint] =  1 ; n       bpoint++; n     } n   } n   cout << aans <<   n  << bans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   int c1, c2, c3; n   c1 = c2 = c3 = 0; n   cin >> str; n   for (int i = 0; i < str.length(); i++) { n     if (str[i] ==  + ) { n       str.erase(i, 1); n       i--; n     } n   } n   for (int i = 0; i < str.length(); i++) { n     if (str[i] ==  1 ) n       c1++; n     else if (str[i] ==  2 ) n       c2++; n     else n       c3++; n   } n   if (c1 == 0) { n     if (c2 == 0) { n       if (c3 == 0) n         cout <<   ; n       else { n         cout <<  3 ; n         for (int i = 0; i < c3 - 1; i++) { n           cout <<  +3 ; n         } n       } n     } else { n       if (c3 == 0) { n         cout <<  2 ; n         for (int i = 0; i < c2 - 1; i++) { n           cout <<  +2 ; n         } n       } else { n         cout <<  2 ; n         for (int i = 0; i < c2 - 1; i++) { n           cout <<  +2 ; n         } n         for (int i = 0; i < c3; i++) { n           cout <<  +3 ; n         } n       } n     } n   } else { n     cout <<  1 ; n     for (int i = 0; i < c1 - 1; i++) { n       cout <<  +1 ; n     } n     if (c2 == 0) { n       if (c3 == 0) n         cout <<   ; n       else { n         for (int i = 0; i < c3; i++) { n           cout <<  +3 ; n         } n       } n     } else { n       if (c3 == 0) { n         for (int i = 0; i < c2; i++) { n           cout <<  +2 ; n         } n       } else { n         for (int i = 0; i < c2; i++) { n           cout <<  +2 ; n         } n         for (int i = 0; i < c3; i++) { n           cout <<  +3 ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int dx[8] = {0, -1, 0, 1, -1, -1, 1, 1}, n           dy[8] = {1, 0, -1, 0, -1, 1, -1, 1}; n int a[100005]; n int cnt = 0; n int n, m, k; n vector<int> vt[100005], vt2[2]; n stack<int> st; n int dfn[100005], color[100005]; n void dfs(int x, int fa) { n   st.push(x); n   dfn[x] = st.size(); n   for (int i = 0; i < vt[x].size(); i++) { n     int v = vt[x][i]; n     if (v == fa) continue; n     if (!dfn[v]) { n       dfs(v, x); n     } else if (dfn[x] - dfn[v] + 1 <= k && dfn[x] > dfn[v]) { n       cout << 2 << endl; n       cout << dfn[x] - dfn[v] + 1 << endl; n       while (1) { n         cout << st.top() <<    ; n         if (st.top() == v) { n           exit(0); n         } n         st.pop(); n       } n     } n   } n   st.pop(); n } n void dfs2(int x, int fa, int co) { n   vt2[co].push_back(x); n   color[x] = co; n   if (vt2[co].size() == k) { n     for (int i = 0; i < vt2[co].size(); i++) { n       cout << vt2[co][i] <<    ; n     } n     exit(0); n   } n   for (int i = 0; i < vt[x].size(); i++) { n     int v = vt[x][i]; n     if (v == fa || color[v] != -1) continue; n     dfs2(v, x, co ^ 1); n   } n } n void solve() { n   memset(color, -1, sizeof color); n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     vt[x].push_back(y); n     vt[y].push_back(x); n   } n   dfs(1, 0); n   k = (k + 1) / 2; n   cout << 1 << endl; n   dfs2(1, 0, 0); n } n int main() { n   ios::sync_with_stdio(0); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n struct Edge { n   int to, cap, flow; n }; n struct Graph { n   int n; n   vector<vector<int> > e; n   vector<Edge> edges; n   vector<int> d, c; n   Graph() {} n   Graph(int _n) { n     n = _n; n     e.resize(n); n   } n   void addEdge(int from, int to, int cap) { n     e[from].push_back(edges.size()); n     edges.push_back(Edge({to, cap, 0})); n     e[to].push_back(edges.size()); n     edges.push_back(Edge({from, 0, 0})); n   } n   bool bfs() { n     d.assign(n, INF); n     c.assign(n, 0); n     vector<int> q(n); n     int qL = 0, qR = 0; n     d[0] = 0; n     q[qR++] = 0; n     while (qL < qR) { n       int v = q[qL++]; n       for (int i = 0; i < (int)e[v].size(); i++) { n         Edge cur = edges[e[v][i]]; n         if (d[cur.to] > d[v] + 1 && cur.flow < cur.cap) { n           d[cur.to] = d[v] + 1; n           q[qR++] = cur.to; n         } n       } n     } n     return d[n - 1] != INF; n   } n   int dfs(int v, int flow) { n     if (v == n - 1) return flow; n     if (flow == 0) return 0; n     for (int &i = c[v]; i < (int)e[v].size(); i++) { n       Edge cur = edges[e[v][i]]; n       if (d[cur.to] != d[v] + 1) continue; n       int pushed = dfs(cur.to, min(flow, cur.cap - cur.flow)); n       if (pushed > 0) { n         edges[e[v][i]].flow += pushed; n         edges[e[v][i] ^ 1].flow -= pushed; n         return pushed; n       } n     } n     return 0; n   } n   long long flow() { n     long long flow = 0; n     while (bfs()) { n       while (int pushed = dfs(0, INF)) { n         flow += pushed; n       } n     } n     return flow; n   } n }; n int main() { n   int n, m, h; n   while (cin >> n >> h >> m) { n     vector<int> l(m), r(m), x(m), c(m); n     for (int i = 0; i < m; i++) { n       cin >> l[i] >> r[i] >> x[i] >> c[i]; n       x[i]++; n       l[i]--; n       r[i]--; n     } n     Graph gr(1 + n * (h + 1) + m + 1); n     int sz = gr.n; n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       for (int j = 1; j <= h; j++) { n         gr.addEdge(1 + i * (h + 1) + (j - 1), 1 + i * (h + 1) + j, INF); n         gr.addEdge(1 + i * (h + 1) + j, sz - 1, 2 * j - 1); n         sum += 2 * j - 1; n       } n     } n     for (int i = 0; i < m; i++) { n       if (x[i] > h) continue; n       for (int t = l[i]; t <= r[i]; t++) { n         gr.addEdge(1 + n * (h + 1) + i, 1 + t * (h + 1) + x[i], INF); n       } n       gr.addEdge(0, 1 + n * (h + 1) + i, c[i]); n     } n     cout << sum - gr.flow() << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n char s[4005], t[4005], ax[4005]; n int main() { n   scanf( %s%s , s, t); n   int n = strlen(s), m = strlen(t), ret = m; n   for (int i = n - 1; i >= 0; i--) s[i + m] = s[i], s[i] = 0; n   n += m; n   for (int i = 0; i < n; i++) { n     int ct = 0; n     for (int j = 0; j < m; j++) { n       if (i + j >= n || s[i + j] != t[j]) ct++; n     } n     ret = min(ret, ct); n     ct = 0; n     for (int j = 0; j < m; j++) { n       if (i - j < 0 || s[i - j] != t[m - 1 - j]) ct++; n     } n     ret = min(ret, ct); n   } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n const int MM = 998244353; n const long double PI = acos(-1); n const long long INF = 2e18; n template <typename T, typename T1> n void amax(T &a, T1 b) { n   if (b > a) a = b; n } n template <typename T, typename T1> n void amin(T &a, T1 b) { n   if (b < a) a = b; n } n long long power(long long b, long long e) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b, e / 2); n   return power(b * b, e / 2); n } n long long power(long long b, long long e, long long m) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b % m, e / 2, m) % m; n   return power(b * b % m, e / 2, m); n } n long long modinv(long long a, long long m) { return power(a, m - 2, m); } n int TLE_TERROR() { n   long long n, p, f = 0, zz = -1; n   cin >> n >> p; n   string s; n   cin >> s; n   if (p == 1) { n     cout <<  NO ; n     return 0; n   } n   if (p == 2) { n     if (n > 2) n       cout <<  NO ; n     else if (n == 1) { n       if (s[0] ==  a ) n         cout <<  b ; n       else n         cout <<  NO ; n     } else { n       if (s ==  ab ) n         cout <<  ba ; n       else n         cout <<  NO ; n     } n     return 0; n   } n   for (long long i = n - 1; i >= 0; i--) { n     char z = s[i]; n     while ((z -  a  + 1) < p) { n       z++; n       if (i > 0 && z == s[i - 1]) continue; n       if (i > 1 && z == s[i - 2]) continue; n       long long q = n - i; n       s[i] = z; n       f = 1; n       zz = i; n       break; n     } n     if (f == 1) break; n   } n   if (f == 0) n     cout <<  NO ; n   else { n     for (long long i = zz + 1; i < n; i++) { n       if (i > 1) { n         if (s[i - 1] !=  a  && s[i - 2] !=  a ) n           s[i] =  a ; n         else if (s[i - 1] !=  b  && s[i - 2] !=  b ) n           s[i] =  b ; n         else n           s[i] =  c ; n       } else { n         if (s[i - 1] !=  a ) n           s[i] =  a ; n         else if (s[i - 1] !=  b ) n           s[i] =  b ; n         else n           s[i] =  c ; n       } n     } n     cout << s; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int TESTS = 1; n   while (TESTS--) TLE_TERROR(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bit[65], f[65][120][2]; n inline long long dp(long long s, int tot, int h1, int h2) { n   memset(f, 0, sizeof f); n   f[0][0][0] = 1; n   int ed = log2(s); n   for (int i = 1; i <= ed; ++i) { n     long long d = (s >> i) & 1; n     int ed = i * 2 - 2; n     for (int j = 0; j <= ed; ++j) n       for (int k = 0; k <= 1; ++k) n         for (int p1 = 0; p1 <= 1; ++p1) n           for (int p2 = 0; p2 <= 1; ++p2) n             if ((i < h1 || p1 == 0) && (i < h2 || p2 == 0) && n                 (p1 + p2 + k) % 2 == d) n               f[i][j + p1 + p2][(k + p1 + p2) / 2] += f[i - 1][j][k]; n   } n   return f[ed][tot][0]; n } n int main() { n   long long x, s, res, ans = 0; n   int ed = 0; n   scanf( %lld , &s); n   bit[0] = 1; n   while (bit[ed] <= s) bit[++ed] = bit[ed - 1] << 1; n   for (int i = 1; i <= ed; ++i) { n     if (bit[i] - 1 > s) break; n     x = s % (bit[i] - 1); n     for (int j = i; j >= 1; --j) n       if (x >= bit[j] - 1) x -= bit[j] - 1; n     if (!x) ++ans; n   } n   for (int h1 = 1; h1 <= ed - 1; ++h1) { n     for (int h2 = 1; bit[h2] - 1 <= s && h2 < ed; ++h2) { n       x = (s - bit[h2] + 1) / (bit[h1 + 1] + bit[h2 + 1] - 3); n       if (x > 0) { n         res = (s - bit[h2] + 1) % (bit[h1 + 1] + bit[h2 + 1] - 3); n         if (res == 0) { n           ++ans; n           continue; n         } n         if (h1 == 1 && h2 == 1) { n           ans += (s == x * 5 + 1); n           continue; n         } n         for (int i = 1; i <= h1 + h2; ++i) n           if ((res + i) % 2 == 0) ans += dp(res + i, i, h1, h2); n       } n     } n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   point() : x(0), y(0) {} n   point(int x, int y) : x(x), y(y) {} n   point operator-(const point& p) const { return point(x - p.x, y - p.y); } n   int dot(const point& p) const { return x * p.x + y * p.y; } n   int cross(const point& p) const { return x * p.y - y * p.x; } n }; n istream& operator>>(istream& is, point& p) { return is >> p.x >> p.y; } n bool pointInSegment(const point& a, const point& b, const point& p) { n   return (p - a).cross(b - a) == 0 && (a - p).dot(b - p) <= 0; n } n bool pointInPerimeter(const vector<point>& P, const point& p) { n   int n = P.size(); n   for (int i = 0; i < n; i++) { n     if (pointInSegment(P[i], P[(i + 1) % n], p)) { n       return true; n     } n   } n   return false; n } n bool crossesRay(const point& a, const point& b, const point& p) { n   return ((b.y >= p.y) - (a.y >= p.y)) * (a - p).cross(b - p) >= 0; n } n bool pointInPolygon(const vector<point>& P, const point& p) { n   if (pointInPerimeter(P, p)) { n     return true; n   } n   int n = P.size(); n   int rays = 0; n   for (int i = 0; i < n; i++) { n     rays += crossesRay(P[i], P[(i + 1) % n], p); n   } n   return rays & 1; n } n int sgn(int x) { n   if (x > 0) return 1; n   if (x < 0) return -1; n   return 0; n } n int intersectSegmentsInfo(const point& a, const point& b, const point& c, n                           const point& d) { n   point v1 = b - a, v2 = d - c; n   int t = sgn(v1.cross(c - a)), u = sgn(v1.cross(d - a)); n   if (t == u) { n     if (t == 0) { n       if (pointInSegment(a, b, c) || pointInSegment(a, b, d) || n           pointInSegment(c, d, a) || pointInSegment(c, d, b)) { n         return -1; n       } else { n         return 0; n       } n     } else { n       return 0; n     } n   } else { n     return sgn(v2.cross(a - c)) != sgn(v2.cross(b - c)); n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   vector<point> a(4), b(4); n   for (point& p : a) cin >> p; n   for (point& p : b) cin >> p; n   bool ans = false; n   for (const point& p : a) { n     ans = ans | pointInPolygon(b, p); n   } n   for (const point& p : b) { n     ans = ans | pointInPolygon(a, p); n   } n   for (int i = 0; i < 4; ++i) { n     for (int j = 0; j < 4; ++j) { n       ans = ans | (intersectSegmentsInfo(a[i], a[(i + 1) % 4], b[j], n                                          b[(j + 1) % 4]) != 0); n     } n   } n   cout << (ans ?  YES  :  NO ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   int flag = 0, f = 0; n   cin >> s1 >> s2; n   int l = s1.length(); n   for (int i = 0; i < l; i++) { n     if (s1[i] <  a ) { n       s1[i] += 32; n     } n     if (s2[i] <  a ) s2[i] += 32; n     if (s1[i] != s2[i]) { n       f = i; n       flag = 1; n       break; n     } n   } n   if (flag == 0) n     cout <<  0 ; n   else { n     if (s1[f] > s2[f]) n       cout <<  1 ; n     else n       cout <<  -1 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, f[33]; n double ans = 0; n vector<int> v[33]; n string s; n template <class T> n ostream& operator<<(ostream& out, vector<T> v) { n   out << v.size() <<   n ; n   for (auto e : v) out << e <<    ; n   return out; n } n double check(int k) { n   double ans = 0, local; n   for (int i = 0; i < n; i++) { n     memset(f, 0, sizeof f); n     for (auto j : v[k]) { n       f[s[(j + i) % n] -  a ]++; n     } n     local = 0; n     for (int i = 0; i <=  z  -  a ; i++) n       if (f[i] == 1) local += (double)1. / v[k].size(); n     ans = max(ans, local); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s; n   n = s.size(); n   for (int i = 0; i < s.size(); i++) v[s[i] -  a ].push_back(i); n   for (int i = 0; i <=  z  -  a ; i++) { n     ans += ((double)v[i].size() / n) * check(i); n   } n   cout << fixed << setprecision(13) << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n long long sqr(long long n) { return n * n; } n long long binpow(long long a, long long n) { n   if (n == 0) return 1; n   if (n % 2 != 0) n     return binpow(a, n - 1) * a; n   else { n     long long b = binpow(a, n / 2); n     return b * b; n   } n } n long long gcd(long long a, long long b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n int n; n vector<string> v; n string ans; n bool check(string s1, string s2) { n   string s = s1 + s2.substr(n - 2); n   multiset<string> v1, pref, suff; n   for (int i = 0; i < n - 1; i++) { n     pref.insert(s.substr(0, n - i - 1)); n     v1.insert(s.substr(0, n - i - 1)); n     suff.insert(s.substr(i + 1)); n     v1.insert(s.substr(i + 1)); n   } n   if (v1 == multiset<string>(v.begin(), v.end())) { n     for (int i = 0; i < 2 * n - 2; i++) { n       if (pref.count(v[i])) { n         ans +=  P ; n         pref.erase(v[i]); n       } else if (suff.count(v[i])) { n         ans +=  S ; n         suff.erase(v[i]); n       } else { n         ans =   ; n         return false; n       } n     } n     return true; n   } n   return false; n } n void solve() { n   cin >> n; n   v = vector<string>(2 * n - 2); n   string mx1 =   , mx2 =   ; n   for (int i = 0; i < 2 * n - 2; i++) { n     cin >> v[i]; n     if (v[i].size() == n - 1 && mx1.size() == 0) n       mx1 = v[i]; n     else if (v[i].size() == n - 1 && mx2.size() == 0) n       mx2 = v[i]; n   } n   if (check(mx1, mx2)) n     cout << ans << endl; n   else { n     check(mx2, mx1); n     cout << ans << endl; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, i, e, f, g, n, m, k, l, A[500005], B[500005], fix[500005], n     root, ans, C[500005]; n vector<long long> v[500005]; n void dfs(long long x, long long y) { n   for (long long i = 0; i < v[x].size(); i++) { n     if (v[x][i] == y) continue; n     dfs(v[x][i], x); n     if (fix[v[x][i]] == 0) C[x]++; n   } n   if (C[x] == 0) return; n   root = x; n   fix[x] = 1; n   for (long long i = 0; i < v[x].size(); i++) { n     if (v[x][i] == y) continue; n     if (fix[v[x][i]] == 1) continue; n     fix[v[x][i]] = 1; n     swap(A[B[root]], A[B[v[x][i]]]); n     ans += 2; n     a = B[root]; n     B[root] = B[v[x][i]]; n     B[v[x][i]] = a; n     root = v[x][i]; n   } n } n int main() { n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     A[i] = i; n     B[i] = i; n   } n   for (long long i = 1; i < n; i++) { n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   dfs(1, 0); n   if (fix[1] == 0) { n     a = v[1][0]; n     swap(A[1], A[B[v[1][0]]]); n     ans += 2; n   } n   cout << ans << endl; n   for (long long i = 1; i <= n; i++) cout << A[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 101; n int n, m; n struct price { n   int p; n   int au; n   bool operator<(const price &rhs) const { n     if (au == rhs.au) return p > rhs.p; n     return au < rhs.au; n   } n } price[MAX_N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &price[i].p); n   } n   for (int i = 0; i < m; ++i) { n     int x; n     scanf( %d , &x); n     price[x].au = 1; n   } n   sort(price + 1, price + n + 1); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     if (price[i].au == 0) { n       ans += price[i].p; n     } else { n       if (ans > price[i].p) n         ans *= 2; n       else n         ans += price[i].p; n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int N = 2e5 + 6; n int cnt[666], dp[777][777]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int _, n; n   cin >> _; n   while (_--) { n     cin >> n; n     for (int i = 0; i <= n - 1; i++) cin >> cnt[i]; n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= n * 2; j++) dp[i][j] = 1e9; n     sort(cnt, cnt + n); n     int ans = 1e9; n     dp[0][0] = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n * 2; j++) { n         for (int k = 0; k <= j - 1; k++) { n           dp[i][j] = min(dp[i - 1][k] + abs(cnt[i - 1] - j), dp[i][j]); n           if (n - i == 0) ans = min(ans, dp[i][j]); n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool calculate_digits(int a) { n   int sum = 0; n   while (a > 0) { n     sum += (a % 10); n     a = a / 10; n   } n   if (sum % 4 == 0) { n     return true; n   } else { n     return false; n   } n } n int main() { n   int a; n   cin >> a; n   for (int i = a; i <= a + 10; i++) { n     if (calculate_digits(i)) { n       cout << i << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, res = 0; n   cin >> n >> k; n   vector<int> arrAB; n   vector<int> arrA; n   vector<int> arrB; n   for (int i = 0; i < n; i++) { n     int time, a, b; n     cin >> time >> a >> b; n     if (a == 1 && b == 1) { n       arrAB.push_back(time); n     } else if (a == 1) { n       arrA.push_back(time); n     } else if (b == 1) { n       arrB.push_back(time); n     } n   } n   sort(arrA.begin(), arrA.end()); n   sort(arrB.begin(), arrB.end()); n   for (int i = 0; i < arrA.size() && i < arrB.size(); i++) { n     arrAB.push_back(arrA[i] + arrB[i]); n   } n   sort(arrAB.begin(), arrAB.end()); n   if (arrAB.size() < k) n     cout <<  -1  << endl; n   else { n     for (int i = 0; i < k; i++) res += arrAB[i]; n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 100000; n int n, k; n char s[max_n + 5]; n void solve(void); n int main(void) { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n void solve(void) { n   scanf( %d%d , &n, &k); n   scanf( %s , s); n   int i, j, t, ans = 0; n   int lcnt = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  W ) { n       if (i > 0 && s[i - 1] ==  W ) n         ans += 2; n       else n         ans++; n     } else n       lcnt++; n   } n   if (lcnt == n) { n     if (!k) n       printf( 0 n ); n     else n       printf( %d n , 2 * k - 1); n     return; n   } n   i = 0; n   vector<int> gap; n   while (i < n) { n     if (s[i] ==  L ) { n       i++; n       continue; n     } n     j = i; n     while (j < n && s[j] ==  W ) j++; n     t = j; n     while (t < n && s[t] ==  L ) t++; n     if (t >= n) break; n     gap.push_back(t - j); n     i = t; n   } n   sort(gap.begin(), gap.end()); n   for (i = 0; k && i < (int)gap.size(); i++) { n     if (gap[i] <= k) { n       k -= gap[i]; n       lcnt -= gap[i]; n       ans += 2 * gap[i] + 1; n     } else { n       lcnt -= k; n       ans += 2 * k; n       k = 0; n     } n   } n   if (k) { n     ans += min(k, lcnt) * 2; n   } n   printf( %d n , ans); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, p, q, r; n   cin >> n >> p >> q >> r; n   long long int a[n], ans = LLONG_MIN; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   long long int prefixMax[n], suffixMax[n]; n   prefixMax[0] = p * a[0]; n   suffixMax[n - 1] = r * a[n - 1]; n   for (long long int i = 1; i < n; i++) n     prefixMax[i] = max(prefixMax[i - 1], p * a[i]); n   for (long long int i = n - 2; i >= 0; i--) n     suffixMax[i] = max(suffixMax[i + 1], r * a[i]); n   for (long long int i = 0; i < n; i++) { n     ans = max(ans, prefixMax[i] + suffixMax[i] + (q * a[i])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const double PI = 3.141592653589793; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n struct Pt { n   double x, y; n   Pt(double a = 0, double b = 0) : x(a), y(b) {} n   bool operator==(const Pt& p) const { return p.x == x && p.y == y; } n   bool operator<(const Pt& p) const { return x == p.x ? y > p.y : x < p.x; } n   Pt operator-(const Pt& p) const { return {x - p.x, y - p.y}; } n   Pt operator+(const Pt& p) const { return {x + p.x, y + p.y}; } n   Pt operator/(const double c) const { return {x / c, y / c}; } n   Pt operator*(const double c) const { return {x * c, y * c}; } n   Pt vetor(const Pt& o) const { return o - (*this); } n   double dot(const Pt& b) const { return (x * b.x) + (y * b.y); } n   double sq() const { return (x * x) + (y * y); } n   double dist(const Pt& b) { return sqrt((b - *this).sq()); } n   double cross(const Pt& b) const { return (x * b.y) - (y * b.x); } n   double cross(const Pt& a, const Pt& b) const { n     return (a - *this).cross(b - *this); n   } n   double angle(Pt& a, Pt& b) const { n     a = vetor(a), b = vetor(b); n     return acos(1.0 * a.dot(b) / sqrt(1.0 * a.dot(a) * b.dot(b))); n   } n } pts[101000]; n Pt obj; n Pt perp(Pt p) { return {-p.y, p.x}; } n ostream& operator<<(ostream& os, Pt p) { n   return os <<  (  << p.x <<  ,  << p.y <<  ) ; n } n template <typename F> n int sgn(F x) { n   return (F(0) < x) - (x < F(0)); n } n struct Line { n   double A, B, C; n   Line() {} n   Line(const Pt& de, const Pt& pa) { n     A = de.y - pa.y; n     B = pa.x - de.x; n     C = -A * de.x - B * de.y; n   } n }; n bool mORe(double a, double b) { return a <= b || fabs(a - b) <= EPS; } n struct Seg { n   Pt A, B; n   Seg() {} n   Seg(Pt a, Pt b) : A(a), B(b) { n     if (B < A) swap(A, B); n   } n   bool inDouble(const Pt& Pt) { n     return mORe(min(A.x, B.x), Pt.x) && mORe(Pt.x, max(A.x, B.x)) && n            mORe(min(A.y, B.y), Pt.y) && mORe(Pt.y, max(A.y, B.y)); n   } n   bool inter(Seg& o, Pt& res) { n     Line l1 = Line(A, B), l2 = Line(o.A, o.B); n     double dx = Pt(l1.C, l1.B).cross(Pt(l2.C, l2.B)); n     double dy = Pt(l1.A, l1.C).cross(Pt(l2.A, l2.C)); n     double d = Pt(l1.A, l1.B).cross(Pt(l2.A, l2.B)); n     if (d == 0) return false; n     res = Pt(-dx / d, -dy / d); n     if (!inDouble(res) || !o.inDouble(res)) return false; n     return true; n   } n }; n int n, p, q; n void solve() { n   vector<Pt> chain; n   int sz = 0; n   for (int i = 0; i < n; i++) { n     while (sz >= 2 && chain[sz - 2].cross(chain[sz - 1], pts[i]) >= 0) n       chain.pop_back(), sz--; n     chain.emplace_back(pts[i]); n     sz++; n   } n   vector<Seg> linhas; n   for (int i = 1; i < chain.size(); i++) { n     linhas.emplace_back(chain[i - 1], chain[i]); n   } n   Seg teste = {Pt(0, 0), obj * 1000002.0}; n   for (Seg linha : linhas) { n     Pt res; n     if (linha.inter(teste, res)) { n       printf( %.12f n , max(obj.x / res.x, obj.y / res.y)); n       return; n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &p, &q); n   obj = {(double)p, (double)q}; n   int x, y; n   int maxx = 0, maxy = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x, &y); n     pts[i] = {(double)x, (double)y}; n     maxx = max(maxx, x); n     maxy = max(maxy, y); n   } n   pts[n] = {0.0, (double)maxy}; n   pts[n + 1] = {(double)maxx, 0.0}; n   n += 2; n   sort(pts, pts + n); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   while (cin >> a >> b >> c >> d) { n     cout << (((a ^ b) & (c | d)) ^ ((b & c) | (a ^ d))) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void exgcd(int a, int b, int *x, int *y) { n   if (b) { n     exgcd(b, a % b, y, x); n     (*y) = (*y) - (a / b) * (*x); n   } else n     *x = 1, *y = 0; n } n int main() { n   std::ios::sync_with_stdio(false); n   double r1, r2, c1, c2, d1, d2; n   cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; n   double a, b, c, d; n   b = (r1 + c2 - d1) / 2.0; n   a = r1 - b; n   d = c2 - b; n   c = d2 - b; n   if ((int)a == a && (int)b == b && (int)c == c && (int)d == d && a + b == r1 && n       c + d == r2 && a + d == d1 && c + b == d2 && a + c == c1 && b + d == c2) { n     set<int> temp; n     temp.insert(a); n     temp.insert(b); n     temp.insert(c); n     temp.insert(d); n     if (temp.size() == 4 && *(temp.begin()) > 0 && *(temp.rbegin()) < 10) { n       cout << (int)a <<     << (int)b << endl; n       cout << (int)c <<     << (int)d << endl; n     } else n       cout << -1; n   } else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int x, y, id; n }; n bool operator<(const Node &s, const Node &t) { n   if (s.x != t.x) n     return s.x < t.x; n   else n     return s.y < t.y; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<Node> vn; n   vector<int> ans; n   set<int> se; n   for (int i = 0; i < m; i++) { n     Node no; n     scanf( %d%d , &(no.x), &(no.y)); n     no.id = i + 1; n     vn.push_back(no); n   } n   sort(vn.begin(), vn.end()); n   for (int i = 0; i < n; i++) se.insert(i); n   for (int i = 0; i < m; i++) { n     set<int>::iterator it = se.lower_bound(n - vn[i].y); n     if (it == se.end()) continue; n     if (*it < vn[i].x) { n       se.erase(it); n       ans.push_back(vn[i].id); n     } n   } n   sort(ans.begin(), ans.end()); n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } n long long power(long long a, long long n) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n     } n     n >>= 1; n     a *= a; n   } n   return p; n } n long long power(long long a, long long n, long long mod) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n       p %= mod; n     } n     n >>= 1; n     a *= a; n     a %= mod; n   } n   return p % mod; n } n using namespace std; n int a[100004], b[100004], c[100004]; n vector<int> p; n void sieve() { n   int i, j; n   for (i = 2; i * i <= 100000; i++) n     if (!b[i]) n       for (j = i * i; j <= 100000; j += i) b[j] = 1; n   for (i = 2; i <= 100000; i++) n     if (!b[i]) p.push_back(i); n } n int main() { n   int i, j; n   int n; n   sieve(); n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   int x, y; n   for (i = 0; i < n; i++) { n     x = a[i]; n     for (j = 0; p[j] * p[j] <= a[i]; j++) { n       if (x % p[j] == 0) c[p[j]]++; n       while (x % p[j] == 0) x = x / p[j]; n     } n     if (x > 1) c[x]++; n   } n   x = 1; n   for (i = 0; i < p.size(); i++) x = max(x, c[p[i]]); n   printf( %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str1, str2; n int main() { n   cin >> str1 >> str2; n   if (str1 == str2) n     cout << str1; n   else n     puts( 1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = unsigned long long; n ll a[5], b[5], c[5], d[5], fl, k, cnt, vis[10]; n set<pair<ll, ll> > st; n map<pair<ll, ll>, ll> mp; n vector<ll> G[10]; n set<ll> stt; n void dfs(ll u = 1, ll par = -1) { n   vis[u] = 1; n   cnt++; n   for (ll v : G[u]) { n     if (par == v) continue; n     if (vis[v]) return; n     dfs(v, u); n   } n } n int main() { n   while (cin >> a[0] >> b[0] >> c[0] >> d[0]) { n     st.clear(); n     stt.clear(); n     for (int i = 1; i < 4; i++) cin >> a[i] >> b[i] >> c[i] >> d[i]; n     fl = 1; n     for (int i = 0; i < 4; i++) { n       if (a[i] != c[i] && b[i] != d[i]) fl = 0; n     } n     k = 0; n     for (int i = 0; i < 10; i++) G[i].clear(); n     for (int i = 0; i < 10; i++) vis[i] = 0; n     for (int i = 0; i < 4; i++) { n       if (mp.find({a[i], b[i]}) == mp.end()) { n         mp[{a[i], b[i]}] = ++k; n       } n       if (mp.find({c[i], d[i]}) == mp.end()) { n         mp[{c[i], d[i]}] = ++k; n       } n       st.insert({a[i], b[i]}); n       st.insert({c[i], d[i]}); n       G[mp[{a[i], b[i]}]].push_back(mp[{c[i], d[i]}]); n       G[mp[{c[i], d[i]}]].push_back(mp[{a[i], b[i]}]); n     } n     if (st.size() != 4) fl = 0; n     for (int i = 0; i < 4; i++) { n       stt.insert((a[i] - c[i]) * (a[i] - c[i]) + (b[i] - d[i]) * (b[i] - d[i])); n     } n     if (stt.size() > 2) fl = 0; n     cnt = 0; n     dfs(); n     if (cnt != 4) fl = 0; n     puts(fl ?  YES  :  NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int d, n, c, ans, m; n   string arr[105]; n   cin >> n >> d; n   ans = 0; n   m = 0; n   for (int i = 0; i < d; i++) { n     cin >> arr[i]; n     c = 0; n     for (int j = 0; j < n; j++) { n       if (arr[i][j] ==  1 ) c++; n     } n     if (c < n) n       m++; n     else n       m = 0; n     if (m > ans) ans = m; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int get_inv(int x) { n   return (x == 1) ? 1 : (mod - (long long)(mod / x) * get_inv(mod % x) % mod); n } n const int MaxN = 200010, MaxT = 1 << 19; n struct tree_node { n   int k, sum; n   int lazy; n   tree_node() { k = lazy = 1; } n } T[MaxT]; n int v[MaxN], deg[MaxN]; n class Graph { n  public: n   int en[MaxN], next[MaxN], point[MaxN], tot; n   void add_edge(int x, int y) { n     next[++tot] = en[x]; n     en[x] = tot; n     point[tot] = y; n   } n } G; n int in[MaxN], out[MaxN]; n int n, q; n int dfn; n void dfs(int now) { n   in[now] = ++dfn; n   for (int i = G.en[now]; i; i = G.next[i]) dfs(G.point[i]); n   out[now] = dfn; n } n inline int mul(int &x, int v) { return (x = (long long)x * v % mod); } n inline int inc(int x, int v) { n   x += v; n   return x >= mod ? x - mod : x; n } n void add_lazy(int now, int lazy) { n   mul(T[now].lazy, lazy); n   mul(T[now].k, lazy); n   mul(T[now].sum, lazy); n } n void push_down(int now) { n   if (T[now].lazy != 1) { n     add_lazy(now << 1, T[now].lazy); n     add_lazy(now << 1 | 1, T[now].lazy); n     T[now].lazy = 1; n   } n } n void update(int now) { T[now].sum = inc(T[now << 1].sum, T[now << 1 | 1].sum); } n void modify_v(int l, int r, int x, int v, int now) { n   if (l == r) { n     T[now].sum = 1ll * T[now].k * v % mod; n     return; n   } n   int mid = (l + r) >> 1; n   push_down(now); n   if (x <= mid) n     modify_v(l, mid, x, v, now << 1); n   else n     modify_v(mid + 1, r, x, v, now << 1 | 1); n   update(now); n } n void modify_k(int l, int r, int x, int y, int k, int now) { n   if ((r < x) || (y < l)) return; n   if ((x <= l) && (r <= y)) return add_lazy(now, k); n   int mid = (l + r) >> 1; n   push_down(now); n   modify_k(l, mid, x, y, k, now << 1); n   modify_k(mid + 1, r, x, y, k, now << 1 | 1); n   update(now); n } n int query_k(int l, int r, int x, int now) { n   if (l == r) return T[now].k; n   int mid = (l + r) >> 1; n   push_down(now); n   if (x <= mid) n     return query_k(l, mid, x, now << 1); n   else n     return query_k(mid + 1, r, x, now << 1 | 1); n } n int query_sum(int l, int r, int x, int y, int now) { n   if ((r < x) || (y < l)) return 0; n   if ((x <= l) && (r <= y)) return T[now].sum; n   int mid = (l + r) >> 1; n   push_down(now); n   return inc(query_sum(l, mid, x, y, now << 1), n              query_sum(mid + 1, r, x, y, now << 1 | 1)); n } n void debug() { n   for (int i = 1; i <= 17; ++i) n     printf( %d: %d %d %d n , i, T[i].sum, T[i].k, T[i].lazy); n } n int main() { n   static int type[MaxN], f[MaxN], u[MaxN]; n   scanf( %d%d , v + 1, &q); n   n = 1; n   for (int i = 1; i <= q; ++i) { n     scanf( %d , type + i); n     if (type[i] == 1) { n       u[i] = ++n; n       scanf( %d%d , &f[n], &v[n]); n       G.add_edge(f[n], n); n     } else n       scanf( %d , u + i); n   } n   dfs(1); n   modify_v(1, n, in[1], v[1], 1); n   deg[1] = 1; n   for (int i = 1; i <= q; ++i) n     if (type[i] == 2) { n       int ans = query_sum(1, n, in[u[i]], out[u[i]], 1), k; n       if (u[i] == 1) n         k = 1; n       else n         k = query_k(1, n, in[f[u[i]]], 1); n       ans = (long long)ans * get_inv(k) % mod; n       printf( %d n , ans); n     } else { n       int k = (long long)(deg[f[u[i]]] + 1) * get_inv(deg[f[u[i]]]) % mod; n       ++deg[f[u[i]]]; n       modify_k(1, n, in[f[u[i]]], out[f[u[i]]], k, 1); n       deg[u[i]] = 1; n       modify_v(1, n, in[u[i]], v[u[i]], 1); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[500005], cmd[500005]; n int baam[500005], daan[500005], jora[500005]; n int n, m, pos; n long long bit[500005]; n void update(int l, int r) { n   bit[l] += 1; n   bit[r + 1] -= 1; n } n void moveLeft() { n   if (baam[pos] > 0 && baam[pos] <= n) pos = baam[pos]; n   return; n } n void moveRight() { n   if (daan[pos] > 0 && daan[pos] <= n) pos = daan[pos]; n   return; n } n void deleteNode() { n   int nextPos, L, R; n   L = min(pos, jora[pos]); n   R = max(pos, jora[pos]); n   if (daan[R] <= n) n     pos = daan[R]; n   else n     pos = baam[L]; n   daan[baam[L]] = daan[R]; n   baam[daan[R]] = baam[L]; n   update(L, R); n   return; n } n int main() { n   scanf( %d %d %d , &n, &m, &pos); n   getchar(); n   for (int i = 1; i <= n; i++) scanf( %c , &str[i]); n   getchar(); n   stack<int> st; n   for (int i = 1; i <= n; i++) { n     if (str[i] ==  ( ) n       st.push(i); n     else { n       jora[i] = st.top(); n       jora[st.top()] = i; n       st.pop(); n     } n   } n   for (int i = 1; i <= n; i++) { n     baam[i] = i - 1; n     daan[i] = i + 1; n   } n   scanf( %s , cmd); n   for (int i = 0; cmd[i]; i++) { n     if (cmd[i] ==  L ) n       moveLeft(); n     else if (cmd[i] ==  R ) n       moveRight(); n     else n       deleteNode(); n   } n   for (int i = 1; i <= n; i++) bit[i] += bit[i - 1]; n   for (int i = 1; i <= n; i++) { n     if (!bit[i]) printf( %c , str[i]); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 2e5 + 10; n int n; n int d[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     d[u]++; n     d[v]++; n   } n   for (int i = 1; i <= n; i++) { n     if (d[i] == 2) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   vector<long long int> v; n   unordered_map<long long int, long long int> mapy; n   v.push_back(0); n   v.push_back(2); n   mapy[2] = 1; n   for (long long int i = 2; i <= 100000; i++) { n     v.push_back(((3 * (i * (i + 1))) / 2) - i); n     mapy[(((3 * (i * (i + 1))) / 2) - i)] = 1; n   } n   while (t--) { n     long long int n; n     cin >> n; n     ; n     long long int c = 0; n     while (n > 1) { n       auto f = lower_bound(v.begin(), v.end(), n); n       if (mapy[n] == 1) { n         c++; n         break; n       } n       long long int hj = f - v.begin(); n       hj--; n       n -= v[hj]; n       c++; n     } n     cout << c << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n const int _ML = 500; n const char _inpf[] =   ; n const char _outf[] =   ; n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) { n   _out << _p.x <<     << _p.y; n   return _out; n } n template <typename T, typename U> n inline istream &operator>>(istream &_in, pair<T, U> &_p) { n   _in >> _p.x >> _p.y; n   return _in; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const vector<T> &_v) { n   if (_v.empty()) return _out; n   _out << _v.front(); n   for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T> n inline istream &operator>>(istream &_in, vector<T> &_v) { n   for (auto &_i : _v) _in >> _i; n   return _in; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const set<T> &_s) { n   if (_s.empty()) return _out; n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const multiset<T> &_s) { n   if (_s.empty()) return _out; n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) { n   if (_s.empty()) return _out; n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) { n   if (_s.empty()) return _out; n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const map<T, U> &_m) { n   if (_m.empty()) return _out; n   _out <<  (  << _m.begin()->x <<  :   << _m.begin()->y <<  ) ; n   for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) n     _out <<  , (  << _it->x <<  :   << _it->y <<  ) ; n   return _out; n } n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) { n   if (_m.empty()) return _out; n   _out <<  (  << _m.begin()->x <<  :   << _m.begin()->y <<  ) ; n   for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) n     _out <<  , (  << _it->x <<  :   << _it->y <<  ) ; n   return _out; n } n inline void mainFunction(void) { n   int n, m, k; n   cin >> n >> m >> k; n   vector<string> v(n + 2, string(m + 2,  * )); n   int x = 228, n       y = 1488 +  m  +  i  +  s  +  h  +  k  +  a  +  =  +  g  +  a  +  y ; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> v[i][j]; n       if (v[i][j] ==  X ) { n         v[i][j] =  . ; n         x = i; n         y = j; n       } n     } n   } n   const int dx[] = {1, 0, 0, -1}; n   const int dy[] = {0, -1, 1, 0}; n   const int N = 1000 * 1000 + 228; n   vector<vector<int>> d(n + 2, vector<int>(m + 2, N)); n   static int qx[N]; n   static int qy[N]; n   int ql = 0; n   int qr = 1; n   qx[0] = x; n   qy[0] = y; n   d[x][y] = 0; n   while (ql != qr) { n     int xx = qx[ql]; n     int yy = qy[ql]; n     ++ql; n     for (int j = 0; j < 4; ++j) { n       if (v[xx + dx[j]][yy + dy[j]] ==  .  && d[xx + dx[j]][yy + dy[j]] == N) { n         d[xx + dx[j]][yy + dy[j]] = d[xx][yy] + 1; n         qx[qr] = xx + dx[j]; n         qy[qr] = yy + dy[j]; n         ++qr; n       } n     } n   } n   const char c[] = { D ,  L ,  R ,  U }; n   static char p[228]; n   p[ U ] =  D ; n   p[ D ] =  U ; n   p[ R ] =  L ; n   p[ L ] =  R ; n   string s; n   for (int i = 0; i < k; ++i) { n     int j; n     for (j = 0; j < 4; ++j) { n       if (v[x + dx[j]][y + dy[j]] ==  .  && n           d[x + dx[j]][y + dy[j]] <= k - i - 1) { n         break; n       } n     } n     if (j == 4) { n       cout <<  IMPOSSIBLE  <<   n ; n       return; n     } n     s += c[j]; n     x += dx[j]; n     y += dy[j]; n   } n   cout << s <<   n ; n } n int main() { n   if (_inpf[0] !=   0 ) assert(freopen(_inpf,  r , stdin) != nullptr); n   if (_outf[0] !=   0 ) assert(freopen(_outf,  w , stdout) != nullptr); n   cin.tie(nullptr); n   ios_base::sync_with_stdio(false); n   cout << setprecision(20); n   mainFunction(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   if (n == 1) { n     cout <<  1 n0 1 n0 n1 ; n     return 0; n   } n   vector<long long> p1, p2; n   p1.push_back(1); n   p1.push_back(0); n   p2.push_back(1); n   for (long long i = 1; i < n; i++) { n     vector<long long> temp = p1; n     p1.push_back(0); n     for (long long j = 0; j < p2.size(); j++) { n       p1[j + 2] += p2[j]; n       p1[j + 2] %= 2LL; n     } n     p2 = temp; n   } n   reverse(p1.begin(), p1.end()); n   reverse(p2.begin(), p2.end()); n   cout << p1.size() - 1 <<   n ; n   for (long long i = 0; i < p1.size(); i++) cout << p1[i] <<    ; n   cout <<   n ; n   cout << p2.size() - 1 <<   n ; n   for (long long i = 0; i < p2.size(); i++) cout << p2[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, d, a[100000], diff; n   long sum = 0; n   cin >> n >> d; n   for (long i = 0; i < n; i++) cin >> a[i]; n   for (long i = 1; i < n; i++) { n     if (a[i - 1] >= a[i]) { n       diff = a[i - 1] - a[i]; n       a[i] = a[i] + (diff / d + 1) * d; n       sum = sum + diff / d + 1; n     } n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 55, p = 1e9 + 7; n int n, m; n long long inv[Maxn], f[Maxn][Maxn], g[Maxn][Maxn], sum[Maxn][Maxn]; n long long get_inv(int x) { n   return x <= 1 ? 1 : (p - p / x) * get_inv(p % x) % p; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) inv[i] = get_inv(i); n   f[0][1] = sum[0][1] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i + 1; j++) n       for (int k = 1; k <= i; k++) n         (g[i][j] += sum[k - 1][j] * sum[i - k][j]) %= p; n     for (int j = 1; j <= i + 1; j++) { n       (g[i][j] += p - g[i][j + 1]) %= p; n       for (int a = n; a >= 0; a--) n         for (int b = n + 1; b >= 0; b--) { n           long long now = 1; n           for (int d = 1; i * d <= a && j * d <= b; d++) { n             (now *= inv[d] * (g[i][j] + d - 1 + p) % p) %= p; n             (f[a][b] += now * f[a - i * d][b - j * d]) %= p; n           } n         } n     } n     for (int j = i + 1; j >= 1; j--) sum[i][j] = (sum[i][j + 1] + f[i][j]) % p; n   } n   printf( %lld , f[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n; n long long mas[300007]; n long long ex[300007]; n vector<long long> s; n int main() { n   scanf( %d %d , &m, &n); n   for (int i = 0; i < m; i++) { n     scanf( %I64d , &mas[i]); n   } n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &ex[i]); n   } n   n--; n   m--; n   for (int i = 0; i < n; i++) { n     ex[i] = ex[i + 1] - ex[i]; n     s.push_back(ex[i]); n   } n   s.push_back((long long)1000000000000); n   for (int i = 0; i < m; i++) { n     mas[i] = mas[i + 1] - mas[i]; n     s.push_back(mas[i]); n   } n   m = n + m + 1; n   vector<int> z(m); n   for (int i = 1, l = 0, r = 0; i < m; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < m && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   int c = 0; n   for (int i = n; i < m; i++) n     if (z[i] >= n) c++; n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1500 + 10; n int n, x, y; n struct node { n   int xx, yy, id; n   bool operator<(const node &a) const { n     if (xx - x >= 0 && a.xx - x <= 0) return 1; n     if (xx - x <= 0 && a.xx - x >= 0) return 0; n     return (yy - y) * (long long)(a.xx - x) < (a.yy - y) * (long long)(xx - x); n   } n } p[M]; n vector<int> g[M]; n int sz[M]; n int ans[M]; n void dfs(int o, int u) { n   sz[u] = 1; n   for (int &v : g[u]) n     if (v != o) { n       dfs(u, v); n       sz[u] += sz[v]; n     } n } n void dfs2(int o, int u, int l, int r) { n   if (sz[u] == 1) { n     ans[p[l].id] = u; n     return; n   } n   int id = l; n   for (int i = l + 1; i < r; ++i) { n     if (p[i].xx < p[id].xx) { n       id = i; n     } n   } n   swap(p[id], p[l]); n   ans[p[l].id] = u; n   x = p[l].xx, y = p[l].yy; n   sort(p + l + 1, p + r); n   int sum = l + 1; n   for (int &v : g[u]) n     if (v != o) { n       dfs2(u, v, sum, sum + sz[v]); n       sum += sz[v]; n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; ++i) { n     scanf( %d%d , &u, &v); n     g[u].emplace_back(v); n     g[v].emplace_back(u); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &p[i].xx, &p[i].yy); n     p[i].id = i; n   } n   dfs(1, 1); n   dfs2(1, 1, 0, n); n   for (int i = 0; i < n; ++i) { n     printf( %d%c , ans[i],    n [i == n - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n pair<long long, long long> a[3005]; n long long v[3005]; n vector<long long> vec[3005]; n priority_queue<long long, vector<long long>, greater<long long> > mc; n bool compare(pair<long long, long long> a, pair<long long, long long> b) { n   return a > b; n } n int main() { n   cin >> n >> m; n   if (m == 1) { n     cout << 0 << endl; n     exit(0); n   }; n   long long x, y; n   for (long long i = 0; i < n; i++) { n     scanf( %I64d%I64d , &x, &y); n     x--; n     v[x]++; n     vec[x].push_back(y); n     if (x) mc.push(y); n   } n   for (long long i = 0; i < m; i++) { n     a[i].first = v[i]; n     a[i].second = i; n     sort(vec[i].begin(), vec[i].end()); n   } n   sort(a, a + m, compare); n   if (a[0].second == 0 && a[0].first > a[1].first) { n     cout << 0 << endl; n     exit(0); n   }; n   long long need = a[0].first - v[0] + 1; n   long long tneed; n   priority_queue<long long, vector<long long>, greater<long long> > tmc; n   long long res = 541885418851154LL; n   long long cost; n   for (long long i = 0; i <= a[0].first; i++) { n     cost = 0; n     while (tmc.size()) { n       tmc.pop(); n     } n     tneed = need; n     if (tneed <= 0) { n       res = 0; n       break; n     } n     long long debug = (i == 2); n     for (long long j = 0; j < m; j++) { n       long long tj = a[j].second; n       for (long long k = max(v[tj] - i, 0LL); k < v[tj]; k++) { n         if (tj != 0) tmc.push(vec[tj][k]); n       } n       if (v[tj] <= i) continue; n       for (long long k = 0; k < v[tj] - i; k++) { n         cost += vec[tj][k]; n         tneed--; n         if (tneed == 0) break; n       } n       if (tneed == 0) break; n     } n     tneed -= a[0].first - i; n     if (tneed <= 0) { n       res = min(res, cost); n       continue; n     } n     for (long long j = 0; j < tneed; j++) { n       cost += tmc.top(); n       tmc.pop(); n     } n     res = min(res, cost); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int res[100005]; n int main() { n   long long int n; n   scanf( %lld , &n); n   if (n % 4 == 2 || n % 4 == 3) { n     printf( -1 ); n     exit(0); n   } n   long long int c = 2; n   for (long long int i = 0; i < n / 2; i += 2) { n     res[i] = c; n     res[i + 1] = n - i; n     c += 2; n   } n   c = 1; n   for (long long int i = n - 2; i >= n / 2; i -= 2) { n     res[i] = c; n     res[i + 1] = n - c; n     c += 2; n   } n   if (n % 2) res[n / 2] = n / 2 + 1; n   for (long long int i = 0; i < n; i++) printf( %lld  , res[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 7; n const int maxm = 1e5 + 7; n struct Que { n   int l, r, rt, id; n }; n struct Data { n   int sum; n   int id; n   bool operator<(const Data& a) const { n     return (sum == a.sum) ? (id < a.id) : (sum > a.sum); n   } n }; n struct Edge { n   int to, last; n } E[maxm]; n vector<Que> Q[maxm]; n struct Node { n   Data val; n   int lch, rch; n } rne[maxm * 20]; n int fail[maxm]; n int ch[maxm][26]; n int len[maxm]; n int n, m; n Data ans[maxn]; n char s[maxn]; n char t[maxn]; n int rt[maxm]; n int head[maxm]; n int f[20][maxm]; n int pos[maxn]; n int Len[maxn]; n int LL[maxn]; n int last; n int root; n int cnt; n int tot; n int qvq; n void Pushup(int u) { n   if (!rne[u].lch && !rne[u].rch) return; n   if (rne[u].lch && !rne[u].rch) n     rne[u].val = rne[rne[u].lch].val; n   else if (rne[u].rch && !rne[u].lch) n     rne[u].val = rne[rne[u].rch].val; n   else n     rne[u].val = min(rne[rne[u].lch].val, rne[rne[u].rch].val); n } n void Modify(int& u, int l, int r, int x) { n   if (!u) u = ++qvq; n   if (l == r) { n     rne[u].val.sum++; n     rne[u].val.id = l; n     return; n   } n   int mid = l + r >> 1; n   if (x <= mid) n     Modify(rne[u].lch, l, mid, x); n   else n     Modify(rne[u].rch, mid + 1, r, x); n   Pushup(u); n } n void Extend(int x, int id) { n   int p = last, np = ++tot; n   last = np; n   len[np] = len[p] + 1; n   while (p && !ch[p][x]) ch[p][x] = np, p = fail[p]; n   if (!p) n     fail[np] = root; n   else { n     int q = ch[p][x]; n     if (len[q] == len[p] + 1) n       fail[np] = q; n     else { n       int nq = ++tot; n       fail[nq] = fail[q]; n       len[nq] = len[p] + 1; n       memcpy(ch[nq], ch[q], sizeof ch[q]); n       fail[np] = fail[q] = nq; n       while (p && ch[p][x] == q) ch[p][x] = nq, p = fail[p]; n     } n   } n   Modify(rt[np], 1, m, id); n } n int Merge(int x, int y, int l, int r) { n   if (!x || !y) return x + y; n   if (l == r) { n     int re = ++qvq; n     rne[re].val.sum = rne[x].val.sum + rne[y].val.sum; n     rne[re].val.id = l; n     return re; n   } n   int mid = l + r >> 1; n   int re = ++qvq; n   rne[re].lch = Merge(rne[x].lch, rne[y].lch, l, mid); n   rne[re].rch = Merge(rne[x].rch, rne[y].rch, mid + 1, r); n   Pushup(re); n   return re; n } n Data Query(int u, int l, int r, int L, int R) { n   if (l >= L && r <= R) return rne[u].val; n   int mid = l + r >> 1; n   Data re = (Data){-1, m + 1}; n   if (L <= mid) re = min(re, Query(rne[u].lch, l, mid, L, R)); n   if (R > mid) re = min(re, Query(rne[u].rch, mid + 1, r, L, R)); n   return re; n } n void Dfs(int x, int p) { n   for (int i = head[x], y; y = E[i].to, i; i = E[i].last) { n     Dfs(y, x); n     rt[x] = Merge(rt[x], rt[y], 1, m); n   } n   for (Que cur : Q[x]) { n     ans[cur.id] = Query(rt[x], 1, m, cur.l, cur.r); n   } n } n void Adde(int x, int y) { n   E[++cnt] = (Edge){y, head[x]}; n   head[x] = cnt; n } n int Read(char c = 0, int re = 0) { n   while (!isdigit(c = getchar())) n     ; n   re = c -  0 ; n   while (isdigit(c = getchar())) re = ((re << 2) + re << 1) + c -  0 ; n   return re; n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   scanf( %d , &m); n   last = tot = root = 1; n   for (int i = 1; i <= m; ++i) { n     scanf( %s , t + 1); n     int len = strlen(t + 1); n     last = 1; n     for (int j = 1; j <= len; ++j) Extend(t[j] -  a , i); n   } n   for (int i = 1; i <= tot; ++i) f[0][i] = fail[i]; n   for (int i = 0; i < 19; ++i) n     for (int x = 1; x <= tot; ++x) f[i + 1][x] = f[i][f[i][x]]; n   int x = 1, tmp = 0; n   for (int i = 1; i <= n; ++i) { n     int d = s[i] -  a ; n     if (ch[x][d]) n       tmp++, x = ch[x][d]; n     else { n       while (x && !ch[x][d]) x = fail[x]; n       if (x) n         tmp = len[x] + 1, x = ch[x][d]; n       else n         x = 1, tmp = 0; n     } n     pos[i] = x; n     Len[i] = tmp; n   } n   for (int i = 1; i <= tot; ++i) n     if (fail[i]) Adde(fail[i], i); n   int q = Read(); n   for (int i = 1; i <= q; ++i) { n     int l = Read(), r = Read(), pl = Read(), pr = Read(); n     LL[i] = l; n     if (Len[pr] < pr - pl + 1) { n       ans[i] = (Data){0, l}; n       continue; n     } n     int rt = pos[pr]; n     int LEN = pr - pl + 1; n     for (int i = 19; ~i; --i) { n       if (len[f[i][rt]] >= LEN) rt = f[i][rt]; n     } n     Q[rt].push_back((Que){l, r, rt, i}); n   } n   Dfs(1, 0); n   for (int i = 1; i <= q; ++i) { n     if (!ans[i].sum) ans[i].id = LL[i]; n     printf( %d %d n , ans[i].id, ans[i].sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5000; n int p[MAXN], size[MAXN], n, m; n int find_set(int v) { return v == p[v] ? v : p[v] = find_set(p[v]); } n void union_set(int x, int y) { n   x = find_set(x); n   y = find_set(y); n   if (x != y) { n     if (size[x] < size[y]) swap(x, y); n     p[x] = y; n     size[y] += size[x]; n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) size[i] = 1, p[i] = i; n   while (m--) { n     int x, y; n     cin >> x >> y; n     union_set(x, y); n   } n   int mx = 0; n   cin >> m; n   while (m--) { n     int x, y; n     cin >> x >> y; n     if (find_set(x) == find_set(y)) size[find_set(x)] = 0; n   } n   for (int i = 1; i <= n; i++) { n     mx = max(mx, size[find_set(i)]); n   } n   cout << mx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int MOD = 1000000009; n int parent[N]; n int Rank[N]; n int Find(int x) { n   if (parent[x] != x) { n     parent[x] = Find(parent[x]); n   } n   return parent[x]; n } n bool Union(int x, int y) { n   x = Find(x); n   y = Find(y); n   if (x == y) return false; n   if (Rank[x] < Rank[y]) { n     parent[x] = y; n   } else if (Rank[x] > Rank[y]) { n     parent[y] = x; n   } else { n     parent[x] = y; n     Rank[y]++; n   } n   return true; n } n int main() { n   int n, m, a, b; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     parent[i] = i; n   } n   int ans = 1; n   while (m--) { n     scanf( %d %d , &a, &b); n     if (!Union(a, b)) { n       ans = (ans * 2) % MOD; n     } n     printf( %d n , ans ? ans - 1 : MOD - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100005], b[2005], d[100005], z[100005], L[100005]; n inline void upd(int &a, int b) { n   if (a < b) a = b; n } n int main(void) { n   while (cin >> n >> m) { n     for (int i = int(1); i <= int(n); i++) cin >> a[i]; n     for (int i = int(1); i <= int(m); i++) cin >> b[i]; n     sort(a + 1, a + n + 1); n     sort(b + 1, b + m + 1); n     memset(d, 0, sizeof d), memset(z, 0, sizeof z), memset(L, 0, sizeof L); n     for (int i = int(1); i <= int(n); i++) { n       if (i > 1 && a[i] - 1 == a[i - 1]) n         L[i] = L[i - 1]; n       else n         L[i] = i; n       int x = upper_bound(b + 1, b + m + 1, a[i]) - b; n       upd(d[i], z[L[i] - 1]); n       for (int j = int(x - 1); j >= int(1); j--) { n         if (i <= a[i] - b[j]) break; n         upd(d[i], z[L[i - a[i] + b[j]] - 1] + x - j); n       } n       upd(z[i], max(z[i - 1], d[i])); n       for (int j = int(x); j <= int(m); j++) { n         if (i + b[j] - a[i] > n) break; n         upd(z[i + b[j] - a[i]], d[i] + j - x + 1); n       } n     } n     cout << z[n] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, r[200005], cnt = 0; n   bool valid[200005]; n   pair<int, int> p; n   pair<int, pair<int, int> > u; n   vector<int> v; n   vector<pair<int, pair<int, int> > > event; n   priority_queue<pair<int, int> > pq; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &l, &r[i]); n     event.push_back(make_pair(l, make_pair(0, i))); n     event.push_back(make_pair(r[i] + 1, make_pair(1, i))); n   } n   sort(event.begin(), event.end()); n   memset(valid, true, sizeof valid); n   for (int i = 0; i < (int)event.size(); i++) { n     u = event[i]; n     l = u.first; n     while (u.first == l) { n       if (u.second.first == 0) { n         pq.push(make_pair(r[u.second.second], u.second.second)); n         cnt++; n       } else if (valid[u.second.second]) { n         valid[u.second.second] = false; n         cnt--; n       } n       i++; n       if (i == (int)event.size()) break; n       u = event[i]; n     } n     i--; n     while (cnt > k) { n       p = pq.top(); n       pq.pop(); n       while (!valid[p.second]) { n         p = pq.top(); n         pq.pop(); n       } n       valid[p.second] = false; n       v.push_back(p.second); n       cnt--; n     } n   } n   printf( %d n , (int)v.size()); n   for (int i = 0; i < (int)v.size(); i++) { n     if (i) putchar(   ); n     printf( %d , v[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200000; n struct Splay { n   struct node { n     node *fa, *ch[2]; n     int num, key, mx, mn, tg; n   } pl[2 * MAXN + 5], *root, *NIL; n   Splay() { n     root = NIL = &pl[0]; n     NIL->ch[0] = NIL->ch[1] = NIL->fa = NIL; n     NIL->mx = -MAXN, NIL->mn = MAXN, NIL->tg = 0; n   } n   void set_child(node *x, node *y, int d) { n     if (x != NIL) x->ch[d] = y; n     if (y != NIL) y->fa = x; n   } n   void pushdown(node *x) { n     if (x->tg) { n       if (x->ch[0] != NIL) n         x->ch[0]->key += x->tg, x->ch[0]->mx += x->tg, x->ch[0]->mn += x->tg, n             x->ch[0]->tg += x->tg; n       if (x->ch[1] != NIL) n         x->ch[1]->key += x->tg, x->ch[1]->mx += x->tg, x->ch[1]->mn += x->tg, n             x->ch[1]->tg += x->tg; n       x->tg = 0; n     } n   } n   void pushup(node *x) { n     x->mn = min(x->key, min(x->ch[0]->mn, x->ch[1]->mn)); n     x->mx = max(x->key, max(x->ch[0]->mx, x->ch[1]->mx)); n   } n   void rotate(node *x) { n     node *y = x->fa; n     int d = (y->ch[1] == x); n     pushdown(y), pushdown(x); n     if (y->fa != NIL) n       set_child(y->fa, x, y->fa->ch[1] == y); n     else n       x->fa = y->fa; n     set_child(y, x->ch[!d], d); n     set_child(x, y, !d); n     if (y == root) root = x; n     pushup(y); n   } n   void splay(node *x, node *rt) { n     pushdown(x); n     while (x->fa != rt) { n       node *y = x->fa; n       if (y->fa == rt) n         rotate(x); n       else { n         if ((y->fa->ch[1] == y) == (y->ch[1] == x)) n           rotate(y); n         else n           rotate(x); n         rotate(x); n       } n     } n     pushup(x); n   } n   void debug(node *x) { n     if (x == NIL) return; n     pushdown(x); n     debug(x->ch[0]); n     printf( %d : %d %d %d | %d %d n , x - pl, x->ch[0] - pl, x->ch[1] - pl, n            x->fa - pl, x->num, x->key); n     debug(x->ch[1]); n   } n   int dist(node *x, node *y) { n     if (x == y) return 0; n     splay(x, NIL), splay(y, root); n     if (x->ch[0] == y) { n       int d = min(min(x->key, y->key), x->ch[0]->ch[1]->mn); n       return x->key + y->key - 2 * d; n     } else { n       int d = min(min(x->key, y->key), x->ch[1]->ch[0]->mn); n       return x->key + y->key - 2 * d; n     } n   } n   node *query(node *x, int d) { n     pushdown(x); n     if (x->ch[1]->mn <= d && d <= x->ch[1]->mx) n       return query(x->ch[1], d); n     else if (x->key == d) { n       splay(x, NIL); n       return x; n     } else n       return query(x->ch[0], d); n   } n   node *prev(node *x, node *rt) { n     splay(x, rt); n     node *ret = x->ch[0]; n     while (ret->ch[1] != NIL) ret = ret->ch[1]; n     splay(ret, rt); n     return ret; n   } n   node *next(node *x, node *rt) { n     splay(x, rt); n     node *ret = x->ch[1]; n     while (ret->ch[0] != NIL) ret = ret->ch[0]; n     splay(ret, rt); n     return ret; n   } n } T; n Splay::node *fir[MAXN + 5], *bac[MAXN + 5]; n int n, m, root; n vector<int> G[MAXN + 5]; n void addedge(int u, int v) { G[u].push_back(v); } n int dep[MAXN + 5], dfn[2 * MAXN + 5], dcnt; n void newnode(Splay::node *nw, int x, int k) { n   nw->ch[0] = nw->ch[1] = nw->fa = T.NIL; n   nw->num = x, nw->key = nw->mn = nw->mx = k, nw->tg = 0; n } n void dfs(int x) { n   dfn[++dcnt] = x, fir[x] = bac[x] = &T.pl[dcnt], n   newnode(&T.pl[dcnt], x, dep[x]); n   for (int i = 0; i < G[x].size(); i++) { n     int p = G[x][i]; n     dep[p] = dep[x] + 1, dfs(p); n     dfn[++dcnt] = x, bac[x] = &T.pl[dcnt], newnode(&T.pl[dcnt], x, dep[x]); n   } n } n Splay::node *build(int l, int r) { n   if (l > r) return T.NIL; n   int mid = (l + r) >> 1; n   Splay::node *p = &T.pl[mid]; n   p->ch[0] = build(l, mid - 1); n   if (p->ch[0] != T.NIL) p->ch[0]->fa = p; n   p->ch[1] = build(mid + 1, r); n   if (p->ch[1] != T.NIL) p->ch[1]->fa = p; n   T.pushup(p); n   return p; n } n bool tag[MAXN + 5]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int l; n     scanf( %d , &l); n     for (int j = 1; j <= l; j++) { n       int x; n       scanf( %d , &x); n       addedge(i, x); n       tag[x] = true; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!tag[i]) root = i; n   newnode(&T.pl[++dcnt], 0, -1), dfs(root), newnode(&T.pl[++dcnt], 0, -1); n   T.root = build(1, dcnt); n   for (int i = 1; i <= m; i++) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int v, u; n       scanf( %d%d , &v, &u); n       printf( %d n , T.dist(fir[v], fir[u])); n     } else if (op == 2) { n       int v, h; n       scanf( %d%d , &v, &h); n       T.splay(fir[v], T.NIL); n       Splay::node *u = T.query(fir[v]->ch[0], fir[v]->key - h); n       Splay::node *l = T.prev(fir[v], T.NIL), *r = T.next(bac[v], T.root); n       if (bac[l->num] == r) bac[l->num] = l; n       T.set_child(l, r->ch[1], 1); n       Splay::node *p = r->ch[0]; n       newnode(r, u->num, u->key), r->ch[0] = p; n       r->tg += 1 - h, T.pushdown(r), T.pushup(r); n       T.next(bac[u->num], T.NIL), T.splay(bac[u->num], T.root); n       bac[u->num] = r; n       T.set_child(T.root->ch[0], r, 1); n     } else if (op == 3) { n       int k; n       scanf( %d , &k); n       printf( %d n , T.query(T.root, k)->num); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct input { n   int a; n   int b; n }; n int myfunc(input x, input y) { n   if (x.a > y.a) { n     return false; n   } else if (x.a < y.a) { n     return true; n   } else { n     if (x.b > y.b) { n       return false; n     } else { n       return true; n     } n   } n } n int main() { n   int n; n   cin >> n; n   vector<input> vec; n   input x; n   for (int i = 0; i < n; i++) { n     cin >> x.a; n     x.b = i + 1; n     vec.push_back(x); n   } n   sort(vec.begin(), vec.end(), myfunc); n   int times = 0; n   unsigned long long ways = 1; n   int last = vec.at(0).a; n   bool right = false; n   int pairs = 0; n   vector<int> ans1; n   vector<int> ans2; n   vector<int> ans3; n   int i; n   for (i = 0; i < n; i++) { n     if (last == vec.at(i).a) { n       times++; n     } else { n       ways *= times; n       if (times == 1) { n         ans1.push_back(vec.at(i - 1).b); n         ans2.push_back(vec.at(i - 1).b); n         ans3.push_back(vec.at(i - 1).b); n       } else if (times == 2) { n         pairs++; n         if (pairs >= 2) { n           ans1.push_back(vec.at(i - 2).b); n           ans1.push_back(vec.at(i - 1).b); n           ans2.push_back(vec.at(i - 1).b); n           ans2.push_back(vec.at(i - 2).b); n           ans3.push_back(vec.at(i - 2).b); n           ans3.push_back(vec.at(i - 1).b); n         } else { n           ans1.push_back(vec.at(i - 2).b); n           ans1.push_back(vec.at(i - 1).b); n           ans2.push_back(vec.at(i - 2).b); n           ans2.push_back(vec.at(i - 1).b); n           ans3.push_back(vec.at(i - 1).b); n           ans3.push_back(vec.at(i - 2).b); n         } n       } else if (times >= 3) { n         for (int j = i - 4; j >= i - times; j--) { n           ans1.push_back(vec.at(j).b); n           ans2.push_back(vec.at(j).b); n           ans3.push_back(vec.at(j).b); n         } n         ans1.push_back(vec.at(i - 3).b); n         ans1.push_back(vec.at(i - 2).b); n         ans1.push_back(vec.at(i - 1).b); n         ans2.push_back(vec.at(i - 2).b); n         ans2.push_back(vec.at(i - 3).b); n         ans2.push_back(vec.at(i - 1).b); n         ans3.push_back(vec.at(i - 3).b); n         ans3.push_back(vec.at(i - 1).b); n         ans3.push_back(vec.at(i - 2).b); n       } n       times = 1; n       last = vec.at(i).a; n     } n     if (i == n - 1) { n       ways *= times; n       if (times == 1) { n         ans1.push_back(vec.at(i).b); n         ans2.push_back(vec.at(i).b); n         ans3.push_back(vec.at(i).b); n       } else if (times == 2) { n         pairs++; n         if (pairs >= 2) { n           ans1.push_back(vec.at(i - 1).b); n           ans1.push_back(vec.at(i).b); n           ans2.push_back(vec.at(i).b); n           ans2.push_back(vec.at(i - 1).b); n           ans3.push_back(vec.at(i - 1).b); n           ans3.push_back(vec.at(i).b); n         } else { n           ans1.push_back(vec.at(i - 1).b); n           ans1.push_back(vec.at(i).b); n           ans2.push_back(vec.at(i - 1).b); n           ans2.push_back(vec.at(i).b); n           ans3.push_back(vec.at(i).b); n           ans3.push_back(vec.at(i - 1).b); n         } n       } else if (times >= 3) { n         for (int j = i - 3; j > i - times; j--) { n           ans1.push_back(vec.at(j).b); n           ans2.push_back(vec.at(j).b); n           ans3.push_back(vec.at(j).b); n         } n         ans1.push_back(vec.at(i - 2).b); n         ans1.push_back(vec.at(i - 1).b); n         ans1.push_back(vec.at(i).b); n         ans2.push_back(vec.at(i - 2).b); n         ans2.push_back(vec.at(i).b); n         ans2.push_back(vec.at(i - 1).b); n         ans3.push_back(vec.at(i - 1).b); n         ans3.push_back(vec.at(i - 2).b); n         ans3.push_back(vec.at(i).b); n       } n     } n     if (ways >= 3) { n       right = true; n     } n   } n   if (!right) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n     for (int j = 0; j < n; j++) { n       cout << ans1.at(j) <<    ; n     } n     cout << endl; n     for (int j = 0; j < n; j++) { n       cout << ans2.at(j) <<    ; n     } n     cout << endl; n     for (int j = 0; j < n; j++) { n       cout << ans3.at(j) <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; 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     sort(a.begin(), a.end()); n     sort(b.rbegin(), b.rend()); n     int f = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] + b[i] > x) { n         f = 1; n         break; n       } n     } n     if (f) n       cout <<  No  << endl; n     else n       cout <<  Yes  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int tc = 1; n const int M = 1e9 + 7; n const long long Inf = (long long)2e18 + 5; n const int N = 2e5 + 5; n std::vector<long long> a[N]; n int solve() { n   int a; n   cin >> a; n   int z = 0; n   int bg = 0; n   int xx = 0; n   int mx = 0; n   long long ans = 0LL; n   for (z = 0; z < a; z++) { n     int n; n     cin >> n; n     mx = 0; n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       mx = max(mx, x); n     } n     if (z > 0) { n       if (xx > mx) { n         ans += (long long)(xx - mx) * n; n         bg += n; n       } else { n         ans += (long long)(mx - xx) * bg; n         xx = mx; n         bg += n; n       } n     } else { n       xx = mx; n       bg += n; n     } n   } n   cout << ans << endl; n   return 0; n } n int main() { n   int test = 1; n   while (test--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x1, x2, y1, y2; n   cin >> n >> m >> x1 >> y1 >> x2 >> y2; n   int dx = abs(x2 - x1); n   int dy = abs(y2 - y1); n   if (dx <= 4 && dy <= 2 || dy <= 4 && dx <= 2 || dx == 3 && dy == 3) { n     cout <<  First ; n   } else { n     cout <<  Second ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, c; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> a >> b >> c; n   if (n == 1) n     cout << 0 << endl; n   else if (c >= min(a, b)) n     cout << (n - 1) * min(a, b) << endl; n   else n     cout << min(a, b) + (n - 2) * c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long s = 0; n     for (long long i = 0; i < n; i++) { n       long long a; n       cin >> a; n       s += 2 * a; n     } n     if (n == m && n > 2) { n       cout << s << endl; n       for (long long i = 1; i < n; i++) cout << i <<     << i + 1 << endl; n       cout << n <<     << 1 << endl; n     } else n       cout <<  -1 n ; n   } n } n