#include <bits/stdc++.h>   using namespace std;   using ll = long long;   bitset<10000> B;   int BIT[100010];   int n;   void update(int pos, int x) {     for (; pos < n; pos |= pos + 1) BIT[pos] += x;   }   int qu(int pos) {     int ret = 0;     for (; pos; pos &= pos - 1) ret += BIT[pos - 1];     return ret;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     for (int len = 1; len <= 4; ++len) {       for (int mask = 0; mask < 1 << len; ++mask) {         int num = 0;         for (int bit = 0; bit < len; ++bit)           if (mask & (1 << bit))             num = num * 10 + 4;           else             num = num * 10 + 7;         B[num] = 1;       }     }     int m;     cin >> n >> m;     int a[n];     for (int i = 0; i < n; ++i) {       cin >> a[i];       if (B[a[i]]) update(i, 1);     }     while (m--) {       string type;       int l, r;       cin >> type >> l >> r;       if (type ==  count ) {         cout << qu(r) - qu(l - 1) <<   n ;       } else {         int d;         cin >> d;         for (int i = l - 1; i <= r - 1; ++i) {           if (B[a[i]]) update(i, -1);           a[i] += d;           if (B[a[i]]) update(i, 1);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int maxn = 1e5 + 5;   long long dp[205][15005];   long long p2[205], p5[205];   long long a[205];   int n, k;   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       while (a[i] % 2 == 0) a[i] /= 2, p2[i]++;       while (a[i] % 5 == 0) a[i] /= 5, p5[i]++;     }     memset(dp, -0x3f3f3f3f, sizeof(dp));     dp[0][0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = k; j >= 1; j--) {         for (int k = 12005; k >= p2[i]; k--) {           dp[j][k] = max(dp[j][k], dp[j - 1][k - p2[i]] + p5[i]);         }       }     }     long long mx = 0;     for (long long i = 0; i <= 12005; i++) {       mx = max(mx, min(i, dp[k][i]));     }     printf( %lld n , mx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> types;   int count(const string &data) {     int amps = 0, stars = 0, i, j;     for (i = 0; i < (int)data.size() && data[i] ==  & ; i++, amps++)       ;     for (j = data.size() - 1; j >= 0 && data[j] ==  * ; j--, stars++)       ;     string t = data.substr(i, j - i + 1);     if (!types.count(t) || types[t] == -1) return -1;     int answer = types[t];     answer += stars - amps, answer = max(-1, answer);     return answer;   }   int n;   string command, a, b;   int main() {     cin >> n;     types[ void ] = 0, types[ errtype ] = -1;     for (int i = 0; i < n; i++) {       cin >> command >> a;       if (command ==  typedef )         cin >> b, types[b] = count(a);       else {         int p = count(a);         if (p == -1)           cout <<  errtype  << endl;         else {           cout <<  void ;           for (int i = 0; i < p; i++) cout <<  * ;           cout << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Case, n, sz, ans, A[300000 + 5], Min[300000 + 5], Max[300000 + 5],       Id[300000 + 5];   int main() {     for (scanf( %d , &Case); Case; Case--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) Id[i] = 0, Min[i] = n + 1, Max[i] = 0;       for (int i = 1; i <= n; i++) {         scanf( %d , A + i);         Id[A[i]] = 1;       }       for (int i = 1; i <= n; i++) Id[i] += Id[i - 1];       sz = Id[n];       for (int i = 1; i <= n; i++) {         A[i] = Id[A[i]];         Min[A[i]] = min(Min[A[i]], i);         Max[A[i]] = max(Max[A[i]], i);       }       ans = sz;       for (int l = 1, r = 0, mx; l <= sz; l++) {         if (r < l) r = l, mx = Max[l];         for (; r < sz && Min[r + 1] > mx; r++) mx = Max[r + 1];         ans = min(ans, sz - (r - l + 1));       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k;   long long int m, lim = 2e18;   long long int dp[70][70][2];   long long int solve(string s, int pos, int k, bool f) {     if (k < 0) return 0;     if (pos == (int)s.size()) {       return (k == 0);     }     if (dp[pos][k][f] != -1) return dp[pos][k][f];     long long int ret = 0;     for (char c =  0 ; c <=  1 ; ++c) {       if (!f and s[pos] < c) continue;       ret += solve(s, pos + 1, k - (c ==  1 ), f | (s[pos] > c));     }     return dp[pos][k][f] = ret;   }   long long int get(string s) {     if (s ==  1 ) {       if (k == 1)         return 1;       else         return 0;     }     memset(dp, -1, sizeof dp);     return solve(s, 0, k, 0);   }   string binary(long long int num) {     string ret =   ;     while (num) {       if (num & 1)         ret +=  1 ;       else         ret +=  0 ;       num >>= 1LL;     }     reverse((ret).begin(), (ret).end());     return ret;   }   long long int search(long long int l, long long int r) {     long long int mid = (l + r) / 2LL;     long long int tot = get(binary(2 * mid)) - get(binary(mid));     if (tot == m)       return mid;     else if (tot < m)       return search(mid + 1, r);     return search(l, mid - 1);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     while (cin >> m >> k) {       long long int ans = search(1LL, lim);       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   int arr[1005];   int n;   using namespace std;   int main() {     int n;     scanf( %d , &n);     int sum = 0;     for (int i = 0; i < 505; i++) {       sum += i;       arr[i + 1] = sum;     }     if (n == 0) {       printf( a n );       return 0;     }     for (int i = 0; i < 26; i++) {       if (n == 0) {         return 0;       }       int howmany = lower_bound(arr, arr + 500, n) - arr;       if (arr[howmany] == n) {         for (int j = 0; j < howmany; j++) {           printf( %c ,  a  + i);         }         printf(  n );         return 0;       }       if (arr[howmany] > n) {         n -= arr[howmany - 1];         for (int j = 0; j < howmany - 1; j++) {           printf( %c ,  a  + i);         }         continue;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     vector<long long> p(n);     for (auto& i : p) cin >> i;     vector<long long> dp(n, 1);     vector<int> exist(n, false);     for (long long i = 0; i < (n); ++i) {       exist[p[i] - 1] = true;       if (p[i] != 1)         if (exist[p[i] - 2]) {           dp[p[i] - 1] = 1 + dp[p[i] - 2];         }     }     long long max_c = 0;     for (long long i = 0; i < (n); ++i) max_c = max(max_c, dp[i]);     cout << n - max_c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5 * 1e5 + 1;   int main() {     long long q;     cin >> q;     vector<long long> v(N);     for (int i = 0; i <= N; i++) {       v[i] = i;     }     deque<long long> dq;     vector<pair<long long, long long>> pq(q);     long long idx = 0;     while (q--) {       long long a;       cin >> a;       if (a == 1) {         long long b;         cin >> b;         pq[idx].first = b;         pq[idx].second = 0;         idx++;       } else {         long long b, c;         cin >> b >> c;         pq[idx].first = b;         pq[idx].second = c;         idx++;       }     }     for (int i = idx - 1; i >= 0; i--) {       if (pq[i].first != 0 && pq[i].second != 0) {         v[pq[i].first] = v[pq[i].second];       } else {         dq.push_front(v[pq[i].first]);       }     }     for (auto it : dq) {       cout << it <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int cap = 1e3;   int n, m;   char grid[cap][cap];   int vis[4][cap][cap] = {};   vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   bool dfs(int i, int j, int d, int t) {     if (i < 0 || i == n || j < 0 || j == m || grid[i][j] ==  *  ||         (vis[d][i][j] != -1 && t >= vis[d][i][j]))       return false;     if (grid[i][j] ==  T ) return true;     vis[d][i][j] = t;     int x, y;     x = i + dirs[d].first;     y = j + dirs[d].second;     if (dfs(x, y, d, t)) return true;     if (t < 2)       for (int k = 0; k < 4; ++k)         if (k != d) {           x = i + dirs[k].first;           y = j + dirs[k].second;           if (dfs(x, y, k, t + 1)) return true;         }     return false;   }   int main() {     memset(vis, -1, sizeof vis);     scanf( %d %d , &n, &m);     int i_s = -1, j_s = -1;     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) {         scanf(  %c , &grid[i][j]);         if (grid[i][j] ==  S ) {           i_s = i;           j_s = j;         }       }     for (int d = 0; d < 4; ++d)       if (dfs(i_s, j_s, d, 0)) {         printf( YES );         return 0;       }     printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, pos, ans = 10000000000000000;   long long a[3000 + 5], b[3000 + 5], f[3000 + 5][3000 + 5], Min[3000 + 5];   long long read() {     char c;     long long x = 0, f = 1;     c = getchar();     while (c >  9  || c <  0 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + c -  0 ;       c = getchar();     }     return x * f;   }   signed main() {     n = read();     for (register long long i = 1; i <= n; ++i) a[i] = read() - i, b[i] = a[i];     sort(b + 1, b + n + 1);     pos = unique(b + 1, b + n + 1) - b - 1;     for (register long long i = 1; i <= n; ++i) {       for (register long long j = 1; j <= pos; ++j)         f[i][j] = Min[j] + abs(a[i] - b[j]);       Min[0] = 10000000000000000;       for (register long long j = 1; j <= pos; ++j)         Min[j] = min(Min[j - 1], f[i][j]);     }     for (register long long i = 1; i <= pos; ++i) ans = min(ans, f[n][i]);     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   #define int long long int   #define us unsigned int   #define null 0   #define m_p make_pair   #define f_i first   #define s_e second   #define p_b push_back      using namespace std;         void IOS()   {       ios_base::sync_with_stdio(false);       cin.tie(NULL);   }                  int32_t main()   {       IOS();   int test;   cin>>test;   while(test--)   {       int n;       cin>>n;       string s;       cin>>s;       map<char,int> mp;       bool flag=true;          for(int i=0;i<n;i++)       {           if(mp.find(s[i])==mp.end())           {               mp[s[i]]=i;           }           else if(i-mp[s[i]]>1)           {               flag=false;               break;           }           mp[s[i]]=i;       }          if(flag)       {           cout<< YES <<endl;       }          else       {           cout<< NO <<endl;       }   }      }


#include <bits/stdc++.h>   using namespace std;   const int Y = 10;   int n;   string fcol =   ;   bool place(int y, int x) {     cout << x <<     << y << endl;     string ret;     cin >> ret;     return (ret == fcol);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     cout <<  0   << Y << endl;     cin >> fcol;     bool lastbl = true;     int cx = 0;     int jmp = (1 << 28);     for (int i = 0; i < n - 1; ++i) {       if (lastbl) {         lastbl = place(Y, cx + jmp);         cx += jmp;       } else {         lastbl = place(Y, cx - jmp);         cx -= jmp;       }       jmp /= 2;     }     if (lastbl) {       cout << cx <<     << Y + 1 <<     << cx + 1 <<     << Y - 1 << endl;     } else {       cout << cx <<     << Y + 1 <<     << cx - 1 <<     << Y - 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct FlowEdge {     int to, cap, rev;     FlowEdge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {}   };   class FordFulkerson {    public:     FordFulkerson() {}     FordFulkerson(int n) : G(n), used(n) {}     void add(int from, int to, int cap) {       G[from].emplace_back(to, cap, (int)G[to].size());       G[to].emplace_back(from, 0, (int)G[from].size() - 1);     }     int get(int s, int t) {       int f = 1, res = 0;       while (f) {         fill(begin(used), end(used), false);         f = dfs(s, t, INT_MAX);         res += f;       }       return res;     }       private:     vector<vector<FlowEdge>> G;     vector<bool> used;     int dfs(int v, int t, int f) {       if (v == t) return f;       used[v] = 1;       for (auto &e : G[v]) {         if (!used[e.to] && e.cap > 0) {           int d = dfs(e.to, t, min(e.cap, f));           if (d > 0) {             e.cap -= d;             G[e.to][e.rev].cap += d;             return d;           }         }       }       return 0;     }   };   const string T =  fair n , F =  unfair n ;   const int MAXV = 20099, SRC = 0, QUERY = 10001, SINK = 20010, MOD5 = 20001;   int N, B, Q, upTo[10099], quantity[10099];   pair<int, int> query[10099];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N >> B >> Q;     for (int i = 0; i < (int)Q; ++i) cin >> upTo[i] >> quantity[i];     for (int i = 0; i < (int)Q; ++i) query[i] = make_pair(upTo[i], quantity[i]);     query[Q] = make_pair(0, 0);     query[Q] = make_pair(B, N);     Q += 2;     sort(query, query + Q);     FordFulkerson flowG(MAXV);     for (int i = 0; i < (int)5; ++i) flowG.add(SRC, MOD5 + i, N / 5);     for (int num = 1; num < (int)B + 1; ++num) {       int mod5 = num % 5;       flowG.add(MOD5 + mod5, num, 1);     }     for (int i = 0; i < (int)Q - 1; ++i) {       int lef = query[i].first, rig = query[i + 1].first;       int cnt = query[i + 1].second - query[i].second;       if (cnt < 0) {         cout << F;         return 0;       }       for (int num = lef + 1; num <= rig; ++num) {         flowG.add(num, QUERY + i, 1);       }       flowG.add(QUERY + i, SINK, cnt);     }     int maxFlow = flowG.get(SRC, SINK);     cout << (maxFlow == N ? T : F);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, x = 0, k = 0;     cin >> t;     while (t--) {       int m, n, x, y = 0;       cin >> m;       cin >> n;       x = m * n;       int a[x];       for (int i = 0; i < x; i++) {         cin >> a[i];         if (a[i] < 0) {           a[i] = 0 - a[i];           k++;         }         y = y + a[i];       }       sort(a, a + x);       if (k % 2 == 0)         cout << y << endl;       else         cout << y - 2 * a[0] << endl;       k = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1000000007;   void runCase(int caseNum) {     string s;     cin >> s;     int res = true;     if (s.size()) {       stack<char> st;       st.push(s[0]);       for (int i = 1; i < s.size(); ++i) {         if (!st.empty() && st.top() == s[i])           st.pop();         else           st.push(s[i]);       }       res = st.empty();     }     cout << (res ?  Yes  :  No ) << endl;   }   int main(int argc, char* argv[]) {     runCase(0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAGIC = 400;   const long long N = 1e5 + 2;   const long long inf = 1e14 + 2;   long long lzval[MAGIC], lzmul[MAGIC], ar[N], add[N];   bool used[N];   vector<long long> idx[MAGIC];   struct hull {     long long ptr;     struct line {       long long a, b, idx;       long long y(long long x) { return a * x + b; }     };     vector<line> hull;     bool bad(line x, line y, line z) {       assert(y.a != x.a);       if (z.a == y.a) {         return z.b > y.b;       }       return (1.0 * (x.b - z.b)) / (z.a - x.a) <              (1.0 * (y.b - z.b)) / (z.a - y.a);     }     void insert(line a) {       while (hull.size() > 1) {         if (bad(hull[hull.size() - 2], hull[hull.size() - 1], a)) {           hull.pop_back();         } else {           break;         }       }       if (hull.size() && hull.back().a == a.a) {         if (hull.back().b > a.b) {           return;         } else {           hull.pop_back();         }       }       hull.push_back(a);     }     void build(vector<long long> &wow) {       hull.clear();       ptr = 0;       for (long long i = 0; i < wow.size(); i++) {         long long idx = wow[i];         if (used[idx]) {           continue;         }         line tmp;         tmp.a = ar[idx];         tmp.b = add[idx];         tmp.idx = idx;         insert(tmp);       }     }     pair<long long, long long> get(long long idx) {       while (ptr + 1 < hull.size() &&              hull[ptr].y(lzmul[idx]) < hull[ptr + 1].y(lzmul[idx])) {         ptr++;       }       return pair<long long, long long>(hull[ptr].y(lzmul[idx]) + lzval[idx],                                         -hull[ptr].idx);     }   } lis[MAGIC];   long long grp(long long x) { return (x + MAGIC - 1) / MAGIC; }   bool cmp(long long x, long long y) {     if (ar[x] != ar[y]) {       return ar[x] > ar[y];     }     return add[x] > add[y];   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n, i, j, k, l, mx, ans = 0, mxans = 0, z, q;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> ar[i];       add[i] = ar[i];       idx[grp(i)].push_back(i);     }     mx = grp(n);     for (i = 1; i <= mx; i++) {       sort(idx[i].begin(), idx[i].end(), cmp);       lis[i].build(idx[i]);     }     for (i = 1; i <= n; i++) {       pair<long long, long long> res = {-inf, -1};       for (j = 1; j <= mx; j++) {         if (lis[j].hull.size()) {           res = max(res, lis[j].get(j));           pair<long long, long long> kek = lis[j].get(j);         }       }       ans += res.first;       mxans = max(mxans, ans);       q = j = -res.second;       used[j] = true;       j = grp(j);       for (k = j + 1; k <= mx; k++) {         lzmul[k]++;       }       for (k = 1; k < j; k++) {         lzval[k] += ar[q];       }       k = (j - 1) * MAGIC + 1, l = min(j * MAGIC, n);       for (z = k; z <= l; z++) {         add[z] += lzval[j] + lzmul[j] * ar[z];         if (z >= q) {           add[z] += ar[z];         } else {           add[z] += ar[q];         }       }       lzval[j] = lzmul[j] = 0;       lis[j].build(idx[j]);     }     cout << mxans;   }


#include <bits/stdc++.h>   using namespace std;   long long m[1000001], cd, kq[100000];   map<long long, long long> ruler;   int main() {     cin >> cd;     for (long long i = 1; i <= cd; i++) {       cin >> m[i];       ruler[m[i]] = 1;     }     for (long long i = 1; i <= cd; i++) {       long long e = 1;       kq[e] = m[i];       long long t = m[i];       while (e <= cd) {         if (ruler[t * 2] == 1) {           kq[e + 1] = t * 2;           t = t * 2;         } else if (t % 3 == 0 && ruler[t / 3] == 1) {           kq[e + 1] = t / 3;           t = t / 3;         } else           break;         e++;         if (e == cd) {           for (long long k = 1; k <= cd; k++) cout << kq[k] <<    ;           exit(0);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> A;   string s;   int main() {     A[ void ] = 100;     A[ errtype ] = 0;     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> s;       if (s[4] ==  d ) {         string a, b;         cin >> a >> b;         int l = a.length();         int p = 0, q = 0;         for (int j = 0; j < l; ++j)           if (a[j] ==  & )             ++p;           else if (a[j] ==  * )             ++q;         a = a.substr(p, l - p - q);         q -= p;         if (A[a] + q < 100)           A[b] = 0;         else           A[b] = A[a] + q;       } else {         string a;         cin >> a;         int p = 0, q = 0;         int l = a.length();         for (int j = 0; j < l; ++j)           if (a[j] ==  & )             ++p;           else if (a[j] ==  * )             ++q;         a = a.substr(p, l - p - q);         q -= p;         if (A[a] + q < 100)           puts( errtype );         else {           printf( void );           int t = A[a] + q;           for (int j = 100; j < t; ++j) printf( * );           puts(  );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using std::map;   const int MAXN = 1000 + 3;   int table[MAXN][MAXN];   int n, m, k;   map<int, int> r, c;   int main(int argc, char *argv[]) {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; ++i) {       r[i] = i;       for (int j = 1; j <= m; ++j) {         if (i == 1) c[j] = j;         scanf( %d , &table[i][j]);       }     }     char cmd;     int tmp, x, y;     while (k--) {       scanf( %s%d%d , &cmd, &x, &y);       if (cmd ==  r ) {         tmp = r[x];         r[x] = r[y];         r[y] = tmp;       } else if (cmd ==  c ) {         tmp = c[x];         c[x] = c[y];         c[y] = tmp;       } else         printf( %d n , table[r[x]][c[y]]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MOD = (int)998244353;   const int MAXN = (int)1e6 + 5;   template <typename T>   class BIT {    public:     BIT(T* _a, int _n) {       a = _a;       n = _n;       bit = new T[n + 1];       for (int i = 1; i <= n; ++i) bit[i] = 0;       for (int i = 1; i <= n; ++i) upd_(i, a[i]);     }     void upd(int i, T val) {       T diff = val - a[i];       a[i] = val;       while (i <= n) {         bit[i] = bit[i] + diff;         i += i & (-i);       }     }     T get(int i) {       T ret = 0;       while (i > 0) {         ret = ret + bit[i];         i -= i & (-i);       }       return ret;     }       private:     void upd_(int i, T val) {       while (i <= n) {         bit[i] = bit[i] + val;         i += i & (-i);       }     }     T *bit, *a;     int n;   };   ll n, k, m, a1[MAXN], a2[MAXN], a3[MAXN];   struct Segment {     Segment(int _l, int _r, int _c, int _p) : l(_l), r(_r), c(_c), p(_p) {       end = 0;     };     int l, r, c, p;     bool end;     int pos;     int get_coord() {       if (end) return r;       return l;     }   };   bool cmp1(Segment& lhs, Segment& rhs) { return lhs.p < rhs.p; }   bool cmp2(Segment& lhs, Segment& rhs) {     int lhsx = (lhs.end == 1) ? lhs.r : lhs.l;     int rhsx = (rhs.end == 1) ? rhs.r : rhs.l;     if (lhsx == rhsx) return lhs.end < rhs.end;     return lhsx < rhsx;   }   vector<Segment> segs;   bool can(BIT<ll>& bit1, BIT<ll>& bit2, int pos) { return (bit1.get(pos) >= k); }   int main() {     cin >> n >> k >> m;     for (int i = 0; i < m; ++i) {       int l, r, c, p;       scanf( %d%d%d%d , &l, &r, &c, &p);       Segment seg(l, r, c, p);       segs.push_back(seg);     }     sort(segs.begin(), segs.end(), cmp1);     int sz = segs.size();     for (int i = 1; i <= sz; ++i) {       segs[i - 1].pos = i;       a3[i] = segs[i - 1].p;       Segment seg = segs[i - 1];       seg.end = 1;       segs.push_back(seg);     }     sort(segs.begin(), segs.end(), cmp2);     ll sol = 0;     BIT<ll> bit1(a1, m), bit2(a2, m);     int segpos = 0;     for (int i = 1; i <= n; ++i) {       while (segpos < 2 * m &&              (segs[segpos].get_coord() < i ||               (segs[segpos].get_coord() == i && segs[segpos].end == 0))) {         int pos = segs[segpos].pos;         if (segs[segpos].end == 0) {           bit1.upd(pos, segs[segpos].c);           bit2.upd(pos, 1LL * segs[segpos].c * segs[segpos].p);         } else {           bit1.upd(pos, 0);           bit2.upd(pos, 0);         }         segpos++;       }       ll solc = m;       int l = 1, r = m;       while (l <= r) {         int mid = (l + r) / 2;         if (can(bit1, bit2, mid)) {           solc = min(solc, (ll)mid);           r = mid - 1;         } else           l = mid + 1;       }       if (bit1.get(m) < k)         sol += bit2.get(m);       else {         ll diff = bit1.get(solc) - k;         sol += bit2.get(solc) - diff * a3[solc];       }     }     cout << sol;     return 0;   }


#include <bits/stdc++.h>   struct node {     int mx, se, cmx;     int cnt;     long long sum;     node() : mx(0), se(-1), cmx(0), cnt(0), sum(0) {}     node(int v) : mx(v), se(-1), cmx(1), cnt(1), sum(v) {}   };   node operator+(const node &a, const node &b) {     node res;     if (a.mx == b.mx) {       res.mx = a.mx;       res.se = std::max(a.se, b.se);       res.cmx = a.cmx + b.cmx;     } else if (a.mx > b.mx) {       res.mx = a.mx;       res.se = std::max(a.se, b.mx);       res.cmx = a.cmx;     } else {       res.mx = b.mx;       res.se = std::max(a.mx, b.se);       res.cmx = b.cmx;     }     res.cnt = a.cnt + b.cnt;     res.sum = a.sum + b.sum;     return res;   }   int enlarge(int n) {     int res = 1;     while (res < n) {       res <<= 1;     }     return res;   }   class segment_tree {     int n;     std::vector<node> a;     std::vector<int> tag, mxtag;     void node_add(int u, int v) {       a[u].mx += v;       a[u].se += v;       a[u].sum += 1ll * v * a[u].cnt;       tag[u] += v;     }     void node_add_mx(int u, int v) {       a[u].mx += v;       a[u].sum += 1ll * v * a[u].cmx;       mxtag[u] += v;     }     void down(int u) {       if (tag[u]) {         node_add(u << 1, tag[u]);         node_add(u << 1 | 1, tag[u]);         tag[u] = 0;       }       if (mxtag[u]) {         int lmx = a[u << 1].mx, rmx = a[u << 1 | 1].mx;         if (lmx >= rmx) {           node_add_mx(u << 1, mxtag[u]);         }         if (rmx >= lmx) {           node_add_mx(u << 1 | 1, mxtag[u]);         }         mxtag[u] = 0;       }     }     void modify(int u, int l, int r, int x, int v) {       if (l + 1 == r) {         a[u] = node(v);         return;       }       int mid = (l + r + 1) >> 1;       down(u);       if (x < mid) {         modify(u << 1, l, mid, x, v);       } else {         modify(u << 1 | 1, mid, r, x, v);       }       a[u] = a[u << 1] + a[u << 1 | 1];     }     void add(int u, int l, int r, int L, int R, int v) {       if (L <= l && r <= R) {         node_add(u, v);         return;       }       int mid = (l + r + 1) >> 1;       down(u);       if (L < mid) {         add(u << 1, l, mid, L, R, v);       }       if (mid < R) {         add(u << 1 | 1, mid, r, L, R, v);       }       a[u] = a[u << 1] + a[u << 1 | 1];     }     void chmin(int u, int l, int r, int L, int R, int v) {       if (a[u].mx <= v) {         return;       }       if (L <= l && r <= R && a[u].se < v) {         node_add_mx(u, v - a[u].mx);         return;       }       int mid = (l + r + 1) >> 1;       down(u);       if (L < mid) {         chmin(u << 1, l, mid, L, R, v);       }       if (mid < R) {         chmin(u << 1 | 1, mid, r, L, R, v);       }       a[u] = a[u << 1] + a[u << 1 | 1];     }     int get_cnt(int u, int l, int r, int L, int R) {       if (L <= l && r <= R) {         return a[u].cnt;       }       int mid = (l + r + 1) >> 1;       down(u);       if (R <= mid) {         return get_cnt(u << 1, l, mid, L, R);       } else if (L >= mid) {         return get_cnt(u << 1 | 1, mid, r, L, R);       } else {         return get_cnt(u << 1, l, mid, L, R) + get_cnt(u << 1 | 1, mid, r, L, R);       }     }       public:     segment_tree(int _n)         : n(_n),           a(enlarge(n) << 1),           tag(enlarge(n) << 1),           mxtag(enlarge(n) << 1) {}     void modify(int x, int v) { modify(1, 0, n, x, v); }     void add(int l, int r, int v) {       if (l >= r) {         return;       }       add(1, 0, n, l, r, v);     }     void chmin(int l, int r, int v) {       if (l >= r) {         return;       }       chmin(1, 0, n, l, r, v);     }     int get_cnt(int l, int r) {       if (l >= r) {         return 0;       }       return get_cnt(1, 0, n, l, r);     }     long long get_sum() { return a[1].sum; }   };   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(0);     int n;     std::cin >> n;     std::vector<int> p(n);     for (int i = 0; i < n; ++i) {       int v;       std::cin >> v;       --v;       p[v] = i;     }     std::vector<long long> ans(n);     for (int _ = 0; _ < 2; ++_) {       segment_tree T(n);       for (int i = 0; i < n; ++i) {         T.modify(p[i], i + 1);         T.chmin(0, p[i], T.get_cnt(0, p[i]));         T.add(p[i] + 1, n, 1);         ans[i] += T.get_sum() - 1ll * i * (i + 1) / 2;       }       for (int i = 0; i < n; ++i) {         p[i] = n - 1 - p[i];       }     }     for (int i = 0; i < n; ++i) {       std::cout << ans[i] - i - 1 <<   n ;     }   }


#include <bits/stdc++.h>   const int INF = 1e9 + 7;   struct Node {     int min, max;     int low, high;     int lcnt, rcnt, cnt;   } st[2000000];   int as[500005];   int query(int w, int L, int R, int& low, int& high) {     if (R - L == 1) {       return as[L] >= low && as[L] <= high;     }     int M = (L + R) / 2;     if (low > st[w].max) return 0;     if (high < st[w].min) return 0;     if (low <= st[w].min && high >= st[w].max) return st[w].cnt;     if (low > st[w].high) {       return query(w * 2 + 1, L, M, low, high);     } else if (high < st[w].low) {       return query(w * 2 + 2, M, R, low, high);     } else {       high = std::max(high, st[w].high);       low = std::min(low, st[w].low);       int res = 0;       int ign1, ign2;       if (high > st[w].high)         res += query(w * 2 + 1, L, M, ign1 = st[w].high + 1, high);       if (low < st[w].low)         res += query(w * 2 + 2, M, R, low, ign2 = st[w].low - 1);       if (low <= st[w].low && high >= st[w].high && st[w].low <= st[w].high)         res++;       return res;     }   }   void pull(int w, int L, int R) {     int ign1, ign2;     assert(R - L > 1);     int M = (L + R) / 2;     st[w].min = std::min(st[w * 2 + 1].min, st[w * 2 + 2].min);     st[w].max = std::max(st[w * 2 + 1].max, st[w * 2 + 2].max);     st[w].high = st[w * 2 + 2].max;     st[w].low = st[w * 2 + 1].min;     query(w * 2 + 1, L, M, st[w].low, st[w].high);     query(w * 2 + 2, M, R, st[w].low, st[w].high);     if (st[w].low <= st[w].high) {       st[w].lcnt = query(w * 2 + 1, L, M, ign1 = st[w].high + 1, ign2 = INF);       st[w].rcnt = query(w * 2 + 2, M, R, ign1 = -INF, ign2 = st[w].low - 1);       st[w].cnt = st[w].lcnt + st[w].rcnt + 1;     } else {       st[w].lcnt = st[w * 2 + 1].cnt;       st[w].rcnt = st[w * 2 + 2].cnt;       st[w].cnt = st[w].lcnt + st[w].rcnt;     }   }   void build(int w, int L, int R, int a, int b) {     if (a >= R || b <= L) return;     if (R - L == 1) {       st[w].min = st[w].max = as[L];       st[w].cnt = 1;     } else {       int M = (L + R) / 2;       build(w * 2 + 1, L, M, a, b);       build(w * 2 + 2, M, R, a, b);       pull(w, L, R);     }   }   void dump(int w, int L, int R) {     int M = (L + R) / 2;     if (R - L > 1) {       printf( [%d,%d): min=%d max=%d low=%d high=%d lcnt=%d rcnt=%d cnt=%d n , L,              R, st[w].min, st[w].max, st[w].low, st[w].high, st[w].lcnt,              st[w].rcnt, st[w].cnt);       dump(w * 2 + 1, L, M);       dump(w * 2 + 2, M, R);     }   }   int main() {     int N, Q;     scanf( %d %d , &N, &Q);     for (int i = 0; i < N; i++) {       scanf( %d , &as[i]);     }     build(0, 0, N, 0, N);     for (int i = 0; i < Q; i++) {       int I, X;       scanf( %d %d , &I, &X);       I--;       as[I] = X;       build(0, 0, N, I, I + 1);       printf( %d n , st[0].cnt);     }   }


#include <bits/stdc++.h>   using namespace std;   const string nameFiles =   ;   void error(vector<string>::iterator) {}   template <typename T, typename... Args>   void error(vector<string>::iterator cur_var, T a, Args... args) {     cerr << cur_var->substr((*cur_var)[0] ==    ) <<   =   << a << endl;     error(++cur_var, args...);   }   vector<string> split(string const& str, char c) {     vector<string> res;     stringstream ss(str);     string x;     while (getline(ss, x, c)) res.emplace_back(x);     return move(res);   }   int main() {     if (!nameFiles.empty()) {       freopen((nameFiles +  .in ).c_str(),  r , stdin);       freopen((nameFiles +  .out ).c_str(),  w , stdout);     }     int n, m;     cin >> n >> m;     vector<int> a(n);     unordered_set<int> mod;     for (int i = 0; i < n; ++i) {       cin >> a[i];       a[i] %= m;       if (a[i] == 0) {         cout <<  YES  << endl;         return 0;       }     }     for (int i = 0; i < n; ++i) {       vector<int> ttt;       for (int x : mod) {         int temp = (x + a[i]) % m;         if (temp == 0) {           cout <<  YES  << endl;           return 0;         }         ttt.push_back(temp);       }       mod.insert(ttt.begin(), ttt.end());       mod.insert(a[i]);     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x7f7f7f7f;   const int MOD = 1e9 + 7;   const double eps = 1e-6;   int main() {     int n, r, v;     scanf( %d%d%d , &n, &r, &v);     double s = acos(-1.0) * r * 2;     while (n--) {       int a, b;       scanf( %d%d , &a, &b);       double d = b - a;       int c = (d / s);       double len = d - c * s;       double L = 0, R = len;       double mid = (L + R) / 2;       while (R - L >= eps) {         if (mid + 2 * r * sin(mid / 2 / r) >= len)           R = mid;         else           L = mid;         mid = (L + R) / 2;       }       printf( %.12lf n , (c * s + mid) / v);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return !b ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a * (b / gcd(a, b));   }   template <typename T>   T sqr(T a) {     return a * a;   }   template <typename T>   T cube(T a) {     return a * a * a;   }   template <typename T>   inline void smin(T &a, T b) {     a = a < b ? a : b;   }   template <typename T>   inline void smax(T &a, T b) {     a = a > b ? a : b;   }   int in() {     int n;     scanf( %d , &n);     return n;   }   long long Lin() {     long long n;     scanf( %lld , &n);     return n;   }   double Din() {     double n;     scanf( %lf , &n);     return n;   }   const long long inf = (long long)1e17;   const long long mod = (long long)1e9 + 7;   const int N = 1e5 + 5;   struct CHT_DEC_MIN {     vector<long long> m, b;     bool bad(int f1, int f2, int f3) {       return 1.0 * (b[f2] - b[f1]) * (m[f1] - m[f3]) >=              1.0 * (b[f3] - b[f1]) * (m[f1] - m[f2]);     }     void add(long long M, long long B) {       m.push_back(M), b.push_back(B);       int sz = (int)m.size();       while (sz >= 3 && bad(sz - 3, sz - 2, sz - 1)) {         m.erase(m.end() - 2);         b.erase(b.end() - 2);         sz--;       }     }     long long f(int idx, long long X) { return m[idx] * X + b[idx]; }     long long query(long long X) {       int low = 0, high = (int)m.size() - 1;       while (high - low > 5) {         int mid1 = (low + low + high) / 3;         int mid2 = (low + high + high) / 3;         if (f(mid1, X) >= f(mid2, X)) {           low = mid1 + 1;         } else {           high = mid2 - 1;         }       }       long long res = f(low, X);       for (int i = low + 1; i <= high; i++) {         res = min(res, f(i, X));       }       return res;     }   };   long long a[N], s[N];   CHT_DEC_MIN tree[4 * N];   void update(int pos, int left, int right, int x, long long M, long long B) {     tree[pos].add(M, B);     if (left == right) return;     int mid = (left + right) >> 1;     if (x <= mid)       update(pos * 2, left, mid, x, M, B);     else       update(pos * 2 + 1, mid + 1, right, x, M, B);   }   long long query(int pos, int left, int right, int x, int y, long long X) {     if (left > y || right < x) return (long long)1e17;     if (left >= x && right <= y) {       return tree[pos].query(X);     }     int mid = (left + right) >> 1;     long long m = query(pos * 2, left, mid, x, y, X);     long long n = query(pos * 2 + 1, mid + 1, right, x, y, X);     return min(m, n);   }   int solve() {     int n = in();     vector<pair<long long, int> > tem;     for (int i = 1; i <= n; i++) {       a[i] = Lin();       s[i] += s[i - 1] + a[i];       tem.push_back({a[i], i});     }     sort(tem.begin(), tem.end());     for (int i = 0; i < n; i++) {       int idx = tem[i].second;       long long M = -a[idx], B = a[idx] * idx - s[idx];       update(1, 1, n, idx, M, B);     }     int q = in();     while (q--) {       int x = in(), y = in();       x = y - x + 1;       printf( %lld n , s[y] + query(1, 1, n, x, y, (long long)x - 1));     }     return 0;   }   int main() {     int test = 1, tc = 0;     while (test--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void write(ostream& out, T t) {     out << t <<   n ;   }   template <typename T, typename... Args>   void write(ostream& out, T t, Args... args) {     out << t <<    ;     write(out, args...);   }   template <typename... Args>   void debug(Args... args) {}   template <typename T>   inline T abs(T t) {     return t < 0 ? -t : t;   }   const long long modn = 1000000007;   const long long modn2 = 10000019;   inline long long mod(long long x) { return x % modn; }   const int MAXK = 1003, MAXQ = 2 * 30004 + 10010;   const int MAXN = MAXQ;   const long long INF = 9000000000LL;   int n, m, k;   vector<pair<long long, int> > s;   long long dp[15009][MAXK];   void prep_cin() {     ios::sync_with_stdio(false);     cin.tie(0);   }   struct qry {     int t, id;   };   vector<qry> q;   int L[MAXN], R[MAXN];   int acu[MAXN];   long long pot[MAXK];   int h;   inline long long f(int h) {     long long res = 0;     long long mx = 0;     for (int a = 1; a <= k; a++) {       mx = mod(max(mx, dp[h][a]));       res = mod(res + mod(mx * pot[a - 1]));     }     return res;   }   inline bool sup(int i, int j, int l, int r) { return (i < l && j > r); }   inline void add(int id) {     h++;     int pes = s[id].second;     long long val = s[id].first;     for (int a = 0; a <= k; a++) {       dp[h][a] = dp[h - 1][a];       if (a >= pes && dp[h - 1][a - pes] != -INF)         dp[h][a] = max(dp[h][a], dp[h - 1][a - pes] + val);     }   }   void go(int i, int j) {     if (i == j) {       if (q[i].t == 3) printf( %lld n , f(h));       return;     }     int m = (i + j) / 2;     if (acu[m] - (i == 0 ? 0 : acu[i - 1]) != 0) {       int old = h;       for (int a = m + 1; a <= j; a++) {         if (q[a].t == 1 || q[a].t == 3) continue;         int id = q[a].id;         if (sup(L[id], R[id], i, m)) add(id);       }       go(i, m);       h = old;     }     if (acu[j] - acu[m] != 0) {       int old = h;       for (int a = i; a <= m; a++) {         if (q[a].t == 2 || q[a].t == 3) continue;         int id = q[a].id;         if (sup(L[id], R[id], m + 1, j)) add(id);       }       go(m + 1, j);       h = old;     }   }   int main() {     scanf( %d%d , &n, &k);     pot[0] = 1;     for (int a = 1; a < k; a++) pot[a] = mod(pot[a - 1] * modn2);     const int inf = 1e7;     for (int a = 0; a < n; a++) {       pair<long long, int> x;       scanf( %lld %d , &x.first, &x.second);       s.push_back(x);       q.push_back({1, a});       L[a] = a;       R[a] = inf;     }     scanf( %d , &m);     for (int a = 0; a < m; a++) {       qry p;       scanf( %d , &p.t);       if (q.size() > 0) acu[q.size()] = acu[q.size() - 1];       if (p.t == 1) {         pair<long long, int> x;         scanf( %lld %d , &x.first, &x.second);         s.push_back(x);         p.id = (int)s.size() - 1;         L[p.id] = q.size();         R[p.id] = inf;       } else if (p.t == 2) {         scanf( %d , &p.id);         p.id--;         R[p.id] = q.size();       } else         acu[q.size()]++;       q.push_back(p);     }     n = s.size();     m = q.size();     for (int a = 0; a <= k; a++) dp[0][a] = -INF;     dp[0][0] = 0;     go(0, m - 1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t, n, k, p;     long long a[50];     cin >> t;     while (t--) {       cin >> n >> k;       for (int i = 0; i < n; i++) cin >> a[i];       long long limit = 10e16;       p = 1;       while (p < limit) {         p *= k;       }       if (p > limit) {         p /= k;       }       while (p != 0) {         for (int i = 0; i < n; i++) {           if (a[i] >= p) {             a[i] -= p;             break;           }         }         p /= k;       }       bool checker = true;       for (int i = 0; i < n; i++) {         if (a[i] != 0) {           checker = false;           break;         }       }       if (checker) {         cout <<  YES n ;       } else {         cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100005, MAX_K = 105;   int N, M, K, S;   int good_type[MAX_N];   int distances[MAX_N][MAX_K];   vector<int> adj[MAX_N];   int dist[MAX_N];   void RunBFS(int type) {     queue<int> q;     for (int i = 0; i < N; ++i) {       if (good_type[i] == type) {         distances[i][type] = 0;         q.push(i);       }     }     while (!q.empty()) {       int u = q.front();       q.pop();       for (const int& v : adj[u]) {         if (distances[v][type] == -1) {           distances[v][type] = distances[u][type] + 1;           q.push(v);         }       }     }   }   int main() {     scanf( %d %d %d %d , &N, &M, &K, &S);     for (int i = 0; i < N; ++i) {       scanf( %d , &good_type[i]);       --good_type[i];     }     for (int i = 0; i < M; ++i) {       int u, v;       scanf( %d %d , &u, &v);       --u;       --v;       adj[u].push_back(v);       adj[v].push_back(u);     }     memset(distances, -1, sizeof distances);     for (int i = 0; i < K; ++i) {       RunBFS(i);     }     for (int i = 0; i < N; ++i) {       sort(distances[i], distances[i] + K);       long long sum = 0;       for (int j = 0; j < S; ++j) sum += distances[i][j];       printf( %lld  , sum);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 7;   const long long mod = 1e9 + 7;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   map<long long, long long> mp;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long i = 1;     vector<long long> v;     long long n, a, b, m;     cin >> m >> a >> b;     long long k = gcd(a, b);     long long z = m / k;     long long val1 = m * z - (k * z * (z + 1) / 2) + z + m + 1;     long long val2 = 0;     long long start = 0;     long long cnt = 0;     long long ans = 0;     long long currmx = 0;     while (start <= (a + b)) {       if (mp[start]) break;       mp[start] = 1;       currmx = max(currmx, start);       if (start <= m) {         ans += min(currmx - start, m - start + 1);       }       if (start >= b)         start -= b;       else         start += a;     }     cout << val1 - ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long linf = 1LL << 62;   const int iinf = 1000000009;   const double dinf = 1e17;   const int Mod = 1e9 + 9;   const int maxn = 1000005;   int n;   long long a, b[maxn];   long long dp[3][maxn];   void solve() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a, b[i] = __builtin_popcountll(a);     long long suf = 0;     dp[0][n + 1] = 1;     long long res = 0;     for (int i = n; i >= 1; i--) {       long long sum = 0, mx = 0, ret = 0;       for (int j = i; j <= min(n, i + 64); j++) {         sum += b[j];         mx = max(b[j], mx);         if (mx > sum - mx && sum % 2 == 0) ret--;       }       suf += b[i];       if (suf & 1)         ret += dp[1][i + 1];       else         ret += dp[0][i + 1];       res += ret;       dp[0][i] = dp[0][i + 1];       dp[1][i] = dp[1][i + 1];       if (suf & 1)         dp[1][i]++;       else         dp[0][i]++;     }     cout << res << endl;   }   int main() {     int t = 1;     for (int i = 1; i <= t; i++) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 511, inf = 1e9 + 100;   bool m[max_n][max_n];   bool vis[max_n][max_n];   int mini[max_n], maxi[max_n];   int minj[max_n], maxj[max_n];   int si, sj;   int n;   int di[] = {0, 0, 1, -1};   int dj[] = {-1, 1, 0, 0};   char dir[] =  LRDU ;   string result;   bool on_field(int i, int j) { return i >= 0 && j >= 0 && i < n && j < n; }   void upd_mx(int i, int j) {     mini[j] = min(mini[j], i);     maxi[j] = max(maxi[j], i);     minj[i] = min(minj[i], j);     maxj[i] = max(maxj[i], j);   }   void dfs(int i, int j) {     if (vis[i][j]) {       return;     }     vis[i][j] = 1;     if (m[i][j] == 0) {       result +=  1 ;       m[i][j] = 1;       upd_mx(i, j);     }     if (on_field(i - 1, j) && !vis[i - 1][j] && mini[j] < i) {       result +=  U ;       dfs(i - 1, j);       result +=  D ;     }     if (on_field(i + 1, j) && !vis[i + 1][j] && maxi[j] > i) {       result +=  D ;       dfs(i + 1, j);       result +=  U ;     }     if (on_field(i, j - 1) && !vis[i][j - 1] && minj[i] < j) {       result +=  L ;       dfs(i, j - 1);       result +=  R ;     }     if (on_field(i, j + 1) && !vis[i][j + 1] && maxj[i] > j) {       result +=  R ;       dfs(i, j + 1);       result +=  L ;     }   }   int main() {     for (int i = 0; i < max_n; ++i) {       minj[i] = mini[i] = max_n;       maxj[i] = maxi[i] = -1;     }     cin >> n >> si >> sj;     --si, --sj;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         cin >> m[i][j];         if (m[i][j] == 1) {           mini[j] = min(mini[j], i);           maxi[j] = max(maxi[j], i);           minj[i] = min(minj[i], j);           maxj[i] = max(maxj[i], j);         }       }     }     dfs(si, sj);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         if (m[i][j] && !vis[i][j]) {           cout <<  NO  << endl;           return 0;         }       }     }     int fi = si, fj = sj;     while (si > 0) {       int upj = -1;       for (int j = 0; j < n; ++j) {         if (m[si][j] && m[si - 1][j]) {           upj = j;           break;         }       }       if (upj == -1) {         break;       }       while (sj < upj) {         result +=  R ;         ++sj;       }       while (sj > upj) {         result +=  L ;         --sj;       }       result +=  U ;       --si;     }     while (si < fi) {       int dj = -1;       for (int j = 0; j < n; ++j) {         if (m[si][j] && m[si + 1][j]) {           dj = j;           break;         }       }       if (dj == -1) {         break;       }       while (sj < dj) {         result +=  R ;         ++sj;       }       while (sj > dj) {         result +=  L ;         --sj;       }       while (sj > 0 && m[si][sj - 1]) {         result +=  L ;         --sj;       }       while (sj < dj) {         result +=  2 ;         result +=  R ;         ++sj;       }       while (sj + 1 < n && m[si][sj + 1]) {         result +=  R ;         ++sj;       }       while (sj > dj) {         result +=  2 ;         result +=  L ;         --sj;       }       result +=  2 ;       result +=  D ;       ++si;     }     while (si + 1 < n) {       int dj = -1;       for (int j = 0; j < n; ++j) {         if (m[si][j] && m[si + 1][j]) {           dj = j;           break;         }       }       if (dj == -1) {         break;       }       while (sj < dj) {         result +=  R ;         ++sj;       }       while (sj > dj) {         result +=  L ;         --sj;       }       result +=  D ;       ++si;     }     while (si > fi) {       int upj = -1;       for (int j = 0; j < n; ++j) {         if (m[si][j] && m[si - 1][j]) {           upj = j;           break;         }       }       if (upj == -1) {         break;       }       while (sj < upj) {         result +=  R ;         ++sj;       }       while (sj > upj) {         result +=  L ;         --sj;       }       while (sj > 0 && m[si][sj - 1]) {         result +=  L ;         --sj;       }       while (sj < upj) {         result +=  2 ;         result +=  R ;         ++sj;       }       while (sj + 1 < n && m[si][sj + 1]) {         result +=  R ;         ++sj;       }       while (sj > upj) {         result +=  2 ;         result +=  L ;         --sj;       }       result +=  2 ;       result +=  U ;       --si;     }     while (sj < fj) {       result +=  R ;       ++sj;     }     while (sj > fj) {       result +=  L ;       --sj;     }     while (sj > 0 && m[si][sj - 1]) {       result +=  L ;       --sj;     }     while (sj < fj) {       result +=  2 ;       result +=  R ;       ++sj;     }     while (sj + 1 < n && m[si][sj + 1]) {       result +=  R ;       ++sj;     }     while (sj > fj) {       result +=  2 ;       result +=  L ;       --sj;     }     result +=  2 ;     if (result.size() > 2e6) {       exit(228);     }     cout <<  YES  << endl;     cout << result << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   #pragma comment(linker,  /STACK:2000000 )   using namespace std;   template <int D, typename T>   struct Vec : public vector<Vec<D - 1, T>> {     static_assert(D >= 1,  Vector dimension must be greater than zero! );     template <typename... Args>     Vec(int n = 0, Args... args)         : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}   };   template <typename T>   struct Vec<1, T> : public vector<T> {     Vec(int n = 0, T val = T()) : vector<T>(n, val) {}   };   using ll = long long;   using db = long double;   using ii = pair<int, int>;   const int N = 2e5 + 5, LG = 19, MOD = 998244353;   const int SQ = 320;   const long double EPS = 1e-7;   void printCase(int cs) { cout <<  Case #  << cs <<  :  ; }   int p[N], n, q;   int fast(int b, int e) {     int res = 1;     for (; e; e >>= 1, b = 1ll * b * b % MOD)       if (e & 1) {         res = 1ll * res * b % MOD;       }     return res;   }   int t[N << 2], lz[N << 2], cur = 1;   void build(int node, int s, int e) {     lz[node] = 1;     if (s == e) {       t[node] = cur;       cur = 1ll * cur * p[s] % MOD;       return;     }     int md = (s + e) >> 1;     build(node << 1, s, md);     build(node << 1 | 1, md + 1, e);     t[node] = t[node << 1] + t[node << 1 | 1];     if (t[node] >= MOD) t[node] -= MOD;   }   void push(int node, int s, int e) {     if (lz[node] == 1) return;     t[node] = 1ll * t[node] * lz[node] % MOD;     if (s != e) {       lz[node << 1] = 1ll * lz[node] * lz[node << 1] % MOD;       lz[node << 1 | 1] = 1ll * lz[node] * lz[node << 1 | 1] % MOD;     }     lz[node] = 1;   }   int qry(int node, int s, int e, int l, int r) {     push(node, s, e);     if (r < s || e < l) return 0;     if (l <= s && e <= r) return t[node];     int md = (s + e) >> 1;     int ret = qry(node << 1, s, md, l, r) + qry(node << 1 | 1, md + 1, e, l, r);     if (ret >= MOD) ret -= MOD;     return ret;   }   void upd(int node, int s, int e, int l, int r, int v) {     push(node, s, e);     if (r < s || e < l) return;     if (l <= s && e <= r) {       lz[node] = 1ll * lz[node] * v % MOD;       push(node, s, e);       return;     }     int md = (s + e) >> 1;     upd(node << 1, s, md, l, r, v);     upd(node << 1 | 1, md + 1, e, l, r, v);     t[node] = t[node << 1] + t[node << 1 | 1];     if (t[node] >= MOD) t[node] -= MOD;   }   int sum[N];   int qry(int l, int r) { return 1ll * sum[r] * fast(sum[l - 1], MOD - 2) % MOD; }   int get(int l, int r) {     return 1ll * qry(1, 1, n, l, r) * fast(qry(l, r), MOD - 2) % MOD;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> q;     const int INV100 = fast(100, MOD - 2);     sum[0] = 1;     for (int i = 1; i < n + 1; i++) {       cin >> p[i];       p[i] = 1ll * p[i] * INV100 % MOD;       sum[i] = 1ll * sum[i - 1] * p[i] % MOD;     }     build(1, 1, n);     set<int> checkPoints({1, n + 1});     int ans = get(1, n);     while (q--) {       int x;       cin >> x;       if (checkPoints.count(x)) {         int l = *(--checkPoints.find(x));         int r = *(checkPoints.upper_bound(x));         ans -= get(l, x - 1);         if (ans < 0) ans += MOD;         ans -= get(x, r - 1);         if (ans < 0) ans += MOD;         int toMul = 1ll * qry(1, 1, n, x - 1, x - 1) * p[x - 1] % MOD;         upd(1, 1, n, x, r - 1, toMul);         ans += get(l, r - 1);         if (ans >= MOD) ans -= MOD;         checkPoints.erase(x);       } else {         int l = *(--checkPoints.upper_bound(x));         int r = *(checkPoints.upper_bound(x));         ans = (ans - get(l, r - 1));         if (ans < 0) ans += MOD;         ans += get(l, x - 1);         if (ans >= MOD) ans -= MOD;         int toDivide = 1ll * qry(1, 1, n, x - 1, x - 1) * p[x - 1] % MOD;         toDivide = fast(toDivide, MOD - 2);         upd(1, 1, n, x, r - 1, toDivide);         ans += get(x, r - 1);         if (ans >= MOD) ans -= MOD;         checkPoints.insert(x);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long b, c, m, n, k, l;     cin >> m >> k;     l = k;     for (int i = 0; i < k; i++) {       if (m % 10 == 0)         m /= 10;       else         m--;     }     cout << m;   }


#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       int a[n];       for (int i = 0; i < n; i++) {         scanf( %d , &a[i]);       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < n - i - 1; j++) {           if (a[j + 1] < a[j]) {             int t = a[j];             a[j] = a[j + 1];             a[j + 1] = t;           }         }       }       int ans = INT_MAX;       for (int i = 0; i < n - 1; i++) {         if (a[i + 1] - a[i] < ans) {           ans = a[i + 1] - a[i];           if (ans == 0) break;         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXD = 500;   const int MAXS = 5000;   bool vis[MAXD][MAXS + 1];   int dig[MAXD][MAXS + 1], from[MAXD][MAXS + 1];   struct Data {     int last, c, prev_r;     int r, sum;     bool operator<(const Data &other) const { return last < other.last; }   };   queue<Data> mst;   int main() {     int d, s;     ios::sync_with_stdio(false);     cin >> d >> s;     mst.push({0, 0, 0, 0, 0});     int last = 0;     while (!mst.empty()) {       auto it = mst.front();       int r = it.r, sum = it.sum;       int prev_r = it.prev_r, c = it.c;       mst.pop();       if (vis[r][sum]) {         continue;       }       vis[r][sum] = 1;       dig[r][sum] = c;       from[r][sum] = prev_r;       for (c = 0; c < 10; c++) {         int cur = (r * 10 + c) % d;         if (sum + c <= s && vis[cur][sum + c] == 0) {           mst.push({++last, c, r, cur, sum + c});         }       }     }     if (vis[0][s]) {       vector<int> sol;       int r = 0;       while (s > 0) {         int cur = dig[r][s];         sol.push_back(cur);         r = from[r][s];         s -= cur;       }       reverse(sol.begin(), sol.end());       for (auto it : sol) {         cout << it;       }       return 0;     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     if (n == 2) {       cout << 2;     } else {       cout << 1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mod = 998244353;   const int M = 2e5 + 10;   const int N = 5e2 + 10;   inline long long read() {     long long b = 1, sum = 0;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) b = -1;       c = getchar();     }     while (isdigit(c)) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return b * sum;   }   int hs, gs;   int n, m, k, T;   int h, g, a[1 << 22];   long long sum;   vector<int> ans;   void change(int x) {     int l = 2 * x, r = 2 * x + 1;     if (a[l] == 0 && a[r] == 0) {       a[x] = 0;     } else {       if (a[l] > a[r]) {         a[x] = a[l];         change(l);       } else {         a[x] = a[r];         change(r);       }     }   }   int pull(int x) {     int l = 2 * x, r = 2 * x + 1;     if (a[l] == 0 && a[r] == 0) {       return x;     } else {       if (a[l] > a[r]) {         return pull(l);       } else {         return pull(r);       }     }   }   void solv(int x) {     if (a[x] == 0) return;     int l = 2 * x, r = 2 * x + 1;     while (pull(x) >= gs) {       ans.push_back(x);       sum -= a[x];       change(x);     }     solv(l);     solv(r);   }   int main() {     int i, j;     T = read();     while (T--) {       sum = 0;       h = read(), g = read();       hs = (1 << h);       gs = (1 << g);       n = hs - gs;       for (i = 1; i <= hs - 1; i++) {         a[i] = read();         a[i + n] = a[i + 2 * n] = 0;         sum += a[i];       }       ans.clear();       solv(1);       printf( %lld n , sum);       for (i = 0; i < ans.size(); i++) {         printf( %d  , ans[i]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int v[1100];   int pd[1100][1 << 8];   int qtd1, qtd2;   int vis[1100][1 << 8];   int inv[1100];   vector<int> pos[10];   bool check(int mask) {     for (int i = 0; i < 8; ++i) {       if (mask & (1 << i)) continue;       if (qtd1) return false;     }     return true;   }   int f(int x, int mask) {     if (x >= n) {       if (check(mask))         return 0;       else         return -0x3F3F3F3F;     }     int &p = pd[x][mask];     if (vis[x][mask] < qtd2) {       vis[x][mask] = qtd2;       p = f(x + 1, mask);       if (!(mask & (1 << v[x]))) {         int fim, qtd;         qtd = pos[v[x]].size() - inv[x];         if (qtd1 && qtd >= qtd1) {           fim = pos[v[x]][inv[x] + qtd1 - 1] + 1;           p = max(p, f(fim, mask | (1 << v[x])) + qtd1);         }         qtd = pos[v[x]].size() - inv[x];         if (qtd >= qtd2) {           fim = pos[v[x]][inv[x] + qtd2 - 1] + 1;           p = max(p, f(fim, mask | (1 << v[x])) + qtd2);         }       }     }     return p;   }   int main() {     int ans = 1;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , &v[i]);       v[i]--;       inv[i] = (int)pos[v[i]].size();       pos[v[i]].push_back(i);     }     for (int i = 0; i < n; ++i) {       qtd1 = i, qtd2 = i + 1;       ans = max(ans, f(0, 0));     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   const int inf = 0x7FFFFFFF;   template <class T>   inline void checkmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   void show(T a, int n) {     for (int i = 0; i < n; ++i) cout << a[i] <<    ;     cout << endl;   }   template <class T>   void show(T a, int r, int l) {     for (int i = 0; i < r; ++i) show(a[i], l);     cout << endl;   }   struct S {     long long w, h;     long long bord, space;     string tp;     vector<int> son;     int vis;     void init(int x, int y, string t) {       w = x, h = y;       bord = space = 0;       tp = t;       son.clear();       vis = false;     }     void dfs();   } s[100000];   char buf[10000];   void S::dfs() {     vis = true;     if (son.size() > 0) {       for (int i = 0; i < son.size(); i++)         if (!s[son[i]].vis) {           s[son[i]].dfs();         }       if (tp ==  VBox ) {         for (int i = 0; i < son.size(); i++) {           int v = son[i];           w = max(s[v].w, w);           if (i > 0) h += space;           h += s[v].h;         }         w += 2 * bord;         h += 2 * bord;       } else if (tp ==  HBox ) {         for (int i = 0; i < son.size(); i++) {           int v = son[i];           h = max(s[v].h, h);           if (i > 0) w += space;           w += s[v].w;         }         w += 2 * bord;         h += bord * 2;       }     }   }   string nn[10000];   struct SS {     string s;     long long x, y;     const bool operator<(const SS &b) const { return s < b.s; }   } ss[10000];   int main() {     int n;     while (cin >> n) {       map<string, int> mp;       int cnt = 0;       for (int i = 0; i < n; i++) {         scanf(  %[^ n] , buf);         bool newtp = true;         for (int i = 0; buf[i]; i++)           if (buf[i] ==  (  || buf[i] ==  )  || buf[i] ==  .  || buf[i] ==  , ) {             if (buf[i] ==  . ) newtp = false;             buf[i] =    ;           }         istringstream sin(buf);         string t, nm, nm2;         sin >> t;         if (t ==  Widget  || t ==  VBox  || t ==  HBox ) {           int x = 0, y = 0;           sin >> nm;           mp[nm] = cnt;           nn[cnt] = nm;           if (t ==  Widget ) {             sin >> x >> y;           }           s[cnt].init(x, y, t);           cnt++;         } else {           nm = t;           int x;           sin >> t;           if (t ==  set_border ) {             sin >> x;             s[mp[nm]].bord = x;           } else if (t ==  set_spacing ) {             sin >> x;             s[mp[nm]].space = x;           } else if (t ==  pack ) {             sin >> nm2;             s[mp[nm]].son.push_back(mp[nm2]);           } else             puts( fuck );         }       }       for (int i = 0; i < cnt; i++) {         if (!s[i].vis) s[i].dfs();       }       for (int i = 0; i < cnt; i++) {         ss[i].s = nn[i];         ss[i].x = s[i].w;         ss[i].y = s[i].h;       }       sort(ss, ss + cnt);       for (int i = 0; i < cnt; i++) {         cout << ss[i].s <<     << ss[i].x <<     << ss[i].y << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7;   template <class T>   void dbs(string str, T t) {     cerr << str <<   :   << t <<   n ;   }   template <class T, class... second>   void dbs(string str, T t, second... s) {     int idx = str.find( , );     cerr << str.substr(0, idx) <<   :   << t <<  ,  ;     dbs(str.substr(idx + 1), s...);   }   template <class second, class T>   ostream& operator<<(ostream& os, const pair<second, T>& p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <class T>   void prc(T a, T b) {     cerr <<  [ ;     for (T i = a; i != b; ++i) {       if (i != a) cerr <<  ,  ;       cerr << *i;     }     cerr <<  ] n ;   }   const int N = 2020;   long long first[N][N];   long long G[N][N];   string s;   long long g(int n, int k);   long long f(int n, int k) {     if (k < 0) return 0;     if (n == 0) return k == 0;     long long& ans = first[n][k];     if (ans != -1) return ans;     ans = 0;     int ch = s[int(s.size()) - n] -  a ;     ans = ch * f(n - 1, k) + (25 - ch) * f(n - 1, k - n) + g(n - 1, k) + (k == 0);     int nk = k;     for (int i = 1; i <= n - 1; ++i) {       int ch = s[int(s.size()) - n + i] -  a ;       nk = k - (i + 1) * (n - i);       if (nk < 0) {         for (int i = n - 1; i >= 1; --i) {           int ch = s[int(s.size()) - n + i] -  a ;           nk = k - (i + 1) * (n - i);           if (nk < 0) break;           ans += (25 - ch) * f(n - i - 1, nk);           ans %= inf;         }         break;       }       ans += (25 - ch) * f(n - i - 1, nk);       ans %= inf;     }     ans %= inf;     return ans;   }   long long g(int n, int k) {     if (k < 0) return 0;     if (n == 0) return 0;     long long& ans = G[n][k];     if (ans != -1) return ans;     int ch = s[int(s.size()) - n] -  a ;     ans = ch * f(n - 1, k) + g(n - 1, k);     ans %= inf;     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     memset(first, -1, sizeof first);     memset(G, -1, sizeof G);     cin >> s;     cout << f(n, k) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &a) {     a = 0;     int c = getchar(), b = 1;     while (c >  9  || c <  0 ) {       if (c ==  - ) b = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) a = (a << 3) + (a << 1) + c - 48, c = getchar();     a *= b;   }   inline int Rem() {     int a = 0, c = getchar(), b = 1;     while (c >  9  || c <  0 ) {       if (c ==  - ) b = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) a = (a << 3) + (a << 1) + c - 48, c = getchar();     return a *= b;   }   inline void write(long long x) {     if (x > 9) write(x / 10);     putchar( 0  + x % 10);   }   inline void W(long long x) {     if (x < 0) {       putchar( - ), x = -x;     }     write(x);   }   const int N = 2e5 + 5, M = 1e6 + 5;   int n, m, a[N], pos[N], block;   long long ans, rec[N], cnt[M];   struct node {     int l, r, id;   } q[N];   inline bool cmp(node a, node b) {     return (pos[a.l] == pos[b.l]) ? (pos[a.l] & 1 ? a.r < b.r : a.r > b.r)                                   : a.l < b.l;   }   inline void add(int x) {     cnt[x]++;     ans += ((cnt[x] * cnt[x]) - ((cnt[x] - 1) * (cnt[x] - 1))) * x;   }   inline void del(int x) {     ans -= ((cnt[x] * cnt[x]) - ((cnt[x] - 1) * (cnt[x] - 1))) * x;     cnt[x]--;   }   int main() {     read(n);     read(m);     block = sqrt(n);     for (register int i = 1; i <= n; i++) read(a[i]), pos[i] = i / block;     for (register int i = 1; i <= m; i++) {       read(q[i].l);       read(q[i].r);       q[i].id = i;     }     sort(q + 1, q + 1 + m, cmp);     for (register int i = 1, ql, qr, l = 1, r = 0; i <= m; i++) {       ql = q[i].l, qr = q[i].r;       while (ql < l) add(a[--l]);       while (ql > l) del(a[l++]);       while (qr < r) del(a[r--]);       while (qr > r) add(a[++r]);       rec[q[i].id] = ans;     }     for (register int i = 1; i <= m; i++) W(rec[i]), putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10, pi = 3.1415926535898;   const int mod = 998244353, maxn = 2e5 + 10;   int T, n, m, k, x, y, a[maxn], p, b;   long long fac[maxn] = {1, 1}, inv[maxn] = {1, 1}, inv_fac[maxn] = {1, 1};   char st[maxn];   void init() {     for (int i = 2; i < maxn; ++i) {       fac[i] = fac[i - 1] * i % mod;       inv[i] = (mod - mod / i) * inv[mod % i] % mod;       inv_fac[i] = inv_fac[i - 1] * inv[i] % mod;     }   }   long long C(long long n, long long m) {     if (m < 0 || n < 0 || n < m) return 0;     return fac[n] * inv_fac[n - m] % mod * inv_fac[m] % mod;   }   int main(int argc, char const *argv[]) {     init();     scanf( %d %d , &n, &m);     if (n == 2) {       printf( 0 n );       return 0;     }     long long ans = 0, cnt = 0;     for (int i = n - 2; i <= m - 1; ++i) {       cnt += C(i, n - 2);     }     cnt %= mod;     for (int i = 2; i <= n - 1; ++i) {       long long cc = cnt;       cc = cc * C(n - 3, i - 2) % mod * (n - 2) % mod;       ans += cc;     }     cout << ans % mod << endl;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b, str;     int n, cont = 0, ca = 0, cb = 0;     cin >> a >> b;     n = a.size();     for (int i = 0; i < n; i++) {       if (a[i] != b[i]) cont++;     }     if (cont % 2 == 0) {       for (int i = 0; i < n; i++) {         if (a[i] != b[i]) {           if (ca > cb)             cb++, str += b[i];           else             ca++, str += a[i];         } else           str += a[i];       }       cout << str << endl;     } else       printf( impossible n );   }


#include <bits/stdc++.h>   using namespace std;   inline void input(long long &x) {     long long y = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) y = -1;       c = getchar();     }     x = 0;     while (c >=  0  && c <=  9 ) {       x = x * 10 + (c -  0 );       c = getchar();     }     x = x * y;   }   const long long inf = 1e18;   long long f, t, temp, a1, cnt, p1, a2, now, p2, resf, rest;   long long ans = inf;   signed main() {     input(f);     input(t);     input(temp);     input(a1);     input(cnt);     input(p1);     input(a2);     input(now);     input(p2);     if (cnt > now) {       swap(a1, a2);       swap(p1, p2);       swap(cnt, now);     }     for (register long long i = 0; (i - 1) * a1 <= f; ++i) {       resf = f - min(i * a1, f);       rest = t - min(i * a1, f) * cnt;       if (!resf) {         if (rest < 0) break;         ans = min(ans, i * p1);         break;       }       if (temp * resf > rest && now * resf > rest) continue;       if (temp * resf <= rest)         ans = min(ans, i * p1);       else {         long long x, pk;         if ((rest - temp * resf) % (now - temp) == 0)           x = (rest - temp * resf) / (now - temp) + 0;         else           x = (rest - temp * resf) / (now - temp) + 1;         if (x % a2 == 0)           pk = (x / a2) + 0;         else           pk = (x / a2) + 1;         ans = min(ans, pk * p2 + i * p1);       }     }     ans = max(ans, 0ll);     if (ans < inf)       printf( %lld n , ans);     else       puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)1e5 + 10;   int n, cnt = 0;   string a, b;   void check(string a, string b) {     int p = 0;     for (; p < ((int)(a).size()) && a[p] == b[p]; ++p)       ;     if (a.substr(0, p) == b.substr(0, p) && a.substr(p) == b.substr(p + 1)) ++cnt;   }   int main() {     cin >> n >> a >> b;     int p = 0;     for (; a[p] == b[p]; ++p)       ;     if (a.substr(0, p) == b.substr(0, p) && a.substr(p + 1) == b.substr(p + 1))       puts( 2 ), exit(0);     check(b.substr(p + 1), a.substr(p));     check(a.substr(p + 1), b.substr(p));     cout << cnt << endl;     fclose(stdin);     fclose(stdout);     return 0;   }


#include <bits/stdc++.h>      using namespace std;   typedef long long ll;   ll a[200009],n,p;   bool vis[200009][2];   pair <ll,ll> g[200009];   int main()   {       ios::sync_with_stdio(0);       int t;       cin>>t;       while(t--)       {           cin>>n>>p;           ll ans=0;           for(int i=1;i<=n;i++)           {               cin>>a[i];               vis[i][0]=vis[i][1]=0;               g[i]=make_pair(a[i],i);           }           vis[1][0]=vis[n][1]=1;         sort(g+1,g+n+1);         for(int i=1;i<=n;i++)         {             int w=g[i].second;             if(a[w]>p)break;             int l,r;             l=r=w;             while(!vis[l][0]&&(a[l-1]%a[w]==0))l--;             while(!vis[r][1]&&(a[r+1]%a[w]==0))r++;             ans+=(r-l)*a[w];             if(l!=r){vis[l][1]=vis[r][0]=1;}             for(int h=l+1;h<r;h++)vis[h][0]=vis[h][1]=1;               }           for(int i=1;i<=n;i++)if(!vis[i][1])ans+=p;        cout<<ans<<endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   inline istream& operator>>(istream& fin, pair<T1, T2>& pr) {     fin >> pr.first >> pr.second;     return fin;   }   template <class T0, class T1, class T2>   inline istream& operator>>(istream& fin, tuple<T0, T1, T2>& t) {     fin >> get<0>(t) >> get<1>(t) >> get<2>(t);     return fin;   }   template <class T>   inline istream& operator>>(istream& fin, vector<T>& a) {     for (auto& u : a) fin >> u;     return fin;   }   template <class T, size_t n>   inline istream& operator>>(istream& fin, array<T, n>& a) {     for (auto& u : a) fin >> u;     return fin;   }   int f542() {     size_t n, time;     cin >> n >> time;     typedef struct {       size_t buf, val;     } value_t;     const auto comp = [](const value_t& a, const value_t& b) {       return a.buf < b.buf || (a.buf == b.buf && a.val < b.val);     };     priority_queue<value_t, vector<value_t>, decltype(comp)> heap(comp);     for (size_t i = 0; i < n; ++i) {       size_t val, lag;       cin >> lag >> val;       if (lag < time + 1) heap.push({time - lag, val});     }     size_t val = 0;     while (!heap.empty()) {       const auto top = heap.top();       heap.pop();       if (top.buf == 0 || heap.empty())         val = max(val, top.val);       else {         const auto next = heap.top();         if (top.buf != next.buf)           heap.push({next.buf, top.val});         else {           heap.pop();           heap.push({top.buf - 1, top.val + next.val});         }       }     }     return val;   }   int main(const int argc, char* argv[]) {     cout << f542();     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, r;     cin >> n >> r;     vector<double> a(1 << n);     double sum = 0, div = (double)(1 << n);     for (int i = 0; i < (1 << n); i++) {       int x;       cin >> x;       a[i] = (double)x;       sum += a[i];     }     double ans = sum / div;     cout << setprecision(7) << fixed << ans <<   n ;     while (r--) {       int z, g;       cin >> z >> g;       double gg = (double)g;       sum += (gg - a[z]);       a[z] = gg;       ans = sum / div;       cout << setprecision(7) << fixed << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   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 modInverse(long long a, long long p) { return power(a, p - 2, p); }   long long gcd(long long x, long long y) {     if (x == 0 || y == 0) {       return max(y, x);     }     return gcd(y % x, x);   }   long long gcdExtended(long long a, long long b, long long &x, long long &y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long gcd = gcdExtended(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return gcd;   };   long long bit[1504];   void update(long long x) {     while (x <= 1500) {       bit[x]++;       x += (x & (-x));     }   }   long long get(long long x) {     long long ret = 0;     while (x) {       ret += bit[x];       x -= (x & (-x));     }     return ret;   }   long long dp[1504][1504];   int main() {     long long n;     scanf( %lld , &n);     vector<long long> a;     for (long long i = 0; i < n; i++) {       long long x;       scanf( %lld , &x);       a.push_back(x);     }     for (long long i = 0; i < n; i++) {       for (long long j = 1; j <= n; j++) bit[j] = 0;       long long here = 0;       for (long long j = i; j < n; j++) {         update(a[j]);         here += get(a[j] - 1);         dp[i][j] = here;       }     }     long long q;     scanf( %lld , &q);     long long tot = dp[0][n - 1];     tot = (n) * (n - 1);     tot /= 2;     tot -= dp[0][n - 1];     tot %= 2;     while (q--) {       long long x, y;       scanf( %lld %lld , &(x), &(y));       long long len = y - x + 1;       long long here = (len) * (len - 1);       here /= 2;       here %= 2;       tot = tot + here;       tot %= 2;       if (tot)         cout <<  odd  << endl;       else         cout <<  even  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int M = 111111;   int Tit[M];   int ra[M];   int k[M];   int r, j, rslt = 0;   vector<int> vec[M];   void dfs(int u, int v) {     ra[u] = ra[v] + 1;     k[ra[u]] ^= 1;     int j = vec[u].size();     int z;     while (j--) {       z = vec[u][j];       if (z != v) dfs(z, u);     }   }   int main() {     scanf( %d , &r);     for (j = 2; j <= r; j = j + 1) scanf( %d , Tit + j), vec[Tit[j]].push_back(j);     dfs(1, 0);     for (j = 1; j <= r; j = j + 1) rslt += k[j];     cout << rslt;     return 0;   }


#include <bits/stdc++.h>   typedef long long ll;   #define endl   n    #define pq priority_queue <pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>   #define graph1 vector<vector<pair<ll,ll>>>   #define graph vector<vector<ll>>   #define pq1 priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll,ll>>>, greater<pair<ll, pair<ll, ll>>>>   #define loop(i,a,b) for (ll i = a; i < b; i++)   using namespace std;   vector <ll>  arr;   bool check3(ll i)   {       if(max(arr[i],arr[i+2]) >=  arr[i+1] && min(arr[i],arr[i+2]) <=  arr[i+1]  )       {           return true;       }       return false;   }   bool check4(ll i)   {       if(check3(i) ||check3(i + 1))       {           return true;       }       if(max(arr[i],arr[i+3]) >=  arr[i + 1] && min(arr[i],arr[i+3]) <=  arr[i+1]  )       {           return true;       }       if(max(arr[i],arr[i+3]) >=  arr[i + 2] && min(arr[i],arr[i+3]) <=  arr[i+2]  )       {           return true;       }       return false;   }   int main()   {       ios::sync_with_stdio(0); cin.tie(0);       ll t;       cin >> t;       while(t--)       {           ll n;           cin >> n;           arr.resize(n);           loop(i,0,n)               cin >> arr[i];              ll ans = n * 2 - 1;              loop(i,0,n-2)           {               if(!check3(i))               ans++;           }           loop(i,0,n-3)           {               if(!check4(i))               ans++;           }           cout << ans << endl;       }   }


#include <bits/stdc++.h>   using namespace std;   using vi = vector<int>;   using vvi = vector<vi>;   using ll = long long;   using vll = vector<ll>;   using vvll = vector<vll>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   ll power[40];   void dfs(int id, vi& v, vi& s) {     if (id < v.size()) {       s.push_back(v[id]);       dfs(id * 2, v, s);       dfs(id * 2 + 1, v, s);     }   }   void solve() {     int h, g, num;     cin >> h >> g;     vi v(power[h]);     vvi mins(power[g], vi());     set<int> fin;     for (int i = 1; i < power[h]; i++) {       cin >> v[i];     }     for (int i = 0; i < power[g - 1]; i++) {       dfs(i + power[g - 1], v, mins[i + power[g - 1]]);       sort(mins[i + power[g - 1]].begin(), mins[i + power[g - 1]].end(),            greater<int>());     }     for (int i = g; i > 0; i--) {       for (int j = power[i - 1]; j < power[i]; j++) {         fin.insert(*mins[j].rbegin());       }       if (i == 1) {         break;       }       for (int j = power[i - 1]; j < power[i]; j += 2) {         num = max(*mins[j].rbegin(), *mins[j + 1].rbegin());         mins[j].pop_back();         mins[j + 1].pop_back();         mins[j / 2].push_back(v[j / 2]);         merge(mins[j].begin(), mins[j].end(), mins[j + 1].begin(),               mins[j + 1].end(), std::back_inserter(mins[j / 2]), greater<int>());         while (*mins[j / 2].rbegin() < num) {           mins[j / 2].pop_back();         }       }     }     ll sum = 0;     vi print;     print.reserve(power[h] - 1);     for (int i = 1; i < power[h]; i++) {       if (fin.find(v[i]) == fin.end()) {         print.push_back(i);       } else {         sum += v[i];       }     }     reverse(print.begin(), print.end());     cout << sum <<   n ;     for (int i : print) {       cout << i <<    ;     }     cout <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     power[0] = 1;     for (int i = 1; i < 40; i++) {       power[i] = 2ll * power[i - 1];     }     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   struct Point {     double x, y;     Point(double x = 0, double y = 0) : x(x), y(y) {}   };   double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }   double Length(Point A) { return sqrt(Dot(A, A)); }   double Angle(Point A, Point B) {     return acos(Dot(A, B) / Length(A) / Length(B));   }   Point Normal(Point A) {     double L = Length(A);     return Point(-A.y / L, A.x / L);   }   double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; }   Point operator+(Point A, Point B) { return Point(A.x + B.x, A.y + B.y); }   Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }   Point operator*(Point A, double p) { return Point(A.x * p, A.y * p); }   Point operator/(Point A, double p) { return Point(A.x / p, A.y / p); }   int dcmp(double x) {     if (fabs(x) < eps) return 0;     return x < 0 ? -1 : 1;   }   bool operator<(const Point &a, const Point &b) {     return a.x < b.x || (a.x == b.x && a.y < b.y);   }   bool operator==(const Point &a, const Point &b) {     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;   }   double GetAngleByLength(double a, double b, double c) {     double cosA = (b * b + c * c - a * a) / (b * c * 2);     return acos(cosA);   }   Point Rotate(Point A, double rad) {     return Point(A.x * cos(rad) - A.y * sin(rad),                  A.x * sin(rad) + A.y * cos(rad));   }   Point GetLineIntersection(Point P, Point v, Point Q, Point w) {     Point u = P - Q;     double t = Cross(w, u) / Cross(v, w);     return P + v * t;   }   Point R;   Point v;   double solve(Point P, Point vP, Point Q, Point vQ) {     auto X = GetLineIntersection(P, vP, R, v);     auto Y = GetLineIntersection(Q, vQ, R, v);     return Length(P - X) + Length(X - Y) + Length(Q - Y);   }   int main() {     double a, b, c, x1, y1, x2, y2;     cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2;     if (a == 0 || b == 0) {       double ans = fabs(x1 - x2) + fabs(y1 - y2);       printf( %.10f n , ans);       return 0;     }     R.x = 0;     R.y = -c / b;     v = Point(b, -a);     double ans = fabs(x1 - x2) + fabs(y1 - y2);     ans = min(ans, solve(Point(x1, y1), Point(1, 0), Point(x2, y2), Point(1, 0)));     ans = min(ans, solve(Point(x1, y1), Point(1, 0), Point(x2, y2), Point(0, 1)));     ans = min(ans, solve(Point(x1, y1), Point(0, 1), Point(x2, y2), Point(1, 0)));     ans = min(ans, solve(Point(x1, y1), Point(0, 1), Point(x2, y2), Point(0, 1)));     printf( %.10f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, m, a[N], type, l, r;   set<int> q[N];   inline int myrand() { return rand() ^ (rand() << 16); }   struct item {     item *l, *r;     int key, prior, val;     long long sum;     item() {}     item(int _key, int _val) {       val = sum = _val;       key = _key;       prior = myrand();       l = r = nullptr;     }   };   typedef item *pitem;   pitem t[4 * N];   inline long long sum(pitem v) { return v ? v->sum : 0; }   inline void upd_cnt(pitem v) {     if (v) {       v->sum = sum(v->l) + v->val + sum(v->r);     }   }   void Merge(pitem &v, pitem l, pitem r) {     if (!l || !r) {       v = l ? l : r;     } else if (l->prior > r->prior) {       Merge(l->r, l->r, r);       v = l;     } else {       Merge(r->l, l, r->l);       v = r;     }     upd_cnt(v);   }   void Split(pitem v, int key, pitem &l, pitem &r) {     if (!v) {       l = r = nullptr;       return;     }     if (key < v->key) {       Split(v->l, key, l, v->l);       r = v;     } else {       Split(v->r, key, v->r, r);       l = v;     }     upd_cnt(v);   }   void Insert(pitem &v, int key, int val) {     pitem cur;     Split(v, key, v, cur);     Merge(v, v, new item(key, val));     Merge(v, v, cur);   }   void Erase(pitem &v, int key) {     if (!v) {       cout <<  SOSI ;       exit(0);     }     if (v->key == key) {       pitem sosi = v;       Merge(v, v->l, v->r);       delete sosi;       return;     }     Erase(key < v->key ? v->l : v->r, key);     upd_cnt(v);   }   void update(int pos, int key, int val, int add) {     for (int i = pos; i <= n; i |= i + 1) {       if (add == 1) {         Insert(t[i], key, val);       } else {         Erase(t[i], key);       }     }   }   inline long long Get(pitem v, int l, int r) {     pitem cur1, cur2;     Split(v, l - 1, v, cur1);     Split(cur1, r, cur1, cur2);     long long ans = sum(cur1);     Merge(v, v, cur1);     Merge(v, v, cur2);     return ans;   }   long long get(int pos, int tl, int tr) {     long long ans = 0;     for (int i = pos; i >= 1; i &= i + 1, i--) {       ans += Get(t[i], tl, tr);     }     return ans;   }   int main() {     srand(time(nullptr));     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       q[i].insert(-1e9);       t[i] = nullptr;     }     for (int i = 1; i <= n; i++) {       cin >> a[i];       int prv = *(--q[a[i]].insert(i).first);       update(i, prv, i - prv, +1);     }     for (int i = 1; i <= m; i++) {       cin >> type >> l >> r;       if (type == 1) {         auto it = q[a[l]].find(l);         int prv = *prev(it);         it = next(it);         update(l, prv, -1, -1);         if (it != q[a[l]].end()) {           int nxt = *it;           update(nxt, l, -1, -1);           update(nxt, prv, nxt - prv, +1);         }         q[a[l]].erase(l);         a[l] = r;         it = q[a[l]].insert(l).first;         prv = *prev(it);         it = next(it);         if (it != q[a[l]].end()) {           int nxt = *it;           update(nxt, prv, -1, -1);           update(nxt, l, nxt - l, +1);         }         update(l, prv, l - prv, +1);       } else {         cout << get(r, l, r) - get(l - 1, l, r) <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   #define ll long long int   #define fast ios_base::sync_with_stdio(false);  cin.tie(NULL); cout.tie(NULL);   #define scan(a,n) for(long long int index=0;index<n;index++) cin>>a[index];   #define print(a,n) for(long long int index=0;index<n;index++) cout<<a[index]<<   ;cout<<endl;   #define mod 1000000007   #define pb push_back   #define mp make_pair   #define ss second   #define ff first   #define vli vector<long long int>   #define vlli vector<pair<long long int,long long int>>   #define vsi vector<string>   #define all(n) n.begin(),n.end()   #define forn(i,a,b) for(i=a;i<b;i++)   ll mul(ll x,ll y){  return (x*y)%mod;}   ll power(ll x, ll y) {ll res = 1; x %= mod;    while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}   ll mod_inv(ll x) {return power(x, mod - 2);}   ll fact(ll n) {ll res = 1; for (ll i = 2; i <= n; i++) res = mul(res , i); return res; }    ll nCr(ll n, ll r) { return mul(fact(n),mod_inv(mul(fact(r),fact(n - r))));}           int main() {     fast;     #ifndef ONLINE_JUDGE     freopen( input.txt , r ,stdin);     freopen( output.txt , w ,stdout);     #endif     ll t=1;     cin>>t;     while(t--){       ll n,k;       cin>>n>>k;       ll a[n],b[n],i;       for(i=0;i<n;i++){         cin>>a[i]>>b[i];       }       ll f=-1,j;       ll x,y;       for(i=0;i<n;i++){        x=0;        for(j=0;j<n;j++){           y=abs(a[i]-a[j])+abs(b[i]-b[j]);         if(y<=k) x++;        }        if(x==n) f=1;       }       cout<<f<<endl;                           }   return 0;        }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n];     long long sum = 0;     for (int i = 0; i < n; i++) cin >> arr[i];     vector<int> odd, even;     for (int i = 0; i < n; i++) {       if (arr[i] & 1)         odd.push_back(arr[i]);       else         even.push_back(arr[i]);     }     sort(odd.begin(), odd.end());     sort(even.begin(), even.end());     if (odd.size() > even.size()) {       for (int i = 0; i < odd.size() - even.size() - 1; i++) sum += odd[i];     } else if (odd.size() == even.size())       sum = 0;     else {       for (int i = 0; i < even.size() - odd.size() - 1; i++) sum += even[i];     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   using vi = vector<long long int>;   using vvi = vector<vi>;   using vb = vector<bool>;   using vc = vector<char>;   using vs = vector<string>;   using vld = vector<long double>;   using pii = pair<long long int, long long int>;   using psi = pair<string, long long int>;   using pci = pair<char, long long int>;   using vpii = vector<pii>;   long long int mod = 1e9 + 7;   long long int const maxn = 2e5 + 5;   long long int const inf = 1e18;   long long int add(long long int a, long long int b) {     return ((a % mod) + (b % mod)) % mod;   }   long long int mul(long long int a, long long int b) {     return ((a % mod) * (b % mod)) % mod;   }   long long int powm(long long int x, long long int n, long long int M) {     long long int result = 1;     while (n > 0) {       if (n % 2 == 1) result = (result * x) % M;       x = (x * x) % M;       n = n / 2;     }     return result;   }   long long int modinverse(long long int a, long long int m) {     return powm(a, m - 2, m);   }   bool prime(long long int x) {     if (x <= 1) return false;     for (int i = 2; i <= sqrt(x); i++)       if (x % i == 0) return false;     return true;   }   long long int divisor(long long int x) {     long long int cnt = 0;     for (int i = 1; i <= sqrt(x); i++) {       if (x % i == 0) {         if (i != x / i)           cnt += 2;         else           cnt += 1;       }     }     return cnt;   }   vector<long long int> sieve(long long int n) {     bool prim[n + 1];     memset(prim, true, sizeof(prim));     for (long long int p = 2; p * p <= n; p++) {       if (prim[p] == true) {         for (int i = p * p; i <= n; i += p) prim[i] = false;       }     }     vector<long long int> v;     for (int i = 2; i <= n; i++)       if (prim[i]) v.push_back(i);     return v;   }   vvi adj(maxn);   vb vis(maxn, false);   long long int x, y;   void dfs(long long int u) {     if (vis[u] == true) return;     vis[u] = true;     if (x == -1 and y == -1) {       x = u;       y = u;     }     x = min(x, u);     y = max(y, u);     for (auto v : adj[u]) {       if (!vis[v]) {         dfs(v);       }     }   }   int main() {     int t;     t = 1;     while (t--) {       long long int n, m;       cin >> n >> m;       vpii ans;       for (long long int i = 0; i < m; i++) {         long long int a, b;         cin >> a >> b;         adj[a].push_back(b);         adj[b].push_back(a);       }       for (long long int i = 1; i <= n; i++) {         if (!vis[i]) {           x = -1;           y = -1;           dfs(i);           ans.push_back({x, y});         }       }       sort(ans.begin(), ans.end());       long long int cnt = 0;       for (long long int i = 1; i <= (long long int)ans.size() - 1; i++) {         if (ans[i - 1].second >= ans[i].first) {           cnt++;           ans[i].second = max(ans[i - 1].second, ans[i].second);         }       }       cout << cnt <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx[8] = {-1, 1, -1, 1, -2, 2, -2, 2};   const int my[8] = {-2, -2, 2, 2, -1, -1, 1, 1};   const int MAXN = 1100;   const int MAXM = 1100;   int N, M;   const int MAXV = MAXN * MAXM;   int V;   int source[2];   int target[2];   int dist[2][MAXV];   int nxt[2][MAXV];   int st_dist[2][2];   bool parity(int v) { return ((v / M) + (v % M)) & 1; }   bool one_move(int a, int b) {     int dx = abs(a / M - b / M);     int dy = abs(a % M - b % M);     return (dx == 1 && dy == 2) || (dx == 2 && dy == 1);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> N >> M;     V = N * M;     {       int X0, Y0, X1, Y1;       cin >> X0 >> Y0 >> X1 >> Y1;       X0--, Y0--, X1--, Y1--;       source[0] = X0 * M + Y0;       source[1] = X1 * M + Y1;     }     target[0] = (N / 2 - 1) * M + (M / 2 - 1);     target[1] = (N / 2 + 1 - 1) * M + (M / 2 - 1);     for (int z = 0; z < 2; z++) {       for (int i = 0; i < V; i++) dist[z][i] = -1;       queue<int> q;       dist[z][target[z]] = 0;       q.push(target[z]);       while (!q.empty()) {         int cur = q.front();         q.pop();         int x = cur / M, y = cur % M;         for (int d = 0; d < 8; d++) {           int nx = x + mx[d], ny = y + my[d];           if (0 <= nx && nx < N && 0 <= ny && ny < M) {             int n = nx * M + ny;             if (dist[z][n] == -1) {               dist[z][n] = dist[z][cur] + 1;               nxt[z][n] = cur;               q.push(n);             }           }         }       }       for (int x = 0; x < 2; x++) {         st_dist[x][z] = dist[z][source[x]];       }     }     int adv;     if (parity(source[0]) != parity(source[1])) {       adv = 0;     } else {       adv = 1;     }     int dis_dist = st_dist[!adv][!adv];     int adv_turns = (adv == 0) ? dis_dist : dis_dist - 1;     int me, goal;     if (st_dist[adv][adv] <= adv_turns) {       me = adv;       goal = adv;     } else if (st_dist[adv][!adv] - 1 <= adv_turns) {       me = adv;       goal = !adv;     } else {       me = !adv;       goal = !adv;     }     nxt[0][target[0]] = target[0] + 2 * M + 1;     nxt[1][target[1]] = target[1] - 2 * M + 1;     cout << (me == 0 ?  WHITE  :  BLACK ) <<   n  << flush;     int cur[2] = {source[0], source[1]};     for (int t = 0; true; t++) {       if (t % 2 == me) {         auto print_pos = [&]() {           cout << (cur[me] / M + 1) <<     << (cur[me] % M + 1) <<   n  << flush;         };         if (one_move(cur[me], cur[!me])) {           cur[me] = cur[!me];           print_pos();           exit(0);         }         if (cur[me] == target[goal]) goal = me;         cur[me] = nxt[goal][cur[me]];         print_pos();         if (cur[me] == target[me]) {           exit(0);         }       } else {         int x, y;         cin >> x >> y;         if (x == -1 && y == -1) {           exit(0);         }         x--, y--;         cur[!me] = x * M + y;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5000 + 7;   int n, a[maxn], ans[maxn], b[maxn], id;   map<int, int> mp;   int t0 = 0, t1 = 0, t2 = 0;   int cnt[maxn];   void work(int id) {     for (int i = 2;; ++i) {       int t = i * i;       if (a[id] >= 0 && t > a[id]) return;       if (a[id] < 0 && t > -1 * a[id]) return;       if (a[id] % t == 0) {         while (a[id] % t == 0) {           a[id] /= t;         }       }     }   }   void init() {     scanf( %d , &n);     id = 1;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       work(i);       int t;       if (t = mp[a[i]])         b[i] = t;       else         b[i] = mp[a[i]] = id++;     }     memset(ans, 0, sizeof ans);   }   int main() {     init();     for (int i = 1; i <= n; ++i) {       for (int k = 1; k < id; ++k) cnt[k] = 0;       int sum = 0;       for (int j = i; j <= n; ++j) {         if (a[j] && !cnt[b[j]]) {           sum++;         }         cnt[b[j]]++;         ans[max(1, sum)]++;       }     }     for (int i = 1; i <= n; ++i) {       printf( %d%c , ans[i], (i == n ?   n  :    ));     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long fact[100001], inv[100001], ifact[100001];   template <typename T, typename T1>   T mod(T x, T1 p) {     x %= p;     if (x < 0) x += p;     return x;   }   template <typename T>   T inverse(T x, T p) {     x = mod(x, p);     if (x == 1) return x;     return mod((1LL * (-p / x) * (inv[p % x] % p)), p);   }   int NcR(int n, int r) {     int ret = (1LL * ifact[n - r] * ifact[r]) % 1000000007;     ret = (1LL * ret * fact[n]) % 1000000007;     return ret;   }   int main(int argc, char **argv) {     ios::sync_with_stdio(false);     fact[0] = 1;     for (int i = 1; i < 100001; i++) {       fact[i] = 1LL * fact[i - 1] * i % 1000000007;     }     ifact[0] = 1;     for (int i = 1; i < 100001; i++) {       inv[i] = inverse(i, 1000000007);       ifact[i] = 1LL * ifact[i - 1] * inv[i] % 1000000007;     }     int N;     cin >> N;     long long ans = 0;     for (int i = 1; i < N; i++) {       ans = (ans + 1LL * NcR(N - 1, i) * NcR(N, i + 1)) % 1000000007;     }     ans = (ans * 2 + N) % 1000000007;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x7fffffff;   long long n, m, k, x, s;   const int maxn = 2e5 + 10;   long long a[maxn], b[maxn], c[maxn], d[maxn];   int main(void) {     cin.sync_with_stdio(false);     cin >> n >> m >> k >> x >> s;     for (int i = (0); i < (m); i++) {       cin >> a[i];     }     for (int i = (0); i < (m); i++) {       cin >> b[i];     }     for (int i = (0); i < (k); i++) {       cin >> c[i];     }     for (int i = (0); i < (k); i++) {       cin >> d[i];     }     long long ans = n * x;     for (int i = (0); i < (m); i++) {       long long cur = 0;       long long curd = s - b[i];       if (curd < 0) continue;       int idx = upper_bound(d, d + k, curd) - d - 1;       if (idx < 0) {         cur = n * a[i];         ans = min(ans, cur);       } else {         cur = (n - c[idx]) * a[i];         ans = min(ans, cur);       }     }     int idx = upper_bound(d, d + k, s) - d - 1;     if (idx >= 0) {       long long cur = (n - c[idx]) * x;       ans = min(ans, cur);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 500;   const int INF = 1e7 + 500;   const int MOD = 1e9 + 7;   const int LOG = 18;   const double EPS = 1e-9;   const double PI = 3.1415926535;   vector<pair<int, int> > P;   long double L = INF, R = -INF, M1, M2;   long double dist(long double x0, long double y0, long double x1,                    long double y1) {     return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));   }   long double f(long double sx) {     long double s, h, r = 0;     for (auto p : P) {       s = dist(sx, 0, p.first, p.second);       h = dist(sx, 0, sx, p.second);       r = max(r, (s * s) / (2 * h));     }     return r;   }   int main() {     int N, xi, yi;     bool pl = false, mi = false;     cin >> N;     for (int i = 0; i < N; i++) {       cin >> xi >> yi;       P.push_back({xi, yi});       if (yi > 0) pl = true;       if (yi < 0) mi = true;       if (L > xi) L = xi;       if (R < xi) R = xi;     }     if (pl & mi) {       cout <<  -1  << endl;       return 0;     }     long double rad1, rad2, ans = 1e18;     for (int i = 0; i < 500; i++) {       M1 = (2 * L + R) / 3;       M2 = (L + 2 * R) / 3;       rad1 = f(M1);       rad2 = f(M2);       if (rad1 < rad2) {         R = M2;         ans = min(ans, rad1);       } else {         L = M1;         ans = min(ans, rad2);       }     }     cout << setprecision(20) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   inline void in(int &MAGNUS) { scanf( %d , &MAGNUS); }   inline void out(int MAGNUS) { printf( %d n , MAGNUS); }   inline void in(int &MAGNUS, int &CLAR) { scanf( %d%d , &MAGNUS, &CLAR); }   inline void out(int MAGNUS, int CLAR) { printf( %d %d n , MAGNUS, CLAR); }   inline void inl(long long &LIV) { scanf( %lld , &LIV); }   inline void inl(long long &LIV, long long &MART) {     scanf( %lld%lld , &LIV, &MART);   }   inline void outl(long long LIV) { printf( %lld n , LIV); }   inline void outl(long long LIV, long long MART) {     printf( %lld %lld n , LIV, MART);   }   using namespace std;   int main() {     int N, one, two, twoone = 0;     in(N);     in(one, two);     int ans = 0;     for (int i = 1; i <= N; i++) {       int x;       in(x);       if (x == 1) {         if (one > 0)           one--;         else {           if (two > 0)             two--, twoone++;           else if (twoone > 0)             twoone--;           else             ans++;         }       } else if (x == 2) {         if (two == 0)           ans += 2;         else           two--;       }     }     out(ans);     return 0;   }