#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   const long long mod = 1e9 + 7;   long long powr(long long a, long long b) {     long long x = 1 % mod;     a %= mod;     while (b) {       if (b & 1) x = (x * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return x;   }   long long inv(long long a) { return powr(a, mod - 2); }   map<int, int> mymap, mymap2;   int par[maxn];   int val[maxn];   int main() {     int n;     cin >> n;     int x;     map<int, int>::iterator it1, it2;     int mx = 0, mn = 1e9 + 10;     int maxi = 1, mini = 1;     for (int i = 1; i <= n; i++) {       cin >> x;       if (i == 1) mx = mn = x;       val[i] = x;       if (i > 1) {         if (x > mx) {           par[i] = maxi;           maxi = i;           mx = x;         } else if (x < mn) {           par[i] = mini;           mini = i;           mn = x;         } else {           it2 = mymap.upper_bound(x);           int x = it2->second;           it2--;           int y = it2->second;           par[i] = max(x, y);         }         cout << val[par[i]] <<    ;       }       mymap[x] = i;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t, n;     string s;     cin >> t;     while (t--) {       cin >> n >> s;       vector<int> szs;       char prev = s.front();       int sz = 0;       for (char c : s) {         if (c != prev) {           szs.push_back(sz);           sz = 1;         } else           sz++;         prev = c;       }       szs.push_back(sz);       int cnt = 0, id = 1;       for (int i : szs) {         if (i > 1) cnt += min(i - 1, id - cnt);         id++;       }       cout << cnt + (szs.size() - cnt + 1) / 2 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MAXN = 3e5 + 10;   template <typename value_t>   class fenwick {    public:     fenwick(int n) {       memset(c, 0, sizeof c);       this->n = n;     }     void add(int i, value_t x) {       while (i <= n) {         c[i] += x;         i += Lowbit(i);       }     }     value_t sum(int x) {       value_t sum = 0;       while (x > 0) {         sum += c[x];         x -= Lowbit(x);       }       return sum;     }     value_t sum(int x1, int x2) { return sum(x2) - sum(x1 - 1); }       private:     value_t c[MAXN];     int n;     inline int Lowbit(int x) { return x & (-x); }   };   class lfs {    public:     lfs(int N) {       memset(head, -1, sizeof head);       ecnt = 0;       n = N;     }     void adde(int from, int to, int w) {       e[ecnt].to = to;       e[ecnt].w = w;       e[ecnt].next = head[from];       head[from] = ecnt++;     }     void addde(int a, int b, int w) {       adde(a, b, w);       adde(b, a, w);     }     void solve() {       memset(ans, 0, sizeof ans);       arr = new fenwick<long long>(n);       dfs(1, 0, 1);     }     vector<pair<int, int>> opr[MAXN];     long long ans[MAXN];       protected:     struct Edge {       int to, next, w;     } e[MAXN * 2];     int head[MAXN];     int ecnt;     int n;     fenwick<long long>* arr;     void dfs(int u, int fa, int dep) {       for (auto& i : opr[u]) {         arr->add(dep, i.second);         arr->add(dep + i.first + 1, -i.second);       }       ans[u] = arr->sum(dep);       for (int i = head[u]; i != -1; i = e[i].next) {         int v = e[i].to;         if (v != fa) dfs(v, u, dep + 1);       }       for (auto& i : opr[u]) {         arr->add(dep, -i.second);         arr->add(dep + i.first + 1, i.second);       }     }   };   template <typename T = int>   inline T read() {     T X = 0, w = 0;     char ch = 0;     while (!isdigit(ch)) {       w |= ch ==  - ;       ch = getchar();     }     while (isdigit(ch)) {       X = (X << 3) + (X << 1) + (ch ^ 48);       ch = getchar();     }     return w ? -X : X;   }   int main() {     int n = read();     lfs* tree = new lfs(n);     for (int i = 1; i < n; i++) {       tree->addde(read(), read(), 1);     }     int m = read();     for (int i = 1; i <= m; i++) {       int u = read();       int d = read();       int x = read();       tree->opr[u].push_back(make_pair(d, x));     }     tree->solve();     for (int i = 1; i <= n; i++) {       cout << tree->ans[i] <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, n, t, r = 0;     cin >> a >> b >> c >> n;     for (int i = 0; i < n; i++) {       cin >> t;       if (t > b && t < c) r++;     }     cout << r << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using ld = long double;   using pii = pair<int, int>;   ll val(ll solve, ll whole) {     if (2 * solve > whole) return 500;     if (4 * solve > whole) return 1000;     if (8 * solve > whole) return 1500;     if (16 * solve > whole) return 2000;     if (32 * solve > whole) return 2500;     return 3000;   }   ll score(vi &t, vector<ll> &cnt, ll whole) {     ll ans = 0;     for (int i = 0; i < t.size(); i++)       if (t[i] != -1) ans += val(cnt[i], whole) * (250 - t[i]);     return ans;   }   bool go(vector<vi> &a, ll k, int mask) {     vector<ll> cnt(a[0].size());     for (int i = 0; i < a.size(); i++)       for (int j = 0; j < a[i].size(); j++)         if (a[i][j] != -1) cnt[j]++;     for (int i = 0; i < 5; i++)       if ((mask & (1 << i)) && a[0][i] != -1) cnt[i] += k;     ll whole = a.size() + (mask == 0 ? 0 : k);     return score(a[0], cnt, whole) > score(a[1], cnt, whole);   }   void solve() {     int n;     cin >> n;     vector<vi> a(n, vi(5));     for (int i = 0; i < n; i++)       for (int j = 0; j < 5; j++) cin >> a[i][j];     for (int k = 0; k < 32 * n; k++)       for (int mask = 1; mask < 1 << 5; mask++)         if (go(a, k, mask)) {           cout << k <<   n ;           return;         }     cout << -1 <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j;     string s;     cin >> s;     if ((int)s[0] >= 97 && (int)s[0] <= 122) {       s[0] = (char)((int)s[0] - 32);     }     for (i = 0; i < s.size(); i++) {       cout << s[i];     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 101000;   int a[maxn], x, y, z, cnt, curr, ans, n, m;   vector<int> v;   int bs(int x) {     int l = 0, r = m;     while (l < r - 1) {       int mid2 = (l + r) / 2;       if (v[mid2] < x) {         l = mid2;       } else {         r = mid2;       }     }     if (v[l] >= x) {       return -1;     }     return l;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     for (int j = 0; j < m; ++j) {       cin >> x >> y >> z;       if (x == 1) {         v.push_back(y);         if (y == 1000000000) {           ++cnt;         }       }     }     sort(a, a + n);     sort(v.begin(), v.end());     ans = cnt + n;     if (!v.size()) {       cout << 0 << endl;       return 0;     }     v.push_back(1000001000);     m = v.size();     for (int i = 0; i < n; ++i) {       curr = i;       int val = bs(a[i]);       curr += m - val - 2;       ans = min(ans, curr);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   long long int mod = 1000000007;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, k;     cin >> n >> k;     long long int p = k / n;     if (k % n != 0) {       p += 1;     }     cout << p;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, z = 1, x, c, zx;   map<string, long long> m;   string a[1001], s;   int main() {     while (getline(cin, s)) {       a[n++] = s;       zx = max(zx, (long long)s.size());     }     for (int i = -2; i < zx; i++) cout <<  * ;     cout <<   n ;     for (int i = 0; i < n; i++) {       int k = zx - (long long)a[i].size();       cout <<  * ;       for (int j = 0; j < (k + x) / 2; j++) cout <<    ;       cout << a[i].c_str();       for (int j = (k + x) / 2; j < k; j++) cout <<    ;       cout <<  *             <<   n ;       if (k & 1) x = 1 - x;     }     for (int i = -2; i < zx; i++) cout <<  * ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int samples, len, temp, sz;     cin >> samples >> len;     deque<int> q;     set<int> s;     sz = s.size();     for (; samples--;) {       cin >> temp;       s.insert(temp);       if (s.size() > sz) {         q.push_front(temp);         if (q.size() > len) {           s.erase(q[q.size() - 1]);           q.pop_back();         }       }       sz = s.size();     }     printf( %d n , q.size());     for (auto x : q) printf( %d  , x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct inter {     int l, r, id;     inter() { l = r = id = -1; }     inter(int _l, int _r, int _id) { l = _l, r = _r, id = _id; }     bool operator<(const inter &r) const { return l < r.l; }   };   int N, L, R;   int A[100005], C[100005];   int B[100005];   map<int, inter> S;   int main() {     while (scanf( %d%d%d , &N, &L, &R) == 3) {       for (int i = 0; i < N; i++) scanf( %d , A + i);       for (int i = 0; i < N; i++)         scanf( %d , C + i), S[C[i]] = inter(L - A[i], R - A[i], i);       int nowL = -2147483646;       int found = 1;       for (map<int, inter>::iterator it = S.begin(); it != S.end(); it++) {         nowL = max(nowL, it->second.l);         if (nowL > it->second.r) {           found = -1;           break;         }         B[it->second.id] = nowL++;       }       if (found == -1) {         printf( -1 );       } else {         for (int i = 0; i < N; i++) {           if (i) printf(   );           printf( %d , B[i] + A[i]);         }       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, c;   int a[1005];   bool check() {     bool good = a[0] > 0;     for (int i = 1; i < n; i++) good &= (a[i] > 0) && (a[i] >= a[i - 1]);     return good;   }   int main() {     cin >> n >> m >> c;     for (int i = 0; i < n; i++) a[i] = 0;     while (m--) {       if (check()) return 0;       int p;       cin >> p;       if (p <= c / 2) {         for (int i = 0; i < n; i++) {           if (a[i] == 0) {             a[i] = p;             cout << i + 1;             break;           }           if (a[i] > p) {             a[i] = p;             cout << i + 1;             break;           }         }       } else {         for (int i = n - 1; i >= 0; i--) {           if (a[i] == 0) {             a[i] = p;             cout << i + 1;             break;           }           if (a[i] < p) {             a[i] = p;             cout << i + 1;             break;           }         }       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1 << 19;   long long n, k;   long long ans[maxn], mx[maxn], mn[maxn];   bool occ[maxn];   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     for (long long i = 0; i < (long long)(1 << k); ++i)       mx[i] = -1e9, mn[i] = 1e9, ans[i] = 2e9;     for (long long i = 0; i < (long long)(n); ++i) {       long long u;       cin >> u;       occ[u] = 1, mx[u] = u, mn[u] = u;     }     for (long long i = 0; i < k; ++i) {       for (long long j = 0; j < (1 << k); j += (1 << i + 1)) {         for (long long t = 0; t < (long long)(1 << i); ++t) {           ans[j + t] = ans[j + t + (1 << i)] =               min(ans[j + t], ans[j + t + (1 << i)]);           ans[j + t] = min(ans[j + t], mn[j + t + (1 << i)] - mx[j + t]);           ans[j + t + (1 << i)] =               min(ans[j + t + (1 << i)],                   mn[j + t] - mx[j + t + (1 << i)] + (1 << i + 1));         }       }       for (long long j = 0; j < (1 << k); j += (1 << i + 1)) {         for (long long t = 0; t < (long long)(1 << i); ++t) {           long long upmn0 = min(mn[j + t], mn[j + t + (1 << i)]);           long long upmn1 =               min(mn[j + t + (1 << i)] - (1 << i), mn[j + t] + (1 << i));           long long upmx0 = max(mx[j + t], mx[j + t + (1 << i)]);           long long upmx1 =               max(mx[j + t + (1 << i)] - (1 << i), mx[j + t] + (1 << i));           mn[j + t] = upmn0, mn[j + t + (1 << i)] = upmn1;           mx[j + t] = upmx0, mx[j + t + (1 << i)] = upmx1;         }       }     }     for (long long i = 0; i < (long long)(1 << k); ++i) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ncr(int n, int r) {     int ret = 1;     for (int i = 1; i <= n - r; i++) {       ret = (ret * (n - r + i)) / i;     }     return ret;   }   int main() {     int n;     cin >> n;     int x;     long long pos = 0;     long long neg = 0;     long long prod = 1;     for (int i = 0; i < n; i++) {       scanf( %d , &x);       if (x < 0) {         prod = (prod + 1) & 1;       }       if (prod) {         pos++;       } else {         neg++;       }     }     cout << neg * pos + neg <<    ;     cout << (neg * (neg - 1)) / 2 + (pos * (pos - 1)) / 2 + pos << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   int a[maxn];   struct node {     int l, r, Max, Min, pa, pi;   } T[maxn << 2];   void pushup(int o) {     T[o].Min = min(T[o << 1].Min, T[o << 1 | 1].Min);     T[o].Max = max(T[o << 1].Max, T[o << 1 | 1].Max);     if (T[o].Min == T[o << 1].Min)       T[o].pi = T[o << 1].pi;     else       T[o].pi = T[o << 1 | 1].pi;     if (T[o].Max == T[o << 1].Max)       T[o].pa = T[o << 1].pa;     else       T[o].pa = T[o << 1 | 1].pa;   }   void build(int o, int l, int r) {     T[o].l = l;     T[o].r = r;     if (l == r) {       T[o].Max = T[o].Min = a[l];       T[o].pa = T[o].pi = l;       return;     }     int mid = (l + r) >> 1;     build(o << 1, l, mid);     build(o << 1 | 1, mid + 1, r);     pushup(o);   }   int query(int o, int l, int r, int op) {     if (T[o].l == l && T[o].r == r) {       if (op == 1)         return T[o].Max;       else if (op == 2)         return T[o].Min;       else if (op == 3)         return T[o].pa;       else         return T[o].pi;     }     int mid = (T[o].l + T[o].r) >> 1;     if (r <= mid)       return query(o << 1, l, r, op);     else if (l > mid)       return query(o << 1 | 1, l, r, op);     else {       if (op == 1)         return max(query(o << 1, l, mid, 1), query(o << 1 | 1, mid + 1, r, 1));       else if (op == 2)         return min(query(o << 1, l, mid, 2), query(o << 1 | 1, mid + 1, r, 2));       else if (op == 3) {         int ans1 = query(o << 1, l, mid, 1),             ans2 = query(o << 1 | 1, mid + 1, r, 1);         int ans = max(ans1, ans2);         if (ans == ans1)           return query(o << 1, l, mid, 3);         else           return query(o << 1 | 1, mid + 1, r, 3);       } else {         int ans1 = query(o << 1, l, mid, 2),             ans2 = query(o << 1 | 1, mid + 1, r, 2);         int ans = min(ans1, ans2);         if (ans == ans1)           return query(o << 1, l, mid, 4);         else           return query(o << 1 | 1, mid + 1, r, 4);       }     }   }   int main() {     int n, m;     while (~scanf( %d%d , &n, &m)) {       for (int i = 1; i <= n; i++) scanf( %d , a + i);       build(1, 1, n);       int l, r, x, Max, Min, pa, pi;       while (m--) {         scanf( %d%d%d , &l, &r, &x);         Max = query(1, l, r, 1);         Min = query(1, l, r, 2);         pa = query(1, l, r, 3);         pi = query(1, l, r, 4);         int ans;         if (Max == Min && Max == x)           ans = -1;         else if (Min != x)           ans = pi;         else if (Max != x)           ans = pa;         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   typedef struct pair {     int x, y;   } pair;   int const N = 1e4 + 5;   int n = 8;   pair sMatrix[N], sac[N];   char matrix[N][N], mariaMove[10][10][100];   pair anna, maria;   int i, j, k, s, m, h, t;   int kontrol(int x, int y) {     int i, j;     for (i = 0; i < s; i++) {       if (sMatrix[i].x == x && sMatrix[i].y == y) return 0;     }     return 1;   }   void sDown(int add) {     int i;     for (i = 0; i < s; i++) {       sMatrix[i].x += add;     }   }   void wrtS() {     int i;     for (i = 0; i < s; i++) {       printf( %d-SS-%d , sMatrix[i].x, sMatrix[i].y), puts(  );     }   }   void dfs(int x, int y, int t) {     int i, j, k;     if (x == anna.x && y == anna.y) {       printf( WIN ), puts(  );       exit(0);     }     if (!(!mariaMove[x][y][t] && x >= 0 && x < n && y >= 0 && y < n) ||         !kontrol(x, y))       return;     sDown(1);     if (!kontrol(x, y)) {       sDown(-1);       return;     }     mariaMove[x][y][t] = 1;     for (i = -1, k = 0; i <= 1; i++) {       for (j = -1; j <= 1; j++, k++) {         dfs(x + i, y + j, k);       }     }     sDown(-1);   }   void clearMariaMove() {     int i, j, k;     for (i = 0; i < 10; i++) {       for (j = 0; j < 10; j++) {         for (k = 0; k < 100; k++) {           mariaMove[i][j][k] = 0;         }       }     }   }   void clear_sMatrix() {     int i;     for (i = 0; i < s; i++) {       sMatrix[i].x = sac[i].x;       sMatrix[i].y = sac[i].y;     }   }   int main() {     for (i = 0; i < n; i++) {       scanf( %s , matrix[i]);       for (j = 0; j < n; j++) {         if (matrix[i][j] ==  S ) {           sMatrix[s].x = i, sMatrix[s].y = j;           sac[s].x = i, sac[s].y = j;           s++;         }       }     }     maria.x = 7, maria.y = 0;     anna.x = 0, anna.y = 7;     for (i = -1; i <= 0; i++) {       for (j = 0; j <= 1; j++) {         dfs(maria.x + i, maria.y + j, 0);         clearMariaMove();         clear_sMatrix();       }     }     puts( LOSE );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   clock_t timeStart, timeFinish;   void timeBegin() { timeStart = clock(); }   void timeEnd() { timeFinish = clock(); }   void timeDuration() {     double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);     cout <<  Time taken by program is :   << fixed << time_taken          << setprecision(5);     cout <<   sec   << endl;   }   long long int n, h, a[105];   bool fun(long long int x) {     long long int val = 0;     for (int i = 0; i < n - 1; i++) {       val += min(a[i + 1] - a[i], x);     }     val += x;     if (val >= h) {       return true;     }     return false;   }   void solve() {     cin >> n >> h;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     long long int l = 1, r = 1000000000000000000;     long long int ans = r;     while (l <= r) {       long long int mid = l + (r - l) / 2;       if (fun(mid)) {         ans = mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     cout << ans <<   n ;     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     for (int i = 0; i < t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int INF = 1e9 + 7;   const int N = 2e5 + 7;   const int M = 1e6 + 7;   long long a[N], b[N];   long long dp[N][2];   signed main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (i > 1) b[i - 1] = abs(a[i] - a[i - 1]);     }     for (int i = 1; i <= n; i++) {       dp[i][0] = max(dp[i - 1][1] + b[i], b[i]);       dp[i][1] = max(dp[i - 1][0] - b[i], 0ll);     }     long long ans = 0;     for (int i = 1; i < n; i++) ans = max(ans, max(dp[i][0], dp[i][1]));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   const int INF = 0x3f3f3f3f;   int n, w[N], h[N];   int maxx;   int sum, sum1, ans = INF;   inline void fuck(int id, int tot) {     sum1 = sum;     priority_queue<int> q;     int tall;     if (tot == 1)       tall = w[id], sum1 = sum1 - w[id] + h[id];     else       tall = h[id];     for (int i = 1; i <= n; ++i) {       if (i == id) continue;       if (w[i] > tall && h[i] > tall) return;       if (w[i] > tall || h[i] > tall) {         if (h[i] > tall) sum1 = sum1 - w[i] + h[i], tot++;       } else if (w[i] <= tall && h[i] <= tall) {         q.push(w[i] - h[i]);       }     }     if (tot > n / 2) return;     while (!q.empty()) {       tot++;       if (tot > n / 2) break;       int now = q.top();       q.pop();       if (now <= 0) break;       sum1 -= now;     }     ans = min(ans, sum1 * tall);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d%d , &w[i], &h[i]), sum += w[i];     for (int i = 1; i <= n; ++i) {       fuck(i, 0);       fuck(i, 1);     }     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, s, q, ans = 0;     cin >> t >> s >> q;     int downloaded = s;     while (downloaded < t) {       downloaded += s * (q - 1);       s = downloaded;       ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 5;   struct Node {     long long ls, rs, val, key, cnt, tag, add;   };   Node t[maxn];   long long pol;   void newnode(long long vv) {     t[++pol].val = vv;     t[pol].cnt = 0;     t[pol].key = rand();     t[pol].ls = t[pol].rs = 0;     t[pol].tag = t[pol].add = 0;     return;   }   void lazy1(long long p, long long k) {     t[p].val += k;     return;   }   void lazy2(long long p, long long k) {     t[p].cnt += k;     return;   }   void pushdown(long long p) {     t[t[p].ls].tag += t[p].tag;     t[t[p].rs].tag += t[p].tag;     lazy1(t[p].ls, t[p].tag);     lazy1(t[p].rs, t[p].tag);     t[p].tag = 0;     t[t[p].ls].add += t[p].add;     t[t[p].rs].add += t[p].add;     lazy2(t[p].ls, t[p].add);     lazy2(t[p].rs, t[p].add);     t[p].add = 0;     return;   }   long long merge(long long x, long long y) {     if (!x || !y) return x + y;     if (t[x].key < t[y].key) {       pushdown(x);       t[x].rs = merge(t[x].rs, y);       return x;     } else {       pushdown(y);       t[y].ls = merge(x, t[y].ls);       return y;     }     return 0;   }   void split(long long now, long long &rx, long long &ry, long long k) {     if (!now) {       rx = ry = 0;       return;     }     pushdown(now);     if (t[now].val <= k) {       rx = now;       split(t[now].rs, t[rx].rs, ry, k);     } else {       ry = now;       split(t[now].ls, rx, t[ry].ls, k);     }     return;   }   long long rt, rx, ry, rz;   void ins(long long nd) {     split(rt, rx, rz, t[nd].val);     rt = merge(merge(rx, nd), rz);     return;   }   void dfs(long long now, long long cc) {     if (!now) return;     pushdown(now);     t[now].val -= cc;     t[now].cnt++;     dfs(t[now].ls, cc);     dfs(t[now].rs, cc);     t[now].ls = t[now].rs = 0;     ins(now);     return;   }   void out(long long now) {     if (!now) return;     pushdown(now);     out(t[now].ls);     cout << t[now].val <<    ;     out(t[now].rs);     return;   }   void costv(long long cc) {     split(rt, rx, ry, cc - 1);     split(ry, ry, rz, 2 * cc);     t[rz].tag -= cc;     lazy1(rz, -cc);     t[rz].add++;     lazy2(rz, 1);     rt = merge(rx, rz);     dfs(ry, cc);     return;   }   struct tshirts {     long long c, q;     bool friend operator<(tshirts t1, tshirts t2) {       return t1.q == t2.q ? t1.c < t2.c : t1.q > t2.q;     }   };   tshirts st[maxn];   long long n, k;   long long v[maxn];   void dfsans(long long now) {     if (!now) return;     pushdown(now);     dfsans(t[now].ls);     dfsans(t[now].rs);     return;   }   int main() {     scanf( %lld , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld %lld , &st[i].c, &st[i].q);     }     sort(st + 1, st + n + 1);     scanf( %lld , &k);     for (int i = 1; i <= k; i++) {       scanf( %lld , &v[i]);       newnode(v[i]);       ins(i);     }     for (int i = 1; i <= n; i++) {       costv(st[i].c);     }     dfsans(rt);     for (int i = 1; i <= k; i++) {       printf( %lld  , t[i].cnt);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mas[1010];   unordered_map<long long, int> sootv;   long long num[100100];   long long gcd(long long a, long long b) {     if (!a) return b;     return gcd(b % a, a);   }   int main() {     int n;     long long k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> mas[i];     }     if (k == 1) {       cout << 1 << endl;       int minidx = 1;       for (int i = 0; i < n; i++) {         if (mas[i] < mas[minidx]) minidx = i;       }       cout << minidx + 1;       return 0;     }     vector<long long> divs;     vector<long long> rdivs;     for (long long i = 1; i * i <= k; i++) {       if (k % i == 0) {         divs.push_back(i);         if (i * i != k) {           rdivs.push_back(k / i);         }       }     }     for (auto it = rdivs.rbegin(); it != rdivs.rend(); it++) {       divs.push_back(*it);     }     int cnt = 1;     sootv.rehash((int)(divs).size() * 5);     for (int i = 0; i < (int)(divs).size(); i++) {       sootv.emplace(divs[i], cnt);       num[cnt] = divs[i];       cnt++;     }     int oo = 1e9;     vector<vector<int> > dp(n + 3);     vector<vector<long long> > sum(n + 3);     vector<vector<int> > lst(n + 3);     for (int i = 0; i < (int)(dp).size(); i++) {       dp[i].resize(cnt + 3);       lst[i].resize(cnt + 3);       sum[i].resize(cnt + 3);       for (int j = 0; j < (int)(dp[i]).size(); j++) {         dp[i][j] = oo;       }       for (int j = 0; j < (int)(lst[i]).size(); j++) {         lst[i][j] = -1;       }       for (int j = 0; j < (int)(sum[i]).size(); j++) {         sum[i][j] = (long long)oo * (long long)oo;       }     }     dp[0][sootv[k]] = 0;     sum[0][sootv[k]] = 0;     int res = oo;     for (int i = 0; i < n; i++) {       for (int divi = 1; divi < cnt; divi++) {         if (dp[i][divi] >= oo) continue;         if (dp[i + 1][divi] > dp[i][divi] ||             dp[i + 1][divi] == dp[i][divi] && sum[i + 1][divi] > sum[i][divi]) {           lst[i + 1][divi] = divi;           dp[i + 1][divi] = dp[i][divi];           sum[i + 1][divi] = sum[i][divi];         }         long long nxt = num[divi] / gcd(num[divi], mas[i]);         if (nxt == num[divi]) continue;         int idx = sootv[nxt];         if (dp[i + 1][idx] > dp[i][divi] + 1 ||             dp[i + 1][idx] == dp[i][divi] + 1 &&                 sum[i + 1][idx] > sum[i][divi] + mas[i]) {           lst[i + 1][idx] = divi;           dp[i + 1][idx] = dp[i][divi] + 1;           sum[i + 1][idx] = sum[i][divi] + mas[i];         }       }     }     res = dp[n][sootv[1]];     if (res == oo) {       res = -1;       cout << res << endl;       return 0;     }     cout << res << endl;     vector<int> path;     int cur = sootv[1];     for (int i = n; i >= 1; i--) {       int l = lst[i][cur];       if (l != cur) {         path.push_back(i);       }       cur = l;       if ((int)(path).size() == res) break;     }     for (int i = (int)(path).size() - 1; i >= 0; i--) printf( %d  , path[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000;   int aa[N];   map<int, int> mp;   set<int> s;   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> aa[i];     for (int i = 0; i < n; i++) {       mp[aa[i]]++;       if (mp[aa[i]] == 1)         s.insert(aa[i]);       else         s.erase(aa[i]);       int j = i - k;       if (j >= 0) {         mp[aa[j]]--;         if (mp[aa[j]] == 1)           s.insert(aa[j]);         else           s.erase(aa[j]);       }       if (i >= k - 1)         if (!s.empty())           cout << *--s.end() <<   n ;         else           cout <<  Nothing n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   class DSU {    public:     vector<int> parent;     vector<int> sizes;     int noOfComponenents;     DSU(int N) {       parent.resize(N);       sizes.resize(N);       noOfComponenents = N;       for (int i = 0; i < N; ++i) {         parent[i] = i;         sizes[i] = 1;       }     }     int find(int x) {       if (parent[x] != x) parent[x] = find(parent[x]);       return parent[x];     }     void unify(int x, int y) {       x = find(x);       y = find(y);       if (isConnected(x, y)) {         return;       }       --noOfComponenents;       sizes[y] += sizes[x];       parent[x] = y;     }     bool isConnected(int p, int q) { return find(p) == find(q); }     int componentSize(int p) { return sizes[p]; }   };   void solve() {     int n;     cin >> n;     string s, t;     cin >> s >> t;     DSU dsu(20);     for (int i = 0; i < n; ++i) {       if (s[i] == t[i]) {         continue;       }       if (s[i] > t[i]) {         cout << -1 <<   n ;         return;       }       dsu.unify(s[i] -  a , t[i] -  a );     }     cout << 20 - dsu.noOfComponenents <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long C(long long n, long long m) {     long long res = 1;     for (long long i = n; i > n - m; i--) res *= i;     for (long long i = m; i >= 1; i--) res /= i;     return res;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     long long n, k, res = 1;     cin >> n >> k;     if (k >= 2) res += C(n, 2);     if (k >= 3) res += C(n, 3) * 2;     if (k >= 4) res += C(n, 4) * 9;     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const int M = 205;   long long n, m, k, cnt, ans;   long long g[N], h[N], dp[N][M];   struct redpocket {     long long s, t, d, w;     bool operator<(const redpocket &x) const {       if (w == x.w) return d < x.d;       return w < x.w;     }   } a[N];   priority_queue<redpocket> q;   bool cmp(redpocket x, redpocket y) { return x.s < y.s; }   signed main() {     scanf( %lld %lld %lld , &n, &m, &k);     for (long long i = 1; i <= k; i++)       scanf( %lld %lld %lld %lld , &a[i].s, &a[i].t, &a[i].d, &a[i].w);     sort(a + 1, a + k + 1, cmp);     g[0] = 1;     cnt = 1;     for (long long i = 1; i <= n; i++) {       while (i >= a[cnt].s && cnt <= k) q.push(a[cnt++]);       if (q.empty())         g[i] = i + 1;       else {         if (q.top().t < i) {           i--;           q.pop();           continue;         }         g[i] = q.top().d + 1;         h[i] = q.top().w;       }     }     memset(dp, 0x3f, sizeof(dp));     dp[0][0] = 0;     for (long long i = 0; i <= n; i++) {       for (long long j = 0; j <= m; j++) {         dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);         dp[g[i]][j] = min(dp[g[i]][j], dp[i][j] + h[i]);       }     }     ans = 0x3f3f3f3f3f3f3f3f;     for (long long i = 0; i <= m; i++) ans = min(ans, dp[n + 1][i]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void PT(long long a);   void PT1(long long a);   const long long M = 2010, mod = 1e9 + 7;   long long n, m, x, y, a, b, dp[M][M], mp[M][M], mp1[M][M];   vector<string> v;   void bfs(long long r, long long c, long long l, long long ri) {     queue<pair<long long, long long>> q;     q.push({r, c});     mp[r][c] = l;     mp1[r][c] = ri;     while (!q.empty()) {       r = (q.front()).first;       c = (q.front()).second;       q.pop();       l = mp[r][c];       ri = mp1[r][c];       if (l <= a && ri <= b) dp[r][c] = 1;       if (c - 1 >= 0 && v[r][c - 1] !=  * ) {         if (l + 1 <= a && ri <= b &&             (mp[r][c - 1] > l + 1 || mp1[r][c - 1] > ri)) {           q.push({r, c - 1});           mp[r][c - 1] = l + 1;           mp1[r][c - 1] = ri;         }       }       if (c + 1 < m && v[r][c + 1] !=  * ) {         if (l <= a && ri + 1 <= b &&             (mp[r][c + 1] > l || mp1[r][c + 1] > ri + 1)) {           q.push({r, c + 1});           mp[r][c + 1] = l;           mp1[r][c + 1] = ri + 1;         }       }       if (r - 1 >= 0 && v[r - 1][c] !=  * ) {         if (l <= a && ri <= b && (mp[r - 1][c] > l || mp1[r - 1][c] > ri)) {           q.push({r - 1, c});           mp[r - 1][c] = l;           mp1[r - 1][c] = ri;         }       }       if (r + 1 < n && v[r + 1][c] !=  * ) {         if (l <= a && ri <= b && (mp[r + 1][c] > l || mp1[r + 1][c] > ri)) {           q.push({r + 1, c});           mp[r + 1][c] = l;           mp1[r + 1][c] = ri;         }       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     {       cin >> n >> m >> x >> y >> a >> b;       for (long long i = 0; i < n; i++) {         string s;         cin >> s;         v.push_back(s);       }       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           mp[i][j] = INT_MAX;           mp1[i][j] = INT_MAX;         }       }       bfs(x - 1, y - 1, 0, 0);       long long ans = 0;       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           if (dp[i][j] == 1) ans++;         }       }       cout << ans;     }     return 0;   }   void PT(long long a) { cout << a <<   n ; }   void PT1(long long a) { cout << a <<    ; }


#include <bits/stdc++.h>   using namespace std;   int main() {     long int i, j, d, m, n, a[100000];     while (cin >> n) {       for (i = 1; i <= n; i++) {         cin >> a[i];       }       i = 1;       j = n;       int A = a[i], B = a[j];       int ca = 0, cb = 0;       while (1) {         if (A > B) {           B += a[--j];           cb++;         } else if (B >= A) {           A += a[++i];           ca++;         }         if (i > j) break;       }       cout << ca <<     << cb << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<long long> sc(n);     for (typeof(n) i = (0); i < (n); i++) cin >> sc[i];     int m;     cin >> m;     for (typeof(m) i = (0); i < (m); i++) {       long long w, h;       cin >> w >> h;       long long b = max(sc[0], sc[w - 1]);       sc[0] = max(sc[0], b + h);       cout << b << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC diagnostic ignored  -Wformat    template <typename T>   void printContainer(T& a) {     auto it = a.begin();     cout <<  {  << *(it++);     for (; it != a.end(); ++it) cout <<  ,   << (*it);     cout <<  } n ;   }   map<long long, int> A1, A2;   int sol;   long long solarea;   void dfs1(long long area, int opsSoFar) {     A1[area] = opsSoFar;     if (area % 3 == 0) {       if (!A1.count(area / 3 * 2)) {         dfs1(area / 3 * 2, opsSoFar + 1);       }     }     if (area % 2 == 0) {       if (!A1.count(area / 2)) dfs1(area / 2, opsSoFar + 1);     }   }   void dfs2(long long area, int opsSoFar) {     A2[area] = opsSoFar;     if (A1.count(area)) {       if (opsSoFar + A1[area] < sol) {         sol = opsSoFar + A1[area];         solarea = area;       }       return;     }     if (area % 3 == 0) {       if (!A2.count(area / 3 * 2)) {         dfs2(area / 3 * 2, opsSoFar + 1);       }     }     if (area % 2 == 0) {       if (!A2.count(area / 2)) dfs2(area / 2, opsSoFar + 1);     }   }   int threeFactors(long long num) {     int cnt = 0;     while (num % 3 == 0) num /= 3, cnt++;     return cnt;   }   int main() {     long long a1, b1, a2, b2;     cin >> a1 >> b1 >> a2 >> b2;     long long S1 = a1 * b1, S2 = a2 * b2;     dfs1(S1, 0);     sol = 1e9;     dfs2(S2, 0);     if (sol == 1e9) {       cout <<  -1  << endl;       return 0;     }     int T = 0, D = 0;     cout << sol << endl;     T = threeFactors(S1) - threeFactors(solarea);     long long tmp = S1;     for (int i = 0; i < (int)(T); ++i) tmp = (tmp / 3 * 2);     while (tmp != solarea) ++D, tmp /= 2;     while (a1 % 3 == 0 && T > 0) a1 = (a1 / 3 * 2), --T;     while (b1 % 3 == 0 && T > 0) b1 = (b1 / 3 * 2), --T;     while (a1 % 2 == 0 && D > 0) a1 = (a1 / 2), --D;     while (b1 % 2 == 0 && D > 0) b1 = (b1 / 2), --D;     cout << a1 <<     << b1 << endl;     T = 0, D = 0;     T = threeFactors(S2) - threeFactors(solarea);     tmp = S2;     for (int i = 0; i < (int)(T); ++i) tmp = (tmp / 3 * 2);     while (tmp != solarea) ++D, tmp /= 2;     while (a2 % 3 == 0 && T > 0) a2 = (a2 / 3 * 2), --T;     while (b2 % 3 == 0 && T > 0) b2 = (b2 / 3 * 2), --T;     while (a2 % 2 == 0 && D > 0) a2 = (a2 / 2), --D;     while (b2 % 2 == 0 && D > 0) b2 = (b2 / 2), --D;     cout << a2 <<     << b2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 17;   int l1[N], r1[N], a[N], use[N];   map<int, bool> mt;   void dfs(int v, int l, int r) {     if (a[v] > l && a[v] < r) mt[a[v]] = 1;     if (l1[v] != -1) dfs(l1[v], l, min(r, a[v]));     if (r1[v] != -1) dfs(r1[v], max(l, a[v]), r);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i] >> l1[i] >> r1[i];       if (l1[i] != -1) use[l1[i]] = 1;       if (r1[i] != -1) use[r1[i]] = 1;     }     for (int i = 1; i <= n; i++)       if (use[i] == 0) dfs(i, -1, 1e9 + 1);     int ans = 0;     for (int i = 1; i <= n; i++)       if (mt[a[i]] == 0) ans++;     cout << ans << endl;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int n;   int ar[100005];   int main() {     scanf( %d , &n);     if (n == 2) {       printf( 2 1 );       return 0;     }     for (int i = 1; i <= n; i++) ar[i] = i;     for (int i = 1; i <= n; i++) {       for (int j = i + i; j <= n; j += i) {         if (j % i == 0 && ar[j] % i == 0) swap(ar[i], ar[j]);       }     }     for (int i = 1; i <= n; i++) printf( %d  , ar[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pt {     long long x, y;     int id;     pt() {}     pt(long long a, long long b, int c = -1) : x(a), y(b), id(c) {}   };   pt operator+(pt a, pt b) { return pt(a.x + b.x, a.y + b.y); }   pt operator-(pt a, pt b) { return pt(a.x - b.x, a.y - b.y); }   long long dot(pt a, pt b) { return a.x * b.x + a.y * b.y; }   long long cross(pt a, pt b) { return a.x * b.y - a.y * b.x; }   long long sq(pt a) { return dot(a, a); }   long long dist(pt a, pt b) { return sq(a - b); }   bool operator<(pt a, pt b) { return make_pair(a.x, a.y) < make_pair(b.x, b.y); }   vector<pt> ConvexHull(vector<pt> a) {     sort(a.begin(), a.end());     vector<pt> b;     for (int i = 0; i < a.size(); i++) {       while (b.size() > 1 &&              cross(a[i] - b.back(), b[b.size() - 2] - b.back()) <= 0)         b.pop_back();       b.push_back(a[i]);     }     int sz = b.size();     for (int i = (int)a.size() - 2; i >= 0; i--) {       while (b.size() > sz &&              cross(a[i] - b.back(), b[b.size() - 2] - b.back()) <= 0)         b.pop_back();       b.push_back(a[i]);     }     b.pop_back();     return b;   }   const int N = 1050;   int ty[N];   void rot(vector<pt> &a) {     int p = -1;     for (int i = 1; i < a.size(); i++)       if (ty[a[i].id] != ty[a[0].id]) {         p = i;         break;       }     if (p == -1) return;     vector<pt> b;     for (int i = 0; i < a.size(); i++) b.push_back(a[(i + p) % a.size()]);     a = b;   }   vector<pair<int, int> > edges;   void AddEdge(int u, int v) { edges.push_back(make_pair(u, v)); }   long long Area(pt a, pt b, pt c) {     return abs(cross(a, b) + cross(b, c) + cross(c, a));   }   bool Inside(pt a, pt b, pt c, pt d) {     return Area(a, b, c) == Area(a, b, d) + Area(a, c, d) + Area(b, c, d);   }   void SolveTriangle(pt a, pt b, pt c, vector<pt> ps) {     int p = -1;     for (int i = 0; i < ps.size(); i++)       if (ty[ps[i].id] == ty[c.id]) p = i;     if (p == -1) {       for (int i = 0; i < ps.size(); i++) AddEdge(a.id, ps[i].id);     } else {       pt d = ps[p];       AddEdge(d.id, c.id);       vector<pt> nxt[3];       for (int i = 0; i < ps.size(); i++)         if (i != p) {           if (Inside(a, b, d, ps[i]))             nxt[0].push_back(ps[i]);           else if (Inside(c, d, a, ps[i]))             nxt[1].push_back(ps[i]);           else             nxt[2].push_back(ps[i]);         }       SolveTriangle(a, b, d, nxt[0]);       SolveTriangle(c, d, a, nxt[1]);       SolveTriangle(c, d, b, nxt[2]);     }   }   bool Check(vector<pt> hull) {     int l = -1, r = hull.size();     while (l + 1 < hull.size() && ty[hull[l + 1].id] == ty[hull[0].id]) l++;     while (r - 1 >= 0 && ty[hull[r - 1].id] == ty[hull.back().id]) r--;     if (r - l > 1) return 0;     return 1;   }   bool onHull[N];   void Triangulate(vector<pt> a) {     vector<pt> hull = ConvexHull(a);     rot(hull);     if (!Check(hull)) {       printf( Impossible n );       exit(0);     }     for (int i = 0; i < hull.size(); i++) onHull[hull[i].id] = 1;     if (ty[hull[0].id] == ty[hull.back().id]) {       for (int i = 0; i + 1 < hull.size(); i++)         AddEdge(hull[i].id, hull[i + 1].id);       int p = -1;       for (int i = 0; i < a.size(); i++)         if (!onHull[a[i].id]) {           if (ty[a[i].id] != ty[hull[0].id]) {             p = i;             break;           }         }       if (p != -1) {         for (int i = 0; i < hull.size(); i++) {           pt A = hull[i], B = hull[(i + 1) % hull.size()], C = a[p];           vector<pt> work;           for (int j = 0; j < a.size(); j++)             if (!onHull[a[j].id] && j != p) {               if (Inside(A, B, C, a[j])) work.push_back(a[j]);             }           SolveTriangle(A, B, C, work);         }       } else {         for (int i = 0; i < a.size(); i++)           if (!onHull[a[i].id]) AddEdge(hull[0].id, a[i].id);       }     } else {       int p = -1;       for (int i = 0; i < hull.size(); i++)         if (ty[hull[i].id] != ty[hull[0].id]) {           p = i;           break;         }       if (p == -1) {         printf( ??? n );         exit(0);       }       for (int i = 0; i + 1 < p; i++) {         pt A = hull[i], B = hull[i + 1], C = hull[p];         AddEdge(A.id, B.id);         vector<pt> work;         for (int j = 0; j < a.size(); j++)           if (!onHull[a[j].id]) {             if (Inside(A, B, C, a[j])) work.push_back(a[j]);           }         SolveTriangle(A, B, C, work);       }       for (int i = p; i + 1 < hull.size(); i++) {         pt A = hull[i], B = hull[i + 1], C = hull[0];         AddEdge(A.id, B.id);         vector<pt> work;         for (int j = 0; j < a.size(); j++)           if (!onHull[a[j].id]) {             if (Inside(A, B, C, a[j])) work.push_back(a[j]);           }         SolveTriangle(A, B, C, work);       }     }   }   int main() {     int n, i;     scanf( %i , &n);     vector<pt> ps(n);     for (int i = 0; i < n; i++) {       int x, y;       scanf( %i %i %i , &x, &y, &ty[i]);       ps[i] = pt(x, y, i);     }     if (n == 1) return 0 * printf( 0 n );     if (n == 2) {       if (ty[0] == ty[1])         printf( 1 n0 1 n );       else         printf( 0 n );       return 0;     }     Triangulate(ps);     printf( %i n , edges.size());     for (auto p : edges) printf( %i %i n , p.first, p.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005, mod = 1e9 + 7;   int n, head[N], nxt[N << 1], to[N << 1], num = 0, dfn[N], DFN = 0, a[N], dep[N];   inline void link(int x, int y) {     nxt[++num] = head[x];     to[num] = y;     head[x] = num;   }   namespace tr {   int fa[N], top[N], sz[N], son[N];   inline void dfs1(int x) {     sz[x] = 1;     dfn[x] = ++DFN;     for (int i = head[x]; i; i = nxt[i]) {       int u = to[i];       if (sz[u]) continue;       fa[u] = x;       dep[u] = dep[x] + 1;       dfs1(u);       sz[x] += sz[u];       if (sz[u] > sz[son[x]]) son[x] = u;     }   }   inline void dfs2(int x, int tp) {     top[x] = tp;     if (son[x]) dfs2(son[x], tp);     for (int i = head[x]; i; i = nxt[i])       if (to[i] != fa[x] && to[i] != son[x]) dfs2(to[i], to[i]);   }   inline int lca(int x, int y) {     while (top[x] != top[y]) {       if (dep[top[x]] < dep[top[y]]) swap(x, y);       x = fa[top[x]];     }     return dep[x] < dep[y] ? x : y;   }   void main() {     dep[1] = 1;     dfs1(1);     dfs2(1, 1);   }   }  // namespace tr   int prime[N], sp = 0, mu[N], phi[N], b[N], m = 0, id[N], g[N];   bool vis[N];   void priwork() {     phi[1] = 1;     mu[1] = 1;     for (int to, i = 2; i <= n; i++) {       if (!vis[i]) prime[++sp] = i, mu[i] = -1, phi[i] = i - 1;       for (int j = 1; j <= sp && i * prime[j] <= n; j++) {         vis[to = i * prime[j]] = 1;         if (i % prime[j])           mu[to] = -mu[i], phi[to] = phi[i] * (prime[j] - 1);         else {           phi[to] = phi[i] * prime[j];           break;         }       }     }     tr::main();   }   inline bool comp(int i, int j) { return dfn[i] < dfn[j]; }   int st[N], top, w[N], f[N], inv[N], ST[N], cnt = 0;   inline int solve() {     int sum = 0, x, lca, ret = 0, y = 0;     top = 0;     for (int i = 1; i <= m; i++)       sum = (sum + phi[a[b[i]]]) % mod, w[b[i]] = phi[a[b[i]]];     sort(b + 1, b + m + 1, comp);     st[++top] = b[1];     w[b[1]] = phi[a[b[1]]];     ST[++cnt] = b[1];     for (int i = 2; i <= m; i++) {       x = b[i];       y = 0;       lca = tr::lca(x, st[top]);       while (dep[lca] < dep[st[top]]) {         if (y)           ret = (ret + 1ll * (dep[y] - dep[st[top]]) * w[y] % mod *                            (sum - w[y] + mod) % mod) %                 mod;         if (y) w[st[top]] = (w[st[top]] + w[y]) % mod;         y = st[top--];       }       if (st[top] != lca) st[++top] = lca, ST[++cnt] = lca;       if (y)         ret = (ret + 1ll * (dep[y] - dep[st[top]]) * w[y] % mod *                          (sum - w[y] + mod) % mod) %               mod;       if (y) w[st[top]] = (w[st[top]] + w[y]) % mod;       if (st[top] != x) st[++top] = x, ST[++cnt] = x;     }     while (top > 1) {       ret = (ret + 1ll * (dep[st[top]] - dep[st[top - 1]]) * w[st[top]] % mod *                        (sum - w[st[top]] + mod) % mod) %             mod;       w[st[top - 1]] = (w[st[top]] + w[st[top - 1]]) % mod;       top--;     }     while (cnt) w[ST[cnt]] = 0, cnt--;     return (ret << 1) % mod;   }   long long qm(long long x, long long k) {     long long sum = 1;     while (k) {       if (k & 1) sum = sum * x % mod;       x = x * x % mod;       k >>= 1;     }     return sum;   }   void work() {     int x, y;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), id[a[i]] = i;     for (int i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       link(x, y);       link(y, x);     }     priwork();     for (int i = 1; i <= n; i++) {       m = 0;       for (int j = i; j <= n; j += i) b[++m] = id[j];       g[i] = solve();     }     for (int i = 1; i <= n; i++)       for (int j = i; j <= n; j += i) f[i] = (f[i] + mu[j / i] * g[j]) % mod;     int ans = 0;     for (int i = 1; i <= n; i++) inv[i] = qm(i, mod - 2);     for (int i = 1; i <= n; i++)       ans = (ans + 1ll * i * inv[phi[i]] % mod * f[i] % mod) % mod;     cout << ans * qm(1ll * n * (n - 1) % mod, mod - 2) % mod << endl;   }   int main() {     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double error = 1e-15;   const double pi = 2 * acos(0);   const int maxn = (int)1e5 + 10;   const int mod = (int)1e9;   int dx[] = {0, -1, 0, +1, -1, -1, +1, +1};   int dy[] = {-1, 0, +1, 0, -1, +1, +1, -1};   long A[205][200], B[200];   vector<int> v;   int main() {     ios_base::sync_with_stdio(0);     long i, j, n, m;     cin >> n;     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++) {         cin >> A[i][j];         if (A[i][j] == 1) {           B[i] = 1;         } else if (A[i][j] == 2) {           B[j] = 1;         } else if (A[i][j] == 3) {           B[i] = 1;           B[j] = 1;         }       }     m = 0;     for (i = 1; i <= n; i++)       if (B[i] == 0) {         m++;         v.push_back(i);       }     cout << m << endl;     for (i = 0; i < m; i++) cout << v[i] <<    ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 300010;   map<pair<pair<int, int>, int>, vector<int> > ans;   map<pair<pair<int, int>, int>, vector<pair<int, int> > > l;   map<pair<pair<int, int>, int>, vector<pair<int, int> > >::iterator it;   int n, m;   struct Point {     int x, y;     Point(int _x = 0, int _y = 0) : x(_x), y(_y){};     Point operator-(const Point &rhs) { return Point(x - rhs.x, y - rhs.y); }     Point operator*(int v) { return Point(v * x, v * y); }     Point operator+(const Point &rhs) { return Point(x + rhs.x, y + rhs.y); }     Point operator/(int v) { return Point(x / v, y / v); }     int len() { return x * x + y * y; }     friend int dis(Point a, Point b) { return Point(a - b).len(); }     void read() { scanf( %d%d , &x, &y); }   };   struct Circle {     int x, y, r;   } c[1510];   inline int Pow(int x) { return x * x; }   inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   pair<pair<int, int>, int> getLine(Point a, Point b) {     if (a.x == b.x) return make_pair(make_pair(0, 0), a.x);     if (a.y == b.y) return make_pair(make_pair(1, 0), a.y);     int dx = a.x - b.x;     int dy = a.y - b.y;     int d = gcd(abs(dx), abs(dy));     dx /= d;     dy /= d;     if (dx < 0) {       dx *= -1;       dy *= -1;     }     int v = (a.y % dy + dy) % dy;     v = a.x + (v - a.y) / dy * dx;     return make_pair(make_pair(dx, dy), v);   }   int getAns(pair<pair<int, int>, int> tmp, int x) {     if (l.find(tmp) == l.end()) return 0;     vector<pair<int, int> > &u = l[tmp];     int p = upper_bound(u.begin(), u.end(), make_pair(x, 1)) - u.begin();     return ans[tmp][p];   }   pair<pair<int, int>, int> getMirrorLine(Point a, Point b) {     Point mid = (a + b) / 2, off = a - b;     swap(off.x, off.y), off.x *= -1;     return getLine(mid + off, mid - off);   }   int main() {     scanf( %d%d , &n, &m);     Point a, b;     while (n--) {       a.read();       b.read();       a = a * 2;       b = b * 2;       if (a.x == b.x) {         l[getLine(a, b)].push_back(make_pair(min(a.y, b.y), 1));         l[getLine(a, b)].push_back(make_pair(max(a.y, b.y) + 1, -1));       } else {         l[getLine(a, b)].push_back(make_pair(min(a.x, b.x), 1));         l[getLine(a, b)].push_back(make_pair(max(a.x, b.x) + 1, -1));       }     }     for (it = l.begin(); it != l.end(); ++it) {       sort((*it).second.begin(), (*it).second.end());       vector<int> &tt = ans[(*it).first];       vector<pair<int, int> > &d = (*it).second;       int v = 0;       tt.push_back(v);       for (int i = 0; i <= d.size(); ++i) {         v += d[i].second;         tt.push_back(v);       }     }     for (int i = 1; i <= m; ++i) {       scanf( %d%d%d , &c[i].x, &c[i].y, &c[i].r);       c[i].x *= 2;       c[i].y *= 2;       c[i].r *= 2;     }     int cnt = 0;     for (int i = 1; i < m; ++i)       for (int j = i + 1; j <= m; ++j) {         if (c[i].r == c[j].r &&             dis(Point(c[i].x, c[i].y), Point(c[j].x, c[j].y)) >                 Pow(c[i].r + c[j].r)) {           int mid;           if (c[i].y == c[j].y)             mid = c[i].y;           else             mid = (c[i].x + c[j].x) >> 1;           cnt += getAns(               getMirrorLine(Point(c[i].x, c[i].y), Point(c[j].x, c[j].y)), mid);         }       }     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   int n, k;   long long f[100009], g[100009];   vector<int> p;   string s1, s2;   int main() {     cin >> s1 >> s2 >> k;     int i;     n = s1.size();     p.clear();     for (i = 1; i <= n; ++i) {       if (s1 == s2) p.push_back(i - 1);       s1.push_back(s1[0]);       s1.erase(0, 1);     }     f[0] = 1LL;     g[0] = 0LL;     for (i = 1; i <= k; ++i) {       f[i] = (g[i - 1] * (n - 1)) % MOD;       g[i] = (f[i - 1] + g[i - 1] * (n - 2)) % MOD;     }     long long ans = 0LL;     if (!p.empty()) {       for (i = 0; i < p.size(); ++i) {         if (p[i] == 0)           ans = (ans + f[k]) % MOD;         else           ans = (ans + g[k]) % MOD;       }     }     cout << ans << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long A[100005];   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     long long n;     long long i;     cin >> n;     for (i = 0; i < n; i++) cin >> A[i];     sort(A, A + n);     long long g, s, num;     s = num = 0;     for (i = 0; i < n; i++) {       s += A[i] * i - num;       num += A[i];     }     s = s * 2;     s += num;     g = gcd(s, n);     cout << s / g <<     << n / g << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 2, M = 1e6 + 2;   int n, m, X[N << 1], Y[N << 1], C[N][N];   void Merge(vector<pair<int, pair<int, int> > > &v) {     sort(v.begin(), v.end());     int q = 0;     for (int i = 1; i < (int)(v.size()); i++) {       if (v[i].first != v[q].first || v[i].second.first > v[q].second.second)         v[++q] = v[i];       else         v[q].second.second = v[i].second.second;     }     v.resize(q + 1);   }   bool Check(int t) {     vector<pair<int, pair<int, int> > > hor, ver;     for (int i = 0; i < m; i++)       for (int j = 0; j < m; j++) C[i][j] = 0;     for (int i = 0; i < n; i++)       hor.push_back(make_pair(Y[i], make_pair(X[i] - 2 * t, X[i] + 2 * t)));     for (int i = n; i < n + m; i++)       ver.push_back(make_pair(X[i], make_pair(Y[i] - 2 * t, Y[i] + 2 * t)));     Merge(hor), Merge(ver);     for (pair<int, pair<int, int> > a : hor) {       vector<int> adj;       for (int i = 0; i < (int)(ver.size()); i++) {         pair<int, pair<int, int> > b = ver[i];         if (b.second.first <= a.first && a.first <= b.second.second &&             a.second.first <= b.first && b.first <= a.second.second) {           for (int j : adj)             if (C[i][j]++) return true;           adj.push_back(i);         }       }     }     return false;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 0; i < n + m; i++) {       cin >> X[i] >> Y[i];       int x = X[i] - Y[i], y = X[i] + Y[i];       X[i] = x, Y[i] = y;     }     int dw = 0, up = M;     while (up - dw > 1) {       int md = (dw + up) >> 1;       if (Check(md))         up = md;       else         dw = md;     }     if (up == M)       cout <<  Poor Sereja!  << endl;     else       cout << up << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long MOD2 = 998244353;   double eps = 1e-12;   using namespace std;   template <typename A, typename B>   ostream &operator<<(ostream &os, const pair<A, B> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T_container, typename T = typename enable_if<                                       !is_same<T_container, string>::value,                                       typename T_container::value_type>::type>   ostream &operator<<(ostream &os, const T_container &v) {     os <<  { ;     string sep;     for (const T &x : v) os << sep << x, sep =  ,  ;     return os <<  } ;   }   void dbg_out() { cerr << endl; }   template <typename Head, typename... Tail>   void dbg_out(Head H, Tail... T) {     cerr <<     << H;     dbg_out(T...);   }   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma )   #pragma GCC optimize( unroll-loops )   template <typename T>   void get_ar(T *ar, long long n) {     for (long long i = 0; i < n; i++) cin >> ar[i];   }   template <typename T>   void printar(T *ar, long long n) {     for (long long i = 0; i < n; i++) cout << ar[i] <<    ;     cout <<   n ;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long imod(long long a, long long m) { return power(a, m - 2, m); }   template <typename T, size_t N, size_t M>   void print2dArray(T (&theArray)[N][M], long long n = N, long long m = M) {     for (long long x = 0; x < n; x++) {       for (long long y = 0; y < m; y++) {         cout << theArray[x][y] <<    ;       }       cout <<   n ;     }   }   template <typename T, size_t N>   void print_ar(T (&theArray)[N], long long n = N) {     for (long long x = 0; x < n; x++) {       cout << theArray[x] <<    ;     }     cout <<   n ;   }   struct DSU {     long long connected;     vector<long long> par, sz, mi, ma;     void init(long long n) {       par = sz = mi = ma = vector<long long>(n + 1, 0);       for (long long i = 1; i <= n; i++)         par[i] = i, sz[i] = 1, mi[i] = i, ma[i] = i;       connected = n;     }     long long getPar(long long u) {       while (u != par[u]) {         par[u] = par[par[u]];         u = par[u];       }       return u;     }     long long getSize(long long u) { return sz[getPar(u)]; }     void merge(long long u, long long v) {       long long par1 = getPar(u), par2 = getPar(v);       if (par1 == par2) return;       connected--;       if (sz[par1] > sz[par2]) swap(par1, par2);       long long aa = min(mi[par1], mi[par2]);       long long bb = max(ma[par1], ma[par2]);       sz[par2] += sz[par1];       sz[par1] = 0;       par[par1] = par[par2];       mi[par2] = aa;       ma[par2] = bb;     }   };   DSU dd;   vector<long long> pres, pref;   long long give(long long p1) {     long long l = dd.mi[p1], r = dd.ma[p1];     long long tot = pres[r] - pres[l - 1];     long long ans = pref[r] - pref[r - tot];     return ans;   }   void solve() {     long long n, m, q, K, ans = 0, tmp;     cin >> n >> m >> q;     vector<long long> a(n), b(m);     for (long long i = 0; i < n; i++) {       cin >> a[i];       ans += a[i];     }     for (long long i = 0; i < m; i++) {       cin >> b[i];     }     vector<vector<long long> > temp;     temp.push_back({0, 0});     for (long long i = 0; i < n; i++) {       temp.push_back({a[i], 1});     }     for (long long i = 0; i < m; i++) {       temp.push_back({b[i], 0});     }     dd.init(n + m + 1);     sort((temp).begin(), (temp).end());     pres.resize(n + m + 1, 0);     pref.resize(n + m + 1, 0);     for (long long i = 1; i < ((long long)(temp).size()); i++) {       pres[i] = pres[i - 1] + temp[i][1];       pref[i] = pref[i - 1] + temp[i][0];     }     vector<vector<long long> > diff;     for (long long i = 1; i < ((long long)(temp).size()) - 1; i++) {       diff.push_back({temp[i + 1][0] - temp[i][0], i});     }     sort((diff).begin(), (diff).end());     long long cur = 0;     vector<vector<long long> > quer;     for (long long i = 0; i < q; i++) {       long long x;       cin >> x;       quer.push_back({x, i});     }     sort((quer).begin(), (quer).end());     vector<long long> fin(q, 0);     for (auto x : quer) {       long long k = x[0], ind = x[1];       while (cur < ((long long)(diff).size()) && diff[cur][0] <= k) {         long long p1 = dd.getPar(diff[cur][1]), p2 = dd.getPar(diff[cur][1] + 1);         ans -= give(p1);         ans -= give(p2);         dd.merge(p1, p2);         long long pa = dd.getPar(p1);         ans += give(pa);         cur++;       }       fin[ind] = ans;     }     for (long long i = 0; i < q; i++) {       cout << fin[i] <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     for (long long it = 1; it <= t; it++) {       solve();     }     return 0;   }


#include <iostream>      using namespace std;         int main()   {       int n,q, l,r;       cin>>n>>q;       string s;       cin>>s;       int arr[n];       arr[0]=s[0]- a +1;       for(int i=1;i<n;i++){           arr[i]=arr[i-1]+s[i]- a +1;       }       while(q--){           cin>>l>>r;           cout<<(arr[r-1]-((l==1)?0:arr[l-2]))<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.sync_with_stdio(false), cin.tie(nullptr);     int tests;     cin >> tests;     while (tests--) {       int n, k;       string s;       cin >> n >> k >> s;       const int need = n / k;       vector<vector<int>> counts(k, vector<int>(26, 0));       for (int i = 0; i < n; i++) {         counts[i % k][s[i] -  a ]++;       }       int ans = 0;       for (int i = 0, j = k - 1; i <= j; i++, j--) {         int cost = need * 2 - counts[i][0] - counts[j][0];         for (int c = 1; c < 26; c++) {           cost = min(cost, need * 2 - counts[i][c] - counts[j][c]);         }         ans += (i == j ? cost / 2 : cost);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> solve(vector<int> a) {     int n = (int)a.size();     vector<int> dp(n + 1, 1e9);     dp[0] = -1e9;     vector<int> ret(n);     for (int i = 0; i < n; i++) {       int x = (int)(upper_bound(dp.begin(), dp.end(), a[i]) - dp.begin());       ret[i] = x;       dp[x] = a[i];     }     return ret;   }   int main() {     int n, m;     cin >> m >> n;     vector<int> a(n);     vector<int> delta(n + 1);     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--;       delta[x]++;       delta[y]--;     }     int temp = 0;     for (int i = 0; i < n; i++) {       temp += delta[i];       a[i] = temp;     }     auto l = solve(a);     reverse(a.begin(), a.end());     auto r = solve(a);     reverse(r.begin(), r.end());     int ans = 0;     for (int i = 0; i < n; i++) {       ans = max(ans, l[i] + r[i] - 1);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N = 1e9 + 7;   void solve() {     long long n;     cin >> n;     vector<long long> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     long long b[n];     long long sum = 0;     for (int i = 0; i < n; i++) sum += v[i];     long long x = (n * (n + 1)) / 2;     long long tot = sum / x;     if (sum % x != 0) {       cout <<  NO ;       return;     }     b[0] = v[n - 1] - v[0];     for (int i = 1; i < n; i++) {       b[i] = v[i - 1] - v[i];     }     long long ans = 0;     vector<long long> an;     for (int i = 0; i < n; i++) {       ans = (b[i] + tot) / (n);       an.push_back(ans);     }     if ((b[0] + tot) % n != 0) an[0] = -1;     int f = 1;     for (int i = 0; i < n; i++) {       if (an[i] <= 0) f = 0;     }     if (f) {       cout <<  YES n ;       for (auto y : an) cout << y <<    ;     } else {       cout <<  NO ;     }   }   int main() {     int t;     cin >> t;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     map<char, long long> m;     for (long long i = 0; i < 26; i++) {       char ch;       cin >> ch;       m[ch] = i;     }     string str;     cin >> str;     long long ans = 0, sum = 0;     for (long long i = 0; i < str.length() - 1; i++) {       sum = abs(m[str[i]] - m[str[i + 1]]);       ans += sum;     }     cout << ans <<   n ;     return;   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     long long t = 1;     cin >> t;     for (long long i = 0; i < t; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 3;   char ch;   bool flag = 0;   void read(int &x) {     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch ==  - )) || 1);          ch = getchar())       ;     for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar())       ;     x *= 1 - 2 * flag;   }   string ans =   ;   bool h = 0;   string s;   void fu() {     if (cin >> s) {       ans += s;       if (s ==  pair ) {         ans +=  < ;         fu();         ans +=  , ;         fu();         ans +=  > ;       }     } else {       h = 1;     }     if (h) return;   }   int main() {     int n;     read(n);     fu();     if (h || (cin >> s))       cout <<  Error occurred  << endl;     else       cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000000;   long long mod = 1000000007LL;   long long mod2 = 998244353LL;   int n;   int t, q;   long long a[100005], b[100005];   long long pre[100005];   struct node {     int l, r;     long long mx, mn;   } tree[100005 << 2];   void build(int id, int l, int r) {     tree[id].l = l;     tree[id].r = r;     if (l == r) {       tree[id].mx = pre[l];       tree[id].mn = pre[l];       return;     }     int mid = (l + r) / 2;     build(id * 2, l, mid);     build(id * 2 + 1, mid + 1, r);     tree[id].mx = max(tree[id * 2].mx, tree[id * 2 + 1].mx);     tree[id].mn = min(tree[id * 2].mn, tree[id * 2 + 1].mn);   }   long long query(int id, int l, int r, bool mx) {     if (tree[id].l == l && tree[id].r == r) {       return (mx ? tree[id].mx : tree[id].mn);     }     int mid = (tree[id].l + tree[id].r) / 2;     if (r <= mid)       return query(id * 2, l, r, mx);     else if (l > mid)       return query(id * 2 + 1, l, r, mx);     else {       long long r1 = query(id * 2, l, mid, mx);       long long r2 = query(id * 2 + 1, mid + 1, r, mx);       return (mx ? max(r1, r2) : min(r1, r2));     }   }   int main() {     cin >> n >> q;     for (int i = 1; i <= n; ++i) {       scanf( %lld , &a[i]);     }     for (int i = 1; i <= n; ++i) {       scanf( %lld , &b[i]);     }     pre[0] = 0LL;     for (int i = 1; i <= n; ++i) {       pre[i] = pre[i - 1] + b[i] - a[i];     }     build(1, 1, n);     while (q-- > 0) {       int l, r;       scanf( %d%d , &l, &r);       if (pre[r] - pre[l - 1] != 0 || query(1, l, r, false) < pre[l - 1]) {         puts( -1 );       } else {         printf( %lld n , query(1, l, r, true) - pre[l - 1]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long x, y;   vector<long long> cnt;   vector<char> sol;   bool ok;   void solve(long long x, long long y) {     if (!x) {       if (y == 1) ok = 1;       return;     }     if (!y) {       if (x == 1) ok = 1;       return;     }     if (x == y) {       if (x == 1) ok = 1;       return;     }     if (x > y) {       sol.push_back( A );       if (x % y == 0) {         cnt.push_back(x / y - 1);       } else {         cnt.push_back(x / y);       }       solve(x % y, y);     } else {       sol.push_back( B );       if (y % x == 0) {         cnt.push_back(y / x - 1);       } else {         cnt.push_back(y / x);       }       solve(x, y % x);     }   }   int main() {     cin >> x >> y;     solve(x, y);     if (ok) {       for (int i = 0; i < sol.size(); ++i) {         cout << cnt[i] << sol[i];       }     } else {       cout <<  Impossible ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char c[100003], b[100003];   int ggg[100003];   string a;   long long d, e, f, g, ming, oo, ksa, lxy;   bool nn, o;   int main() {     gets(c);     c[strlen(c)] =    ;     a = c;     for (int i = 0; i <= a.length(); i++) {       if (a.substr(i, 1) ==    ) {         if (o == 0) {           if (i >= 5 && a.substr(i - 5, 5) ==  liala )             nn = 1;           else if (i >= 4 && a.substr(i - 4, 4) ==  etra )             nn = 1;           else if (i >= 6 && a.substr(i - 6, 6) ==  inites )             nn = 1;           o = 1;         }         if (!nn) {           if (i >= 3 && a.substr(i - 3, 3) ==  etr ) {             ming += 1;             g++;             ggg[g] = 2;           } else if (i >= 6 && a.substr(i - 6, 6) ==  initis ) {             g++;             ggg[g] = 3;           } else if (i >= 4 && a.substr(i - 4, 4) ==  lios ) {             g++;             ggg[g] = 1;           } else {             cout <<  NO  << endl;             return 0;           }         }         if (nn) {           if (i >= 4 && a.substr(i - 4, 4) ==  etra ) {             g++;             ggg[g] = 2;             ming += 1;           } else if (i >= 6 && a.substr(i - 6, 6) ==  inites ) {             g++;             ggg[g] = 3;           } else if (i >= 5 && a.substr(i - 5, 5) ==  liala ) {             g++;             ggg[g] = 1;           } else {             cout <<  NO  << endl;             return 0;           }         }         oo++;       }     }     if (oo == 1) {       if (a.length() >= 5 && a.substr(a.length() - 5, 4) ==  lios ) ming = 1;       if (a.length() >= 4 && a.substr(a.length() - 4, 3) ==  etr ) ming = 1;       if (a.length() >= 7 && a.substr(a.length() - 7, 6) ==  inites ) ming = 1;       if (a.length() >= 7 && a.substr(a.length() - 7, 6) ==  initis ) ming = 1;       if (a.length() >= 6 && a.substr(a.length() - 6, 5) ==  liala ) ming = 1;       if (a.length() >= 5 && a.substr(a.length() - 5, 4) ==  etra ) ming = 1;     }     for (int i = 1; i <= 100002; i++) {       if (ggg[i] == 0) {         ksa = 1;         break;       }       if (ggg[i] < ggg[i - 1]) break;     }     if (ming == 1 && oo == 1) {       cout <<  YES  << endl;       return 0;     } else if (oo == 1) {       cout <<  NO  << endl;       return 0;     }     if (ming == 1 && ksa == 1)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     double x, y;   };   Point points[100000];   int gcd(int a, int b) {     if (b) return gcd(b, a % b);     return a;   }   int main() {     int n;     while (scanf( %d , &n) != EOF) {       long double A = 0.0;       long double b = 0.0;       for (int i = int(0); i < int(n); i++) {         scanf( %lf %lf , &points[i].x, &points[i].y);       }       long double totProb = 1.0;       if (n < 60) {         totProb = pow(2., n) - n * (n - 1) / 2. - n - 1;         for (int i = int(0); i < int(n); i++) {           for (int j = int(1); j < int(n); j++) {             int k = (i + j) % n;             long double prob = pow(2, n - j - 1) - 1.;             A += prob * (points[i].x * points[k].y - points[k].x * points[i].y);             b += prob * gcd(abs(points[i].x - points[k].x),                             abs(points[i].y - points[k].y));           }         }       } else {         for (int i = int(0); i < int(n); i++) {           for (int j = int(1); j < int(60); j++) {             int k = (i + j) % n;             long double prob = pow(2, -j - 1);             A += prob * (points[i].x * points[k].y - points[k].x * points[i].y);             b += prob * gcd(abs(points[i].x - points[k].x),                             abs(points[i].y - points[k].y));           }         }       }       double ans = (A - b) / totProb / 2. + 1.;       printf( %.12lf n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long MAX = 6e6 + 10;   int w[1000];   int a[1000];   void solve() {     int n;     cin >> n;     memset(a, 0, sizeof(a));     for (int i = 0; i < n; i++) {       cin >> w[i];       a[w[i]]++;     }     int ans = 0;     for (int i = 2; i <= n * 2; i++) {       int sum = 0;       for (int j = 0; j <= i; j++) {         sum += min(a[j], a[i - j]);       }       ans = max(ans, sum / 2);     }     cout << ans << endl;   }   int main() {     int _ = 1;     cin >> _;     while (_--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, i, a[200], s1, s2, j = 2;   string can =  YES ;   int main() {     cin >> n;     cin >> x;     x = 7 - x;     for (i = 0; i < n * 2; i++) {       cin >> a[i];     }     for (i = 1; i < n; i++) {       s1 = a[j];       s2 = a[j + 1];       if (x == s1 || x == s2 || x == 7 - s2 || x == 7 - s1) {         can =  NO ;         break;       }       j += 2;     }     cout << can;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 502;   long long c[maxN][maxN];   long long a[maxN];   int n, m, b, md;   int main() {     cin >> n >> m >> b >> md;     for (int i = 1; i <= n; i++) cin >> a[i];     c[0][0] = 1;     for (int i = 1; i <= n; i++)       for (int j = 0; j < m; j++)         for (int t = 0; t <= b - a[i]; t++)           if (c[j][t] > 0) {             c[j + 1][t + a[i]] = (c[j + 1][t + a[i]] + c[j][t]) % md;           }     long long ans = 0;     for (int i = 0; i <= b; i++) ans = (ans + c[m][i]) % md;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void print(vector<long long> v) {     long long n = v.size();     for (long long i = 0; i < n; i++) {       cout << v[i] <<    ;     }   }   long long power(long long a, long long b) {     long long res = 1;     while (b != 0) {       if (b & 1) {         res *= a;       }       a = (a * a);       b = (b / 2);     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long n;     cin >> n;     vector<long long> v(n);     for (long long i = 0; i < n; i++) {       cin >> v[i];     }     if (n <= 3) {       cout << 1;       return 0;     }     vector<long long> v1 = v;     sort(v1.begin(), v1.end());     vector<long long> diff;     map<long long, vector<long long> > mp;     for (long long i = 1; i < n; i++) {       diff.push_back(v1[i] - v1[i - 1]);       mp[diff[i - 1]].push_back(i);     }     if (mp.size() > 3) {       cout << -1;       return 0;     }     if (mp.size() == 1) {       for (long long i = 0; i < n; i++) {         if (v[i] == v1[0]) {           cout << i + 1;           return 0;         }       }     }     if (mp.size() == 2) {       auto it = mp.begin();       vector<long long> v2 = it->second;       if (v2.size() == 1) {         long long x = v2[0], y;         if (x == 1) y = v1[0];         if (x == n - 1) y = v1[x];         if (x == 1 || x == n - 1) {           for (long long i = 0; i < n; i++) {             if (v[i] == y) {               cout << i + 1;               return 0;             }           }         }         if (it->first == 0) {           long long y = v1[v2[0]];           for (long long i = 0; i < n; i++) {             if (v[i] == y) {               cout << i + 1;               return 0;             }           }         }       }       if (v2.size() == 2) {         auto it2 = it;         it2++;         if (v2[1] - v2[0] == 1 && it->first * 2 == it2->first) {           long long x = v2[0], y;           y = v1[x];           for (long long i = 0; i < n; i++) {             if (v[i] == y) {               cout << i + 1;               return 0;             }           }         }       }       it++;       v2 = it->second;       if (v2.size() == 1) {         long long x = v2[0], y;         if (x == 1) y = v1[0];         if (x == n - 1) y = v1[x];         if (x == 1 || x == n - 1) {           for (long long i = 0; i < n; i++) {             if (v[i] == y) {               cout << i + 1;               return 0;             }           }         }       }       if (v2.size() == 2) {         auto it2 = it;         it2++;         if (v2[1] - v2[0] == 1 && it->first * 2 == it2->first) {           long long x = v2[0], y;           y = v1[x];           for (long long i = 0; i < n; i++) {             if (v[i] == y) {               cout << i + 1;               return 0;             }           }         }       }     }     if (mp.size() == 3) {       auto it = mp.begin();       auto it1 = it;       it1++;       auto it2 = it1;       it2++;       if (it->first + it1->first == it2->first) {         if (abs(it->second[0] - it1->second[0]) == 1) {           long long x1 = it->second[0], x2 = it1->second[0];           long long x = min(x1, x2);           long long y = v1[x];           for (long long i = 0; i < n; i++) {             if (v[i] == y) {               cout << i + 1;               return 0;             }           }         }       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long x, d;     int cntDist = 0;     string inp;     cin >> n >> x;     getline(cin, inp);     for (int i = 0; i < n; i++) {       getline(cin, inp);       stringstream ss(inp.substr(2));       ss >> d;       if (inp[0] ==  + ) {         x += d;       } else {         if (x < d)           cntDist++;         else           x -= d;       }     }     cout << x <<     << cntDist;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, c, d[100005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> a >> b >> c;     a = a % b;     d[a]++;     int t;     for (int i = 1; i <= 100005; ++i) {       t = a * 10 / b;       if (t == c) {         cout << i;         exit(0);       }       a = ((a * 10) % b);       d[a]++;       if (d[a] == 2) {         cout <<  -1 ;         exit(0);       }     }   }


#include <bits/stdc++.h>   using namespace std;   void _fill_int(int* p, int val, int rep) {     int i;     for (i = 0; i < rep; i++) p[i] = val;   }   signed long long GETi() {     signed long long i;     scanf( %lld , &i);     return i;   }   int N;   map<vector<int>, double> M[64];   int st[64], num[64];   double dodo(int top, vector<int> V) {     int otop, i, x, y, z, ng = 0;     otop = top = st[top];     sort(V.begin(), V.end());     vector<int> VV = V;     if (M[top].find(V) != M[top].end()) return M[top][V];     if (num[top] == 3) V.push_back(top), top = 0, ng = 1;     double ma[4] = {0, 1e15, 1e15, 1e15};     int pre = -1;     for (i = 0; i < V.size() - ng; i++) {       if (pre == V[i]) continue;       pre = V[i];       x = V[i] % 4;       y = V[i] / 4 % 4;       z = V[i] / 16;       vector<int> V2 = V;       V2.erase(V2.begin() + i);       if (x && y && z) {         if (top % 4 == 0) ma[y] = min(ma[y], dodo(top + y * 1, V2));         if (top / 4 % 4 == 0) ma[y] = min(ma[y], dodo(top + y * 4, V2));         if (top / 16 == 0 && top % 4) ma[y] = min(ma[y], dodo(top + y * 16, V2));       }       if (z && y) {         if (x) {           V[i] = 4 + x;           if (top % 4 == 0) ma[z] = min(ma[z], dodo(top + z * 1, V));           if (top / 4 % 4 == 0) ma[z] = min(ma[z], dodo(top + z * 4, V));           if (top / 16 == 0 && top % 4) ma[z] = min(ma[z], dodo(top + z * 16, V));         } else {           if (top % 4 == 0) ma[z] = min(ma[z], dodo(top + z * 1, V2));           if (top / 4 % 4 == 0) ma[z] = min(ma[z], dodo(top + z * 4, V2));           if (top / 16 == 0 && top % 4)             ma[z] = min(ma[z], dodo(top + z * 16, V2));         }       }       if (y && x) {         if (z) {           V[i] = z + 4;           if (top % 4 == 0) ma[x] = min(ma[x], dodo(top + x * 1, V));           if (top / 4 % 4 == 0) ma[x] = min(ma[x], dodo(top + x * 4, V));           if (top / 16 == 0 && top % 4) ma[x] = min(ma[x], dodo(top + x * 16, V));         } else {           if (top % 4 == 0) ma[x] = min(ma[x], dodo(top + x * 1, V2));           if (top / 4 % 4 == 0) ma[x] = min(ma[x], dodo(top + x * 4, V2));           if (top / 16 == 0 && top % 4)             ma[x] = min(ma[x], dodo(top + x * 16, V2));         }       }       V[i] = pre;     }     int sum = 1;     if (ma[1] >= 1e15) ma[1] = 0, sum += 1;     if (ma[2] >= 1e15) ma[2] = 0, sum += 2;     if (ma[3] >= 1e15) ma[3] = 0, sum += 2;     if (sum == 6) return M[otop][VV] = 0;     M[otop][VV] = (1 + ma[1] / 6 + ma[2] / 3 + ma[3] / 3) / (1 - sum / 6.0);     return M[otop][VV] =                (1 + ma[1] / 6 + ma[2] / 3 + ma[3] / 3) / (1 - sum / 6.0);   }   void solve() {     int f, i, j, k, l, x, y, x2, y2, z;     cin >> N;     string S;     for (i = 0; i < 64; i++) {       x = i % 4;       y = i / 4 % 4;       z = i / 16;       st[i] = (z > x) ? (x * 16 + y * 4 + z) : i;       num[i] = (x > 0) + (y > 0) + (z > 0);     }     vector<int> V;     for (i = 0; i < N; i++) {       cin >> S;       x = 0;       for (y = 0; y < 3; y++) {         if (S[y] ==  R ) x += 1 << (y * 2);         if (S[y] ==  G ) x += 2 << (y * 2);         if (S[y] ==  B ) x += 3 << (y * 2);       }       if (i < N - 1) V.push_back(st[x]);     }     (void)printf( %.12lf n , dodo(st[x], V));   }   int main(int argc, char** argv) {     string s;     if (argc == 1) ios::sync_with_stdio(false);     for (int i = 1; i < argc; i++) s += argv[i], s +=   n ;     for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, r, k, tot;   int dx[] = {0, -1, 0, 1};   int dy[] = {-1, 0, 1, 0};   struct Cell {     long long c, x, y;     bool operator<(const Cell &oth) const { return c < oth.c; }   };   class Map {    private:     set<long long> vis;       public:     bool isVisit(long long x, long long y) {       return vis.count((x - 1) * m + y - 1);     }     void setVisit(long long x, long long y) { vis.insert((x - 1) * m + y - 1); }   } mp;   long long calc(long long x, long long y) {     return min(r, min(n - r + 1, min(x, n - x + 1))) *            min(r, min(m - r + 1, min(y, m - y + 1)));   }   priority_queue<Cell> pq;   void solve() {     scanf( %lld%lld%lld%lld , &n, &m, &r, &k);     pq.push({calc(r, r), r, r});     mp.setVisit(r, r);     while (!pq.empty() && k) {       Cell cur = pq.top();       pq.pop();       k--;       tot += cur.c;       for (int i = 0; i < 4; i++) {         int nx = cur.x + dx[i], ny = cur.y + dy[i];         if (0 < nx && nx <= n && 0 < ny && ny <= m && !mp.isVisit(nx, ny)) {           pq.push({calc(nx, ny), nx, ny});           mp.setVisit(nx, ny);         }       }     }     printf( %.10f n , 1.0 * tot / (1.0 * (n - r + 1) * (m - r + 1)));   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pw(int a, int b) {     if (b == 0) return 1;     if (b == 1) return a;     long long rep = pw(a, b / 2), M = 1e9 + 7;     if (b % 2 == 0) return (rep * rep) % M;     return (((rep * rep) % M) * a) % M;   }   int main() {     int n, k, m;     cin >> n >> m >> k;     if (k == 1 || k > n) {       cout << pw(m, n);       return 0;     }     if (k == n) {       cout << pw(m, (n + 1) / 2);       return 0;     }     long long answer = m;     if (k % 2 == 1) {       answer += m * (m - 1);     }     cout << answer << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, i, j;     string ss;     cin >> n >> m >> ss;     for (i = 0; i < m; i++) {       for (j = 0; j < n; j++) {         if (ss[j] ==  B  && ss[j + 1] ==  G ) {           ss[j] =  G ;           ss[j + 1] =  B ;           j++;         }       }     }     cout << ss << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 4010;   double num1[Maxn];   int num2[Maxn];   double dif[Maxn];   int main() {     int n = 0, N = 0;     int cnt = 0, cnt1 = 0, cnt2 = 0;     double sum = 0;     double ans = 100000000.0;     scanf( %d , &n);     N = 2 * n;     for (int i = 0; i < N; i++) {       scanf( %lf , &num1[i]);       num2[i] = num1[i];       dif[i] = (double)num1[i] - (double)num2[i];     }     for (int i = 0; i < N; i++) {       if (dif[i] != 0) {         cnt1++;         sum += dif[i];       } else {         cnt2++;       }     }     cnt = min(cnt1, n);     for (int i = 0; i <= cnt; i++) {       if (i + cnt2 >= n) {         ans = min(ans, fabs(sum - i * 1.0));       }     }     printf( %.3f , ans);     return 0;   }


/* Author:- Neel_Patel */   #include <bits/stdc++.h>   #define ff first   #define ss second   #define pb push_back   #define all(c) (c).begin(),(c).end()   #define vi vector<int>   #define pii pair<int,int>   #define pll pair<long long,long long>     #define vll vector<long long>   #define priq priority_queue   #define sp fixed<<setprecision   #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)   using namespace std;   typedef long long ll;   typedef unsigned long long int llu;   const long double pi=3.14159265358979323846;           const int inf=INT_MAX;   const ll inff=LLONG_MAX;   const long long int mod=1000000007;      ///////////////// debug   #define deb(...) print1(#__VA_ARGS__,__VA_ARGS__)   void print()   {cout<<endl;}   template <typename T, typename... Types>  void print(T var1, Types... var2) {cout << var1 <<  ,   ;print(var2...) ;}   template<typename ...Args> void print1(string vars, Args&&... values) {cout<<vars<<  =  ;print(values...);}   ////////////////////      // ostream operator overloading   template<typename T>   ostream& operator<< (ostream& out, const vector<T>& v) {       out <<  { ;size_t last = v.size() - 1;for(size_t i = 0; i < v.size(); ++i) { out << v[i];  if (i != last)  out <<  ,  ;}out <<  } ;return out;   }   template<typename T1,typename T2>    ostream & operator << (ostream &out,pair<T1,T2> x) { out<< ( <<x.ff<< , <<x.ss<< ) ;return out; }    ///////////////////////// functions    template<typename T>   T gcd(T a,T b,T& x,T& y)    // extended gcd:- x and y such that xa+yb=gcd(a,b);   {       if(b==0) { x=1;y=0;return a;}       T x1,y1;  T d=gcd(b,a%b,x1,y1);  x=y1;  y=x1-y1*(a/b);  return d;   }   template<typename T>   T pow(T a,T b,T M)   {       // if M is prime # then we can take //  b=b%(M-1);  // using fermat s littel theorem        // b=b%(M-1);       T res = 1;       while(b)       {           if(b&1) res=(res*a)%M;           a=(a*a)%M;           b>>=1;       }       return res;   }      template<typename T>   T modular_inverse(T n, T mod)    { return pow(n, mod-2, mod); }         //////////////////////////////////////////////////////////////////////////////////////   //////////////////////////////////////////////////////////////////////////////////////   //////////////////////////////////////////////////////////////////////////////////////   //////////////////////////////////////////////////////////////////////////////////////   // check value of t(#of test case -- in main function)   /////////////global variable///////////////////////   // #define int ll   #define ld long double   const ld eps=1e-10;   ld fun(ld c,ld m,ld p,ld v)   {       ld ans=0;        // deb(c,m,p,v);       if(c>eps)       {           ld diff=min(c,v);           ld nm=m,nc=c-diff,np=p;           if(m>eps)           {               nm+=diff/2.0;               np+=diff/2.0;           }           else           {               np+=diff;           }           //  deb(nc,nm,np,v);           ans+=c*(1.0+fun(nc,nm,np,v));       }       if(m>eps)       {           ld diff=min(m,v);           ld nm=m-diff,nc=c,np=p;           if(c>eps)           {               nc+=diff/2.0;               np+=diff/2.0;           }           else           {               np+=diff;           }           ans+=m*(1.0+fun(nc,nm,np,v));       }       return ans+p;      }   ll ask(ll n)   {       cout<<n<<endl;       cin>>n;       return n;   }   void solve()   {       ld c,m,p,v;       cin>>c>>m>>p>>v;       cout<<sp(20)<<fun(c,m,p,v)<<endl;             }   // priority_queue <int, vector<int>, greater<int>> g = gq; // for small value on top   ////////////////////////////////////////////////////////////////////////////////////   //////////////////////////////////////////////////////////////////////////////////////   //////////////////////////////////////////////////////////////////////////////////////   //////////////////////////////////////////////////////////////////////////////////////   signed main()  // signed int   {       // snippet = https://snippet-generator.app/       #ifndef ONLINE_JUDGE       cout<< hi!! ;      //  cout<< Code is running.... n ;       freopen( input.txt ,  r , stdin);        freopen( output.txt ,  w , stdout);       //  chrono::time_point<std::chrono::system_clock> start, end;       //  start = chrono::system_clock::now();        #endif // ONLINE_JUDGE       fast;        // cout<< hi n ;       int t=1;       cin>>t;       while(t--)         {                      solve();       }          /* #ifndef ONLINE_JUDGE           end = chrono::system_clock::now();           chrono::duration<double> elapsed_seconds = end - start;           cout<<elapsed_seconds.count()<<endl;       #endif // ONLINE_JUDGE  */      }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long a[200010];   const long long inf_int = 1e9;   pair<long long, long long> S[200010];   long long st[400010];   long long st_[400010];   long long lazy[400010];   void construct_st(int l = 1, int r = n, int i = 0) {     if (l == r) {       st[i] = a[l];       st_[i] = a[l];       return;     }     int mid = (l + r) >> 1;     construct_st(l, mid, i * 2 + 1);     construct_st(mid + 1, r, i * 2 + 2);     st[i] = max(st[i * 2 + 1], st[i * 2 + 2]);     st_[i] = min(st_[i * 2 + 1], st_[i * 2 + 2]);   }   void update(int s, int e, int value, int l = 1, int r = n, int i = 0) {     if (lazy[i]) {       st[i] += lazy[i];       st_[i] += lazy[i];       if (l != r) {         lazy[i * 2 + 1] += lazy[i];         lazy[i * 2 + 2] += lazy[i];       }       lazy[i] = 0;     }     if (e < l || s > r) return;     if (s <= l && r <= e) {       st[i] += value;       st_[i] += value;       if (l != r) {         lazy[i * 2 + 1] += value;         lazy[i * 2 + 2] += value;       }       return;     }     int mid = (l + r) >> 1;     update(s, e, value, l, mid, i * 2 + 1);     update(s, e, value, mid + 1, r, i * 2 + 2);     st[i] = max(st[i * 2 + 1], st[i * 2 + 2]);     st_[i] = min(st_[i * 2 + 1], st_[i * 2 + 2]);   }   int rmq(int s, int e, int l = 1, int r = n, int i = 0) {     if (lazy[i]) {       st[i] += lazy[i];       st_[i] += lazy[i];       if (l != r) {         lazy[i * 2 + 1] += lazy[i];         lazy[i * 2 + 2] += lazy[i];       }       lazy[i] = 0;     }     if (e < l || s > r) return inf_int;     if (s <= l && r <= e) return st_[i];     int mid = (l + r) >> 1;     long long L = rmq(s, e, l, mid, i * 2 + 1);     long long R = rmq(s, e, mid + 1, r, i * 2 + 2);     return min(L, R);   }   int rMq(int s, int e, int l = 1, int r = n, int i = 0) {     if (lazy[i]) {       st[i] += lazy[i];       st_[i] += lazy[i];       if (l != r) {         lazy[i * 2 + 1] += lazy[i];         lazy[i * 2 + 2] += lazy[i];       }       lazy[i] = 0;     }     if (e < l || s > r) return -inf_int;     if (s <= l && r <= e) return st[i];     int mid = (l + r) >> 1;     long long L = rMq(s, e, l, mid, i * 2 + 1);     long long R = rMq(s, e, mid + 1, r, i * 2 + 2);     return max(L, R);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= m; i++) {       cin >> S[i].first;       cin >> S[i].second;     }     construct_st();     long long maxi = rMq(1, n);     long long mini = rmq(1, n);     long long answer = maxi - mini;     long long answer_index = -1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int L = S[j].first;         int R = S[j].second;         if (i < L || i > R) {           update(L, R, -1);         }       }       maxi = rMq(1, n);       mini = rmq(1, n);       long long possible_answer = maxi - mini;       if (possible_answer > answer) {         answer = possible_answer;         answer_index = i;       }       for (int j = 1; j <= m; j++) {         int L = S[j].first;         int R = S[j].second;         if (i < L || i > R) {           update(L, R, 1);         }       }     }     cout << answer <<   n ;     if (answer_index != -1) {       vector<int> changes;       for (int j = 1; j <= m; j++) {         int L = S[j].first;         int R = S[j].second;         if (answer_index < L || answer_index > R) {           changes.push_back(j);         }       }       cout << changes.size() <<   n ;       for (auto t : changes) {         cout << t <<    ;       }       cout <<   n ;     } else {       cout << 0 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   double res, a, b;   int main() {     int n, k;     cin >> n >> k;     vector<double> x(n), y(n);     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i];       if (i) {         a = x[i] - x[i - 1];         b = y[i] - y[i - 1];         res += sqrt((a * a) + (b * b));       }     }     res /= 50;     cout << setprecision(10) << res * k << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int mass[n];     for (int i = 0; i < n; i++) cin >> mass[i];     list<int> lis;     int ans = 0;     for (int i = 0; i < m; i++) {       int temp;       cin >> temp;       temp--;       if (lis.empty())         lis.push_front(temp);       else {         int sum = 0;         list<int>::iterator i;         for (i = lis.begin(); i != lis.end(); i++) {           if (*i == temp)             break;           else             sum += mass[*i];         }         if (i != lis.end()) lis.erase(i);         lis.push_front(temp);         ans += sum;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int sst(string g) {     int f;     stringstream ss;     ss << g;     ss >> f;     return f;   }   int main() {     string s, s1, s2, s3;     cin >> s;     int max = -1, h = 0;     int t = s.size(), x, y, z;     if (t < 3) {       cout <<  -1  << endl;       return 0;     }     for (int i = 0; i < t - 1; i++) {       for (int j = i + 1; j < t - 1; j++) {         s1 = s.substr(0, i + 1);         s2 = s.substr(i + 1, j - i);         s3 = s.substr(j + 1, t - j - 1);         x = sst(s1);         y = sst(s2);         z = sst(s3);         if (s1.size() == 0 || s2.size() == 0 || s3.size() == 0) continue;         if (s1[0] ==  0  && s1.size() > 1) continue;         if (s2[0] ==  0  && s2.size() > 1) continue;         if (s3[0] ==  0  && s3.size() > 1) continue;         if (x <= 1000000 && y <= 1000000 && z <= 1000000 && x + y + z > max) {           max = x + y + z;           h = 1;         }       }     }     if (h = 1)       cout << max;     else       cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string binary(long long n) {     string result;     do result +=  0  + (n & 1);     while (n >>= 1);     reverse(result.begin(), result.end());     return result;   }   int countSubstr(const string& str, const string& sub) {     if (sub.length() == 0) return 0;     int count = 0;     for (size_t offset = str.find(sub); offset != string::npos;          offset = str.find(sub, offset + sub.length())) {       ++count;     }     return count;   }   vector<bool> primes;   vector<int> primesCnt;   void seive(int a, int b) {     primes = vector<bool>(b + 1, true);     primesCnt = vector<int>(b, true);     primes[0] = primes[1] = false;     for (int i = 2; i * i <= b; i++) {       for (int j = i; j * i <= b; j++) {         primes[i * j] = false;       }     }     primesCnt[0] = 0;     for (decltype(b + 1)(i) = (1); (i) < (b + 1); (i)++) {       primesCnt[i] = primesCnt[i - 1] + primes[i];     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int a, b, k, ans = -1;     cin >> a >> b >> k;     seive(a, b);     if (primesCnt[b] - primesCnt[a - 1] < k) {       cout << ans;       return 0;     }     int left = 0, right = b - a + 1, med;     while (right > left + 1) {       med = (right + left) / 2;       bool can = true;       int j = a + med - 1;       while (j <= b) {         if (primesCnt[j] - primesCnt[j - med] < k) {           can = false;           break;         }         j++;       }       if (can)         right = med;       else         left = med;     }     cout << right;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool compare(const pair<int, int>& i, const pair<int, int>& j) {     return i.first < j.first;   }   int ncr(int n, int k) {     if (n < k) return 0;     int C[n + 1][k + 1];     int i, j;     for (i = 0; i <= n; i++) {       for (j = 0; j <= min(i, k); j++) {         if (j == 0 || j == i)           C[i][j] = 1;         else           C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 1000000007;       }     }     return C[n][k];   }   long long modpowIter(long long a, long long b, long long c) {     long long ans = 1;     while (b != 0) {       if (b % 2 == 1) ans = (ans * a) % LLONG_MAX;       a = (a * a) % LLONG_MAX;       b /= 2;     }     return ans;   }   string intobin(int n) {     string s =   ;     if (n == 0) s =  0 ;     while (n) {       if (n % 2)         s =  1  + s;       else         s =  0  + s;       n /= 2;     }     return s;   }   int bintoin(string s) {     long long n = 0;     vector<long long> v;     v.push_back(1);     for (int i = 0; i < 400; i++) {       v.push_back((v[v.size() - 1] * 2) % 1000003);     }     for (int i = s.length() - 1; i > -1; i--) {       n = (n + (s[i] -  0 ) * v[s.length() - i - 1]) % 1000003;     }     return n;   }   long long nc2(long long n, long long k) {     if (n < k) return 0;     return (n * (n - 1)) / 2;   }   bool prime[1000001];   void SieveOfEratosthenes(int n) {     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }   }   int main() {     int n;     cin >> n;     int m;     cin >> m;     int arr[n];     double b[n];     for (int i = 0; i < n; i++) {       cin >> arr[i] >> b[i];     }     int dp[n];     dp[0] = 1;     for (int i = 1; i <= n - 1; i++) {       dp[i] = 1;       for (int j = 0; j < i; j++) {         if (arr[i] >= arr[j] && dp[i] < dp[j] + 1) dp[i] = dp[j] + 1;       }     }     cout << n - (*max_element(dp, dp + n));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000 + 20;   int a[maxn];   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     int ans = 0;     for (int i = n - 1; i >= 0; i -= k) {       ans += 2 * (a[i] - 1);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2000;   int n, k;   int a, b, c, d;   int num[MAXN], num_cnt = 0;   int rank[MAXN];   set<int> s;   vector<int> v;   int main() {     cin >> n >> k;     cin >> a >> b >> c >> d;     if (k <= n) {       cout << -1 << endl;     } else {       if (n >= 5) {         for (int i = 1; i <= n; ++i) {           if (i != a && i != b && i != c && i != d) {             s.insert(i);           }         }         v.push_back(a);         v.push_back(c);         while (!s.empty()) {           v.push_back(*s.begin());           s.erase(s.begin());         }         v.push_back(d);         v.push_back(b);         for (int i = 0; i < v.size(); ++i) {           cout << v[i] <<    ;         }         cout << endl;         cout << c <<     << a <<    ;         for (int i = (int)v.size() - 3; i >= 2; --i) {           cout << v[i] <<    ;         }         cout << b <<     << d << endl;       } else if (n == 4) {         cout << -1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int a[1234];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , a + i);     int mx = 0;     for (int i = 0; i < n; i++)       if (a[i] > mx) mx = a[i];     for (int u = 1; u <= mx; u++) {       for (int i = 0; i < n; i++) {         if (a[i] >= u) putchar( P );         if (i < n - 1)           putchar( R );         else {           for (int j = 0; j < n - 1; j++) putchar( L );         }       }     }     printf(  n );     return 0;   }