#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2000;   int cubeta[MAXN + 2];   int color;   int arr[MAXN + 2];   void mapea(int n) {     set<int> nums;     for (int i = 1; i <= n; i++) nums.insert(arr[i]);     map<int, int> val;     int cnt = 1;     set<int>::iterator it = nums.begin();     while (it != nums.end()) {       val[*it] = cnt;       cnt++;       it++;     }     for (int i = 1; i <= n; i++) arr[i] = val[arr[i]];   }   int busca(int l, int n) {     int r = n;     while (r > l && cubeta[arr[r]] != color) {       cubeta[arr[r]] = color;       r--;     }     return r + 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> arr[i];     mapea(n);     int resp = n;     for (int l = 0; l <= n; l++) {       color++;       bool continuar = true;       for (int i = 1; i <= l; i++) {         if (cubeta[arr[i]] == color) continuar = false;         cubeta[arr[i]] = color;       }       if (!continuar) break;       int r = busca(l, n);       resp = min(resp, r - l - 1);     }     cout << resp <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int res = -1;     for (int i = 1; i < s.size(); i++) {       for (int j = 1; i + j < s.size(); j++) {         int p1;         string P1 = s.substr(0, i);         if (P1.size() > 1 && P1[0] ==  0 )           p1 = -1;         else if (P1.size() >= 7 && P1 !=  1000000 )           p1 = -1;         else           istringstream(P1) >> p1;         int p2;         string P2 = s.substr(i, j);         if (P2.size() > 1 && P2[0] ==  0 )           p2 = -1;         else if (P2.size() >= 7 && P2 !=  1000000 )           p2 = -1;         else           istringstream(P2) >> p2;         int p3;         string P3 = s.substr(i + j, s.size());         if (P3.size() > 1 && P3[0] ==  0 )           p3 = -1;         else if (P3.size() >= 7 && P3 !=  1000000 )           p3 = -1;         else           istringstream(P3) >> p3;         if (p1 == -1 || p2 == -1 || p3 == -1) continue;         res = max(res, p1 + p2 + p3);       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using PII = pair<int, int>;   int main() {     auto get_factors = [](int n) {       vector<int> ret;       for (int i = 1; i * i <= n; i++) {         if (n % i == 0) {           ret.push_back(i);           if (i * i != n) ret.push_back(n / i);         }       }       sort(ret.begin(), ret.end());       return ret;     };     vector<vector<int>> factors(1e5 + 1);     for (int i = (1); i <= (1e5); ++i) factors[i] = get_factors(i);     ios::sync_with_stdio(false);     int tc;     cin >> tc;     while (tc--) {       int x[3], cnt[8] = {0};       cin >> x[0] >> x[1] >> x[2];       vector<int> v;       for (int i = 0; i < 3; i++) {         for (auto val : factors[x[i]]) v.push_back(val);       }       sort(v.begin(), v.end());       v.erase(unique(v.begin(), v.end()), v.end());       for (auto val : v) {         int mask = 0;         for (int i = 0; i < 3; i++) {           if (x[i] % val == 0) mask |= 1 << i;         }         cnt[mask]++;       }       int ans = 0;       for (int a = 1; a < 8; a++) {         for (int b = a; b < 8; b++) {           int t1 = a | b;           if (t1 == (t1 & -t1)) continue;           for (int c = b; c < 8; c++) {             int t2 = a | c, t3 = b | c;             if (t2 == (t2 & -t2)) continue;             if (t3 == (t3 & -t3)) continue;             if ((a | b | c) != 7) continue;             if (a != b && a != c && b != c) {               ans += cnt[a] * cnt[b] * cnt[c];             } else if (a == b && b == c) {               int n = cnt[a] + 2;               ans += n * (n - 1) * (n - 2) / 6;             } else if (a == b) {               ans += cnt[a] * (cnt[a] + 1) / 2 * cnt[c];             } else if (a == c) {               ans += cnt[a] * (cnt[a] + 1) / 2 * cnt[b];             } else if (b == c) {               ans += cnt[b] * (cnt[b] + 1) / 2 * cnt[a];             }           }         }       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, i, name[100], cnt;     string str;     char ch;     cin >> n >> k;     cnt = 0;     for (i = 1; i <= k - 1; i++) {       name[i] = ++cnt;     }     for (i = k; i <= n; i++) {       cin >> str;       if (str[0] ==  N ) {         name[i] = name[i - k + 1];       } else {         name[i] = ++cnt;       }     }     for (i = 1; i <= n; i++) {       if (name[i] <= 26) {         ch = ( A  - 1) + name[i];         str = ch;       } else {         ch = ( a ) + name[i] - 26;         str =  A ;         str = str + ch;       }       cout << str <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   char c[100];   int main() {     int n;     cin >> n;     int i;     cin.getline(c, 100);     int open = 0;     string tt =   ;     for (i = 0; i < n; i++) {       cin.getline(c, 100);       string s =   ;       int k = strlen(c);       int j = 0, z;       while (j < k && (c[j] ==     || c[j] ==   t )) j++;       z = k - 1;       while (z >= j && (c[z] ==     || c[z] ==   t )) z--;       for (; j <= z; j++) {         s += c[j];       }       if (s ==  try ) {         open++;       }       if (s.substr(0, 5) ==  throw ) {         open = 0;         k = s.length();         j = 5;         while (j < k && (s[j] ==  (  || s[j] ==     || s[j] ==   t )) j++;         z = k - 1;         while (z >= j && (s[z] ==  )  || s[z] ==     || s[z] ==   t )) z--;         for (; j <= z; j++) {           tt += s[j];         }       }       if (s.substr(0, 5) ==  catch ) {         if (open == 0 && tt !=   ) {           j = 5;           k = s.length();           while (j < k && (s[j] ==  (  || s[j] ==    )) j++;           string ss =   ;           string out =   ;           for (;; j++) {             if (s[j] ==     || s[j] ==  , ) break;             ss += s[j];           }           while (j < k && s[j] !=    ) j++;           for (j++; s[j] !=    ; j++) {             out += s[j];           }           if (ss == tt) {             printf( %s n , out.c_str());             tt =   ;           }         }         open--;         if (open < 0) open = 0;       }     }     if (tt !=   ) {       printf( Unhandled Exception n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void debugv(std::vector<long long> v);   void printv(std::vector<long long> v);   void inputv(std::vector<long long> &v);   void solve() {     long long n;     cin >> n;     string s;     cin >> s;     unordered_map<string, long long> mp;     for (long long i = 0; i < n - 1; i++) {       string k = s.substr(i, 2);       mp[k]++;     }     long long maxi = INT_MIN;     string ans =   ;     for (auto i : mp) {       if (i.second > maxi) {         ans = i.first;         maxi = i.second;       }     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }   void debugv(std::vector<long long> v) {     long long size = v.size();     for (long long i = 0; i < size; i++) {       cerr << v[i] <<    ;     }     cout <<   n ;   }   void printv(std::vector<long long> v) {     long long size = v.size();     for (long long i = 0; i < size; i++) {       cout << v[i] <<    ;     }     cout <<   n ;   }   void inputv(std::vector<long long> &v) {     for (auto &i : v) {       cin >> i;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, s[30][105], cnt[105];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     string t;     cin >> t;     s[t[0] -  a ][1] = 1;     for (int i = 1; i < t.size(); i++) {       int u = t[i] -  a ;       for (int j = i + 1; j >= 2; j--) {         s[u][j] = 0;         for (int k = 0; k < 26; k++) {           s[u][j] += s[k][j - 1];         }       }       s[u][1] = 1;     }     for (int i = 0; i < 26; i++) {       for (int j = 1; j <= t.size(); j++) {         cnt[j] += s[i][j];       }     }     cnt[0] = 1;     long long res = 0, id = t.size();     while (k > 0) {       long long u = min(k, cnt[id]);       k -= u;       res += (n - id) * u;       id--;       if (id < 0) break;     }     if (k > 0) return cout << -1, 0;     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mod = 1e9 + 7;   int power(int p, int k) {     int a = 1;     while (k) {       if (k & 1) a = (a * p) % mod;       p = (p * p) % mod;       k >>= 1;     }     return a;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int tc = 1;     int i, j;     cin >> tc;     while (tc--) {       int n;       cin >> n;       if (n == 1 || n == 2)         cout << 1;       else if (n == 3)         cout << 2;       else if (n % 2 == 0)         cout << n / 2;       else if (n % 2 == 1)         cout << n / 2 + 1;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct time {     int s;     int e;   };   int main() {     int n;     cin >> n;     int s, e;     int minc = INT_MAX, maxc = -1;     for (int i = 0; i < n; i++) {       cin >> s >> e;       if (minc > e) {         minc = e;       }       if (maxc < s) {         maxc = s;       }     }     int m;     cin >> m;     int maxp = -1, minp = INT_MAX;     for (int i = 0; i < m; i++) {       cin >> s >> e;       if (minp > e) {         minp = e;       }       if (maxp < s) {         maxp = s;       }     }     int p = maxp - minc;     int q = maxc - minp;     if (p < 0 && q < 0) {       cout << 0;     } else {       cout << max(p, q);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const long double PI = 3.141592653589793;   const long long int INF = 9223372036854775807ll;   void solve() {     string p, h;     cin >> p >> h;     if (h.size() < p.size()) {       cout <<  NO n ;       return;     }     vector<int> x(26, 0);     for (int i = 0; i < p.size(); i++) x[p[i] -  a ]++;     for (int i = 0; i < h.size() - p.size() + 1; i++) {       vector<int> v(26, 0);       for (int j = i; j < i + p.size(); j++) v[h[j] -  a ]++;       int cnt = 0;       for (int i = 0; i < 26; i++)         if (x[i] == v[i]) cnt++;       if (cnt == 26) {         cout <<  YES n ;         return;       }     }     cout <<  NO n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace IO {   const int maxn(1 << 21 | 1);   char *iS, *iT, ibuf[maxn], obuf[maxn], *oS = obuf, *oT = obuf + maxn - 1, c,                                          st[20];   int f, tp, len;   inline char getc() {     return iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin),                        iS == iT ? EOF : *iS++)                     : *iS++;   }   template <class T>   inline void read(T &x) {     for (c = getc(), f = 1; c <  0  || c >  9 ; c = getc()) f = c ==  -  ? -1 : 1;     for (x = 0; c >=  0  && c <=  9 ; c = getc()) x = x * 10 + (c ^ 48);     x *= f;   }   template <class T, class... Other>   inline void read(T &x, Other &...y) {     read(x), read(y...);   }   inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; }   inline void putc(char ch) {     *oS++ = ch;     if (oS == oT) flush();   }   template <class T>   inline void print(T x, char ch =   n ) {     if (!x) putc( 0 );     if (x < 0) putc( - ), x = -x;     while (x) st[++tp] = x % 10 +  0 , x /= 10;     while (tp) putc(st[tp--]);     putc(ch);   }   template <class T, class... Other>   inline void print(T x, Other... y) {     print(x), print(y...);   }   inline void print_str(char *s) {     for (len = strlen(s), f = 0; f < len; ++f) putc(s[f]);   }   inline void read_str(char *s, int &l) {     for (l = 0, c = getc(); (c <  a  || c >  z ) && (c <  A  || c >  Z );          c = getc())       ;     for (; (c >=  a  && c <=  z ) || (c >=  A  && c <=  Z ); c = getc())       s[l++] = c;     s[l] =   0 ;   }   }  // namespace IO   using IO ::flush;   using IO ::print;   using IO ::print_str;   using IO ::read;   using IO ::read_str;   template <class T>   inline void cmax(T &x, T y) {     x = x >= y ? x : y;   }   template <class T, class... Other>   inline void cmax(T &x, T y, Other... z) {     Cmax(x, y), Cmax(x, z...);   }   template <class T>   inline void cmin(T &x, T y) {     x = x <= y ? x : y;   }   template <class T, class... Other>   inline void cmin(T &x, T y, Other... z) {     Cmin(x, y), Cmin(x, z...);   }   inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }   const int mod(998244353);   inline void inc(int &x, const int y) { x = x + y >= mod ? x + y - mod : x + y; }   inline void dec(int &x, const int y) { x = x - y < 0 ? x - y + mod : x - y; }   inline int add(const int x, const int y) {     return x + y >= mod ? x + y - mod : x + y;   }   inline int sub(const int x, const int y) {     return x - y < 0 ? x - y + mod : x - y;   }   inline int fpow(long long x, int y) {     long long ret = 1;     for (; y; y >>= 1, x = x * x % mod)       if (y & 1) ret = ret * x % mod;     return ret;   }   const int maxn(1e5 + 5);   int n, seq[maxn], st[18][maxn], lg[maxn], mx;   long long cnt[maxn], sum[maxn], sumv[maxn];   inline int query_gcd(int l, int r) {     int len = lg[r - l + 1];     return gcd(st[len][l], st[len][r - (1 << len) + 1]);   }   inline long long calc(long long m, long long a, long long b, long long c) {     if (c < 0) c = -c, a = -a, b = -b;     if (m <= 0) return 0;     if (m == 1) return (a + b) / c;     if (!a) return b / c * m;     long long ma, mb, ra, rb, nm;     if (b < 0 || b >= c || a < 0 || a >= c) {       ma = (a % c + c) % c, mb = (b % c + c) % c;       ra = (a - ma) / c, rb = (b - mb) / c;       return calc(m, ma, mb, c) + (ra * m * (m + 1) >> 1) + rb * m;     }     nm = (a * m + b) / c;     return nm * m - calc(nm, c, -b - 1, a);   }   inline long long solve(long long x, long long cx, long long y, long long cy,                          long long s) {     if (!cx || !cy) return 0;     if (cx * x + cy * y <= s) return cx * cy;     long long s1 = s - cy * y, s2 = s - cx * x, ss = s - cx * x - cy * y;     return calc(s / x, -x, s, y) - calc(s1 / x, -x, s1, y) -            calc(s2 / y, -y, s2, x) + calc(ss / x, -x, ss, y);   }   inline long long check(long long mid) {     int i, j, p;     long long ret = 0, s, k;     for (i = j = 1; i <= mx; ++i) {       if (!cnt[i]) continue;       k = min(mid / i, cnt[i]);       if (k) ret += cnt[i] * k - (k * (k - 1) >> 1);       while (j < mx && sumv[j + 1] - sumv[i - 1] <= mid) ++j;       if (j >= i) ret += cnt[i] * (sum[j] - sum[i]);       if (j == mx) continue;       for (p = max(i, j); p < mx; ++p) {         s = mid - sumv[p] + sumv[i];         if (s < 0) break;         ret += solve(i, cnt[i], p + 1, cnt[p + 1], s);       }     }     return ret;   }   int main() {     int i, j, k, p, g;     long long ql = 1, qr = 0, mid, ans, lim;     read(n);     for (i = 1; i <= n; ++i) read(seq[i]), st[0][i] = seq[i], cmax(mx, seq[i]);     for (i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;     for (j = 1; j <= lg[n]; ++j)       for (i = 1; i + (1 << j) - 1 <= n; ++i)         st[j][i] = gcd(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);     for (i = 1; i <= n; ++i)       for (j = i; j <= n; ++j) {         g = query_gcd(i, j), p = j;         for (k = lg[n - j]; ~k; --k)           if (p + (1 << k) - 1 <= n && st[k][p] % g == 0) p += (1 << k) - 1;         cnt[g] += p - j + 1, j = p;       }     for (i = 1; i <= mx; ++i)       qr += cnt[i] * i, sum[i] = sum[i - 1] + cnt[i],                         sumv[i] = sumv[i - 1] + cnt[i] * i;     lim = (long long)n * (n + 1) >> 1, lim = lim * (lim + 1) >> 1,     lim = (lim + 1) >> 1;     while (ql <= qr) {       mid = (ql + qr) >> 1;       if (check(mid) >= lim)         qr = mid - 1, ans = mid;       else         ql = mid + 1;     }     print(ans);     return flush(), 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int flag1;     flag1 = 0;     int flag2 = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  I ) {         flag1++;       }       if (s[i] ==  A ) {         flag2++;       }     }     if (flag1 == 0) {       cout << flag2 << endl;     } else if (flag1 == 1) {       cout << 1 << endl;     } else {       cout << 0 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long INF = mod * mod;   const long double eps = 1e-5;   const long double pi = acos(-1.0);   int n, m;   long long w[1 << 18];   vector<int> G[1 << 18];   bool canr[1 << 18];   long long score[1 << 18];   int c[1 << 18];   bool iseda[1 << 18];   long long dfs(int id, int fr) {     long long ret = 0;     for (int j = 0; j < G[id].size(); j++) {       int to = G[id][j];       if (to == fr) continue;       ret = max(ret, dfs(to, id));     }     return ret + w[id];   }   long long ad = 0;   bool sear(int id, int fr) {     for (int j = 0; j < G[id].size(); j++) {       int to = G[id][j];       if (to == fr) continue;       if (!iseda[to]) {         ad += w[id];         w[id] = 0;         return true;       }       if (sear(to, id)) {         ad += w[id];         w[id] = 0;         return true;       }     }     return false;   }   void solve() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> w[i];     }     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       G[a].push_back(b);       G[b].push_back(a);     }     int s;     cin >> s;     s--;     queue<int> q;     q.push(s);     canr[s] = true;     while (!q.empty()) {       int id = q.front();       q.pop();       for (int j = 0; j < G[id].size(); j++) {         int to = G[id][j];         if (canr[to]) continue;         canr[to] = true;         q.push(to);       }     }     int cnt = 0;     int cnte = 0;     for (int i = 0; i < n; i++) {       if (canr[i]) {         cnt++;         cnte += G[i].size();       }     }     cnte /= 2;     if (cnt == cnte + 1) {       long long ans = 0;       for (int j = 0; j < G[s].size(); j++) {         ans = max(ans, dfs(G[s][j], s));       }       cout << ans + w[s] << endl;     } else {       queue<int> q;       for (int i = 0; i < n; i++) {         if (canr[i]) {           c[i] = G[i].size();         }       }       for (int i = 0; i < n; i++) {         if (canr[i]) {           if (c[i] == 1) {             q.push(i);             iseda[i] = true;             score[i] = 0;           }         }       }       while (!q.empty()) {         int id = q.front();         q.pop();         for (int j = 0; j < G[id].size(); j++) {           int to = G[id][j];           if (iseda[to]) continue;           score[to] = max(score[to], score[id] + w[id]);           c[to]--;           if (c[to] == 1) {             q.push(to);             iseda[to] = true;           }         }       }       if (iseda[s]) {         long long ans = 0;         long long ma = 0;         sear(s, -1);         for (int i = 0; i < n; i++) {           if (canr[i] && !iseda[i]) {             ans += w[i];             for (int j = 0; j < G[i].size(); j++) {               int to = G[i][j];               if (iseda[to]) {                 ma = max(ma, dfs(to, i));               }             }           }         }         cout << ans + ad + ma << endl;       } else {         long long ans = 0;         long long ma = 0;         for (int i = 0; i < n; i++) {           if (canr[i] && !iseda[i]) {             ans += w[i];             for (int j = 0; j < G[i].size(); j++) {               int to = G[i][j];               if (iseda[to]) {                 ma = max(ma, dfs(to, i));               }             }           }         }         cout << ans + ma << endl;       }     }   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   const int K = 20;   int go[N][K];   int tin[N], tout[N];   vector<int> g[N];   int timer = 0;   int perm[N];   int where[N];   pair<int, int> t[4 * N];   int n;   void dfs(int u, int p = 0) {     go[u][0] = p;     tin[u] = timer++;     for (int i = 1; i < K; ++i) {       go[u][i] = go[go[u][i - 1]][i - 1];     }     for (auto v : g[u]) {       if (v != p) {         dfs(v, u);       }     }     tout[u] = timer++;   }   bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; }   int lca(int a, int b) {     if (anc(a, b)) return a;     for (int k = K - 1; k >= 0; --k) {       if (!anc(go[a][k], b)) {         a = go[a][k];       }     }     return go[a][0];   }   pair<int, int> null = {-1, -1};   pair<int, int> empt = {-2, -2};   int d;   bool on_vert(int top, int bot, int v) { return anc(top, v) && anc(v, bot); }   bool check(pair<int, int> p, pair<int, int> a) {     int lc = lca(p.first, p.second);     if (!on_vert(lc, p.first, a.first) && !on_vert(lc, p.second, a.first))       return false;     if (!on_vert(lc, p.first, a.second) && !on_vert(lc, p.second, a.second))       return false;     return true;   }   pair<int, int> merge(pair<int, int> a, pair<int, int> b) {     if (a.first == -1 || b.first == -1) return {-1, -1};     if (check({a.first, b.first}, a) && check({a.first, b.first}, b))       return {a.first, b.first};     if (check({a.first, b.second}, a) && check({a.first, b.second}, b))       return {a.first, b.second};     if (check({a.second, b.first}, a) && check({a.second, b.first}, b))       return {a.second, b.first};     if (check({a.second, b.second}, a) && check({a.second, b.second}, b))       return {a.second, b.second};     if (check({a.first, a.second}, a) && check({a.first, a.second}, b))       return {a.first, a.second};     if (check({b.first, b.second}, a) && check({b.first, b.second}, b))       return {b.first, b.second};     return {-1, -1};   }   void build(int l, int r, int v) {     if (l == r) {       t[v] = {where[l], where[l]};     } else {       int m = (l + r) >> 1;       build(l, m, 2 * v + 1);       build(m + 1, r, 2 * v + 2);       t[v] = merge(t[2 * v + 1], t[2 * v + 2]);     }   }   void modify(int ind, int l, int r, int v) {     if (l == r) {       t[v] = {where[ind], where[ind]};     } else {       int m = (l + r) >> 1;       if (ind <= m)         modify(ind, l, m, 2 * v + 1);       else         modify(ind, m + 1, r, 2 * v + 2);       t[v] = merge(t[2 * v + 1], t[2 * v + 2]);     }   }   void update(int a, int b) {     int pa = perm[a];     int pb = perm[b];     swap(where[perm[a]], where[perm[b]]);     swap(perm[a], perm[b]);     modify(pa, 0, n - 1, 0);     modify(pb, 0, n - 1, 0);   }   int get(int v, int l, int r, pair<int, int> cur) {     if (l == r) {       return l;     }     int m = (l + r) >> 1;     auto mem = merge(t[2 * v + 1], cur);     if (mem.first != -1) return get(2 * v + 2, m + 1, r, mem);     return get(2 * v + 1, l, m, cur);   }   int mex() {     if (t[0].first != -1) return n;     return get(0, 0, n - 1, {where[0], where[0]});   }   void init() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> perm[i];       where[perm[i]] = i;     }     for (int i = 1; i < n; ++i) {       int p;       cin >> p;       --p;       g[p].push_back(i);     }     dfs(0);     build(0, n - 1, 0);   }   void solve() {     int q;     cin >> q;     while (q--) {       int t;       cin >> t;       if (t == 1) {         int a, b;         cin >> a >> b;         --a, --b;         update(a, b);       } else {         cout << mex() <<   n ;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.setf(ios::fixed);     cout.precision(20);     init();     solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0)->sync_with_stdio(0);     string s;     cin >> s;     string t = s;     int n = s.size();     auto check = [&]() {       for (int i = 0; i <= 0; i++) {         int mx0 = 0, mx1 = 0, mmx0 = 0, mmx1 = 0;         for (int j = i; j < n; j++) {           if (s[j] ==  1 ) {             mx1 = max(mx1 + 1, mx0 + 1);           } else {             mx0++;           }           if (t[j] ==  1 ) {             mmx1 = max(mmx1 + 1, mmx0 + 1);           } else {             mmx0++;           }           if (max(mx0, mx1) != max(mmx0, mmx1)) {             return 0;           }         }       }       return 1;     };     for (int i = 0; i < n; i++) {       if (s[i] ==  0 ) continue;       s[i] =  0 ;       if (!check()) {         s[i] =  1 ;       }     }     cout << s <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX1 = 1e6 + 10;   long long T, a, b, c, d, e, f, x, y, z, l, r, mid, num[MAX1], cnt, flag,       p1[55][55], p2[55][55];   string s, t;   stack<char> st;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> s;     a = s.size();     for (int i = 0; i < a; ++i) {       if (st.empty())         st.push(s[i]);       else if (st.top() == s[i]) {         st.pop();       } else         st.push(s[i]);     }     if (st.empty())       cout <<  Yes ;     else       cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int l;     cin >> l;     string s;     cin >> s;     if (s.length() % l != 0) {       string pattern(l,  0 );       pattern[0] =  1 ;       int times = s.length() / l + 1;       while (times-- > 0) {         cout << pattern;       }     } else {       string pattern = s.substr(0, l);       int times = s.length() / l;       string presumablyAnswer;       for (int i = 0; i < times; ++i) {         presumablyAnswer += pattern;       }       if (presumablyAnswer > s) {         cout << presumablyAnswer << endl;       } else {         if (count(pattern.begin(), pattern.end(),  9 ) < l) {           int i = (int)pattern.size() - 1;           while (pattern[i] ==  9 ) {             pattern[i] =  0 ;             --i;           }           ++pattern[i];           for (int i = 0; i < times; ++i) {             cout << pattern;           }         } else {           fill(pattern.begin(), pattern.end(),  0 );           pattern[0] =  1 ;           for (int i = 0; i <= times; ++i) {             cout << pattern;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cnt[5005];   int nearest[5005];   int main(void) {     int n, m;     cin >> n >> m;     memset(cnt, 0, sizeof(cnt));     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       b = b > a ? b : b + n;       if (!cnt[a] || b < nearest[a]) nearest[a] = b;       cnt[a]++;     }     for (int i = 1; i <= n; ++i) {       int time = 0;       for (int j = 1; j <= n; ++j) {         int x = (cnt[j] - 1) * n + (nearest[j] + (i > j ? n : 0));         time = max(x, time);       }       cout << time - i <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f, mod = 1e9 + 7;   const int maxn = 2e5 + 100, mxn = 5e5 + 100;   const long double PI = acos(-1.0);   long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }   void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {     if (!b) {       d = a;       x = 1;       y = 0;     } else {       exgcd(b, a % b, d, y, x);       y -= x * (a / b);     }   }   long long power(long long a, long long n) {     long long p = 1;     while (n > 0) {       if (n % 2) {         p = p * a;       }       n >>= 1;       a *= a;     }     return p;   }   long long power(long long a, long long n, long long mod) {     long long p = 1;     while (n > 0) {       if (n % 2) {         p = p * a;         p %= mod;       }       n >>= 1;       a *= a;       a %= mod;     }     return p % mod;   }   int n, m;   double a[maxn], b[maxn], p, q;   double f(double x) {     double minn = 100000.0;     for (int i = 1; i <= n; i++) minn = min(minn, (1 - b[i] * x) / a[i]);     return p * minn + q * x;   }   int main() {     while (cin >> n >> p >> q) {       double maxx = 0;       for (int i = 1; i <= n; i++) {         cin >> a[i] >> b[i];         maxx = max(b[i], maxx);       }       double l = 0, r = 1 / maxx;       for (int i = 1; i <= 400; i++) {         double mid = (l + r) / 2.0;         double midmid = (mid + r) / 2.0;         if (f(mid) >= f(midmid))           r = midmid;         else           l = mid;       }       printf( %.10f n , f((l + r) / 2.0));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> final;   void seive(int n) {     vector<bool> prime(n + 1, true);     prime[0] = false;     prime[1] = false;     for (long long int i = 2; i <= (long long int)sqrt(n); ++i) {       if (prime[i]) {         for (int j = i * i; j <= n; j += i) prime[j] = false;       }     }     for (long long int i = 2; i <= n; ++i) {       if (prime[i]) final.push_back(i);     }   }   const int mod = 998244353;   const int mx = 2e5 + 1;   vector<int> fact(mx);   vector<int> inv_fact(mx);   long long int expo_pow(long long int a, long long int b) {     a %= mod;     long long int res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return res;   }   void precompute() {     fact[0] = 1;     for (long long int i = 1; i <= mx; ++i) fact[i] = (fact[i - 1] * i) % mod;     for (long long int i = 0; i <= mx; ++i)       inv_fact[i] = expo_pow(fact[i], mod - 2) % mod;   }   long long int compute(long long int n, long long int r) {     return fact[n] * 1ll * inv_fact[r] % mod * inv_fact[n - r] % mod;   }   struct cmp {     bool operator()(long long int const& a, long long int const& b) {       return a > b;     }   };   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     while (t--) {       long long int n, k, gain, sum = 0;       cin >> n >> k;       vector<long long int> cost(n), a(n);       for (long long int i = 0; i < n; ++i) cin >> a[i];       cin >> gain;       for (long long int i = 0; i < n; ++i) cin >> cost[i];       priority_queue<long long int, vector<long long int>, cmp> pq;       bool cond = true;       for (long long int i = 0; i < n; ++i) {         pq.push(cost[i]);         if (k < a[i]) {           while (!pq.empty() && k < a[i]) {             k += gain;             sum += pq.top();             pq.pop();           }           if (k >= a[i])             continue;           else {             cond = false;             break;           }           cout <<   n ;         }       }       if (cond)         cout << sum <<   n ;       else         cout << -1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, w, l;     cin >> n >> l >> w;     long double p[n + 1];     long long int b[n + 1];     for (long long int i = 1; i <= n; i++) {       cin >> p[i];       p[i] /= 100;     }     for (long long int i = 1; i <= n; i++) {       cin >> b[i];       b[i]++;     }     long double a[n + 1][n + 1][n + 1];     for (long long int i = 0; i <= n; i++) {       for (long long int j = 0; j <= n; j++) {         for (long long int k = 0; k <= n; k++) {           a[i][j][k] = 0;         }       }     }     long double x, y;     long long int z = 0;     if (w > n) w = n;     a[0][0][w] = 1;     for (long long int i = 1; i <= n; i++) {       for (long long int j = 0; j <= i; j++) {         for (long long int k = 0; k <= n; k++) {           z = b[i];           z += k;           if (z > n) z = n;           if (j > 0) a[j][i - j][z] += ((a[j - 1][i - j][k]) * (p[i]));           if ((i - j) > 0)             a[j][i - j][k] += ((a[j][i - j - 1][k]) * (1.0 - p[i]));         }       }     }     long double ans = 0.0;     for (long long int i = 0; i <= n; i++) {       for (long long int j = 0; j <= n; j++) {         for (long long int k = 0; k <= n; k++) {           if ((k >= i) && (i >= l) && ((i + j) == n)) {             ans += a[i][j][k];           }         }       }     }     cout << fixed << setprecision(6) << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     multiset<long long> A, B;     for (long long i = 0; i < n; i++) {       long long num;       cin >> num;       A.insert(-num);     }     for (long long i = 0; i < n; i++) {       long long num;       cin >> num;       B.insert(-num);     }     long long ansa = 0, ansb = 0;     while (!A.empty() || !B.empty()) {       long long v1 = 0, v2 = 0;       if (A.size() > 0) {         v1 = -(*A.begin());         if (B.size() > 0) {           v2 = -(*B.begin());           if (v2 >= v1) {             auto it = B.begin();             B.erase(it);           } else {             auto it = A.begin();             A.erase(it);             ansa += v1;           }         } else {           auto it = A.begin();           A.erase(it);           ansa += v1;         }       } else {         if (B.size() > 0) {           auto it = B.begin();           B.erase(it);         }       }       v1 = 0, v2 = 0;       if (B.size() > 0) {         long long v1 = -(*B.begin());         if (A.size() > 0) {           v2 = -(*A.begin());           if (v2 >= v1) {             auto it = A.begin();             A.erase(it);           } else {             auto it = B.begin();             B.erase(it);             ansb += v1;           }         } else {           auto it = B.begin();           B.erase(it);           ansb += v1;         }       } else {         if (A.size() > 0) {           auto it = A.begin();           A.erase(it);         }       }     }     cout << ansa - ansb;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int adj[5000005], can[5000005], fuck[5000005];   inline int read() {     int x = 0, f = 1;     char ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) f = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return x * f;   }   int main() {     n = read(), m = read();     if (n == 1) {       puts( 0 );       return 0;     }     for (int i = 0; i < n; i++) adj[i] = 1 << i;     while (m--) {       int x = read() - 1, y = read() - 1;       adj[x] |= 1 << y, adj[y] |= 1 << x;     }     int all = (1 << n) - 1;     bool flag = 1;     for (int i = 0; i < n; i++)       if (adj[i] != all) flag = 0;     if (flag) {       puts( 0 );       return 0;     }     for (int i = 0; i < n; i++) can[1 << i] = 1, fuck[1 << i] = adj[i];     for (int i = 1; i < (1 << n); i++)       if (can[i]) {         for (int j = 0; j < n; j++)           if ((~i >> j & 1) && (fuck[i] >> j & 1))             can[1 << j | i] = 1, fuck[1 << j | i] = fuck[i] | adj[j];       }     int ans = n, mask = 0;     for (int i = 1; i < (1 << n); i++)       if (can[i] && (fuck[i] == all)) {         int res = __builtin_popcount(i);         if (ans > res) ans = res, mask = i;       }     printf( %d n , ans);     for (int i = 0; i < n; i++)       if (mask >> i & 1) printf( %d  , i + 1);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int n;   int dig[N];   vector<int> ans[N];   bool vis[N];   int main() {     cin >> n;     vector<int> x;     for (int i = (0); i < (n); i++) scanf( %d , &dig[i]), x.push_back(dig[i]);     sort((x).begin(), (x).end());     for (int i = (0); i < (n); i++)       dig[i] = lower_bound((x).begin(), (x).end(), dig[i]) - x.begin();     memset(vis, false, sizeof(vis));     int ned = 0;     for (int i = (0); i < (n); i++)       if (!vis[i]) {         for (int u = i; !vis[u]; u = dig[u]) vis[u] = true, ans[ned].push_back(u);         ned++;       }     printf( %d n , ned);     for (int i = (0); i < (ned); i++) {       printf( %d , ((int)(ans[i]).size()));       for (int j = (0); j < (((int)(ans[i]).size())); j++)         printf(  %d , ans[i][j] + 1);       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s1, s2;     cin >> s1 >> s2;     for (int i = 0; i < s1.size(); i++) {       if (s1[i] <= 90) s1[i] += 32;       if (s2[i] <= 90) s2[i] += 32;       if (s1[i] > s2[i]) {         cout << 1 << endl;         return 0;       } else if (s1[i] < s2[i]) {         cout << -1 << endl;         return 0;       }     }     cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       char a[n][m];       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> a[i][j];         }       }       int ans = 0;       for (int j = 0; j < m - 1; j++) {         if (a[n - 1][j] ==  D ) {           ans++;         }       }       for (int i = 0; i < n - 1; i++) {         if (a[i][m - 1] ==  R ) {           ans++;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1000 + 20, MAX_K = 5 + 2;   int a[MAX_K][MAX_N];   int dp[MAX_N];   map<int, int> D, ke;   int ans;   int n, k;   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     for (int i(0); i < int(k); ++i)       for (int j(0); j < int(n); ++j) {         cin >> a[i][j];         --a[i][j];       }     for (int i(0); i < int(n); ++i) D[a[0][i]] = i;     for (int i(0); i < int(k); ++i)       for (int j(0); j < int(n); ++j) a[i][j] = D[a[i][j]];     for (int i(0); i < int(n); ++i) {       ke.clear();       dp[i] = 1;       for (int j(0); j < int(k); ++j)         for (int l = 0; l < n && a[j][l] != i; ++l) ke[a[j][l]]++;       for (int j(0); j < int(i); ++j)         if (ke[j] == k) dp[i] = max(dp[i], dp[j] + 1);     }     for (int i(0); i < int(n); ++i) ans = max(ans, dp[i]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   const char nl =   n ;   using vi = vector<int>;   using vll = vector<ll>;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   template <typename... Args, template <typename...> typename T>   string to_string(T<Args...> const &);   string to_string(string const &s) { return     + s +    ; }   string to_string(char const &c) { return to_string(string(1, c)); }   string to_string(char const *c) { return to_string(string(c)); }   string to_string(bool const &b) { return (b ?  T  :  F ); }   template <typename... Args>   string to_string(pair<Args...> const &p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename... Args, template <typename...> typename T>   string to_string(T<Args...> const &S) {     string s =  { ;     for (auto const &e : S) s +=     + to_string(e);     s +=   } ;     return s;   }   template <typename Tail>   void debug_out(Tail t) {     cerr <<  [   << to_string(t) <<   ]  << endl;   }   template <typename Head, typename... Tail>   void debug_out(Head h, Tail... t) {     cerr <<     << to_string(h) <<  , ;     debug_out(t...);   }   template <typename T>   void dbr(T lb, T ub) {     cerr <<  { ;     for (auto it = lb; it != ub; it++) cerr <<     << to_string(*it);     cerr <<   }  << endl;   }   template <typename T, typename Comp = less<T>>   bool smin(T &mem, T const &v, Comp const &cmp = Comp()) {     return cmp(v, mem) ? mem = v, true : false;   }   template <typename T, typename Comp = less<T>>   bool smax(T &mem, T const &v, Comp const &cmp = Comp()) {     return cmp(mem, v) ? mem = v, true : false;   }   const string getEnvVar(string const &key) {     char *val = getenv(key.c_str());     return val == NULL ? string(  ) : string(val);   }   vector<string> splitString(const string &str, const string &delim =    ) {     vector<string> tokens;     size_t prev = 0, pos = 0;     do {       pos = str.find(delim, prev);       if (pos == string::npos) pos = str.length();       string token = str.substr(prev, pos - prev);       if (!token.empty()) tokens.push_back(token);       prev = pos + delim.length();     } while (pos < str.length() && prev < str.length());     return tokens;   }   void header() {     ios::sync_with_stdio(false);     cin.tie(nullptr);   }   int main() {     header();     string enc, dec =   ;     cin >> enc;     for (int i = 0; i < enc.length(); i++) {       if (enc[i] ==  . ) {         dec +=  0 ;       } else {         if (enc[i + 1] ==  . )           dec +=  1 ;         else           dec +=  2 ;         i++;       }     }     cout << dec << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y, need4demon, puppets;     cin >> n >> x >> y;     need4demon = ceil((y / 100.0) * n);     if (x >= need4demon)       puppets = 0;     else       puppets = need4demon - x;     cout << puppets << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   int a[1000000], n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     int i = 1, j = n / 2 + 1;     int ans = n;     while (true) {       if (i > n / 2) break;       if (j > n) break;       if (2 * a[i] <= a[j]) {         i++;         j++;         ans--;       } else         j++;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int sum = 0;     char c = getchar();     bool f = 0;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     if (f) return -sum;     return sum;   }   const int N = 200005;   const int orz = 2000000;   const int otz = 1000000005;   int n, K, d, a[N], b[N], c[N];   namespace Seg {   int lazy[N * 4], t[N * 4], v, rch;   inline void push(int p) {     if (!lazy[p]) return;     int x = lazy[p], l = p << 1, r = (l | 1);     t[l] += x;     t[r] += x;     lazy[l] += x;     lazy[r] += x;     lazy[p] = 0;   }   inline void ch(int p, int l, int r, int L, int R) {     if (l == L && r == R) {       lazy[p] += v;       t[p] += v;       return;     }     int mid = (l + r) >> 1;     push(p);     if (R <= mid)       ch(p << 1, l, mid, L, R);     else if (L > mid)       ch(p << 1 | 1, mid + 1, r, L, R);     else       ch(p << 1, l, mid, L, mid), ch(p << 1 | 1, mid + 1, r, mid + 1, R);     t[p] = min(t[p << 1], t[p << 1 | 1]);   }   inline void ask(int p, int l, int r) {     if (l == r) {       rch = l;       return;     }     int mid = (l + r) >> 1;     push(p);     if (t[p << 1] < K)       ask(p << 1, l, mid);     else       ask(p << 1 | 1, mid + 1, r);   }   }  // namespace Seg   using namespace Seg;   int s1[N], s2[N], t1, t2;   map<int, int> H;   int main() {     int i, r, st = 1, ed = 1, ans = 1, ino = 0;     n = read();     K = read();     d = read();     for (i = 1; i <= n; i++) a[i] = read() + otz;     if (!d) {       for (i = 1; i <= n; i = r) {         r = i;         while (r <= n && a[r] == a[i]) r++;         if (r - i > ans) ans = r - i, st = i, ed = r - 1;       }       printf( %d %d , st, ed);       return 0;     }     for (i = 1; i <= n; i++) b[i] = a[i] % d, c[i] = a[i] / d;     a[0] = a[1];     b[0] = b[1];     v = orz;     ch(1, 1, n, 1, n);     for (i = 1; i <= n; i++) {       if (b[i] != b[i - 1]) v = orz, ch(1, 1, n, ino + 1, i - 1), ino = i - 1;       if (H[a[i]] > ino) v = orz, ch(1, 1, n, ino + 1, H[a[i]]), ino = H[a[i]];       while (t1 && a[i] > a[s1[t1]])         v = c[i] - c[s1[t1]], ch(1, 1, n, s1[t1 - 1] + 1, s1[t1]), t1--;       while (t2 && a[i] < a[s2[t2]])         v = c[s2[t2]] - c[i], ch(1, 1, n, s2[t2 - 1] + 1, s2[t2]), t2--;       H[a[i]] = i;       s1[++t1] = i;       s2[++t2] = i;       v = -1;       ch(1, 1, n, 1, i);       v = -orz;       ch(1, 1, n, i, i);       ask(1, 1, n);       if (i - rch + 1 > ans) ans = i - rch + 1, st = rch, ed = i;     }     printf( %d %d , st, ed);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int lowbit(int x) { return x & (-x); }   int n, K, a[16], sum[1 << 16], mp[1 << 16], b[16], fac[2001], vl[2001];   bitset<2001> f[1 << 16];   vector<int> v[16];   void construct(int cur, int sum) {     if (!cur) return;     if (sum * K <= 2000 && f[cur][sum * K]) {       for (int j = 0; j < n; j++)         if ((cur >> j) & 1) b[j]++;       construct(cur, sum * K);     } else {       for (int j = 0; j < n; j++) {         if (((cur >> j) & 1) && sum >= a[j] && f[cur ^ (1 << j)][sum - a[j]]) {           construct(cur ^ (1 << j), sum - a[j]);           return;         }       }     }   }   int main() {     scanf( %d%d , &n, &K);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 1; i * K <= 2000; i++) fac[i * K] = fac[i] + 1;     for (int i = 1; i <= 2000; i++) {       vl[i] = i;       for (int j = 0; j < fac[i]; j++) vl[i] /= K;     }     for (int i = 0; i < n; i++) mp[1 << i] = i;     for (int i = 1; i < (1 << n); i++) {       int t = lowbit(i);       sum[i] = sum[i ^ t] + a[mp[t]];     }     f[0][0] = 1;     for (int i = 0; i < (1 << n); i++) {       for (int j = sum[i] / K * K; j >= 0; j -= K)         f[i][j / K] = f[i][j / K] | f[i][j];       for (int j = 0; j < n; j++)         if (!((i >> j) & 1)) f[i | (1 << j)] |= f[i] << a[j];     }     if (f[(1 << n) - 1][1]) {       puts( YES );       construct((1 << n) - 1, 1);       int bpos = 0;       for (int i = 0; i < n; i++) bpos = max(bpos, b[i]), v[b[i]].push_back(a[i]);       for (int i = 1; i < n; i++) {         while (v[bpos].empty()) bpos--;         int x = v[bpos].size();         printf( %d %d n , v[bpos][x - 1], v[bpos][x - 2]);         int cur = v[bpos][x - 1] + v[bpos][x - 2];         v[bpos].pop_back();         v[bpos].pop_back();         v[bpos - fac[cur]].push_back(vl[cur]);       }     } else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   pair<int, int> d[3] = {{0, 1}, {1, 0}, {1, 1}};   int b[550][550];   void rec(int k) {     if (k == 1) {       b[0][0] = 1;       return;     }     rec(k /= 2);     for (int c = 0; c < 3; c++) {       for (int i = 0; i < k; i++) {         for (int j = 0; j < k; j++) {           b[i + d[c].first * k][j + d[c].second * k] = !b[i][j];         }       }     }   }   int main() {     cin >> k;     n = pow(2, k);     rec(n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) cout << (b[i][j] ?  +  :  * );       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x7FFFFFFF;   template <class T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <class T>   T big_mod(T n, T p, T m) {     if (p == 0) return (T)1;     T x = big_mod(n, p / 2, m);     x = (x * x) % m;     if (p & 1) x = (x * n) % m;     return x;   }   template <class T>   T my_pow(T n, T p) {     if (p == 0) return 1;     T x = my_pow(n, p / 2);     x = (x * x);     if (p & 1) x = (x * n);     return x;   }   template <class T>   inline T Mod(T n, T m) {     return (n % m + m) % m;   }   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 n, m, segment[10000];   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       for (int j = x; j <= y; j++) {         segment[j]++;       }     }     for (int i = 1; i <= n; i++) {       if (segment[i] != 1) {         printf( %d %d n , i, segment[i]);         return 0;       }     }     printf( OK n );     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   long long cp[200010];   long long find(long long x) { return cp[x] = cp[x] == x ? x : find(cp[x]); }   void union_(long long x, long long y) { cp[find(x)] = find(y); }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     for (long long j = 0; j < 200010; ++j) cp[j] = j;     long long m, n;     cin >> m >> n;     vector<long long> A(m);     vector<long long> B(n);     for (long long i = 0; i < m; ++i) cin >> A[i];     for (long long i = 0; i < n; ++i) cin >> B[i];     vector<pair<long long, pair<long long, long long>>> edg;     for (long long u = 0; u < m; ++u) {       long long s;       cin >> s;       for (long long i = 0; i < s; ++i) {         long long v;         cin >> v;         edg.push_back({B[v - 1] + A[u], {u, v - 1ll}});       }     }     sort((edg).begin(), (edg).end());     reverse((edg).begin(), (edg).end());     long long ans = 0;     for (long long k = 0; k < edg.size(); ++k) {       long long u = edg[k].second.first, v = edg[k].second.second + m;       if (find(u) == find(v)) {         ans += edg[k].first;       } else {         union_(u, v);       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int DIM = 1e5 + 5;   int szt[DIM], ans[DIM];   vector<int> edg[DIM];   map<int, int> son, par, out;   void dfs1(int x, int sz) {     szt[x] = 1;     for (int y : edg[x]) {       dfs1(y, sz);       szt[x] += szt[y];     }     out[szt[x]]++;     ans[x] = sz - 1;     return;   }   void add(map<int, int> &mmp, int x, int val) {     mmp[x] += val;     out[x] -= val;     if (!mmp[x]) mmp.erase(x);     if (!out[x]) out.erase(x);     return;   }   void bns(map<int, int> &mmp, int x, int le, int ri, int mnm, int aux) {     int val = ri, sol = ri;     while (le <= ri) {       int md = (le + ri) >> 1;       map<int, int>::iterator it = mmp.lower_bound(val - md + aux);       if (it == mmp.end() || it->first + mnm > md + aux)         le = md + 1;       else         ri = md - 1, sol = min(sol, md);     }     ans[x] = min(ans[x], sol);     return;   }   void dfs3(int x, int val, int pos) {     add(son, szt[x], val);     for (int y : edg[x])       if (y != pos) dfs3(y, val, pos);     return;   }   void dfs2(int x, int sz, bool oki) {     int mxm1(-1), mxm2(-1), pos(-1), mnm(sz - 1);     add(par, szt[x], 1);     for (int y : edg[x]) {       mnm = min(mnm, szt[y]);       if (mxm1 < szt[y])         mxm2 = mxm1, mxm1 = szt[y], pos = y;       else         mxm2 = max(mxm2, szt[y]);     }     if (szt[x] != sz) mnm = min(mnm, sz - szt[x]);     for (int y : edg[x])       if (y != pos) dfs2(y, sz, false);     if (pos != -1) dfs2(pos, sz, true);     if (mxm1 >= sz - szt[x]) {       mxm2 = max(mxm2, sz - szt[x]);       if (mnm != sz - 1) bns(son, x, mxm2, mxm1, mnm, 0);     }     dfs3(x, 1, pos);     add(par, szt[x], -1);     if (mxm1 <= sz - szt[x] && mnm != sz - 1) {       bns(par, x, mxm1, sz - szt[x], mnm, szt[x]);       bns(out, x, mxm1, sz - szt[x], mnm, 0);     }     if (!oki) dfs3(x, -1, -1);     return;   }   int main(void) {     int n, r(-1);     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       int x, y;       scanf( %d %d , &x, &y);       if (x == 0)         r = y;       else         edg[x].push_back(y);     }     dfs1(r, n);     dfs2(r, n, true);     for (int i = 1; i <= n; ++i) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long n;     while (cin >> n) {       string s;       cin >> s;       long a[10010] = {0}, x = 0, c = 0, l;       l = s.size();       for (int i = 0; i < l; i++) {         if (i % 2 == 0) {           a[s[i]]++;         } else {           x = s[i] + 32;           if (a[x] > 0) {             a[x]--;           } else             c++;         }       }       cout << c <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxN = 1e6 + 5;   const long long inf = 1e10;   const long long mod = 1e9 + 7;   long long n;   long long x[maxN];   long long a, b;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (long long i = 1; i <= n; i++) cin >> x[i];     cin >> a >> b;     sort(x + 1, x + 1 + n);     n = unique(x + 1, x + 1 + n) - (x + 1);     long long res = 0;     while (a > b) {       long long next = a - 1;       for (long long i = 1; i <= n; i++) {         long long tmp = a - a % x[i];         if (tmp < b) {           x[i--] = x[--n];         } else if (tmp < next)           next = tmp;       }       a = next;       res++;     }     cout << res;     return 0;   }


/// By: CooloTran   #include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   using str = string;   #define st first   #define nd second   #define pb push_back   #define pf push_front   #define lb lower_bound   #define ub upper_bound   #define er equal_range   #define file  TEST    ll gcd(ll a, ll b) {    if (b == 0) return a;    return gcd(b, a % b);   }   ll lcm(ll a, ll b) {    return a / gcd(a, b) * b;   }   ll MULMOD(ll a, ll b, ll n) {    if (b == 0) return 0;    ll tmp = MULMOD(a, b >> 1, n);    if (b & 1)    return (tmp * 2 % n + a) % n;    return tmp * 2 % n;   }   int n;   ll a[200005], cur = 0;   signed main() {    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);    // freopen(file .INP ,  r , stdin);    // freopen(file .OUT ,  w , stdout);    cin >> n;    for (int i = 1; i <= n; ++i)    cin >> a[i];    priority_queue<int, vector<int>, greater<int> > pq;    for (int i = 1; i <= n; ++i) {     if (cur + a[i] >= 0) {      cur += a[i];      pq.push(a[i]);     }     else {      if (pq.empty())      continue;      ll tmp = pq.top();      pq.pop();      if (tmp < a[i]) {       pq.push(a[i]);       cur += a[i] - tmp;      }      else pq.push(tmp);     }    }    cout << pq.size() <<   n ;    return 0;   }


#include <bits/stdc++.h>   int cmp(const void* a, const void* b);   int main() {     long long t = 1;     while (t--) {       long long n, n1, n2;       scanf( %lld%lld%lld , &n, &n1, &n2);       long long arr[n];       for (long long i = 0; i < n; i++) {         scanf( %lld , &arr[i]);       }       qsort(arr, n, sizeof(arr[0]), cmp);       double s1 = 0, s2 = 0;       if (n2 < n1) {         long long x = n2;         n2 = n1;         n1 = x;       }       if (1) {         for (long long i = 0; i < n1; i++) s1 += arr[i];         for (long long i = n1; i < n1 + n2; i++) s2 += arr[i];       }       printf( %f n , s1 / n1 + s2 / n2);     }     return 0;   }   int cmp(const void* a, const void* b) { return (*(int*)b - *(int*)a); }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, num = 0;     for (i = 1; i <= 5; i++) {       int temp;       scanf( %d , &temp);       num += temp;     }     if (num % 5 || num == 0) {       printf( -1 n );     } else {       printf( %d n , num / 5);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     ios_base::sync_with_stdio(false);     cin >> s;     bool f = false;     for (int i = 0; i < s.size() / 2; i++)       if (s[0] != s[i] or s[0] != s[s.size() - i - 1]) {         f = true;         break;       }     if (!f) {       cout <<  Impossible  << endl;       return 0;     }     for (int i = 1; i < s.size() - 1; i++) {       string v = s.substr(i, s.size() - i) + s.substr(0, i);       string w = v;       reverse(w.begin(), w.end());       if (v == w and v != s) {         cout << 1 << endl;         return 0;       }     }     cout << 2 << endl;     return 0;   }


#include <bits/stdc++.h>   static const int MaxSize = 1e5;   int tree[4 * MaxSize][2];   static void build(std::vector<int> &v, int vertex, int tl, int tr) {     if (tl == tr)       tree[vertex][0] = v[tl];     else {       int tm = (tl + tr) / 2;       build(v, 2 * vertex + 1, tl, tm + 0);       build(v, 2 * vertex + 2, tm + 1, tr);       tree[vertex][0] =           std::min(tree[2 * vertex + 1][0], tree[2 * vertex + 2][0]);     }   }   static void update(int vertex, int tl, int tr, int l, int r, int x) {     if (l > r) return;     if (l == tl && tr == r)       tree[vertex][1] += x;     else {       int tm = (tl + tr) / 2;       update(2 * vertex + 1, tl, tm + 0, l, std::min(r, tm + 0), x);       update(2 * vertex + 2, tm + 1, tr, std::max(l, tm + 1), r, x);       tree[vertex][0] =           std::min(tree[2 * vertex + 1][0] + tree[2 * vertex + 1][1],                    tree[2 * vertex + 2][0] + tree[2 * vertex + 2][1]);     }   }   static int get(int vertex, int tl, int tr, int l, int r, int x) {     if (l == tl && tr == r) return tree[vertex][0] + tree[vertex][1];     x += tree[vertex][1];     int tm = (tl + tr) / 2;     return std::min(         x + get(2 * vertex + 1, tl, tm + 0, l, std::min(r, tm + 0), x),         x + get(2 * vertex + 2, tm + 1, tr, std::max(l, tm + 1), r, x));   }   static void build(std::vector<int> &v, int n) { build(v, 0, 0, n - 1); }   static void update(int n, int l, int r, int x) { update(0, 0, n - 1, l, r, x); }   static int get(int n, int l, int r) { return get(0, 0, n - 1, l, r, 0); }   int main(int argc, const char *argv[]) {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     int n, m;     std::cin >> n >> m;     std::vector<int> v(n), tree(4 * n);     for (int i = 0; i < n; i++) std::cin >> v[i];     std::vector<std::pair<int, int>> p(m);     for (int i = 0; i < m; i++) {       std::cin >> p[i].first >> p[i].second;       p[i].first--;       p[i].second--;     }     build(v, n);     std::vector<bool> used(m);     std::vector<int> t(4 * n), r;     int result = 0;     for (int i = 0; i < n; i++) {       std::vector<int> intervals;       for (int j = 0; j < m; j++) {         if (p[j].first <= i && i <= p[j].second) {           if (used[j]) {             used[j] = false;             update(n, p[j].first, p[j].second, 1);           }         } else {           if (!used[j]) update(n, p[j].first, p[j].second, -1);           intervals.push_back(j + 1);           used[j] = true;         }       }       int curr = v[i] - get(n, 0, n - 1);       if (result < curr) {         result = curr;         std::swap(r, intervals);       }     }     std::cout << result << std::endl;     std::cout << r.size() << std::endl;     if (r.size() > 0) {       for (int number : r) std::cout << number <<    ;       std::cout << std::endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solveA(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y,              pair<pair<int, int>, int> z) {     pair<int, int> a = x.first;     pair<int, int> b = y.first;     pair<int, int> c = z.first;     int n = a.first + b.first + c.first;     if (n != a.second) return -1;     if (n != b.second) return -1;     if (n != c.second) return -1;     cout << n <<   n ;     for (int i = 1; i <= a.first; ++i) {       for (int j = 1; j <= a.second; ++j) {         cout << (char)( A  + x.second);       }       cout <<   n ;     }     for (int i = 1; i <= b.first; ++i) {       for (int j = 1; j <= b.second; ++j) {         cout << (char)( A  + y.second);       }       cout <<   n ;     }     for (int i = 1; i <= c.first; ++i) {       for (int j = 1; j <= c.second; ++j) {         cout << (char)( A  + z.second);       }       cout <<   n ;     }     return n;   }   int solveB(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y,              pair<pair<int, int>, int> z) {     pair<int, int> a = x.first;     pair<int, int> b = y.first;     pair<int, int> c = z.first;     int n = a.first;     if (n != b.first + c.first) return -1;     if (n != b.second + a.second) return -1;     if (n != c.second + a.second) return -1;     cout << n <<   n ;     for (int i = 1; i <= a.second; ++i) {       for (int j = 1; j <= n; ++j) {         cout << (char)( A  + x.second);       }       cout <<   n ;     }     for (int i = 1; i <= b.second; ++i) {       for (int j = 1; j <= b.first; ++j) {         cout << (char)( A  + y.second);       }       for (int j = 1; j <= c.first; ++j) {         cout << (char)( A  + z.second);       }       cout <<   n ;     }     return n;   }   int main(int argc, const char* argv[]) {     pair<int, int> a[3];     cin >> a[0].first >> a[0].second;     cin >> a[1].first >> a[1].second;     cin >> a[2].first >> a[2].second;     pair<pair<int, int>, int> b[3];     for (int i = 0; i < 8; ++i) {       if (i & 1)         b[0].first = a[0];       else         b[0].first = make_pair(a[0].second, a[0].first);       if (i >> 1 & 1)         b[1].first = a[1];       else         b[1].first = make_pair(a[1].second, a[1].first);       if (i >> 2 & 1)         b[2].first = a[2];       else         b[2].first = make_pair(a[2].second, a[2].first);       for (int i = 0; i <= 2; ++i) {         b[i].second = i;       }       sort(b, b + 3);       do {         int n;         n = solveA(b[0], b[1], b[2]);         if (n != -1) {           return 0;         }         n = solveB(b[0], b[1], b[2]);         if (n != -1) {           return 0;         }       } while (next_permutation(b, b + 3));     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3005;   const int inf = 5e8;   vector<int> to[N], from[N];   int d[N][N], b[N][N];   vector<pair<int, int>> go[N], back[N];   bool done[N];   int n;   void bfs(vector<int>* edge, int dist[N][N], vector<pair<int, int>>* ret) {     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         dist[i][j] = -inf;       }     }     for (int i = 1; i <= n; i++) {       memset(done, 0, sizeof(done));       queue<int> q;       done[i] = 1;       q.push(i);       dist[i][i] = 0;       while (!q.empty()) {         int now = q.front();         q.pop();         for (auto it : edge[now]) {           if (!done[it]) {             done[it] = 1;             dist[i][it] = dist[i][now] + 1;             q.push(it);           }         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i != j) {           ret[i].emplace_back(dist[i][j], j);         }       }       sort(ret[i].begin(), ret[i].end());       reverse(ret[i].begin(), ret[i].end());     }   }   pair<int, int> get(int p, vector<pair<int, int>>* ret, vector<int>& ojo) {     for (auto it : ret[p]) {       bool ada = 0;       for (auto j : ojo) {         if (it.second == j) {           ada = 1;           break;         }       }       if (!ada) return it;     }     return {-inf, -inf};   }   int main() {     int m;     scanf( %d %d , &n, &m);     while (m--) {       int u, v;       scanf( %d %d , &u, &v);       to[u].push_back(v);       from[v].push_back(u);     }     bfs(to, d, go);     bfs(from, b, back);     int ans = -inf, ai, aj, ak, al;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         assert(d[i][j] == b[j][i]);         if (i == j) continue;         vector<int> ojo = {i, j};         pair<int, int> lef = get(i, back, ojo);         pair<int, int> rig = get(j, go, ojo);         vector<int> tmp;         if (lef.second == rig.second) {           tmp = {i, j, lef.second};           pair<int, int> l = get(i, back, tmp);           int cur = d[i][j] + l.first + rig.first;           if (cur > ans) {             ans = cur;             ai = l.second;             aj = i;             ak = j;             al = rig.second;           }           pair<int, int> r = get(j, go, tmp);           cur = d[i][j] + lef.first + r.first;           if (cur > ans) {             ans = cur;             ai = lef.second;             aj = i;             ak = j;             al = r.second;           }         } else {           int cur = d[i][j] + lef.first + rig.first;           if (cur > ans) {             ans = cur;             ai = lef.second;             aj = i;             ak = j;             al = rig.second;           }         }       }     }     printf( %d %d %d %d n , ai, aj, ak, al);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};   int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};   struct node {     int r, c, dist;     node() {}     node(int _r, int _c, int _dist) : r(_r), c(_c), dist(_dist) {}   };   set<pair<int, int> > valid, visited;   int main() {     int r0, c0, r1, c1, n;     int i, j, r, a, b;     scanf( %d%d%d%d%d , &r0, &c0, &r1, &c1, &n);     for (i = 1; i <= n; ++i) {       scanf( %d%d%d , &r, &a, &b);       for (j = a; j <= b; ++j) valid.insert(make_pair(r, j));     }     int ans = -1;     queue<node> Q;     Q.push(node(r0, c0, 0));     visited.insert(make_pair(r0, c0));     while (Q.size() > 0) {       node cur = Q.front();       Q.pop();       if (cur.c == c1 && cur.r == r1) {         ans = cur.dist;         break;       }       for (i = 0; i < 8; ++i) {         int r2 = cur.r + dr[i];         int c2 = cur.c + dc[i];         if (valid.find(make_pair(r2, c2)) != valid.end() &&             visited.find(make_pair(r2, c2)) == visited.end()) {           visited.insert(make_pair(r2, c2));           Q.push(node(r2, c2, cur.dist + 1));         }       }     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     vector<int> arr;     arr.push_back(b);     while (b > a) {       if (b % 10 == 1)         arr.push_back(b /= 10);       else if (b % 2 == 1) {         cout <<  NO  << endl;         return 0;       } else         arr.push_back(b /= 2);     }     if (arr.back() != a) {       cout <<  NO  << endl;       return 0;     }     cout <<  YES n  << arr.size() << endl;     for (auto it = arr.rbegin(); it != arr.rend(); ++it) cout << *it <<    ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool chmax(T &a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool chmin(T &a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   const long long INF = 1e18;   int N, K;   vector<int> vertex;   vector<int> d[200];   set<pair<int, int>> st;   vector<int> g[30000];   int dist[200][30000];   void dfs(int idx, int now, int from) {     for (auto to : g[now]) {       if (to == from) continue;       dist[idx][to] = dist[idx][now] + 1;       dfs(idx, to, now);     }   }   set<int> appear;   map<int, vector<pair<int, int>>> children;   struct UnionFind {     vector<int> par;     vector<int> rank;     vector<long long> Size;     UnionFind(int n = 1) { init(n); }     void init(int n = 1) {       par.resize(n + 1);       rank.resize(n + 1);       Size.resize(n + 1);       for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;     }     int root(int x) {       if (par[x] == x) {         return x;       } else {         int r = root(par[x]);         return par[x] = r;       }     }     bool issame(int x, int y) { return root(x) == root(y); }     bool merge(int x, int y) {       x = root(x);       y = root(y);       if (x == y) return false;       if (rank[x] < rank[y]) swap(x, y);       if (rank[x] == rank[y]) ++rank[x];       par[y] = x;       Size[x] += Size[y];       return true;     }     long long size(int x) { return Size[root(x)]; }   };   int match[30000];   int TL = 2980;   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     auto start = chrono::steady_clock::now();     int nowtimer;     cin >> N >> K;     vertex.resize(K, -1);     for (int k = 0; k < K; k++) {       d[k].resize(N);       for (int i = 0; i < N; i++) {         cin >> d[k][i];         if (d[k][i] == 0) {           if (vertex[k] != -1) {             cout << -1 << endl;             return 0;           } else {             vertex[k] = i;           }         }       }       if (vertex[k] == -1) {         cout << -1 << endl;         return 0;         ;       }       appear.insert(vertex[k]);     }     for (int k = 1; k < K; k++) {       int mini = d[0][vertex[k]];       vector<int> v(mini + 1, -1);       for (int i = 0; i < N; i++) {         if (d[0][i] + d[k][i] < mini) {           cout << -1 << endl;         }         if (d[0][i] + d[k][i] == mini) {           if (v[d[0][i]] != -1) {             cout << -1 << endl;             return 0;             ;           }           v[d[0][i]] = i;         }       }       for (int i = 0; i <= mini; i++) {         if (v[i] == -1) {           cout << -1 << endl;           return 0;           ;         }       }       for (int i = 0; i < mini; i++) {         int a = v[i];         int b = v[i + 1];         if (a > b) swap(a, b);         st.insert({a, b});       }     }     UnionFind uni(N);     for (auto tmp : st) {       if (!uni.merge(tmp.first, tmp.second)) {         cout << -1 << endl;         return 0;       } else {         g[tmp.first].push_back(tmp.second);         g[tmp.second].push_back(tmp.first);         appear.insert(tmp.first);         appear.insert(tmp.second);       }     }     vector<pair<vector<int>, int>> mp;     for (int i = 0; i < N; i++) {       if (appear.find(i) == appear.end()) continue;       vector<int> v(K);       int mini = 1e9;       for (int j = 0; j < K; j++) {         v[j] = d[j][i];         chmin(mini, v[j]);       }       for (int j = 0; j < K; j++) {         v[j] -= mini;       }       mp.push_back({v, i});     }     mp.push_back({{999999999}, -1});     sort(mp.begin(), mp.end());     vector<pair<vector<int>, int>> sub;     for (int i = 0; i < N; i++) {       if (appear.find(i) != appear.end()) continue;       vector<int> v(K);       int mini = 1e9;       for (int j = 0; j < K; j++) {         v[j] = d[j][i];         chmin(mini, v[j]);       }       for (int j = 0; j < K; j++) {         v[j] -= mini;       }       sub.push_back({v, i});     }     sort(sub.begin(), sub.end());     int idx = 0;     if ((nowtimer = chrono::duration_cast<chrono::milliseconds>(                         chrono::steady_clock::now() - start)                         .count()) > TL) {       assert(0);     }     for (int i = 0; i < sub.size(); i++) {       if ((nowtimer = chrono::duration_cast<chrono::milliseconds>(                           chrono::steady_clock::now() - start)                           .count()) > TL) {         return 0;       }       while (mp[idx].first < sub[i].first) idx++;       if (mp[idx].first == sub[i].first) {         match[sub[i].second] = mp[idx].second;       } else {         cout << -1 << endl;         return 0;       }     }     for (int i = 0; i < N; i++) {       if (appear.find(i) != appear.end()) continue;       int p = match[i];       int len = d[0][i] - d[0][p];       if (len <= 0) {         cout << -1 << endl;         return 0;       }       children[p].push_back({len, i});     }     for (int i = 0; i < N; i++) {       if (children[i].empty()) continue;       auto v = children[i];       sort(v.begin(), v.end());       if (v[0].first != 1) {         cout << -1 << endl;         return 0;       }       int Last = i;       st.insert({i, v[0].second});       g[Last].push_back(v[0].second);       g[v[0].second].push_back(Last);       for (int i = 1; i < v.size(); i++) {         if (v[i].first > v[i - 1].first + 1) {           cout << -1 << endl;           return 0;         } else if (v[i].first == v[i - 1].first) {           st.insert({Last, v[i].second});           g[Last].push_back(v[i].second);           g[v[i].second].push_back(Last);         } else {           Last = v[i - 1].second;           st.insert({Last, v[i].second});           g[Last].push_back(v[i].second);           g[v[i].second].push_back(Last);         }       }     }     for (int i = 0; i < K; i++) {       dfs(i, vertex[i], -1);       for (int j = 0; j < N; j++) {         if (dist[i][j] != d[i][j]) {           cout << -1 << endl;           return 0;         }       }     }     for (auto e : st) {       cout << e.first + 1 <<     << e.second + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int num;     scanf( %d , &num);     int res = 0;     for (int i = 0; i < num; i++) {       char shape[30];       scanf( %s , shape);       switch (shape[0]) {         case  T :           res += 4;           break;         case  C :           res += 6;           break;         case  O :           res += 8;           break;         case  D :           res += 12;           break;         case  I :           res += 20;           break;       }     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, len, vf, va, k;     cin >> n >> len >> vf >> va >> k;     long long bl = (n + k - 1) / k;     double eff = static_cast<double>(va - vf) / (va + vf);     double koef = (va + (bl + (bl - 1) * eff - 1) * vf);     double ta = len / koef;     double ans = ta * (bl + (bl - 1) * eff);     cout << setprecision(15) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize  trapv    using namespace std;   void solveEachTest(long long int T35TC453N = 1) {     unsigned long long int a, b;     cin >> a >> b;     bool ok = true;     bitset<64> xandy((a - b) / 2);     bitset<64> xxory(b);     unsigned long long int x = (a - b) / 2;     unsigned long long int y = x + b;     if (a < b) ok = false;     for (auto i = (0); (((1) < 0) ? (i) > (64) : (i) < (64)); (i) += (1)) {       if (xandy[i] == 1 and xxory[i] == 1) {         ok = false;         break;       }     }     if (!ok)       cout <<  -1 ;     else       cout << x <<     << y;     cout <<   n ;     return;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long int T3X0 = 0, T353 = 1;     solveEachTest(T353 - T3X0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k;   string res;   int c[500];   int main(void) {     for (int i = 2; i < 449; i++) {       c[i] = i * (i - 1) / 2;     }     scanf( %d , &k);     if (k == 0) {       cout <<  a  << endl;     }     char cur =  a ;     while (k > 0) {       int takeind = upper_bound(c + 2, c + 449, k) - c - 1;       k -= c[takeind];       for (int i = 0; i < takeind; i++) {         res += cur;       }       cur++;     }     cout << res << endl;   }


#include <bits/stdc++.h>   const int inf = 1e9 + 7;   const int md = 1e9 + 7;   const double eps = -1e5;   using namespace std;   int n, ans, x, y;   bool a[1001][1001];   bool isin(int x, int y) { return x > 0 && x <= 1000 && y > 0 && y <= 1000; }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x >> y;       a[x][y] = true;     }     for (int x = 1; x <= 1000; x++)       for (int y = 1; y <= 1000; y++)         if (a[x][y]) {           for (int xx = x, yy = y; isin(xx, yy); xx--, yy--) ans += a[xx][yy];           ans--;           for (int xx = x, yy = y; isin(xx, yy); xx++, yy--) ans += a[xx][yy];           ans--;           for (int xx = x, yy = y; isin(xx, yy); xx--, yy++) ans += a[xx][yy];           ans--;           for (int xx = x, yy = y; isin(xx, yy); xx++, yy++) ans += a[xx][yy];           ans--;         }     cout << ans / 2;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int maxn = 1e5 + 5;   long long h[maxn], l[maxn], r[maxn];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %lld , &h[i]);     l[1] = r[n] = 1;     for (int i = 2; i <= n; ++i) l[i] = min(h[i], l[i - 1] + 1);     for (int i = n - 1; i >= 1; --i) r[i] = min(h[i], r[i + 1] + 1);     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long now = min(l[i], r[i]);       if (now > ans) ans = now;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, tot, i;     cin >> n >> k;     tot = n * (n - 1) / 2;     if (tot <= k) {       cout <<  no solution ;     } else {       for (i = 0; i < n; i++) {         cout << 0 <<     << i << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300000;   int parent[N], size[N];   vector<pair<int, pair<int, int>>> v;   pair<int, int> q[N];   long long ans[N];   int Find(int a) {     if (parent[a] == a) return a;     return parent[a] = Find(parent[a]);   }   void Union(int a, int b) { parent[Find(b)] = Find(a); }   long long doit(int a, int b) {     int x = Find(a);     int y = Find(b);     long long p = size[x];     long long qq = size[y];     long long anss = p * qq;     size[x] += size[y];     Union(x, y);     return anss;   }   int main(int argc, char const *argv[]) {     for (int i = 0; i < N; i++) parent[i] = i, size[i] = 1;     int n, m, a, b, c;     scanf( %d%d , &n, &m);     for (int i = 0; i < n - 1; ++i) {       scanf( %d%d%d , &a, &b, &c);       v.push_back({c, {a, b}});     }     sort(v.begin(), v.end());     for (int i = 0; i < m; i++) {       scanf( %d , &a);       q[i] = {a, i};     }     sort(q, q + m);     int at = 0;     long long sum = 0;     for (int i = 0; i < m; i++) {       if (at < v.size()) {         while (v[at].first <= q[i].first) {           if (at >= v.size()) break;           long long kk = doit(v[at].second.first, v[at].second.second);           sum += kk;           at++;           if (at >= v.size()) break;         }       }       ans[q[i].second] = sum;     }     for (int i = 0; i < m; i++) {       printf( %lld  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string line;     while (getline(cin, line)) {       map<int, vector<string> > answer;       line +=  , ;       istringstream iss(line);       string s;       stack<int> stk;       string tmp;       while (getline(iss, s,  , )) {         getline(iss, tmp,  , );         int children = stoi(tmp);         answer[stk.size()].push_back(s);         if (stk.size() && stk.top() > 0) {           stk.top()--;         }         stk.push(children);         while (stk.size() && stk.top() <= 0) stk.pop();       }       cout << answer.size() << endl;       for (auto i = 0; i < answer.size(); i++) {         for (auto &word : answer[i]) cout << word <<    ;         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   long long sum, s[1000005], ans = -(1LL << 62);   struct el {     long long val;     int poz;     bool operator<(const el &A) const { return val < A.val; }   };   el a[1000005];   int main() {     int i, poz;     cin.sync_with_stdio(0);     cin >> n;     for (i = 1; i <= n; ++i) {       cin >> a[i].val;       a[i].poz = i;     }     cin >> k;     sort(a + 1, a + n + 1);     for (i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i].val;     for (i = 1, sum = 0; i <= k; ++i) sum += a[i].val * (2 * i - 1 - k);     ans = sum;     poz = 1;     for (i = k + 1; i <= n; ++i) {       sum = sum - a[i - k].val * (1 - k) + a[i].val * (k - 1) -             2 * (s[i - 1] - s[i - k]);       if (sum < ans) {         ans = sum;         poz = i - k + 1;       }     }     for (i = poz; i < poz + k; ++i) cout << a[i].poz <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b, x, y, aa;     scanf( %I64d %I64d , &x, &y);     if (y > x) {       printf( -1 n );       return 0;     }     a = x / y;     if ((x % y) && (a % 2)) a++;     if (!(a % 2))       aa = a - 1;     else       aa = a;     double cc = (double)(x - aa * y);     double c = cc / (double)a;     double res = y + c;     printf( %0.11lf n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100010;   int N, M;   int ar[MAXN];   char s[MAXN];   int dn[MAXN][2];   int used[MAXN][2];   int f(int k, int flag) {     if (k == -1) return 0;     if (used[k][flag]) return dn[k][flag];     used[k][flag] = 1;     if (!flag) return dn[k][flag] = f(k - 1, 0) + ar[k];     if (s[k] ==  0 ) return dn[k][flag] = f(k - 1, 1);     if (s[k] ==  1 ) return dn[k][flag] = max(f(k - 1, 1) + ar[k], f(k - 1, 0));   }   int main() {     cin >> N;     for (int i = 0; i < N; i++) scanf(  %d , &ar[i]);     scanf(  %s , s);     cout << f(N - 1, 1) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char a[15];   char rr[15];   char ans[15];   int main() {     int maxx = -99;     cin >> a;     int len = strlen(a);     for (int i = 1; i < 1 << len; i++) {       int cnt = 0;       for (int j = 0; j <= len - 1; j++) {         if (i >> j & 1) rr[++cnt] = a[j];       }       int flag = 0;       for (int l = 1, r = cnt; l <= r; l++, r--) {         if (rr[l] != rr[r]) {           flag = 1;           break;         }       }       if (flag == 0) {         if (strcmp(ans + 1, rr + 1) < 0) {           for (int i = 1; i <= 14; i++) {             if (i <= cnt)               ans[i] = rr[i];             else               ans[i] =   0 ;           }         }       }     }     cout << ans + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int dist(int a, int b) { return min(abs(a - b), 360 - abs(a - b)); }   long long d;   int main() {     scanf( %I64d , &d);     d = (d % 360 + 360) % 360;     int ans = 1000;     for (int i = 0; i < 4; i++) ans = min(ans, dist(d, 90 * i));     for (int i = 0; i < 4; i++) {       if (dist(d, 90 * i) == ans) {         printf( %d n , i);         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   const int MOD = 1e9 + 7;   int a[MAXN];   class Matrix {    public:     void prinf() {       for (int i = 0; i < 2; i++) {         for (int j = 0; j < 2; j++) {           cout << a[i][j];         }         cout << endl;       }     }     long long a[2][2];     Matrix() { memset(a, 0, sizeof a); }     void init() {       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) a[i][j] = (i == j);     }     Matrix operator+(const Matrix &B) const {       Matrix C;       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) C.a[i][j] = (a[i][j] + B.a[i][j]) % MOD;       return C;     }     Matrix operator*(const Matrix &B) const {       Matrix C;       for (int i = 0; i < 2; i++)         for (int k = 0; k < 2; k++)           for (int j = 0; j < 2; j++)             C.a[i][j] = (C.a[i][j] + 1LL * a[i][k] * B.a[k][j]) % MOD;       return C;     }     Matrix operator^(const int &t) const {       Matrix A = (*this), res;       res.init();       int p = t;       while (p) {         if (p & 1) res = res * A;         A = A * A;         p >>= 1;       }       return res;     }   };   Matrix sum[MAXN << 2], add[MAXN << 2], f;   void pushdown(int rt) {     sum[rt << 1] = sum[rt << 1] * add[rt];     sum[rt << 1 | 1] = sum[rt << 1 | 1] * add[rt];     add[rt << 1] = add[rt << 1] * add[rt];     add[rt << 1 | 1] = add[rt << 1 | 1] * add[rt];     add[rt].init();   }   void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; }   void build(int l, int r, int rt) {     sum[rt].init();     add[rt].init();     if (l == r) {       sum[rt] = f ^ (a[l] - 1);       return;     }     int m = (l + r) >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);     pushup(rt);   }   void update(int L, int R, Matrix v, int l, int r, int rt) {     if (L <= l && R >= r) {       sum[rt] = sum[rt] * v;       add[rt] = add[rt] * v;       return;     }     pushdown(rt);     int m = (l + r) >> 1;     if (L <= m) update(L, R, v, l, m, rt << 1);     if (R > m) update(L, R, v, m + 1, r, rt << 1 | 1);     pushup(rt);   }   long long query(int L, int R, int l, int r, int rt) {     if (L <= l && R >= r) {       return sum[rt].a[0][0];     }     pushdown(rt);     int m = (l + r) >> 1;     int cnt = 0;     if (L <= m) cnt = (cnt + query(L, R, l, m, rt << 1)) % MOD;     if (R > m) cnt = (cnt + query(L, R, m + 1, r, rt << 1 | 1)) % MOD;     return cnt;   }   int main() {     int m, n;     f.a[0][0] = 1;     f.a[0][1] = 1;     f.a[1][0] = 1;     f.a[1][1] = 0;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     build(1, n, 1);     int a1, b, c;     for (int i = 0; i < m; i++) {       scanf( %d %d %d , &a1, &b, &c);       if (a1 == 1) {         int s;         scanf( %d , &s);         update(b, c, f ^ s, 1, n, 1);       } else {         cout << query(b, c, 1, n, 1) % MOD << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[100146];   int val[100146], a[100146], b[100146];   long long cnt[1 << 18], sum[1 << 18];   int leftError[100146], rightError[100146];   void countDps(int e) {     if (v[e].size() == 0)       leftError[e] = rightError[e] = val[e];     else {       countDps(v[e][0]);       countDps(v[e][1]);       if (val[v[e][0]] < val[v[e][1]]) {         leftError[e] = leftError[v[e][0]];         rightError[e] = rightError[v[e][1]];       } else {         leftError[e] = leftError[v[e][1]];         rightError[e] = rightError[v[e][0]];       }     }   }   void add(long long* tree, int cur, int l, int r, int cl, int cr, long long c) {     if (r < cl || cr < l) return;     if (cl <= l && r <= cr)       tree[cur] += c;     else {       int mid = (l + r) / 2;       add(tree, cur * 2, l, mid, cl, cr, c);       add(tree, cur * 2 + 1, mid + 1, r, cl, cr, c);     }   }   void addToSegment(long long* tree, int l, int r, long long c) {     if (l > r) return;     add(tree, 1, 0, (1 << 17) - 1, l, r, c);   }   void dfs(int e, int l, int r) {     if (v[e].size() == 0) return;     int mid = lower_bound(a + l, a + r, val[e]) - a;     if (val[v[e][0]] < val[v[e][1]]) {       dfs(v[e][0], l, mid);       dfs(v[e][1], mid, r);       addToSegment(cnt, l, mid - 1, 1);       addToSegment(sum, l, mid - 1, leftError[v[e][1]]);       addToSegment(cnt, mid, r - 1, 1);       addToSegment(sum, mid, r - 1, rightError[v[e][0]]);     } else {       dfs(v[e][1], l, mid);       dfs(v[e][0], mid, r);       addToSegment(cnt, l, mid - 1, 1);       addToSegment(sum, l, mid - 1, leftError[v[e][0]]);       addToSegment(cnt, mid, r - 1, 1);       addToSegment(sum, mid, r - 1, rightError[v[e][1]]);     }   }   long long getValue(long long* tree, int el) {     long long cur = el + (1 << 17);     long long ans = 0;     while (cur) {       ans += tree[cur];       cur /= 2;     }     return ans;   }   int main() {     int n;     scanf( %d , &n);     int root = -1;     for (int i = 0; i < n; i++) {       int p;       scanf( %d%d , &p, &val[i]);       if (p != -1)         v[p - 1].push_back(i);       else         root = i;     }     int m;     scanf( %d , &m);     for (int i = 0; i < m; i++) {       scanf( %d , &a[i]);       b[i] = a[i];     }     sort(a, a + m);     countDps(root);     memset(sum, 0, sizeof(sum));     memset(cnt, 0, sizeof(cnt));     dfs(root, 0, m);     for (int i = 0; i < m; i++) {       int ind = lower_bound(a, a + m, b[i]) - a;       double ans = (double)getValue(sum, ind) / getValue(cnt, ind);       printf( %.9lf n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int> > G;   vector<vector<int> > ID;   vector<vector<int> > ans;   void addp(int id, int t) {     while (ans.size() <= t) ans.push_back(vector<int>());     ans[t].push_back(id);   }   void solve(int from, int lab, int v) {     int ctr = 0;     for (int i = 0; i < G[v].size(); ++i) {       if (ctr == lab) ++ctr;       if (G[v][i] == from) continue;       addp(ID[v][i], ctr);       solve(v, ctr++, G[v][i]);     }   }   int main() {     ios::sync_with_stdio(0);     cin >> n;     G = vector<vector<int> >(n, vector<int>());     ID = vector<vector<int> >(n, vector<int>());     for (int i = 1; i < n; ++i) {       int a, b;       cin >> a >> b;       G[--a].push_back(--b);       G[b].push_back(a);       ID[a].push_back(i);       ID[b].push_back(i);     }     solve(-1, -1, 0);     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); ++i) {       cout << ans[i].size();       for (int j = 0; j < ans[i].size(); ++j) cout <<     << ans[i][j];       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<string> split(const string& s, char c) {     vector<string> v;     stringstream second(s);     string x;     while (getline(second, x, c)) v.emplace_back(x);     return move(v);   }   template <typename T, typename... Args>   inline string arrStr(T arr, int n) {     stringstream s;     s <<  [ ;     for (int i = 0; i < n - 1; i++) s << arr[i] <<  , ;     s << arr[n - 1] <<  ] ;     return s.str();   }   inline void __evars_begin(int line) { cerr <<  #  << line <<  :  ; }   template <typename T>   inline void __evars_out_var(vector<T> val) {     cerr << arrStr(val, val.size());   }   template <typename T>   inline void __evars_out_var(T* val) {     cerr << arrStr(val, 10);   }   template <typename T>   inline void __evars_out_var(T val) {     cerr << val;   }   inline void __evars(vector<string>::iterator it) { cerr <<   n ; }   template <typename T, typename... Args>   inline void __evars(vector<string>::iterator it, T a, Args... args) {     cerr << it->substr((*it)[0] ==    , it->length()) <<  = ;     __evars_out_var(a);     cerr <<  ;  ;     __evars(++it, args...);   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};   int dy[8] = {0, 0, -1, 1, 2, -2, 2, -2};   int myrandom(int i) { return std::rand() % i; }   const int MAX = 200010;   const unsigned long long inf = 1000000007;   int ans = 0, maxi = 0;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, m;     cin >> n >> m;     long long x, k, y;     cin >> x >> k >> y;     vector<long long> v(n), t(m);     for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));          i += 1 - 2 * ((0) > (n))) {       cin >> v[i];     }     for (__typeof(m) j = (0) - ((0) > (m)); j != (m) - ((0) > (m));          j += 1 - 2 * ((0) > (m))) {       cin >> t[j];     }     vector<long long> vis(n, 0);     int i = 0, j = 0, cnt = 0;     while (i < n && j < m) {       if (v[i] == t[j]) {         vis[i] = 1;         i++;         j++;         cnt++;       } else {         i++;       }     }     if (cnt != m) {       cout << -1 <<   n ;       return 0;     }     i = 0;     long long ans = 0;     while (i < n) {       while (i < n && vis[i] == 1) {         i++;       }       if (i == n) break;       int l = i;       vector<long long> temp;       while (i < n && vis[i] == 0) {         temp.push_back(v[i]);         i++;       }       sort((temp).begin(), (temp).end());       if (y * k > x) {         if ((int)temp.size() >= k) {           ans += ((long long)temp.size() % k) * y;           ans += ((long long)temp.size() / k) * x;         } else {           long long left = l - 1 >= 0 ? v[l - 1] : 0;           long long right = i < n ? v[i] : 0;           if (temp.back() < max(left, right)) {             ans += (long long)temp.size() * y;           } else {             cout << -1 <<   n ;             return 0;           }         }       } else {         long long left = l - 1 >= 0 ? v[l - 1] : 0;         long long right = i < n ? v[i] : 0;         if ((long long)temp.size() >= k) {           if (temp.back() < max(left, right)) {             ans += (long long)temp.size() * y;           } else {             ans += ((long long)temp.size() - k) * y + x;           }         } else {           if (temp.back() < max(left, right)) {             ans += (long long)temp.size() * y;           } else {             cout << -1 <<   n ;             return 0;           }         }       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> s[10];   int n1, n2;   int ok(int a, int b) {     int sum = a + b * 2;     int len = 0;     for (int i = 0; i < n1 - a; i++) len += s[1][i];     for (int i = 0; i < n2 - b; i++) len += s[2][i];     return len <= sum;   }   int main() {     int a, b, n, m, i, j, ans = 99999999;     ;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d%d , &a, &b);       s[a].push_back(b);     }     sort(s[1].begin(), s[1].end());     sort(s[2].begin(), s[2].end());     n1 = s[1].size();     n2 = s[2].size();     for (i = 0; i <= n1; i++)       for (j = 0; j <= n2; j++) {         if (ok(i, j)) {           if (i + j * 2 < ans) ans = i + j * 2;         }       }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (1 << 30) - 1;   const int maxn = 100010;   char s[maxn];   int calc(char a, char b) {     int mx = 30;     mx = min(mx, a > b ? a - b : b - a);     mx = min(mx, a > b ? b + 26 - a : a + 26 - b);     return mx;   }   int main() {     scanf( %s , s);     int len = strlen(s);     int ans = 0;     ans += calc( a , s[0]);     for (int i = 0; i < len - 1; i++) ans += calc(s[i], s[i + 1]);     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int lcm(int a, int b) { return a * b / gcd(a, b); }   struct data {     int num;     int init;   };   bool byNum(const data &left, const data &right) { return left.num > right.num; }   bool descend(const int &left, const int &right) { return left > right; }   data total[105];   pair<int, int> pile[105];   int sisa[105], card[105];   int main(void) {     int temp, n, i, j;     scanf( %d , &n);     for (i = 0; i <= n - 1; i++) {       scanf( %d , &card[i]);       sisa[i] = 0;       for (j = 0; j <= card[i] / 2 - 1; j++) {         scanf( %d , &temp);         pile[i].first += temp;       }       if (card[i] % 2) {         scanf( %d , &temp);         sisa[i] = temp;       }       for (j = 0; j <= card[i] / 2 - 1; j++) {         scanf( %d , &temp);         pile[i].second += temp;       }       total[i].num = pile[i].first + sisa[i] + pile[i].second;       total[i].init = i;     }     sort(total, total + n, byNum);     sort(sisa, sisa + n, descend);     long long a = 0, b = 0;     for (i = 0; i <= n - 1; i++) {       a += pile[total[i].init].first;       b += pile[total[i].init].second;     }     for (i = 0; i <= n - 1; i++) {       if (i % 2)         b += sisa[i];       else         a += sisa[i];       if (!sisa[i]) break;     }     cout << a <<     << b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> caps, full;   int main() {     int w, l;     cin >> w >> l;     caps = vector<int>(w + 1, 0);     caps[w] = 2e9;     full = vector<int>(w + 1, 0);     full[0] = 2e9;     for (int i = 1; i < w; i++) cin >> caps[i];     int from = 0;     int to = 1;     while (from != w) {       ;       if (to == from)         to++;       else if (to - from > l) {         ;         from++;       } else if (full[to] == caps[to]) {         ;         to++;       } else if (full[from] == 0) {         ;         from++;       } else {         int k = min(full[from], caps[to] - full[to]);         full[from] -= k;         full[to] += k;         ;       }     }     cout << full[w] << endl;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long T;     cin >> T;     while (T--) {       string s;       cin >> s;       long long ans = (long long)s.size(), curr = 0;       for (long long i = 0; i < s.size(); i++) {         if (s[i] ==  + ) {           curr++;         } else {           curr--;         }         if (curr < 0) {           ans += (i + 1);           curr++;         }       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int event, ghotona;     cin >> event;     int hired = 0, untreated = 0;     for (int i = 0; i < event; i++) {       cin >> ghotona;       hired += ghotona;       if (hired < 0) {         untreated++;         hired = 0;       }     }     cout << untreated << endl;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   int bit[1100];   int tot = 0;   void GetBinary(int n) {     memset(bit, 0, sizeof bit);     while (n) {       bit[tot++] = n & 1;       n >>= 1;     }   }   int layer[1100][2];   void Construct() {     layer[1][0] = 1;     layer[2][0] = 3;     layer[3][0] = 4;     layer[3][1] = 5;     for (int i = 4; i <= 60; i += 2) {       layer[i][0] = layer[i - 2][0] + 3;       layer[i + 1][0] = layer[i - 1][0] + 3;       layer[i + 1][1] = layer[i - 1][1] + 3;     }   }   int g[1100][1100];   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     GetBinary(n);     Construct();     memset(g, 0, sizeof g);     int total = tot << 1;     layer[total + 1][0] = 2;     for (int i = 2; i <= total; ++i) {       if (i == 2) {         g[layer[i - 1][0]][layer[i][0]] = 1;         continue;       }       if (i % 2 == 1) {         g[layer[i - 1][0]][layer[i][0]] = 1;         g[layer[i - 1][0]][layer[i][1]] = 1;       } else {         g[layer[i - 1][0]][layer[i][0]] = 1;         g[layer[i - 1][1]][layer[i][0]] = 1;       }     }     g[2][layer[total][0]] = 1;     int PathLength = total;     int ind = tot * 3, indd = tot * 3, end = tot * 3;     for (int i = 0; i < tot - 1; ++i) {       if (bit[i]) {         int pre = layer[(i + 1) * 2][0];         indd = ind;         for (int j = 1; j <= PathLength - (i + 1) * 2 - 1; ++j) {           g[pre][j + indd] = 1;           pre = j + indd;           ind++;         }         g[pre][end] = 1;       }     }     cout << ind << endl;     for (int i = 1; i <= ind; ++i) {       for (int j = 1; j <= ind; ++j) {         if (i == j)           cout <<  N ;         else if (g[i][j] || g[j][i])           cout <<  Y ;         else           cout <<  N ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n;     string s;     cin >> n >> s;     for (long long int i = 0; i < n; i++) cout << s[n - 1];     cout << endl;   }   int main() {     int test;     cin >> test;     for (int TEST = 1; TEST <= test; TEST++) solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     char arr[n][n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> arr[i][j];       }     }     pair<int, int> p[n * n];     int in = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (arr[i][j] ==  C ) {           p[in] = make_pair(i, j);           in++;         }       }     }     int cnt = 0;     for (int i = 0; i < in; i++) {       for (int j = i + 1; j < in; j++) {         if (p[i].first == p[j].first || p[i].second == p[j].second) {           cnt++;         }       }     }     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 103;   int p[MAXN], c[MAXN];   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       bool check = true;       for (int i = 1; i <= n; i++) {         cin >> p[i] >> c[i];         if (c[i] > p[i] || c[i] < c[i - 1] || p[i] < c[i] ||             p[i] - p[i - 1] < c[i] - c[i - 1])           check = false;       }       if (check)         cout <<  YES               <<   n ;       else         cout <<  NO               <<   n ;     }   }