#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<int, char> > pr; n bool is_prime(int n) { n   if (n == 1) return 0; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return 0; n   return 1; n } n vector<int> prime, pos[1004]; n map<int, int> mark, mark2; n map<int, int>::iterator it; n bool vis[10000]; n void solve(int n, int prm, int idx) { n   vis[prm] = 1; n   int rng = n / prm; n   for (int j = 1; j <= rng; j++) { n     int x = prm * j; n     if (!mark2[x]) pos[idx].push_back(x); n     mark2[x] = 1; n     for (int k = 2; k * k <= x; k++) { n       if (x % k == 0) { n         while (x % k == 0) x /= k; n         if (!vis[k] && k <= n) solve(n, k, idx); n       } n     } n     if (x > 1 && !vis[x]) solve(n, x, idx); n   } n } n int main() { n   int n; n   string tmp, str; n   str +=  1 ; n   cin >> tmp; n   str += tmp; n   n = tmp.size(); n   char ans[10000]; n   for (int i = 0; i <= n; i++) ans[i] =  1 ; n   for (int i = 2; i <= n / 2; i++) n     if (is_prime(i)) prime.push_back(i); n   bool flg = 1; n   int sz = 0; n   for (int i = 0; i < prime.size(); i++) n     if (!vis[prime[i]]) solve(n, prime[i], sz++); n   for (int i = 1; i <= n; i++) mark[str[i]]++; n   for (it = mark.begin(); it != mark.end(); it++) n     pr.push(make_pair(it->second, it->first)); n   for (int i = 0; i < sz; i++) { n     if (pos[i].size() <= pr.top().first) { n       for (int j = 0; j < pos[i].size(); j++) ans[pos[i][j]] = pr.top().second; n       int x = pr.top().first - pos[i].size(); n       char ch = pr.top().second; n       pr.pop(); n       pr.push(make_pair(x, ch)); n     } else { n       flg = 0; n       break; n     } n   } n   if (flg) { n     printf( YES n ); n     for (int i = 1; i <= n; i++) { n       if (ans[i] ==  1 ) { n         while (!pr.top().first) pr.pop(); n         ans[i] = pr.top().second; n         int x = pr.top().first - 1; n         char ch = pr.top().second; n         pr.pop(); n         pr.push(make_pair(x, ch)); n       } n     } n     for (int i = 1; i <= n; i++) printf( %c , ans[i]); n     printf(  n ); n   } else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, k, cnt = 0; n int a[200005], st[200005]; n struct hehe { n   int x, y; n } e[200005]; n void add(int x, int y) { n   cnt++; n   e[cnt].x = x; n   e[cnt].y = y; n } n void solve() { n   int i, j; n   int minn = 1, maxx = 1; n   a[0] = 1; n   st[0] = 1; n   for (i = 1; i <= t; i++) { n     maxx = maxx + a[i] - 1; n     st[i] = st[i - 1] + a[i - 1]; n     if (a[i] >= a[i - 1]) minn += a[i] - a[i - 1]; n   } n   if (minn > k || maxx < k) { n     printf( -1 n ); n     return; n   } n   printf( %d n , n); n   for (i = st[1]; i <= st[1] + a[1] - 1; i++) add(1, i); n   for (i = 1; i <= t - 1; i++) { n     if (minn < k) { n       for (j = 1; j <= a[i + 1] - a[i] && j <= a[i + 1]; j++) n         add(st[i], st[i + 1] + j - 1); n       int zuida; n       if (a[i + 1] >= a[i]) n         zuida = a[i] - 1; n       else n         zuida = a[i + 1] - 1; n       if (minn + zuida < k) { n         int delta = max(0, a[i + 1] - a[i]); n         minn += zuida; n         for (j = st[i + 1] + delta; j <= st[i + 1] + a[i + 1] - 1; j++) n           add(st[i], j); n       } else { n         int delta = max(0, a[i + 1] - a[i]); n         int xiaid; n         int zhiqian; n         if (a[i + 1] >= a[i]) n           zhiqian = 0; n         else n           zhiqian = a[i] - a[i + 1]; n         for (j = 1; j <= a[i] - (zhiqian + (k - minn)); j++) { n           xiaid = st[i + 1] + delta + j - 1; n           add(st[i] + j - 1, xiaid); n         } n         for (j = xiaid + 1; j <= st[i + 1] + a[i + 1] - 1; j++) add(st[i], j); n         minn = k; n       } n     } else { n       for (j = 1; j <= a[i + 1] - a[i]; j++) add(st[i], st[i + 1] + j - 1); n       int id = st[i]; n       int delta = max(0, a[i + 1] - a[i]); n       for (j = st[i + 1] + delta; j <= st[i + 1] + a[i + 1] - 1; j++) { n         add(id, j); n         id++; n       } n     } n   } n   for (i = 1; i <= cnt; i++) cout << e[i].x <<     << e[i].y << endl; n } n int main() { n   while (scanf( %d%d%d , &n, &t, &k) != EOF) { n     int i; n     cnt = 0; n     for (i = 1; i <= t; i++) scanf( %d , &a[i]); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n long long qp(long long a, long long b, long long mod) { n   long long t = 1; n   while (b) { n     if (b & 1) t = t * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return t; n } n struct DOT { n   int x; n   int y; n }; n inline void read(int &x) { n   int k = 0; n   char f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) k = k * 10 + c -  0 ; n   x = k * f; n } n void ex() { n   puts( -1 ); n   exit(0); n } n const int dx[4] = {-1, 0, 1, 0}; n const int dy[4] = {0, 1, 0, -1}; n const int inf = 0x3f3f3f3f; n const long long Linf = 0x3f3f3f3f3f3f3f3fll; n const long long mod = 1e9 + 7; n const double pi = acos(-1.0); n const double eps = 1e-7; n const int maxn = 5e5 + 344; n int n, k; n map<int, int> mp; n int a[maxn], b[maxn]; n int main() { n   scanf( %d%d , &n, &k); n   int rembk = k; n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n     mp[a[i]]++; n   } n   int tot = 0; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     if (a[i] != a[i - 1]) { n       b[++tot] = a[i]; n     } n   } n   int mov = 0; n   int mnto = b[1]; n   for (int i = 1; i <= tot; i++) { n     if (1ll * mov * (b[i] - b[i - 1]) <= 1ll * k) { n       k -= mov * (b[i] - b[i - 1]); n       mnto = b[i]; n       mov += mp[b[i]]; n     } else n       break; n   } n   mnto += k / mov; n   k = rembk; n   mov = 0; n   int mxto = b[tot]; n   b[tot + 1] = b[tot]; n   for (int i = tot; i >= 1; i--) { n     if (1ll * mov * (b[i + 1] - b[i]) <= 1ll * k) { n       k -= mov * (b[i + 1] - b[i]); n       mxto = b[i]; n       mov += mp[b[i]]; n     } else n       break; n   } n   mxto -= k / mov; n   if (mxto <= mnto) { n     if (sum % n == 0) { n       puts( 0 ); n     } else n       puts( 1 ); n   } else { n     printf( %d n , mxto - mnto); n   } n } n


#include <bits/stdc++.h> n const int MAX = 1e3 + 5; n using namespace std; n int edge, mx, ans, seen[MAX], a, n, m, k, spec[MAX], check[MAX][MAX]; n vector<int> adj[MAX]; n vector<int> cur; n void dfs(int node) { n   if (seen[node] == 1) return; n   seen[node] = 1; n   cur.push_back(node); n   for (int i = int(0); i <= int(adj[node].size() - 1); i++) dfs(adj[node][i]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = int(1); i <= int(k); i++) cin >> a, spec[a] = 1; n   for (int i = int(1); i <= int(m); i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n     check[u][v] = check[v][u] = 1; n   } n   for (int i = int(1); i <= int(n); i++) n     if (spec[i] == 1) { n       dfs(i); n       mx = max(int(cur.size()), mx); n       int comb = (cur.size() == 1 ? 0 : (cur.size() * (cur.size() - 1) / 2)); n       for (int i = int(0); i <= int(cur.size() - 1); i++) { n         for (int j = int(i + 1); j <= int(cur.size() - 1); j++) n           edge += check[cur[i]][cur[j]]; n       } n       ans += comb - edge; n       cur.clear(); n       edge = 0; n     } n   for (int i = int(1); i <= int(n); i++) { n     if (seen[i] == 1) continue; n     dfs(i); n     int comb = (cur.size() == 1 ? 0 : (cur.size() * (cur.size() - 1) / 2)); n     for (int i = int(0); i <= int(cur.size() - 1); i++) { n       for (int j = int(i + 1); j <= int(cur.size() - 1); j++) n         edge += check[cur[i]][cur[j]]; n     } n     ans += comb - edge; n     ans += mx * int(cur.size()); n     mx += cur.size(); n     cur.clear(); n     edge = 0; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int po(int a) { n   if (a == 0) return 1; n   long long int x = po(a / 2); n   x = x * x; n   x %= 1000000007; n   if (a % 2 == 0) { n     return x; n   } else { n     x = x * 2; n     x %= 1000000007; n     return x; n   } n } n int main() { n   int x, y; n   cin >> x >> y; n   if (y % x != 0) { n     cout << 0 << endl; n   } else { n     y = y / x; n     vector<int> val; n     for (int i = 1; i * i <= y; i++) { n       if (y % i == 0) { n         val.push_back(i); n         if (i * i != y) { n           val.push_back(y / i); n         } n       } n     } n     sort(val.begin(), val.end()); n     vector<int> ans(val.size(), 0); n     ans[0] = 1; n     for (int i = 1; i < val.size(); i++) { n       ans[i] = po(val[i] - 1); n       for (int j = 0; j < i; j++) { n         if (val[i] % val[j] != 0) continue; n         ans[i] -= ans[j]; n         ans[i] = ans[i] % 1000000007; n       } n     } n     if (ans[val.size() - 1] < 0) { n       ans[val.size() - 1] += 1000000007; n     } n     cout << ans[val.size() - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e6 + 5; n const double delta = 1e-6; n const long long mod = 1e9 + 7; n struct ev { n   double coord; n   int type; n   int mincost; n }; n const bool operator<(const ev& f, const ev& s) { return f.coord < s.coord; } n void init() {} n class fenwick { n   vector<int> sums; n  n  public: n   fenwick(int n) { sums.resize(n + 3); } n   void add(int pos, int delta) { n     for (pos++; pos < sums.size(); pos += pos & (-pos)) sums[pos] += delta; n   } n   int size() { return sums.size() - 2; } n   int pref_sum(int pos) { n     if (pos < -1) return 0; n     int answer = 0; n     for (pos++; pos > 0; pos -= pos & (-pos)) answer += sums[pos]; n     return answer; n   } n   int get_sum(int left, int right) { n     return pref_sum(right) - pref_sum(left - 1); n   } n }; n const bool cmppp(const pair<int, int>& f, const pair<int, int>& s) { n   return (f.second == s.second) ? (f.first < s.first) : (f.second > s.second); n } n vector<int> unique_xs; n void compress() { n   sort(unique_xs.begin(), unique_xs.end()); n   unique_xs = vector<int>(unique_xs.begin(), n                           unique(unique_xs.begin(), unique_xs.end())); n } n int get_num(int dt) { n   return lower_bound(unique_xs.begin(), unique_xs.end(), dt) - n          unique_xs.begin(); n } n class dsu { n  public: n   vector<int> a; n   vector<int> sizes; n   dsu(int n, vector<int>& sizes) : sizes(sizes) { n     a.resize(n); n     for (int i = 0; i < n; i++) { n       a[i] = i; n     } n   } n   int get_parent(int x) { return (a[x] == x) ? x : (a[x] = get_parent(a[x])); } n   void unite(int l, int r) { n     int par_1 = get_parent(l); n     int par_2 = get_parent(r); n     if (par_1 == par_2) return; n     if (sizes[par_1] < sizes[par_2]) swap(par_1, par_2); n     sizes[par_1] += sizes[par_2]; n     sizes[par_2] = 0; n     a[par_2] = par_1; n   } n }; n bool is_prime(int number) { n   for (int i = 2; i * i <= number; i++) { n     if (number % i == 0) { n       return false; n     } n   } n   return true; n } n string s; n string left_answer =   ; n string right_answer =   ; n void squeeze(int& l, int& r) { n   if (l == r) { n     left_answer += s[l]; n     l++; n     return; n   } n   if (s[l] == s[r]) { n     left_answer += s[l]; n     right_answer += s[r]; n     l++; n     r--; n     return; n   } n   if (l + 1 >= r && s[l] != s[r]) { n     left_answer += s[l]; n     l++; n     r--; n     return; n   } n   if (s[l + 1] == s[r]) { n     left_answer += s[l + 1]; n     right_answer += s[r]; n     l += 2; n     r--; n     return; n   } n   if (s[l] == s[r - 1]) { n     left_answer += s[l]; n     right_answer += s[r - 1]; n     l++; n     r -= 2; n     return; n   } n   l++; n   r--; n } n void solve() { n   cin >> s; n   int left = 0; n   int right = s.size() - 1; n   while (left <= right) { n     squeeze(left, right); n   } n   reverse(right_answer.begin(), right_answer.end()); n   cout << left_answer << right_answer <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   srand(58164); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int dp[4010][4010]; n int pa[4010][4010], pb[4010][4010]; n char a[4010], b[4010]; n int ti, td, tr, te; n int la, lb; n int main() { n   scanf( %d%d%d%d , &ti, &td, &tr, &te); n   scanf( %s%s , a + 1, b + 1); n   la = strlen(a + 1); n   lb = strlen(b + 1); n   for (int i = 1; i <= la; i++) { n     for (int j = 1; j <= lb; j++) { n       pa[i][j] = pa[i][j - 1]; n       if (a[i] == b[j]) pa[i][j] = j; n     } n   } n   for (int i = 1; i <= lb; i++) { n     for (int j = 1; j <= la; j++) { n       pb[i][j] = pb[i][j - 1]; n       if (a[j] == b[i]) pb[i][j] = j; n     } n   } n   for (int i = 0; i <= la; i++) { n     for (int j = 0; j <= lb; j++) { n       if (!i && !j) continue; n       dp[i][j] = 1e9; n       if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + ti); n       if (i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + td); n       if (i && j) { n         if (a[i] == b[j]) n           dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); n         else n           dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + tr); n         int li = pb[j][i - 1], lj = pa[i][j - 1]; n         if (li && lj) { n           dp[i][j] = min(dp[i][j], dp[li - 1][lj - 1] + td * (i - li - 1) + n                                        ti * (j - lj - 1) + te); n         } n       } n     } n   } n   printf( %d , dp[la][lb]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n int n, k; n long long quick(long long A, long long B) { n   if (B == 0) return 1; n   long long tmp = quick(A, B >> 1); n   tmp *= tmp; n   tmp %= MOD; n   if (B & 1) tmp *= A, tmp %= MOD; n   return tmp; n } n int fact[100000 + 20], invfact[100000 + 20]; n int suf[100000 + 20]; n int tot; n int rest = 0; n long long c(int A, int B) { n   if (B == 0) return 1; n   if (B < 0) return 0; n   if (A < B) return 0; n   return 1ll * fact[A] * invfact[A - B] % MOD * invfact[B] % MOD; n } n int main() { n   scanf( %d%d , &n, &k); n   string s; n   cin >> s; n   for (int i = n - 1; i >= 1; --i) suf[i] = suf[i + 1] + (s[i - 1] -  0 ); n   fact[0] = 1; n   for (int i = 1; i <= n; ++i) { n     fact[i] = 1ll * fact[i - 1] * i % MOD; n   } n   invfact[n] = quick(fact[n], MOD - 2); n   for (int i = n - 1; i >= 0; --i) n     invfact[i] = 1ll * invfact[i + 1] * (i + 1) % MOD; n   tot = 0; n   int bad = 0, ten = 1; n   for (int i = 1; i <= n - 2; ++i) { n     tot -= bad; n     tot += MOD; n     tot %= MOD; n     tot = 10ll * tot % MOD; n     bad += 1ll * ten * (s[n - 1 - i] -  0 ) % MOD; n     bad %= MOD; n     ten = 10ll * ten % MOD; n     (tot += suf[i + 1]) %= MOD; n     (rest += 1ll * tot * c(n - 2 - i, k - 2) % MOD) %= MOD; n   } n   int tmp = 0; n   for (int i = 1; i <= n - 1; ++i) { n     tmp = 10ll * tmp % MOD; n     (tmp += (s[i - 1] -  0 )) %= MOD; n     (rest += 1ll * tmp * c(n - 1 - i, k - 1) % MOD) %= MOD; n   } n   int z = 1; n   tmp = 0; n   for (int i = 1; i <= n; ++i) { n     (tmp += 1ll * (s[n - i] -  0 ) * z % MOD) %= MOD; n     z = 10ll * z % MOD; n     (rest += 1ll * tmp * c(n - 1 - i, k - (i != n)) % MOD) %= MOD; n   } n   cout << rest << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   char **a = new char *[n]; n   for (int i = 0; i < n; i++) { n     a[i] = new char[m + 1]; n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       for (int k = 0; k < n; k++) n         for (int l = 0; l < m; l++) { n           if (i == k && j == l) { n             continue; n           } n           if (a[i][j] ==  W  || a[k][l] ==  W ) { n             continue; n           } n           bool found1 = true; n           for (int i1 = j; i1 <= l; i1++) { n             if (a[i][i1] ==  W ) { n               found1 = false; n               break; n             } n           } n           for (int i1 = i; i1 <= k; i1++) { n             if (a[i1][l] ==  W ) { n               found1 = false; n               break; n             } n           } n           if (found1) { n             continue; n           } n           found1 = true; n           for (int i1 = i; i1 <= k; i1++) { n             if (a[i1][j] ==  W ) { n               found1 = false; n               break; n             } n           } n           for (int i1 = j; i1 <= l; i1++) { n             if (a[k][i1] ==  W ) { n               found1 = false; n               break; n             } n           } n           if (found1) { n             continue; n           } else { n             cout <<  NO ; n             return 0; n           } n         } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 gen(chrono::system_clock::now().time_since_epoch().count()); n const int N = 1e6 + 10; n const long long MOD = 1e9 + 7; n const long long p1 = 57, M1 = 1e9 + 3; n const int rx[4] = {-1, 0, 1, 0}; n const int ry[4] = {0, 1, 0, -1}; n const int hx[8] = {-2, -2, -1, 1, 2, 2, 1, -1}; n const int hy[8] = {-1, 1, 2, 2, 1, -1, -2, -2}; n const long double eps = 1e-7; n const double pi = acos(-1.0); n const long long INF = 1e14 + 1000; n struct segtree { n   vector<pair<int, int> > t; n   vector<int> lazy; n   int nw; n   segtree(int n) { n     nw = (1 << (32 - __builtin_clz(n) - (__builtin_popcount(n) == 1))); n     t.resize(nw << 1, {0, 1}); n     lazy.resize(nw << 1); n     for (int i = nw - 1; i > 0; i--) { n       t[i].second = t[i * 2].second + t[i * 2 + 1].second; n     } n   } n   void push(int v) { n     t[v * 2].first += lazy[v]; n     t[v * 2 + 1].first += lazy[v]; n     lazy[v * 2] += lazy[v]; n     lazy[v * 2 + 1] += lazy[v]; n     lazy[v] = 0; n   } n   void upd(int v, int cl, int cr, int l, int r, int x) { n     if (cl >= l && cr <= r) { n       t[v].first += x; n       lazy[v] += x; n       return; n     } n     if (cl >= r || cr <= l) return; n     push(v); n     int mid = (cl + cr) / 2; n     upd(v * 2, cl, mid, l, r, x); n     upd(v * 2 + 1, mid, cr, l, r, x); n     if (t[v * 2].first < t[v * 2 + 1].first) { n       t[v] = t[v * 2]; n     } else if (t[v * 2 + 1].first < t[v * 2].first) { n       t[v] = t[v * 2 + 1]; n     } else { n       t[v] = t[v * 2]; n       t[v].second += t[v * 2 + 1].second; n     } n   } n   void upd(int l, int r, int x) { upd(1, 1, nw + 1, l, r + 1, x); } n   int kol(int v, int cl, int cr, int l, int r) { n     if (cl >= l && cr <= r) { n       if (t[v].first == 0) n         return t[v].second; n       else n         return 0; n     } n     if (cl >= r || cr <= l) return 0; n     push(v); n     int mid = (cl + cr) / 2; n     return kol(v * 2, cl, mid, l, r) + kol(v * 2 + 1, mid, cr, l, r); n   } n   int kol(int l, int r) { return kol(1, 1, nw + 1, l, r + 1); } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   vector<int> pos[n]; n   long long res = 0; n   segtree sgt(n); n   for (int i = 0; i < n; i++) { n     pos[a[i]].push_back(i); n     int sz = pos[a[i]].size(); n     if (sz == 1) { n       sgt.upd(1, i + 1, 1); n     } else if (sz == 2) { n       sgt.upd(1, pos[a[i]][0] + 1, -1); n       sgt.upd(1, i + 1, 1); n     } else if (sz == 3) { n       sgt.upd(1, pos[a[i]][1] + 1, -1); n       sgt.upd(pos[a[i]][0] + 2, i + 1, 1); n     } else if (sz == 4) { n       sgt.upd(pos[a[i]][0] + 2, pos[a[i]][2] + 1, -1); n       sgt.upd(1, pos[a[i]][0] + 1, 1); n       sgt.upd(pos[a[i]][1] + 2, i + 1, 1); n     } else { n       sgt.upd(1, pos[a[i]][sz - 5] + 1, -1); n       sgt.upd(pos[a[i]][sz - 4] + 2, pos[a[i]][sz - 2] + 1, -1); n       sgt.upd(1, pos[a[i]][sz - 4] + 1, 1); n       sgt.upd(pos[a[i]][sz - 3] + 2, i + 1, 1); n     } n     res += sgt.kol(1, i + 1); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   string ans =   ; n   long long int reqd = k / 2, op = 0, cl = 0; n   for (auto x : s) { n     if (op < reqd) { n       if (x ==  ( ) { n         ans.push_back(x); n         op++; n       } n     } n     if (cl < reqd) { n       if (x ==  ) ) { n         ans.push_back(x); n         cl++; n       } n     } n     if (op == reqd and cl == reqd) { n       break; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   int64_t n, m, d; n   cin >> n >> m >> d; n   vector<pair<int64_t, int64_t> > a1(n), a2(m); n   for (auto &e : a1) { n     cin >> e.first >> e.second; n     e.second = -e.second; n   } n   for (auto &e : a2) { n     cin >> e.first >> e.second; n     e.second = -e.second; n   } n   sort(a1.begin(), a1.end()); n   sort(a2.begin(), a2.end()); n   reverse(a1.begin(), a1.end()); n   reverse(a2.begin(), a2.end()); n   vector<int64_t> p2c(m), p2w(m); n   int64_t ans = 0; n   for (int64_t i = 0; i < m; i++) { n     p2c[i] = a2[i].first; n     p2w[i] = -a2[i].second; n     if (i > 0) { n       p2c[i] += p2c[i - 1]; n       p2w[i] += p2w[i - 1]; n     } n   } n   int64_t Len = 0, C = 0; n   for (int64_t i = 0; i < n; i++) { n     C += a1[i].first; n     d += a1[i].second; n     int64_t idx = upper_bound(p2w.begin(), p2w.end(), d) - p2w.begin(); n     if (idx == 0) break; n     ans = max(ans, C + p2c[idx - 1]); n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &x, T y) { n   return y < x ? x = y, 1 : 0; n } n template <typename T> n inline bool chkmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n const int INF = 0x3f3f3f3f; n const int N = 2e3 + 10, mod = 1e9 + 7; n int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n int vis[N][N]; n char Map[N][N]; n struct node { n   int x, y; n } dis[N][N]; n deque<node> Q; n inline int read() { n   int x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) flag = -1, ch = getchar(); n   while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch -  0 ), ch = getchar(); n   return x * flag; n } n int main() { n   int n = read(), m = read(), X = read(), Y = read(), L = read(), R = read(); n   for (int i = (1), iend = (n); i <= iend; i++) scanf( %s , Map[i] + 1); n   int cnt = 1; n   Q.push_back((node){X, Y}), vis[X][Y] = true; n   while (!Q.empty()) { n     node cur = Q.front(); n     Q.pop_front(); n     int x = cur.x, y = cur.y; n     for (int i = (0), iend = (3); i <= iend; i++) { n       int Nx = x + dir[i][0], Ny = y + dir[i][1], curdL = dis[x][y].x, n           curdR = dis[x][y].y; n       if (Nx <= 0 || Ny <= 0 || Nx > n || Ny > m || vis[Nx][Ny] || n           Map[Nx][Ny] ==  * ) n         continue; n       if (i == 2) curdR++; n       if (i == 3) curdL++; n       if (curdL > L || curdR > R) continue; n       if (i == 2 || i == 3) n         Q.push_back((node){Nx, Ny}); n       else n         Q.push_front((node){Nx, Ny}); n       dis[Nx][Ny] = (node){curdL, curdR}; n       vis[Nx][Ny] = true, cnt++; n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n long long int tmp = 85; n long long int a[MAXN], b[MAXN], ind[MAXN]; n vector<long long int> o; n long long int n, d, x; n long long int getNextX() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n int main() { n   cin >> n >> d >> x; n   for (long long int i = 0; i < n; i = i + 1) { n     a[i] = i + 1; n   } n   for (long long int i = 0; i < n; i = i + 1) { n     swap(a[i], a[getNextX() % (i + 1)]); n   } n   for (long long int i = 0; i < n; i = i + 1) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (int i = 0; i < n; i = i + 1) { n     swap(b[i], b[getNextX() % (i + 1)]); n   } n   for (int i = 0; i < n; i++) { n     ind[a[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     if (b[i] == 1) { n       o.push_back(i); n     } n   } n   for (int i = 0; i < n; i++) { n     long long int cur = n; n     bool check = false; n     while (cur >= 0 and cur >= n - tmp) { n       if (ind[cur] <= i and b[i - ind[cur]] == 1) { n         cout << cur << endl; n         check = true; n         break; n       } n       cur--; n     } n     if (check == true) { n       continue; n     } n     long long int cur_ans = 0; n     for (int j = 0; j < o.size(); j++) { n       if (o[j] <= i) { n         cur_ans = max(cur_ans, a[i - o[j]]); n       } n     } n     cout << cur_ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int a = 1; n   for (int i = 1; i < s.size(); i++) { n     if (s[i] ==  1 ) { n       a += 10; n     } else { n       a += s[i] -  0 ; n     } n   } n   cout << a <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   long long i; n   bool flag = true; n   for (i = 0; i < s.length(); i++) { n     if (s[i] ==  0 ) { n       s.erase(i, 1); n       flag = false; n       break; n     } n   } n   if (flag) { n     s.erase(s.begin()); n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n int add(int x, int y) { n   x += y; n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n   return x; n } n int mul(int x, int y) { return (x * 1ll * y) % MOD; } n int power(int n, int m, int p) { n   int res = 1; n   while (m > 0) { n     if (m & 1) res = (res * 1ll * n) % p; n     n = (n * 1ll * n) % p; n     m /= 2; n   } n   return res; n } n int solve() { n   int n; n   cin >> n; n   int d = power(100, MOD - 2, MOD); n   vector<int> arr(n), p(n), q(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     p[i] = mul(arr[i], d); n     q[i] = mul(100 - arr[i], d); n   } n   int cur = 1, res = 0, x = 0; n   for (int i = 0; i < n; i++) { n     int tmp = mul(cur, p[i]), tmq = mul(cur, q[i]); n     res = add(res, add(tmp, tmq)); n     x = add(x, tmq); n     cur = tmp; n   } n   res = mul(res, power(add(1, -x), MOD - 2, MOD)); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << solve() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void itval(istream_iterator<string> it) {} n template <typename T, typename... Args> n void itval(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   itval(++it, args...); n } n const long long int MOD = 1e9 + 7; n template <typename T> n inline void print(T x) { n   cout << x <<   n ; n } n template <typename T> n inline void printvec(T x) { n   for (auto a : x) cout << a <<    ; n   cout <<   n ; n } n struct custom { n   bool operator()(const pair<long long int, long long int> &p1, n                   const pair<long long int, long long int> &p2) const { n     return p1.first < p2.first; n   } n }; n long long int get_pow(long long int a, long long int b, long long int M = MOD) { n   long long int res = 1; n   while (b) { n     if (b & 1) res = (res * a) % M; n     a = (a * a) % M; n     b >>= 1; n   } n   return res; n } n const long long int N = 2e5 + 5, inf = 4e18, M = 302; n void solve() { n   long long int c, p, q, a, b; n   cin >> c >> p >> q >> a >> b; n   long long int x = c / a; n   long long int ans = 0; n   for (long long int i = (long long int)max(0ll, x - 10000); n        i < (long long int)(x + 10000); i++) { n     if (i * a <= c) { n       ans = max(ans, i * p + ((c - i * a) / b) * q); n     } else n       break; n   } n   long long int y = c / b; n   for (long long int i = (long long int)max(0ll, y - 10000); n        i < (long long int)(y + 10000); i++) { n     if (i * b <= c) { n       ans = max(ans, i * q + ((c - i * b) / a) * p); n     } else n       break; n   } n   if (p * b == q * a) { n     long long int x = c / a / 2; n     for (long long int i = (long long int)max(0ll, x - 10000); n          i < (long long int)(x + 10000); i++) { n       if (i * a <= c) { n         ans = max(ans, i * p + ((c - i * a) / b) * q); n       } else n         break; n     } n     long long int y = c / b / 2; n     for (long long int i = (long long int)max(0ll, y - 10000); n          i < (long long int)(y + 10000); i++) { n       if (i * b <= c) { n         ans = max(ans, i * q + ((c - i * b) / a) * p); n       } else n         break; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int test = 1; n   clock_t z = clock(); n   for (long long int tes = (long long int)0; tes < (long long int)(test); n        tes++) { n     solve(); n   } n   fprintf(stderr,  Total Time:%.4f n , (double)(clock() - z) / CLOCKS_PER_SEC), n       fflush(stderr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INFL = 1LL << 60; n const long long INF = 1 << 30; n long long MOD = 1000000007; n vector<long long> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; n vector<long long> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; n void pres(double A, long long x = 10) { n   cout << fixed << setprecision(x) << A << endl; n } n void BinarySay(long long x, long long y = 60) { n   for (long long i = 0; i < (y); i++) cout << (x >> (y - 1 - i) & 1); n   cout << endl; n } n long long cnt_bit(long long x) { return __builtin_popcountll(x); } n long long pow_long(long long x, long long k) { n   long long res = 1; n   while (k > 0) { n     if (k % 2) res *= x; n     x *= x; n     k /= 2; n   } n   return res; n } n long long pow_mod(long long x, long long k) { n   long long res = 1; n   while (k > 0) { n     if (k % 2) { n       res *= x; n       res %= MOD; n     } n     x *= x; n     x %= MOD; n     k /= 2; n   } n   return res; n } n long long inverse(long long x) { return pow_mod(x, MOD - 2); }; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }; n vector<long long> Row[100010]; n vector<long long> Col[100010]; n void solve() { n   long long N, M, K; n   cin >> N >> M >> K; n   for (long long i = 0; i < (K); i++) { n     long long a, b; n     cin >> a >> b; n     Row[a].push_back(b); n     Col[b].push_back(a); n   } n   for (long long i = 0; i <= M + 1; i++) { n     Row[0].push_back(i); n     Col[i].push_back(0); n     Row[N + 1].push_back(i); n     Col[i].push_back(N + 1); n   } n   for (long long i = 0; i <= N + 1; i++) { n     Col[0].push_back(i); n     Row[i].push_back(0); n     Col[M + 1].push_back(i); n     Row[i].push_back(M + 1); n   } n   for (long long i = 0; i < (N + 1); i++) { n     sort(Row[i].begin(), Row[i].end()); n     Row[i].erase(unique(Row[i].begin(), Row[i].end()), Row[i].end()); n   } n   for (long long i = 0; i < (M + 1); i++) { n     sort(Col[i].begin(), Col[i].end()); n     Col[i].erase(unique(Col[i].begin(), Col[i].end()), Col[i].end()); n   } n   long long Cnt = 0; n   for (long long i = 1; i <= N; i++) { n     long long x = *lower_bound(Row[i].begin(), Row[i].end(), 1); n     if (x == 1) break; n     Cnt++; n   } n   if (Cnt == N * M - K) { n     cout <<  Yes  << endl; n     return; n   } n   long long y = 1, x = 1; n   long long dir = 1; n   long long cnt = 1; n   long long L = 0, R = M + 1, U = 1, D = N + 1; n   while (true) { n     long long dif = -cnt; n     if (dir == 1) { n       long long nx = *lower_bound(Row[y].begin(), Row[y].end(), x); n       nx = min(nx - 1, R - 1); n       cnt += nx - x; n       x = nx; n       R = x; n     } n     if (dir == 2) { n       long long ny = *lower_bound(Col[x].begin(), Col[x].end(), y); n       ny = min(ny - 1, D - 1); n       cnt += ny - y; n       y = ny; n       D = y; n     } n     if (dir == 3) { n       auto itr = lower_bound(Row[y].begin(), Row[y].end(), x); n       itr--; n       long long nx = *itr; n       nx = max(nx + 1, L + 1); n       cnt += x - nx; n       x = nx; n       L = x; n     } n     if (dir == 4) { n       auto itr = lower_bound(Col[x].begin(), Col[x].end(), y); n       itr--; n       long long ny = *itr; n       ny = max(ny + 1, U + 1); n       cnt += y - ny; n       y = ny; n       U = y; n     } n     dif += cnt; n     if (dif == 0) break; n     dir++; n     if (dir == 5) dir = 1; n   } n   if (cnt == N * M - K) { n     cout <<  Yes  << endl; n   } else { n     cout <<  No  << endl; n   } n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int n, x, m, tmp, ans, sum, t; n int a[N], b[N], h[N]; n string s, p; n map<string, int> pol; n vector<int> nei[N]; n bool mark[N]; n void read_input() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] > 1 && tmp == 0) tmp = i; n     sum += a[i]; n   } n   if (sum == n) { n     cout << (n % 2 ?  First n  :  Second n ); n     return; n   } n   cout << (tmp % 2 ?  First n  :  Second n ); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     sum = 0; n     tmp = 0; n     read_input(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using pi = std::pair<int, int>; n using vec = std::vector<pi>; n const int N = 300007, P = 1019260817; n char str[N]; n int n, fa[N], size[N], dep[N], son[N], top[N], dfn[N], id[N], pw[N], ipw[N], n     h[N][2]; n std::vector<int> e[N]; n int sgn(int x) { return x < 0 ? -1 : 1; } n int sub(int a, int b) { return a -= b, a += a >> 31 & P; } n int mul(int a, int b) { return 1ll * a * b % P; } n int read() { n   int x = 0, c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) (x *= 10) += c & 15, c = getchar(); n   return x; n } n void dfs1(int u, int f) { n   size[u] = 1, dep[u] = dep[fa[u] = f] + 1, n   h[u][0] = (str[u] + 233ll * h[f][0]) % P, n   h[u][1] = (h[f][1] + 1ll * str[u] * pw[dep[u] - 1]) % P; n   for (int v : e[u]) n     if (v ^ f) n       dfs1(v, u), size[u] += size[v], n           son[u] = size[v] > size[son[u]] ? v : son[u]; n } n void dfs2(int u, int t) { n   static int T; n   top[u] = t, id[dfn[u] = ++T] = u; n   if (son[u]) dfs2(son[u], t); n   for (int v : e[u]) n     if (v ^ fa[u] && v ^ son[u]) dfs2(v, v); n } n vec split(int u, int v) { n   vec l, r; n   while (top[u] ^ top[v]) n     dep[top[u]] > dep[top[v]] ? (l.push_back({u, top[u]}), u = fa[top[u]]) n                               : (r.push_back({top[v], v}), v = fa[top[v]]); n   l.push_back({u, v}), std::reverse(r.begin(), r.end()), n       l.insert(l.end(), r.begin(), r.end()); n   return l; n } n int ask(int x, int fx, int d) { n   return fx == 1 n              ? mul(sub(h[x][1], h[fa[id[dfn[x] - d + 1]]][1]), ipw[dep[x] - d]) n              : sub(h[id[dfn[x] + d - 1]][0], mul(h[fa[x]][0], pw[d])); n } n int cal(int u, int fu, int v, int fv, int lim) { n   if (ask(u, fu, lim) == ask(v, fv, lim)) return lim; n   int l = 1, r = lim - 1; n   for (int mid; l <= r;) n     mid = (l + r) / 2, n     ask(u, fu, mid) == ask(v, fv, mid) ? l = mid + 1 : r = mid - 1; n   return r; n } n void solve() { n   int a = read(), b = read(), c = read(), d = read(), ans = 0; n   vec l = split(a, b), r = split(c, d); n   for (int pl = 0, pr = 0, l1, l2, f1, f2, d, x; n        pl < (int)l.size() && pr < (int)r.size();) { n     l1 = abs(dep[l[pl].first] - dep[l[pl].second]) + 1, n     l2 = abs(dep[r[pr].first] - dep[r[pr].second]) + 1, n     f1 = sgn(dep[l[pl].first] - dep[l[pl].second]), n     f2 = sgn(dep[r[pr].first] - dep[r[pr].second]), d = std::min(l1, l2); n     ans += x = cal(l[pl].first, f1, r[pr].first, f2, d); n     if (d ^ x) break; n     (l1 == d ? ++pl : l[pl].first = id[dfn[l[pl].first] - f1 * d]), n         (l2 == d ? ++pr : r[pr].first = id[dfn[r[pr].first] - f2 * d]); n   } n   printf( %d n , ans); n } n int main() { n   n = read(), scanf( %s , str + 1), pw[0] = ipw[0] = 1; n   for (int i = 1; i <= n; ++i) n     pw[i] = mul(233, pw[i - 1]), ipw[i] = mul(78741179, ipw[i - 1]); n   for (int i = 1, u, v; i < n; ++i) n     u = read(), v = read(), e[u].push_back(v), e[v].push_back(u); n   dfs1(1, 0), dfs2(1, 1); n   for (int m = read(); m; --m) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n default_random_engine generator; n uniform_int_distribution<int> distribution(1, INT_MAX); n vector<vector<long long> > one(int sz) { n   vector<vector<long long> > ret; n   ret.resize(sz); n   for (int i = 0; i < ret.size(); i++) { n     ret[i].resize(sz); n     for (int j = 0; j < ret.size(); j++) { n       ret[i][j] = (i == j); n     } n   } n   return ret; n } n vector<vector<long long> > mult(vector<vector<long long> > a, n                                 vector<vector<long long> > b) { n   vector<vector<long long> > ret; n   ret.resize(a.size()); n   for (int i = 0; i < ret.size(); i++) { n     ret[i].resize(a.size()); n     for (int j = 0; j < ret.size(); j++) { n       ret[i][j] = 0; n     } n   } n   for (int k = 0; k < ret.size(); k++) { n     for (int i = 0; i < a.size(); i++) { n       for (int j = 0; j < b.size(); j++) { n         ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]); n         ret[i][j] %= 998244353; n       } n     } n   } n   return ret; n } n vector<vector<long long> > pw(vector<vector<long long> > a, long long b) { n   if (b == 0) return one((int)a.size()); n   if (b % 2) return mult(a, pw(a, b - 1)); n   return pw(mult(a, a), b / 2); n } n long long my_rand(long long a = 0, long long b = INT_MAX) { n   return a + distribution(generator) % b; n } n inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); } n inline int norm(int a) { n   if (a >= 998244353) a -= 998244353; n   if (a < 0) a += 998244353; n   return a; n } n inline int binPow(int a, int k) { n   int ans = 1; n   while (k > 0) { n     if (k & 1) ans = mul(ans, a); n     a = mul(a, a); n     k >>= 1; n   } n   return ans; n } n inline int subtract(int a, int b) { return norm(a - b); } n inline int inv(int a) { return binPow(a, 998244353 - 2); } n long long ceil_int(long long a, long long b) { return a / b + (a % b != 0); } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n int main() { n   generator.seed(chrono::system_clock::now().time_since_epoch().count()); n   cin.sync_with_stdio(false); n   cin.tie(0); n   int x; n   cin >> x; n   int cnt = 0; n   vector<int> op; n   int ok = 0; n   do { n     int k = x; n     int pos = -1; n     int j = 0; n     while (k > 0) { n       if ((k & 1) == 0) { n         pos = j; n       } n       j++; n       k >>= 1; n     } n     if (pos == -1) n       break; n     else if (cnt % 2 == 0) n       op.push_back(pos + 1); n     if (cnt % 2 == 0) { n       x = x ^ ((1 << (pos + 1)) - 1); n     } else { n       x++; n     } n     cnt++; n   } while (1); n   cout << cnt << endl; n   for (int i = 0; i < op.size(); i++) { n     cout << op[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast,unroll-loops,no-stack-protector,fast-math ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n const long long mod = 1e9 + 7; n const long long N = 1e5 + 5; n void solve(void) { n   long long a, flag = 0; n   string b; n   cin >> a >> b; n   long long n = b.length(); n   for (long long i = 0; i < n; i++) { n     if (b[i] !=  9 ) { n       flag = 1; n       break; n     } n   } n   n -= flag; n   cout << n * a <<   n ; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << fixed << setprecision(10); n   ; n   long long ___ = 1; n   cin >> ___; n   while (___--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n #pragma GCC target( avx,avx2 ) n using namespace std; n template <class t> n inline t read(t &x) { n   char c = getchar(); n   bool f = 0; n   x = 0; n   while (!isdigit(c)) f |= c ==  - , c = getchar(); n   while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   if (f) x = -x; n   return x; n } n template <class t> n inline void write(t x) { n   if (x < 0) n     putchar( - ), write(-x); n   else { n     if (x > 9) write(x / 10); n     putchar( 0  + x % 10); n   } n } n const int mod = 1e9, N = 2e5 + 5; n int tr0[N << 2], tr1[N << 2], tg[N << 2], n, m, f[N], sum[N]; n int trans(int x, int len) { n   if (!len) return tr0[x]; n   if (len == 1) return tr1[x]; n   return (1ll * tr0[x] * f[len - 2] + 1ll * tr1[x] * f[len - 1]) % mod; n } n void pushup(int x, int l, int r) { n   int mid = l + r >> 1; n   tr0[x] = tr0[x << 1] + trans(x << 1 | 1, mid - l + 1); n   if (tr0[x] > mod) tr0[x] -= mod; n   tr1[x] = tr1[x << 1] + trans(x << 1 | 1, mid - l + 2); n   if (tr1[x] > mod) tr1[x] -= mod; n } n void build(int x, int l, int r) { n   if (l == r) { n     tr0[x] = read(tr1[x]); n     return; n   } n   int mid = l + r >> 1; n   build(x << 1, l, mid); n   build(x << 1 | 1, mid + 1, r); n   pushup(x, l, r); n } n void add(int x, int len, int v) { n   tg[x] += v; n   if (tg[x] > mod) tg[x] -= mod; n   tr0[x] = (tr0[x] + 1ll * sum[len - 1] * v) % mod; n   tr1[x] = (tr1[x] + 1ll * (sum[len] - 1 + mod) * v) % mod; n } n void pushdown(int x, int l, int r) { n   if (!tg[x]) return; n   int mid = l + r >> 1; n   add(x << 1, mid - l + 1, tg[x]); n   add(x << 1 | 1, r - mid, tg[x]); n   tg[x] = 0; n } n void mfy(int x, int l, int r, int p, int v) { n   if (l == r) { n     tr0[x] = tr1[x] = v; n     return; n   } n   pushdown(x, l, r); n   int mid = l + r >> 1; n   if (p <= mid) n     mfy(x << 1, l, mid, p, v); n   else n     mfy(x << 1 | 1, mid + 1, r, p, v); n   pushup(x, l, r); n } n void up(int x, int l, int r, int p, int q, int v) { n   if (p <= l && r <= q) { n     add(x, r - l + 1, v); n     return; n   } n   pushdown(x, l, r); n   int mid = l + r >> 1; n   if (p <= mid) up(x << 1, l, mid, p, q, v); n   if (q > mid) up(x << 1 | 1, mid + 1, r, p, q, v); n   pushup(x, l, r); n } n int que(int x, int l, int r, int p, int q) { n   if (p <= l && r <= q) return trans(x, l - p); n   pushdown(x, l, r); n   int mid = l + r >> 1, res = 0; n   if (p <= mid) res += que(x << 1, l, mid, p, q); n   if (q > mid) res += que(x << 1 | 1, mid + 1, r, p, q); n   if (res > mod) res -= mod; n   return res; n } n signed main() { n   read(n); n   read(m); n   f[0] = f[1] = 1; n   sum[0] = 1; n   sum[1] = 2; n   for (int i = 2; i <= n; i++) { n     f[i] = f[i - 1] + f[i - 2]; n     if (f[i] > mod) f[i] -= mod; n     sum[i] = sum[i - 1] + f[i]; n     if (sum[i] > mod) sum[i] -= mod; n   } n   build(1, 1, n); n   while (m--) { n     int op; n     read(op); n     if (op == 1) { n       int p, v; n       read(p); n       read(v); n       mfy(1, 1, n, p, v); n     } n     if (op == 2) { n       int l, r; n       read(l); n       read(r); n       write(que(1, 1, n, l, r)); n       puts(  ); n     } n     if (op == 3) { n       int l, r, v; n       read(l); n       read(r); n       read(v); n       up(1, 1, n, l, r, v); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int now, ot, mi, last, cnt, le, d[10010], st[10010][15], len[10010], ans[10010], n     trs[10010][26], fa[10010], tot[10010], lev[10010]; n char s[10010]; n inline int ext(int x) { n   for (now = last; s[le - len[now]] ^ x; now = fa[now]) n     ; n   if (trs[now][x]) return last = trs[now][x]; n   for (ot = fa[now]; s[le - len[ot]] ^ x; ot = fa[ot]) n     ; n   ++d[fa[++cnt] = trs[ot][x]]; n   len[trs[now][x] = cnt] = len[now] + 2; n   return last = cnt; n } n inline void topo() { n   int dl[10010], l = 0, r = 0; n   for (int i = 0; i <= cnt; ++i) n     if (!d[i]) dl[++r] = i; n   while (l < r) { n     int v = fa[dl[++l]]; n     tot[v] += tot[dl[l]]; n     if (!--d[v]) dl[++r] = v; n   } n } n int main() { n   len[last = cnt = 1] = -1; n   fa[1] = fa[0] = 1; n   for (s[le + 1] = getchar(); s[le + 1] !=   n ; s[++le + 1] = getchar()) { n     ext(s[le + 1] -=  a  - 1); n     ++tot[last]; n     if (!lev[last]) { n       st[last][0] = fa[last]; n       for (int i = 1; i <= 13; ++i) st[st[last][i - 1]][i - 1]; n       if (len[last] == 1) n         lev[last] = 1; n       else { n         for (mi = 13, now = last; mi >= 0;) { n           while (mi >= 0 && len[st[now][mi]] < len[last] >> 1) --mi; n           if (mi >= 0) now = st[now][mi]; n         } n         if (len[now] == len[last] >> 1) n           lev[last] = lev[now] + 1; n         else n           lev[last] = 1; n       } n     } n   } n   topo(); n   for (int i = 2; i <= cnt; ++i) ans[lev[i]] += tot[i]; n   for (int i = le; i; --i) ans[i - 1] += ans[i]; n   for (int i = 1; i <= le; ++i) printf( %d  , ans[i]); 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 n; n     cin >> n; n     long count1 = 0, count2 = 0; n     int f = 0; n     while (n != 1) { n       if (n % 3 == 0) { n         n = n / 3; n         count1++; n       } n       if (n % 2 == 0) { n         n = n / 2; n         count2++; n       } n       if (n % 3 != 0 and n % 2 != 0 and n != 1) { n         f = 1; n         break; n       } n     } n     if (f == 1) n       cout <<  -1  << endl; n     else if (count1 >= count2 and f == 0) { n       long ans = count1 - count2; n       if (count1 - count2 != 0) n         ans += count1; n       else n         ans = count1; n       cout << ans << endl; n     } else n       cout <<  -1  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 10; n struct Edge { n   int v; n   bool used; n   Edge(int v, int used) : v(v), used(used) {} n }; n vector<int> adj[MAXN], odd, ans; n vector<Edge> edges; n int p, n, m, deg[MAXN]; n int cur[MAXN]; n void dfs(int u) { n   for (auto &idx = cur[u]; idx < adj[u].size(); ++idx) { n     if (!edges[adj[u][idx]].used) { n       edges[adj[u][idx]].used = 1; n       edges[adj[u][idx] ^ 1].used = 1; n       dfs(edges[adj[u][idx]].v); n     } n   } n   ans.push_back(u); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     ++deg[u], ++deg[v]; n     edges.emplace_back(v, 0); n     edges.emplace_back(u, 0); n     int sz = edges.size(); n     adj[u].push_back(sz - 2); n     adj[v].push_back(sz - 1); n   } n   for (int i = (1); i < (n + 1); ++i) n     if (deg[i] & 1) { n       odd.push_back(i); n     } n   for (int i = 0; i < (int)odd.size(); i += 2) { n     int u = odd[i], v = odd[i + 1]; n     edges.emplace_back(v, 0); n     edges.emplace_back(u, 0); n     int sz = edges.size(); n     adj[u].push_back(sz - 2); n     adj[v].push_back(sz - 1); n   } n   p = m + (odd.size() / 2); n   dfs(1); n   if (ans.size() % 2 == 0) { n     ++p; n     ans.push_back(1); n   } n   cout << p <<   n ; n   for (int i = 1; i < (int)ans.size(); ++i) { n     int u = ans[i - 1], v = ans[i]; n     if (!(i & 1)) { n       swap(u, v); n     } n     cout << u <<     << v <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1.0); n const long double eps = 1e-6; n const int maxw = 1e6 + 10; n const int maxn = 5e2 + 10; n int n, m, K; n char s[maxn][maxn]; n vector<int> res[maxn]; n int num[maxn][maxn], dp[maxn][maxn]; n void solve() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (s[i][j] ==  1 ) { n         res[i].push_back(j); n       } n     } n   } n   int ret, tmp; n   for (int i = 1; i <= n; i++) { n     if (res[i].empty()) { n       for (int j = 0; j <= K; j++) { n         num[i][j] = 0; n       } n       continue; n     } n     for (int j = 0; j <= m; j++) { n       num[i][j] = maxw; n     } n     for (int j = 0; j < (int)res[i].size(); j++) { n       for (int k = j; k < (int)res[i].size(); k++) { n         ret = (int)res[i].size() - (k - j + 1); n         num[i][ret] = min(num[i][ret], res[i][k] - res[i][j] + 1); n       } n     } n     num[i][(int)res[i].size()] = 0; n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= K; j++) { n       dp[i][j] = maxw; n     } n   } n   memset(dp[0], 0, sizeof(dp[0])); n   for (int i = 1; i <= n; i++) { n     for (int j = K; j >= 0; j--) { n       for (int k = 0; k <= j; k++) { n         dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + num[i][k]); n       } n     } n   } n   printf( %d n , dp[n][K]); n } n int main(void) { n   scanf( %d %d %d , &n, &m, &K); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, x, y, n; n int main() { n   cin.tie(NULL); n   ios::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     cin >> n >> x >> y; n     vector<int> ans; n     int number = y - x; n     for (int i = 1; i <= number; i++) { n       if (number % i) continue; n       if (number / i + 1 > n) continue; n       number = i; n       break; n     } n     for (int i = y; i >= 1 && ans.size() < n; i -= number) ans.push_back(i); n     for (int i = y + number; i <= (int)1e9 && ans.size() < n; i += number) n       ans.push_back(i); n     for (auto here : ans) cout << here <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n const int dx[4] = {1, 0, -1, 0}; n const int dy[4] = {0, -1, 0, 1}; n using namespace std; n char str[5001]; n class Point { n  public: n   int Left; n   int Right; n   int Up; n   int Down; n }; n Point f1[5001], f2[5001]; n int ans1, ans2, n, m; n int dfs(int p, int tot) { n   int tmp; n   if (str[p] ==  D ) n     tmp = f2[p].Down; n   else if (str[p] ==  U ) n     tmp = f2[p].Up; n   else if (str[p] ==  L ) n     tmp = f2[p].Left; n   else n     tmp = f2[p].Right; n   if (f2[p].Left != -1) f2[f2[p].Left].Right = f2[p].Right; n   if (f2[p].Right != -1) f2[f2[p].Right].Left = f2[p].Left; n   if (f2[p].Up != -1) f2[f2[p].Up].Down = f2[p].Down; n   if (f2[p].Down != -1) f2[f2[p].Down].Up = f2[p].Up; n   if (ans1 == tot) ans2++; n   if (ans1 < tot) { n     ans1 = tot; n     ans2 = 1; n   } n   if (tmp != -1) dfs(tmp, tot + 1); n   return 0; n } n int work() { n   scanf( %d%d , &n, &m); n   getchar(); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) str[i * m + j] = getchar(); n     getchar(); n   } n   memset(f1, -1, sizeof(f1)); n   memset(f2, -1, sizeof(f2)); n   int k; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       int p = i * m + j; n       if (str[p] ==  . ) continue; n       { n         k = i + 1; n         while (str[k * m + j] ==  .  && k < n) k++; n         if (k < n) { n           f1[p].Down = k * m + j; n           f1[k * m + j].Up = p; n         } n       } n       { n         k = i - 1; n         while (str[k * m + j] ==  .  && k >= 0) k--; n         if (k >= 0) { n           f1[p].Up = k * m + j; n           f1[k * m + j].Down = p; n         } n       } n       { n         k = j - 1; n         while (str[i * m + k] ==  .  && k >= 0) k--; n         if (k >= 0) { n           f1[p].Left = i * m + k; n           f1[i * m + k].Right = p; n         } n       } n       { n         k = j + 1; n         while (str[i * m + k] ==  .  && k < m) k++; n         if (k < m) { n           f1[p].Right = i * m + k; n           f1[i * m + k].Left = p; n         } n       } n     } n   ans1 = ans2 = 0; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (str[i * m + j] !=  . ) { n         for (int k = 0; k < 5001; ++k) f2[k] = f1[k]; n         dfs(i * m + j, 1); n       } n   printf( %d %d n , ans1, ans2); n   return 0; n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n == 2) { n     cout <<  1 2  << endl; n     return 0; n   } n   bool usedN = false; n   for (int i = 0; i < n; i++) { n     int maxi = 0; n     for (int j = 0; j < n; j++) { n       int a; n       cin >> a; n       if (a > maxi) { n         maxi = a; n       } n     } n     if (maxi == n - 1 && !usedN) { n       usedN = true; n       maxi = n; n     } n     cout << maxi <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int a[3003]; n double f[3003 * 3003]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n + 1; i++) scanf( %d , &a[i]); n   int cnt = 0; n   for (int i = 1; i < n + 1; i++) { n     for (int j = i + 1; j < n + 1; j++) { n       if (a[i] > a[j]) cnt++; n     } n   } n   f[0] = 0; n   f[1] = 1; n   for (int i = 2; i < cnt + 1; i++) f[i] = f[i - 2] + 4; n   printf( %.6f , f[cnt]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class DSU { n  public: n   vector<int> parent; n   int total_components; n   DSU(int n) { n     parent.resize(n + 1); n     for (int i = 1; i <= n; i++) { n       parent[i] = i; n     } n     total_components = n; n   } n   int get(int x) { n     if (parent[x] == x) { n       return x; n     } n     return parent[x] = get(parent[x]); n   } n   bool unite(int x, int y) { n     int p1 = get(x); n     int p2 = get(y); n     if (p1 == p2) { n       return true; n     } n     parent[p1] = p2; n     total_components--; n     return false; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   bool cycle = false; n   DSU g(n); n   while (m--) { n     int u, v; n     cin >> u >> v; n     if (g.unite(u, v)) { n       cycle = true; n     } n   } n   if (cycle || g.total_components > 1) { n     cout <<  no  << endl; n   } else { n     cout <<  yes  << endl; n   } n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int N = 10003; n const long long oo = 1e18; n int get(int x, int l, int r) { n   if (x <= l) return l - x; n   if (r <= x) return x - r; n   return 0; n } n int l[N], r[N], zn[N]; n long long d1[N], d2[N]; n vector<int> st; n map<int, int> mp; n int main() { n   if (0) { n     freopen( input.txt ,  w , stdout); n     return 0; n   } n   int i, u, n, m, x, li, ri, xp; n   long long tt, ans; n   scanf( %d%d , &n, &x); n   st.push_back(x); n   for (i = 0; i < n; ++i) { n     scanf( %d%d , &li, &ri); n     l[i] = li; n     r[i] = ri; n     st.push_back(li); n     st.push_back(ri); n   } n   sort(st.begin(), st.end()); n   m = 0; n   for (i = 0; i < st.size(); ++i) n     if ((i + 1 == st.size()) || (st[i] != st[i + 1])) { n       zn[m] = st[i]; n       mp[st[i]] = m++; n     } n   xp = mp[x]; n   for (i = 0; i < m; ++i) d1[i] = oo; n   d1[xp] = 0; n   for (u = 0; u < n; ++u) { n     for (i = 0; i < m; ++i) d2[i] = oo; n     li = l[u]; n     ri = r[u]; n     tt = oo; n     for (i = 0; i < m; ++i) { n       if (i) tt += zn[i] - zn[i - 1]; n       tt = min(tt, d1[i]); n       if (tt >= oo) { n         tt = oo; n         continue; n       } n       d2[i] = min(d2[i], tt + get(zn[i], li, ri)); n     } n     tt = oo; n     for (i = m - 1; i >= 0; --i) { n       if (i != m - 1) tt += zn[i + 1] - zn[i]; n       tt = min(tt, d1[i]); n       if (tt >= oo) { n         tt = oo; n         continue; n       } n       d2[i] = min(d2[i], tt + get(zn[i], li, ri)); n     } n     for (i = 0; i < m; ++i) d1[i] = d2[i]; n   } n   ans = oo; n   for (i = 0; i < m; ++i) ans = min(ans, d1[i]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pows[51]; n vector<int> base_3(long long int n) { n   vector<int> f(51); n   int r = 0; n   while (n > 0) { n     f[r++] = (int)n % 3; n     n /= 3; n   } n   return f; n } n int main() { n   int q; n   cin >> q; n   pows[1] = 3; n   pows[0] = 1; n   for (int i = 1; i <= 50; i++) { n     pows[i] = pows[i - 1] * 3; n   } n   while (q--) { n     long long int n; n     cin >> n; n     vector<int> v = base_3(n); n     int ind = -1; n     for (int j = 50; j >= 0; j--) { n       if (v[j] == 2 && ind == -1) { n         ind = j; n       } n     } n     if (ind == -1) { n       cout << n <<   n ; n     } else { n       int r = ind + 1; n       while (r < 50 && v[r] == 1) { n         r++; n       } n       v[r] = 1; n       for (int i = 0; i < r; i++) { n         v[i] = 0; n       } n       long long int ans = 0; n       for (int j = 0; j < 51; j++) { n         ans += v[j] * pows[j]; n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int qq = 3e5 + 10; n vector<int> G[qq]; n multiset<int, greater<int> > myset; n int num[qq]; n void Del(int x) { n   auto t = myset.find(x); n   myset.erase(t); n } n int main() { n   int ans = 2e9 + 10; n   int n; n   scanf( %d , &n); n   int a, b; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , num + i); n     myset.insert(num[i]); n   } n   for (int i = 1; i <= n - 1; ++i) { n     scanf( %d%d , &a, &b); n     G[a].push_back(b), G[b].push_back(a); n   } n   for (int i = 1; i <= n; ++i) { n     Del(num[i]); n     int ma = num[i]; n     for (int y : G[i]) { n       Del(num[y]); n       ma = max(ma, num[y] + 1); n     } n     if (!myset.empty()) { n       ma = max(ma, *myset.begin() + 2); n     } n     for (int y : G[i]) { n       myset.insert(num[y]); n     } n     myset.insert(num[i]); n     ans = min(ans, ma); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n void debug_out() { cerr << endl; } n template <class T> n ostream& prnt(ostream& out, T v) { n   out << v.size() <<   n ; n   for (auto e : v) out << e <<    ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, vector<T> v) { n   return prnt(out, v); n } n template <class T> n ostream& operator<<(ostream& out, set<T> v) { n   return prnt(out, v); n } n template <class T1, class T2> n ostream& operator<<(ostream& out, map<T1, T2> v) { n   return prnt(out, v); n } n template <class T1, class T2> n ostream& operator<<(ostream& out, pair<T1, T2> p) { n   return out <<  (  << p.first <<     << p.second <<  ) ; n } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << H; n   debug_out(T...); n } n const long double PI = acos(-1); n int n, m, c, st[2 * 20005], dr[2 * 20005], a, b; n bool uz[20005]; n vector<int> v[2 * 20005]; n bool cup(int nod) { n   uz[nod] = 1; n   for (auto it : v[nod]) { n     if (!dr[it]) { n       dr[it] = nod; n       st[nod] = it; n       return 1; n     } n   } n   for (auto it : v[nod]) { n     if (!uz[dr[it]] && cup(dr[it])) { n       dr[it] = nod; n       st[nod] = it; n       return 1; n     } n   } n   return 0; n } n struct edge { n   int a, b, c; n }; n vector<edge> E2; n int _try(int x) { n   for (int i = 0; i <= 2 * n + 1; i++) { n     v[i].clear(); n   } n   memset(st, 0, sizeof(st)); n   memset(dr, 0, sizeof(dr)); n   for (auto it : E2) { n     if (it.c > x) continue; n     v[it.a].push_back(it.b); n     v[it.b].push_back(it.a); n   } n   int ok = 1; n   while (ok) { n     ok = 0; n     memset(uz, 0, sizeof(uz)); n     for (int i = 1; i <= n; i++) n       if (!st[i] && !uz[i]) ok |= cup(i); n   } n   int nr = 0; n   for (int i = 1; i <= n; i++) n     if (st[i]) nr++; n   return nr == n; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) { n     int p, q, c; n     cin >> p >> q >> c; n     q += n; n     E2.push_back({p, q, c}); n   } n   int p = 1, st = 0; n   for (; p <= 1e9; p <<= 1) n     ; n   for (; p >= 1; p >>= 1) { n     if (st + p < 1e9 && !_try(st + p)) { n       st += p; n     } n   } n   st++; n   if (!_try(st)) { n     cout <<  -1 n ; n   } else n     cout << st <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10][5]; n int ulx, uly, drx, dry; n bool isd(int x, int y) { return x <= drx && x >= ulx && y <= dry && y >= uly; } n bool vis[210]; n int numtot; n int pos; n int len; n int abs(int x) { return x < 0 ? -x : x; } n bool isd2(int x, int y) { n   return abs(x - a[pos][2]) + abs(y - a[pos + 1][2] + len) <= len; n } n bool crss(int a, int b, int c, int d, int e, int f, int g, int h) { n   if (a > c) swap(a, c); n   if (b > d) swap(b, d); n   if (e > g) swap(e, g), swap(f, h); n   if (b == d) { n     if (h > f) return b >= f && b <= h && e + b - f >= a && e + b - f <= c; n     return b >= h && b <= f && e + f - b >= a && e + f - b <= c; n   } n   if (h > f) return a >= e && a <= g && f + a - e >= b && f + a - e <= d; n   return a >= e && a <= g && f + e - a >= b && f + e - a <= d; n } n int main() { n   int tmp = -0x7fffffff; n   for (int k = 1; k <= 2; k++) n     for (int i = 1; i <= 8; i++) scanf( %d , &a[i][k]); n   for (int i = 1; i <= 8; i += 2) n     if (!vis[a[i][1] + 101]) numtot++, vis[a[i][1] + 101] = true; n   if (numtot > 2) n     for (int i = 1; i <= 8; i++) swap(a[i][1], a[i][2]); n   ulx = min(a[1][1], a[5][1]); n   uly = min(a[2][1], a[6][1]); n   drx = max(a[1][1], a[5][1]); n   dry = max(a[2][1], a[6][1]); n   if (isd(a[1][2], a[2][2]) && isd(a[3][2], a[4][2]) && isd(a[5][2], a[6][2]) && n       isd(a[7][2], a[8][2])) n     return puts( yes ), 0; n   for (int i = 2; i <= 8; i += 2) n     if (a[i][2] > tmp) tmp = a[i][2], pos = i - 1; n   len = abs(a[pos - 2 < 1 ? pos - 2 + 8 : pos - 2][2] - n             a[pos + 2 > 8 ? pos + 2 - 8 : pos + 2][2]) / n         2; n   if (isd2(a[1][1], a[2][1]) && isd2(a[3][1], a[4][1]) && n       isd2(a[5][1], a[6][1]) && isd2(a[7][1], a[8][1])) n     return puts( yes ), 0; n   for (int i = 1; i <= 4; i++) n     for (int j = 1; j <= 4; j++) n       if (crss(a[i * 2 - 1][1], a[i * 2][1], n                a[(i + 1 == 5 ? 1 : i + 1) * 2 - 1][1], n                a[(i + 1 == 5 ? 1 : i + 1) * 2][1], a[j * 2 - 1][2], a[j * 2][2], n                a[(j + 1 == 5 ? 1 : j + 1) * 2 - 1][2], n                a[(j + 1 == 5 ? 1 : j + 1) * 2][2])) n         return puts( yes ), 0; n   return puts( no ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, S, T, du[10010]; n struct edge { n   int u, v, cap, flow; n   edge() {} n   edge(int u, int v, int c, int f) : u(u), v(v), cap(c), flow(f) {} n }; n vector<edge> edges; n vector<int> e[10010]; n inline void add_edge(int u, int v, int c) { n   edges.push_back(edge(u, v, c, 0)); n   edges.push_back(edge(v, u, 0, 0)); n   int m = edges.size(); n   e[u].push_back(m - 2); n   e[v].push_back(m - 1); n } n int vis[10010], dis[10010], cur[10010]; n bool bfs(int s, int t) { n   memset(vis, 0, sizeof(vis)); n   queue<int> que; n   que.push(s); n   vis[s] = 1; n   dis[s] = 0; n   while (!que.empty()) { n     int now = que.front(); n     que.pop(); n     for (int i = 0; i < (e[now].size()); i++) { n       edge& nxt = edges[e[now][i]]; n       if (!vis[nxt.v] && nxt.cap > nxt.flow) { n         vis[nxt.v] = 1; n         dis[nxt.v] = dis[now] + 1; n         que.push(nxt.v); n       } n     } n   } n   return vis[t]; n } n int dfs(int st, int t, int low) { n   if (st == t || low == 0) return low; n   int flow = 0, f; n   for (int& i = cur[st]; i < e[st].size(); i++) { n     edge& nxt = edges[e[st][i]]; n     if (dis[nxt.v] == dis[st] + 1 && n         (f = dfs(nxt.v, t, min(low, nxt.cap - nxt.flow))) > 0) { n       nxt.flow += f; n       edges[e[st][i] ^ 1].flow -= f; n       flow += f; n       low -= f; n       if (low == 0) break; n     } n   } n   return flow; n } n int Dinic(int s, int t) { n   int ans = 0; n   while (bfs(s, t)) { n     memset(cur, 0, sizeof(cur)); n     ans += dfs(s, t, 0x3f3f3f3f); n   } n   return ans; n } n pair<int, int> pp[10010]; n bool check(int mid) { n   edges.clear(); n   for (int i = 0; i <= n + m + 1; i++) e[i].clear(); n   for (int i = 1; i <= m; i++) { n     add_edge(S, i, 1); n     add_edge(i, m + pp[i].first, 1); n     add_edge(i, m + pp[i].second, 1); n   } n   for (int i = 1; i <= n; i++) add_edge(m + i, T, min(du[i], mid)); n   int ans = Dinic(S, T); n   if (ans < m) return false; n   return true; n } n int main() { n   scanf( %d %d , &n, &m); n   S = 0, T = n + m + 1; n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     pp[i] = make_pair(u, v); n     du[u]++; n     du[v]++; n   } n   int L = 1, R = min(m, n - 1), ans = min(m, n - 1); n   while (L <= R) { n     int mid = (L + R) >> 1; n     if (check(mid)) { n       ans = mid; n       R = mid - 1; n     } else n       L = mid + 1; n   } n   printf( %d n , ans); n   check(ans); n   int tmp = 0; n   for (int i = 0; i < edges.size(); i += 2) { n     if (edges[i].u <= m && edges[i].u >= 1 && edges[i].v >= m + 1 && n         edges[i].v <= n + m && edges[i].cap == edges[i].flow) { n       int nxt = pp[edges[i].u].first; n       if (nxt == edges[i].v - m) nxt = pp[edges[i].u].second; n       printf( %d %d n , edges[i].v - m, nxt); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int N = 1005; n int n, m; n int a[N], ans[N], pre[N]; n int main() { n   int i, j, k, t, nc = 0; n   while (scanf( %d%d , &n, &m) != EOF) { n     int tmin = -1, tpre = -1; n     for (i = 1; i <= m; i++) { n       scanf( %d , &a[i]); n       if (tmin < a[i]) { n         k = i; n         tmin = a[i]; n       } n     } n     bool work = true; n     ans[1] = k; n     a[k]--; n     for (i = 1; i <= m; i++) pre[i] = N; n     pre[k] = 1; n     for (i = 2; i < n; i++) { n       tmin = -1; n       for (j = 1; j <= m; j++) { n         if (a[j] == 0 || j == ans[i - 1]) continue; n         if (tmin < a[j] || tmin == a[j] && pre[j] < tpre) { n           k = j; n           tmin = a[j]; n           tpre = pre[j]; n         } n       } n       if (tmin == N) break; n       ans[i] = k; n       a[k]--; n       pre[k] = i; n     } n     if (i < n) n       work = false; n     else { n       for (i = 1; i <= m; i++) { n         if (a[i] > 0 && i != ans[1] && i != ans[n - 1]) { n           ans[n] = i; n           break; n         } n       } n       if (i > m) work = false; n     } n     if (!work) n       printf( -1 n ); n     else { n       for (i = 1; i <= n; i++) { n         printf( %d , ans[i]); n         if (i < n) n           printf(   ); n         else n           printf(  n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n class FKuroniAndThePunishment { n  public: n   vector<long long> primes; n   void pre(int n) { n     vector<bool> isPrime(n + 1, true); n     isPrime[1] = false; n     isPrime[0] = false; n     for (int i = 2; i <= n; i++) { n       if (isPrime[i]) n         for (int j = 2 * i; j <= n; j += i) isPrime[j] = false; n       if (isPrime[i]) primes.push_back(i); n     } n   } n   void solve(std::istream &in, std::ostream &out) { n     int n; n     in >> n; n     vector<long long> ar(n); n     for (long long &i : ar) in >> i; n     long long mn = LLONG_MIN; n     for (long long i : ar) mn = max(mn, i); n     pre(1000000); n     int idx = primes.size(); n     for (long long i = max(1ll, mn - n); i <= mn + n; i++) primes.push_back(i); n     long long first = primes[idx]; n     for (int i = 0; i < idx; i++) { n       long long index = n           idx + (first % primes[i] == 0 ? 0 : primes[i] - (first % primes[i])); n       for (long long j = index; j < primes.size(); j += primes[i]) n         while (primes[j] % primes[i] == 0) primes[j] /= primes[i]; n     } n     long long ans = n; n     for (long long &i : primes) { n       if (i == 1) continue; n       long long res = 0; n       for (long long &j : ar) { n         if (j < i) n           res += (i - j); n         else n           res += min(j % i, (i - (j % i))); n         if (res >= ans) break; n       } n       ans = min(ans, res); n     } n     out << ans <<   n ; n   } n }; n int main() { n   FKuroniAndThePunishment solver; n   std::ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   std::istream &in(std::cin); n   std::ostream &out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int n, k, now, last; n long long dp[2][505][505], sum[505], ans = 0, s; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> k; n   memset(dp[1], 0, sizeof dp[1]); n   dp[1][1][1] = 1; n   for (int i = 2; i <= n; i++) { n     now = i & 1; n     last = now ^ 1; n     memset(dp[now], 0, sizeof dp[now]); n     for (int j = 1; j < i; j++) { n       for (int k = 1; k < i; k++) { n         if (j != k) n           dp[now][j][k + 1] = (dp[now][j][k + 1] + dp[last][j][k]) % mod; n         else n           dp[now][j + 1][k + 1] = n               (dp[now][j + 1][k + 1] + dp[last][j][k]) % mod; n         dp[now][j][1] = (dp[now][j][1] + dp[last][j][k]) % mod; n       } n     } n   } n   memset(sum, 0, sizeof sum); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= i; j++) sum[i] = (sum[i] + dp[now][i][j]) % mod; n   s = 0; n   for (int i = 1; i <= n; i++) s = (s + sum[i]) % mod; n   int t = n; n   for (int i = 1; i <= n; i++) { n     while (t * i >= k) { n       s = (s - sum[t] + mod) % mod; n       t--; n     } n     ans = (ans + sum[i] * s % mod) % mod; n   } n   ans = ans * 2 % mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool poss = true; n int n, rt = 0; n vector<int> v, res; n vector<vector<int>> adj; n vector<pair<int, int>> dfs(int second, int p) { n   vector<pair<int, int>> cur; n   for (auto i : adj[second]) { n     if (i != p) { n       vector<pair<int, int>> ch = dfs(i, second); n       for (auto i : ch) { n         cur.push_back(i); n       } n     } n   } n   sort(cur.begin(), cur.end()); n   for (int i = 0; i < cur.size(); ++i) { n     if (i < v[second]) n       cur[i].first = i; n     else n       cur[i].first = i + 1; n   } n   if (v[second] > cur.size()) poss = false; n   cur.push_back({v[second], second}); n   return cur; n } n int main() { n   cin >> n; n   adj.assign(n, vector<int>()); n   v.assign(n, 0); n   res.assign(n, 0); n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x >> v[i]; n     x--; n     if (x != -1) { n       adj[x].push_back(i); n       adj[i].push_back(x); n     } else n       rt = i; n   } n   vector<pair<int, int>> cur = dfs(rt, rt); n   for (auto i : cur) { n     res[i.second] = i.first + 1; n   } n   if (!poss) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   for (auto i : res) { n     cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n struct matrix { n   int val; n } matr[109][109]; n matrix* mat[109][109]; n int main() { n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       matr[i][j].val = 0; n       mat[i][j] = &matr[i][j]; n     } n   int t1, t2, t3, t4, t5; n   matrix* tmp; n   for (int i = 0; i < q; i++) { n     scanf( %d , &t1); n     if (t1 == 1) { n       scanf( %d , &t2); n       tmp = mat[t2][1]; n       for (int j = 1; j < m; j++) { n         mat[t2][j] = mat[t2][j + 1]; n       } n       mat[t2][m] = tmp; n     } else if (t1 == 2) { n       scanf( %d , &t2); n       tmp = mat[1][t2]; n       for (int i = 1; i < n; i++) { n         mat[i][t2] = mat[i + 1][t2]; n       } n       mat[n][t2] = tmp; n     } else if (t1 == 3) { n       scanf( %d %d %d , &t3, &t4, &t5); n       mat[t3][t4]->val = t5; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       printf( %d  , matr[i][j].val); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n const int maxpow = 1 << 17; n const int inf = 1e9 + 7; n long long h[maxn]; n int par[maxn]; n struct node { n   pair<int, int> a; n   node(pair<int, int> _a) : a(_a) {} n   node() : a(0, -1) {} n   void merge(node x, node y) { a = max(x.a, y.a); } n }; n node tree[maxpow * 2]; n node query(int i, int ns, int ne, int qs, int qe) { n   if (qe <= ns || qs >= ne) { n     return node(); n   } n   if (ns >= qs && ne <= qe) { n     return tree[i]; n   } n   int mid = (ns + ne) / 2; n   node res1 = query(2 * i, ns, mid, qs, qe); n   node res2 = query(2 * i + 1, mid, ne, qs, qe); n   node res; n   res.merge(res1, res2); n   return res; n } n void update(int i, int ns, int ne, int qs, pair<int, int> v) { n   if (qs + 1 <= ns || qs >= ne) { n     return; n   } n   if (ns + 1 == ne) { n     tree[i] = node(v); n     return; n   } n   int mid = (ns + ne) / 2; n   update(2 * i, ns, mid, qs, v); n   update(2 * i + 1, mid, ne, qs, v); n   tree[i].merge(tree[2 * i], tree[2 * i + 1]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, d; n   cin >> n >> d; n   vector<long long> a; n   for (long long i = 0; i < n; i++) { n     cin >> h[i]; n     a.push_back(h[i]); n   } n   sort(a.begin(), a.end()); n   a.erase(unique(a.begin(), a.end()), a.end()); n   pair<int, int> mx(-1, -1); n   for (int i = 0; i < n; i++) { n     int left = lower_bound(a.begin(), a.end(), h[i] - d + 1) - a.begin(); n     int right = lower_bound(a.begin(), a.end(), h[i] + d) - a.begin(); n     int cur = lower_bound(a.begin(), a.end(), h[i]) - a.begin(); n     node ans1 = query(1, 0, maxpow, 0, left); n     node ans2 = query(1, 0, maxpow, right, n); n     pair<int, int> ans = max(ans1.a, ans2.a); n     ans.first += 1; n     par[i] = ans.second; n     ans.second = i; n     mx = max(mx, ans); n     update(1, 0, maxpow, cur, ans); n   } n   cout << mx.first << endl; n   int ind = mx.second; n   vector<int> res; n   while (ind != -1) { n     res.push_back(ind); n     ind = par[ind]; n   } n   reverse(res.begin(), res.end()); n   for (int i = 0; i < res.size(); i++) { n     cout << res[i] + 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, r = 0, l = 0; n   vector<int> d; n   cin >> n; n   d.resize(n); n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     l += x; n     r += y; n     d[i] = x - y; n   } n   int maxx = abs(r - l), n_max = -1; n   for (int i = 0; i < n; ++i) { n     int dl = l - d[i], dr = r + d[i]; n     if (abs(dr - dl) > maxx) { n       maxx = abs(dr - dl); n       n_max = i + 1; n     } n   } n   if (n_max == -1) n     cout << 0 << endl; n   else n     cout << n_max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int TC; n   cin >> TC; n   while (TC--) { n     int p, f, cnts, cntw, s, w; n     cin >> p >> f >> cnts >> cntw >> s >> w; n     if (s < w) { n       swap(s, w); n       swap(cnts, cntw); n     } n     int maxi = 0; n     for (int i = 0; i <= min(p / w, cntw); i++) { n       int pw = i; n       int ps = min((p - i * w) / s, cnts); n       int fw, fs; n       int cntW = cntw - i; n       int cntS = cnts - ps; n       fw = min(cntW, f / w); n       if (f / w > cntW) { n         fs = min(cntS, (f - cntW * w) / s); n       } else { n         fs = 0; n       } n       int total = ps + pw + fs + fw; n       maxi = max(maxi, total); n     } n     cout << maxi <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<bool> vis; n vector<vector<long long>> adj; n void dfs(long long node) { n   vis[node] = true; n   for (long long child : adj[node]) { n     if (vis[child]) continue; n     dfs(child); n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> arr(n - 1); n   vector<long long> deg(n + 1, 1); n   for (long long i = 0; i < n - 1; ++i) { n     cin >> arr[i]; n     ++deg[arr[i]]; n   } n   set<pair<long long, long long>> st; n   for (long long i = 1; i <= n; ++i) { n     if (deg[i] == 1) { n       st.insert({i, i}); n     } n   } n   cout << arr[0] <<   n ; n   for (long long i = n - 2; i >= 0; --i) { n     const long long node = arr[i]; n     auto it = st.begin(); n     long long currMx = it->first; n     long long currRoot = it->second; n     st.erase(it); n     cout << currRoot <<     << node <<   n ; n     --deg[node]; n     if (deg[node] == 1) { n       st.insert({max(currMx, node), node}); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-10; n template <typename T, typename S> n vector<T>& operator<<(vector<T>& a, S b) { n   a.push_back(b); n   return a; n } n template <typename T> n void operator>>(vector<T>& a, int b) { n   while (b--) n     if (!a.empty()) a.pop_back(); n } n bool isprime(int n) { n   if (n < 2) return false; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n long long b_pow(long long x, long long n) { n   return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; n } n string itos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n string read() { n   string s; n   char c; n   while (cin >> c && c !=  > ) s += c; n   if (s.size() == 0) exit(0); n   return s.substr(1); n } n int f(string s) { return s[0] ==  / ; } n string get(string s) { n   if (s[0] ==  / ) return s.substr(1); n   return s; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m, ret = 0; n   stack<string> s; n   while (1) { n     string t = read(); n     if (t ==   ) return 0; n     if (s.size() == 0 || (!f(t) || get(s.top()) != get(t))) { n       cout << string(s.size() * 2,    ) <<  <  << t <<  >  << endl; n       s.push(t); n     } else { n       s.pop(); n       cout << string(s.size() * 2,    ) <<  <  << t <<  >  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long ti; n   cin >> ti; n   for (long long io = 0; io < ti; io++) { n     long long n, m; n     cin >> n >> m; n     vector<long long> a, ao, b; n     for (long long i = 0; i < m; i++) { n       long long x, y; n       cin >> x >> y; n       a.push_back(x); n       ao.push_back(x); n       b.push_back(y); n     } n     sort(a.begin(), a.end()); n     long long sf[m + 1]; n     long long sm = 0; n     for (long long i = m - 1; i >= 0; i--) { n       sm += a[i]; n       sf[m - i] = sm; n     } n     long long ans = 0; n     if (n <= m) ans = sf[n]; n     for (long long i = 0; i < m; i++) { n       long long v = b[i]; n       long long idx = upper_bound(a.begin(), a.end(), v) - a.begin() + 1; n       long long avl = m - idx + 1; n       if (idx > m) { n         ans = max(ans, ao[i] + (n - 1) * b[i]); n       } else if (avl < n) { n         long long as = 0; n         as = as + sf[avl]; n         long long rm = n - avl; n         if (ao[i] <= b[i]) { n           as += ao[i]; n           rm--; n         } n         as += rm * b[i]; n         ans = max(ans, as); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ed { n   int a, b, w; n } p[600010]; n int n, m, fa[600010], d[600010], ans; n bool cmp(ed a, ed b) { return (a.w > b.w); } n int gf(int x) { return (fa[x] != x) ? fa[x] = gf(fa[x]) : x; } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) cin >> p[i].a >> p[i].b >> p[i].w; n   sort(p + 1, p + m + 1, cmp); n   for (int i = 1; i <= n; i++) fa[i] = i, d[i] = 1; n   for (int i = 1; i <= m; i++) { n     int x = gf(p[i].a), y = gf(p[i].b); n     if (x != y && d[x] + d[y] > 0) n       fa[y] = x, ans += p[i].w, d[x] = d[x] & d[y]; n     else if (x == y && d[x]) { n       d[x] = 0, ans += p[i].w; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int arr[100005]; n int k; n void unsort(int left, int right) { n   if (k <= 0 || left >= right - 1) return; n   int mid = (left + right) / 2; n   std::swap(arr[mid - 1], arr[mid]); n   --k; n   unsort(left, mid); n   unsort(mid, right); n } n int main() { n   int n; n   scanf( %d%d , &n, &k); n   if (k % 2 == 0 || k > 2 * n) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     arr[i] = i + 1; n   } n   k /= 2; n   unsort(0, n); n   for (int i = 0; i < n; ++i) { n     printf( %d  , arr[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, i, j, res = 0, k, v1, v2; n   cin >> n; n   vector<long long> v; n   for (i = 0; i < n; i++) { n     cin >> j; n     v.emplace_back(j); n   } n   if (n == 1) { n     cout << v[0] <<   n ; n     return 0; n   } else if (n == 2) { n     cout << (v[0] | v[1]) <<   n ; n     return 0; n   } n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       v2 = v[i] | v[j]; n       for (k = j + 1; k < n; k++) { n         v1 = v2 | v[k]; n         res = max(res, v1); n       } n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100010; n string nick[MAX]; n int ara[MAX], ord[MAX], b[MAX]; n int save[MAX]; n bool cmp(int a, int b) { n   if (ara[a] == ara[b]) return nick[a] < nick[b]; n   return ara[a] > ara[b]; n } n deque<int> D; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n; n   if (n == 0) { n     cout << 0 <<     << 0 << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     cin >> nick[i] >> ara[i]; n     save[i] = ara[i]; n   } n   cin >> m; n   for (int i = 1; i <= m; i++) cin >> b[i], D.push_back(b[i]); n   sort(D.begin(), D.end()); n   string s; n   cin >> s; n   int id = -1; n   for (int i = 1; i <= n; i++) { n     if (s == nick[i]) { n       if (id == -1) n         id = i; n       else n         assert(false); n     } n   } n   for (int i = 1; i <= n; i++) ord[i] = i; n   sort(ord + 1, ord + n + 1, cmp); n   int ans1, ans2; n   if (m == 0) { n     for (int i = 1; i <= n; i++) n       if (ord[i] == id) { n         ans2 = ans1 = i; n         break; n       } n     cout << ans1 <<     << ans2 << endl; n     return 0; n   } n   int cur; n   for (int i = 1; i <= n; i++) n     if (ord[i] == id) cur = i; n   if (n == m) { n     ara[id] += D.front(); n     D.pop_front(); n   } n   int parbo = n - cur; n   int bad = D.size() - parbo; n   for (int i = cur - 1; i >= 1 and bad > 0; i--, bad--) { n     ara[ord[i]] += D.front(); n     D.pop_front(); n   } n   int need; n   for (int i = cur + 1; i <= n; i++) { n     need = (ara[id] + 1) - ara[ord[i]]; n     int paic = 0; n     while (D.size() > 0) { n       if (D.front() >= need) { n         paic = D.front(); n         D.pop_front(); n         break; n       } else n         D.pop_front(); n     } n     ara[ord[i]] += paic; n   } n   sort(ord + 1, ord + n + 1, cmp); n   for (int i = 1; i <= n; i++) n     if (ord[i] == id) ans2 = i; n   for (int i = 1; i <= n; i++) ara[i] = save[i]; n   D.clear(); n   for (int i = 1; i <= m; i++) D.push_back(b[i]); n   sort(D.begin(), D.end()); n   ara[id] += D.back(); n   D.pop_back(); n   sort(ord + 1, ord + n + 1, cmp); n   for (int i = n; i >= 1 and D.size() > 0; i--) { n     if (ord[i] == id) continue; n     ara[ord[i]] += D.back(); n     D.pop_back(); n   } n   sort(ord + 1, ord + n + 1, cmp); n   for (int i = 1; i <= n; i++) n     if (ord[i] == id) ans1 = i; n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n vector<int> a[N]; n int d[N]; n void work(int l, int r) { n   d[l]++; n   d[r + 1]--; n } n int main() { n   cin.tie(0); n   cin.sync_with_stdio(false); n   memset(d, 0, sizeof(d)); n   int n, c, m, tp, i, j, x, y; n   cin >> n >> c; n   int sum = 0, flag = 1; n   for (i = 1; i <= n; i++) { n     cin >> m; n     a[i].push_back(m); n     for (j = 1; j <= a[i][0]; j++) { n       cin >> m; n       a[i].push_back(m); n     } n   } n   for (i = 1; i < n; i++) { n     tp = min(a[i][0], a[i + 1][0]); n     for (j = 1; j <= tp; j++) { n       x = a[i][j]; n       y = a[i + 1][j]; n       if (x != y) break; n     } n     if (j > tp && a[i][0] > a[i + 1][0]) { n       cout << -1 << endl; n       return 0; n     } n     if (y > x) { n       work(0, c - y); n       work(c + 1 - x, c - 1); n     } else if (x > y) { n       work(c + 1 - x, c - y); n     } else n       work(0, c - 1); n   } n   for (int i = 0; i < c; i++) { n     sum += d[i]; n     if (sum == n - 1) { n       cout << i << endl; n       flag = 0; n       break; n     } n   } n   if (flag) cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, M = N * N; n bool vis[N]; n char s[N][2][25]; n int n, m, dfn[N], low[N], tt, S[N], col[N]; n int hd[N], V[M], nx[M], fs[N], q[N], id[N], in[N]; n vector<int> e[N]; n void add(int u, int v) { n   nx[++tt] = hd[u]; n   V[hd[u] = tt] = v; n } n void tarjan(int x) { n   dfn[x] = low[x] = ++tt; n   S[++S[0]] = x; n   vis[x] = 1; n   for (int i = hd[x]; i; i = nx[i]) { n     if (!dfn[V[i]]) n       tarjan(V[i]), low[x] = min(low[x], low[V[i]]); n     else if (vis[V[i]]) n       low[x] = min(low[x], dfn[V[i]]); n   } n   if (dfn[x] == low[x]) { n     col[x] = ++col[0]; n     while (S[S[0]] != x) vis[S[S[0]]] = 0, col[S[S[0]--]] = col[0]; n     S[0]--; n     vis[x] = 0; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %s%s , s[i][0], s[i][1]); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       bool J = 1; n       for (int k = 0; k < 3; k++) J &= (s[i][0][k] == s[j][0][k]); n       if (J) add(i, i + n), add(j, j + n); n       J = (s[i][1][0] == s[j][1][0]); n       for (int k = 0; k < 2; k++) J &= (s[i][0][k] == s[j][0][k]); n       if (J) add(i + n, j), add(j + n, i); n       J = (s[i][0][2] == s[j][1][0]); n       for (int k = 0; k < 2; k++) J &= (s[i][0][k] == s[j][0][k]); n       if (J) add(i, j), add(j + n, i + n); n       J = (s[i][1][0] == s[j][0][2]); n       for (int k = 0; k < 2; k++) J &= (s[i][0][k] == s[j][0][k]); n       if (J) add(j, i), add(i + n, j + n); n     } n   tt = 0; n   for (int i = 1; i <= n + n; i++) n     if (!dfn[i]) tarjan(i); n   for (int i = 1; i <= n; i++) n     if (col[i] == col[i + n]) { n       puts( NO ); n       return 0; n     } n   puts( YES ); n   for (int i = 1; i <= n + n; i++) n     for (int j = hd[i]; j; j = nx[j]) n       if (col[V[j]] != col[i]) e[col[i]].push_back(col[V[j]]), in[col[V[j]]]++; n   int l = 1, r = 0; n   for (int i = 1; i <= col[0]; i++) n     if (!in[i]) q[id[i] = ++r] = i; n   while (l <= r) { n     int x = q[l++], z = e[x].size(); n     for (int v, i = 0; i < z; i++) n       if (!(--in[v = e[x][i]])) q[id[v] = ++r] = v; n   } n   for (int i = 1; i <= n; i++) { n     if (id[col[i]] < id[col[i + n]]) s[i][0][2] = s[i][1][0]; n     for (int k = 0; k < 3; k++) putchar(s[i][0][k]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char c; n   int ans = 0; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       cin >> c; n       if (c ==  . ) continue; n       if (c ==  Q ) ans += 9; n       if (c ==  q ) ans -= 9; n       if (c ==  R ) ans += 5; n       if (c ==  r ) ans -= 5; n       if (c ==  B ) ans += 3; n       if (c ==  b ) ans -= 3; n       if (c ==  N ) ans += 3; n       if (c ==  n ) ans -= 3; n       if (c ==  P ) ans += 1; n       if (c ==  p ) ans -= 1; n     } n   } n   if (ans == 0) { n     cout <<  Draw  << endl; n   } else if (ans > 0) { n     cout <<  White  << endl; n   } else { n     cout <<  Black  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long base = 31; n int xx[4] = {0, 0, 1, -1}; n int yy[4] = {-1, 1, 0, 0}; n string step =  URDL ; n long long Pow(long long a, long long b) { n   long long res = 1; n   if (b <= 0) return 1; n   while (b > 0) { n     if (b % 2) { n       res = (res % mod * a % mod); n       res %= mod; n     } n     a = (a % mod * a % mod) % mod; n     b /= 2; n   } n   return res % mod; n } n const int maxn = 1e6 + 1; n long long a[maxn], b[maxn]; n string s; n void solve() { n   cin >> s; n   int n = s.size(); n   for (int i = 0; i < n; i++) { n     a[i + 1] = a[i]; n     b[i + 1] = b[i]; n     if (s[i] ==  a ) n       a[i + 1]++; n     else n       b[i]++; n   } n   long long res = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  b ) { n       res = (res % mod + (Pow(2, a[i + 1]) - 1)) % mod; n     } n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n long long int n, k; n long long int a[100010], max; n long long int print[100010], pc; n int main(void) { n   long long int i, j, f, l, mid; n   long long int sum, baki = 2147483647; n   scanf( %I64d %I64d , &n, &k); n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n     if (max < a[i]) max = a[i]; n   } n   if (k == 0) { n     for (i = 1; i <= n; i++) { n       printf( %I64d  , i); n     } n     return false; n   } n   f = 1, l = max; n   for (;;) { n     mid = (f + l) / 2; n     sum = 0; n     for (i = 1; i <= n; i++) { n       if (a[i] >= mid) n         sum += mid; n       else n         sum += a[i]; n     } n     if (sum >= k) { n       if (baki > mid) baki = mid; n       l = mid - 1; n     } else { n       f = mid + 1; n     } n     if (f > l) break; n   } n   if (baki == 2147483647) { n     printf( -1 n ); n     return false; n   } n   for (i = 1; i <= n; i++) { n     if (baki > a[i]) n       k -= a[i]; n     else n       k -= baki - 1; n   } n   for (i = 1; i <= n; i++) { n     if (baki <= a[i]) k--; n     if (k == 0) break; n   } n   if (k > 0) { n     printf( -1 n ); n     return false; n   } n   for (j = i + 1; j <= n; j++) { n     if (baki <= a[j]) { n       print[++pc] = j; n     } n   } n   for (j = 1; j <= i; j++) { n     if (baki < a[j]) { n       print[++pc] = j; n     } n   } n   for (i = 1; i <= pc; i++) { n     printf( %I64d  , print[i]); n   } n   return false; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pr(const string& name, T t) { n   cerr << name <<  :   << t << endl; n } n template <typename T, typename... Types> n void pr(const string& names, T t, Types... rest) { n   auto comma_pos = names.find( , ); n   cerr << names.substr(0, comma_pos) <<  :   << t <<  ,  ; n   auto next_name_pos = names.find_first_not_of(   t n , comma_pos + 1); n   pr(string(names, next_name_pos), rest...); n } n template <class T, class U> n ostream& operator<<(ostream& o, const pair<T, U>& v) { n   return o <<  (  << v.first <<  ,   << v.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& v) { n   o <<  { ; n   for (int i = 0; i < (int)((int)((v).size())); ++i) n     o << (i ?  ,   :   ) << v[i]; n   return o <<  } ; n } n const int dx[] = {0, 1, 0, -1}; n const int dy[] = {-1, 0, 1, 0}; n const int MAX_N = 2e5 + 10; n vector<int> G[MAX_N]; n vector<int> rG[MAX_N]; n int cmp[MAX_N]; n bool vis[MAX_N]; n int V; n vector<int> vs; n void dfs(int v) { n   vis[v] = true; n   for (auto e : G[v]) n     if (!vis[e]) dfs(e); n   vs.push_back(v); n } n void rdfs(int v, int k) { n   vis[v] = true; n   cmp[v] = k; n   for (auto e : rG[v]) n     if (!vis[e]) rdfs(e, k); n } n int scc() { n   vs.clear(); n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < (int)(V); ++i) n     if (!vis[i]) dfs(i); n   memset(vis, 0, sizeof(vis)); n   int k = 0; n   reverse((vs).begin(), (vs).end()); n   for (auto e : vs) n     if (!vis[e]) rdfs(e, k++); n   return k; n } n int un[MAX_N]; n int find(int x) { n   if (un[x] == x) return x; n   return un[x] = find(un[x]); n } n void unit(int a, int b) { un[find(a)] = find(b); } n int main(int argc, char* argv[]) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   memset(un, -1, sizeof(un)); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < (int)(n); ++i) un[i] = i; n   for (int i = 0; i < (int)(m); ++i) { n     int a, b; n     cin >> a >> b; n     --a, --b; n     G[a].push_back(b); n     rG[b].push_back(a); n     unit(a, b); n   } n   V = n; n   scc(); n   map<int, set<int> > app; n   for (int i = 0; i < (int)(n); ++i) app[find(i)].insert(i); n   int ans = 0; n   for (auto& e : app) { n     if ((int)((e.second).size()) == 1) continue; n     set<int> cnum; n     for (auto v : e.second) cnum.insert(cmp[v]); n     if ((int)((cnum).size()) != (int)((e.second).size())) n       ans += (int)((e.second).size()); n     else n       ans += (int)((e.second).size()) - 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < 10; i++) { n   } n   set<int> s(a.begin(), a.end()); n   if (s.size() < k) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n     int cnt = 0; n     for (auto item : s) { n       if (cnt == k) { n         break; n       } n       for (int i = 0; i < n; i++) { n         if (a[i] == item) { n           cout << i + 1 <<    ; n           cnt++; n           break; n         } n       } n     } n     for (int i = 0; i < 10; i++) { n     } n     cout << endl; n   } n   for (int i = 0; i < 10; i++) { n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[100005]; n vector<int> rG[100005]; n int f[100005]; n int n, m; n bool canUsing[100005]; n bool canAss[100005]; n void dfs0(int u) { n   canUsing[u] = 1; n   if (f[u] == 1) return; n   for (int i = 0; i < rG[u].size(); i++) { n     int v = rG[u][i]; n     if (canUsing[v]) continue; n     dfs0(v); n   } n } n void dfs1(int u) { n   canAss[u] = 1; n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (canAss[v]) continue; n     dfs1(v); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &f[i]); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     rG[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (f[i] == 2 && canUsing[i] == 0) dfs0(i); n     if (f[i] == 1 && canAss[i] == 0) dfs1(i); n   } n   for (int i = 1; i <= n; i++) { n     int t = canAss[i] & canUsing[i]; n     printf( %d n , t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool letras[26]; n int main() { n   list<char> kbd; n   list<char>::iterator i; n   int t, j; n   bool foundKbd; n   string s; n   for (cin >> t; t > 0; t--) { n     foundKbd = true; n     cin >> s; n     for (j = 0; j < s.size() && foundKbd; j++) { n       if (kbd.size() == 0) { n         kbd.push_back(s.at(j)); n         letras[s.at(j) -  a ] = true; n       } else if (letras[s.at(j) -  a ] == false) { n         for (i = kbd.begin(); i != kbd.end() && *i != s.at(j - 1); i++) n           ; n         if (i == kbd.begin()) { n           kbd.push_front(s.at(j)); n           letras[s.at(j) -  a ] = true; n         } else if (i == --kbd.end()) { n           kbd.push_back(s.at(j)); n           letras[s.at(j) -  a ] = true; n         } else n           foundKbd = false; n       } else { n         for (i = kbd.begin(); i != kbd.end() && *i != s.at(j - 1); i++) n           ; n         if (*(++i) == s.at(j)) n           --i; n         else if (*(--(--i)) != s.at(j)) n           foundKbd = false; n       } n     } n     if (foundKbd) { n       cout <<  YES  << endl; n       for (j = 0; j < 26; j++) n         if (letras[j] == false) kbd.push_back( a  + j); n       for (i = kbd.begin(); i != kbd.end(); i++) cout << *i; n       cout << endl; n     } else n       cout <<  NO  << endl; n     kbd.clear(); n     for (j = 0; j < 26; j++) letras[j] = false; n   } n } n