#include <bits/stdc++.h>   using namespace std;   long long qitaceruts(long long num) {     long long n = 0, d, p = 1;     while (num > 0) {       d = num % 10;       num = num / 10;       if (d != 0) {         n = (d * p) + n;         p *= 10;       }     }     return n;   }   int main() {     long long a, b, c1, c2;     cin >> a >> b;     c1 = a + b;     a = qitaceruts(a);     b = qitaceruts(b);     c1 = qitaceruts(c1);     if (a + b == c1) {       cout <<  YES n ;     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s1, s2;   int main() {     int n, i, j;     scanf( %d , &n);     cin >> s1 >> s2;     int ans = 0;     for (i = 0; i < (n / 2); i++) {       j = n - i - 1;       map<char, int> mp;       mp[s1[i]]++;       mp[s1[j]]++;       mp[s2[i]]++;       mp[s2[j]]++;       int len = mp.size();       if (len == 4)         ans += 2;       else if (len == 3)         ans += 1 + (s1[i] == s1[j]);       else if (len == 2)         ans += (mp[s1[i]] != 2);     }     if ((n % 2) && (s1[n / 2] != s2[n / 2])) ans++;     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cout.tie(0);     int n;     cin >> n;     vector<bool> in(n + 1);     for (int i = 0; i < n; i++) {       int a;       cin >> a;       in[a] = true;     }     vector<int> b(n);     for (int i = 0; i < n; i++) {       cin >> b[i];     }     int ind = 0;     int next_ = 1;     vector<int> c = b;     for (; next_ <= n; ind++) {       int t = ind - n - 1;       if (t >= 0 && t < n) {         in[b[ind - n - 1]] = true;       }       if (c[ind % n] == next_) {         next_++;         c[ind % n] = -1;         continue;       }       if (c[ind % n] == -1 && in[next_]) {         next_++;         continue;       }       c[ind % n] = -1;       if (ind - next_ + 1 < n) {         next_ = 1;       }     }     cout << ind - n << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long L[500005];   char ch[500005];   class segtree {    public:     long long seg[(1 << 19) * 2];     long long lazy[(1 << 19) * 2];     void lazy_evaluate(int k) {       if (k * 2 + 2 >= (1 << 19) * 2) return;       lazy[k * 2 + 2] += lazy[k];       lazy[k * 2 + 1] += lazy[k];       seg[k * 2 + 2] += lazy[k];       seg[k * 2 + 1] += lazy[k];       lazy[k] = 0;     }     long long update(int beg, int end, int idx, int lb, int ub, long long num) {       if (ub < beg || end < lb) {         return seg[idx];       }       if (beg <= lb && ub <= end) {         lazy[idx] += num;         seg[idx] += num;         return seg[idx];       }       if (lazy[idx]) {         lazy_evaluate(idx);       }       return seg[idx] =                  min(update(beg, end, idx * 2 + 1, lb, (lb + ub) / 2, num),                      update(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub, num));     }     long long query(int beg, int end, int idx, int lb, int ub) {       if (ub < beg || end < lb) {         return 1500000000000000000LL;       }       if (beg <= lb && ub <= end) {         return seg[idx];       }       if (lazy[idx]) {         lazy_evaluate(idx);       }       return min(query(beg, end, idx * 2 + 1, lb, (lb + ub) / 2),                  query(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub));     }   } kaede;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lld , &L[i]);     scanf( %s , &ch);     long long cur = 0;     long long ans = 0;     bool ok = 0;     for (int i = 0; i < n; i++) {       if (ch[i] ==  L )         ans += L[i];       else if (ch[i] ==  W )         ans += L[i] * 3LL;       else         ans += L[i] * 5LL;     }     vector<long long> vec;     int lasw, lasg;     for (int i = 0; i < n; i++) {       if (ch[i] ==  G ) {         cur += L[i];         kaede.update(i, i, 0, 0, (1 << 19) - 1, cur);         vec.push_back(L[i]);         lasg = i;         continue;       }       if (ch[i] ==  W ) {         cur += L[i];         lasw = i;         ok = 1;         kaede.update(i, i, 0, 0, (1 << 19) - 1, cur);         vec.push_back(L[i]);         continue;       }       if (ch[i] ==  L ) {         if (cur >= L[i]) {           cur -= L[i];           kaede.update(i, i, 0, 0, (1 << 19) - 1, cur);           vec.push_back(L[i]);           continue;         } else {           long long need = L[i] - cur;           if (ok) {             ans += 3LL * need;             vec[lasw] += need;           } else {             ans += 5LL * need;             vec[lasg] += need;           }           cur = 0;           vec.push_back(L[i]);           kaede.update(i, i, 0, 0, (1 << 19) - 1, cur);           continue;         }       }     }     for (int i = n - 1; i >= 0; i--) {       if (ch[i] ==  L ) continue;       if (ch[i] ==  W ) continue;       long long mn = kaede.query(i, n - 1, 0, 0, (1 << 19) - 1);       if (2LL * vec[i] <= mn) {         kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -2LL * vec[i]);         ans -= 4LL * vec[i];       } else {         kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -mn);         ans -= 2LL * mn;       }     }     for (int i = n - 1; i >= 0; i--) {       if (ch[i] ==  L ) continue;       if (ch[i] ==  G ) continue;       long long mn = kaede.query(i, n - 1, 0, 0, (1 << 19) - 1);       if (2LL * vec[i] <= mn) {         kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -2LL * vec[i]);         ans -= 2LL * vec[i];       } else {         kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -mn);         ans -= 1LL * mn;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int INF = 0x3f3f3f3f;   const int MAXN = 3e6 + 3;   bool appear[MAXN];   int ans[MAXN];   signed main() {     int cnt = 1;     int n, x;     scanf( %d%d , &n, &x);     int now = 0;     appear[0] = true;     appear[x] = true;     for (int i = 1; i < (1 << n); ++i) {       if (appear[i]) continue;       appear[i ^ x] = true;       ans[cnt++] = i;     }     printf( %d n , cnt - 1);     for (int i = 1; i < cnt; ++i) printf( %d  , ans[i] ^ ans[i - 1]);   }


#include <bits/stdc++.h>   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   void JIZZ(string output =   ) {     cout << output;     exit(0);   }   const long double PI = 3.14159265358979323846264338327950288;   const long double eps = 1e-13;   const long long mod = 1e9 + 7;   long long a[200005], pre[200005];   vector<int> good;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     long long k;     cin >> n >> k;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + a[i];     for (int i = 1; i <= n; ++i)       if (a[i] > 1) good.push_back(i);     good.push_back(n + 1);     if (0) {       long long ans = 0;       for (int i = 1; i <= n; ++i) {         long long s = 0, p = 1;         for (int j = i; j <= n; ++j) {           if (((long double)p) * a[j] > 2e18) break;           p *= a[j], s += a[j];           if (s * k == p) cout << i <<     << j << endl, ++ans;         }       }       cout << ans << endl;     }     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long p = a[i], s = a[i];       int now = i;       auto ptr = upper_bound(good.begin(), good.end(), i);       if (s * k <= p && p <= (s + *ptr - now) * k && p % k == 0) ++ans;       while (p <= 2e18) {         if (*ptr == n + 1) break;         if (((long double)p) * a[*ptr] > 2e18) break;         p *= a[*ptr];         s += pre[*ptr] - pre[now];         now = *ptr;         ++ptr;         if (s * k <= p && p <= (s + *ptr - now - 1) * k && p % k == 0) ++ans;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long N, Q;   long long A[1100100], a, b;   vector<pair<long long, long long> > qu[1100100];   long long sol[1100100], dp[1100100];   long long solve(long long I, long long b) {     if (I > N) return 0;     long long ret = A[I];     return ret += solve(I + b, b);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> N;     for (long long I = 1; I <= N; I++) cin >> A[I];     cin >> Q;     for (long long q = 1; q <= Q; q++) {       cin >> a >> b;       qu[b].push_back(pair<long long, long long>(a, q));     }     for (long long b = 1; b <= N; b++) {       if (b < 300) {         for (long long I = N; I >= 1; I--) {           dp[I] = A[I];           if (I + b <= N) dp[I] += dp[I + b];         }         for (typeof((qu[b]).begin()) it = (qu[b]).begin(); it != (qu[b]).end();              it++)           sol[it->second] = dp[it->first];       } else {         for (typeof((qu[b]).begin()) it = (qu[b]).begin(); it != (qu[b]).end();              it++)           sol[it->second] = solve(it->first, b);       }     }     for (long long q = 1; q <= Q; q++) cout << sol[q] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100000 + 10;   int n, m;   pair<int, int> s[MAX];   pair<long long, long long> ans;   void update(long long a, long long b) {     if (b <= 0) return;     if (a > ans.first) {       ans.first = a;       ans.second = b;     } else if (a == ans.first)       ans.second += b;   }   void work1() {     int i, mi = n, mm = 0;     long long num = 0;     for ((i) = (1); (i) <= (m); ++(i))       if (s[i].first == 0) {         ++num;         mi = min(mi, s[i].second);         mm = max(mm, s[i].second);       }     long long num_mi = 0, num_mm = 0;     for ((i) = (1); (i) <= (m); ++(i)) {       if (s[i].first == 0 && s[i].second == mi) ++num_mi;       if (s[i].first == 0 && s[i].second == mm) ++num_mm;     }     if (mm == mi) {       update(0, num * (num - 1) * (num - 2) / 6);       return;     }     num -= num_mi;     num -= num_mm;     update((mm - mi) * 2, (num_mi - 1) * num_mi / 2 * num_mm +                               (num_mm - 1) * num_mm / 2 * num_mi +                               num * num_mi * num_mm);   }   int cmp(pair<int, int> a, pair<int, int> b) {     if (a.second != b.second)       return a.second < b.second;     else       return a.first < b.first;   }   void work2() {     sort(s + 1, s + m + 1, cmp);     int i;     long long num0 = 0;     for ((i) = (1); (i) <= (m); ++(i))       if (s[i].first == 0) ++num0;     int largb = 0, numb = 0, tmp = num0;     for ((i) = (1); (i) <= (m); ++(i))       if (s[i].first == 0) {         --tmp;         if (largb != s[i].second)           update(2LL * n + 2 * largb - 2 * s[i].second, (long long)tmp * numb);       } else if (s[i].second == largb)         ++numb;       else if (s[i].second > largb)         largb = s[i].second, numb = 1;     int smallb = n + 1;     numb = 0;     tmp = num0;     for (i = m; i >= 1; --i)       if (s[i].first == 0) {         --tmp;         if (smallb != s[i].second)           update(2LL * n - 2 * smallb + 2 * s[i].second, (long long)tmp * numb);       } else if (s[i].second == smallb)         ++numb;       else if (s[i].second < smallb)         smallb = s[i].second, numb = 1;     map<int, int> hash;     for ((i) = (1); (i) <= (m); ++(i))       if (s[i].first == 0) hash[s[i].second]++;     tmp = 0;     for ((i) = (1); (i) <= (m); ++(i)) {       if (s[i].first == 0)         ++tmp;       else {         long long u = hash[s[i].second];         long long now = (long long)tmp * (num0 - tmp) +                         (long long)u * (u - 1) / 2 + (tmp - u) * u;         update(2LL * n, now);       }     }   }   void work() {     work1();     work2();   }   int main() {     int i;     scanf( %d%d , &n, &m);     for ((i) = (1); (i) <= (m); ++(i)) scanf( %d%d , &s[i].first, &s[i].second);     work();     for ((i) = (1); (i) <= (m); ++(i)) s[i].first ^= 1;     work();     cout << ans.second << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   const int inf = 1e9 + 7;   int main() {     int n;     scanf( %d , &n);     while (n--) {       int x;       scanf( %d , &x);       if (x <= 3 || x == 5 || x == 7 || x == 11)         puts( -1 );       else {         int ans = 0, tg = x & 3;         if (!tg) ans = max(ans, x / 4);         if (tg == 2) ans = max(ans, 1 + (x - 6) / 4);         if (tg == 1) ans = max(ans, 1 + (x - 9) / 4);         if (tg == 3) ans = max(ans, 2 + (x - 15) / 4);         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = ((int)1e5) + 100;   int arr[Maxn], n;   map<int, int> num, numL, numR;   long long Solve(int a[]) {     int l = 0, r = n - 1;     while (a[l] == a[r] && l < r) ++l, --r;     if (l >= r) return n * 1ll * (n + 1) / 2;     for (int(i) = (l); (i) <= (r); ++(i)) num[a[i]] += 1;     int odds = 0, key = -1;     for (auto it = num.begin(); it != num.end(); ++it)       if ((it->second) & 1) odds += 1, key = it->first;     if (odds > 1) return 0;     int oks = 0, l2 = l - 1;     while (oks < num.size()) {       numL[a[++l2]] += 1;       int _a = numL[a[l2]] << 1;       int _b = _a - 2, _c = num[a[l2]];       if (_a >= _c && _b < _c) ++oks;     }     oks = 0;     int r2 = r + 1;     while (oks < num.size()) {       numR[a[--r2]] += 1;       int _a = numR[a[r2]] << 1;       int _b = _a - 2, _c = num[a[r2]];       if (_a >= _c && _b < _c) ++oks;     }     int mid = (r + l) >> 1;     if (l2 <= mid && (!(n & 1) || a[mid] == key)) {       int g, h;       if (n & 1)         g = mid - 1, h = mid + 1;       else         g = mid, h = mid + 1;       while (a[g] == a[h]) --g, ++h;       l2 = g, r2 = h;     }     long long sum = 0;     sum += (l + 1) * 1ll * (n - l2);     sum += (n - r) * 1ll * (r2 + 1);     sum -= (l + 1) * 1ll * (n - r);     return sum;   }   int main() {     iostream::sync_with_stdio(false);     cin >> n;     for (int(i) = (0); (i) <= (n - 1); ++(i)) cin >> arr[i];     cout << Solve(arr) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const long long INF = 1e18;   const int M = 1e9 + 7;   const int maxn = 1e6;   int b[maxn], c[maxn];   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout.setf(ios::fixed);     cout.precision(10);     int m, k, n, s;     cin >> m >> k >> n >> s;     vector<int> a(m);     for (int i = 0; i < m; ++i) {       cin >> a[i];     }     for (int i = 0; i < s; ++i) {       int x;       cin >> x;       ++b[x];     }     int res = inf;     int l = 0, r = 0, cnt = 0;     int L = 0, R = 0;     while (r < m) {       while (r < m && cnt < s) {         if (c[a[r]] < b[a[r]]) {           ++cnt;         }         ++c[a[r]];         ++r;       }       while (l < m && cnt == s) {         int tl = l / k, tr = (r - 1) / k;         int tot;         if (tl == tr) {           tot = 0;         } else {           tot = r - tl * k - k;         }         if (tot < res) {           L = l;           R = r - 1;           res = tot;         }         c[a[l]]--;         if (c[a[l]] < b[a[l]]) {           --cnt;         }         ++l;       }     }     if ((m - res) / k < n) {       cout << -1 << endl;     } else {       cout << res << endl;       for (int i = R; i >= L - L % k; --i) {         if (res == 0) break;         if (!b[a[i]]) {           cout << i + 1 <<    ;           --res;         } else {           --b[a[i]];         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100007;   const int maxv = 100000;   long long fac[maxn], fac2[maxn];   map<pair<int, int>, int> save;   int cnt;   long long mypow(int base, int n) {     if (n == 0) return 1;     long long t = mypow(base, n / 2);     t = (t * t) % 1000000007;     if (n & 1)       return (t * base) % 1000000007;     else       return t;   }   long long C(int n, int k) {     long long res = fac[n] * fac2[k] % 1000000007;     res = res * fac2[n - k] % 1000000007;     return res;   }   long long solve(int f, int n) {     if (f == 1) return (n == 1);     if (f == n) return 1;     if (f > n) return 0;     if (save.count(pair<int, int>(f, n))) return save[pair<int, int>(f, n)];     long long res = 0;     for (int i = 2; i * i <= n; i++)       if (n % i == 0) {         int x = i;         res = (res + solve(f, n / x)) % 1000000007;         if (i * i == n) continue;         x = n / i;         res = (res + solve(f, n / x)) % 1000000007;       }     save[pair<int, int>(f, n)] =         (C(n - 1, f - 1) - res + 1000000007) % 1000000007;     return save[pair<int, int>(f, n)];   }   int main() {     fac[0] = 1;     for (int i = 1, _c = maxv; i <= _c; i++) fac[i] = fac[i - 1] * i % 1000000007;     for (int v = 0, _c = maxv; v <= _c; v++)       fac2[v] = mypow(fac[v], 1000000007 - 2);     int q, f, n;     scanf( %d , &q);     for (int i = 0, _a = (q); i < _a; ++i) {       scanf( %d%d , &n, &f);       printf( %I64d n , solve(f, n));     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long in() {     int32_t x;     scanf( %d , &x);     return x;   }   inline string getStr() {     char ch[1000000];     scanf( %s , ch);     return ch;   }   const long long MOD = 1e9 + 7;   const long long base = 29;   const long long MAX_N = 3e5 + 1;   inline long long divide(long long a, long long b) { return (a + b - 1) / b; }   vector<long long> g[MAX_N];   long long deg[MAX_N];   bool vis[MAX_N];   long long res[MAX_N];   inline void dfs(long long node, long long part) {     if (vis[node] == true) return;     res[node] = part;     long long cnt = 0;     for (long long i = 0; i < g[node].size(); i++) {       long long u = g[node][i];       if (res[node] == res[u]) cnt++;     }     if (cnt > 1) {       res[node] ^= 1;       dfs(node, res[node]);       vis[node] = false;       return;     }     vis[node] = true;     for (long long pt = 0; pt < g[node].size(); pt++) {       long long u = g[node][pt];       if (res[u] == res[node]) {         dfs(u, res[u]);         vis[node] = 0;         return;       }     }     vis[node] = false;   }   int32_t main() {     long long n = in(), m = in();     for (long long i = 0; i < m; i++) {       long long v = in(), u = in();       deg[v]++, deg[u]++;       g[v].push_back(u);       g[u].push_back(v);     }     for (long long i = 1; i <= n; i++) res[i] = 1;     for (long long i = 1; i <= n; i++) {       dfs(i, res[i]);     }     for (long long i = 1; i <= n; i++) cout << res[i];   }


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


#include <bits/stdc++.h>   using namespace std;   int n, x, y, z, x_, y_, z_;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x_ >> y_ >> z_;       x += x_;       z += z_;       y += y_;     }     if (x != 0 || y != 0 || z != 0)       cout <<  NO  << endl;     else       cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct IInt {     int z;   };   bool cmp(int a, int b) { return a < b; }   bool operator<(IInt a, IInt b) { return a.z > b.z; }   int main() {     int n, m, x, i, ans, ans1;     IInt t;     scanf( %d%d , &n, &m);     vector<int> q;     priority_queue<IInt> w;     for (i = 0; i < m; i++) {       scanf( %d , &x);       q.push_back(x);       t.z = x;       w.push(t);     }     make_heap(q.begin(), q.end(), cmp);     ans = ans1 = 0;     for (i = 0; i < n; i++) {       ans += q[0];       q[0]--;       make_heap(q.begin(), q.end(), cmp);     }     for (i = 0; i < n; i++) {       t = w.top();       w.pop();       ans1 += t.z;       if (t.z == 1) {         continue;       } else {         t.z--;         w.push(t);       }     }     printf( %d %d n , ans, ans1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005, M = 2000010;   const long long MOD = 1E9 + 7;   long long Pow(long long a, long long b) {     long long c = 1;     for (; b; a = a * a % MOD, b >>= 1)       if (b & 1) c = c * a % MOD;     return c;   }   struct Node {     int x, y;     bool operator<(const Node &a) const {       return (x != a.x) ? (x < a.x) : (y < a.y);     }   } p[N];   int h, w, n, m;   long long fac[M], fac_inv[M];   long long f[N];   void Init() {     fac[0] = 1;     for (int i = 1; i <= m; ++i) fac[i] = fac[i - 1] * i % MOD;     fac_inv[m] = Pow(fac[m], MOD - 2);     for (int i = m; i; --i) fac_inv[i - 1] = fac_inv[i] * i % MOD;     return;   }   inline long long F(int a, int b) {     return fac[a + b] * fac_inv[a] % MOD * fac_inv[b] % MOD;   }   int main() {     scanf( %d%d%d , &h, &w, &n);     m = h + w;     Init();     for (int i = 1; i <= n; ++i) scanf( %d%d , &p[i].x, &p[i].y);     sort(p + 1, p + n + 1);     for (int i = 1; i <= n; ++i) {       f[i] = F(p[i].x - 1, p[i].y - 1);       for (int j = 1; j < i; ++j)         if (p[j].y <= p[i].y)           f[i] = (f[i] + MOD - f[j] * F(p[i].x - p[j].x, p[i].y - p[j].y) % MOD) %                  MOD;     }     long long res = F(h - 1, w - 1);     for (int i = 1; i <= n; ++i)       res = (res + MOD - f[i] * F(h - p[i].x, w - p[i].y) % MOD) % MOD;     printf( %lld n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const int MOD = 1000000007;   signed main() {     cin.tie(0), cout.tie(0);     ios::sync_with_stdio(0);     int n, k;     cin >> n >> k;     int a, b, c, d;     cin >> a >> b >> c >> d;     if (k < n + 1 || n <= 4)       cout << -1 <<   n , exit(0);     else {       cout << a <<     << c <<    ;       for (int i = 1; i <= n; i++)         if (i != a && i != b && i != c && i != d) cout << i <<    ;       cout << d <<     << b <<   n ;       cout << c <<     << a <<    ;       for (int i = 1; i <= n; i++)         if (i != a && i != b && i != c && i != d) cout << i <<    ;       cout << b <<     << d <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int h1, a1, c1, h2, a2;     int move_counter = 0;     vector<char> move;     cin >> h1 >> a1 >> c1 >> h2 >> a2;     while (h2 > 0) {       if (h2 - a1 <= 0) {         move.push_back( S );         ++move_counter;         break;       } else {         if (h1 - a2 <= 0) {           h1 += c1;           move.push_back( H );           ++move_counter;         } else {           h2 -= a1;           move.push_back( S );           ++move_counter;         }         h1 -= a2;       }     }     cout << move_counter <<   n ;     for (const auto it : move) {       if (it ==  H )         cout <<  HEAL  <<   n ;       else         cout <<  STRIKE  <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, x[1010], dp[1010][1010], sm[1010];   long long solve(long long gap) {     dp[0][0] = 1, x[0] = -200000;     for (long long i = 1; i <= k; ++i) {       for (long long j = 0; j <= n; ++j)         sm[j] = j == 0 ? dp[i - 1][j] : (dp[i - 1][j] + sm[j - 1]) % 998244353;       long long pos = 0;       for (long long j = 1; j <= n; ++j) {         while (x[j] - x[pos + 1] >= gap) pos++;         dp[i][j] = sm[pos];       }     }     long long sum = 0;     for (long long i = 1; i <= n; ++i) sum = (sum + dp[k][i]) % 998244353;     return sum;   }   int32_t main() {     cin.tie(0), cout.sync_with_stdio(0);     cin >> n >> k;     for (long long i = 1; i <= n; ++i) cin >> x[i];     sort(x + 1, x + 1 + n);     long long dis = 100000 / (k - 1), pre = solve(1), ans = 0;     for (long long i = 2; i <= dis + 5; ++i) {       long long now = solve(i);       ans = (ans + (pre - now + 998244353) * (i - 1)) % 998244353;       pre = now;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[1000];     int num;     cin >> num;     for (int i = 0; i < num; i++) {       cin >> arr[i];       arr[num + i] = arr[i];     }     int ans = 360;     for (int i = 0; i < num; i++) {       int temp = 0;       for (int j = i; j < num * 2; j++) {         temp += arr[j];         ans = min(ans, abs(360 - 2 * temp));       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b;   char s[1000005];   int arr[205];   int main() {     scanf( %d%d%d%s , &n, &a, &b, s);     int i, j, len = strlen(s), tag;     if (len > n * b || len < n * a) {       puts( No solution );       return 0;     }     int eve = len / n;     for (i = 0; i < n; i++) {       arr[i] = eve;     }     eve = len % n;     for (i = 0; i < eve; i++) arr[i]++;     for (i = tag = 0; i < n; i++) {       for (j = 0; j < arr[i]; j++) printf( %c , s[tag++]);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char instr[51], outstr[51] = {}, ch;     int n, i;     cin >> instr;     cin >> n;     for (i = 0; i < strlen(instr); i++) {       if (instr[i] < 97) instr[i] += 32;     }     for (i = 0; i < strlen(instr); i++) {       ch = instr[i];       if (int(ch) < (n + 97)) {         outstr[i] = ch - 32;       } else {         outstr[i] = ch;       }     }     cout << outstr;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int N, M;     cin >> N >> M;     vector<int> arr(N + 2);     arr[0] = 1;     for (int i = 0; i < N; ++i) cin >> arr[i + 1];     arr[N + 1] = 1;     int q = M, w = M;     int r = 0;     while (q > 0 || w < N + 1) {       int s = arr[q] + arr[w];       if (s == 2) r += s - (q == 0) - (w == N + 1) - (w == q);       if (q > 0) q--;       if (w < N + 1) w++;     }     cout << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int d[300002];   int safe[300002];   bool solve() {     long long int n, k, l, i;     cin >> n >> k >> l;     for (i = 1; i <= n; i++) cin >> d[i];     int tide, dir, mtide;     tide = k;     dir = -1;     for (i = 1; i <= n; i++) {       if (d[i] + k <= l)         tide = k, dir = -1;       else if (d[i] > l)         return false;       else {         mtide = l - d[i];         if (tide + dir <= mtide) {           tide += dir;           if (tide == 0) dir = 1;         } else if (dir == -1 && mtide < tide - 1) {           tide = mtide;           if (tide == 0) dir = 1;         } else           return false;       }     }     return true;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t, i;     cin >> t;     while (t--) {       if (solve())         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long int a[n];       unordered_map<long long int, long long int> mp;       for (int i = 0; i < n; i++) {         cin >> a[i];         mp[a[i]]++;       }       if (mp.size() == 1)         cout <<  NO  << endl;       else {         cout <<  YES  << endl;         int f = -1, s = -1;         for (int i = 1; i < n; i++) {           if (a[i] != a[0]) {             f = i;             break;           }         }         for (int i = 1; i < n; i++) {           if (a[i] != a[f])             cout << i + 1 <<     << f + 1 << endl;           else             cout << 1 <<     << i + 1 << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 2e5 + 10;   int n;   string s, xx =  RGB ;   int getID(char c) {     if (c ==  R )       return 1;     else if (c ==  G )       return 2;     return 3;   }   int last[4];   int dp[MAX][4][4][4];   int mn = 1e9;   vector<char> v, ans;   int tokyoGhoul(int cur, int cnt) {     if (cur == n) {       if (cnt < mn) {         mn = cnt;         ans = v;       }       return cnt;     }     int &ret = dp[cur][last[0]][last[1]][last[2]];     if (~ret) return ret;     ret = 1e18;     for (int i = 0; i < xx.size(); i++) {       int id = getID(xx[i]);       int prev = last[id];       char ch = xx[i];       if (prev == -1 || (cur - prev) != 1) {         v.push_back(ch);         last[id] = cur;         if (xx[i] != s[cur])           ret = min(ret, tokyoGhoul(cur + 1, cnt + 1));         else           ret = min(ret, tokyoGhoul(cur + 1, cnt));         last[id] = prev;         v.pop_back();       }     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> s;     vector<string> vv;     int cnt = 0, cnt2 = 0;     string s1 =   , s2 =   ;     for (int j = 0; j < 3; j++) {       cnt = 0, s1 =   ;       s1 += xx[j];       if (s[0] != s1[0]) cnt++;       for (int i = 1; i < n; i++) {         if (s[i] == s1[i - 1]) {           cnt++;           bool f = 1;           for (int j = 0; j < xx.size(); j++) {             if (s1[i - 1] != xx[j] && (i + 1 == n || s[i + 1] != xx[j])) {               s1 += xx[j];               f = 0;               break;             }           }           if (f)             for (int j = 0; j < xx.size(); j++) {               if (s[i - 1] != xx[j]) {                 s1 += xx[j];                 break;               }             }         } else           s1 += s[i];       }       if (cnt < mn) {         mn = cnt;         s2 = s1;       }     }     cout << mn << endl;     for (auto i : s2) cout << i;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T min(T a, T b, T c) {     return min(a, min(b, c));   }   template <class T>   T max(T a, T b, T c) {     return max(a, max(b, c));   }   int r, c, k, n, a[15][15];   int main() {     scanf( %d%d%d%d , &r, &c, &n, &k);     for (int i = (1); i <= (n); ++i) {       int x, y;       scanf( %d%d , &x, &y);       a[x][y] = 1;     }     int ans = 0;     for (int x1 = (1); x1 <= (r); ++x1) {       for (int y1 = (1); y1 <= (c); ++y1) {         for (int x2 = (x1); x2 <= (r); ++x2) {           for (int y2 = (y1); y2 <= (c); ++y2) {             int sum = 0;             for (int x = (x1); x <= (x2); ++x) {               for (int y = (y1); y <= (y2); ++y) sum += a[x][y];             }             if (sum >= k) ++ans;           }         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5010, inf = 0x7fffffff;   bool has[maxn][maxn], flag = 0;   int sum[maxn][maxn], n, m, l[10], r[10], req;   long long ans;   void init() {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         sum[i][j] =             sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (has[i][j] == 1);   }   int calculate(int x1, int y1, int x2, int y2) {     return sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1];   }   void work(int up, int down, int left, int right, bool dir) {     if (left == right || up == down) return;     if (left + 1 == right && up + 1 == down) {       ans += calculate(up, left, down, right) == req;       return;     }     if (dir == 0) {       int mid = (left + right) >> 1;       work(up, down, left, mid, dir ^ 1);       work(up, down, mid, right, dir ^ 1);       for (int i = up; i < down; i++) {         l[0] = r[0] = mid;         for (int k = 1; k <= req + 1; k++) l[k] = left, r[k] = right;         for (int j = i + 1; j <= down; j++) {           for (int k = 1; k <= req + 1; k++)             while (calculate(i, l[k], j, mid) >= k) l[k]++;           for (int k = 1; k <= req + 1; k++)             while (calculate(i, mid, j, r[k]) >= k) r[k]--;           for (int k = 0; k <= req; k++)             ans += (l[k] - l[k + 1]) * (r[req - k + 1] - r[req - k]);         }       }     } else {       int mid = (up + down) >> 1;       work(up, mid, left, right, dir ^ 1);       work(mid, down, left, right, dir ^ 1);       for (int i = left; i < right; i++) {         l[0] = r[0] = mid;         for (int k = 1; k <= req + 1; k++) l[k] = up, r[k] = down;         for (int j = i + 1; j <= right; j++) {           for (int k = 1; k <= req + 1; k++)             while (calculate(l[k], i, mid, j) >= k) l[k]++;           for (int k = 1; k <= req + 1; k++)             while (calculate(mid, i, r[k], j) >= k) r[k]--;           for (int k = 0; k <= req; k++)             ans += (l[k] - l[k + 1]) * (r[req - k + 1] - r[req - k]);         }       }     }   }   int main() {     while (scanf( %d%d%d , &n, &m, &req) == 3) {       memset(has, 0, sizeof(has));       ans = 0;       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) scanf( %1d , &has[i][j]);       init();       work(0, n, 0, m, n < m);       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, ta, tb;     cin >> a >> ta >> b >> tb;     int t, m;     char c;     cin >> t >> c >> m;     m += 60 * t;     int cur = 5 * 60;     int res = 0;     while (cur < 24 * 60) {       if (min(cur + tb, m + ta) > max(cur, m)) ++res;       cur += b;     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 100000007;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<long long> nums(n);       for (int i = 0; i < n; i++) cin >> nums[i];       double all = 0;       sort(nums.rbegin(), nums.rend());       for (int i = 1; i < n; i++) all += nums[i];       all = 1.0 * all / (n - 1) + nums[0];       printf( %.8lf n , all);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string res =   ;     cin >> n;     int arr[2004] = {0};     for (int i = 0; i < n; ++i) {       cin >> arr[i];     }     int now_rejt = 0;     int mesto = 1;     for (int i = 0; i < n; ++i) {       now_rejt = arr[i];       mesto = 1;       for (int j = 0; j < n; ++j) {         if (now_rejt < arr[j]) {           ++mesto;         }       }       stringstream ss;       ss << mesto;       string strmesto;       ss >> strmesto;       res = res +     + strmesto;     }     res.erase(0, 1);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, map<int, vector<int>>> answers;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int bus, tram, money;       cin >> bus >> tram >> money;       string s;       cin >> s;       int cur = s.size() - 2;       while (true) {         char cur_char = s[cur];         if (cur_char ==  A ) {           if (bus > money) {             cout << cur + 2 <<   n ;             break;           }           money -= bus;         }         if (cur_char ==  B ) {           if (tram > money) {             cout << cur + 2 <<   n ;             break;           }           money -= tram;         }         while (cur >= 0 && s[cur] == cur_char) {           cur -= 1;         }         if (cur == -1) {           cout << 1 <<   n ;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int GCD(int x, int y) { return !y ? x : GCD(y, x % y); }   int LCM(int x, int y) { return x * y / GCD(x, y); }   long long ceilDivision(long long dividend, long long divisor) {     return (dividend + divisor - 1) / divisor;   }   bool check(pair<long long, int> right, pair<long long, int> left) {     return ((left.second > right.second) ||             (left.second == right.second && left.first > right.first));   }   int main() {     long long r, avg, sum = 0, target, counter = 0;     int n, pointer = 0;     cin >> n >> r >> avg;     vector<pair<long long, int> > arr(n);     target = avg * n;     for (int i = 0; i < n; i++)       cin >> arr[i].first >> arr[i].second, sum += arr[i].first;     if (sum < target) {       sort(arr.begin(), arr.end(), check);       target -= sum;       while (target) {         if (arr[pointer].first < r)           counter += (min(target, r - arr[pointer].first) * arr[pointer].second),               target -= min(target, r - arr[pointer].first);         pointer++;       }     }     cout << counter << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> dir_graph;   vector<int> vis, order;   void dfs(int i) {     vis[i] = true;     for (int j = 0; j < dir_graph[i].size(); j++) {       if (!vis[dir_graph[i][j]]) {         dfs(dir_graph[i][j]);       }     }     order.push_back(i);   }   int main() {     int cases;     cin >> cases;     while (cases--) {       int n, m;       cin >> n >> m;       dir_graph = vector<vector<int>>(n);       order.clear();       vis = vector<int>(n);       vector<pair<int, int>> edges;       int type, s, d;       for (int i = 0; i < m; i++) {         cin >> type >> s >> d;         s--;         d--;         if (type == 1) dir_graph[s].push_back(d);         edges.push_back({s, d});       }       for (int i = 0; i < n; i++) {         if (!vis[i]) {           dfs(i);         }       }       vector<int> pos(n);       reverse(order.begin(), order.end());       for (int i = 0; i < n; ++i) {         pos[order[i]] = i;       }       bool hasCycle = false;       for (int i = 0; i < n; i++) {         for (int j = 0; j < dir_graph[i].size(); j++) {           if (pos[i] > pos[dir_graph[i][j]]) {             hasCycle = true;             break;           }         }       }       if (hasCycle)         cout <<  NO  << endl;       else {         cout <<  YES n ;         for (int i = 0; i < edges.size(); i++) {           if (pos[edges[i].first] < pos[edges[i].second]) {             cout << edges[i].first + 1 <<     << edges[i].second + 1 << endl;           } else {             cout << edges[i].second + 1 <<     << edges[i].first + 1 << endl;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char ch[] = { B ,  u ,  l ,  b ,  a ,  s ,  a ,  u ,  r };     map<char, int> mp;     string ss;     int n, k, i, cnt = 100000;     cin >> ss;     for (i = 0; i < ss.size(); i++) {       mp[ss[i]]++;     }     for (i = 0; i < 9; i++) {       k = mp[ch[i]];       if (ch[i] ==  u  || ch[i] ==  a ) k = k / 2;       cnt = min(cnt, k);     }     cout << cnt << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int n;   int arr1[10];   int arr2[100005];   pair<int, int> diffs[600005];   int cnts[100005];   int main() {     while (scanf( %d , arr1) == 1) {       for (int i = 1; i < 6; ++i) {         scanf( %d , arr1 + i);       }       scanf( %d , &n);       for (int i = 0; i < n; ++i) {         scanf( %d , arr2 + i);       }       int idx = 0;       for (int i = 0; i < 6; ++i) {         for (int j = 0; j < n; ++j) {           diffs[idx].first = arr2[j] - arr1[i];           diffs[idx].second = j;           ++idx;         }       }       sort(diffs, diffs + idx);       memset(cnts, 0, sizeof(cnts));       int cnt = 0;       int j = 0;       int ans = INF;       for (int i = 0; i < 5 * n; ++i) {         if (i && --cnts[diffs[i - 1].second] <= 0) {           --cnt;         }         while (cnt < n && j < 6 * n) {           if (cnts[diffs[j].second]++ == 0) {             ++cnt;           }           ++j;         }         if (cnt >= n) {           ans = min(ans, diffs[j - 1].first - diffs[i].first);         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char a;     cin >> a;     string str1, str2;     cin >> str1 >> str2;     int b1, b2;     switch (str1[0]) {       case  T :         b1 = 10;         break;       case  J :         b1 = 11;         break;       case  Q :         b1 = 12;         break;       case  K :         b1 = 13;         break;       case  A :         b1 = 14;         break;       default:         b1 = str1[0] -  0 ;     }     switch (str2[0]) {       case  T :         b2 = 10;         break;       case  J :         b2 = 11;         break;       case  Q :         b2 = 12;         break;       case  K :         b2 = 13;         break;       case  A :         b2 = 14;         break;       default:         b2 = str2[0] -  0 ;     }     if (str1[1] == a)       if (str2[1] != str1[1]) {         cout <<  YES ;       } else {         if (b1 > b2) {           cout <<  YES ;         } else           cout <<  NO ;       }     else {       if (str2[1] == str1[1]) {         if (b1 > b2) {           cout <<  YES ;         } else           cout <<  NO ;       } else {         cout <<  NO ;       };     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5001;   int n, m, x, y, c[N], p[N], A;   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) p[i] = N;     while (m-- && cin >> x >> y) c[x]++, p[x] = min(p[x], (y + n - x) % n);     for (int i = 1; i <= n; i++) {       A = 0;       for (int j = 1; j <= n; j++)         if (c[j]) A = max(A, (j + n - i) % n + (c[j] - 1) * n + p[j]);       cout << A <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 50;   int n, q;   int lca[N][25], mark[N], sum[N], h[N], a[N][4];   vector<int> adj[N];   map<int, map<int, int> > m;   void DFS(int v, int p, int hi) {     h[v] = hi;     lca[v][0] = p;     for (auto u : adj[v]) {       if (u == p) continue;       DFS(u, v, hi + 1);     }   }   void dfs_sum(int v, int p) {     sum[v] += mark[v];     for (auto u : adj[v]) {       if (u == p) continue;       dfs_sum(u, v);       sum[v] += sum[u];     }     m[p][v] = sum[v];   }   void LCA() {     for (int j = 1; j <= 20; j++)       for (int i = 1; i <= n; i++) lca[i][j] = lca[lca[i][j - 1]][j - 1];   }   int find_par(int v, int k) {     for (int j = 0; j <= 20; j++) {       if (k & (1 << j)) v = lca[v][j];     }     return v;   }   int find_lca(int u, int v) {     v = find_par(v, h[v] - h[u]);     if (v == u) return u;     for (int j = 20; j >= 0; j--) {       if (lca[u][j] != lca[v][j]) {         u = lca[u][j];         v = lca[v][j];       }     }     return lca[u][0];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie();     cin >> n;     for (int i = 1; i < n; i++) {       cin >> a[i][0] >> a[i][1];       adj[a[i][0]].push_back(a[i][1]);       adj[a[i][1]].push_back(a[i][0]);     }     DFS(n - 1, 0, 1);     LCA();     cin >> q;     for (int i = 0; i < q; i++) {       int u, v;       cin >> u >> v;       if (h[u] > h[v]) swap(u, v);       int lca = find_lca(u, v);       mark[u]++;       mark[v]++;       mark[lca] -= 2;     }     dfs_sum(n - 1, 0);     for (int i = 1; i < n; i++) {       cout << max(m[a[i][0]][a[i][1]], m[a[i][1]][a[i][0]]) <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long maxn = 1e5 + 5;   void solve() {     long long k;     cin >> k;     long long first = pow(2, 17);     cout << 2 <<     << 3 <<   n ;     cout << first + k <<     << first <<     << 0 <<   n           << k <<     << first + k <<     << k;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long tc = 1;     while (tc--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 6;   int n, m, k;   int s[N], c[N];   bool vis[N];   int fir[N];   long long ans = 1e15;   void can(int u) {     long long rep = 0;     for (int i = 1; i <= n;) {       if (fir[i + u] == i) {         return;       }       rep += c[u];       if (i + u > n) {         break;       }       i = fir[i + u];     }     ans = min(ans, rep);   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= m; i++) {       scanf( %d , &s[i]);       vis[s[i] + 1] = 1;     }     for (int i = 1; i <= k; i++) {       scanf( %d , &c[i]);     }     for (int i = 1; i <= n; i++) {       if (vis[i]) {         fir[i] = fir[i - 1];       } else {         fir[i] = i;       }     }     if (!fir[1]) {       printf( -1 n );       return 0;     }     for (int i = 1; i <= k; i++) {       can(i);     }     if (ans == 1e15) {       printf( -1 n );     } else {       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int where[26][26];   int n, dp[(1 << 23) + 5], d[35];   bool fdp[(1 << 23) + 5];   int rec(int mask) {     if (fdp[mask]) return dp[mask];     int next = 0;     for (int i = n - 1; i >= 0; --i) {       if (mask & (1 << i)) {         next = i;         break;       }     }     next++;     fdp[mask] = 1;     if (next == n) return dp[mask] = __builtin_popcount(mask);     int make = d[next + 1];     dp[mask] = 30;     int valid = 0;     for (int i = 0; i < n; ++i) {       if (mask & (1 << i)) {         int j = where[next + 1][i + 1];         if (d[i + 1] < make and j) {           j--;           if (mask & (1 << j)) valid = 1;         }       }     }     if (valid) {       dp[mask] = rec(mask | (1 << next));       for (int i = 0; i < next; ++i) {         if (mask & (1 << i))           dp[mask] = min(dp[mask], rec((mask ^ (1 << i)) | (1 << next)));       }     }     return dp[mask];   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> d[i];     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         int f = d[i] - d[j];         for (int k = 1; k <= n; ++k) {           if (d[k] == f) {             where[i][j] = k;           }         }       }     }     int ans = rec(1);     if (ans == 30) ans = -1;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, p, q, ay, by, tx, g, l, ty, x, y, n = 15, d, m, tot, last, lst2;   map<int, int> wow;   long long shob[104];   vector<int> all;   char c;   int setbit(int n, int pos) { return n | (1 << (pos)); }   int isbit(int n, int pos) { return n & (1 << (pos)); }   int how(int a, long long &x) {     int ret = 0, boom = 1, wow = 0;     while (a > 0) {       ret++;       int md = a % 7;       wow = boom * md + wow;       boom *= 10;       a /= 7;     }     x = wow;     return ret;   }   int Bboom(int n, int len, int mask, int A) {     if (len > b) {       return 0;     }     int now = n, ans = 0;     for (int i = 0; i < 7; i++) {       if (isbit(mask, i) == 0) {         now = n * 10 + i;         if (len == b && now <= y) {           ans++;         } else if (len < b && now <= y && isbit(mask, i) == 0) {           ans += Bboom(now, len + 1, setbit(mask, i), A);         }       }     }     return ans;   }   int Aboom(int n, int len, int mask) {     if (len > a) {       return 0;     }     int now = n, ans = 0;     for (int i = 0; i < 7; i++) {       if (isbit(mask, i) == 0) {         now = n * 10 + i;         if (len == a && now <= x) {           ans += Bboom(0, 1, setbit(mask, i), now);         } else if (len < a && now <= x) {           ans += Aboom(now, len + 1, setbit(mask, i));         }       }     }     return ans;   }   int main() {     cin >> n >> m;     n--;     m--;     a = max(how(n, x), 1);     b = max(how(m, y), 1);     if (a + b > 7) {       cout << 0 << endl;     } else {       n = a + b;       p = x;       q = y;       cout << Aboom(0, 1, 0);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = (long long)1e9 + 7;   const long long inf = (long long)1e16;   const long double eps = 1e-12;   const long long N = (int)1e5 + 5;   const long long LOGN = 19;   const long double PI = 3.14159265358979323846;   inline long long mul(long long a, long long b, long long m = mod) {     return (long long)(a * b) % m;   }   inline long long add(long long a, long long b, long long m = mod) {     a += b;     if (a >= m) a -= m;     if (a < 0) a += m;     return a;   }   inline long long power(long long a, long long b, long long m = mod) {     if (b == 0) return 1;     if (b == 1) return (a % m);     long long x = power(a, b / 2, m);     x = mul(x, x, m);     if (b % 2) x = mul(x, a, m);     return x;   }   bool check(string& k) {     int l = 0, r = k.size() - 1;     while (l < r) {       if (k[l] != k[r]) {         return false;       }       l++;       r--;     }     return true;   }   bool poss(string& k) {     map<char, int> mp;     int num = 0;     for (int i = 0; i < k.length(); i++) {       if (mp.find(k[i]) == mp.end()) {         mp[k[i]]++;         num++;       }     }     if (num == 1) return false;     return true;   }   int rec(string& s) {     int n = s.length();     if (!poss(s)) return -1;     if (!check(s)) return 0;     string k;     for (int i = 0; i < n / 2; i++) {       k += s[i];     }     bool ok = check(k);     if (!ok) {       if (n & 1) return 2;       return 1;     }     int ret = rec(k);     if (ret == -2) {       if (n & 1) return 2;       return 1;     }     if (ret == -1) {       if (n & 1) return -2;       return -1;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     if (0) {       freopen( C:  Users  Dishant  Desktop  Collection-DEV c++  input.txt ,  r ,               stdin);       freopen( C:  Users  Dishant  Desktop  Collection-DEV c++  output.txt ,  w ,               stdout);     }     string s;     cin >> s;     int n = s.length();     int ans = rec(s);     if (ans <= -1) {       cout <<  Impossible ;     } else       cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     cin >> n >> k;     if (n / k % 2)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int maxx(int fd, int sd) { return (fd > sd) ? fd : sd; }   int minn(int fd, int sd) { return (fd < sd) ? fd : sd; }   long long nod(long long a, long long b) {     if (a < b) {       return nod(b, a);     }     if (b == 0) {       return a;     }     return nod(b, a % b);   }   long long nok(long a, long b) {     long long result = (long long)a * (long long)b;     result = result / nod(a, b);     return result;   }   int main() {     long long n, k;     long long overallNOK = 1LL;     scanf( %I64d %I64d , &n, &k);     for (int i = 0; i < n; ++i) {       long long c;       scanf( %I64d , &c);       overallNOK = nok(nod(k, c), overallNOK);     }     printf( %s , (overallNOK % k == 0) ?  Yes  :  No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   const int maxx = 10005;   const int INF = 0x3f3f3f3f;   int n, m, k;   int vis[maxx];   int ans = 0, cnt = 0, pos = 0;   int l = 0, r = 0;   string a, b;   string s1, s2;   int main() {     scanf( %d%d , &n, &m);     cin >> a >> b;     if (n > m + 1) {       printf( NO n );       return 0;     }     int fk = 0;     int tag = 0;     for (int i = 0; i < n; i++) {       if (a[i] ==  * ) {         tag = 1;         continue;       }       if (tag == 0)         s1.push_back(a[i]);       else         s2.push_back(a[i]);     }     if (tag) {       fk = 0;       for (int i = 0; i < ((int)(s1).size()); i++) {         if (b[i] != s1[i]) fk = 1;       }       for (int i = ((int)(s2).size()) - 1; i >= 0; i--) {         if (b[m - ((int)(s2).size()) + i] != s2[i]) {           fk = 1;         }       }     } else {       if (n != m)         fk = 1;       else {         for (int i = 0; i < n; i++) {           if (a[i] != b[i]) {             fk = 1;             break;           }         }       }     }     if (a ==  * ) fk = 0;     if (fk)       printf( NO n );     else       printf( YES n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int b;     bool x;     edge *next;   } E[100010], *rt[100010];   int o = 0;   int mi = 100000000;   int N;   void addedge(int a, int b) {     edge *p = &E[o++];     p->b = b;     p->x = false;     p->next = rt[a];     rt[a] = p;     p = &E[o++];     p->b = a;     p->x = true;     p->next = rt[b];     rt[b] = p;   }   int S[10010];   bool jud[10010];   void init() {     for (int i = 1; i <= N; i++) {       jud[i] = false;       S[i] = 0;     }   }   void pdfs(int n) {     jud[n] = true;     for (edge *p = rt[n]; p != NULL; p = p->next) {       int b = p->b;       if (!jud[b]) {         pdfs(b);         S[n] += S[b];         if (p->x) S[n]++;       }     }   }   void dfs(int n, int s0, int s1) {     jud[n] = true;     int b, s, t0, t1;     for (edge *p = rt[n]; p != NULL; p = p->next) {       b = p->b;       if (!jud[b]) {         s = S[n];         mi = min(mi, s + s0);         t0 = s + s0 - S[b];         if (p->x)           s = S[n] - 1;         else           s = S[n] + 1;         mi = min(mi, s + s1);         t1 = s + s1 - S[b];         s = S[n] - (p->x == true);         mi = min(mi, s + s0);         t1 = min(t1, s + s0 - S[b]);         dfs(b, t0, t1);       }     }   }   int main() {     o = 0;     int i, j, k;     int a, b, s;     scanf( %d , &N);     if (N == 1) {       printf( 0 n );       return 0;     }     for (i = 0; i <= N; i++) {       rt[i] = NULL;     }     for (i = 1; i < N; i++) {       scanf( %d%d , &a, &b);       addedge(a, b);     }     for (i = 1; i <= N; i++) {       init();       pdfs(i);       for (j = 0; j <= N; j++) jud[j] = false;       dfs(i, 0, 0);     }     printf( %d n , mi);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long h[100000], ans[100000];     cin >> n;     for (int i = 0; i < n; i++) cin >> h[i];     ans[n - 1] = 0;     for (int i = n - 2; i >= 0; i--) {       if (h[i] <= h[i + 1])         ans[i] = h[i + 1] - h[i] + 1;       else         ans[i] = 0;       h[i] = max(h[i], h[i + 1]);     }     for (int i = 0; i < n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   int inp() {     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     int sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum;   }   int root[1000010];   struct President_Tree {     int lc[10000000], rc[10000000];     long long k[10000000], b[10000000];     int cnt;     void push_up(int cur) {       k[cur] = k[lc[cur]] + k[rc[cur]];       b[cur] = b[lc[cur]] + b[rc[cur]];     }     void insert(int &cur, int last, int x, int kc, int bc, int l, int r) {       cur = ++cnt;       if (l == r) {         k[cur] = k[last] + kc;         b[cur] = b[last] + bc;         return;       }       int mid = (l + r) >> 1;       if (x <= mid) {         insert(lc[cur], lc[last], x, kc, bc, l, mid);         rc[cur] = rc[last];       } else {         lc[cur] = lc[last];         insert(rc[cur], rc[last], x, kc, bc, mid + 1, r);       }       push_up(cur);     }     long long query(int cur, int last, int ql, int qr, long long x, int l,                     int r) {       if (l > qr || r < ql) return 0;       if (l >= ql && r <= qr) return x * (k[cur] - k[last]) + (b[cur] - b[last]);       int mid = (l + r) >> 1;       return query(lc[cur], lc[last], ql, qr, x, l, mid) +              query(rc[cur], rc[last], ql, qr, x, mid + 1, r);     }   } t;   int main() {     int n = inp();     for (int i = 1; i <= n; i++) {       int xl = inp();       int xr = inp();       int yl = inp();       int k = inp();       int b = inp();       int yr = inp();       t.insert(root[i], root[i - 1], 0, 0, yl, 0, 1e9);       t.insert(root[i], root[i], xl + 1, k, b - yl, 0, 1e9);       t.insert(root[i], root[i], xr + 1, -k, yr - b, 0, 1e9);     }     int m = inp();     long long lastans = 0;     while (m--) {       int l = inp();       int r = inp();       int x = (inp() + lastans) % 1000000000;       lastans = t.query(root[r], root[l - 1], 0, x, x, 0, 1e9);       printf( %lld n , lastans);     }   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     while (b) {       int temp = b;       b = a % b;       a = temp;     }     return a;   }   int main() {     int a, b, n, counting = 0, GCD;     scanf( %d %d %d , &a, &b, &n);     while (1) {       counting++;       GCD = gcd(a, n);       if (n < GCD) break;       n = n - GCD;       counting++;       GCD = gcd(b, n);       if (n < GCD) break;       n = n - GCD;     }     printf( %d n , counting % 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long date(int y, int m, int d) {     if (m < 3) y--, m += 12;     return (long long)365 * y + y / 4 - y / 100 + y / 400 + (153 * m - 457) / 5 +            d - 306;   }   int main() {     int y1, y2, m1, m2, d1, d2;     scanf( %d:%d:%d , &y1, &m1, &d1);     scanf( %d:%d:%d , &y2, &m2, &d2);     printf( %lld n , llabs(date(y1, m1, d1) - date(y2, m2, d2)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 Rand(time(0));   long long rnd(long long a) { return Rand() % a; }   long long rnd(long long a, long long b) { return Rand() % (b - a + 1) + a; }   double rnd_01d() { return (double)Rand() / (1ll << 32); }   double rnd_d(double r) { return rnd_01d() * r; }   const long long inf = 1e9 + 7;   void run() {     long long n;     cin >> n;     vector<long long> h(n);     for (long long i = 0; i < n; ++i) cin >> h[i];     long long l = -1, r = 1e9 + 3;     while (r - l > 1) {       long long mid = (l + r) / 2;       vector<long long> b;       copy(begin(h), end(h), back_inserter(b));       bool bad = false;       for (long long i = n - 1; i >= 2; --i) {         if (b[i] < mid) bad = true;         long long d = min(b[i] - mid, h[i]) / 3;         b[i - 1] += d;         b[i - 2] += 2 * d;       }       if (b[1] < mid || b[0] < mid || bad)         r = mid;       else         l = mid;     }     cout << l << endl;   }   signed main() {     cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0);     cout.precision(5);     long long t = 1;     cin >> t;     while (t--) {       run();     }   }


#include <bits/stdc++.h>   using namespace std;   int minimum(int x, int y) { return (x < y ? x : y); }   template <typename type>   struct even {     bool operator()(const type &x, const type &y) const { return x % 2 == 0; }   };   template <typename type>   struct odd {     bool operator()(const type &x, const type &y) const { return x % 2 != 0; }   };   bool flusher(int x) {     string s;     cout << x << endl;     fflush(stdout);     cin >> s;     return s ==  yes ;   }   long long int magic(string s) {     long long int sum = 0;     for (int i = 0; i < s.size(); i++) {       sum += s[i] -  0 ;     }     return sum;   }   long long int binaryToInt(string s) {     long long int n = s.length();     long long int res = 0;     long long int base = 1;     for (long long int i = n - 1; i >= 0; i--) {       if (s[i] ==  1 ) {         res += base;       }       base *= 2;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n, k;     cin >> n >> k;     int length = n * 2 + 1;     int res = 0;     int x[length];     for (int i = 0; i < length; i++) {       cin >> x[i];     }     for (int i = 1; i < length; i += 2) {       if (x[i] - 1 > x[i - 1] && x[i] - 1 > x[i + 1]) {         x[i]--;         res++;       }       if (res == k) {         break;       }     }     for (int i = 0; i < length; i++) {       cout << x[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     cout << (n * 3) / 2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 50;   long long kol, sum, id, ans;   string s;   vector<long long> a;   int main() {     cin >> s;     ans = 0;     sum = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  v ) {         id += 1;         if (i == s.size() - 1) {           sum += id - 1;         }       } else {         if (id != 0) {           sum += id - 1;           id = 0;         }         a.push_back(sum);         kol += 1;       }     }     if (a.size() == 0) {       cout << 0;       return 0;     }     for (int i = 0; i < a.size(); i++) {       ans += a[i] * (sum - a[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(NULL);     int n, x;     cin >> n;     while (n--) {       cin >> x;       if (x < 3 || x == 4 || x == 5 || x == 8 || x == 11)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     int ans = 0;     cin >> n >> m >> k;     int t;     for (int i = 0; i < n; ++i) {       cin >> t;       if (t == 1) {         if (m == 0)           ++ans;         else           --m;       } else {         if (m + k == 0)           ++ans;         else {           if (k)             --k;           else             --m;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 300005;   const int MAXINT = 1073741823;   vector<int> neighbors[MAXN];   int a[MAXN];   int greater_right[MAXN];   int greater_left[MAXN];   int less_right[MAXN];   int less_left[MAXN];   int dp[MAXN];   stack<int> s;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       while (!s.empty() && a[s.top()] < a[i]) s.pop();       greater_left[i] = (s.empty() ? -1 : s.top());       s.push(i);     }     stack<int>().swap(s);     for (int i = 0; i < n; i++) {       while (!s.empty() && a[s.top()] > a[i]) s.pop();       less_left[i] = (s.empty() ? -1 : s.top());       s.push(i);     }     stack<int>().swap(s);     for (int i = n - 1; i >= 0; i--) {       while (!s.empty() && a[s.top()] < a[i]) s.pop();       greater_right[i] = (s.empty() ? -1 : s.top());       s.push(i);     }     stack<int>().swap(s);     for (int i = n - 1; i >= 0; i--) {       while (!s.empty() && a[s.top()] > a[i]) s.pop();       less_right[i] = (s.empty() ? -1 : s.top());       s.push(i);     }     for (int i = 0; i < n; i++) {       if (greater_left[i] != -1) neighbors[greater_left[i]].push_back(i);       if (greater_right[i] != -1) neighbors[i].push_back(greater_right[i]);       if (less_left[i] != -1) neighbors[less_left[i]].push_back(i);       if (less_right[i] != -1) neighbors[i].push_back(less_right[i]);     }     fill(dp, dp + n, MAXINT);     dp[0] = 0;     for (int i = 0; i < n; i++)       for (int j : neighbors[i]) dp[j] = min(dp[j], dp[i] + 1);     cout << dp[n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const int N = 2e5 + 5;   int n;   vector<int> adj[N];   int root;   int degree[N], removed[N], level[N];   void dfs(int u, int p) {     for (auto v : adj[u]) {       if (v == p) continue;       level[v] = level[u] + 1;       dfs(v, u);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       if (x == 0) {         root = i;         continue;       }       adj[x].push_back(i);       adj[i].push_back(x);       degree[i]++, degree[x]++;     }     if (n % 2 == 0) {       cout <<  NO n ;       return 0;     }     dfs(root, 0);     set<pair<int, int> > s;     for (int i = 1; i <= n; i++) {       if (degree[i] % 2 == 0) s.insert({level[i], i});     }     vector<int> ans;     while (!s.empty()) {       auto top = *s.rbegin();       s.erase(prev(s.end()));       int node = top.second;       for (auto v : adj[node]) {         if (removed[v]) continue;         degree[v]--;         if (degree[v] % 2 == 1) {           s.erase(s.find({level[v], v}));         } else {           s.insert({level[v], v});         }       }       ans.push_back(node);       removed[node] = 1;     }     if (ans.size() == n) {       cout <<  YES n ;       for (auto i : ans) cout << i <<   n ;     } else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using ll = long long;   using ld = long double;   using ull = unsigned long long;   using namespace std;   const int N = 2e5 + 2, mod = 998244353;   int n, m, d[N][20];   pair<int, int> dist[N][2];   vector<int> g[2][N];   struct State {     int d, ver, B;     bool operator<(const State& oth) const { return d > oth.d; }   };   struct State1 {     int B, A, ver;     bool operator<(const State1& oth) const {       return B > oth.B || (B == oth.B && A > oth.A);     }   };   void solve1() {     priority_queue<State1> pq;     for (int i = (1); i <= (n); ++i) dist[i][0] = dist[i][1] = {1e9, 1e9};     dist[1][0] = {0, 0};     pq.push({0, 0, 1});     while (!pq.empty()) {       State1 cur = pq.top();       pq.pop();       int side = cur.B & 1;       if (pair<int, int>(cur.B, cur.A) != dist[cur.ver][side]) continue;       for (int v : g[side][cur.ver])         if (dist[v][side].first > cur.B ||             (dist[v][side].first == cur.B && dist[v][side].second > cur.A + 1)) {           dist[v][side] = {cur.B, cur.A + 1};           pq.push({cur.B, cur.A + 1, v});         }       if (dist[cur.ver][side ^ 1].first > cur.B + 1 ||           (dist[cur.ver][side ^ 1].first == cur.B + 1 &&            dist[cur.ver][side ^ 1].second > cur.A)) {         dist[cur.ver][side ^ 1] = {cur.B + 1, cur.A};         pq.push({cur.B + 1, cur.A, cur.ver});       }     }     pair<int, int> ans = dist[n][0];     if (dist[n][1].first < ans.first ||         (dist[n][1].first == ans.first && dist[n][1].second < ans.second))       ans = dist[n][1];     assert(ans.first != 1e9 && ans.second != 1e9);     int res = 1;     for (int i = (1); i <= (ans.first); ++i) res = res * 2LL % mod;     cout << (res + ans.second - 1) % mod;   }   void solve() {     int LG = 32 - __builtin_clz(m);     assert(LG <= 19);     priority_queue<State> pq;     pq.push({0, 1, 0});     for (int i = (1); i <= (n); ++i)       for (int j = (0); j <= (19); ++j) d[i][j] = 1e9;     d[1][0] = 0;     while (!pq.empty()) {       State cur = pq.top();       pq.pop();       if (cur.d != d[cur.ver][cur.B]) continue;       for (int v : g[cur.B & 1][cur.ver])         if (d[v][cur.B] > cur.d + 1) {           d[v][cur.B] = cur.d + 1;           pq.push({d[v][cur.B], v, cur.B});         }       if (cur.B < LG) {         if (d[cur.ver][cur.B + 1] > cur.d + (1 << cur.B)) {           d[cur.ver][cur.B + 1] = cur.d + (1 << cur.B);           pq.push({d[cur.ver][cur.B + 1], cur.ver, cur.B + 1});         }       }     }     int ans = 1e9;     for (int i = (0); i <= (LG); ++i)       if (ans > d[n][i]) ans = d[n][i];     if (ans == 1e9)       solve1();     else       cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m;     for (int i = (1); i <= (m); ++i) {       int u, v;       cin >> u >> v;       g[0][u].emplace_back(v);       g[1][v].emplace_back(u);     }     solve();     return 0;   }


#include <iostream>   #include <algorithm>   #include <cassert>   #include <fstream>      using ll = long long;   const int maxn = 5007;   ll t, n, S[maxn], ans[maxn];   ll res;      int main(int argc, char *argv[]) {      std::ios::sync_with_stdio(false);    std::cin.tie(0);     std::cin >> t;    while (t--) {     res = 0;     std::cin >> n;     for (ll i = 0; i < n; i++) {      std::cin >> S[i];      ans[i] = 0;     }     for (ll i = 0; i < n; i++) {      ll temp = ans[i];      if (temp < S[i]-1){       res += S[i] - 1 - temp;       temp += S[i] - 1 - temp;      }      ans[i+1] += temp - S[i] + 1;      if (i+2 < n) {       for (int j = i+2; j < std::min(n,i+S[i]+1); j++) {        ans[j]++;       }      }      }     std::cout << res <<   n ;    }   }


#include <bits/stdc++.h>   using namespace std;   int n, k, pos;   long long a[200005], sum[200005], ans;   vector<long long> vec;   int main() {     cin.tie(0), ios::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       sum[i] = a[i];       sum[i] += sum[i - 1];       if (a[i] > 1) vec.push_back(i);     }     a[n + 1] = 1;     vec.push_back(n + 1);     for (int i = 1; i <= n; i++) {       pos = i;       long long tich = a[i];       while (1) {         int it = lower_bound(vec.begin(), vec.end(), pos + 1) - vec.begin();         int l = pos, r = vec[it] - 1, mid;         if (tich % k == 0) {           long long val = tich / k;           while (l < r) {             mid = (l + r) / 2;             if (sum[mid] - sum[i - 1] >= val)               r = mid;             else               l = mid + 1;           }           if (l >= pos && sum[l] - sum[i - 1] == val) {             ans++;           }         }         long long amount = LLONG_MAX / a[vec[it]];         if (tich >= amount) break;         tich *= a[vec[it]];         pos = vec[it];         if (pos == n + 1) break;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int a[300000];   int ans[300000];   map<int, int> m;   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n * n; i++) {       scanf( %d , &a[i]);       m[a[i]]++;     }     sort(a, a + n * n);     int k = 0;     for (int i = n * n - 1; i >= 0; i--) {       if (m[a[i]] > 0) {         ans[k++] = a[i];         for (int j = 0; j < k; j++) {           m[gcd(a[i], ans[j])] -= 2;         }       }     }     for (int i = 0; i < k - 1; i++) {       printf( %d  , ans[i]);     }     printf( %d , ans[k - 1]);     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long int, long long int>> v1;   int main() {     long long int n, i, A[300005], minn, t1, a1, i1;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> A[i];       v1.push_back(make_pair(A[i], i));     }     minn = 10000000000;     for (i = 2; i <= n; i++) {       t1 = min(A[1], A[i]) / (i - 1);       if (t1 < minn) minn = t1;     }     for (i = n - 1; i >= 1; i--) {       t1 = min(A[n], A[i]) / (n - i);       if (t1 < minn) minn = t1;     }     sort(v1.begin(), v1.end());     a1 = v1[0].first;     i1 = v1[0].second;     for (i = 1; i < v1.size(); i++) {       t1 = min(a1, v1[i].first) / (abs(i1 - v1[i].second));       if (t1 < minn) minn = t1;     }     cout << minn << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 2e5 + 10;   const long long oo = 1e18;   char s[maxN];   long long cost[maxN][2];   int main() {     int test;     scanf( %d , &test);     while (test--) {       int n, a, b;       scanf( %d%d%d%*c , &n, &a, &b);       gets(s);       cost[0][0] = b;       cost[0][1] = oo;       for (int i = 0; i < n; i++) {         if (s[i] ==  0 ) {           cost[i + 1][0] = min(cost[i][0] + a + b, cost[i][1] + a * 2 + b);           cost[i + 1][1] = min(cost[i][0] + 2 * (a + b), cost[i][1] + a + b * 2);         } else {           cost[i + 1][1] = cost[i][1] + a + b * 2;           cost[i + 1][0] = oo;         }       }       printf( %lld n , cost[n][0]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[100001], g[100001], h[100001], m;   set<int> events[100001];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) events[i].clear();     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), events[a[i]].insert(i);     m = 0;     bool ok = true;     for (int i = 1; i <= n; ++i)       if (!events[i].empty())         if (events[i].find(i) == events[i].end()) {           ok = false;           break;         }     if (!ok) {       printf( -1 n );       return 0;     }     for (int i = 1; i <= n; i++)       if (!events[i].empty()) {         h[++m] = i;         for (set<int>::iterator itr = events[i].begin(); itr != events[i].end();              ++itr)           g[*itr] = m;       }     printf( %d n , m);     for (int i = 1; i <= n; i++) printf( %d  , g[i]);     printf(  n );     for (int i = 1; i <= m; i++) printf( %d  , h[i]);     printf(  n );   }


#include<bits/stdc++.h>   using namespace std;   long long a,b,c,d,e,i,j,ii,jj,zx,xc,tes,t,f[200009],A[200009],B[200009],rg[200009][20],rgx[200009][20],lf[200009][20],lfx[200009][20],P[200009],pas;   stack <long long> st;   set <long long> s;   set <long long>::iterator it,lft,rgt;   long long uprg(long long q, long long w){    if(q>=w) return 0;    long long jm=0;    long long h=0;    for(h=18; h>=0; h--){     if(rg[q][h]!=0&&rg[q][h]<w){      jm+=rgx[q][h];      q=rg[q][h];     }    }    jm+=rgx[q][0];    return jm;   }   long long uplf(long long q, long long w){    if(w<=q) return 0;    long long jm=0;    long long h=0;    for(h=19; h>=0; h--){     if(lf[w][h]!=0&&lf[w][h]>q){      jm+=lfx[w][h];      w=lf[w][h];     }    }    jm+=lfx[w][0];    return jm;   }   long long fun(long long q, long long w){    if(q>=w) return 0;    long long jm=0;    long long h=0;    for(h=18; h>=0; h--){     if(rg[q][h]!=0&&rg[q][h]<=w){      jm+=rgx[q][h];      q=rg[q][h];     }    }    if(q==w) return jm;    jm+=uplf(q,w);    return jm;   }   int main(){    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);    cin>>a>>tes;    for(i=1; i<=a; i++) cin>>f[i];    for(i=1; i<=a; i++) cin>>A[i];    for(i=1; i<=a; i++) cin>>B[i];    while(st.size()) st.pop();    st.push(a+1);    f[a+1]=a+2;    for(i=a; i>=1; i--){     while(st.size()>0&&f[st.top()]<f[i]) st.pop();     if(st.size()==0){           }else{      rg[i][0]=st.top();      rgx[i][0]=min(B[i],A[i]+uprg(i+1,st.top()));     }     for(j=1; j<=18; j++){      rg[i][j]=rg[rg[i][j-1]][j-1];      rgx[i][j]=rgx[i][j-1]+rgx[rg[i][j-1]][j-1];     }     st.push(i);    }    while(st.size()) st.pop();    for(i=1; i<=a; i++){     while(st.size()>0&&f[st.top()]<f[i]) st.pop();     if(st.size()==0){           }else{      lf[i][0]=st.top();      lfx[i][0]=A[st.top()]+uprg(st.top()+1,i);     }     for(j=1; j<=18; j++){      lf[i][j]=lf[lf[i][j-1]][j-1];      lfx[i][j]=lfx[i][j-1]+lfx[lf[i][j-1]][j-1];     }     st.push(i);    }    s.insert(1);    s.insert(a+1);    P[1]=fun(1,a+1);    pas+=P[1];    for(t=1; t<=tes; t++){     cin>>c;     if(c==1){      cout<<pas<<endl;      continue;     }     it=s.lower_bound(c);     if(it==s.end()||(*it)!=c){      P[c]=fun(c,(*it));      pas+=P[c];      lft=it;lft--;      pas-=P[(*lft)];      P[(*lft)]=fun((*lft),c);      pas+=P[(*lft)];      s.insert(c);     }else{      lft=it;lft--;      pas-=P[(*lft)];      rgt=it;rgt++;      pas-=P[c];      P[(*lft)]=fun((*lft),(*rgt));      pas+=P[(*lft)];      s.erase(it);     }     cout<<pas<<endl;    }    P[0]=2*6;    pas=P[0]+5;    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve(int n, int m, int k) {     if (n > m) return 0;     if (n == 0) return min(m + 1, (1 << (k + 1)));     if ((1 << k) & n) {       int c = solve(n - (1 << k), m - (1 << k), k - 1);       if (c == (1 << k))         return (1 << (k + 1));       else         return c;     } else {       int z = solve(n, m - (1 << k), k - 1);       if (z == (1 << k))         return 2 * z;       else {         int c = solve(n, m, k - 1);         if (c == (1 << k))           return (1 << k) + z;         else           return c;       }     }   }   void solve() {     int n, m;     cin >> n >> m;     cout << solve(n, m, 29) <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   bool f = 0;   const double eps = 1e-8;   int n;   struct point {     double x, y;   } p[100010];   inline int rd() {     int x = 0;     bool f = 0;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = 1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     return f ? -x : x;   }   inline bool valid(double x) {     double l = -(double)1e18, r = (double)1e18, dlt;     for (register int i = 1; i <= n; ++i) {       if (p[i].y > x + x) return 0;       dlt = sqrt(p[i].y * (2 * x - p[i].y));       l = max(l, p[i].x - dlt);       r = min(r, p[i].x + dlt);     }     return (r - l + eps >= 0.0);   }   int main() {     n = rd();     for (register int i = 1; i <= n; ++i) {       p[i].x = (double)rd();       p[i].y = (double)rd();       if (p[i].y < 0.0) f = 1;     }     if (f == 1)       for (register int i = 1; i <= n; ++i) {         p[i].y = -p[i].y;         if (p[i].y < 0.0) {           puts( -1 );           return 0;         }       }     int t = 0;     register double l = 0.0, r = (double)1e18, mid;     while (t <= 300) {       mid = (l + r) / 2.0;       ++t;       valid(mid) ? r = mid : l = mid;     }     printf( %.10lf , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const long long inf = 2e9 + 10;   template <typename T1, typename T2>   static void relax(T1 &x, T2 y) {     if (x > y) x = y;   }   template <typename T1, typename T2>   static void gain(T1 &x, T2 y) {     if (x < y) x = y;   }   long long powmod(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   inline void add(int &x, int y) {     x += y;     if (x >= mod) x -= mod;   }   inline void sub(int &x, int y) {     x -= y;     if (x < 0) x += mod;   }   const int maxl = 2e3 + 3;   int m, d;   string a, b;   int dp[maxl][maxl][2];   int solve(string s) {     int n = (int)s.length();     for (int(i) = 0; (i) < (int)(n + 1); ++(i))       for (int(j) = 0; (j) < (int)(m); ++(j))         for (int(k) = 0; (k) < (int)(2); ++(k)) dp[i][j][k] = 0;     dp[0][0][1] = 1;     for (int(i) = 0; (i) < (int)(n); ++(i))       for (int(j) = 0; (j) < (int)(m); ++(j))         for (int(k) = 0; (k) < (int)(2); ++(k))           for (int dgt = 0; dgt <= (k ? int(s[i] -  0 ) : 9); ++dgt) {             if ((i & 1) && dgt != d) continue;             if (!(i & 1) && dgt == d) continue;             int ni = i + 1;             int nj = ((j * 10 + dgt) % m);             int nk = k && (dgt == int(s[i] -  0 ));             add(dp[ni][nj][nk], dp[i][j][k]);           }     int ans = 0;     for (int(k) = 0; (k) < (int)(2); ++(k)) add(ans, dp[n][0][k]);     return ans;   }   bool ok(string s) {     int n = (int)s.length();     int rem = 0;     for (int(i) = 0; (i) < (int)(n); ++(i)) {       int dgt = int(s[i] -  0 );       if ((i & 1) && dgt != d) return false;       if (!(i & 1) && dgt == d) return false;       rem = (rem * 10 + dgt) % m;     }     return !rem;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> m >> d;     cin >> a;     cin >> b;     int ans = 0;     add(ans, solve(b));     sub(ans, solve(a));     add(ans, ok(a));     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200010];   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int f = 0;     for (int i = 1; i < n; i++)       if (a[i] != a[i - 1]) {         f = 1;         break;       }     if (f == 0) {       if (a[0] == 0) {         cout <<  YES  << endl;         for (int i = 0; i < n; i++) cout <<  1  <<    ;         exit(0);       }       cout <<  NO  << endl;       exit(0);     }     long long mx = 0, mxer = 0;     for (int i = 0; i < n; i++)       if (a[i] > mx) mx = a[mxer = i];     while (true) {       int x = mxer - 1;       if (x < 0) x += n;       if (a[x] == a[mxer])         mxer = x;       else         break;     }     int flag = 1;     for (int i = 1; i < n; i++) {       int x = mxer - 1;       if (x < 0) x += n;       if (flag && a[x] == 0) {         a[x] += a[mxer];         flag = 0;       }       a[x] += a[mxer];       mxer = x;     }     cout <<  YES  << endl;     for (int i = 0; i < n; i++) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int(a), int(b)) { return ((a) % (b) ? gcd((b), (a) % (b)) : (b)); }   long long lgcd(long long(a), long long(b)) {     return ((a) % (b) ? gcd((b), (a) % (b)) : (b));   }   int lcm(int a, int b) { return (a) / gcd((a), (b)) * b; }   long long llcm(long long a, long long b) { return (a) / lgcd((a), (b)) * b; }   int n, m, ans[100010], crime[100010], notcrime[100010];   int main() {     memset(ans, 0, sizeof(ans));     memset(crime, 0, sizeof(crime));     memset(notcrime, 0, sizeof(notcrime));     scanf( %d%d , &n, &m);     getchar();     for (int i = 1; i <= n; i++) {       char c;       int temp;       scanf( %c%d , &c, &temp);       getchar();       if (c ==  + ) {         crime[temp]++;         ans[i] = temp;       } else {         notcrime[temp]++;         ans[i] = -temp;       }     }     int nnc = 0;     for (int i = 1; i <= n; i++) nnc += notcrime[i];     int nc = 0;     bool cc[100010];     memset(cc, false, sizeof(cc));     for (int i = 1; i <= n; i++) {       if (crime[i] + nnc - notcrime[i] == m) {         cc[i] = true;         nc++;       }     }     for (int i = 1; i <= n; i++) {       if (ans[i] > 0) {         if (cc[((ans[i]) > 0 ? (ans[i]) : -(ans[i]))]) {           if (nc > 1)             printf( Not defined n );           else             printf( Truth n );         } else           printf( Lie n );       } else {         if (cc[((ans[i]) > 0 ? (ans[i]) : -(ans[i]))]) {           if (nc == 1)             printf( Lie n );           else             printf( Not defined n );         } else           printf( Truth n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, -1, 0, 1};   const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1};   const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1};   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     vector<vector<long long>> dp(n, vector<long long>(n));     vector<vector<long long>> sum(n, vector<long long>(n));     for (long long i = 0; i < n; i++) cin >> a[i];     sort(a.begin(), a.end());     vector<long long> s(n), l(n);     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < n; j++) {         if (a[i] >= 2 * a[j]) {           l[i] = j;           s[i]++;         }       }     }     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < n; j++) {         if (j == 0) {           dp[i][j] = 1;           long long x = i - 1 >= 0 ? sum[i - 1][j] : 0;           sum[i][j] = x + dp[i][j];           sum[i][j] %= 998244353;           continue;         }         if (s[i] - j + 1 > 0) {           dp[i][j] =               (dp[i][j - 1] * (s[i] - j + 1)) % 998244353 + sum[l[i]][j - 1];           dp[i][j] %= 998244353;         } else           dp[i][j] = 0;         long long x = i - 1 >= 0 ? sum[i - 1][j] : 0;         sum[i][j] = x + dp[i][j];         sum[i][j] %= 998244353;       }     }     cout << dp[n - 1][n - 1] <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();   }


#include <bits/stdc++.h>   using namespace std;   template <class node>   struct link_cut_tree {     bool connected(node* u, node* v) { return lca(u, v) != NULL; }     int depth(node* u) {       access(u);       return get_sz(u->ch[0]);     }     node* get_root(node* u) {       access(u);       while (u->ch[0]) u = u->ch[0], u->push();       return access(u), u;     }     node* ancestor(node* u, int k) {       k = depth(u) - k;       assert(k >= 0);       for (;; u->push()) {         int sz = get_sz(u->ch[0]);         if (sz == k) return access(u), u;         if (sz < k)           k -= sz + 1, u = u->ch[1];         else           u = u->ch[0];       }       assert(0);     }     node* lca(node* u, node* v) {       if (u == v) return u;       access(u);       access(v);       if (!u->p) return NULL;       u->splay();       return u->p ?: u;     }     void link(node* u, node* v) {       make_root(v);       access(u);       set_link(v, u, 0);       v->update();     }     void cut(node* u) {       access(u);       u->ch[0]->p = NULL;       u->ch[0] = NULL;       u->update();     }     void cut(node* u, node* v) { cut(depth(u) > depth(v) ? u : v); }     void make_root(node* u) {       access(u);       u->reverse();       access(u);       assert(!u->ch[0] && !u->ch[1]);     }     void access(node* u) {       for (node *v = u, *pre = NULL; v; v = v->p) {         v->splay();         if (pre) v->update_vsub(pre, false);         if (v->ch[1]) v->update_vsub(v->ch[1], true);         v->ch[1] = pre;         v->update();         pre = v;       }       u->splay();       assert(!u->ch[1]);     }     node* operator[](int i) { return &data[i]; }     int operator[](node* i) { return i - &data[0]; }     vector<node> data;     link_cut_tree(int n) : data(n) {}   };   template <typename pnode>   struct splay_tree {     pnode ch[2], p;     splay_tree() { ch[0] = ch[1] = p = NULL; }     int dir() {       if (!p) return -2;       if (p->ch[0] == this) return 0;       if (p->ch[1] == this) return 1;       return -1;     }     bool is_root() { return dir() < 0; }     friend void set_link(pnode u, pnode v, int d) {       if (v) v->p = u;       if (d >= 0) u->ch[d] = v;     }     void rotate() {       assert(!is_root());       int x = dir();       pnode g = p;       set_link(g->p, static_cast<pnode>(this), g->dir());       set_link(g, ch[x ^ 1], x);       set_link(static_cast<pnode>(this), g, x ^ 1);       g->update();     }     void splay() {       const pnode& self = static_cast<pnode>(this);       while (!is_root() && !p->is_root()) {         p->p->push(), p->push(), self->push();         dir() == p->dir() ? p->rotate() : self->rotate();         self->rotate();       }       if (!is_root()) p->push(), self->push(), self->rotate();       self->push();       self->update();     }   };   template <typename pnode, class splay_tree_vchs>   struct splay_tree_lct : splay_tree<pnode> {     splay_tree_vchs vnode;     using splay_tree<pnode>::ch;     bool rev;     splay_tree_vchs* root;     splay_tree_lct() : splay_tree<pnode>() {       root = NULL;       rev = 0;     }     void update() {}     void push() {       if (rev) {         if (ch[0]) ch[0]->reverse();         if (ch[1]) ch[1]->reverse();         rev = 0;       }     }     void rotate() {       swap(vnode, this->p->vnode);       splay_tree<pnode>::rotate();     }     void splay() {       auto v = &vnode;       bool r = !v->key;       splay_tree<pnode>::splay();       if (r && v->key) {         if (v->p) v->p->ch[v->p->ch[1] == &v->key->vnode] = v;         if (v->ch[0]) v->ch[0]->p = v;         if (v->ch[1]) v->ch[1]->p = v;         v->key = static_cast<pnode>(this);       }     }     void reverse() {       rev ^= 1;       swap(ch[0], ch[1]);     }     void update_vsub(pnode v, bool add) {       auto& u = root;       if (add) {         v->vnode = splay_tree_vchs(v);         if (!u) {           u = &v->vnode;           return;         }         while (u->ch[1]) u = u->ch[1];         u->ch[1] = &v->vnode;         u->ch[1]->p = u;         u = u->ch[1];         u->splay();       } else {         auto x = &v->vnode;         x->splay();         if (!x->ch[0]) {           u = x->ch[1];         } else if (!x->ch[1]) {           u = x->ch[0];         } else {           u = x->ch[0];           u->p = NULL;           while (u->ch[1]) u = u->ch[1];           u->ch[1] = x->ch[1];           x->ch[1]->p = u;           u->splay();         }         if (u) u->p = NULL;         x->key = NULL;       }     }   };   const int inf = -1e8;   template <typename pnode>   struct splay_tree_vchs : splay_tree<splay_tree_vchs<pnode>*> {     using splay_tree<splay_tree_vchs<pnode>*>::ch;     pnode key;     int x;     array<int, 4> y;     splay_tree_vchs(){};     splay_tree_vchs(const pnode& key)         : splay_tree<splay_tree_vchs<pnode>*>(), key(key) {       build();     }     void build() {       x = key->x;       y[0] = key->pp[0];       y[1] = 0;       y[2] = key->pp[1];       y[3] = inf;     }     void update() {       build();       for (int c = 0; c < 2; ++c)         if (ch[c]) {           x = max(x, ch[c]->x);           for (int i = 0; i < 4; i += 2) {             if (ch[c]->y[i] > y[i]) {               y[i + 1] = y[i];               y[i] = ch[c]->y[i];               if (ch[c]->y[i + 1] > y[i + 1]) y[i + 1] = ch[c]->y[i + 1];             } else if (ch[c]->y[i] > y[i + 1])               y[i + 1] = ch[c]->y[i];           }         }     }     void push() {}   };   const array<int, 4> ainf = {0, inf, 0, inf};   struct node : splay_tree_lct<node*, splay_tree_vchs<node*>> {     bool val, len, sval;     int slen;     int x;     array<int, 4> pp;     node() : splay_tree_lct() {       val = len = sval = 0;       slen = x = 0;       pp = ainf;     }     void update() {       splay_tree_lct::update();       x = 0;       sval = val;       slen = len;       array<int, 4> up = ainf, dw = ainf;       array<int, 2> v1 = {0, inf}, v2 = v1;       bool upsval = val;       int upslen = len;       bool dwsval = val;       int dwslen = len;       if (ch[0]) {         x = max(x, ch[0]->x);         up = ch[0]->pp;         sval ^= ch[0]->sval;         slen += ch[0]->slen;         upsval ^= ch[0]->sval;         upslen += ch[0]->slen;       }       if (ch[1]) {         x = max(x, ch[1]->x);         dw = ch[1]->pp;         sval ^= ch[1]->sval;         slen += ch[1]->slen;         dwsval ^= ch[1]->sval;         dwslen += ch[1]->slen;       }       if (root) {         x = max(x, root->x);         v1[0] = root->y[0];         v1[1] = root->y[2];         v2[0] = root->y[1];         v2[1] = root->y[3];       }       pp[0] = up[0], pp[1] = up[1];       pp[2] = dw[2], pp[3] = dw[3];       for (int i = 0; i < 2; ++i) {         x = max(x, len + max({up[2 + i] + dw[i ^ val], up[2 + i] + v1[i ^ val],                               dw[i] + v1[i ^ val], v1[i] + v2[i ^ val]}));         pp[i ^ upsval] = max(pp[i ^ upsval], upslen + max(dw[i], v1[i]));         pp[2 + (i ^ dwsval)] =             max(pp[2 + (i ^ dwsval)], dwslen + max(up[2 + i], v1[i]));       }     }     void push() { splay_tree_lct::push(); }     void reverse() {       splay_tree_lct::reverse();       swap(pp[0], pp[2]);       swap(pp[1], pp[3]);     }   };   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int n;     cin >> n;     link_cut_tree<node> lct(2 * n);     vector<vector<tuple<int, bool, int>>> adj(n);     for (int i = 1, u, v, w; i < n; ++i) {       cin >> u >> v >> w;       --u, --v;       adj[u].push_back({v, w, i});       adj[v].push_back({u, w, i});     }     function<void(int, int)> dfs = [&](int u, int p) {       for (auto [v, w, i] : adj[u])         if (v != p) {           dfs(v, u);           lct[n + i]->val = w;           lct[n + i]->len = true;           lct[n + i]->update();           lct.link(lct[u], lct[n + i]);           lct.link(lct[n + i], lct[v]);         }     };     dfs(0, -1);     int q;     cin >> q;     for (int i = 0, u; i < q; ++i) {       cin >> u;       lct.access(lct[n + u]);       lct[n + u]->val ^= 1;       lct[n + u]->update();       cout << lct[n + u]->x <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, map<int, int> > m;   long long int lol(long long int a, long long int r) {     if (a >= 0 && r <= 0) return a;     if (a <= 0 && r >= 0) return a;     return a % r;   }   int main() {     long long int a, b;     cin >> a >> b;     string s;     cin >> s;     long x = 0;     long y = 0;     if (a == 0 && b == 0) {       cout <<  Yes  << endl;       return 0;     }     for (long i = 0; i < s.size(); i++) {       if (s[i] ==  L ) x--;       if (s[i] ==  R ) x++;       if (s[i] ==  U ) y++;       if (s[i] ==  D ) y--;     }     long p = 2;     long w = 0;     long q = 0;     int d = 0;     for (long i = 0; i < s.size(); i++) {       if (s[i] ==  L ) w--;       if (s[i] ==  R ) w++;       if (s[i] ==  U ) q++;       if (s[i] ==  D ) q--;       long long int u = a - w;       long long int e = b - q;       if (x != 0) {         if (y != 0) {           long long int v = u / x;           long long int z = e / y;           if (v == z && v >= 0 && u % x == 0 && e % y == 0) d = 1;         } else {           long long int v = a / x;           if (e == 0 && v >= 0 && u % x == 0) d = 1;         }       } else {         if (y != 0) {           long long int z = e / y;           if (z >= 0 && u == 0 && e % y == 0) d = 1;         } else {           if (u == 0 && e == 0) d = 1;         }       }     }     if (d == 1)       cout <<  Yes  << endl;     else       cout <<  No  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int V = 100100;   int l[V], r[V], a[V], ret, n;   int main() {     while (~scanf( %d , &n)) {       ret = 0;       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       for (int i = 1; i <= n; i++) l[i] = r[i] = i;       for (int i = 1; i <= n; i++) {         while (l[i] != 1 && a[l[i] - 1] < a[i]) l[i] = l[l[i] - 1];         if (l[i] != 1) ret = max(ret, a[i] ^ a[l[i] - 1]);       }       for (int i = n; i >= 1; i--) {         while (r[i] != n && a[r[i] + 1] < a[i]) r[i] = r[r[i] + 1];         if (r[i] != n) ret = max(ret, a[i] ^ a[r[i] + 1]);       }       printf( %d n , ret);     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 17;   int n;   vector<int> adj[(1 << MAX_N) + 9];   bool used[(1 << MAX_N) + 9];   int cnt[(1 << MAX_N) + 9];   vector<int> vRoot;   vector<int> ans;   void buildCnt(int x) {     used[x] = true;     cnt[x] = 1;     for (int y : adj[x]) {       if (!used[y]) {         buildCnt(y);         cnt[x] += cnt[y];       }     }     used[x] = false;   }   void findRoot(int x, vector<int>& vRoot) {     used[x] = true;     for (int y : adj[x]) {       if (!used[y]) {         findRoot(y, vRoot);       }     }     if ((1 << n) - 2 - cnt[x] == (1 << n - 1) - 1) {       vRoot.push_back(x);     } else {       for (int y : adj[x]) {         if (!used[y]) {           if (cnt[y] == (1 << n - 1) - 1) {             vRoot.push_back(x);             break;           }         }       }     }     used[x] = false;   }   bool checkTree1(int x, int depth) {     used[x] = true;     bool result = false;     if (cnt[x] != (1 << depth) - 1) {       result = false;     } else if (depth == 1) {       result = true;     } else {       result = true;       for (int y : adj[x]) {         if (!used[y]) {           if (!checkTree1(y, depth - 1)) {             result = false;             break;           }         }       }     }     used[x] = false;     return result;   }   int checkTree2(int x, int depth) {     used[x] = true;     int result = -1;     if (cnt[x] != (1 << depth) - 2) {       result = -1;     } else if (depth == 2) {       result = x;     } else {       vector<pair<int, int>> children;       for (int y : adj[x]) {         if (!used[y]) {           children.push_back(make_pair(cnt[y], y));         }       }       sort(children.begin(), children.end());       if (children.size() == 2) {         int tmp = checkTree2(children[0].second, depth - 1);         if (tmp != -1 && checkTree1(children[1].second, depth - 1)) {           result = tmp;         } else {           result = -1;         }       } else if (children.size() == 3) {         if (checkTree1(children[0].second, depth - 2) &&             checkTree1(children[1].second, depth - 2) &&             checkTree1(children[2].second, depth - 1)) {           result = x;         } else {           result = -1;         }       } else {         result = -1;       }     }     used[x] = false;     return result;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 1; i <= (1 << n) - 3; i++) {       int x;       int y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     buildCnt(1);     findRoot(1, vRoot);     for (int root : vRoot) {       buildCnt(root);       int x = checkTree2(root, n);       if (x != -1) {         ans.push_back(x);       }     }     sort(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (int x : ans) {       cout << x <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, maxx, idx, max1, s;   int main(int argc, char *argv[]) {     pair<int, int> a[10000];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = 0;     }     maxx = 0;     max1 = 0;     for (int i = 0; i < n; i++) {       if (a[i].first > maxx) {         max1 = maxx;         maxx = a[i].first;         idx = i;       } else if (max1 < a[i].first)         max1 = a[i].first;     }     cout << idx + 1 <<     << max1 << endl;     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   long long z, mn, mx, sum, ans, k, pos, pos1, r, xx, yy, zz, n, m, x, y;   vector<int> v;   bool OK[1000002], KO[1000002], zoom;   int b[100003], c[100002];   map<char, int> mp, mr;   set<int> st;   int main() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> x >> y >> k;       cout << ((k + 1) / 2 * x) - (k / 2 * y) <<   n ;     }   }