#include <bits/stdc++.h>   using namespace std;   int a[25][25], n, m, dp[1 << 22];   char s[25][222];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) scanf( %s , s[i]);     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) scanf( %d , &a[i][j]);     memset(dp, 63, sizeof(dp)), dp[0] = 0;     for (int l, msk = 0; msk < (1 << n); ++msk) {       for (int b = 0; b < n; ++b)         if (!(msk & (1 << b))) {           l = b;           break;         }       for (int c = 0; c < m; ++c) {         dp[msk + (1 << l)] = min(dp[msk + (1 << l)], dp[msk] + a[l][c]);         int t = 0, h = 0, tmsk = msk;         for (int i = 0; i < n; ++i)           if (s[i][c] == s[l][c])             tmsk |= (1 << i), t += a[i][c], h = max(h, a[i][c]);         dp[tmsk] = min(dp[tmsk], dp[msk] + t - h);       }     }     printf( %d n , dp[(1 << n) - 1]);   }


#include <bits/stdc++.h>   using namespace std;   long long ans[200005];   vector<vector<pair<long long, long long> > > G(200005);   int main() {     long long i, j, n, m, a, b, c;     priority_queue<pair<long long, long long> > pq;     scanf( %lld %lld , &n, &m);     for (i = 0; i < m; i++) {       scanf( %lld %lld %lld , &a, &b, &c);       G[a].push_back({b, c});       G[b].push_back({a, c});     }     for (i = 1; i <= n; i++) {       scanf( %lld , &ans[i]);       pq.push({-ans[i], i});     }     while (!pq.empty()) {       pair<long long, long long> tp = pq.top();       pq.pop();       tp.first *= -1;       if (tp.first > ans[tp.second]) continue;       ans[tp.second] = tp.first;       for (pair<long long, long long> next : G[tp.second]) {         long long poss = next.second * 2 + tp.first;         if (poss < ans[next.first]) {           pq.push({-poss, next.first});         }       }     }     for (i = 1; i <= n; i++) printf( %lld  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void k_007() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool sortbysec(const pair<long long, long long> &a,                  const pair<long long, long long> &b) {     return (a.second < b.second);   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long prime(long long n) {     long long flag = 1;     for (long long i = 2; i <= sqrt(n); i++) {       if (n % i == 0) {         flag = 0;         break;       }     }     return flag;   }   long long power(long long x, long long y) {     if (y == 0)       return 1;     else if (y % 2 == 0)       return ((power(x, y / 2) % 1000000007) * (power(x, y / 2) % 1000000007)) %              1000000007;     else       return ((x % 1000000007) * (power(x, y / 2) % 1000000007) *               (power(x, y / 2) % 1000000007)) %              1000000007;   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   int32_t main() {     k_007();     long long n;     cin >> n;     if (n & 1) {       if (n == 1) {         cout << 3 << endl;       } else {         cout << 1 << endl;       }     } else {       long long m = 1;       while (prime(n * m + 1)) {         m++;       }       cout << m << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i{0}; i < n; i++) {       int found{0};       int num{};       cin >> num;       int res{};       for (int j{0}; j < num; j++) {         int numI;         cin >> numI;         if (numI % 2 == 0) {           found = 1;           res = j + 1;         }       }       if (found == 1) {         cout << 1 <<   n  << res <<   n ;       } else {         if (num > 1)           cout << 2 <<   n  << 1 <<     << 2 <<   n ;         else           cout << -1 <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, l, r, st;   map<pair<long long, long long>, long long> M[2];   long long Count(long long len, long long t, bool w) {     if (M[w].count({len, t})) return (M[w][{len, t}]);     if (t > len) return (M[w][{len, t}] = 0);     if (len == t) return (M[w][{len, t}] = 1);     return (M[w][{len, t}] =                 Count(len / 2, t, w) + Count((len - 1) / 2, t, w) + w);   }   long long get(long long len, long long t) {     return (Count(len, t, 0) + Count(len, t + 1, 0));   }   int main() {     scanf( %I64d%I64d , &n, &k);     if (k == 1) return !printf( 1 );     if (k == 2) return !printf( %I64d , n);     n -= 2;     k -= 2;     l = 1;     r = n;     while (r - l > 1) {       if (k > Count(n, (l + r) / 2 * 2 - 1, 1))         r = (l + r) / 2;       else         l = (l + r) / 2;     }     st = l * 2 - 1;     l = 1;     r = n;     k -= Count(n, st + 2, 1);     while (get(r - l + 1, st) && !(st == 1 && r - l <= 1)) {       if (k <= get((l + r) / 2 - l, st))         r = (l + r) / 2 - 1;       else         k -= get((l + r) / 2 - l, st), l = (l + r) / 2 + 1;     }     if (st == 1 && r - l <= 1) l += k;     return !printf( %I64d , l);   }


#include <bits/stdc++.h>   using namespace std;   struct algo {     int n;     map<pair<int, int>, int> v1;     vector<vector<int> > drogi;     vector<pair<int, int> > tab;     vector<pair<int, int> > wy;     algo() {       cin >> n;       string s;       int li = 0;       tab.push_back({});       for (long long a, b, i = 1; i <= n; i++) {         cin >> s;         a = b = 0;         for (int j = 0; j < s.size(); j++) {           a = (a * 33 + s[j] -  a  + 1) % 1000000009;           if (j != 0) b = (b * 33 + s[j] -  a  + 1) % 1000000009;           if (v1.find({a, j + 1}) != v1.end()) continue;           v1[{a, j + 1}] = ++li;           tab.push_back({b, j});         }       }       drogi.resize(li + 10);       for (int i = 1; i <= li; i++) {         drogi[v1[tab[i]]].push_back(i);       }       wy.resize(li + 10);       cout << dfs().second <<   n ;     }     pair<int, int> dfs(int gdzie = 0) {       if (wy[gdzie].second != 0) return wy[gdzie];       int x = 0, y = 0;       pair<int, int> p;       for (int i : drogi[gdzie]) {         p = dfs(i);         x += p.first;         y += p.second;       }       return wy[gdzie] = {max(y + 1, x), x};     }   };   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       algo p;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n, ans = 0, i, temp;     vector<int> a;     cin >> n;     for (i = 0; i < n; i++) {       cin >> temp;       a.push_back(temp);     }     sort(a.begin(), a.end());     for (i = 0; i < n; i = i + 2) {       if (a[i] != a[i + 1]) {         ans += (a[i + 1] - a[i]);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isluck() {     unsigned long long n, s1 = 0, s2 = 0;     string s;     cin >> n >> s;     for (int register i = 0; i < n / 2; i++) {       int x = (s[i] -  0 ) % 10;       if (x != 4 && x != 7) return false;       s1 += x;     }     for (int register i = n / 2; i < n; i++) {       int x = (s[i] -  0 ) % 10;       if (x != 4 && x != 7) return false;       s2 += x;     }     return (s1 == s2);   }   int main() {     isluck() ? cout <<  YES  << endl : cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b;   int sum;   int main() {     cin >> a >> b;     while (a >= b) {       sum += a / b * b;       a = a / b + a % b;     }     cout << sum + a;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, j;   std::vector<char> v;   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n;     int i = 0;     while (i < n) {       j = 0;       while (j < 2 && i < n) {         cout <<  a ;         j++;         i++;       }       j = 0;       while (j < 2 && i < n) {         cout <<  b ;         j++;         i++;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3000;   char a[N][N];   int n, m, k, vis[N][N];   long long ans = 0;   int main() {     while (~scanf( %d%d%d , &n, &m, &k)) {       ans = 0;       for (register int i = 0; i <= n - 1; ++i) scanf( %s , a[i]);       for (register int i = 0; i <= n - 1; ++i) {         int c = 0;         for (register int j = 0; j <= m - 1; ++j) {           if (a[i][j] ==  . )             c++;           else             c = 0;           if (c >= k) ans++;         }       }       for (register int j = 0; j <= m - 1; ++j) {         int c = 0;         for (register int i = 0; i <= n - 1; ++i) {           if (a[i][j] ==  . )             c++;           else             c = 0;           if (c >= k) ans++;         }       }       if (k == 1) ans /= 2;       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mark[1000009];   int main() {     long long n, m = 0, i, j, t;     long long sum;     cin >> n;     long long a[n];     for (i = 0; i < n; i++) {       scanf( %lld , &a[i]);     }     t = a[n - 1];     sum = t;     for (j = n - 1; j > 0; j--) {       if (a[j - 1] < t) {         sum += a[j - 1];         t = a[j - 1];       } else {         if (t - 1 >= 0) {           sum += t - 1;           t = t - 1;         } else           break;       }     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   inline char nc() {     static char buf[100000], *l = buf, *r = buf;     return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)                ? EOF                : *l++;   }   template <class T>   void read(T &x) {     x = 0;     int f = 1, ch = nc();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = nc();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 -  0  + ch;       ch = nc();     }     x *= f;   }   const int inf = 1e9;   const int maxn = 3e5 + 50;   int n;   int a[maxn];   int b[maxn];   long long an;   namespace seg {   const int maxnode = maxn << 2;   int mn[maxnode];   int cnt[maxnode];   int tag[maxnode];   inline void change(int u, int d) {     mn[u] += d;     tag[u] += d;   }   inline void pushup(int u) {     mn[u] = min(mn[u << 1], mn[u << 1 | 1]);     cnt[u] = 0;     if (mn[u] == mn[u << 1]) cnt[u] += cnt[u << 1];     if (mn[u] == mn[u << 1 | 1]) cnt[u] += cnt[u << 1 | 1];   }   inline void pushdown(int u) {     if (tag[u]) {       change(u << 1, tag[u]);       change(u << 1 | 1, tag[u]);       tag[u] = 0;     }   }   void build(int u, int l, int r) {     tag[u] = 0;     if (l == r) {       mn[u] = -l;       cnt[u] = 1;       return;     }     int mid = (l + r) >> 1;     build(u << 1, l, mid);     build(u << 1 | 1, mid + 1, r);     pushup(u);   }   void update(int u, int l, int r, int ql, int qr, int qv) {     if (l == ql && r == qr) {       change(u, qv);       return;     }     pushdown(u);     int mid = (l + r) >> 1;     if (qr <= mid)       update(u << 1, l, mid, ql, qr, qv);     else if (ql > mid)       update(u << 1 | 1, mid + 1, r, ql, qr, qv);     else {       update(u << 1, l, mid, ql, mid, qv);       update(u << 1 | 1, mid + 1, r, mid + 1, qr, qv);     }     pushup(u);   }   }  // namespace seg   void divide(int l, int r) {     static int mn[maxn];     int hd0 = 1, tl0 = 0;     static int mx[maxn];     int hd1 = 1, tl1 = 0;     if (l == r) {       return;     }     int mid = (l + r) >> 1;     divide(l, mid);     divide(mid + 1, r);     int L = r - mid;     seg::build(1, 1, L);     for (int i = 1; i <= L; ++i) {       b[i] = a[mid + i];       if (hd0 > tl0 || b[i] < b[mn[tl0]]) mn[++tl0] = i;       if (hd1 > tl1 || b[i] > b[mx[tl1]]) mx[++tl1] = i;     }     mn[++tl0] = L + 1;     mx[++tl1] = L + 1;     for (int i = hd0; i < tl0; ++i) {       seg::update(1, 1, L, mn[i], mn[i + 1] - 1, -b[mn[i]]);     }     for (int i = hd1; i < tl1; ++i) {       seg::update(1, 1, L, mx[i], mx[i + 1] - 1, b[mx[i]]);     }     int Mx = -inf, Mn = inf;     for (int i = mid; i >= l; --i) {       seg::update(1, 1, L, 1, L, -1);       if (a[i] < Mn) {         if (hd0 != 1) seg::update(1, 1, L, 1, mn[hd0] - 1, Mn);         Mn = a[i];         while (hd0 < tl0 && Mn < b[mn[hd0]]) {           seg::update(1, 1, L, mn[hd0], mn[hd0 + 1] - 1, b[mn[hd0]]), ++hd0;         }         if (hd0 != 1) seg::update(1, 1, L, 1, mn[hd0] - 1, -Mn);       }       if (a[i] > Mx) {         if (hd1 != 1) seg::update(1, 1, L, 1, mx[hd1] - 1, -Mx);         Mx = a[i];         while (hd1 < tl1 && Mx > b[mx[hd1]]) {           seg::update(1, 1, L, mx[hd1], mx[hd1 + 1] - 1, -b[mx[hd1]]), ++hd1;         }         if (hd1 != 1) seg::update(1, 1, L, 1, mx[hd1] - 1, Mx);       }       if (seg::mn[1] == -1) an += seg::cnt[1];     }   }   int main() {     read(n);     for (int i = 1; i <= n; ++i) {       int r, c;       read(r), read(c);       a[r] = c;     }     an = n;     divide(1, n);     printf( %lld n , an);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200000;   const long long maxp = 1000000007;   int n, m;   struct data {     int adj, p, p_;   };   vector<data> e[maxn];   bool v[maxn];   long long f[maxn][2][2][2];   int con[maxn][2];   void dfs(int x) {     v[x] = false;     bool flag = false;     for (auto y : e[x])       if (v[y.adj]) {         dfs(y.adj);         flag = true;         for (int i = 0; i < 2; ++i)           for (int j = 0; j < 2; ++j)             for (int k = 0; k < 2; ++k)               for (int l = 0; l < 2; ++l)                 (f[x][i][k][l ^ ((y.p ^ i) | (y.p_ ^ j)) ^ con[x][i]] +=                  f[y.adj][j][k][l]) %= maxp;       }     if (!flag) {       f[x][1][1][con[x][1]] = 1;       f[x][0][0][con[x][0]] = 1;     }   }   int ind[maxn];   int main() {     scanf( %d%d , &m, &n);     memset(ind, 0, sizeof(ind));     for (int i = 1; i <= m; ++i) {       int k;       scanf( %d , &k);       int a, b;       if (k == 1) {         scanf( %d , &a);         if (a < 0)           con[-a][0] ^= 1;         else           con[a][1] ^= 1;       } else {         scanf( %d%d , &a, &b);         int p1 = 0, p2 = 0;         if (a < 0) {           p1 = 1;           a = -a;         }         if (b < 0) {           p2 = 1;           b = -b;         }         e[a].push_back({b, p1, p2});         e[b].push_back({a, p2, p1});         ind[a]++;         ind[b]++;       }     }     memset(v, true, sizeof(v));     memset(f, 0, sizeof(f));     long long ans[2], tmp[2];     ans[0] = 1;     ans[1] = 0;     int ab = 3;     for (int i = 1; i <= n; ++i)       if (v[i] && (ind[i] == 1 || ind[i] == 0)) {         dfs(i);         tmp[0] = ans[0];         tmp[1] = ans[1];         ans[0] = 0;         ans[1] = 0;         for (int p1 = 0; p1 < 2; ++p1)           for (int p2 = 0; p2 < 2; ++p2)             for (int u = 0; u < 2; ++u)               for (int v = 0; v < 2; ++v)                 (ans[p1 ^ p2] += tmp[p1] * f[i][u][v][p2]) %= maxp;       }     for (int i = 1; i <= n; ++i)       if (v[i]) {         dfs(i);         tmp[0] = ans[0];         tmp[1] = ans[1];         ans[0] = 0;         ans[1] = 0;         int p = e[i][1].p, p_ = e[i][1].p_;         for (int p1 = 0; p1 < 2; ++p1)           for (int p2 = 0; p2 < 2; ++p2)             for (int u = 0; u < 2; ++u)               for (int v = 0; v < 2; ++v) {                 (ans[p1 ^ p2 ^ ((u ^ p) | (v ^ p_))] +=                  tmp[p1] * f[i][u][v][p2]) %= maxp;               }       }     cout << ans[1] << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long f = 1, x = 0;     char s = getchar();     while (s <  0  || s >  9 ) {       if (s ==  - ) f = -1;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = x * 10 + (s -  0 );       s = getchar();     }     return x * f;   }   const int jzm = 1000000007;   const int zxy = 998244353;   int n, m, i, j, s, o, k;   int U[100005], V[100005], W[100005];   struct it {     int v, w;     it() { v = w = 0; }     it(int V, int W) {       v = V;       w = W;     }   };   vector<it> g[100005];   vector<it> g2[100005];   bool v[100005];   long long dp1[100005] = {(long long)1e18}, dp2[100005] = {(long long)1e18};   long long f1[100005][2], f2[100005][2];   int tre1[100005 << 2], tre2[100005 << 2], M;   int bing1(int a, int b) { return dp1[a] < dp1[b] ? a : b; }   int bing2(int a, int b) { return dp2[a] < dp2[b] ? a : b; }   void maketree(int n) {     M = 1;     while (M < n + 2) M <<= 1;   }   void addt1(int x, int y) {     int s = M + x;     tre1[s] = y;     s >>= 1;     while (s) {       tre1[s] = bing1(tre1[s << 1], tre1[s << 1 | 1]);       s >>= 1;     }   }   void addt2(int x, int y) {     int s = M + x;     tre2[s] = y;     s >>= 1;     while (s) {       tre2[s] = bing2(tre2[s << 1], tre2[s << 1 | 1]);       s >>= 1;     }   }   int findall1() { return tre1[1]; }   int findall2() { return tre2[1]; }   bool cmp(long long *a, long long *b) { return a[0] == b[0] && a[1] == b[1]; }   int main() {     n = read();     m = read();     int S = read(), T = read();     for (int i = 1; i <= m; i++) {       s = read();       o = read();       k = read();       g[s].push_back(it(o, k));       g2[o].push_back(it(s, k));       U[i] = s;       V[i] = o;       W[i] = k;     }     maketree(n);     for (int i = 1; i <= n; i++) dp1[i] = dp2[i] = 1e18;     dp1[S] = 0;     f1[S][0] = f1[S][1] = 1;     addt1(S, S);     dp2[T] = 0;     f2[T][0] = f2[T][1] = 1;     addt2(T, T);     for (int i = 1; i < n; i++) {       int t = findall1();       if (t == 0) break;       for (int j = 0; j < g[t].size(); j++) {         int y = g[t][j].v, w = g[t][j].w;         if (dp1[t] + w < dp1[y]) {           dp1[y] = dp1[t] + w;           f1[y][0] = f1[t][0];           f1[y][1] = f1[t][1];           addt1(y, y);         } else if (dp1[t] + w == dp1[y]) {           (f1[y][0] += f1[t][0]) %= jzm;           (f1[y][1] += f1[t][1]) %= zxy;         }       }       addt1(t, 0);     }     for (int i = 1; i < n; i++) {       int t = findall2();       if (t == 0) break;       for (int j = 0; j < g2[t].size(); j++) {         int y = g2[t][j].v, w = g2[t][j].w;         if (dp2[t] + w < dp2[y]) {           dp2[y] = dp2[t] + w;           f2[y][0] = f2[t][0];           f2[y][1] = f2[t][1];           addt2(y, y);         } else if (dp2[t] + w == dp2[y]) {           (f2[y][0] += f2[t][0]) %= jzm;           (f2[y][1] += f2[t][1]) %= zxy;         }       }       addt2(t, 0);     }     long long F[2] = {f1[T][0], f1[T][1]}, le = dp1[T];     for (int i = 1; i <= m; i++) {       s = U[i];       o = V[i];       k = W[i];       long long F2[2] = {f1[s][0] * 1ll * f2[o][0] % jzm,                          f1[s][1] * 1ll * f2[o][1] % zxy};       if (dp1[s] + (long long)k + dp2[o] > le || !cmp(F, F2)) {         if (le - 1 - dp1[s] - dp2[o] <= 0)           printf( NO n );         else           printf( CAN %d n , k - (le - 1 - dp1[s] - dp2[o]));       } else         printf( YES n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void setIO(string name) {     ios_base::sync_with_stdio(0);     cin.tie(0);     freopen((name +  .in ).c_str(),  r , stdin);     freopen((name +  .out ).c_str(),  w , stdout);   }   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   int main() {     fast();     int r, c;     cin >> r >> c;     char a[r][c];     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         cin >> a[i][j];         if (a[i][j] ==  . ) {           a[i][j] =  D ;         }       }     }     int f = 1;     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         if (a[i][j] ==  S ) {           if ((i - 1 >= 0 && a[i - 1][j] ==  W ) ||               (j - 1 >= 0 && a[i][j - 1] ==  W ) ||               (i + 1 <= r - 1 && a[i + 1][j] ==  W ) ||               (j + 1 <= c - 1 && a[i][j + 1] ==  W )) {             f = 0;             break;           }         }       }     }     if (f) {       cout <<  Yes             <<   n ;       for (int i = 0; i < r; i++) {         for (int j = 0; j < c; j++) {           cout << a[i][j];         }         cout <<   n ;       }     } else {       cout <<  No             <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const int MAXN = 1e5 + 10;   const int N = 102;   const long long mod = 1e9 + 7;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     return x * f;   }   int a[5];   int main() {     ios::sync_with_stdio(false);     int T;     cin >> T;     while (T--) {       int A, B, C, D;       memset(a, 0, sizeof(a));       cin >> A >> B >> C >> D;       C += B;       D += A;       if ((A + B) % 2 == 0) {         a[1] = a[2] = 0;         if (C > 0) a[3] = 1;         if (D > 0) a[4] = 1;       } else {         a[3] = a[4] = 0;         if (C > 0) a[2] = 1;         if (D > 0) a[1] = 1;       }       for (int i = 1; i <= 4; ++i)         if (a[i])           cout <<  Ya  ;         else           cout <<  Tidak  ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, a, b, g[3005 * 3005], x, y, z, A[3005][3005], ans;   deque<long long> q;   vector<long long> v;   vector<vector<long long> > V;   vector<long long> fun(vector<long long> v, long long n, long long b) {     vector<long long> ans;     q.clear();     for (__typeof((b)) i = (0); i < (b); i++) {       while ((long long)q.size() && v[i] <= v[q.back()]) q.pop_back();       q.push_back(i);     }     ans.push_back(v[q.front()]);     for (__typeof((n)) i = (b); i < (n); i++) {       while ((long long)q.size() && q.front() <= i - b) q.pop_front();       while ((long long)q.size() && v[i] <= v[q.back()]) q.pop_back();       q.push_back(i);       ans.push_back(v[q.front()]);     }     return ans;   }   void solve() {     cin >> n >> m >> a >> b >> g[0] >> x >> y >> z;     for (__typeof((3005 * 3005)) i = (1); i < (3005 * 3005); i++)       g[i] = (g[i - 1] * x + y) % z;     for (__typeof((n + 1)) i = (1); i < (n + 1); i++) {       for (__typeof((m + 1)) j = (1); j < (m + 1); j++)         A[i][j] = g[(i - 1) * m + j - 1];     }     for (__typeof((n + 1)) i = (1); i < (n + 1); i++) {       v.clear();       for (__typeof((m + 1)) j = (1); j < (m + 1); j++) v.push_back(A[i][j]);       V.push_back(fun(v, m, b));     }     for (__typeof(((long long)V[0].size())) j = (0); j < ((long long)V[0].size());          j++) {       v.clear();       for (__typeof((n)) i = (0); i < (n); i++) v.push_back(V[i][j]);       vector<long long> res = fun(v, n, a);       for (auto i : res) ans += i;     }     {       cout << ans <<   n ;       return;     };   }   void prep() {}   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     prep();     cout << fixed << setprecision(12);     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   const int inf = 0x3f3f3f3f;   int n, e[2][maxn], tot, rd[maxn];   int net[maxn], tail[maxn];   inline int read(void) {     int num, sign = 1;     char c;     while (!isdigit(c = getchar()))       if (c ==  - ) sign = 0;     num = c -  0 ;     while (isdigit(c = getchar())) num = (num << 1) + (num << 3) + c -  0 ;     return sign ? num : -num;   }   int dfs(int u) {     tail[u] = u;     if (!tail[net[u]]) return tail[u] = dfs(net[u]);     return tail[u];   }   signed main(void) {     n = read();     for (register int i = 1; i <= n; ++i) net[i] = read(), ++rd[net[i]];     for (register int i = 1; i <= n; ++i)       if (!rd[i]) {         e[0][++tot] = i;         e[1][tot] = dfs(i);       }     int ft = tot;     for (register int i = 1; i <= n; ++i)       if (!tail[i]) {         e[0][++tot] = i;         e[1][tot] = dfs(i);       }     if (tot == 1 && !ft) tot = 0;     printf( %d n , tot);     for (register int i = 1; i <= tot; ++i)       printf( %d %d n , e[1][i], e[0][i % tot + 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     int q;     cin >> q;     for (int i = 0; i < q; i++) {       ll k, n, a, b;       cin >> k >> n >> a >> b;       if (k - n * b <= 0) {         cout << -1 <<   n ;         continue;       }       ll l = -1, r = n;       while (r - l > 1) {         ll m = (l + r) / 2;         if (k - m * b - (n - m) * a > 0) {           r = m;         } else {           l = m;         }       }       cout << n - r <<   n ;     }   }


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


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10, mod = 1e9 + 7;   const long long inf = 1e18;   unsigned int dp[maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie();     int n, cnt = 0;     cin >> n;     string s;     cin >> s;     if (n & 1) return cout << 0 << endl, 0;     dp[0] = 1;     for (int i = 0; i < n; i++) {       if (s[i] !=  ? ) {         cnt++;         for (int j = i + 1; j >= 0; j -= 2) dp[j] = (j == 0 ? 0 : dp[j - 1]);       } else {         for (int j = i + 1; j >= 0; j -= 2)           dp[j] = dp[j + 1] + (j == 0 ? 0 : dp[j - 1]);       }     }     int num = (n - 2 * cnt) / 2;     unsigned int ans = dp[0];     for (int i = 0; i < num; i++) ans *= 25;     return cout << ans << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v;     int n, i, j, k, len, x, y, c, a[1020] = {0};     cin >> n;     if (n == 1) {       cout <<  0  << endl;       return 0;     }     a[1] = 1;     for (i = 2; i <= 1000; i++) {       c = 2;       for (j = i * c; j <= 1000; j = i * c) {         a[j] = 1;         c++;       }     }     for (i = 2; i <= n; i++) {       if (a[i] == 0) {         for (j = i; j <= n; j = j * i) {           v.push_back(j);         }       }     }     cout << v.size() << endl;     for (i = 0; i < v.size(); i++) {       cout << v[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   int visited[30009];   bool ok = false;   vector<int> C[30009];   void dfs(int u) {     if (u == t) {       ok = true;       return;     }     visited[u] = 1;     if (C[u].size())       for (int i = 0; i <= C[u].size() - 1; i++) {         int v = C[u][i];         if (!visited[v] && v <= n) {           dfs(v);           if (ok) return;         }       }   }   int main() {     scanf( %d%d , &n, &t);     for (int i = 1; i <= n - 1; i++) {       int x;       scanf( %d , &x);       C[i].push_back(x + i);     }     dfs(1);     if (ok)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> G[((int)2e5 + 10)];   int dis[((int)2e5 + 10)];   int n;   int ans = 0;   void dfs(int f, int son, int d) {     int flag = 0;     dis[son] = d;     for (int i = 0; i < G[son].size(); i++) {       if (G[son][i] == f) continue;       dfs(son, G[son][i], d + 1);       if (dis[G[son][i]] > 2) {         flag = 1;         dis[G[son][i]] = 2;         dis[son] = 1;         dis[f] = 2;       }     }     ans += flag;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n - 1; i++) {       int x, y;       scanf( %d %d , &x, &y);       G[x].push_back(y);       G[y].push_back(x);     }     dfs(-1, 1, 0);     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005, MAXT = 500, INF = 0x3f3f3f3f;   int mx[MAXT], add[MAXT], sz[MAXT], num[MAXT][MAXT], A[MAXN];   int N, T = 314;   void upd(int p) {     if (add[p]) {       for (int i = 0; i < sz[p]; ++i) num[p][i] += add[p];       add[p] = 0;     }   }   void ins(int x, int k) {     int p = x / T;     x = x % T;     for (int i = 0; i < p; ++i) add[i] += k, mx[i] += k;     upd(p);     for (int i = 0; i <= x; ++i) num[p][i] += k;     mx[p] = -INF;     for (int i = 0; i < sz[p]; ++i) mx[p] = max(mx[p], num[p][i]);   }   int query() {     int p = (N - 1) / T;     while (p >= 0 && mx[p] <= 0) p--;     if (p < 0) return -1;     upd(p);     int x = sz[p] - 1;     while (num[p][x] <= 0) x--;     return A[p * T + x];   }   int main() {     scanf( %d , &N);     int p = (N - 1) / T;     for (int i = 0; i < p; ++i) sz[i] = T;     sz[p] = (N - 1) % T + 1;     for (int i = 0; i < N; ++i) {       int a, b, c;       scanf( %d%d , &a, &b);       if (b) {         scanf( %d , &c);         A[a - 1] = c;         ins(a - 1, 1);         printf( %d n , query());       } else {         ins(a - 1, -1);         printf( %d n , query());       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-11;   long long n, l, v1, v2, k;   bool check(double tim) {     int tmp = n;     double t = 0;     double grx = 0, bus = 0;     while (tmp > 0) {       double del = l - grx;       if (del / v2 > tim + EPS / 100) {         return 0;       }       double x = (v1 * tim - del) * v2 * 1.0 / (v1 - v2);       double step = x / v2;       tim -= step;       bus += x;       tmp -= k;       if (tmp <= 0) break;       grx += step * v1;       double step2 = (bus - grx) / (v1 + v2);       grx += step2 * v1;       tim -= step2;       if (tim < -EPS / 100) return 0;       bus = grx;     }     return 1;   }   double bs(double lo, double hi) {     if ((hi - lo) <= EPS * 10 || (hi - lo) / hi <= EPS * 10) {       return lo;     }     double mid = (lo + hi) / 2;     if (check(mid)) {       return bs(lo, mid);     }     return bs(mid, hi);   }   int main() {     if (fopen( D.in ,  r )) freopen( D.in ,  r , stdin);     cin >> n >> l >> v1 >> v2 >> k;     cout << setprecision(10) << bs(0, 1e10);     return 0;   }


#include <iostream>   #include <cstdio>   #include <cmath>   #include <ctime>   #include <cstdlib>   #include <cassert>   #include <vector>   #include <list>   #include <stack>   #include <queue>   #include <deque>   #include <map>   #include <set>   #include <bitset>   #include <string>   #include <algorithm>   #include <utility>   #include <complex>   #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)   #define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)   #define chmin(x, y) (x) = min((x), (y))   #define chmax(x, y) (x) = max((x), (y))   #define sz(x) ((ll)(x).size())   #define ceil(x, y) (((x)+(y)-1) / (y))   #define all(x) (x).begin(),(x).end()   #define outl(...) dump_func(__VA_ARGS__)   #define inf 1e18      using namespace std;      typedef long long llint;   typedef long long ll;   typedef pair<ll, ll> P;      struct edge{    ll to, cost;    edge(){}    edge(ll a, ll b){ to = a, cost = b;}   };   const ll dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};      const ll mod = 1000000007;   //const ll mod = 998244353;      struct mint{    ll x = 0;    mint(ll y = 0){x = y; if(x < 0 || x >= mod) x = (x%mod+mod)%mod;}    mint(const mint &ope) {x = ope.x;}        mint operator-(){return mint(-x);}    mint operator+(const mint &ope){return mint(x) += ope;}    mint operator-(const mint &ope){return mint(x) -= ope;}    mint operator*(const mint &ope){return mint(x) *= ope;}    mint operator/(const mint &ope){return mint(x) /= ope;}    mint& operator+=(const mint &ope){     x += ope.x;     if(x >= mod) x -= mod;     return *this;    }    mint& operator-=(const mint &ope){     x += mod - ope.x;     if(x >= mod) x -= mod;     return *this;    }    mint& operator*=(const mint &ope){     x *= ope.x, x %= mod;     return *this;    }    mint& operator/=(const mint &ope){     ll n = mod-2; mint mul = ope;     while(n){      if(n & 1) *this *= mul;      mul *= mul;      n >>= 1;     }     return *this;    }    mint inverse(){return mint(1) / *this;}    bool operator ==(const mint &ope){return x == ope.x;}    bool operator !=(const mint &ope){return x != ope.x;}   };   mint modpow(mint a, ll n){    if(n == 0) return mint(1);    if(n % 2) return a * modpow(a, n-1);    else return modpow(a*a, n/2);   }   istream& operator >>(istream &is, mint &ope){    ll t; is >> t, ope.x = t;    return is;   }   ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}   ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}      bool exceed(ll x, ll y, ll m){return x >= m / y + 1;}   void mark(){ cout <<  *  << endl; }   void yes(){ cout <<  Yes  << endl; }   void no(){ cout <<  No  << endl; }   ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}   ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}   ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}      template<typename T>   ostream& operator << (ostream& os, vector<T>& vec) {    for(int i = 0; i < vec.size(); i++) {     os << vec[i] << (i + 1 == vec.size() ?    :    );    }    return os;   }   template<typename T>   ostream& operator << (ostream& os, deque<T>& deq) {    for(int i = 0; i < deq.size(); i++) {     os << deq[i] << (i + 1 == deq.size() ?    :    );    }    return os;   }   template<typename T, typename U>   ostream& operator << (ostream& os, pair<T, U>& pair_var) {    os <<  (  << pair_var.first <<  ,   << pair_var.second <<  ) ;    return os;   }   template<typename T, typename U>   ostream& operator << (ostream& os, const pair<T, U>& pair_var) {    os <<  (  << pair_var.first <<  ,   << pair_var.second <<  ) ;    return os;   }   template<typename T, typename U>   ostream& operator << (ostream& os, map<T, U>& map_var) {    for(typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end(); itr++) {     os <<  (  << itr->first <<  ,   << itr->second <<  ) ;     itr++;     if(itr != map_var.end()) os <<  , ;     itr--;    }    return os;   }   template<typename T>   ostream& operator << (ostream& os, set<T>& set_var) {    for(typename set<T>::iterator itr = set_var.begin(); itr != set_var.end(); itr++) {     os << *itr;     ++itr;     if(itr != set_var.end()) os <<    ;     itr--;    }    return os;   }   template<typename T>   void outa(T a[], ll s, ll t)   {    for(ll i = s; i <= t; i++){ cout << a[i]; if(i < t) cout <<    ;}    cout << endl;   }   void dump_func() {cout << endl;}   template <class Head, class... Tail>   void dump_func(Head &&head, Tail &&... tail) {    cout << head;    if(sizeof...(Tail) > 0) cout <<    ;    dump_func(std::move(tail)...);   }         ll n;   ll a[200005], b[200005], p[400005], f[400005];   ll c[400005];   set<ll> S;      int main(void)   {    ios::sync_with_stdio(0);    cin.tie(0);        cin >> n;    rep(i, 1, n) cin >> a[i] >> b[i], f[a[i]] = 1, p[a[i]] = b[i], p[b[i]] = a[i];    rep(i, 1, 2*n) S.insert(i);        priority_queue<P, vector<P>, greater<P> > leftQ;    priority_queue<P> rightQ;        ll ans = 0;    while(sz(S)){     ll s = *S.begin();     leftQ.push(P(s, 1));     c[s] = 1;     S.erase(s);          ll cnt = 0, fcnt = 0;     while(sz(leftQ) + sz(rightQ)){      while(sz(leftQ)){       cnt++;       ll x = leftQ.top().first, y = leftQ.top().second; leftQ.pop();       //outl(x, y);              c[x] = y; if(y != f[x]) fcnt++;       ll px = p[x];       c[px] = 1-y;       S.erase(px);              for(auto it = S.upper_bound(px); it != S.end();){        rightQ.push(P(*it, y));        auto tmp = it;        it++;        S.erase(tmp);       }      }      while(sz(rightQ)){       cnt++;       ll x = rightQ.top().first, y = rightQ.top().second; rightQ.pop();       //outl(x, y);              c[x] = y;  if(y != f[x]) fcnt++;       ll px = p[x];       c[px] = 1-y;       S.erase(px);              for(auto it = S.begin(); it != S.end() && *it < px;){        leftQ.push(P(*it, y));        auto tmp = it;        it++;        S.erase(tmp);       }      }     }          ans += min(fcnt, cnt-fcnt);    }        vector<ll> vec;    rep(i, 1, 2*n) if(c[i]) vec.push_back(i);    sort(all(vec));        rep(i, 1, sz(vec)-1) if(p[vec[i-1]] < p[vec[i]]){     outl(-1);     return 0;    }    outl(ans);        return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[112];     int i;     for (i = 0; i < 4; i++) cin >> a[i];     sort(a, a + 4);     int tr = 0, seg = 0, im = 0;     if (a[1] + a[2] > a[3]) {       tr++;     } else if (a[0] + a[1] > a[3]) {       tr++;     } else if (a[0] + a[2] > a[3]) {       tr++;     } else if (a[0] + a[1] > a[2]) {       tr++;     } else if (a[0] + a[1] == a[2])       seg++;     else if (a[0] + a[2] == a[3])       seg++;     else if (a[1] + a[2] == a[3])       seg++;     else if (a[0] + a[1] == a[3])       seg++;     else       im++;     if (tr > 0)       cout <<  TRIANGLE  << endl;     else if (seg > 0)       cout <<  SEGMENT  << endl;     else if (im > 0)       cout <<  IMPOSSIBLE  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};   const double EPS = 1e-8;   const double PI = acos(-1.0);   int popcount(int n) { return __builtin_popcount(n); }   int popcount(long long n) { return __builtin_popcountll(n); }   template <class T>   int SIZE(T a) {     return a.size();   }   template <class T>   string IntToString(T num) {     string res;     stringstream ss;     ss << num;     return ss.str();   }   template <class T>   T StringToInt(string str) {     T res = 0;     for (int i = 0; i < SIZE(str); i++) res = (res * 10 + str[i] -  0 );     return res;   }   template <class T>   T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <class T>   void PrintSeq(T &a, int sz) {     for (int i = 0; i < sz; i++) {       cout << a[i];       if (sz == i + 1)         cout << endl;       else         cout <<    ;     }   }   long long getTen(int a) { return (a <= 0) ? 1 : (getTen(a - 1) * 10); }   bool EQ(double a, double b) { return abs(a - b) < EPS; }   void fastStream() {     cin.tie(0);     std::ios_base::sync_with_stdio(0);   }   vector<string> split(string str, char del) {     vector<string> res;     for (int i = 0, s = 0; i < SIZE(str); i++) {       if (str[i] == del) {         if (i - s != 0) res.push_back(str.substr(s, i - s));         s = i + 1;       } else if (i == SIZE(str) - 1) {         res.push_back(str.substr(s));       }     }     return res;   }   int N;   int as[500001];   int valToPos[500010];   int posToVal[500010];   const int MAX_N = 5000100;   class BIT {    private:     long long bit[MAX_N];     int n;       public:     BIT(int sz) {       n = sz;       for (int i = 0; i <= n; i++) bit[i] = 0;     }     long long sum(int i) {       long long s = 0;       while (i > 0) {         s += bit[i];         i -= i & -i;       }       return s;     }     long long rangeSum(int from, int to) {       long long res = 0;       res = sum(to) - sum(from - 1);       return res;     }     void add(int i, long long x) {       while (i <= n) {         bit[i] += x;         i += i & -i;       }     }     void insert(int i, long long x) { add(i, -(sum(i) - sum(i - 1)) + x); }   };   BIT bit(1000000);   int main() {     cin >> N;     for (int i = 0; i < N; i++) {       cin >> as[i];       posToVal[i] = as[i];       valToPos[as[i]] = i;     }     for (int i = 0; i < N; i++) {       if (as[i] == 1)         bit.add(1, 0);       else {         int pos1 = i;         int pos2 = valToPos[as[i] - 1];         if (pos2 > pos1) {           bit.add(as[i], 1);         }       }     }     int Q;     cin >> Q;     for (int i = 0; i < Q; i++) {       int p, x, y;       cin >> p >> x >> y;       if (p == 1) {         cout << bit.rangeSum(x, y) + 1 - bit.rangeSum(x, x) << endl;       } else {         int posx = x - 1;         int posy = y - 1;         x = posToVal[posx];         y = posToVal[posy];         valToPos[x] = posy;         valToPos[y] = posx;         posToVal[posx] = y;         posToVal[posy] = x;         int ary[6] = {x, x - 1, x + 1, y, y - 1, y + 1};         for (int j = 0; j < 6; j++) {           int num = ary[j];           if (num == 0)             continue;           else if (num == 1)             continue;           else {             int pos1 = valToPos[num];             int pos2 = valToPos[num - 1];             if (pos2 > pos1)               bit.insert(num, 1);             else               bit.insert(num, 0);           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);   }   void online_judge() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   const int flag_max = 0x3f3f3f3f;   const long long OO = 1e9;   const double EPS = (1e-7);   int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }   long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }   int n, p;   int F = 0;   map<pair<int, int>, bool> check;   vector<pair<int, int>> pa_ir;   void dfs(int star) {     for (int i = star + 1; i <= n; i++) {       if (!check[{star, i}]) {         F++;         check[{star, i}] = 1;         check[{i, star}] = 1;         pa_ir.push_back({star, i});         dfs(i);       }     }   }   int main() {     fast();     int t;     cin >> t;     while (t--) {       cin >> n >> p;       p = 2 * n + p;       pa_ir.clear();       check.clear();       dfs(1);       sort(((pa_ir).begin()), ((pa_ir).end()));       for (int i = 0; i < p; i++) {         cout << pa_ir[i].first <<     << pa_ir[i].second <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std; namespace {       using T = long long;       constexpr char endl =   n ;       const int maxN = 205;       const int maxM = 1e5 + 5;       const T base = 100000;          T Ceil(T x, T y) {           return (x + y - 1) / y;       }          int n, m;       T t[maxN], x[maxN], y[maxN];       bool f[maxM];          int ans[maxM];          void Reach(int i, int x) {           if(!f[x])               ans[x] = i, f[x] = true;       }          void Solve() {           cin >> n >> m;           for(int i = 1; i <= n; i++)               cin >> t[i] >> x[i] >> y[i];           for(int i = 1; i <= m; i++)               ans[i] = -1;           f[0] = true;           for(int i = 1; i <= n; i++)                for(int j = m; j >= 0; j--) if(f[j]) {                   T now = j;                   for(int k = 1; k <= y[i]; k++) {                       if(t[i] == 1)   now += Ceil(x[i], base);                       else            now = Ceil(now * x[i], base);                       if(now > m || f[now])                           break;                       Reach(i, now);                   }               }           for(int i = 1; i <= m; i++)               cout << ans[i] <<    n [i == m];       }   }   int main() {       cin.tie(nullptr)->sync_with_stdio(false);       int t(1); //cin >> t;       for(int i = 1; i <= t; i++) Solve();       cout << flush;       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, maxi[((int)101 * 1000) / ((int)350) + 1000], fnsh, id[((int)101 * 1000)],       ex[((int)101 * 1000)];   vector<int> v[((int)101 * 1000) / ((int)350) + 1000];   void reset() {     int num = 0;     for (int i = 0; i < fnsh; v[i].clear(), v[i].resize(0), i++)       for (auto u : v[i]) ex[num++] = u;     fnsh = 0;     maxi[0] = 0;     for (int i = 0; i < num; i++) {       v[fnsh].push_back(ex[i]);       maxi[fnsh] = max(maxi[fnsh], ex[i]);       if (i % ((int)350) == ((int)350) - 1 || i == num - 1)         fnsh++, maxi[fnsh] = 0;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       int a, c;       cin >> a >> c;       id[a] = i + 1;       bool flg = 0;       for (int j = 0, num = 0; j < fnsh && !flg; num += (int)v[j].size(), j++)         if (num + (int)v[j].size() > c || maxi[j] > a) {           flg = 1;           for (int k = 0; k < v[j].size(); k++) {             if (v[j][k] > a) {               v[j].insert(v[j].begin() + k, a);               break;             }             if (k + num == c) {               v[j].insert(v[j].begin() + k, a);               maxi[j] = max(maxi[j], a);               break;             }           }         }       if (!flg) {         v[fnsh].push_back(a);         maxi[fnsh] = a;         fnsh++;       }       if (i % ((int)350) == ((int)350) - 1) reset();     }     for (int i = fnsh - 1; i >= 0; i--)       for (int j = (int)v[i].size() - 1; j >= 0; j--) cout << id[v[i][j]] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int _Int() {     int x;     scanf( %d , &x);     return x;   }   long long int bigMod(long long int A, long long int P, int M) {     long long int R = 1;     for (A %= M; P; P >>= 1) {       if (P & 1) R = (R * A) % M;       A = (A * A) % M;     }     return R;   }   long long int bigMul(long long int A, long long int B, long long int M) {     long long int R = 0;     for (A %= M; B; B >>= 1) {       if (B & 1) R = (R + A) % M;       A = (A + A) % M;     }     return R;   }   long long int negMod(long long int A, long long int B) {     return (((A % B) + B) % B);   }   unsigned long long int _pow(unsigned long long int A, int P) {     unsigned long long int R = 1;     for (; P; P >>= 1) {       if (P & 1) R = (R * A);       A = (A * A);     }     return R;   }   template <class T>   T GCD(T x, T y) {     while (x) x ^= y ^= x ^= y %= x;     return y;   }   bool isVowel(char c) {     if (c ==  A ) return 1;     if (c ==  E ) return 1;     if (c ==  I ) return 1;     if (c ==  O ) return 1;     if (c ==  U ) return 1;     if (c ==  Y ) return 1;     return 0;   }   char s[100 + 7];   void Main() {     scanf( %s , s + 1);     int left = 0;     int n = strlen(s + 1), mx = 1;     for (int i = 1; i <= n; i++) {       if (isVowel(s[i])) {         mx = max(mx, i - left);         left = i;       }     }     mx = max(mx, n + 1 - left);     printf( %d n , mx);   }   int main() {     Main();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(char c) { return      + string(1, c) +     ; }   string to_string(const char *s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) res +=  ,  ;       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};   string to_str(const int &n) {     ostringstream stm;     stm << n;     return stm.str();   }   string to_str(const long long &n) {     ostringstream stm;     stm << n;     return stm.str();   }   template <class T>   string tostring(T x, int len = 0) {     stringstream ss;     ss << x;     string r = ss.str();     if (int((r).size()) < len) r = string(len - int((r).size()),  0 ) + r;     return r;   }   template <class T>   void convert(string x, T &r) {     stringstream ss(x);     ss >> r;   }   long long powmod(long long a, long long b) {     long long res = 1;     a %= 1000000007;     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   long long modinv(long long a) { return powmod(a, 1000000007 - 2); }   long long modinv(long long a, long long m) { return powmod(a, m - 2); }   long long nCkMp(long long n, long long k) {     long long numerator = 1, denominator = 1;     for (int i = 0; i < k; i++) numerator = (numerator * (n - i)) % 1000000007;     for (int i = 1; i <= k; i++) denominator = (denominator * i) % 1000000007;     return (numerator * modinv(denominator)) % 1000000007;   }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   int TT = 1, tt;   clock_t t1;   void solve() {     t1 = clock();     int n;     long long a;     long long b;     (cin >> (n));     vector<int> x(n);     for (auto i = (0); i < (n); i++) (cin >> (x[i]));     sort((x).begin(), (x).end());     b = x[n - 1];     a = accumulate((x).begin(), (x).end(), 0ll) - b;     (cout << (abs(a - b) + 1) << endl);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     for (tt = 0; tt < TT; tt++) solve();     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     map<long long, vector<long long> > m;     map<long long, bool> v;     map<long long, long long> c;     vector<long long> b;     long long i, j, k, x, y, r;     for (i = 1; i <= n; i++) {       cin >> x >> y;       if (x == -1)         r = i;       else         m[x].push_back(i);       v[i] = false;       c[i] = y;     }     for (i = 1; i <= n; i++) {     }     queue<long long> q;     q.push(r);     long long f = 1;     while (!q.empty()) {       j = q.front();       if (c[j] == 1) f = 0;       for (i = 0; i < m[j].size(); i++) {         q.push(m[j][i]);         if (c[m[j][i]] != 1 and !f) {           f = 1;         }       }       if (f == 0) {         b.push_back(j);       }       f = 1;       q.pop();     }     if (b.size() == 0)       cout << -1;     else {       sort(b.begin(), b.begin() + b.size());       for (i = 0; i < b.size(); i++) {         cout << b[i] <<    ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double eps = 1e-8;   const double PI = acos(-1.0);   const int maxn = 370360;   int dp[maxn], k, vis[maxn], sp[maxn], head, tail;   long long q[maxn];   int gcd(int x, int y) {     int tp = x % y;     while (tp) {       x = y;       y = tp;       tp = x % y;     }     return y;   }   int dfs(int x) {     int tp = INF, i;     if (dp[x] != -1) return dp[x];     for (i = 2; i <= k; i++)       if (x % i != 0) tp = min(dfs(x - x % i), tp);     tp = min(dfs(x - 1), tp);     return dp[x] = tp + 1;   }   int bfs(int st, int ed) {     int i;     memset(vis, 0, sizeof vis);     head = tail = 0;     vis[st] = 1;     q[tail] = st;     sp[tail++] = 0;     while (head < tail) {       if (q[head] == ed) return sp[head];       for (i = 2; i <= k; i++) {         if (!vis[q[head] - q[head] % i]) {           vis[q[head] - q[head] % i] = 1;           q[tail] = q[head] - q[head] % i;           sp[tail++] = sp[head] + 1;         }       }       if (!vis[q[head] - 1]) {         vis[q[head] - 1] = 1;         q[tail] = q[head] - 1;         sp[tail++] = sp[head] + 1;       }       head++;     }     return -1;   }   int main() {     int i, mc;     long long a, b, df, ans, rep;     while (~scanf( %I64d%I64d%d , &a, &b, &k)) {       mc = 1, ans = 0;       for (i = 2; i <= k; i++) mc = (mc * i) / gcd(mc, i);       for (i = 1; i < mc; i++) dp[i] = -1;       dp[0] = 0;       for (i = 1; i < mc; i++) dfs(i);       if (a - a % mc > b) ans += dp[a % mc], a -= a % mc;       df = a - b;       rep = df / mc;       a = a - rep * mc;       ans += rep * (1 + dp[mc - 1]);       df = a - b;       if (df > 0) {         if (a % mc == 0)           ans += bfs((a - 1) % mc, b % mc) + 1;         else           ans += bfs(a % mc, b % mc);       }       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int size = 1e5 + 10;   int afx[size];   int dis[size];   int main() {     int n, k;     scanf( %d%d , &n, &k);     int total = 0;     int ans = 0;     for (int i = 0; i < n; i++) {       scanf( %d , afx + i);       dis[i] = 10 - afx[i] % 10;       total += dis[i];       ans += afx[i] / 10;     }     if (k <= total) {       sort(dis, dis + n);       int i = 0;       while (k && i < n) {         k -= dis[i];         if (k < 0) break;         ans++;         i++;       }       printf( %d n , min(10 * n, ans));       return 0;     }     k -= total;     ans += n;     ans += k / 10;     printf( %d n , min(10 * n, ans));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d[100];   int main() {     int n, a, b, c = 0;     cin >> n;     for (int i = 1; i < n; i++) {       cin >> d[i];     }     cin >> a >> b;     for (int i = a; i < b; i++) {       c += d[i];     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using Int = long long;   template <class T1, class T2>   ostream &operator<<(ostream &os, const pair<T1, T2> &a) {     return os <<  (  << a.first <<  ,   << a.second <<  ) ;   };   template <class T>   void pv(T a, T b) {     for (T i = a; i != b; ++i) cerr << *i <<    ;     cerr << endl;   }   template <class T>   bool chmin(T &t, const T &f) {     if (t > f) {       t = f;       return true;     }     return false;   }   template <class T>   bool chmax(T &t, const T &f) {     if (t < f) {       t = f;       return true;     }     return false;   }   template <class T, class S, class OpTT, class OpST, class OpSS>   struct SegmentTree {     const OpTT opTT;     const OpST opST;     const OpSS opSS;     const T idT;     const S idS;     int n;     vector<T> ts;     vector<S> ss;     SegmentTree(int n_, const OpTT opTT, const OpST opST, const OpSS opSS,                 const T &idT, const S &idS)         : opTT(opTT), opST(opST), opSS(opSS), idT(idT), idS(idS) {       for (n = 1; n < n_; n <<= 1) {       }       ts.assign(n << 1, idT);       ss.assign(n << 1, idS);     }     T &at(int a) { return ts[n + a]; }     void build() {       for (int a = n; --a;) ts[a] = opTT(ts[a << 1], ts[a << 1 | 1]);     }     T query(int a, int b, const S &s) { return query(1, 0, n, a, b, s); }       private:     T query(int u, int l, int r, int a, int b, const S &s) {       if (a < l) a = l;       if (b > r) b = r;       if (a >= b) return idT;       if (a == l && b == r) {         ts[u] = opST(s, ts[u], r - l);         ss[u] = opSS(s, ss[u]);         return ts[u];       }       const int uL = u << 1, uR = u << 1 | 1;       const int mid = (l + r) >> 1;       {         ts[uL] = opST(ss[u], ts[uL], mid - l);         ts[uR] = opST(ss[u], ts[uR], r - mid);         ss[uL] = opSS(ss[u], ss[uL]);         ss[uR] = opSS(ss[u], ss[uR]);         ss[u] = idS;       }       const T resL = query(uL, l, mid, a, b, s);       const T resR = query(uR, mid, r, a, b, s);       ts[u] = opTT(ts[uL], ts[uR]);       return opTT(resL, resR);     }   };   constexpr int MAX = 50005;   int N, M, Q, C;   int F[MAX], P[MAX];   int D[MAX];   int T[MAX], X[MAX], K[MAX];   double anss[MAX];   int f[MAX];   int queriesLen;   pair<pair<int, int>, pair<int, int>> queries[4 * MAX];   Int vals[MAX];   void brute() {     fill(vals, vals + N, 0);     Int s1 = 0, s2 = 0;     for (int h = 0; h < queriesLen; ++h) {       switch (queries[h].second.second) {         case +1: {           const int u = queries[h].second.first;           for (int v = u; v != -1; v = P[v]) {             ++s1;             s2 += vals[v];           }           for (int v = u; v != -1; v = P[v]) {             ++vals[v];           }         } break;         case -1: {           const int u = queries[h].second.first;           for (int v = u; v != -1; v = P[v]) {             --vals[v];           }           for (int v = u; v != -1; v = P[v]) {             --s1;             s2 -= vals[v];           }         } break;         case 0: {           const int k = queries[h].first.first;           double ans = 0.0;           ans += 1.0 * C * C * N;           ans -= 2.0 * C * D[k] * s1;           ans += 1.0 * D[k] * D[k] * (s1 + 2.0 * s2);           ans /= N;           anss[queries[h].second.first] = ans;         } break;         default:           assert(false);       }     }     for (int q = 0; q < Q; ++q) {       if (T[q] == 2) {         printf( %.10f n , anss[q]);       }     }   }   vector<int> G[MAX];   int par[MAX], sz[MAX], dep[MAX];   int O, ord[MAX], head[MAX];   void dfs0(int u, int p) {     par[u] = p;     sz[u] = 1;     dep[u] = (p == -1) ? 0 : (dep[p] + 1);     for (const int v : G[u]) {       if (v != p) {         dfs0(v, u);         sz[u] += sz[v];       }     }   }   void dfs1(int u, int p, int h) {     ord[u] = O++;     head[u] = h;     int vm = -1;     for (const int v : G[u]) {       if (v != p) {         if (vm == -1 || sz[vm] < sz[v]) {           vm = v;         }       }     }     if (vm != -1) {       dfs1(vm, u, h);       for (const int v : G[u]) {         if (v != p && v != vm) {           dfs1(v, u, v);         }       }     }   }   void solve() {     for (int u = 0; u < N; ++u) {       G[u].clear();     }     for (int u = 1; u < N; ++u) {       G[P[u]].push_back(u);     }     const int rt = 0;     dfs0(rt, -1);     O = 0;     dfs1(rt, -1, 0);     auto opTT = [](Int t0, Int t1) { return t0 + t1; };     auto opST = [](Int s, Int t, Int sz) { return t + s * sz; };     auto opSS = [](Int s0, Int s1) { return s0 + s1; };     SegmentTree<Int, Int, decltype(opTT), decltype(opST), decltype(opSS)> seg(         N, opTT, opST, opSS, 0, 0);     auto query = [&](int u, int val) {       Int ret = 0;       for (; u != -1;) {         const Int h = head[u];         ret += seg.query(ord[h], ord[u] + 1, val);         u = par[h];       }       return ret;     };     Int s1 = 0, s2 = 0;     for (int h = 0; h < queriesLen; ++h) {       switch (queries[h].second.second) {         case +1: {           const int u = queries[h].second.first;           s1 += (dep[u] + 1);           s2 += query(u, 0);           query(u, +1);         } break;         case -1: {           const int u = queries[h].second.first;           s1 -= (dep[u] + 1);           s2 -= query(u, -1);         } break;         case 0: {           const int k = queries[h].first.first;           double ans = 0.0;           ans += 1.0 * C * C * N;           ans -= 2.0 * C * D[k] * s1;           ans += 1.0 * D[k] * D[k] * (s1 + 2.0 * s2);           ans /= N;           anss[queries[h].second.first] = ans;         } break;         default:           assert(false);       }     }     for (int q = 0; q < Q; ++q) {       if (T[q] == 2) {         printf( %.10f n , anss[q]);       }     }   }   int main() {     for (; ~scanf( %d%d%d%d , &N, &M, &Q, &C);) {       for (int u = 0; u < N; ++u) {         scanf( %d , &F[u]);         --F[u];       }       P[0] = -1;       for (int u = 1; u < N; ++u) {         scanf( %d , &P[u]);         --P[u];       }       for (int k = 0; k < M; ++k) {         scanf( %d , &D[k]);       }       for (int q = 0; q < Q; ++q) {         scanf( %d , &T[q]);         if (T[q] == 1) {           scanf( %d , &X[q]);           --X[q];         }         scanf( %d , &K[q]);         --K[q];       }       copy(F, F + N, f);       queriesLen = 0;       for (int u = 0; u < N; ++u) {         queries[queriesLen++] = {{f[u], -1}, {u, +1}};       }       for (int q = 0; q < Q; ++q) {         if (T[q] == 1) {           const int u = X[q];           queries[queriesLen++] = {{f[u], q}, {u, -1}};           f[u] = K[q];           queries[queriesLen++] = {{f[u], q}, {u, +1}};         } else {           queries[queriesLen++] = {{K[q], q}, {q, 0}};         }       }       for (int u = 0; u < N; ++u) {         queries[queriesLen++] = {{f[u], Q}, {u, -1}};       }       sort(queries, queries + queriesLen);       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005, maxd = 15;   int f[maxn], g[maxn][maxd][maxn], rfac[maxn], n, d, mo;   int pow(int a, int b) {     int tmp = 1;     for (; b; b >>= 1, a = a * 1ll * a % mo)       if (b & 1) tmp = tmp * 1ll * a % mo;     return tmp;   }   int main() {     cin >> n >> d >> mo;     if (n == 1) {       cout << 1 << endl;       return 0;     }     rfac[0] = 1;     for (int i = 1; i <= 1000; i++)       rfac[i] = rfac[i - 1] * 1ll * pow(i, mo - 2) % mo;     for (int i = 0; i <= n; i++) g[0][0][i] = 1;     for (int i = 1; i <= n; i++) {       if (i != 1)         f[i] = g[i - 1][d - 1][n];       else         f[i] = 1;       for (int dd = 1; dd <= d; dd++) {         for (int x = 1; x <= i; x++) {           int v = f[x];           for (int k = 1, cv = 1; k <= dd && x * k <= i; k++) {             cv = cv * 1ll * (v + k - 1) % mo;             g[i][dd][x] = (g[i][dd][x] + g[i - x * k][dd - k][x - 1] * 1ll * cv %                                              mo * rfac[k] % mo) %                           mo;           }         }         for (int x = 1; x <= n; x++)           g[i][dd][x] = (g[i][dd][x] + g[i][dd][x - 1]) % mo;       }     }     int ans = g[n - 1][d][(n - 1) / 2];     if (!(n & 1)) {       int v = f[n / 2];       ans = (ans + v * 1ll * (v + 1) % mo * rfac[2] % mo) % mo;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1000 * 100 + 1;   const long long INF = 1e12;   const int MOD = 1000 * 1000 * 1000 + 7;   const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};   const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};   const int MAXN = 2 * 1000 * 100 + 1;   struct edge {     int to, w;   };   int n, m;   vector<edge> g[MAXN];   long long dist[MAXN];   int par[MAXN];   set<pair<long long, int> > s;   vector<int> ans;   edge e;   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       int a, b, w;       scanf( %d %d %d , &a, &b, &w);       e.to = b;       e.w = w;       g[a].push_back(e);       e.to = a;       g[b].push_back(e);     }     s.insert(make_pair(0, 1));     for (int i = 2; i <= n; i++) {       dist[i] = INF;       s.insert(make_pair(INF, i));     }     while (!s.empty()) {       int cur = s.begin()->second;       long long cur_dist = s.begin()->first;       s.erase(s.begin());       for (int i = 0; i < (int)g[cur].size(); i++) {         int to = g[cur][i].to, w = g[cur][i].w;         if (cur_dist + w < dist[to]) {           s.erase(make_pair(dist[to], to));           dist[to] = cur_dist + w;           par[to] = cur;           s.insert(make_pair(dist[to], to));         }       }     }     if (dist[n] == INF) {       printf( -1 );       return 0;     }     int cur = n;     while (par[cur] != 0) {       ans.push_back(cur);       cur = par[cur];     }     ans.push_back(1);     reverse(ans.begin(), ans.end());     for (int i = 0; i < (int)ans.size(); i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int T, n, m, u, v, lab[300001];   vector<int> g[300001];   long long cnt[3];   long long const mod = 998244353;   bool impossible;   void DFS(const int& u) {     for (const int& v : g[u])       if (!lab[v]) {         lab[v] = 3 - lab[u];         cnt[lab[v]]++;         DFS(v);         if (impossible) return;       } else if (lab[v] == lab[u]) {         impossible = true;         return;       }   }   long long Pow(const long long& k) {     if (k == 0) return 1;     long long tmp = Pow(k / 2);     if (k % 2 == 0)       return tmp * tmp % mod;     else       return 2 * tmp * tmp % mod;   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &m);       for (int i = 1; i <= n; i++) {         g[i].clear();         lab[i] = 0;       }       while (m--) {         scanf( %d%d , &u, &v);         g[u].push_back(v);         g[v].push_back(u);       }       long long res = 1;       impossible = false;       for (int i = 1; i <= n && !impossible; i++)         if (!lab[i]) {           lab[i] = 1;           cnt[1] = 1;           cnt[2] = 0;           DFS(i);           res = res * ((Pow(cnt[1]) + Pow(cnt[2])) % mod) % mod;         }       if (impossible)         cout << 0 <<   n ;       else         cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   template <typename T, typename U>   inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   inline void amax(T &x, U y) {     if (x < y) x = y;   }   template <typename T>   inline void write(T x) {     int i = 20;     char buf[21];     buf[20] =   n ;     do {       buf[--i] = x % 10 +  0 ;       x /= 10;     } while (x);     do putchar(buf[i]);     while (buf[i++] !=   n );   }   template <typename T>   inline T readInt() {     T n = 0, s = 1;     char p = getchar();     if (p ==  - ) s = -1;     while ((p <  0  || p >  9 ) && p != EOF && p !=  - ) p = getchar();     if (p ==  - ) s = -1, p = getchar();     while (p >=  0  && p <=  9 ) {       n = (n << 3) + (n << 1) + (p -  0 );       p = getchar();     }     return n * s;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, k, *h, smin, c;     n = readInt<long long int>();     h = (long long int *)malloc(n * sizeof(long long int));     k = readInt<long long int>();     for (int i = 0; i < n; i += 1) h[i] = readInt<long long int>();     sort(h, h + n);     reverse(h, h + n);     if (h[0] == h[n - 1]) {       write(0);       return 0;     }     smin = 1;     c = 0;     for (int i = 0; i < n - 1; i += 1) {       for (int j = h[i]; j >= h[i + 1] + 1; j -= 1) {         if (c + i + 1 > k) {           smin++;           c = i + 1;         } else           c += i + 1;       }     }     write(smin);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int smallest, largest, n, count = 0, a;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a;       if (i == 0) {         smallest = a;         largest = a;       } else {         if (a > largest) {           count += 1;           largest = a;         } else if (a < smallest) {           count += 1;           smallest = a;         }       }     }     cout << count;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b;   string s, g;   map<string, pair<int, int> > m;   int main() {     m[ goat ] = make_pair(1, 1);     m[ asiago ] = make_pair(1, 2);     m[ camembert ] = make_pair(1, 3);     m[ gouda ] = make_pair(2, 1);     m[ feta ] = make_pair(2, 2);     m[ swiss ] = make_pair(2, 3);     m[ muenster ] = make_pair(3, 1);     m[ cheddar ] = make_pair(3, 2);     m[ brie ] = make_pair(3, 3);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> s >> g;       if (g ==  soft ) {         a++;       } else {         b++;       }     }     int res = 1;     while ((res * res) / 2 + (res * res) % 2 < max(a, b) || res * res < a + b) {       res++;     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   int n, m, p;   const double PI = acos(-1);   vector<int> a, b;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n >> m >> p;     for (int i = (int)1; i <= (int)n; i++) {       int x;       cin >> x;       a.push_back(x);     }     for (int i = (int)1; i <= (int)m; i++) {       int x;       cin >> x;       b.push_back(x);     }     int i = 0, j = 0;     while (i < n) {       if (a[i] % p != 0) break;       i++;     }     while (j < m) {       if (b[j] % p != 0) break;       j++;     }     cout << i + j << endl;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:400000000 )   int n, a[200010], b[200010], f[200010];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       int x;       cin >> x;       f[x - 1] = i;     }     for (int i = 0; i < n; i++) b[i] = f[a[i] - 1];     for (int i = 1; i < n; i++) {       if (b[i] < b[i - 1]) {         cout << n - i;         return 0;       }     }     cout << 0;   }


#include <bits/stdc++.h>   using namespace std;   int memo[77][107][107];   inline int index(char ch) { return (ch ==  @ ) ? 0 : ch -  a ; }   bool eval(char ch, int i, int j, vector<vector<pair<int, char>>> &adj) {     int val = memo[index(ch)][i][j];     if (val == 1) return true;     if (val == 2) return false;     for (auto nxt : adj[i]) {       if (ch <= nxt.second) {         if (!eval(nxt.second, j, nxt.first, adj)) {           memo[index(ch)][i][j] = 1;           return true;         }       }     }     memo[index(ch)][i][j] = 2;     return false;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<vector<pair<int, char>>> adj(n + 1);     for (int i = 0; i < m; i++) {       int u, v;       char c;       cin >> u >> v >> c;       adj[u].push_back({v, c});     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) cout << (eval( @ , i, j, adj) ?  A  :  B );       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 126, MOD = 12345;   int res[maxn][maxn], tmp[maxn][maxn], POW[maxn][maxn], flag[maxn], check[maxn];   int A[maxn][maxn], B[maxn][maxn], a[maxn << 3], b[maxn << 3];   int m, size = 1, temp, ans, vec[maxn << 3], judge;   char ch;   long long n;   void mul(int a[maxn][maxn], int b[maxn][maxn], int c[maxn][maxn]) {     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) {         tmp[i][j] = 0;         for (int k = 1; k <= size; k++)           tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]) % MOD;       }     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) c[i][j] = tmp[i][j];   }   void power(int a[maxn][maxn], long long p, int b[maxn][maxn]) {     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) POW[i][j] = a[i][j], res[i][j] = (i == j);     while (p) {       if (p & 1) mul(res, POW, res);       mul(POW, POW, POW);       p >>= 1;     }     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) b[i][j] = res[i][j];   }   int main() {     scanf( %lld%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf(  n%c %d , &ch, &b[i]);       a[i] = ch -  A ;       flag[a[i]] = 1;       size *= b[i];     }     for (int i = 0; i <= size - 1; i++) {       temp = i;       for (int j = m; j >= 1; j--) vec[j] = temp % b[j], temp /= b[j];       for (int j = 0; j <= 25; j++)         if (flag[j]) {           temp = 0;           for (int k = 1; k <= m; k++)             temp = temp * b[k] + (vec[k] + (a[k] == j)) % b[k];           A[temp + 1][i + 1]++;         }     }     power(A, n, A);     B[1][1] = 1;     mul(A, B, B);     for (int i = 0; i <= size - 1; i++) {       temp = i;       for (int j = m; j >= 1; j--) check[a[j]] |= !(temp % b[j]), temp /= b[j];       judge = 1;       for (int j = 0; j <= 25; j++) {         judge &= !(flag[j] ^ check[j]);         check[j] = 0;       }       if (judge) ans = (ans + B[i + 1][1]) % MOD;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[100005];   char b[100005];   int main() {     cin >> s;     int cnt = 0;     char x =  a ;     for (int i = strlen(s); i >= 0; --i) {       if (s[i] >= x) {         x = s[i];         b[cnt++] = x;       }     }     while (cnt--) {       printf( %c , b[cnt]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<string> nap(n);     for (int i = 0; i < n; ++i) cin >> nap[i];     vector<vector<int> > dp(26);     for (int i = 0; i < 26; ++i)       for (int j = 0; j < 26; ++j) dp[i].push_back(0);     for (int i = 0; i < n; ++i) {       int dl = nap[i].size();       for (char c =  a ; c <=  z ; ++c) {         if (dp[c -  a ][nap[i][0] -  a ] != 0) {           dp[c -  a ][nap[i][dl - 1] -  a ] =               max(dp[c -  a ][nap[i][dl - 1] -  a ],                   dp[c -  a ][nap[i][0] -  a ] + dl);         }       }       dp[nap[i][0] -  a ][nap[i][dl - 1] -  a ] =           max(dp[nap[i][0] -  a ][nap[i][dl - 1] -  a ], dl);     }     int odp = 0;     for (int i = 0; i < 26; ++i) odp = max(odp, dp[i][i]);     cout << odp;     return 0;   }


#include <bits/stdc++.h>   using ld = long double;   using ul = std::size_t;   template <typename T, ul C>   using ar = std::array<T, C>;   const int MN = 55;   const ld INF = 1e10;   const ld EPS = 1e-9;   template <ul C>   ar<ld, C>& operator*=(ar<ld, C>& a, const ld& v) {     for (ul i = 0; i < C; ++i) a[i] *= v;     return a;   }   template <ul C>   ar<ld, C> operator*(ar<ld, C> a, const ld& v) {     return a *= v;   }   template <ul C>   ar<ld, C>& operator+=(ar<ld, C>& a, const ar<ld, C>& b) {     for (ul i = 0; i < C; ++i) a[i] += b[i];     return a;   }   template <ul C>   ar<ld, C> operator+(ar<ld, C> a, const ar<ld, C>& b) {     return a += b;   }   inline bool equ(const ld& a, const ld& b) { return std::abs(a - b) < EPS; }   struct vec {    public:     ld x, y;     vec(ld _x = 0, ld _y = 0) : x(_x), y(_y) {}     vec& operator+=(const vec& o) { return x += o.x, y += o.y, *this; }     vec& operator-=(const vec& o) { return x -= o.x, y -= o.y, *this; }     vec& operator*=(const ld& o) { return x *= o, y *= o, *this; }     friend vec operator+(vec a, const vec& b) { return a += b; }     friend vec operator-(vec a, const vec& b) { return a -= b; }     friend vec operator*(vec a, const ld& b) { return a *= b; }     ld mag() const { return hypot(x, y); }   };   struct range {    public:     ld l, r;   };   using ranges = std::vector<range>;   ranges unite(const ranges& a, const ranges& b) {     ranges f(0);     bool rdy = 0;     ld l = -INF - 1, r = -INF - 1;     auto add = [&]() {       if (rdy)         f.push_back({l, r});       else         rdy = 1;     };     range t;     for (int i = 0, j = 0; i < a.size() || j < b.size();) {       if (j < b.size() && (i == a.size() || b[j].l < a[i].l))         t = b[j++];       else         t = a[i++];       if (r + EPS < t.l) add(), l = t.l;       r = std::max(r, t.r);     }     add();     return f;   }   ranges isect(const ranges& a, const ranges& b) {     ranges f;     for (int i = 0, j = 0; i < a.size() && j < b.size();) {       ld l = std::max(a[i].l, b[j].l);       ld r = std::min(a[i].r, b[j].r);       if (l + EPS < r) f.push_back({l, r});       if (a[i].r < b[j].r)         ++i;       else         ++j;     }     return f;   }   int N;   vec p[MN];   ld pf[MN];   ar<ld, 6> sqr(const ar<ld, 3>& a) {     return {a[0] * a[0], 2 * a[0] * a[1], 2 * a[0] * a[2],             a[1] * a[1], a[2] * a[2],     2 * a[1] * a[2]};   }   ranges neg(const ar<ld, 3>& a) {     if (equ(a[2], 0)) {       if (equ(a[1], 0))         return a[0] < 0 ? ranges{{-INF, INF}} : ranges();       else         return a[1] < 0 ? ranges{{-a[0] / a[1], INF}}                         : ranges{{-INF, -a[0] / a[1]}};     } else if (a[2] < 0) {       ld d = a[1] * a[1] - 4 * a[0] * a[2];       if (d > EPS) {         d = sqrt(d) / (2 * -a[2]);         ld c = -a[1] / (2 * a[2]);         return ranges{{-INF, c - d}, {c + d, INF}};       }       return ranges{{-INF, INF}};     } else {       ld d = a[1] * a[1] - 4 * a[0] * a[2];       if (d > EPS) {         d = sqrt(d) / (2 * a[2]);         ld c = -a[1] / (2 * a[2]);         return ranges{{c - d, c + d}};       }       return ranges();     }   }   ranges eval(const ar<ld, 6>& a) {     ranges f;     f = neg({a[0], a[1], a[3]});     f = unite(f, neg({a[0] + a[2] + a[4], a[1] + a[5], a[3]}));     if (a[4] > EPS) {       if (equ(a[5], 0)) {         ld yv = a[2] / (-2 * a[4]);         if (0 < yv && yv < 1)           f = unite(f, neg({a[0] + a[2] * yv + a[4] * yv * yv, a[1], a[3]}));       } else {         range xr = {(-2 * a[4] - a[2]) / a[5], -a[2] / a[5]};         if (a[5] < 0) xr = {xr.r, xr.l};         f = unite(f, isect(ranges{xr}, neg({a[0] - a[2] * a[2] / (4 * a[4]),                                             a[1] - 2 * a[2] * a[5] / (4 * a[4]),                                             a[3] - a[5] * a[5] / (4 * a[4])})));       }     }     return isect(f, ranges{{0, 1}});   }   bool ok(const ld& v) {     ranges bot, top;     for (int i = 0; i < N; ++i)       for (int j = i + 1; j < N; ++j) {         ld mi = (p[i + 1] - p[i]).mag();         ld mj = (p[j + 1] - p[j]).mag();         ar<ld, 6> L = sqr({pf[j] - pf[i], -mi, mj});         ar<ld, 6> Dx =             sqr({p[j].x - p[i].x, -p[i + 1].x + p[i].x, p[j + 1].x - p[j].x});         ar<ld, 6> Dy =             sqr({p[j].y - p[i].y, -p[i + 1].y + p[i].y, p[j + 1].y - p[j].y});         L *= -1;         Dx *= v * v;         Dy *= v * v;         ar<ld, 6> fin = L + Dx + Dy;         ranges bot_ = eval(fin);         std::swap(fin[1], fin[2]);         std::swap(fin[3], fin[4]);         ranges top_ = eval(fin);         for (range& x : bot_) x.l = pf[i] + mi * x.l, x.r = pf[i] + mi * x.r;         for (range& x : top_) x.l = pf[j] + mj * x.l, x.r = pf[j] + mj * x.r;         bot = unite(bot, bot_);         top = unite(top, top_);       }     return isect(bot, top).empty();   }   int main() {     scanf( %d , &N);     for (int i = 0; i <= N; ++i) {       int u, v;       scanf( %d%d , &u, &v);       p[i] = {(ld)u, (ld)v};     }     for (int i = 0; i < N; ++i) pf[i + 1] = pf[i] + (p[i + 1] - p[i]).mag();     ld l = 1, r = 5000;     for (int i = 0; i < 100; ++i) {       ld m = l + (r - l) / 2;       if (ok(m))         r = m;       else         l = m;     }     printf( %.9lf n , (double)r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, dd, hh, mm, t;     int sep[16];     char buf[80];     vector<string> todo;     map<string, int> cost;     vector<pair<int, int> > time;     vector<vector<pair<int, bool> > > dp;     vector<pair<pair<int, int>, pair<int, int> > > job;     scanf( %d%d%d , &n, &m, &dd);     for (int i = 0; i < n; ++i) {       scanf( %s , buf);       todo.push_back(buf);     }     for (int i = 0; i < n; ++i) {       scanf( %d , &cost[todo[i]]);     }     for (int i = 0; i < 8; ++i) {       scanf( %*[^0-9]%d:%d , &hh, &mm);       sep[i] = hh * 60 + mm;     }     for (int i = 1, k = 0; i <= dd; ++i) {       for (int j = 0; j < 24 * 60; ++j) {         if (k % 2 == 0 && j == sep[k]) {           k = (k + 1) % 8;         }         if (k % 2 == 0) {           time.push_back(make_pair(i, j));         }         if (k % 2 != 0 && j == sep[k]) {           k = (k + 1) % 8;         }       }     }     for (int i = 0; i < m; ++i) {       scanf( %s%d%d:%d%d , buf, &dd, &hh, &mm, &t);       if (cost.count(buf) == 0) {         continue;       }       job.push_back(make_pair(make_pair(lower_bound(time.begin(), time.end(),                                                     make_pair(dd, hh * 60 + mm)) -                                             time.begin(),                                         i),                               make_pair(cost[buf], t)));     }     sort(job.begin(), job.end());     reverse(job.begin(), job.end());     n = time.size();     m = job.size();     dp = vector<vector<pair<int, bool> > >(1, vector<pair<int, bool> >(n + 1));     for (int i = 0; i < m; ++i) {       t = job[i].second.first;       dp.push_back(dp.back());       for (int j = 0; j <= n; ++j) {         dp[i + 1][j].second = false;       }       for (int j = t; j <= job[i].first.first; ++j) {         if (dp[i + 1][j - t].first < dp[i][j].first + job[i].second.second) {           dp[i + 1][j - t].first = dp[i][j].first + job[i].second.second;           dp[i + 1][j - t].second = true;         }       }     }     printf( %d n , dp[m][0].first);     todo.clear();     for (int i = m - 1, j = 0; i >= 0; --i) {       if (dp[i + 1][j].second) {         t = j + job[i].second.first - 1;         sprintf(buf,  %d %d %02d:%02d %d %02d:%02d , job[i].first.second + 1,                 time[j].first, time[j].second / 60, time[j].second % 60,                 time[t].first, time[t].second / 60, time[t].second % 60);         todo.push_back(buf);         j = t + 1;       }     }     printf( %d n , todo.size());     for (int i = 0; i < (int)todo.size(); ++i) {       puts(todo[i].c_str());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool r[100010], c[100010];   int main() {     ios_base::sync_with_stdio(false);     long long n, m, a, b, li, co;     cin >> n >> m;     li = co = 0;     long long ans;     for (int i = 0; i < m; i++) {       cin >> a >> b;       if (r[a] == false) {         r[a] = true;         li++;       }       if (c[b] == false) {         c[b] = true;         co++;       }       ans = n * n - co * n;       ans = ans - li * (n - co);       if (i > 0) cout <<    ;       cout << ans;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> Ts;   string size[101] = { S ,  M ,  L ,  XL ,  XXL };   inline int to_size(string s) {     for (int i = 0; i < 5; ++i) {       if (s == size[i]) return i;     }   }   int main() {     int a[11], k;     string pe[10010];     for (int i = 0; i < 5; ++i) cin >> a[i];     for (int i = 0; i < 5; ++i) {       Ts[size[i]] = a[i];     }     cin >> k;     for (int i = 0; i < k; ++i) cin >> pe[i];     for (int i = 0; i < k; ++i) {       int pe1 = to_size(pe[i]);       for (int j = 0; j <= 4; ++j) {         if (pe1 + j <= 4) {           if (Ts[size[pe1 + j]] > 0) {             Ts[size[pe1 + j]]--;             cout << size[pe1 + j] << endl;             break;           }         }         if (pe1 - j >= 0) {           if (Ts[size[pe1 - j]] > 0) {             Ts[size[pe1 - j]]--;             cout << size[pe1 - j] << endl;             break;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 5010;   vector<int> G[maxn];   int d[maxn][maxn][2];   void check(int &a, int b) {     if (a > b) a = b;   }   int dfs(int u, int fa) {     int num = G[u].size(), tot = 0, now = 0;     d[u][0][0] = 0;     d[u][0][1] = 0;     if (num == 1) {       d[u][0][1] = INF;       d[u][1][1] = 0;       return 1;     }     for (int i = 0; i < num; ++i) {       int v = G[u][i];       if (v == fa) continue;       now = dfs(v, u);       tot += now;       for (int j = tot; j >= 0; --j) {         int mn0, mn1;         mn0 = mn1 = INF;         for (int k = 0; k <= now; ++k) {           check(mn0, min(d[u][j - k][0] + d[v][k][0],                          d[u][j - k][0] + d[v][k][1] + 1));           check(mn1, min(d[u][j - k][1] + d[v][k][0] + 1,                          d[u][j - k][1] + d[v][k][1]));         }         d[u][j][0] = mn0;         d[u][j][1] = mn1;       }     }     return tot;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int u, v;       scanf( %d %d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     if (n == 2) {       puts( 1 );       return 0;     }     int cnt = 0, p = 1;     memset(d, INF, sizeof d);     for (int i = 1; i <= n; ++i) {       if (G[i].size() == 1)         ++cnt;       else         p = i;     }     dfs(p, 0);     int ans = min(d[p][cnt / 2][0], d[p][cnt / 2][1]);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   const int inf = (1ll << 30) - 1;   const int maxn = (int)1e5 + 10;   const int mod = (int)1e9 + 7;   using namespace std;   void solve() {     string second;     cin >> second;     sort(second.begin(), second.end());     int cnt = 0;     vector<string> v;     string x =   ;     x += second[0];     for (int i = 1; i < second.size(); i++) {       if (second[i] != second[i - 1]) {         v.push_back(x);         x =   ;       }       x += second[i];     }     v.push_back(x);     if (v.size() == 1) {       cout << second <<   n ;       return;     }     string A =   ;     if (v.size() > 3) {       A = v[2] + v[0] + v[3] + v[1];       for (int i = 4; i < v.size(); i++) {         if (i % 2 == 0)           A += v[i];         else           A = v[i] + A;       }       cout << A <<   n ;       return;     }     if (v.size() == 2) {       if (v[1][0] - v[0][0] == 1) {         cout <<  No answer n ;         return;       }       cout << second <<   n ;       return;     }     if (v[1][0] - v[0][0] == 1 && v[2][0] - v[1][0] == 1) {       cout <<  No answer n ;       return;     }     if (v[1][0] - v[0][0] != 1 && v[2][0] - v[1][0] != 1) {       cout << second <<   n ;       return;     }     if (v[1][0] - v[0][0] == 1)       cout << v[0] << v[2] << v[1] <<   n ;     else       cout << v[1] << v[0] << v[2] <<   n ;   }   int main() {     int t = 1;     scanf( %d , &t);     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct flow_graph {     int V, E, s, t;     int *flow, *low, *cap, *to, *next, *last, *delta;     int *dist, *q, *now, head, tail;     flow_graph() {}     flow_graph(int V, int E) {       (*this).V = V;       (*this).E = 0;       int TE = 2 * (E + V + 1);       flow = new int[TE];       low = new int[TE];       cap = new int[TE];       to = new int[TE];       next = new int[TE];       last = new int[V + 2];       delta = new int[V];       dist = new int[V + 2];       q = new int[V + 2];       now = new int[V + 2];     }     void clear(int V) {       (*this).V = V;       (*this).E = 0;       fill(last, last + V, -1);     }     void add_edge(int a, int b, int l, int u) {       to[E] = b;       low[E] = l;       cap[E] = u;       flow[E] = 0;       next[E] = last[a];       last[a] = E++;       to[E] = a;       low[E] = 0;       cap[E] = 0;       flow[E] = 0;       next[E] = last[b];       last[b] = E++;     }     bool bfs() {       fill(dist, dist + V + 2, -1);       head = tail = 0;       q[tail] = t;       ++tail;       dist[t] = 0;       while (head < tail) {         int v = q[head];         ++head;         for (int e = last[v]; e != -1; e = next[e]) {           if (cap[e ^ 1] > flow[e ^ 1] && dist[to[e]] == -1) {             q[tail] = to[e];             ++tail;             dist[to[e]] = dist[v] + 1;           }         }       }       return dist[s] != -1;     }     int dfs(int v, int f) {       if (v == t) return f;       for (int &e = now[v]; e != -1; e = next[e]) {         if (cap[e] > flow[e] && dist[to[e]] == dist[v] - 1) {           int ret = dfs(to[e], min(f, cap[e] - flow[e]));           if (ret > 0) {             flow[e] += ret;             flow[e ^ 1] -= ret;             return ret;           }         }       }       return 0;     }     int max_flow(int source, int sink) {       fill(delta, delta + V, 0);       for (int e = 0; e < E; e += 2) {         delta[to[e ^ 1]] -= low[e];         delta[to[e]] += low[e];         cap[e] -= low[e];       }       last[V] = last[V + 1] = -1;       int sum = 0;       for (int i = 0; i < V; ++i) {         if (delta[i] > 0) {           add_edge(V, i, 0, delta[i]);           sum += delta[i];         }         if (delta[i] < 0) add_edge(i, V + 1, 0, -delta[i]);       }       add_edge(sink, source, 0, INT_MAX);       s = V;       t = V + 1;       int f = 0, df;       fill(flow, flow + E, 0);       while (bfs()) {         for (int i = V + 1; i >= 0; --i) now[i] = last[i];         while (true) {           df = dfs(s, INT_MAX);           if (df == 0) break;           f += df;         }       }       if (f != sum) return -1;       for (int e = 0; e < E; e += 2) {         cap[e] += low[e];         flow[e] += low[e];         flow[e ^ 1] -= low[e];         cap[e ^ 1] -= low[e];       }       s = source;       t = sink;       last[s] = next[last[s]];       last[t] = next[last[t]];       E -= 2;       while (bfs()) {         for (int i = V - 1; i >= 0; --i) now[i] = last[i];         while (true) {           df = dfs(s, INT_MAX);           if (df == 0) break;           f += df;         }       }       return f;     }   };   const int N = 100005;   pair<int, int> arr[N];   int xs[N], ys[N];   int dx[N], dy[N];   int qntx[N], qnty[N];   int pnt_id[N];   char ans[N];   int main() {     int n, m;     scanf( %d %d , &n, &m);     int r, b;     scanf( %d %d , &r, &b);     char cheap =  r , expensive =  b ;     if (r > b) {       swap(cheap, expensive);       swap(r, b);     }     for (int i = 0; i < n; ++i) {       int x, y;       scanf( %d %d , &x, &y);       arr[i] = pair<int, int>(x, y);       xs[i] = x;       ys[i] = y;     }     sort(xs, xs + n);     int xssz = unique(xs, xs + n) - xs;     sort(ys, ys + n);     int yssz = unique(ys, ys + n) - ys;     for (int i = 0; i < n; ++i) {       arr[i].first = lower_bound(xs, xs + xssz, arr[i].first) - xs;       arr[i].second = lower_bound(ys, ys + yssz, arr[i].second) - ys;       ++qntx[arr[i].first];       ++qnty[arr[i].second];       ++dx[arr[i].first];       ++dy[arr[i].second];     }     for (int i = 0; i < m; ++i) {       int t, l, d;       scanf( %d %d %d , &t, &l, &d);       if (t == 1) {         int k = lower_bound(xs, xs + xssz, l) - xs;         if (xs[k] == l) {           dx[k] = min(dx[k], d);         }       } else {         int k = lower_bound(ys, ys + yssz, l) - ys;         if (ys[k] == l) {           dy[k] = min(dy[k], d);         }       }     }     flow_graph fg = flow_graph(xssz + yssz + 2, n + xssz + yssz);     fg.clear(xssz + yssz + 2);     int source = xssz + yssz, sink = source + 1;     for (int i = 0; i < xssz; ++i) {       int l = (qntx[i] - dx[i] + 1) / 2;       int r = (qntx[i] + dx[i]) / 2;       if (l > r) {         puts( -1 );         return 0;       }       fg.add_edge(source, i, l, r);     }     for (int i = 0; i < n; ++i) {       pnt_id[i] = fg.E;       fg.add_edge(arr[i].first, arr[i].second + xssz, 0, 1);     }     for (int i = 0; i < yssz; ++i) {       int l = (qnty[i] - dy[i] + 1) / 2;       int r = (qnty[i] + dy[i]) / 2;       if (l > r) {         puts( -1 );         return 0;       }       fg.add_edge(i + xssz, sink, l, r);     }     if (fg.max_flow(source, sink) < 0) {       puts( -1 );       return 0;     }     long long qnt = 0;     for (int i = 0; i < n; ++i) {       int id = pnt_id[i];       if (fg.flow[id] > 0)         ans[i] = cheap, ++qnt;       else         ans[i] = expensive;     }     printf( %lld n , qnt * r + (n - qnt) * b);     printf( %s n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, K;   vector<int> G[700005], W[700005], I[700005];   long long int ptr[700005];   long long int ans[700005];   int dis[700005], rem[700005], ulta[700005], vis[700005], tim = 0, lev[700005];   int mx = 0;   vector<int> jinish[700005];   void dfs(int u, int l) {     lev[u] = l;     mx = max(mx, l);     vis[u] = 1;     dis[u] = ++tim;     ulta[tim] = u;     jinish[l].push_back(tim);     for (int v : G[u]) {       if (!vis[v]) {         dfs(v, l + 1);       }     }     rem[u] = tim;   }   void dfs2(int u) {     vis[u] = 1;     for (auto v : G[u]) {       if (!vis[v]) {         ans[v] += ans[u];         dfs2(v);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d %d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     dfs(1, 1);     scanf( %d , &m);     while (m--) {       int u, d, x;       scanf( %d %d %d , &u, &d, &x);       ans[u] += x;       int go = min(lev[u] + d + 1, mx + 1);       int in = lower_bound(jinish[go].begin(), jinish[go].end(), dis[u]) -                jinish[go].begin();       if (in < jinish[go].size()) ptr[jinish[go][in]] += x;       int inn = upper_bound(jinish[go].begin(), jinish[go].end(), rem[u]) -                 jinish[go].begin();       if (inn < jinish[go].size()) ptr[jinish[go][inn]] -= x;     }     for (int i = 1; i <= mx; i++) {       for (int j = 1; j < jinish[i].size(); j++) {         ptr[jinish[i][j]] += ptr[jinish[i][j - 1]];       }     }     for (int i = 1; i <= mx; i++) {       for (int j = 0; j < jinish[i].size(); j++) {         ans[ulta[jinish[i][j]]] -= ptr[jinish[i][j]];       }     }     memset(vis, 0, sizeof vis);     dfs2(1);     bool ff = false;     for (int u = 1; u <= n; u++) {       if (ff) printf(   );       ff = true;       printf( %lld , ans[u]);     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   class Timer {     clock_t start;       public:     Timer() : start(clock()) {}     double now() { return (double)(clock() - start) / CLOCKS_PER_SEC; }   } timer;   struct node {     int u, v, l;     friend bool operator<(const node& a, const node& b) { return a.l < b.l; }   } in[300005];   int dp[300005];   queue<pair<int, int> > qe;   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int u, v, l;       scanf( %d%d%d , &in[i].u, &in[i].v, &in[i].l);     }     sort(in + 1, in + 1 + m);     for (int i = 1; i <= m;) {       int l = in[i].l;       while (i <= m && in[i].l == l) {         qe.push(make_pair(in[i].v, dp[in[i].u] + 1));         i++;       }       while (!qe.empty()) {         dp[qe.front().first] = max(dp[qe.front().first], qe.front().second);         qe.pop();       }     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, dp[i]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, i;     cin >> a >> b;     int c = a;     while (a >= b) {       c++;       a++;       a = a - b;     }     i++;     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b, c, co, i, j, k, l, m, n;     cin >> n;     co = 0;     set<long long int> myset;     for (i = 0; i < n; i++) {       cin >> a;       myset.insert(a);     }     if (myset.size() > 3)       cout <<  NO n ;     else if (myset.size() == 3) {       i = 0;       for (auto it = myset.begin(); it != myset.end(); it++) {         if (i == 0) a = *it;         if (i == 1) b = *it;         if (i == 2) c = *it;         i++;       }       if (abs(a - b) == abs(b - c))         cout <<  YES n ;       else         cout <<  NO n ;     } else       cout <<  YES n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int mod = 1e9 + 7;   const int inf = (1 << 30) - 1;   const ll infll = (1LL << 61) - 1;   double a[1 << 17];   int b[1 << 17], N;   ll pos_sum = 0, neg_sum = 0;   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> N;     for (int i = 0; i < N; i++) {       cin >> a[i];       if (a[i] > 0) {         b[i] = ceil(a[i]);         pos_sum += b[i];       } else if (a[i] < 0) {         b[i] = floor(a[i]);         neg_sum += b[i];       }     }     if (abs(pos_sum) < abs(neg_sum)) {       for (int i = 0; i < N && abs(pos_sum) < abs(neg_sum); i++) {         if (a[i] < 0 && ceil(a[i]) != b[i]) {           b[i] = ceil(a[i]);           neg_sum++;         }       }     } else if (abs(pos_sum) > abs(neg_sum)) {       for (int i = 0; i < N && abs(pos_sum) > abs(neg_sum); i++) {         if (a[i] > 0 && floor(a[i]) != b[i]) {           b[i] = floor(a[i]);           pos_sum--;         }       }     }     for (int i = 0; i < N; i++) {       cout << b[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<char> A(n * 2);     for (int i = 0; i < n * 2; i++) cin >> A[i];     vector<vector<pair<int, int> > > table(n * 2, vector<pair<int, int> >(20));     for (int i = 0; i < n * 2; i++) {       if (A[i] ==  X  || i == n - 1 || i == n * 2 - 1)         table[i][0] = make_pair(-1, -1);       else {         if (A[i + 1] ==  . )           table[i][0] = make_pair(i + 1, 1);         else {           int num = (i < n ? i + n : i - n);           if (A[num] ==  . )             table[i][0] = make_pair(num, 1);           else             table[i][0] = make_pair(-1, -1);         }       }     }     for (int k = 1; k < 20; k++) {       for (int i = 0; i < n * 2; i++) {         if (table[i][k - 1].first == -1)           table[i][k] = make_pair(-1, -1);         else {           pair<int, int> p = table[i][k - 1];           table[i][k] = make_pair(table[p.first][k - 1].first,                                   p.second + table[p.first][k - 1].second);         }       }     }     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--, b--;       if (a % n > b % n) swap(a, b);       int k = 19;       int res = 0;       while (k >= 0) {         if (table[a][k].first != -1 && table[a][k].first % n < b % n) {           res += table[a][k].second;           a = table[a][k].first;         }         k--;       }       if (a % n != b % n) {         if (A[a + 1] ==  . )           a = a + 1, res++;         else {           a = a < n ? a + n : a - n, res++;           if (A[a] ==  X ) {             cout <<  -1 n ;             continue;           }           if (A[a + 1] ==  . )             a = a + 1, res++;           else {             cout <<  -1 n ;             continue;           }         }       }       if (a == b)         cout << res <<   n ;       else {         if (A[a < n ? a + n : a - n] ==  . ) {           a = (a < n ? a + n : a - n), res++;           if (a == b)             cout << res <<   n ;           else             cout <<  -1 n ;         } else           cout <<  -1 n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5, mod = 1e9 + 7;   int grundy[2000][2000], dr[50], dc[50];   int x, y, n, d;   bool valid(int x, int y) { return x * x + y * y <= d * d; }   int mex(set<int> &st) {     int ret = 0;     while (st.count(ret)) ret++;     return ret;   }   int solve(int x, int y) {     if (grundy[x][y] != -1) return grundy[x][y];     set<int> st;     for (int i = 0; i < n; i++) {       int nx, ny;       nx = x + dr[i];       ny = y + dc[i];       if (valid(nx - 200, ny - 200)) {         st.insert(solve(nx, ny));       }     }     return grundy[x][y] = mex(st);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     memset(grundy, -1, sizeof grundy);     cin >> x >> y >> n >> d;     for (int i = 0; i < n; i++) {       cin >> dr[i] >> dc[i];     }     if (solve(x + 200, y + 200))       cout <<  Anton ;     else       cout <<  Dasha ;     return 0;   }


#include <bits/stdc++.h>   long long int MAX(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   using namespace std;   int main() {     string S, SS;     cin >> S >> SS;     int PR = 0, DIF = 0, l = S.length();     for (int x = 0; x < l; x++)       if (S[x] != SS[x]) DIF++;     if (DIF & 1)       printf( impossible n );     else {       DIF /= 2;       bool SEC = false;       for (int i = 0; i < l; i++) {         if (S[i] == SS[i])           printf( 1 );         else {           if (PR == DIF) SEC = true;           if (!SEC) {             if (S[i] ==  1 )               printf( 0 );             else               printf( 1 );           } else {             if (SS[i] ==  1 )               printf( 0 );             else               printf( 1 );           }           PR++;         }       }       printf(  n );     }     return 0;   }