#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1024; n int n, m; n bool b[MAXN][MAXN]; n int main() { n   cin >> n >> m; n   for (register int l = (1); l < (int)(m + 1); ++l) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     b[x][y] = true; n     for (register int xx = (x - 2); xx < (int)(x + 1); ++xx) n       for (register int yy = (y - 2); yy < (int)(y + 1); ++yy) n         if (xx >= 0 && yy >= 0) { n           bool ok = true; n           for (register int i = (0); i < (int)(3); ++i) n             for (register int j = (0); j < (int)(3); ++j) n               if (!b[i + xx][j + yy]) ok = false; n           if (ok) { n             cout << l << endl; n             return 0; n           } n         } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n template <int MOD> n struct Num { n   int x; n   Num() : x(0) {} n   explicit Num(int x, bool skip_check = false) { n     if (skip_check) n       this->x = x; n     else { n       if (x >= 0 && x < MOD) n         this->x = x; n       else { n         this->x = x % MOD; n         if (this->x < 0) this->x += MOD; n       }; n     } n   } n   Num<MOD> operator+(const Num<MOD>& oth) const { n     int rem = this->x + oth.x; n     if (rem >= MOD) rem -= MOD; n     return Num(rem, true); n   } n   Num<MOD> operator-(const Num<MOD>& oth) const { n     int rem = this->x - oth.x; n     if (rem < 0) rem += MOD; n     return Num(rem, true); n   } n   Num<MOD> operator*(const Num<MOD>& oth) const { n     int rem = (long long)this->x * oth.x % MOD; n     return Num(rem, true); n   } n   Num<MOD> to_pow(int n) const { n     Num<MOD> res(1); n     Num<MOD> cur = *this; n     while (n) { n       if (n & 1) res = res * cur; n       cur = cur * cur; n       n >>= 1; n     } n     return res; n   } n   Num<MOD> inv_prime() const { return this->to_pow(MOD - 2); } n   Num<MOD> operator/(const Num<MOD>& oth) const { n     return *this * oth.inv_prime(); n   } n   bool operator==(const Num<MOD>& oth) const { return x == oth.x; } n   bool operator!=(const Num<MOD>& oth) const { return !(x == oth); } n }; n int sgn(long long x) { n   if (x > 0) return 1; n   if (x < 0) return -1; n   return 0; n } n struct Frac { n   int numer, den; n   explicit Frac(int numer, int den) : numer(numer), den(den) {} n   int cmp(const Frac& oth) const { n     long long res_numer = n         (long long)numer * oth.den - (long long)oth.numer * den; n     long long res_den = (long long)den * oth.den; n     return sgn(res_numer) * sgn(res_den); n   } n }; n const int mod = 998244353; n struct Event { n   int lind; n   int ldir, rdir; n   Frac t; n   Event(int lind, int ldir, int rdir, Frac t) n       : lind(lind), ldir(ldir), rdir(rdir), t(t) {} n   bool operator<(const Event& oth) const { return this->t.cmp(oth.t) < 0; } n }; n struct Pipe { n   int x, v, p_right; n   Pipe() {} n   Pipe(int x, int v, int p_right) : x(x), v(v), p_right(p_right) {} n }; n struct TreeNodeData { n   Num<mod> p_ok[2][2]; n   Num<mod> p_all[2][2]; n   TreeNodeData() {} n   TreeNodeData combine(const TreeNodeData& oth, const int forb[2][2]) const { n     TreeNodeData res; n     for (int d1 = 0; d1 < 2; ++d1) n       for (int d2 = 0; d2 < 2; ++d2) n         for (int d3 = 0; d3 < 2; ++d3) n           for (int d4 = 0; d4 < 2; ++d4) { n             res.p_all[d1][d4] = n                 res.p_all[d1][d4] + p_all[d1][d2] * oth.p_all[d3][d4]; n             if (forb[d2][d3]) continue; n             res.p_ok[d1][d4] = n                 res.p_ok[d1][d4] + p_ok[d1][d2] * oth.p_ok[d3][d4]; n           } n     return res; n   } n }; n const int maxtr = 270000; n TreeNodeData tree[maxtr]; n const int maxn = 100010; n int forb[maxn][2][2]; n vector<TreeNodeData> leaves; n void build_tree(int v, int L, int R) { n   if (L == R) { n     tree[v] = leaves[L]; n     return; n   } n   int m = (L + R) >> 1; n   build_tree(v + v + 1, L, m); n   build_tree(v + v + 2, m + 1, R); n   tree[v] = tree[v + v + 1].combine(tree[v + v + 2], forb[m]); n } n void tree_update(int v, int L, int R, int pos) { n   if (L == R) return; n   int m = (L + R) >> 1; n   if (pos <= m) n     tree_update(v + v + 1, L, m, pos); n   else n     tree_update(v + v + 2, m + 1, R, pos); n   tree[v] = tree[v + v + 1].combine(tree[v + v + 2], forb[m]); n } n int main() { n   int n; n   scanf( %d , &n); n   vector<Pipe> pipes; n   for (int i = 0; i < n; ++i) { n     int cx, cv, cp; n     scanf( %d%d%d , &cx, &cv, &cp); n     pipes.push_back(Pipe(cx, cv, cp)); n   } n   for (auto pipe : pipes) { n     TreeNodeData curres; n     curres.p_ok[1][1] = curres.p_all[1][1] = n         Num<mod>(pipe.p_right) / Num<mod>(100); n     curres.p_ok[0][0] = curres.p_all[0][0] = n         Num<mod>(100 - pipe.p_right) / Num<mod>(100); n     leaves.push_back(curres); n   } n   build_tree(0, 0, n - 1); n   vector<Event> events; n   for (int i = 0; i + 1 < n; ++i) { n     auto p1 = pipes[i]; n     auto p2 = pipes[i + 1]; n     events.push_back(Event(i, 1, 0, Frac(p2.x - p1.x, p1.v + p2.v))); n     if (p1.v > p2.v) n       events.push_back(Event(i, 1, 1, Frac(p2.x - p1.x, p1.v - p2.v))); n     else if (p2.v > p1.v) n       events.push_back(Event(i, 0, 0, Frac(p2.x - p1.x, p2.v - p1.v))); n   } n   sort(events.begin(), events.end()); n   Num<mod> result(0); n   Num<mod> pred_nok_prob(0); n   for (int ev_ind = 0; ev_ind < int((events).size());) { n     int fr_ev = ev_ind; n     int to_ev = ev_ind; n     while (to_ev < int((events).size()) && n            events[fr_ev].t.cmp(events[to_ev].t) == 0) n       ++to_ev; n     Num<mod> cur_ok_prob(tree[0].p_ok[0][0] + tree[0].p_ok[0][1] + n                          tree[0].p_ok[1][0] + tree[0].p_ok[1][1]); n     for (int i = fr_ev; i < to_ev; ++i) { n       forb[events[i].lind][events[i].ldir][events[i].rdir] = 1; n       tree_update(0, 0, n - 1, events[i].lind); n     } n     Num<mod> cur_nok_prob = ((tree[0].p_all[0][0] + tree[0].p_all[0][1] + n                               tree[0].p_all[1][0] + tree[0].p_all[1][1]) - n                              (tree[0].p_ok[0][0] + tree[0].p_ok[0][1] + n                               tree[0].p_ok[1][0] + tree[0].p_ok[1][1])); n     Num<mod> cur_delta = (cur_nok_prob - pred_nok_prob) * n                          Num<mod>(events[fr_ev].t.numer) / n                          Num<mod>(events[fr_ev].t.den); n     result = result + cur_delta; n     pred_nok_prob = cur_nok_prob; n     ev_ind = to_ev; n   } n   cout << result.x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 1e6 + 66; n vector<int> g[N], f[N]; n int used[N]; n vector<int> topsort; n void dfs(int v) { n   used[v] = 1; n   for (int to : g[v]) n     if (used[to] != 1) dfs(to); n   topsort.emplace_back(v); n } n int c[N]; n void color(int v) { n   if (false) cerr << v <<   color n ; n   if (c[v] == 0) c[v] = v; n   used[v] = 2; n   for (int to : f[v]) n     if (used[to] != 2) c[to] = c[v], color(to); n } n int S; n vector<int> newg[N]; n void fnd(int v) { n   if (false) cerr << v <<   fnd n ; n   used[v] = 3; n   for (int to : newg[v]) n     if (used[to] != 3) fnd(to); n   if (S == -1) S = v; n } n void clr(int v) { n   used[v] = 4; n   for (int to : g[v]) n     if (used[to] != 4) clr(to); n } n void solve() { n   int n, m; n   cin >> n >> m; n   topsort.clear(); n   for (int i = 1; i <= n; ++i) { n     used[i] = 0; n     g[i].clear(); n     f[i].clear(); n     newg[i].clear(); n     c[i] = 0; n   } n   for (int i = 1; i <= m; ++i) { n     int a, b; n     cin >> a >> b; n     g[a].emplace_back(b); n     f[b].emplace_back(a); n   } n   for (int i = 1; i <= n; ++i) { n     if (!used[i]) { n       dfs(i); n     } n   } n   reverse(topsort.begin(), topsort.end()); n   for (int i : topsort) { n     if (used[i] != 2) { n       color(i); n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (false) cerr << c[i] <<    n [i == n]; n     for (int j : g[i]) { n       newg[c[i]].emplace_back(c[j]); n     } n   } n   S = -1, fnd(c[1]); n   clr(S); n   if (false) cerr << S <<   S n ; n   vector<int> j, p; n   for (int i = 1; i <= n; ++i) { n     if (used[i] != 4) n       p.emplace_back(i); n     else n       j.emplace_back(i); n   } n   assert(!j.empty()); n   if (p.empty()) { n     cout <<  No n ; n   } else { n     cout <<  Yes n  << j.size() <<     << p.size() <<   n ; n     for (int i : j) cout << i <<    ; n     cout <<   n ; n     for (int i : p) cout << i <<    ; n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int TT; n   cin >> TT; n   while (TT--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void read_file(bool outToFile = true) {} n int n, K; n const int MAXN = 200000 + 99; n int A[MAXN]; n set<int> X; n int vis[MAXN]; n bool check() { n   memset(vis, 0, sizeof vis); n   stack<int> s; n   int idx = 0; n   int cur = 1; n   while (true) { n     while (s.empty() || s.top() != cur) { n       s.push(A[idx]); n       vis[A[idx]] = 1; n       idx++; n     } n     s.pop(); n     cur++; n     if (cur == n + 1) break; n     if (vis[cur] && cur != s.top()) return false; n   } n   return true; n } n int main() { n   read_file(); n   while (scanf( %d%d , &n, &K) != -1) { n     X.clear(); n     memset(vis, 0, sizeof vis); n     for (int i = 0; i < K; i++) { n       scanf( %d , &A[i]); n       if (A[i] != 1) X.insert(A[i] - 1); n       vis[A[i]] = 1; n     } n     for (int i = 0; i < K; i++) { n       if (X.count(A[i])) X.erase(A[i]); n     } n     int idx = K; n     while (!X.empty()) { n       int x = *X.begin(); n       X.erase(X.begin()); n       A[idx++] = x; n       vis[x] = 1; n       if (x == 1) continue; n       if (!vis[x - 1]) X.insert(x - 1); n     } n     int mx = 0; n     for (int i = 0; i < idx; i++) mx = max(mx, A[i]); n     for (int i = n; i > mx; i--) A[idx + n - i] = i; n     if (!check()) { n       printf( -1 n ); n       continue; n     } n     for (int i = 0; i < n; i++) printf( %d%c , A[i],    n [i == n - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, t, tt, i; n   long long t1, t2, t3, t4; n   while (~scanf( %lld %lld , &a, &b)) { n     t = b - a; n     t1 = a % 10; n     t2 = b % 10; n     t3 = (a / 10) % 10; n     t4 = (b / 10) % 10; n     if (t >= 10 || (t4 != t3)) { n       printf( 0 n ); n     } else { n       if (t1 <= 2 && t2 >= 5) n         printf( 0 n ); n       else { n         tt = 1; n         for (i = t1 + 1; i <= t2; i++) { n           tt *= i; n         } n         printf( %lld n , tt % 10); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100050, mod = 1000000007; n ; n int n, ans, k; n int fa[maxN], c[maxN]; n int getfa(int u) { n   if (fa[u] == u) return u; n   int t = fa[u]; n   fa[u] = getfa(fa[u]); n   while (c[u] < 0) c[u] += mod; n   while (c[t] < 0) c[t] += mod; n   c[u] = (c[u] + c[t]) % mod; n   return fa[u]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n     c[i] = 0; n   } n   int v, x; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &k); n     for (int j = 1; j <= k; j++) { n       scanf( %d%d , &v, &x); n       int t = getfa(v); n       fa[t] = i; n       while (c[v] < 0) c[v] += mod; n       while (x < 0) x += mod; n       c[t] = (c[v] + x) % mod; n       while (c[t] < 0) c[t] += mod; n       while (ans < 0) ans += mod; n       ans = (ans + c[t]) % mod; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:5000000000 ) n const long long mod = 1000000007; n long long Inf = (long long)2e9; n long long LINF = (long long)1e18 + 1e17; n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<long long> a(n); n   vector<bool> can(n, true); n   map<long long, set<int>> pos; n   for (int(i) = 0; (i) < n; (i)++) { n     cin >> a[i]; n     pos[a[i]].insert(i); n   } n   int k = n; n   while (pos.size()) { n     if (pos.begin()->second.size() < 2) { n       pos.erase(pos.begin()); n       continue; n     } n     int i1 = *pos.begin()->second.begin(); n     pos.begin()->second.erase(i1); n     int i2 = *pos.begin()->second.begin(); n     pos.begin()->second.erase(i2); n     long long x = pos.begin()->first; n     k--; n     can[i1] = false; n     pos[2 * x].insert(i2); n     a[i2] = 2 * x; n   } n   cout << k << endl; n   for (int(i) = 0; (i) < n; (i)++) n     if (can[i]) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30); n const int N = 100005; n int n, k; n int g[105][105]; n int main() { n   int i, j, t, cas = 0; n   memset(g, 0, sizeof(g)); n   scanf( %d , &k); n   for (n = 3; n <= 100; n++) { n     if ((n) * (n - 1) * (n - 2) / 6 > k) break; n   } n   n--; n   k -= (n) * (n - 1) * (n - 2) / 6; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       if (i == j) continue; n       g[i][j] = 1; n     } n   } n   while (k) { n     for (i = 2;; i++) { n       if ((i) * (i - 1) / 2 > k) break; n     } n     i--; n     n++; n     for (j = 1; j <= i; j++) g[j][n] = g[n][j] = 1; n     k -= (i) * (i - 1) / 2; n   } n   printf( %d n , n); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       printf( %d , g[i][j]); n     } n     puts(  ); 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 long long int mod = 1e9 + 7; n long long int mod1 = 998244351; n long long int power(long long int a, long long int b) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return power(a * a % mod, b / 2) % mod; n   else n     return (a % mod * power(a * a % mod, b / 2)) % mod; n } n long long int bit(long long int x) { return __builtin_popcountll(x); } n long long int temp1, temp2, temp3, temp4; n void fi(long long int x3, long long int y3, long long int x4, n         long long int y4) { n   long long int x5 = max(temp1, x3); n   long long int y5 = max(temp2, y3); n   long long int x6 = min(temp3, x4); n   long long int y6 = min(temp4, y4); n   if (x5 > x6 || y5 > y6) { n     temp1 = -1000000009, temp2 = -1e9 - 1, temp3 = -1e9 - 1, temp4 = -1e9 - 1; n     return; n   } n   temp1 = x5; n   temp2 = y5; n   temp3 = x6; n   temp4 = y6; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n; n   cin >> n; n   long long int a[n], b[n], c[n], d[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i] >> b[i] >> c[i] >> d[i]; n   } n   temp1 = a[0], temp2 = b[0], temp3 = c[0], temp4 = d[0]; n   long long int count = 0; n   vector<int> index; n   for (int i = 1; i < n; ++i) { n     long long int p = temp1, p1 = temp2, p3 = temp3, p4 = temp4; n     fi(a[i], b[i], c[i], d[i]); n     if (temp1 == -1000000009) { n       temp1 = p; n       temp2 = p1; n       temp3 = p3; n       temp4 = p4; n       count++; n     } n   } n   if (count <= 1) { n     cout << temp1 <<     << temp2 <<   n ; n     return 0; n   } n   temp1 = a[n - 1]; n   temp2 = b[n - 1]; n   temp3 = c[n - 1]; n   temp4 = d[n - 1]; n   count = 0; n   for (int i = n - 1; i >= 0; --i) { n     long long int p = temp1, p1 = temp2, p3 = temp3, p4 = temp4; n     fi(a[i], b[i], c[i], d[i]); n     if (temp1 == -1000000009) { n       temp1 = p; n       temp2 = p1; n       temp3 = p3; n       temp4 = p4; n       count++; n     } n   } n   if (count <= 1) { n     cout << temp1 <<     << temp2 <<   n ; n     return 0; n   } n   temp1 = a[1], temp2 = b[1], temp3 = c[1], temp4 = d[1], count = 0; n   for (int i = 2; i < n; ++i) { n     long long int p = temp1, p1 = temp2, p3 = temp3, p4 = temp4; n     fi(a[i], b[i], c[i], d[i]); n   } n   cout << temp3 <<     << temp4 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, numone, numtwo, sum, tmp; n char nlong; n string nameone, nametwo; n string read; n struct node { n   string name; n   int goal, moregoal, score; n   int q; n } wxy[100]; n bool check(int t) { n   for (int i = 0; i < tmp; i++) n     if (wxy[t].name[i] != nameone[i]) return false; n   return true; n } n bool checktwo(int t) { n   for (int i = tmp + 1; i < sum; i++) n     if (wxy[t].name[i - tmp - 1] != nametwo[i - tmp - 1]) return false; n   return true; n } n bool cmpfirst(node a, node b) { n   if (a.score > b.score) return 1; n   if (a.score < b.score) return 0; n   if (a.moregoal > b.moregoal) return 1; n   if (a.moregoal < b.moregoal) return 0; n   if (a.goal > b.goal) return 1; n   return 0; n } n bool cmplast(node a, node b) { return a.name < b.name; } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> wxy[i].name; n     wxy[i].q = wxy[i].name.size(); n   } n   for (int j = 1; j <= n * (n - 1) / 2; j++) { n     sum = 0; n     tmp = 0; n     nameone =   , nametwo =   ; n     numone = numtwo = 0; n     while (1) { n       scanf( %c , &nlong); n       if (nlong ==  - ) n         break; n       else if ((nlong >=  a  && nlong <=  z ) || n                (nlong >=  A  && nlong <=  Z )) { n         nameone += nlong; n         sum++; n       } n     } n     tmp = sum; n     sum++; n     while (1) { n       scanf( %c , &nlong); n       if (nlong ==    ) n         break; n       else { n         nametwo += nlong; n         sum++; n       } n     } n     while (1) { n       scanf( %c , &nlong); n       if (nlong ==  : ) n         break; n       else if (nlong >=  0  && nlong <=  9 ) n         numone = numone * 10 + nlong -  0 ; n     } n     cin >> numtwo; n     int one, two; n     for (int i = 1; i <= n; i++) n       if (wxy[i].q == tmp) n         if (check(i)) { n           one = i; n           break; n         } n     for (int i = 1; i <= n; i++) n       if (wxy[i].q + tmp + 1 == sum) n         if (checktwo(i)) { n           two = i; n           break; n         } n     wxy[one].goal += numone; n     wxy[two].goal += numtwo; n     wxy[one].moregoal += numone - numtwo; n     wxy[two].moregoal += numtwo - numone; n     if (numone > numtwo) n       wxy[one].score += 3; n     else if (numtwo > numone) n       wxy[two].score += 3; n     else { n       wxy[one].score++; n       wxy[two].score++; n     } n   } n   sort(wxy + 1, wxy + n + 1, cmpfirst); n   sort(wxy + 1, wxy + (n) / 2 + 1, cmplast); n   for (int i = 1; i <= n / 2; i++) cout << wxy[i].name << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[26]; n char s[100005]; n int dp[100005][26]; n int main(void) { n   int n; n   scanf( %s , s + 1); n   scanf( %d , &n); n   for (int i = 0; i < 26; i++) f[i] = -1; n   for (int i = 0; i < n; i++) { n     char x[10]; n     scanf( %s , x); n     f[x[0] -  a ] = x[1] -  a ; n     f[x[1] -  a ] = x[0] -  a ; n   } n   for (int i = 1; s[i]; i++) { n     int mins = 1 << 20; n     for (int j = 0; j < 26; j++) n       if (j != f[s[i] -  a ]) mins = min(dp[i - 1][j], mins); n     for (int j = 0; j < 26; j++) { n       if (j == s[i] -  a ) { n         dp[i][j] = mins; n       } else { n         dp[i][j] = dp[i - 1][j] + 1; n       } n     } n   } n   int m = strlen(s + 1); n   printf( %d n , *min_element(dp[m], dp[m] + 26)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int a, b; n     cin >> a >> b; n     int x = abs(a - b); n     int ans = x / 5; n     x %= 5; n     ans += x / 2; n     x %= 2; n     ans += x; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, s; n vector<double> x, y11, z; n double rx, ry, rz, mn = -1000000000; n double mln(double x) { return log(x); } n void upd(double nx, double ny, double nz) { n   if (nx + ny + nz > (s + 1e-7)) return; n   double cur = 0; n   if (a) cur += a * mln(nx); n   if (b) cur += b * mln(ny); n   if (c) cur += c * mln(nz); n   if (cur > mn) { n     mn = cur; n     rx = nx; n     ry = ny; n     rz = nz; n   } n } n int main() { n   scanf( %d %d %d %d , &s, &a, &b, &c); n   if (a == 0 && b == 0 && c == 0) { n     puts( 0.0 0.0 0.0 ); n     return 0; n   } n   if (a != 0) { n     double S = s; n     double m = 1 + b * 1. / a + c * 1. / a; n     double x0 = S / m; n     x.push_back(x0); n     y11.push_back(x0 * b * 1. / a); n     z.push_back(x0 * c * 1. / a); n   } else if (b != 0) { n     double S = s; n     double m = 1 + a * 1. / b + c * 1. / b; n     double x0 = S / m; n     x.push_back(x0 * a * 1. / b); n     y11.push_back(x0); n     z.push_back(x0 * c * 1. / b); n   } else if (c != 0) { n     double S = s; n     double m = 1 + a * 1. / c + b * 1. / c; n     double x0 = S / m; n     x.push_back(x0 * a * 1. / c); n     y11.push_back(x0 * b * 1. / c); n     z.push_back(x0); n   } n   double S = s; n   x.push_back(0); n   y11.push_back(0); n   z.push_back(s); n   x.push_back(0); n   y11.push_back(s); n   z.push_back(0); n   x.push_back(s); n   y11.push_back(0); n   z.push_back(0); n   if (a + b) { n     z.push_back(0); n     z.push_back(0); n     x.push_back(a * c / (b + a + 0.)); n     y11.push_back(S - a * c / (b + a + 0.)); n     x.push_back(b * c / (b + a + 0.)); n     y11.push_back(S - b * c / (b + a + 0.)); n   } n   if (b + c) { n     x.push_back(0); n     x.push_back(0); n     y11.push_back(b * a / (b + c + 0.)); n     z.push_back(S - b * a / (b + c + 0.)); n     y11.push_back(c * a / (b + c + 0.)); n     z.push_back(S - c * a / (b + c + 0.)); n   } n   if (a + c) { n     y11.push_back(0); n     y11.push_back(0); n     x.push_back(a * b / (c + a + 0.)); n     z.push_back(S - a * b / (c + a + 0.)); n     x.push_back(c * b / (c + a + 0.)); n     z.push_back(S - c * b / (c + a + 0.)); n   } n   for (int i = 0; i < x.size(); i++) { n     upd(x[i], y11[i], z[i]); n   } n   printf( %.11lf %.11lf %.11lf n , rx, ry, rz); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Pnt { n   int x, y; n   Pnt() {} n   Pnt(int _x, int _y) { n     x = _x; n     y = _y; n   } n   bool operator<(const Pnt& o) const { n     if (x != o.x) return x < o.x; n     return y < o.y; n   } n   void show() { printf( Pnt %d %d n , x, y); } n }; n const int MAXN = 1000005; n const int MAXS = 20; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n int n, m; n char s[MAXN]; n int f[MAXN][2]; n void preGao() {} n void init() { n   if (scanf( %s , s) == EOF) exit(0); n   n = strlen(s); n } n void work() { n   f[n][0] = 0; n   f[n][1] = 2; n   for (int i = n - 1; i >= 0; i--) { n     f[i][0] = f[i + 1][0] + (s[i] ==  1 ); n     f[i][0] = min(f[i][0], f[i + 1][1]); n     f[i][1] = f[i + 1][0] + 2 + (s[i] ==  0 ); n     f[i][1] = min(f[i][1], f[i + 1][1] + (s[i] ==  0 )); n   } n   printf( %d n , min(f[0][0], f[0][1])); n } n int main() { n   preGao(); n   while (true) { n     init(); n     work(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const int INF = 1000000000; n const int dx[4] = {0, 0, 1, -1}; n const int dy[4] = {1, -1, 0, 0}; n const int N = 100001; n int n, a[N]; n int ans; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; (i) < (n); ++i) cin >> a[i]; n   sort(a, a + n); n   int c = 1; n   for (int i = 0; i < n; i++) { n     if (a[i] >= c) { n       c++; n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const int inf = 1e9; n char aa[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     for (int i = 1; i <= n; i++) { n       cin >> aa[i]; n     } n     sort(aa + 1, aa + 1 + n); n     if (aa[1] != aa[k]) n       cout << aa[k] << endl; n     else { n       int mark = 1; n       if (aa[n] == aa[k + 1]) mark = 0; n       if (mark == 0) { n         for (int i = 1; i <= n; i += k) cout << aa[i]; n         cout << endl; n       } else { n         cout << aa[1]; n         for (int i = k + 1; i <= n; i++) cout << aa[i]; n         cout << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[14]; n int dp[1 << 14], dp2[14][14][1 << 14]; n vector<int> re[1 << 14]; n pair<int, int> last_pair[1 << 14]; n int pre[1 << 14]; n int last[14][14][1 << 14]; n bool v[1 << 14]; n int main() { n   int n, m, x, y; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     G[x - 1].push_back(y - 1); n     G[y - 1].push_back(x - 1); n   } n   for (int i = 0; i < n; i++) v[1 << i] = 1; n   memset(dp, 0x3f, sizeof(dp)); n   dp[1] = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < (1 << n); k++) { n         dp2[i][j][k] = 0x3f3f3f3f; n       } n   for (int i = 0; i < n; i++) n     for (auto x : G[i]) { n       dp2[i][x][0] = 1; n       last[i][x][0] = i; n     } n   for (int i = 0; i < (1 << n); i++) { n     for (int j = 0; j < n; j++) { n       if ((i >> j) & 1) continue; n       for (int k = 0; k < n; k++) { n         if ((i >> k) & 1) continue; n         if (j == k || dp2[j][k][i] == 0x3f3f3f3f) continue; n         for (auto z : G[k]) { n           if ((i >> z) & 1) continue; n           if (z == last[j][k][i]) continue; n           int tmp = i ^ (1 << k); n           if (dp2[j][z][tmp] == 0x3f3f3f3f) { n             dp2[j][z][tmp] = 1; n             last[j][z][tmp] = k; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) dp[1 << i] = 0; n   for (int i = 0; i < (1 << n); i++) n     for (int j = 0; j < n; j++) { n       if ((i >> j) & 1) re[i].push_back(j); n     } n   for (int i = 0; i < (1 << n); i++) { n     for (int j = i; j; j = (j - 1) & i) { n       int tmp = i ^ j; n       int cnt = __builtin_popcount(j) + 1; n       if (dp[i] < dp[tmp] + cnt) continue; n       for (auto x : re[tmp]) n         for (auto y : re[tmp]) { n           if (dp2[x][y][j] == 1) { n             dp[i] = min(dp[i], dp[tmp] + cnt); n             last_pair[i] = make_pair(x, y); n             pre[i] = tmp; n           } n         } n     } n   } n   if (dp[(1 << n) - 1] == 0x3f3f3f3f) { n     printf( -1 n ); n   } else { n     printf( %d n , dp[(1 << n) - 1]); n     int now = (1 << n) - 1; n     while (!v[now]) { n       int x = last_pair[now].first, y = last_pair[now].second; n       int tmp = now ^ pre[now]; n       while (tmp) { n         int tmp1 = last[x][y][tmp]; n         printf( %d %d n , y + 1, tmp1 + 1); n         y = tmp1; n         tmp ^= (1 << tmp1); n       } n       printf( %d %d n , x + 1, y + 1); n       now = pre[now]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n void faster() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n } n long long n, m; n vector<long long> vst; n vector<long long> dis; n long long bfs(long long u, vector<set<long long> > &adj) { n   queue<pair<long long, long long> > q; n   q.push(make_pair(u, 0)); n   while (!q.empty()) { n     long long v, d; n     tie(v, d) = q.front(); n     q.pop(); n     vst[v] = 1; n     dis[v - 1] = d; n     for (auto e : adj[v]) { n       if (!vst[e]) { n         q.push(make_pair(e, d + 1)); n         vst[e] = 1; n       } n     } n   } n   return dis[n - 1]; n } n void solve() { n   cin >> n >> m; n   vector<set<long long> > adj(n + 1); n   vst.resize(n + 1); n   dis.resize(n); n   fill((dis).begin(), (dis).end(), 1000); n   bool flag = true; n   for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m)); n        i += 1 - 2 * ((0) > (m))) { n     long long x, y; n     cin >> x >> y; n     if (min(x, y) == 1 && max(x, y) == n) flag = false; n     adj[x].insert(y); n     adj[y].insert(x); n   } n   if (flag) { n     long long dist = bfs(1, adj); n     if (dist == 1000) dist = -1; n     cout << dist << endl; n   } else { n     vector<set<long long> > adjp(n + 1); n     for (long long i = 1; i < n + 1; i++) { n       for (long long j = 1; j < n + 1; j++) { n         if (j != i && !adj[i].count(j)) adjp[i].insert(j); n       } n     } n     long long dist = bfs(1, adjp); n     if (dist == 1000) dist = -1; n     cout << dist << endl; n   } n } n signed main() { n   faster(); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n, k; n vector<pair<int, int> > g[maxn]; n int d[maxn], par[maxn]; n void dfs1(int u, int fa) { n   par[u] = fa; n   for (auto p : g[u]) { n     int v = p.first; n     if (v == fa) continue; n     d[v] = d[u] + p.second; n     dfs1(v, u); n   } n } n int a[maxn]; n bool f[maxn]; n int c[maxn]; n void dfs(int u, int fa) { n   d[u] = 0; n   par[u] = fa; n   for (auto p : g[u]) { n     int v = p.first; n     if (v == fa) continue; n     dfs(v, u); n     c[v] = p.second; n     if (!f[v]) d[u] = max(d[u], d[v] + p.second); n   } n } n int l[maxn], r[maxn], t[maxn], tail = 1; n int main() { n   scanf( %d%d , &n, &k); n   int u, v, w; n   for (int i = 1; i <= n - 1; i++) n     scanf( %d%d%d , &u, &v, &w), g[u].push_back(pair<int, int>(v, w)), n         g[v].push_back(pair<int, int>(u, w)); n   dfs1(1, -1); n   d[1] = 0; n   int rt = 1; n   for (int i = 1; i <= n; i++) n     if (d[i] > d[rt]) rt = i; n   d[rt] = 0; n   dfs1(rt, -1); n   int y = rt; n   for (int i = 1; i <= n; i++) n     if (d[i] > d[y]) y = i; n   int p = y; n   int m = 0; n   while (p > 0) a[++m] = p, p = par[p]; n   dfs(rt, -1); n   for (int i = 1; i <= n; i++) l[i] = d[i]; n   dfs(y, -1); n   for (int i = 1; i <= n; i++) r[i] = d[i]; n   for (int i = 1; i <= m; i++) f[a[i]] = 1; n   dfs(rt, -1); n   tail = 1; n   int head = 1; n   int ans = 1e9; n   for (int i = 1; i <= m; i++) { n     while (tail > head && d[a[t[tail - 1]]] <= d[a[i]]) tail--; n     t[tail++] = i; n     int q = d[a[t[head]]]; n     q = max(q, (i - k + 1 > 0 ? l[a[i - k + 1]] : 0)); n     q = max(q, r[a[i]]); n     ans = min(ans, q); n     if (t[head] == i - k) head++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   static char cc[100000 + 1]; n   int n, k, a, i; n   scanf( %d%d , &n, &k); n   a = (n - k) / 2; n   for (i = 0; i < n; i++) cc[i] =  0  + (i % (a + 1) == a); n   printf( %s n , cc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l, r, ar[110], ubah; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> l >> r; n     ar[l]++; n     ar[r + 1]--; n   } n   for (int i = 1; i <= n; i++) { n     ubah += ar[i]; n     if (ubah != 1) { n       cout << i <<     << ubah <<   n ; n       return 0; n     } n   } n   cout <<  OK n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long lvl_len(int lvl) { n   static long long len[64] = {0}; n   if (len[lvl]) n     return len[lvl]; n   else { n     len[1] = 1; n     for (int i = 2; i < 64; ++i) len[i] = len[i - 1] * 2 + 1; n   } n   return len[lvl]; n } n void rec(int lvl, unsigned long long pos) { n   if (pos < lvl_len(lvl - 1)) n     rec(lvl - 1, pos); n   else if ((pos -= lvl_len(lvl - 1)) < 1) n     cout << lvl; n   else n     rec(lvl - 1, pos - 1); n }; n int main(int, char **) { n   ios::sync_with_stdio(false); n   unsigned long long n, p; n   cin >> n >> p; n   rec(n, p - 1); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std ; n  n #define ll              long long  n #define pb              push_back n #define all(v)          v.begin(),v.end() n #define sz(a)           (ll)a.size() n #define F               first n #define S               second n #define INF             2000000000000000000 n #define popcount(x)     __builtin_popcountll(x) n #define pll             pair<ll,ll>  n #define pii             pair<int,int> n #define ld              long double n  n const int M = 1000000007; n const int MM = 998244353; n const long double PI = acos(-1); n  n template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; } n template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; } n template<typename T, typename U> ostream& operator<<(ostream &os, const pair<T, U> &p) n {  n     return os<< ( <<p.F<<  , <<p.S<< ) ;  n } n  n  n  n template <typename A, typename B> n string to_string(pair<A, B> p); n string to_string(const string& s) { n     return     + s +    ; n } n string to_string(const char* s) { n     return to_string((string) s); n } n string to_string(bool b) { n     return (b ?  true  :  false ); n } n string to_string(char c) { n     string t =     + string(1,c) +    ; n     return t; n } n string to_string(vector<bool> v) { n     bool first = true; n     string res =  { ; n     for (int i = 0; i < static_cast<int>(v.size()); i++) { n         if (!first) { n             res +=  ,  ; n         } n         first = false; n         res += to_string(v[i]); n     } n     res +=  } ; n     return res; n } n template <size_t N> n string to_string(bitset<N> v) { n     string res =   ; n     for(size_t i = 0; i < N; i++) { n         res += static_cast<char>( 0  + v[i]); n     } n     return res; n } n template <typename A> n string to_string(A v) { n     bool first = true; n     string res =  { ; n     for(const auto &x : v) { n         if (!first) { n             res +=  ,  ; n         } n         first = false; n         res += to_string(x); n     } n     res +=  } ; n     return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n  n  n template<typename T> n void debug_out(const char* name, T&& x) {  n     cerr <<  [  << name <<  ] :   << to_string(x) << endl;  n } n template <typename H, typename... T> n void debug_out(const char* names, H&& arg1, T&&... args){ n     const char* comma = strchr(names + 1,  , ); n     cerr <<  [ ; n     cerr.write(names, comma - names) <<  ] :   << to_string(arg1) <<  |  ; n     debug_out(comma+1, args...); n } n #ifdef LOCAL n #define debug(...) debug_out(#__VA_ARGS__, __VA_ARGS__) n #else n #define debug(...) 2351 n #endif n  n const int N = 300005; n  n ll ans = 0; n int in[N],out[N],cur = 1; n vector<int> v[N],g[N]; n  n set<pii> s; n  n void dfs2(int s,int p) n { n     in[s] = cur++; n     for(auto j:g[s]) n         dfs2(j,s); n     out[s] = cur-1; n } n  n void dfs(int u,int p) n { n     bool added = false; n     pii rm = {-1,-1}; n     // cout<<u<<   <<in[u]<<   <<out[u]<<  n ; n     auto it = s.lower_bound({in[u],-1}); n     if(it != s.end()) n     { n         if(it->F <= out[u]) n         { n             amax(ans,sz(s)); n             goto yo; n         } n     } n     if(it != s.begin()) n     { n         --it; n         if(it->S >= in[u]) n         { n             assert(it->F <= in[u]); n             added = true; n             rm = {it->F,it->S}; n             s.erase(rm); n             s.insert({in[u],out[u]}); n             amax(ans,sz(s)); n             goto yo; n         } n     } n     s.insert({in[u],out[u]}); n     amax(ans,sz(s)); n     yo:; n     // debug(s); n     for(auto j:v[u]) n         dfs(j,u); n     if(s.count({in[u],out[u]})) n         s.erase({in[u],out[u]}); n     if(added) n     { n         s.insert(rm); n     } n } n  n int _runtimeTerror_() n { n     ans = 0; n     int n; n     cin>>n; n     cur = 1; n     s.clear(); n     for(int i=1;i<=n;++i) n     { n         in[i] = out[i] = 0; n         v[i].clear(),g[i].clear(); n     } n     for(int i=2;i<=n;++i) n     { n         int x; n         cin>>x; n         v[x].push_back(i); n     } n     for(int i=2;i<=n;++i) n     { n         int x; n         cin>>x; n         g[x].push_back(i); n     } n     dfs2(1,0); n     dfs(1,0); n     cout<<ans<<  n ; n     return 0; n } n  n int main() n { n     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n     #ifdef runSieve n         sieve(); n     #endif n     #ifdef NCR n         initialize(); n     #endif n     int TESTS=1; n     cin>>TESTS; n     while(TESTS--) n         _runtimeTerror_(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<vector<int> > A(n, vector<int>(n)); n   int counter = 0; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < k - 1; j++) { n       A[i][j] = ++counter; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = k - 1; j < n; j++) { n       A[i][j] = ++counter; n     } n   } n   for (int i = 0; i < n; i++) { n     sum += A[i][k - 1]; n   } n   cout << sum << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cout << A[i][j] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long int n #define ff first n #define ss second n #define pb push_back n #define mp make_pair n #define deb(x) cout<< #x <<     << x <<   n ; n #define MAX 9223372036854775807 n #define MIN -9223372036854775807 n #define setbits(n) __builtin_popcountll(n) n #define mkunique(a) sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end())-a.begin()); n #define print(s) for(auto u: s) cout<<u<<   ; cout<<  n ; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const ll mod=998244353; n   n   n const int N=501; n ll dp[N][N][2], n; n vector<ll> a; n ll inc, id; n   n ll go(ll pos, ll diff, ll f){ n     if(pos==n){ n         if(f) return 1; n         return 0; n     } n     if(dp[pos][diff][f]!=-1) n         return dp[pos][diff][f]; n     ll ans = go(pos+1,diff,f); n     if(a[pos]>0){ n         ll val = a[pos]; n         if(pos == id) n             ans = (ans + go(pos+1,diff+1,1))%mod; n         else{ n             if(val<inc || (val==inc && pos<id)) n                 ans = (ans + go(pos+1,diff+1,f))%mod; n             else n                 ans = (ans + go(pos+1,diff,f))%mod; n         } n     } n     else{ n         if(diff-1 < 1) n             ans = (ans + go(pos+1,0,0))%mod; n         else n             ans = (ans + go(pos+1,diff-1,f))%mod; n     } n     return dp[pos][diff][f] = ans; n } n   n int main(){ n   n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n     ll TT=clock(); n   n     cin>>n; n     for(ll i=0;i<n;i++){ n         char c; n         cin>>c; n         if(c== + ){ n             ll num; n             cin>>num; n             a.pb(num); n         } n         else{ n             a.pb(-1); n         } n     } n     ll ans=0; n     for(ll i=0;i<n;i++){ n         if(a[i]>0){ n             inc = a[i]; id = i; n             memset(dp,-1,sizeof(dp)); n             ll mul = go(0,0,0); n             ans = (ans + inc*mul%mod)%mod; n         } n     } n     cout<<ans; n   n   n     cerr<<  n nTIME:  <<(long double)(clock()-TT)/CLOCKS_PER_SEC<<  sec n ; n     TT = clock(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<string, string> mp; n   set<string> s; n   for (int i = 0; i < n; i++) { n     string old, edg; n     cin >> old >> edg; n     mp[old] = edg; n     s.insert(edg); n   } n   map<string, string>::iterator p; n   p = mp.begin(); n   vector<pair<string, string>> a; n   for (p; p != mp.end(); p++) { n     if (s.count(p->first)) continue; n     string cur; n     cur = p->first; n     while (mp.count(cur)) { n       cur = mp[cur]; n     } n     a.push_back(make_pair(p->first, cur)); n   } n   cout << a.size() <<   n ; n   for (int i = 0; i < a.size(); i++) n     cout << a[i].first <<     << a[i].second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long add(long long x, long long y) { n   x += y; n   while (x >= 998244353) x -= 998244353; n   while (x < 0) x += 998244353; n   return x; n } n long long mul(long long x, long long y) { return (x * 1ll * y) % 998244353; } n long long binpow(long long x, long long y) { n   long long z = 1; n   while (y > 0) { n     if (y % 2 == 1) z = mul(z, x); n     x = mul(x, x); n     y /= 2; n   } n   return z; n } n long long inv(long long x) { return binpow(x, 998244353 - 2); } n long long divide(long long x, long long y) { return mul(x, inv(y)); } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   vector<long long> a(k); n   for (long long i = 0; i < (long long)k; ++i) cin >> a[i]; n   vector<long long> last(n + 1, -1), fst(n + 1, k); n   for (long long i = 1; i <= k; i++) { n     last[a[i - 1]] = i; n   } n   for (long long i = k; i >= 1; i--) { n     fst[a[i - 1]] = i; n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     for (long long j = max(1ll, i - 1); j <= min(i + 1, n); j++) { n       if (j == i) { n         if (last[i] == -1) ans += 1; n       } else n         ans += (last[i] <= fst[j]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n long long T[N]; n long long sum[5]; n multiset<long long, greater<long long>> ms[5]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k, b, c; n   cin >> n >> k >> b >> c; n   for (int i = 1; i <= n; i++) { n     cin >> T[i]; n   } n   sort(T + 1, T + n + 1); n   if (T[1] < 0) { n     long long val = -T[1]; n     for (int i = 1; i <= n; i++) { n       T[i] += val; n     } n   } n   if (5 * c <= b) { n     long long ans = 9e18; n     long long sum = 0; n     for (int i = 1; i <= n; i++) { n       sum += T[i]; n       if (i >= k) { n         ans = min(ans, T[i] * k - sum); n         sum -= T[i - k + 1]; n       } n     } n     cout << ans <<   n ; n     exit(0); n   } n   long long ans = 9e18; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 5; j++) { n       long long cnt = -((T[i] + j) / 5) * b + j * c; n       int mm = (T[i] + j) % 5; n       sum[mm] += cnt; n       ms[mm].insert(cnt); n     } n     if (i > k) { n       for (int j = 0; j < 5; j++) { n         sum[j] -= *ms[j].begin(); n         ms[j].erase(ms[j].begin()); n       } n     } n     if (i >= k) { n       for (int j = 0; j < 5; j++) { n         int mm = (T[i] + j) % 5; n         ans = min(ans, ((T[i] + j) / 5) * b * k + sum[mm]); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n long long int mod = 1000000007; n using namespace std; n void solve() { n   long long int n, k; n   cin >> n >> k; n   char ans[4][n]; n   int nval = n - 2; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       ans[i][j] =  . ; n     } n   } n   int mid = nval / 2; n   mid++; n   if (k % 2 == 1) { n     for (int i = 1; i < 3; i++) { n       if (k <= 0) break; n       for (int j = mid; j >= 1; j--) { n         if (i > 1 && k % 2 == 0 && k < nval) { n           ans[i][1] =  # ; n           ans[i][n - 2] =  # ; n           k--; n           k--; n         } n         if (k > 0) { n           ans[i][j] =  # ; n           k--; n           if (k <= 0) break; n           ans[i][mid + (mid - j)] =  # ; n           if (j != mid) k--; n         } else n           break; n       } n     } n   } else { n     for (int i = 1; i < n - 1; i++) { n       for (int j = 1; j < 3; j++) { n         if (k) { n           ans[j][i] =  # ; n           k--; n         } n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       cout << ans[i][j]; n     } n     cout << endl; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n const long long mod = 998244353; n long long fac[maxn], invfac[maxn]; n int a[maxn]; n long long qp(long long x, long long p, long long MOD) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * x % MOD; n     x = x * x % MOD; n     p >>= 1; n   } n   return res; n } n void init() { n   fac[0] = 1; n   invfac[0] = 1; n   for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * 1ll * i % mod; n   invfac[maxn - 1] = qp(fac[maxn - 1], mod - 2, mod); n   for (int i = maxn - 2; i >= 1; i--) n     invfac[i] = invfac[i + 1] * 1ll * (i + 1) % mod; n } n long long C(int n, int k) { n   return fac[n] * invfac[n - k] % mod * invfac[k] % mod; n } n int main() { n   init(); n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n + n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n * 2 + 1); n   long long ans = 0; n   for (int i = 1; i <= n * 2; i++) { n     if (i <= n) { n       ans = (ans - C(n * 2, n) * a[i] % mod) % mod; n     } else { n       ans = (ans + C(n * 2, n) * a[i] % mod + mod) % mod; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f3(int n) { return n * (n - 1) * (n - 2) / 6; } n int main() { n   int K; n   cin >> K; n   int N = 3; n   while (f3(N + 1) <= K) N++; n   int MN = N * (N - 1) / 2; n   int TN = 100; n   vector<string> E(TN, string(TN,  0 )); n   for (int i = 0; i < N; i++) { n     for (int j = i + 1; j < N; j++) { n       E[i][j] = E[j][i] =  1 ; n     } n   } n   int GK = f3(N); n   for (int i = N; i < TN; i++) { n     for (int j = 0; j < N && GK + j <= K; j++) { n       E[i][j] = E[j][i] =  1 ; n       GK += j; n     } n   } n   cout << TN << endl; n   for (int i = 0; i < E.size(); i++) cout << E[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int up = 1e6 + 6; n int n, m, k, pos; n int cost[up], p[up], last[up]; n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   scanf( %d , &k); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &pos); n     p[pos] = 1; n   } n   if (p[0]) { n     printf( %d n , -1); n     return 0; n   } n   for (int i = 0; i <= n; i++) { n     if (!p[i]) n       last[i] = i; n     else n       last[i] = last[i - 1]; n   } n   for (int i = 1; i <= k; i++) { n     scanf( %d , &cost[i]); n   } n   long long res = 1ll * up * up; n   for (int i = 1; i <= k; i++) { n     int cnt = 1; n     for (int j = i; j < n; j += i) { n       if (j - last[j] >= i) { n         cnt = n + 10; n         break; n       } else { n         j = last[j]; n         cnt++; n       } n     } n     if (cnt <= n) { n       res = min(res, 1ll * cnt * cost[i]); n     } n   } n   if (res == 1ll * up * up) { n     res = -1ll; n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rang( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n int rng(int lim) { n   uniform_int_distribution<int> uid(0, lim - 1); n   return uid(rang); n } n void solution() { n   int y; n   cin >> y; n   while (true) { n     y = y + 1; n     int a = y / 1000; n     int b = y / 100 % 10; n     int c = y / 10 % 10; n     int d = y % 10; n     if (a != b && a != c && a != d && b != c && b != d && c != d) { n       break; n     } n   } n   cout << y << endl; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n   int test = 1; n   while (test--) { n     solution(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, char> q; n int main() { n   char a[30], b[30]; n   scanf( %s , a); n   scanf( %s , b); n   q.clear(); n   for (int i = 0; i < 26; i++) { n     q[a[i]] = b[i]; n     q[a[i] - 32] = b[i] - 32; n   } n   char aim[2000]; n   scanf( %s , aim); n   for (int i = 0; aim[i]; i++) { n     if (q[aim[i]]) n       printf( %c , q[aim[i]]); n     else n       printf( %c , aim[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n const double EPS = 1e-7; n const double PI = 3.1415926535897932384626; n const int MAXN = 100001; n int n, a[MAXN]; n int main() { n   std::cin >> n; n   for (int i = 1; i <= n; i++) { n     std::cin >> a[i]; n   } n   long long answer = 0; n   for (int i = 1; i <= n; i++) { n     answer += a[i]; n   } n   std::sort(a + 1, a + n + 1); n   if (answer & 1) { n     for (int i = 1; i <= n; i++) n       if (a[i] & 1) { n         answer -= a[i]; n         break; n       } n   } n   std::cout << answer << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n int n; n int degree[100010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n - 1; i++) { n     int r1, r2; n     scanf( %d%d , &r1, &r2); n     degree[r1]++; n     degree[r2]++; n   } n   for (int i = 1; i <= n; i++) { n     if (degree[i] == 2) { n       printf( NO ); n       return 0; n     } n   } n   printf( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long int cost = 0; n vector<int> a, b, c; n vector<vector<int>> g; n pair<int, int> dfs(int cur, int par, int mn_before) { n   pair<int, int> ap = {0, 0}; n   int mn_cur = min(a[cur], mn_before); n   if (b[cur] != c[cur]) { n     if (b[cur] == 1) { n       ap.first = 1; n       ap.second = 0; n     } else { n       ap.first = 0; n       ap.second = 1; n     } n   } n   for (auto it : g[cur]) { n     if (it != par) { n       pair<int, int> pa = dfs(it, cur, mn_cur); n       ap.first += pa.first; n       ap.second += pa.second; n     } n   } n   if (a[cur] < mn_before) { n     int cnt = min(ap.first, ap.second); n     cost += 2 * 1ll * cnt * 1ll * a[cur]; n     ap.first -= cnt; n     ap.second -= cnt; n   } n   return ap; n } n long long int solve() { n   cin >> n; n   a.resize(n + 1); n   b.resize(n + 1); n   c.resize(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i] >> c[i]; n   } n   g.resize(n + 1); n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   pair<int, int> a = dfs(1, -1, 1000000000); n   if (a.first != a.second) { n     return -1; n   } n   return cost; n } n int main() { n   int t = 1; n   while (t--) { n     cout << solve() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int num, rt[200010], n; n struct data { n   int l, r, v; n } tr[200010 * 20]; n void insert(int p, int l, int r, int n, int &x) { n   x = ++num; n   tr[x] = tr[n]; n   if (l == r) { n     tr[x].v++; n     return; n   } n   int mid = l + r >> 1; n   if (p <= mid) n     insert(p, l, mid, tr[n].l, tr[x].l); n   else n     insert(p, mid + 1, r, tr[n].r, tr[x].r); n   tr[x].v = tr[tr[x].l].v + tr[tr[x].r].v; n } n int ask(int al, int ar, int l, int r, int lx, int x) { n   if (x == 0) return 0; n   if (al <= l && r <= ar) return tr[x].v - tr[lx].v; n   int mid = l + r >> 1, ans = 0; n   if (al <= mid) ans = ask(al, ar, l, mid, tr[lx].l, tr[x].l); n   if (ar > mid) ans += ask(al, ar, mid + 1, r, tr[lx].r, tr[x].r); n   return ans; n } n int ask(int r1, int r2, int l1, int l2) { n   if (r1 > r2 || l1 > l2) return 0; n   return ask(r1, r2, 1, n, rt[l1 - 1], rt[l2]); n } n int main() { n   int m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     insert(x, 1, n, rt[i - 1], rt[i]); n   } n   for (int i = 1; i <= m; i++) { n     int l1, l2, r1, r2; n     scanf( %d%d%d%d , &l1, &r1, &l2, &r2); n     long long ans = 0; n     ans += 1ll * ask(1, r1 - 1, 1, l1 - 1) * ask(r1, n, l1, n); n     ans += 1ll * ask(1, r1 - 1, l1, l2) * ask(r1, n, 1, n); n     ans += 1ll * ask(1, r1 - 1, l2 + 1, n) * ask(r1, n, 1, l2); n     ans += 1ll * ask(r1, r2, 1, l1 - 1) * ask(r1, n, l1, n); n     ans += 1ll * ask(r1, r2, l1, l2) * n            (ask(r1, n, 1, n) - ask(r1, r2, 1, l1 - 1) - ask(r1, r2, l1, l2)); n     ans += 1ll * ask(r1, r2, l2 + 1, n) * ask(r2 + 1, n, 1, l2); n     ans += 1ll * ask(r1, r2, l1, l2) * (ask(r1, r2, l1, l2) - 1) / 2; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 10000; n const long long MAXN = 1000; n int n, a[MAXN], sum, cnt, ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   sort(a, a + n + 1); n   for (int i = n; i >= 1; i--) { n     cnt += a[i]; n     ans++; n     if (cnt > sum - cnt) { n       cout << ans; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int l = 0; l < t; l++) { n     long long n, s, k; n     cin >> n >> s >> k; n     unordered_set<long long> tilos; n     for (int i = 0; i < k; ++i) { n       long long x; n       cin >> x; n       tilos.insert(x); n     } n     for (long long i = 0; i <= k; i++) { n       if ((s + i <= n) && (tilos.find(s + i) == tilos.end())) { n         cout << i << endl; n         break; n       } n       if ((s - i >= 1) && (tilos.find(s - i) == tilos.end())) { n         cout << i << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[200100], a[200100]; n long long ans; n int main() { n   int n, m, i, tmp, s, x; n   bool f = false, flag = true; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %d , d + i); n   scanf( %d , &m); n   if (m % ((n - 1) << 1) == 0) f = true; n   s = (m / ((n - 1) << 1)) << 1; n   ans = (long long)s * (d[n] - d[1]); n   while (m--) { n     scanf( %d , &x); n     ++a[x]; n   } n   a[1] <<= 1; n   a[n] <<= 1; n   tmp = d[2] - d[1]; n   for (i = 1; i < n; ++i) { n     if (f && d[i + 1] - d[i] != tmp) flag = false; n     ans += min(a[i] - s, a[i + 1] - s) * (d[i + 1] - d[i]); n   } n   if (flag) n     printf( %I64d n , ans - f * tmp); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long abs2(long long a) { return a >= 0 ? a : -a; } n long long calc(long long k, long long a) { n   if (k >= 0) return k / a; n   return (k - (a - 1)) / a; n } n long long calc2(long long a1, long long b1, long long a2, long long b2) { n   long long x = abs2(a1 - a2) + abs2(b2 - (a2 + (b1 - a1))); n   long long y = abs2(a1 - a2) + abs2(b2 - (-a2 + a1 + b1)); n   return min(x, y); n } n int main() { n   long long a, b, x1, x2, y1, y2; n   while (cin >> a >> b >> x1 >> y1 >> x2 >> y2) { n     long long a1, b1; n     long long a2, b2; n     a1 = calc(x1 + y1, 2 * a); n     b1 = calc(x1 - y1, 2 * b); n     a2 = calc(x2 + y2, 2 * a); n     b2 = calc(x2 - y2, 2 * b); n     long long x = calc2(a1, b1, a2, b2); n     long long y = calc2(b1, a1, b2, a2); n     cout << (int)min(x, y) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lld = signed long long int; n lld solve() { n   lld n, m, k; n   cin >> n >> m >> k; n   lld x = n / k; n   if (m <= x) return m; n   if (m <= x + (k - 1)) return x - 1; n   return x - ceil((m - x) / (k - 1.0)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cout << solve() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<  :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<  :   << arg1 <<   | ; n   __f(comma + 1, args...); n } n const int INF = 1 << 29; n const int MOD = 1e9 + 7; n struct Node { n   Node *pre, *nxt; n   int val, pos, len; n   bool operator<(const Node& rhs) const { n     if (len != rhs.len) return len > rhs.len; n     return pos < rhs.pos; n   } n }; n const int N = 2e5 + 10; n Node pool[N << 1], *last; n struct cmp { n   bool operator()(int u, int v) const { return pool[u] < pool[v]; } n }; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   set<int, cmp> A; n   last = pool; n   for (int i = 0, j; i < n; i = j) { n     for (j = i + 1; j < n && a[j] == a[i]; ++j) n       ; n     Node* cur = last++; n     cur->val = a[i]; n     cur->pos = i; n     cur->len = j - i; n     cur->pre = cur->nxt = nullptr; n     if (i > 0) { n       auto pre = last - 2; n       pre->nxt = cur; n       cur->pre = pre; n     } n   } n   for (auto it = pool; it != last; ++it) { n     A.insert(it - pool); n   } n   int ret = 0; n   while (A.size()) { n     auto it = A.begin(); n     auto cur = &pool[*it]; n     auto pre = cur->pre; n     auto nxt = cur->nxt; n     if (pre && nxt && pre->val == nxt->val) { n       cur = last++; n       cur->val = pre->val; n       cur->pos = pre->pos; n       cur->len = pre->len + nxt->len; n       cur->pre = pre->pre; n       cur->nxt = nxt->nxt; n       if (cur->pre) cur->pre->nxt = cur; n       if (cur->nxt) cur->nxt->pre = cur; n       A.erase(pre - pool); n       A.erase(nxt - pool); n       A.insert(cur - pool); n     } else { n       if (pre) pre->nxt = nxt; n       if (nxt) nxt->pre = pre; n     } n     A.erase(it); n     ++ret; n   } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n int a[maxn], b[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   long long ans = 0; n   int cnt1 = 0, cnt2 = 0, cnt0 = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     if (a[i] <= -1) { n       ans += -1 - a[i]; n       cnt1++; n     } else if (a[i] >= 1) { n       ans += a[i] - 1; n       cnt2++; n     } else { n       cnt0++; n     } n   } n   if (cnt1 % 2) { n     if (cnt0) { n       ans += cnt0; n     } else { n       ans += 2; n     } n   } else { n     ans += cnt0; n   } n   printf( %lld n , ans); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n int T, n, m, las = 1, ndnum = 1, ans; n char s[maxn]; n struct node { n   int ch[26], len, fa; n } d[maxn << 1]; n int siz[maxn << 1], vis[maxn << 1], timer; n int head[maxn << 1], tot; n struct edge { n   int nxt, to; n } e[maxn << 1]; n void Addedge(int x, int y) { n   e[++tot] = (edge){head[x], y}; n   head[x] = tot; n } n void Insert(int x) { n   int p = las, np = las = ++ndnum; n   siz[np] = 1; n   d[np].len = d[p].len + 1; n   while (p && !d[p].ch[x]) { n     d[p].ch[x] = np; n     p = d[p].fa; n   } n   if (!p) { n     d[np].fa = 1; n   } else { n     int q = d[p].ch[x]; n     if (d[q].len == d[p].len + 1) { n       d[np].fa = q; n     } else { n       int nq = ++ndnum; n       d[nq] = d[q]; n       d[nq].len = d[p].len + 1; n       d[q].fa = d[np].fa = nq; n       while (p && d[p].ch[x] == q) { n         d[p].ch[x] = nq; n         p = d[p].fa; n       } n     } n   } n } n void dfs(int x) { n   int id; n   for (int i = head[x]; i; i = e[i].nxt) { n     id = e[i].to; n     dfs(id); n     siz[x] += siz[id]; n   } n } n void go(int &x, int y, int &len) { n   if (d[x].ch[y]) { n     ++len; n     x = d[x].ch[y]; n   } else { n     while (x && !d[x].ch[y]) x = d[x].fa; n     if (!x) { n       x = 1; n       len = 0; n     } else { n       len = d[x].len + 1; n       x = d[x].ch[y]; n     } n   } n } n void del(int &x, int &len) { n   if (len != m) return; n   --len; n   if (len == d[d[x].fa].len) x = d[x].fa; n } n void work(int x, int y) { n   if (y < m || vis[x] == timer) return; n   ans += siz[x]; n   vis[x] = timer; n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; ++i) Insert(s[i] -  a ); n   for (int i = 2; i <= ndnum; ++i) Addedge(d[i].fa, i); n   dfs(1); n   scanf( %d , &T); n   int now, len; n   while (T--) { n     scanf( %s , s + 1); n     m = strlen(s + 1); n     now = 1; n     len = 0; n     for (int i = 1; i <= m; ++i) go(now, s[i] -  a , len); n     ++timer; n     ans = 0; n     work(now, len); n     for (int i = 1; i < m; ++i) { n       del(now, len); n       go(now, s[i] -  a , len); n       work(now, len); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = (int)2e9; n const double PI = 2 * acos(0.0); n const double EPS = 1e-9; n int dcmp(double a, double b) { return fabs(a - b) <= EPS ? 0 : a > b ? 1 : 2; } n int DI[] = {-1, 0, 1, 0, 1, -1, -1, 1}; n int DJ[] = {0, 1, 0, -1, 1, -1, 1, -1}; n void clc1(string x) { n   int idx = x.find( C ); n   string r = x.substr(1, idx - 1); n   string c = x.substr(idx + 1); n   int cc = atoi(c.c_str()); n   string ans =   ; n   while (cc) { n     char d = cc % 26 +  A  - 1; n     cc /= 26; n     if (d ==  @ ) { n       d =  Z ; n       cc--; n     } n     ans += d; n   } n   reverse(ans.begin(), ans.end()); n   cout << ans << r << endl; n } n void clc2(string x) { n   string c =   ; n   cout <<  R ; n   for (int i = 0; i < (int)(int)x.size(); i++) { n     if (isdigit(x[i])) n       cout << x[i]; n     else n       c += x[i]; n   } n   cout <<  C ; n   int ans = 0; n   int p = 1; n   for (int i = (int)c.size() - 1; i >= 0; i--) { n     ans += (((c[i] -  A ) + 1) * p); n     p *= 26; n   } n   cout << ans << endl; n } n bool f(string x) { n   if (x[0] !=  R ) return 0; n   int r = x.find( C ); n   if (r == -1) return 0; n   for (int i = 1; i < r; i++) n     if (isdigit(x[i])) return 1; n   return 0; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < (int)n; i++) { n     string x; n     cin >> x; n     if (f(x)) n       clc1(x); n     else n       clc2(x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 300; n const int mod = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const double eps = 1e-6; n string s; n int gx[] = {0, 0, 1, -1}; n int gy[] = {1, -1, 0, 0}; n bool used[MAX_N][MAX_N]; n bool check(int x, int y) { n   int cnt = 0; n   for (int i = 0; i < 4; ++i) { n     int nx = x + gx[i]; n     int ny = y + gy[i]; n     if (used[nx][ny]) ++cnt; n   } n   return cnt == 1; n } n bool solve() { n   cin >> s; n   int x = 100, y = 100; n   used[x][y] = true; n   for (auto &ch : s) { n     if (ch ==  U ) n       ch = ch -  U  +  3 ; n     else if (ch ==  D ) n       ch = ch -  D  +  2 ; n     else if (ch ==  L ) n       ch = ch -  L  +  1 ; n     else n       ch = ch -  R  +  0 ; n   } n   for (auto ch : s) { n     x += gx[ch -  0 ]; n     y += gy[ch -  0 ]; n     if (used[x][y]) return false; n     used[x][y] = true; n     if (!check(x, y)) return false; n   } n   return true; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t = 1; n   while (t--) { n     if (solve()) n       cout <<  OK  <<   n ; n     else n       cout <<  BUG  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace kotespace { n template <class T> n class duplet { n  private: n  public: n   T x, y; n   duplet(){}; n   duplet(T a, T b) : x(a), y(b){}; n   bool operator<(const duplet P) const { n     return (x < P.x || (x == P.x && y < P.y)); n   } n   bool operator>(const duplet P) const { n     return (x > P.x || (x == P.x && y > P.y)); n   } n   bool operator==(const duplet P) const { return (x == P.x && y == P.y); } n   bool operator!=(const duplet P) const { return (x != P.x || y != P.y); } n   void reverse() { std::swap(x, y); } n }; n template <class P> n istream &operator>>(istream &in, duplet<P> &T) { n   return (in >> T.x >> T.y); n } n template <class P> n ostream &operator<<(ostream &out, duplet<P> T) { n   return (out << T.x <<     << T.y); n } n template <class T> n class triplet { n  private: n  public: n   T x, y, z; n   triplet(){}; n   triplet(T a, T b, T c) : x(a), y(b), z(c){}; n   bool operator<(const triplet P) const { n     return (x < P.x || (x == P.x && y < P.y) || n             (x == P.x && y == P.y && z < P.z)); n   } n   bool operator>(const triplet P) const { n     return (x > P.x || (x == P.x && y > P.y) || n             (x == P.x && y == P.y && z > P.z)); n   } n   bool operator==(const triplet P) const { n     return (x == P.x && y == P.y && z == P.z); n   } n   bool operator!=(const triplet P) const { n     return (x != P.x || y != P.y || z != P.z); n   } n   void reverse() { std::swap(x, z); } n   void cycle_right(int a) { n     if (a == 1) { n       std::swap(x, y); n       std::swap(x, z); n     } n     if (a == 2) { n       std::swap(x, z); n       std::swap(y, x); n     } n   } n   void cycle_left(int a) { n     if (a == 1) { n       std::swap(x, z); n       std::swap(y, x); n     } n     if (a == 2) { n       std::swap(x, y); n       std::swap(x, z); n     } n   } n }; n template <class P> n istream &operator>>(istream &in, triplet<P> &T) { n   return (in >> T.x >> T.y >> T.z); n } n template <class P> n ostream &operator<<(ostream &out, triplet<P> &T) { n   return (out << T.x <<     << T.y <<     << T.z); n } n }  // namespace kotespace n using namespace kotespace; n long long inf = 1000 * 1000 * 1000 + 7; n long long inf64 = inf * inf; n long long mod = 228228227; n float START_TIME; n namespace std { n template <> n struct hash<duplet<int>> { n   size_t operator()(const duplet<int> &k) const { n     return (size_t)(k.x) * 102931204 + (size_t)(k.y) * 434823123; n   } n }; n }  // namespace std n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cerr << fixed << setprecision(8); n   cout << fixed << setprecision(8); n   srand(time(0)); n   START_TIME = clock(); n   int t; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     int balx = 0, baly = 0; n     map<pair<int, int>, vector<int>> q; n     long long l = -inf, r = inf; n     q[make_pair(0, 0)].push_back(0); n     for (int i = 0; i < n; ++i) { n       if (s[i] ==  L ) ++balx; n       if (s[i] ==  R ) --balx; n       if (s[i] ==  U ) ++baly; n       if (s[i] ==  D ) --baly; n       q[make_pair(balx, baly)].push_back(i + 1); n     } n     for (auto &c : q) { n       for (int i = 0; i + 1 < c.second.size(); ++i) { n         if (c.second[i + 1] - c.second[i] < r - l) { n           r = c.second[i + 1]; n           l = c.second[i]; n         } n       } n     } n     if (r == inf) n       cout << -1 << endl; n     else n       cout << l + 1 <<     << r << endl; n   } n   cerr << endl << (clock() - START_TIME) / CLOCKS_PER_SEC <<   sec.  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n template <class T> n void chkmax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n void chkmin(T &a, T b) { n   if (a > b) a = b; n } n const int inf = 0x3f3f3f3f; n const long long linf = 2e18 + 100; n const double dinf = 2e18 + 100; n const double eps = 1e-7; n const double pi = 3.14159265358979323846264338327950288L; n bool scanld(double &first) { n   double xx; n   int f = scanf( %lf , &xx); n   first = xx; n   return f > 0; n } n void printld(const double &first) { n   printf( %.10lf , fabs(first) < eps ? 0 : (double)first); n } n inline char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n template <class T> n void read(T &first) { n   char c = nc(), b = 1; n   for (; !((c >=  0  && c <=  9 ) || c == EOF); c = nc()) n     if (c ==  - ) b = -b; n   for (first = 0; c >=  0  && c <=  9 ; first = first * 10 + c -  0 , c = nc()) n     ; n   first *= b; n } n inline void read(char *s) { n   char c = nc(); n   int len = 0; n   for (; c ==     || c ==   t  || c ==   n ; c = nc()) n     ; n   for (; !(c ==     || c ==   t  || c ==   n  || c == EOF); n        s[len++] = c, c = nc()) n     ; n   s[len] = 0; n } n int n, P[100005], len; n vector<int> v[100005]; n int ds[100005]; n void dfs(int k, int pa) { n   P[k] = pa; n   for (auto first : v[k]) { n     if (first == pa) continue; n     dfs(first, k); n   } n   return; n } n int mx[100005][3], id[100005][3]; n void dfs_2(int k, int pa) { n   mx[k][0] = 1, mx[k][1] = 0, mx[k][2] = 0; n   id[k][0] = k, id[k][1] = 0, id[k][2] = 0; n   for (auto first : v[k]) { n     if (first == pa) continue; n     dfs_2(first, k); n     int d = mx[first][0] + 1; n     if (d > mx[k][2]) mx[k][2] = d, id[k][2] = first; n     if (mx[k][2] > mx[k][1]) swap(mx[k][1], mx[k][2]), swap(id[k][1], id[k][2]); n     if (mx[k][1] > mx[k][0]) swap(mx[k][1], mx[k][0]), swap(id[k][1], id[k][0]); n   } n   return; n } n bool ok[100005]; n int pmx[100005]; n void dfs_3(int k, int pa) { n   if (!pa) n     pmx[k] = 0; n   else { n     int d = id[pa][0] == k ? mx[pa][1] : mx[pa][0]; n     chkmax(d, pmx[pa]); n     pmx[k] = d + 1; n   } n   if (mx[k][2] > len || mx[k][1] > len && pmx[k] > len) n     ok[k] = 1; n   else n     ok[k] = 0; n   for (auto first : v[k]) { n     if (first == pa) continue; n     dfs_3(first, k); n   } n   return; n } n int G, st[100005], fs[100005]; n int p[100005][17], dp[100005]; n void dfs_4(int k, int pa) { n   dp[k] = dp[pa] + 1; n   p[k][0] = pa; n   st[k] = ++G; n   for (int i = 1; i <= 16; i++) p[k][i] = p[p[k][i - 1]][i - 1]; n   ds[k] = k; n   for (auto first : v[k]) { n     if (first == pa) continue; n     dfs_4(first, k); n     if (dp[ds[first]] > dp[ds[k]]) ds[k] = ds[first]; n   } n   fs[k] = ++G; n   return; n } n int kth_father(int first, int k) { n   for (int i = 16; i >= 0; i--) n     if (k & (1 << i)) first = p[first][i]; n   return first; n } n bool is_anc(int p, int first) { n   return st[p] <= st[first] && fs[first] <= fs[p]; n } n bool chk[100005]; n int head, tail; n int main() { n   int T; n   for (read(T); T--;) { n     read(n); n     read(head), read(tail); n     int first, second; n     for (int i = 1; i < n; i++) { n       read(first), read(second); n       v[first].push_back(second); n       v[second].push_back(first); n     } n     dfs(head, 0); n     len = 0; n     int t = tail; n     while (t != head) { n       len++; n       t = P[t]; n     } n     dfs_2(head, 0); n     dfs_3(head, 0); n     int R = -1; n     for (int i = 1; i <= n; i++) n       if (ok[i]) R = i; n     if (R < 0) n       puts( NO ); n     else { n       dfs_4(R, 0); n       int dph = dp[head], dpt = dp[tail]; n       bool yes = is_anc(head, tail) || is_anc(tail, head); n       if (!yes) { n         int ch = head, ct = tail; n         int lca = R; n         for (int i = 1; i <= n; i++) n           if (is_anc(i, ch) && is_anc(i, ct) && st[i] > st[lca]) lca = i; n         int lh = dp[ch] - dp[lca], lt = dp[ct] - dp[lca]; n         bool fh = 0; n         while (!yes) { n           if (fh && chk[ch]) break; n           if (fh) chk[ch] = 1; n           if (fh) { n             ch = ds[ch]; n             if (dp[ch] - dp[lca] >= len) n               yes = 1; n             else { n               lh = dp[ch] - dp[lca]; n               ct = kth_father(ct, lt - (len - lh)); n               lt = dp[ct] - dp[lca]; n             } n           } else { n             ct = ds[ct]; n             if (dp[ct] - dp[lca] >= len) n               yes = 1; n             else { n               lt = dp[ct] - dp[lca]; n               ch = kth_father(ch, lh - (len - lt)); n               lh = dp[ch] - dp[lca]; n             } n           } n           fh ^= 1; n         } n       } n       puts(yes ?  YES  :  NO ); n     } n     for (int i = 1; i <= n; i++) v[i].clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.); n const int MX = 500500; n int t, n, i, j, z[MX]; n char s[MX]; n vector<complex<double> > a, b; n vector<int> r, ans; n void fft(vector<complex<double> >& a, bool inv) { n   int n = a.size(); n   if (n == 1) return; n   for (int i = 1, rv = 0; i < n; i++) { n     int j = n / 2; n     for (; rv >= j; j /= 2) rv -= j; n     rv += j; n     if (i < rv) swap(a[i], a[rv]); n   } n   for (int l = 2; l <= n; l *= 2) { n     double alpha = 2 * pi / l; n     if (inv) alpha = -alpha; n     for (int i = 0; i < n; i += l) { n       complex<double> w(1), wn(cos(alpha), sin(alpha)); n       for (int j = 0; j < l / 2; j++) { n         complex<double> x = a[i + j]; n         complex<double> y = w * a[i + j + l / 2]; n         a[i + j] = x + y; n         a[i + j + l / 2] = x - y; n         w *= wn; n       } n     } n   } n   if (inv) n     for (int i = 0; i < n; i++) a[i] /= n; n } n void mul(vector<complex<double> >& a, vector<complex<double> >& b) { n   int n; n   for (n = 1; n < a.size() || n < b.size(); n *= 2) n     ; n   n *= 2; n   a.resize(n); n   b.resize(n); n   r.resize(n); n   fft(a, false); n   fft(b, false); n   for (int i = 0; i < n; i++) a[i] *= b[i]; n   fft(a, true); n   for (int i = 0; i < n; i++) r[i] = int(a[i].real() + 0.5); n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     scanf( %s , s); n     a.resize(n); n     b.resize(n); n     for (i = 0; i < n; i++) a[i] = int(s[i] ==  V ); n     for (i = 0; i < n; i++) b[i] = int(s[n - i - 1] ==  K ); n     mul(a, b); n     for (i = 1; i < n; i++) z[i] = r[n - 1 - i] + r[n - 1 + i]; n     ans.clear(); n     for (i = 1; i < n; i++) n       if (z[i] == 0) { n         for (j = i + i; j < n; j += i) n           if (z[j] > 0) break; n         if (j >= n) ans.push_back(i); n       } n     printf( %d n , int(ans.size()) + 1); n     for (i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n     printf( %d n , n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n int a[N], L[N], tot, R[N], Stack[N], n, m; n long long Ans[N]; n struct Ask { n   int l, r, from; n } A[N]; n inline int cmp1(Ask x, Ask y) { return x.l > y.l; } n inline int cmp2(Ask x, Ask y) { return x.r < y.r; } n struct segTree { n   struct Tree { n     int l, r; n     long long num, flag; n   } T[N * 4]; n   inline void build(int x, int l, int r) { n     T[x].l = l; n     T[x].r = r; n     T[x].flag = T[x].num = 0; n     if (l == r) return; n     int mid = (l + r) / 2; n     build(x * 2, l, mid); n     build(x * 2 + 1, mid + 1, r); n   } n   inline void down(int x) { n     if (!T[x].flag) return; n     T[x * 2].num += T[x].flag * (T[x * 2].r - T[x * 2].l + 1); n     T[x * 2 + 1].num += T[x].flag * (T[x * 2 + 1].r - T[x * 2 + 1].l + 1); n     T[x * 2].flag += T[x].flag; n     T[x * 2 + 1].flag += T[x].flag; n     T[x].flag = 0; n   } n   inline void insert(int x, int l, int r, int z) { n     if (T[x].l > r || l > T[x].r) return; n     if (T[x].l >= l && T[x].r <= r) { n       T[x].num += z * (T[x].r - T[x].l + 1); n       T[x].flag += z; n       return; n     } n     down(x); n     insert(x * 2, l, r, z); n     insert(x * 2 + 1, l, r, z); n     T[x].num = T[x * 2].num + T[x * 2 + 1].num; n   } n   inline long long find(int x, int y) { n     if (T[x].l == T[x].r) return T[x].num; n     down(x); n     int mid = (T[x].l + T[x].r) / 2; n     if (y <= mid) return find(x * 2, y); n     return find(x * 2 + 1, y); n   } n } T1, T2; n signed main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) A[i].from = i; n   for (int i = 1; i <= m; i++) scanf( %d , &A[i].l); n   for (int i = 1; i <= m; i++) scanf( %d , &A[i].r); n   for (int i = 1; i <= m; i++) Ans[i] = A[i].r - A[i].l + 1; n   a[0] = a[n + 1] = n + 1; n   Stack[++tot] = 0; n   for (int i = 1; i <= n; i++) { n     while (a[i] > a[Stack[tot]]) tot--; n     L[i] = Stack[tot] + 1; n     Stack[++tot] = i; n   } n   Stack[tot = 1] = n + 1; n   for (int i = n; i; i--) { n     while (a[i] > a[Stack[tot]]) tot--; n     R[i] = Stack[tot] - 1; n     Stack[++tot] = i; n   } n   T1.build(1, 1, n); n   T2.build(1, 1, n); n   sort(A + 1, A + m + 1, cmp2); n   for (int i = 1, j = 1; i <= n; i++) { n     T1.insert(1, 1, L[i] - 1, -L[i]); n     T2.insert(1, L[i], i, -1); n     while (j <= m && A[j].r == i) { n       Ans[A[j].from] += n           T1.find(1, A[j].l) + (long long)T2.find(1, A[j].l) * A[j].l; n       j++; n     } n   } n   T1.build(1, 1, n); n   T2.build(1, 1, n); n   sort(A + 1, A + m + 1, cmp1); n   for (int i = n, j = 1; i; i--) { n     T1.insert(1, R[i] + 1, n, R[i]); n     T2.insert(1, i, R[i], 1); n     while (j <= m && A[j].l == i) { n       Ans[A[j].from] += n           T1.find(1, A[j].r) + (long long)T2.find(1, A[j].r) * A[j].r; n       j++; n     } n   } n   for (int i = 1; i <= m; i++) printf( %lld  , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int maxn = (1 << 18) - 1; n int main() { n   int k; n   scanf( %d , &k); n   int b = maxn, cnt = 0; n   for (int i = 0; i <= 20; ++i) { n     if ((k >> i) & 1) { n       b ^= (1 << i); n     } n   } n   int cc = maxn >> 1; n   printf( %d %d n , 3, 3); n   printf( %d %d %d n , maxn, maxn, b); n   printf( %d %d% d n , maxn, cc, maxn); n   printf( %d %d %d n , b, maxn, cc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, q; n   cin >> n >> q; n   bool lava[2][n]; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < n; j++) lava[i][j] = false; n   } n   set<int> blocked1; n   set<int> blocked2; n   for (int i = 0; i < q; i++) { n     int r, c; n     cin >> r >> c; n     r--; n     c--; n     if (lava[r][c]) { n       blocked2.erase(c); n       if (c < n - 1 && !lava[r][c + 1]) { n         blocked1.erase(c); n       } n       if (c > 0 && !lava[r][c - 1]) { n         blocked1.erase(c - 1); n       } n     } else { n       if (lava[(r + 1) % 2][c]) { n         blocked2.insert(c); n       } n       if (c < n - 1 && lava[(r + 1) % 2][c + 1]) { n         blocked1.insert(c); n       } n       if (c > 0 && lava[(r + 1) % 2][c - 1]) { n         blocked1.insert(c - 1); n       } n     } n     lava[r][c] = !lava[r][c]; n     if (blocked1.empty() && blocked2.empty() && !lava[0][0] && n         !lava[1][n - 1]) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n   } n } n


#include <bits/stdc++.h> n const long long maxn = 8e9 + 5; n long long v[10000000]; n long long k[10000000]; n long long n, sum; n int judge(long long m) { n   long long i, sum1 = sum; n   for (i = 0; i < n; i++) { n     if (m * v[i] > k[i]) { n       if (k[i] + sum1 < m * v[i]) n         break; n       else if (k[i] + sum1 >= m * v[i]) n         sum1 = sum1 + k[i] - m * v[i]; n     } n   } n   if (i < n) n     return 0; n   else n     return 1; n } n int main() { n   long long l, r, mid; n   scanf( %I64d%I64d , &n, &sum); n   for (long long i = 0; i < n; i++) { n     scanf( %I64d , &v[i]); n   } n   for (long long i = 0; i < n; i++) { n     scanf( %I64d , &k[i]); n   } n   l = 0; n   r = maxn; n   while (l + 1 < r) { n     mid = l + (r - l) / 2; n     if (judge(mid)) { n       l = mid; n     } else n       r = mid; n   } n   if (judge(l + 1)) n     printf( %I64d , l + 1); n   else n     printf( %I64d , l); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> g(n + 1); n   for (int i = 0; i < n; i++) { n     int u; n     cin >> u; n     g[i + 1] = u; n   } n   for (int i = 1; i <= n; i++) { n     if (g[g[g[i]]] == i) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n const int maxn = 100010; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   double l, v1, v2; n   cin >> n >> l >> v1 >> v2 >> k; n   int t = (n + k - 1) / k; n   if (t == 1) { n     cout.precision(17); n     cout << fixed << l / v2 <<   n ; n   } else { n     double y = l * (1 + v2 / v1) / (2.0 * t - 1 + v2 / v1); n     double x = l - y; n     double a = y / v2 + x / v1; n     cout.precision(17); n     cout << fixed << a <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD(string s, long long c) { n   long long ans = 0, p = 1; n   for (int i = (((int)s.size()) - 1); i >= (0); i--) { n     ans = (ans + (s[i] -  0 ) * p) % c; n     p *= 10; n     p %= c; n   } n   return ans; n } n long long mod(long long a, long long b, long long c) { n   long long ans = 1; n   while (b) { n     if (b % 2) ans = ans * a % c; n     a = a * a % c; n     b /= 2; n   } n   return ans; n } n long long solve(long long a, string b, long long c) { n   long long ans = 1; n   for (int i = (((int)b.size()) - 1); i >= (0); i--) { n     ans = ans * mod(a, (b[i] -  0 ), c) % c; n     a = mod(a, 10, c); n   } n   return ans; n } n string dec(string s) { n   for (int i = (((int)s.size()) - 1); i >= (0); i--) { n     if (s[i] !=  0 ) { n       s[i]--; n       break; n     } else { n       s[i] =  9 ; n     } n   } n   return s; n } n int main() { n   string bb, n; n   long long c; n   cin >> bb >> n >> c; n   long long b = MOD(bb, c); n   long long ans = (b - 1 + c) * solve(b, dec(n), c) % c; n   if (ans == 0) ans = c; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double INF = 1e18; n const int MAX_N = 100100; n const int IT = 100; n const double PI = acos(-1.0); n int x[MAX_N]; n int y[MAX_N]; n long long dist(int a, int b) { n   long long dx = x[a] - x[b]; n   long long dy = y[a] - y[b]; n   return dx * dx + dy * dy; n } n double dist1(double xx, double yy) { n   xx -= x[0]; n   yy -= y[0]; n   return xx * xx + yy * yy; n } n double fun(int a, int b) { n   double l = 0.0, r = 1.0; n   for (int i = 0; i < IT; i++) { n     double ml = l + (r - l) / 3.0; n     double mr = r - (r - l) / 3.0; n     double x1 = x[a] * ml + (1.0 - ml) * x[b]; n     double y1 = y[a] * ml + (1.0 - ml) * y[b]; n     double x2 = x[a] * mr + (1.0 - mr) * x[b]; n     double y2 = y[a] * mr + (1.0 - mr) * y[b]; n     if (dist1(x1, y1) > dist1(x2, y2)) { n       l = ml; n     } else { n       r = mr; n     } n   } n   double xx = x[a] * l + (1.0 - l) * x[b]; n   double yy = y[a] * l + (1.0 - l) * y[b]; n   return dist1(xx, yy); n } n void solve() { n   int n; n   scanf( %d%d%d , &n, &x[0], &y[0]); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &y[i]); n   } n   double mn = INF; n   double mx = 0; n   for (int i = 1; i <= n; i++) { n     int j = (i == n ? 1 : i + 1); n     mx = max(mx, (double)dist(0, i)); n     mx = max(mx, (double)dist(0, i)); n     mn = min(mn, fun(i, j)); n   } n   double answer = PI * (mx - mn); n   printf( %.18lf n , answer); n } n int main() { n   int cases; n   cases = 1; n   for (int i = 0; i < cases; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a.begin(), a.end()); n   for (auto i : a) { n     if (i != a[0]) { n       printf( %d , i); n       return 0; n     } n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[20]; n long long b[20][10][10]; n long long c[20][10][10]; n long long dp(long long n, int p) { n   long long n0 = n; n   long long ans = 0; n   while (n >= 10) { n     long long m = n; n     long long e = 0; n     while ((m + 10) % (a[e] * 10) < 10) e++; n     if (e == 1 || a[e] >= n) { n       long long m = n; n       int k = 0; n       while (m != 0) { n         if (m % 10 >= k) k = m % 10; n         m = m / 10; n       } n       ans += 1; n       if (p < k) n         n -= k; n       else n         n -= p; n       continue; n     } n     int i = 0; n     int k = 0; n     for (i; m != 0; i++) { n       if (i >= e && m % 10 >= k) k = m % 10; n       m = m / 10; n     } n     if (k < p) k = p; n     if (b[e][(n + 10) % a[e]][k] == -1) { n       dp(n % a[e], k); n     } n     ans += b[e][(n + 10) % a[e]][k]; n     n = n / a[e] * a[e] + c[e][(n + 10) % a[e]][k]; n   } n   long long e = 0; n   while ((n0 + 10) % (a[e] * 10) < 10) e++; n   if (a[e] > n0) { n     b[e][(n0 + 10) % a[e]][p] = ans; n     c[e][(n0 + 10) % a[e]][p] = n; n   } n   return ans; n } n int main() { n   a[0] = 1; n   for (int i = 1; i <= 18; i++) a[i] = a[i - 1] * 10; n   for (int i = 0; i < 20; i++) n     for (int j = 0; j < 10; j++) n       for (int k = 0; k < 10; k++) { n         b[i][j][k] = -1; n         c[i][j][k] = -1; n       } n   long long n; n   cin >> n; n   if (n == 0) { n     cout << 0 << endl; n     return 0; n   } n   cout << dp(n, 0) + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int dp[maxn], ans; n const int inf = 1e9; n int a1, a2, adfds, fd; n vector<int> edge[maxn]; n int n, m, d, num[maxn], fuck; n void dfs1(int u, int fa) { n   int i; n   for (i = 0; i < (int)edge[u].size(); i++) { n     int v = edge[u][i]; n     if (v == fa) continue; n     dfs1(v, u); n     dp[u] = max(dp[u], dp[v] + 1); n   } n   if (num[u]) dp[u] = max(dp[u], 0); n } n void dfs2(int u, int fa, int fv) { n   int flag = 0, i, j, k; n   if (fv + 1 > d) flag = 1; n   int l1 = -inf, id1 = 0, l2 = -inf, id2 = 0; n   for (i = 0; i < (int)edge[u].size(); i++) { n     int v = edge[u][i]; n     if (v == fa) continue; n     if (dp[v] + 1 > d) flag = 1; n     if (dp[v] >= l1) { n       l2 = l1, id2 = id1; n       l1 = dp[v], id1 = v; n     } else if (dp[v] > l2) { n       l2 = dp[v], id2 = v; n     } n   } n   if (!flag) ans++; n   for (i = 0; i < (int)edge[u].size(); i++) { n     int v = edge[u][i]; n     if (v == fa) continue; n     int fvv = fv; n     if (v == id1) n       fvv = max(fvv, l2); n     else n       fvv = max(fvv, l1); n     if (num[u]) fvv = max(fvv, -1); n     dfs2(v, u, fvv + 1); n   } n } n int main() { n   int i, j, k, a, b, c; n   ans = 0; n   scanf( %d%d%d , &n, &m, &d); n   for (i = 0; i <= n; i++) { n     dp[i] = -inf; n     edge[i].clear(); n   } n   for (i = 1; i <= m; i++) { n     scanf( %d , &a); n     num[a] = 1; n   } n   for (i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   dfs1(1, 0); n   dfs2(1, 0, -inf); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const double eps = 1e-9; n int n, t; n char A[N], B[N]; n int main() { n   scanf( %d %d n , &n, &t); n   gets(A); n   gets(B); n   int sameCnt = 0; n   for (int i = 0; i < n; ++i) { n     if (A[i] == B[i]) ++sameCnt; n   } n   int dif = n - sameCnt; n   if (dif / 2 + dif % 2 <= t) { n     int wic = 0; n     int have = 0; n     t -= dif / 2 + dif % 2; n     have = dif % 2; n     if (t > sameCnt) { n       have += 2 * (t - sameCnt); n       t = sameCnt; n     } n     if (n == 1000) { n     } n     for (int i = 0; i < n; ++i) { n       if (have && A[i] != B[i]) { n         char tempval =  a ; n         for (int j = 0; j < 26; ++j) { n           if (tempval + j != A[i] && tempval + j != B[i]) { n             printf( %c , tempval + j); n             break; n           } n         } n         have--; n         continue; n       } n       if (A[i] != B[i]) { n         if (wic) n           printf( %c , A[i]); n         else n           printf( %c , B[i]); n         wic = 1 - wic; n         continue; n       } n       if (t > 0 && A[i] == B[i]) { n         t--; n         char temp = A[i] + 1; n         if (A[i] ==  z ) temp =  y ; n         printf( %c , temp); n       } else { n         printf( %c , A[i]); n       } n     } n     printf(  n ); n   } else { n     cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n const int c=200002, k=20; n int n, q, p[c], szint[c], fel[c][k], f[c], db[c]; n long long aa[c], bb[c], dist[c], val[c], sum; n bool v[c]; n set<int> s; n int lca(int a, int b) { n     if (szint[a]<szint[b]) { n         swap(a, b); n     } n     for (int i=k-1; i>=0; i--) { n         if (szint[fel[a][i]]>=szint[b]) { n             a=fel[a][i]; n         } n     } n     if (a==b) { n         return a; n     } n     for (int i=k-1; i>=0; i--) { n         if (fel[a][i]!=fel[b][i]) { n             a=fel[a][i], b=fel[b][i]; n         } n     } n     return f[a]; n } n long long tav(int a, int b) { n     int koz=lca(a, b); n     return dist[a]+dist[b]-2*dist[koz]; n } n void keres(int a, int &l, int &r) { n     auto it=s.find(a); n     --it; n     l=*it; n     ++it, ++it; n     if (it!=s.end()) { n         r=*it; n     } n } n void add(int a) { n     s.insert(a); n     int l=0, r=0; n     keres(a, l, r); n     sum+=tav(l, a)+tav(a, r)-tav(l, r); n } n void cl(int a) { n     int l=0, r=0; n     keres(a, l, r); n     sum-=tav(l, a)+tav(a, r)-tav(l, r); n     s.erase(a); n } n int main() n { n     ios_base::sync_with_stdio(false); n     cin >> n >> q; n     p[0]=n+1; n     for (int i=1; i<=n; i++) { n         cin >> p[i]; n         f[i]=i-1; n         while (p[i]>p[f[i]]) { n             f[i]=f[f[i]]; n         } n         szint[i]=szint[f[i]]+1; n         fel[i][0]=f[i]; n         for (int j=1; j<k; j++) { n             fel[i][j]=fel[fel[i][j-1]][j-1]; n         } n     } n     for (int i=1; i<=n; i++) { n         cin >> aa[i]; n         val[i]+=aa[i]; n     } n     for (int i=1; i<=n; i++) { n         cin >> bb[i]; n         val[i]-=bb[i]; n         val[f[i]]+=bb[i]; n     } n     for (int i=n; i>=1; i--) { n         if (val[i]<0) { n             val[f[i]]+=val[i]; n             val[i]=0; n         } n     } n     for (int i=1; i<=n; i++) { n         dist[i]=dist[f[i]]+val[i]; n     } n     db[0]=1; n     s.insert(0); n     for (int i=1; i<=q; i++) { n         int x; cin >> x; n         if (!v[x]) { n             v[x]=1; n             if (++db[f[x]]==1) { n                 add(f[x]); n             } n         } else { n             v[x]=0; n             if (--db[f[x]]==0) { n                 cl(f[x]); n             } n         } n         cout << sum/2+val[0] <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400005; n map<int, int> H; n int n, m, p, l, r, s, tot, T, a[N], b[N], c[N], ans[N]; n int get() { n   int v; n   scanf( %d , &v); n   if (!H[v]) H[v] = ++tot; n   return H[v]; n } n void add(int x) { n   if (c[x]++ < 0) n     --s; n   else n     ++s; n } n void del(int x) { n   if (c[x]-- > 0) n     --s; n   else n     ++s; n } n int main() { n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 0; i < n; ++i) a[i] = get(); n   for (int i = 0; i < m; ++i) b[i] = get(), --c[b[i]]; n   for (int i = 0; i < p; ++i) { n     l = i, r = i, s = m; n     for (int j = 0; j < m - 1; ++j) { n       add(a[r]), r += p; n       if (r >= n) break; n     } n     while (r <= n) { n       add(a[r]), r += p; n       if (!s) ans[T++] = l; n       del(a[l]), l += p; n     } n     while (l <= n) del(a[l]), l += p; n   } n   printf( %d n , T); n   sort(ans, ans + T); n   for (int i = 0; i < T; ++i) printf( %d  , ans[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e9; n struct comparator { n   bool operator()(const pair<int, int> &a, const pair<int, int> &b) { n     return a.first > b.first; n   } n } cmp; n int main() { n   char in; n   vector<pair<int, int> > white, black; n   int board[8][8]; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       board[i][j] = 0; n     } n   } n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       cin >> in; n       if (in ==  W ) { n         white.push_back(pair<int, int>(i, j)); n         board[i][j] = 1; n       } n       if (in ==  B ) { n         black.push_back(pair<int, int>(i, j)); n         board[i][j] = 2; n       } n     } n   } n   sort(white.begin(), white.end()); n   sort(black.begin(), black.end(), cmp); n   int i = 0, j = 0, x; n   if (white[i].first == 0) { n     cout <<  A  << endl; n     return 0; n   } n   int pos_A = white[i].first; n   int pos_B = black[j].first; n   if (pos_A - 1 == 0 && board[pos_A - 1][white[i].second] == 0) { n     cout <<  A  << endl; n     return 0; n   } n   while (pos_A != 0) { n     pos_A--; n     if (board[pos_A][white[i].second] != 0) { n       i++; n       pos_A = white[i].first; n       continue; n     } n   } n   while (pos_B != 7) { n     pos_B++; n     if (board[pos_B][black[j].second] != 0) { n       j++; n       pos_B = black[j].first; n       continue; n     } n   } n   pos_A = white[i].first; n   pos_B = black[j].first; n   if (pos_A - (7 - pos_B) <= 0) { n     cout <<  A  << endl; n   } else { n     cout <<  B  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, n; n   string s, s1, b; n   cin >> n >> k >> s; n   b = s; n   for (i = k; i < n; i++) b[i] = b[i - k]; n   if (b >= s) n     cout << n << endl << b << endl; n   else { n     for (i = k - 1; i >= 0; i--) { n       if (b[i] ==  9 ) n         b[i] =  0 ; n       else { n         b[i]++; n         break; n       } n     } n     for (i = k; i < n; i++) b[i] = b[i - k]; n     cout << n << endl << b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int u, int y) { return u > y; } n int main() { n   int i, j; n   int n, m, x; n   int a[100009], b[100009]; n   int cnt; n   while (scanf( %d %d , &n, &x) != EOF) { n     for (i = 0; i < n; i++) scanf( %d , &a[i]); n     sort(a, a + n, cmp); n     for (i = 0; i < n; i++) scanf( %d , &b[i]); n     sort(b, b + n); n     cnt = 0; n     int k = 0; n     for (i = 0; i < n; i++) { n       for (j = k; j < n; j++) { n         if (a[i] != -0x3fffffff && b[j] != -0x3fffffff) { n           if (a[i] + b[j] >= x) { n             a[i] = -0x3fffffff; n             b[j] = -0x3fffffff; n             cnt++; n             k = j; n             break; n           } n         } n       } n     } n     printf( 1 %d n , cnt); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int find(string s, string sub, int begin, int end) { n   for (int i = begin; i < end - 1; i++) n     if (s[i] == sub[0] && s[i + 1] == sub[1]) return i; n   return -1; n } n int main() { n   string s, sub; n   cin >> s; n   int size = s.size(); n   int index1, index2, index3, index4; n   index1 = find(s,  AB , 0, size); n   index2 = find(s,  BA , 0, size); n   index3 = find(s,  BA , index1 + 2, size); n   index4 = find(s,  AB , index2 + 2, size); n   if ((index1 + 1 && index3 + 1) || (index2 + 1 && index4 + 1)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   std::ios::sync_with_stdio(false); n   string a; n   cin >> a; n   for (int i = 0; i < a.size(); i++) { n     if (((int)(a[i]) >= 48 && (int)(a[i]) <= 57) || n         ((int)(a[i]) >= 97 && (int)(a[i]) <= 122) || (int)(a[i]) == 95 || n         (int)(a[i]) == 64 || (int)(a[i]) == 46 || (int)(a[i]) == 47 || n         ((int)(a[i]) >= 65 && (int)(a[i]) <= 90)) { n       continue; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   string d =   ; n   int ans1 = -1; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] ==  @ ) { n       ans1 = i; n       break; n     } n     d += a[i]; n   } n   if (ans1 == 0 || ans1 == -1) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < d.size(); i++) { n     if (d[i] ==  .  || d[i] ==  / ) { n       cout <<  NO ; n       return 0; n     } n   } n   string e =   ; n   int ans2 = -1; n   for (int i = ans1 + 1; i < a.size(); i++) { n     if (a[i] ==  / ) { n       ans2 = i; n       break; n     } n     e += a[i]; n   } n   if (e.size() == 0) { n     cout <<  NO ; n     return 0; n   } n   if (ans2 - ans1 == 1) { n     cout <<  NO ; n     return 0; n   } n   int ans3 = 0; n   for (int i = 0; i < e.size(); i++) { n     if (e[i] ==  @ ) { n       cout <<  NO ; n       return 0; n     } n     if (e[i] ==  . ) { n       ans3++; n     } n   } n   if (e[e.size() - 1] ==  .  || e[0] ==  . ) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < e.size() - 1; i++) { n     if (e[i] ==  .  && e[i + 1] ==  . ) { n       cout <<  NO ; n       return 0; n     } n   } n   if (ans2 == -1) { n     cout <<  YES ; n     return 0; n   } n   string r =   ; n   for (int i = ans2 + 1; i < a.size(); i++) { n     if (a[i] ==  /  || a[i] ==  .  || a[i] ==  @ ) { n       cout <<  NO ; n       return 0; n     } n   } n   if (ans2 == a.size() - 1) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int M = 1e9 + 7; n void madd(int& a, int b) { n   a += b; n   if (a >= M) a -= M; n } n int mult(int a, int b) { return (1LL * a * b) % M; } n int modexp(int a, int b) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = mult(res, a); n     a = mult(a, a); n     b >>= 1; n   } n   return res; n } n int inverse(int x) { return modexp(x, M - 2); } n const int maxn = 1e5 + 5; n int n, a[maxn]; n int cnt[maxn]; n int mu[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     cnt[a[i]]++; n   } n   mu[1] = 1; n   for (int i = 1; i < maxn; i++) { n     for (int j = 2 * i; j < maxn; j += i) { n       mu[j] -= mu[i]; n       cnt[i] += cnt[j]; n     } n   } n   int ans = 0; n   for (int d = 1; d <= 100000; d++) { n     madd(ans, mult((mu[d] + M) % M, modexp(2, cnt[d]) - 1)); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int x, d, nxt = 1, arr[10001] = {0}, pos = 0; n   cin >> x >> d; n   for (int j = 30; j >= 0; j--) n     if (x >= (((long long int)1 << j))) { n       x -= ((long long int)1 << j); n       for (int i = 1; i <= j; i++) arr[pos++] = nxt; n       nxt += d; n       arr[pos++] = nxt; n       nxt += d; n     } n   if (x == 0) { n     cout << pos << endl; n     for (int j = 0; j < pos; j++) cout << arr[j] <<    ; n     cout << endl; n   } else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v, f; n   Edge *next, *rest; n }; n int n, m, b, x[100], k[100], p[100]; n long long dp[1048576]; n long long ans = 0x3FFFFFFFFFFFFFFFLL; n int main() { n   scanf( %d%d%d , &n, &m, &b); n   for (int i = 0, len; i < n; i++) { n     scanf( %d%d%d , &x[i], &k[i], &len); n     for (int j = 0, t; j < len; j++) scanf( %d , &t), p[i] |= 1 << (t - 1); n     p[i] ^= (1 << m) - 1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = n - 1; j > i; j--) { n       if (k[j - 1] > k[j]) n         swap(k[j - 1], k[j]), swap(x[j - 1], x[j]), swap(p[j - 1], p[j]); n     } n   } n   for (int i = 1; i < 1 << m; i++) dp[i] = 0x3FFFFFFFFFFFFFFFLL; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 1 << m; j++) dp[j] = min(dp[j], dp[j & p[i]] + x[i]); n     if (dp[(1 << m) - 1] < 0x3FFFFFFFFFFFFFFFLL) n       ans = min(ans, dp[(1 << m) - 1] + (long long)k[i] * b); n   } n   printf( %I64d , ans == 0x3FFFFFFFFFFFFFFFLL ? -1 : ans); n   getchar(); n   getchar(); n   return 0; n } n