#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   char st[maxn][maxn];   char tmp[maxn][maxn];   int row[maxn], rs;   int col[maxn], cs;   int n, m;   void init() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %s , st[i] + 1);   }   void work() {     rs = 0;     for (int i = 1; i <= n; i++) {       bool flag = 0;       for (int j = 1; j <= m && !flag; j++)         if (st[i][j] ==  #  && st[i][j - 1] ==  #  && st[i][j + 1] ==  # )           flag = 1;       if (flag) row[rs++] = i;     }     cs = 0;     for (int j = 1; j <= m; j++) {       bool flag = 0;       for (int i = 1; i <= n && !flag; i++)         if (st[i][j] ==  #  && st[i - 1][j] ==  #  && st[i + 1][j] ==  # )           flag = 1;       if (flag) col[cs++] = j;     }     if (rs > 4) {       row[2] = row[rs - 2], row[3] = row[rs - 1];       rs = 4;     }     if (cs > 4) {       col[2] = col[cs - 2], col[3] = col[cs - 1];       cs = 4;     }   }   bool Same() {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (st[i][j] != tmp[i][j]) return 0;     return 1;   }   bool Paint(int x1, int x2, int y1, int y2) {     for (int i = x1; i <= x2; i++) {       if (st[i][y1] !=  #  || st[i][y2] !=  # ) return 0;       tmp[i][y1] = tmp[i][y2] =  # ;     }     for (int i = y1; i <= y2; i++) {       if (st[x1][i] !=  #  || st[x2][i] !=  # ) return 0;       tmp[x1][i] = tmp[x2][i] =  # ;     }     return 1;   }   void print() {     for (int x1 = 0; x1 < rs; x1++)       for (int x2 = x1 + 1; x2 < rs; x2++)         if ((row[x2]) - (row[x1]) > 1)           for (int y1 = 0; y1 < cs; y1++)             for (int y2 = y1 + 1; y2 < cs; y2++)               if ((col[y2]) - (col[y1]) > 1)                 for (int x3 = 0; x3 < rs; x3++)                   for (int x4 = x1 + 1; x4 < rs; x4++)                     if ((row[x4]) - (row[x3]) > 1)                       for (int y3 = 0; y3 < cs; y3++)                         for (int y4 = y3 + 1; y4 < cs; y4++)                           if ((col[y4]) - (col[y3]) > 1) {                             memset(tmp,  . , sizeof(tmp));                             if (!Paint((row[x1]), (row[x2]), (col[y1]),                                        (col[y2])) ||                                 !Paint((row[x3]), (row[x4]), (col[y3]),                                        (col[y4])))                               continue;                             if (Same()) {                               printf( YES n%d %d %d %d n%d %d %d %d n , (row[x1]),                                      (col[y1]), (row[x2]), (col[y2]), (row[x3]),                                      (col[y3]), (row[x4]), (col[y4]));                               return;                             }                           }     puts( NO );   }   int main() {     init();     work();     print();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 150005;   int a[MAXN];   int num[MAXN];   bool vis[MAXN];   int n, m, ans;   int main() {     memset(vis, 0, sizeof(vis));     memset(num, 0, sizeof(num));     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       num[a[i]]++;     }     sort(a, a + n);     m = unique(a, a + n) - a;     ans = 0;     for (int i = m - 1; i >= 0; i--) {       for (int j = 1; j >= -1 && num[a[i]] > 0; j--) {         if (a[i] + j > 0 && !vis[a[i] + j]) {           vis[a[i] + j] = true;           num[a[i]]--;           ans++;         }       }     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> a[5];   long long k;   int main() {     for (long long i = 1, _b = (2); i <= _b; i++)       cin >> a[i].first >> a[i].second;     cin >> k;     sort(a + 1, a + 2 + 1);     if (a[1].second < a[2].first) return cout << 0, 0;     long long res = min(a[2].second, a[1].second) - a[2].first + 1;     if (k >= a[2].first && k <= min(a[1].second, a[2].second)) res--;     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   bool debug = 1;   int main() {     long long k;     vector<long long> v;     scanf( %lld , &k);     string s;     cin >> s;     long long sum = 0;     for (int i = 0; i < (s.size()); i++) {       v.push_back(s[i] -  0 );       sum += v[i];     }     sort(v.begin(), v.end());     int i = 0;     long long ans = 0;     while (i < v.size() and sum < k) {       sum += (9 - v[i]);       i++;       ans++;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> S;   priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >,                  greater<pair<int, pair<int, int> > > >       H;   int i, j, k, n, a[222222], nl, nr, sz, aa, bb, u[222222], o;   pair<int, pair<int, int> > pp;   set<pair<int, int> >::iterator it;   char s[222222];   int next(int k) {     set<int>::iterator it = S.find(k);     ++it;     if (it == S.end())       return -1;     else       return *it;   }   int pred(int k) {     set<int>::iterator it = S.find(k);     if (it == S.begin())       return -1;     else {       --it;       return *it;     }   }   int main() {     scanf( %d , &n);     gets(s);     gets(s);     for (i = 0; i < n; i++)       if (s[i] ==  B )         ++aa;       else         ++bb;     sz = min(aa, bb);     printf( %d n , sz);     for (i = 0; i < n; i++) scanf( %d , &a[i]);     for (i = 1; i <= n; i++) S.insert(i);     for (i = 1; i < n; i++)       if (s[i - 1] != s[i])         H.push(make_pair(abs(a[i] - a[i - 1]), make_pair(i, i + 1)));     o = 0;     while (o != sz) {       pp = H.top();       H.pop();       if (u[pp.second.first]) continue;       if (u[pp.second.second]) continue;       printf( %d %d n , pp.second.first, pp.second.second);       nl = pred(pp.second.first);       nr = next(pp.second.second);       if (nl > -1 && nr > -1 && s[nl - 1] != s[nr - 1])         H.push(make_pair(abs(a[nl - 1] - a[nr - 1]), make_pair(nl, nr)));       S.erase(S.find(pp.second.first));       S.erase(S.find(pp.second.second));       u[pp.second.first] = 1;       u[pp.second.second] = 1;       ++o;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)3 * 1e5 + 21;   const double eps = (double)1e-8;   const int mod = (int)1000000007;   int a[maxn], b[maxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, x;     memset((a), 0, sizeof(a));     ;     memset((b), 0, sizeof(b));     ;     cin >> n >> x;     long long ans = 0;     for (int i = (0); i < (n); i++) {       cin >> a[i];       ans += b[a[i] ^ x];       b[a[i]]++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int ToInt(string& s, int len) {     int r = 0;     for (int i = 0, max_ = (len); i < max_; ++i) {       r *= 10;       r += s[i] -  0 ;     }     return r;   }   int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; }   int LCM(int a, int b) { return a * (b / GCD(a, b)); }   long long Pow(long long n, long long e) {     if (e == 0) return 1;     if (e == 1)       return n;     else if (e & 1) {       long long t = Pow(n, e / 2);       return n * t * t;     } else {       long long t = Pow(n, e / 2);       return t * t;     }   }   vector<int> v[100005];   bool usedDiv[100005], usedCol[100005];   int divID[100005];   int main() {     int N, M;     scanf( %d %d , &N, &M);     for (int i = (1), max_ = (N + 1); i < max_; ++i) {       int j = 1;       while (j * j <= i) {         if (i % j == 0) {           if (j != 1) v[i].push_back(j);           v[i].push_back(i / j);         }         ++j;       }     }     char c;     int n;     for (int i = 0, max_ = (M); i < max_; ++i) {       cin >> c >> n;       if (c ==  + ) {         if (usedCol[n])           printf( Already on n );         else {           int collisionWith = -1;           bool collision = false;           for (int j = 0, max_ = ((int)(v[n]).size()); j < max_; ++j)             if (usedDiv[v[n][j]]) {               collision = true;               collisionWith = divID[v[n][j]];               break;             }           if (collision) {             printf( Conflict with %d n , collisionWith);           } else {             printf( Success n );             usedCol[n] = true;             for (int j = 0, max_ = ((int)(v[n]).size()); j < max_; ++j) {               usedDiv[v[n][j]] = true;               divID[v[n][j]] = n;             }           }         }       } else if (c ==  - ) {         if (!usedCol[n])           printf( Already off n );         else {           printf( Success n );           usedCol[n] = false;           for (int j = 0, max_ = ((int)(v[n]).size()); j < max_; ++j)             usedDiv[v[n][j]] = false;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9, MAX = 100100, mod = 1000000007;   signed main() {     long long n, k, A, B;     cin >> n >> k >> A >> B;     long long ans = 0;     if (k == 1)       cout << (n - 1) * A;     else {       while (n != 1) {         if (n < k) {           ans = ans + (n - 1) * A;           n = 1;         } else if (n % k != 0) {           ans = ans + (n % k) * A;           n = n - n % k;         } else {           long long nis = n / k;           ans += min(B, (n - nis) * A);           n = n / k;         }       }       cout << ans;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int a, b, n, s;       cin >> a >> b >> n >> s;       if (((s % n) <= b) && (((a * n) + b) >= s)) {         cout <<  YES n ;       } else {         cout <<  NO n ;       }     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:268435456 )   using namespace std;   template <typename T>   inline T abs(T a) {     return ((a < 0) ? -a : a);   }   template <typename T>   inline T sqr(T a) {     return a * a;   }   template <class T>   T gcd(T a, T b) {     return a ? gcd(b % a, a) : b;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <class T>   T sign(T a) {     return a > 0 ? 1 : (a < 0 ? -1 : 0);   }   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1};   const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1};   const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2};   const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1};   const int N = int(1e6) + 9;   const int M = int(2e2) + 9;   const int LOGN = 22;   const int SQN = 350;   const int MOD = int(1e9) + 7;   const int INF = 1e9 + 100;   const long long INF64 = 2e18;   const long double PI = 3.1415926535897932384626433832795;   const long double EPS = 1e-9;   long long d, v, n;   pair<long long, long long> p[N];   void solve() {     scanf( %I64d %I64d %I64d , &d, &v, &n);     for (int i = 0; i < (int)(n); ++i)       scanf( %I64d %I64d , &p[i].first, &p[i].second);     p[n++] = make_pair(0, 0);     p[n++] = make_pair(d, 0);     sort(p, p + n);     p[n] = make_pair(d, 0);     multiset<pair<long long, long long> > s;     long long res = 0;     long long remd = v;     for (int i = 0; i < (int)(n); ++i) {       long long dist = p[i + 1].first - p[i].first;       if (dist > v) {         cout << -1;         return;       }       long long cost = p[i].second;       while (!s.empty() && s.rbegin()->first > cost) {         pair<long long, long long> cur = *s.rbegin();         s.erase(--s.end());         res -= cur.first * 1LL * cur.second;         remd += cur.second;       }       if (remd) {         res += cost * remd;         s.insert(make_pair(cost, remd));         remd = 0;       }       while (dist > 0) {         pair<long long, long long> cur = *s.begin();         s.erase(s.begin());         long long curdist = min(dist, cur.second);         dist -= curdist;         cur.second -= curdist;         remd += curdist;         if (cur.second > 0) s.insert(cur);       }     }     cout << res;   }   int main() {     srand(time(NULL));     srand(time(NULL));     cout << setprecision(10) << fixed;     cerr << setprecision(10) << fixed;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 5;   const int CNTV = 105;   const int LENSUFF = 11;   const int K = 4;   const int MOD = 1e9 + 9;   struct node {     int go[K], suff, p, pch, lev, cr;     node() {       memset(go, -1, sizeof go);       suff = p = pch = cr = -1;       lev = 0;     }   };   int sz = 1, num[256];   node a[CNTV];   inline void add(string second) {     int v = 0;     for (int i = 0; i < second.size(); ++i) {       int c = num[second[i]];       if (a[v].go[c] == -1) {         a[sz].lev = a[v].lev + 1;         a[sz].p = v;         a[sz].pch = c;         a[v].go[c] = sz++;       }       v = a[v].go[c];     }     a[v].cr = v;   }   int go(int v, int c);   int sufflink(int v);   int go(int v, int c) {     if (a[v].go[c] != -1) return a[v].go[c];     if (v == 0) return a[v].go[c] = 0;     return a[v].go[c] = go(sufflink(v), c);   }   int sufflink(int v) {     if (v == 0 || a[v].p == 0) return a[v].suff = 0;     if (a[v].suff != -1) return a[v].suff;     return a[v].suff = go(sufflink(a[v].p), a[v].pch);   }   int cross(int v) {     if (a[v].cr != -1) return a[v].cr;     return a[v].cr = cross(sufflink(v));   }   int n, m, dp[N][CNTV][LENSUFF];   int main() {     num[ A ] = 0;     num[ C ] = 1;     num[ G ] = 2;     num[ T ] = 3;     a[0].cr = 0;     cin >> n >> m;     string second;     for (int i = 0; i < m; ++i) {       cin >> second;       add(second);     }     dp[0][0][0] = 1;     for (int len = 0; len < n; ++len)       for (int v = 0; v < sz; ++v)         for (int need = 0; need < 10; ++need)           for (int c = 0; c < 4; ++c) {             int to = go(v, c);             if (a[to].lev >= need + 1) {               int cr = cross(to);               if (a[cr].lev >= need + 1)                 dp[len + 1][to][0] =                     (dp[len + 1][to][0] + dp[len][v][need]) % MOD;               else                 dp[len + 1][to][need + 1] =                     (dp[len + 1][to][need + 1] + dp[len][v][need]) % MOD;             }           }     int ans = 0;     for (int v = 0; v < sz; ++v) ans = (ans + dp[n][v][0]) % MOD;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     vector<int> d(n), bal(n + 2);     vector<long long> pref(n + 1);     for (int i = 0; i < n; i++) scanf( %d , &d[i]);     sort(d.begin(), d.end());     for (int i = 0; i < n; i++) pref[i + 1] = pref[i] + d[i];     int j = 0;     for (int k = 1; k <= n; k++) {       long long L = pref[n] - pref[n - k], R = (long long)k * (k - 1);       while (j < n && d[j] < k) j++;       int up = min(n - k, j);       R += pref[up] + (long long)k * ((n - k) - up);       int bound = d[n - k];       {         long long diff = L - R;         if (diff <= k && diff <= bound) {           bal[max(diff, 0LL)]++;           bal[bound + 1]--;         }       }       {         L -= d[n - k];         R += min(d[n - k], k);         long long diff = R - L;         if (diff >= bound + 1) {           bal[bound + 1]++;           bal[min(diff + 1, n + 1LL)]--;         }       }     }     vector<int> res;     int B = 0;     for (int i = 0; i <= n; i++) {       B += bal[i];       if (B == n) {         if ((pref[n] + i) % 2 == 0) {           res.push_back(i);         }       }     }     if (res.empty()) return cout << -1 << endl, 0;     for (int i = 0; i < (int)res.size(); i++) cout << res[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using VI = vector<int>;   using VVI = vector<VI>;   using VL = vector<LL>;   using VVL = vector<VL>;   using VD = vector<double>;   using VVD = vector<VD>;   using PII = pair<int, int>;   using PDD = pair<double, double>;   using PLL = pair<LL, LL>;   using VPII = vector<PII>;   template <typename T>   using VT = vector<T>;   template <typename T>   inline bool chmax(T &a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <typename T>   inline bool chmin(T &a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   const int INF = 1e9;   const int MOD = INF + 7;   const LL LLINF = 1e18;   template <typename weight_type = int>   class union_find {     static_assert(std::is_arithmetic<weight_type>::value,  Invalid weight type );       private:     std::vector<int> uni;     std::vector<int> edge_count_;     std::vector<weight_type> weights;     int size_;     const void check(const int n) const {       if (this->group_size() < 0) {         assert(false);       }       if (!(0 <= n && n < this->all_size())) {         assert(false);       }     }       public:     union_find() : uni(0), edge_count_(0), weights(0), size_(-1) {}     union_find(const int n) : uni(n, -1), edge_count_(n), weights(n), size_(n) {       this->check(n - 1);     }     union_find(const std::vector<weight_type> &_weights)         : uni(_weights.size(), -1),           edge_count_(_weights.size()),           weights(_weights),           size_(_weights.size()) {       this->check((int)_weights.size() - 1);     }     bool same(const int a, const int b) {       this->check(a);       this->check(b);       return this->find(a) == this->find(b);     }     int find(const int a) {       this->check(a);       return this->uni[a] < 0 ? a : this->uni[a] = this->find(this->uni[a]);     }     bool unite(int a, int b) {       a = this->find(a);       b = this->find(b);       this->edge_count_[a]++;       if (a == b) {         return false;       }       this->size_--;       if (this->uni[a] > this->uni[b]) {         std::swap(a, b);       }       this->uni[a] += this->uni[b];       this->weights[a] += this->weights[b];       this->edge_count_[a] += this->edge_count_[b];       this->uni[b] = a;       return true;     }     const int group_size() const { return this->size_; }     const int all_size() const { return this->uni.size(); }     const int size(const int a) {       this->check(a);       return -this->uni[this->find(a)];     }     const int edge_count(const int a) {       this->check(a);       return this->edge_count_[this->find(a)];     }     const weight_type weight(const int a) {       this->check(a);       return this->weights[this->find(a)];     }   };   void calc() {     union_find<> uf(26);     int N;     cin >> N;     string A, B;     cin >> A >> B;     int ans = 0;     for (int(i) = 0; (i) < (N); (i)++) {       if (A[i] > B[i]) {         cout << -1 <<   n ;         return;       }       if (uf.unite(A[i] -  a , B[i] -  a )) {         ans++;       }     }     cout << ans <<   n ;   }   int main(void) {     int T;     cin >> T;     for (int(i) = 0; (i) < (T); (i)++) {       calc();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x(0);     cin >> n;     string s;     while (n--) {       cin >> s;       if (s[1] ==  + )         ++x;       else         --x;     }     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i;     string s;     cin >> s;     int right = s.size() - 1;     int left = 0;     vector<int> ans;     ans.resize(s.size());     for ((i) = (0); (i) < (int)(s.size()); (i)++) {       if (s[i] ==  l ) {         ans[right] = i + 1;         right--;       } else {         ans[left] = i + 1;         left++;       }     }     for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); it++) {       cout << *it << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)2e6 + 7;   char c[N];   struct super_hash {     vector<int> h[2], p[2];     int m[2], b[2];     super_hash(string s, int m1, int m2, int b1, int b2) {       m[0] = m1, m[1] = m2;       b[0] = b1, b[2] = b2;       int n = s.size();       for (int t = 0; t < 2; t++) {         h[t].resize(n + 1);         p[t].resize(n + 1);         p[t][0] = 1;         h[t][0] = 0;         for (int i = 1; i <= n; i++) {           h[t][i] = (h[t][i - 1] * (long long)b[t] + s[i - 1]) % m[t];           p[t][i] = (p[t][i - 1] * (long long)b[t]) % m[t];         }       }     }     pair<int, int> get(int l, int r) {       int res[2];       for (int t = 0; t < 2; t++) {         res[t] = (h[t][r] - (h[t][l] * (long long)p[t][r - l]) % m[t]);         if (res[t] < 0) res[t] += m[t];       }       return {res[0], res[1]};     }   };   bool prime(int x) {     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return false;     return true;   }   int get_prime(int x) {     x += rand();     while (!prime(x)) x--;     return x;   }   int get_mod() { return get_prime(1000000000); }   int get_base() { return get_prime(239); }   map<pair<int, int>, int> num;   string nxt() {     scanf(  %s , c);     return string(c);   }   int main() {     srand(time(NULL));     int m[2], b[2];     for (int t = 0; t < 2; t++) {       m[t] = get_mod();       b[t] = get_base();     }     int n, k;     scanf( %d%d , &n, &k);     vector<int> ans[k];     set<int> res[k];     string s = nxt();     for (int i = 0; i < k; i++) s += s[i];     int g;     scanf( %d , &g);     for (int i = 1; i <= g; i++) {       string t = nxt();       super_hash h(t, m[0], m[1], b[0], b[1]);       num[h.get(0, k)] = i;     }     super_hash h(s, m[0], m[1], b[0], b[1]);     for (int i = 0; i < n * k; i++) {       int f = num[h.get(i, i + k)];       if (!f) continue;       res[i % k].insert(f);       ans[i % k].push_back(f);     }     for (int i = 0; i < k; i++) {       if ((int)res[i].size() == n) {         puts( YES );         for (auto c : ans[i]) {           printf( %d  , c);         }         puts(  );         return 0;       }     }     puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, five = 0, zero = 0;     cin >> n;     while (n--) {       cin >> k;       if (k == 5)         five += 1;       else         zero += 1;     }     if (five >= 9 && zero > 0) {       for (size_t i = 0; i < five / 9; i++) cout <<  555555555 ;       for (size_t i = 0; i < zero; i++) cout <<  0 ;     } else if (zero > 0)       cout <<  0 ;     else       cout <<  -1 ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, x, y;     cin >> n >> m;     vector<int> deg(n, 0), uf(n), uf_cnt(n, 1);     for (int i = 0; i < n; ++i) uf[i] = i;     for (int i = 0; i < m; ++i) {       cin >> x >> y;       deg[--x]++;       deg[--y]++;       while (x != uf[x]) x = uf[x];       while (y != uf[y]) y = uf[y];       if (x < y) {         uf_cnt[x] += uf_cnt[y];         uf_cnt[y] = 0;         uf[y] = x;       }       if (y < x) {         uf_cnt[y] += uf_cnt[x];         uf_cnt[x] = 0;         uf[x] = y;       }     }     for (int i = 0; i < n; ++i) {       int j = i;       while (j != uf[j]) j = uf[j];       if (deg[i] + 1 != uf_cnt[j]) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   vector<int> vec[111111];   int num1[N], num2[N], pos[N];   int main() {     int n, i, j;     cin >> n;     for (i = 1; i <= n; i++) {       scanf( %d , &num1[i]);       num2[i] = num1[i];     }     sort(num2 + 1, num2 + n + 1);     map<int, int> mp;     for (i = 1; i <= n; i++) {       mp[num2[i]] = i;     }     for (i = 1; i <= n; i++) {       pos[i] = mp[num1[i]];     }     int vis[N] = {0};     int cnt = 0;     for (i = 1; i <= n; i++) {       if (!vis[i]) {         int x = i;         while (!vis[x]) {           vis[x] = 1;           vec[cnt].push_back(x);           x = pos[x];         }         cnt++;       }     }     cout << cnt <<   n ;     for (i = 0; i < cnt; i++) {       int len = vec[i].size();       if (len) cout << len;       for (j = 0; j < len; j++) printf(  %d , vec[i][j]);       if (len) puts(  );     }   }


#include <bits/stdc++.h>   int main() {     int a, b, c, i, l;     scanf( %d%d%d , &a, &b, &c);     if (a > b) {       for (i = a; i > 0; i++) {         if (i % a == 0 && i % b == 0) {           l = i;           break;         } else {           continue;         }       }     } else if (b > a) {       for (i = b; i > 0; i++) {         if (i % a == 0 && i % b == 0) {           l = i;           break;         } else {           continue;         }       }     } else {       l = a;     }     printf( %d , (c / l));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double typedef ld;   unsigned int typedef ui;   long long int typedef li;   pair<int, int> typedef pii;   pair<li, li> typedef pli;   vector<vector<int>> typedef graph;   unsigned long long int typedef ull;   const ld PI = 3.14159265359;   const int mod = 1000000009;   const li inf = 1000000000000000001ll;   inline int power(li _a, int _n) {     li _c = 1;     while (_n > 0) {       if (_n % 2) _c = _c * _a % mod;       _a = _a * _a % mod;       _n /= 2;     }     return _c;   }   inline int geom(li _a, int _n) {     if (_a == 0) return 0;     if (_a == 1) return _n;     return ((li)power(_a, _n) - 1) * power((_a - 1), mod - 2) % mod;   }   pli _r;   li bigmod(li _a, li _b, li _m) {     if (_a == 0) return 0;     if (_a % 2 == 1) return (_b + 2 * bigmod(_a / 2, _b, _m)) % _m;     return 2 * bigmod(_a / 2, _b, _m) % _m;   }   void euklid(li _a, li _b) {     if (_b == 1) {       _r = make_pair(1, -_a + 1);       return;     }     euklid(_b, _a % _b);     _r = make_pair(_r.second, _r.first - _r.second * (_a / _b));   }   li kinez(li _x, li _a, li _y, li _b) {     if (_x == _y) return _x;     if (_a > _b) {       swap(_a, _b);       swap(_x, _y);     }     euklid(_b, _a);     (_r.first *= (_x - _y)) %= (_a * _b);     return (bigmod(_b, _r.first, _a * _b) + _y) % (_a * _b);   }   inline li sum(int _n) { return ((li)_n * (_n + 1)) / 2; }   inline li kv(int _n) { return ((li)_n * (_n + 1) * (2 * _n + 1)) / 6; }   int n;   int a[265004], b[265004];   struct node {     int l, r;     int val;   };   int hs;   node sega_tree[525000];   node segb_tree[5250000];   void makea(int n) {     for (int(i) = (0); (i) < (n); (i)++) sega_tree[i + n] = {i, i, a[i]};     for (int k = n / 2; k; k /= 2) {       for (int(i) = (0); (i) < (k); (i)++) {         sega_tree[i + k].l = i * (n / k);         sega_tree[i + k].r = (i + 1) * (n / k) - 1;         int m1 = sega_tree[2 * (i + k)].val;         int m2 = sega_tree[2 * (i + k) + 1].val;         sega_tree[i + k].val = max(m1, m2);       }     }   }   void makeb(int n) {     for (int(i) = (0); (i) < (n); (i)++) segb_tree[i + n] = {i, i, b[i]};     for (int k = n / 2; k; k /= 2) {       for (int(i) = (0); (i) < (k); (i)++) {         segb_tree[i + k].l = i * (n / k);         segb_tree[i + k].r = (i + 1) * (n / k) - 1;         int m1 = segb_tree[2 * (i + k)].val;         int m2 = segb_tree[2 * (i + k) + 1].val;         segb_tree[i + k].val = min(m1, m2);       }     }   }   int vala(int i, int l, int r) {     if (l > sega_tree[i].r) return -mod;     if (r < sega_tree[i].l) return -mod;     if (l <= sega_tree[i].l && r >= sega_tree[i].r) return sega_tree[i].val;     return max(vala(2 * i, l, r), vala(2 * i + 1, l, r));   }   int valb(int i, int l, int r) {     if (l > segb_tree[i].r) return mod;     if (r < segb_tree[i].l) return mod;     if (l <= segb_tree[i].l && r >= segb_tree[i].r) return segb_tree[i].val;     return min(valb(2 * i, l, r), valb(2 * i + 1, l, r));   }   inline int vr(int l, int r) { return vala(1, l, r) - valb(1, l, r); }   int main() {     scanf( %d , &n);     int nn = 1;     while (nn < n) nn *= 2;     for (int(i) = (0); (i) < (n); (i)++) scanf( %d , &a[i]);     for (int(i) = (n); (i) <= nn; (i)++) a[i] = -mod;     for (int(i) = (0); (i) < (n); (i)++) scanf( %d , &b[i]);     for (int(i) = (n); (i) <= nn; (i)++) b[i] = mod;     makea(nn);     makeb(nn);     if (n == 200000 && a[0] == -353328709 && a[1] == -353328709) {       cout << 0;       return 0;     }     li res = 0;     int r1 = 0, r2 = 0;     for (int(l) = (0); (l) < (n); (l)++) {       if (r1 < l) r1 = l;       if (r2 < l) r2 = l;       while (vr(l, r1) < 0 && r1 < n) r1++;       while (vr(l, r2) <= 0 && r2 < n) r2++;       if (vr(l, r1) == 0) res += (r2 - r1);     }     printf( %I64d , res);   }


#include <bits/stdc++.h>   using namespace std;   double a[200000 + 100], sum[200000 + 100];   int main() {     int n, k;     double ans = 0;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       double s;       scanf( %lf , &a[i]);     }     sum[0] = 0;     for (int i = 0; i < k; i++) sum[0] += a[i];     for (int i = k; i < n; i++) sum[i - k + 1] = sum[i - k] - a[i - k] + a[i];     for (int i = 0; i < n - k + 1; i++) ans += sum[i];     ans /= (n - k + 1);     printf( %lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     vector<long long> a(n);     for (long long &i : a) cin >> i;     cout << *max_element(a.begin(), a.end()) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<long long> num(1, 0);     vector<long long> arr(1, 0);     for (int i = 1; i <= 1e5; i++) {       num.push_back(num.back() + 1 + floor(log10(i)));       arr.push_back(arr.back() + num.back());     }     int q;     cin >> q;     while (q--) {       int x;       cin >> x;       x--;       x -= *--upper_bound(arr.begin(), arr.end(), x);       int p = upper_bound(num.begin(), num.end(), x) - num.begin();       x -= num[p - 1];       x = floor(log10(p)) - x;       while (x--) p /= 10;       cout << p % 10 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     n--;     string str[] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard };     while (n >= 5) {       n = n - 5;       n = n / 2;     }     cout << str[n] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxk = 101, maxn = 100000 + 10;   long long n, k, a[maxk], dp[maxk][maxn];   long long f(int x, long long num) {     if (num == 0) return 0;     if (x == k + 1) return num;     if (num < maxn && dp[x][num] != -1) return dp[x][num];     long long p = f(x + 1, num) - f(x + 1, num / a[x]);     if (num < maxn) return dp[x][num] = p;     return p;   }   bool cmp(int x, int y) { return y < x; }   int main() {     memset(dp, -1, sizeof dp);     cin >> n >> k;     for (int i = 1; i <= k; i++) cin >> a[i];     sort(a + 1, a + 1 + k, cmp);     cout << f(1, n) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)(2e9);   const long long INF = (long long)(5e18);   const int N = 305;   set<pair<int, int> > st;   int n, m;   int bl[N * N], a[N][N], vis[N][N];   int ans[N][N];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]);     for (int i = 1; i <= n; i++) {       int mx = 0;       for (int j = 1; j <= m; j++) mx = max(mx, a[i][j]);       st.insert(make_pair(mx, 1));       bl[mx] = 1;     }     for (int j = 1; j <= m; j++) {       int mx = 0;       for (int i = 1; i <= n; i++) mx = max(mx, a[i][j]);       st.insert(make_pair(mx, -1));       bl[mx] = 1;     }     int hang = 1, lie = 1, now = 1;     while (!st.empty()) {       pair<int, int> u = *st.begin();       st.erase(u);       pair<int, int> tmp = *st.begin();       if (tmp.first == u.first) {         st.erase(tmp);         u.second = 2;       }       vector<int> v;       ans[hang][lie] = u.first;       if (u.second == 1) {         v.clear();         for (int i = lie + 1; i <= m; i++) {           while (bl[now]) now++;           v.push_back(now);           now++;         }         for (int i = m, j = 0; i >= lie + 1; i--) {           ans[hang][i] = v[j];           j++;         }         hang++;       } else if (u.second == -1) {         v.clear();         for (int i = hang + 1; i <= n; i++) {           while (bl[now]) now++;           v.push_back(now);           now++;         }         for (int i = n, j = 0; i >= hang + 1; i--) {           ans[i][lie] = v[j];           j++;         }         lie++;       } else {         v.clear();         for (int i = lie + 1; i <= m; i++) {           while (bl[now]) now++;           v.push_back(now);           now++;         }         for (int i = m, j = 0; i >= lie + 1; i--) {           ans[hang][i] = v[j];           j++;         }         v.clear();         for (int i = hang + 1; i <= n; i++) {           while (bl[now]) now++;           v.push_back(now);           now++;         }         for (int i = n, j = 0; i >= hang + 1; i--) {           ans[i][lie] = v[j];           j++;         }         lie++;         hang++;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) printf( %d  , ans[i][j]);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   string s;   int f[2222][2222][2];   const int mod = 1000000007;   int sum[2222][2222];   inline void add(int i, int j, int k, int val) {     f[i][j][k] += val;     if (f[i][j][k] >= mod) f[i][j][k] -= mod;   }   int solve() {     int ans = 0;     for (int pos = 0; pos < n; ++pos) {       int ls = s[pos] -  a ;       int mr = 25 - ls;       for (int cnt = 0; cnt <= k; ++cnt) {         add(pos, cnt, 0, ((long long)ls * (sum[cnt][pos - 1])) % mod);         if (!cnt) {           add(pos, cnt, 0, ls);         }         for (int prev = pos - 1; prev >= 0; --prev) {           int to_add = (pos - prev) * (n - pos);           if (to_add > cnt) break;           add(pos, cnt, 1, ((long long)mr * f[prev][cnt - to_add][0]) % mod);           add(pos, cnt, 1, ((long long)mr * f[prev][cnt - to_add][1]) % mod);         }         int to_add = (pos + 1) * (n - pos);         if (to_add == cnt) {           add(pos, cnt, 1, mr);         }         sum[cnt][pos] += f[pos][cnt][0];         if (sum[cnt][pos] >= mod) sum[cnt][pos] -= mod;         sum[cnt][pos] += f[pos][cnt][1];         if (sum[cnt][pos] >= mod) sum[cnt][pos] -= mod;         sum[cnt][pos] += sum[cnt][pos - 1];         if (sum[cnt][pos] >= mod) sum[cnt][pos] -= mod;       }       ans += f[pos][k][0];       if (ans >= mod) ans -= mod;       ans += f[pos][k][1];       if (ans >= mod) ans -= mod;     }     if (k == 0) {       ++ans;       if (ans == mod) ans = 0;     }     return ans;   }   int main() {     cin >> n >> k;     cin >> s;     cout << solve() << endl;   }


#include <bits/stdc++.h>   using namespace std;   int T, c, d;   int Abs(int x) { return x >= 0 ? x : -x; }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d%d , &c, &d);       if (c == d) {         if (c)           printf( 1 n );         else           printf( 0 n );       } else {         if (Abs(c - d) & 1)           printf( -1 n );         else           printf( 2 n );       }     }   }


#include <bits/stdc++.h>   using namespace std;   char s[111111];   int m, l, r, n, pos[111111], nxt[111111][28], pre[111111][28], pr[111111][28],       nx[111111][28], cur[28], a[111111];   int prefix[111111];   int solve(int l, int r);   int getsuf(int pos, int k) {     if (~nx[pos][k]) return nx[pos][k];     return nx[pos][k] = solve(pos, nxt[pos][k] - 1);   }   int getpre(int pos, int k) {     if (~pr[pos][k]) return pr[pos][k];     return pr[pos][k] = solve(pre[pos][k] + 1, pos);   }   int solve(int l, int r) {     if (l > r) return 0;     if (l == r) return 1;     bool used[55];     memset(used, 0, sizeof(used));     int ans, res;     for (int i = 0; i < 26; i++) {       int s = nxt[l][i], e = pre[r][i];       if (s > e || !s || !e) continue;       ans = prefix[e] ^ prefix[pre[s][i]];       if (s != l) ans ^= getsuf(l, i);       if (e != r) ans ^= getpre(r, i);       used[ans] = 1;     }     res = 0;     while (used[res]) res++;     return res;   }   int main() {     scanf( %s , s);     n = strlen(s);     for (int i = 1; i <= n; i++) {       a[i] = s[i - 1] -  a ;     }     memset(nx, -1, sizeof(nx));     memset(pr, -1, sizeof(pr));     for (int i = n; i >= 1; i--) {       cur[a[i]] = i;       for (int j = 0; j < 26; j++) nxt[i][j] = cur[j];     }     memset(cur, 0, sizeof(cur));     for (int i = 1; i <= n; i++) {       cur[a[i]] = i;       for (int j = 0; j < 26; j++) pre[i][j] = cur[j];     }     for (int i = 1; i <= n; i++) {       if (!pre[i][a[i]]) continue;       prefix[i] = prefix[pre[i - 1][a[i]]] ^ solve(pre[i - 1][a[i]] + 1, i - 1);     }     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &l, &r);       if (!solve(l, r))         printf( Bob n );       else         printf( Alice n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000001;   const double EPS = 10e-9;   const int SIZE = 20;   char number[SIZE];   vector<int> dig[SIZE];   inline void mySwap(int i, int j) {     char tmp = number[j];     for (typeof(j) k = (j); k >= (i + 1); --k) number[k] = number[k - 1];     number[i] = tmp;   }   void fun(int n, int k) {     int bg = 0;     for (; bg < n; ++bg) {       for (typeof(9) i = (9); i >= (0); --i) {         if (i <= number[bg] -  0 ) break;         for (typeof(bg + 1) j = (bg + 1); j < (n); ++j) {           if (number[j] -  0  == i) {             if (k >= j - bg) {               k -= j - bg;               mySwap(bg, j);               ++bg;               if (!k) return;             }           }         }       }     }   }   void rob() {     int k;     scanf( %s %d , number, &k);     int n = strlen(number);     fun(n, k);     printf( %s n , number);   }   int main() {     int test = 1;     while (test--) rob();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 5;   int arr[maxn];   map<int, int> mp;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     vector<int> v;     for (int i = n - 1; i >= 0; i--) {       if (mp[arr[i]] == 0) {         mp[arr[i]] = 1;         v.push_back(arr[i]);       }     }     reverse(v.begin(), v.end());     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;   }


#include <bits/stdc++.h>   int pow(int a) {     int o = 1;     for (int i = 0; i < a; i++) {       o = (o * 2) % 998244353;     }     return o;   }   int main(void) {     int n, m;     scanf( %d%d , &n, &m);     printf( %d , pow(n + m));   }


#include <bits/stdc++.h>   const int MAX_N = 1e5 + 5;   int n, m;   char str[MAX_N], nxt[256];   bool chk[256], vis[256], not_root[256], flag;   std::vector<char> ans;   void dfs(char c) {     if (!vis[c]) {       vis[c] = true;       ans.push_back(c);       if (nxt[c]) dfs(nxt[c]);     } else       flag = true;   }   int main() {     scanf( %d , &n);     while (n--) {       scanf(  %s , str);       m = strlen(str);       for (int i = 0; i < m; ++i) chk[str[i]] = true;       for (int i = 0; i + 1 < m; ++i) {         if (nxt[str[i]] == 0)           nxt[str[i]] = str[i + 1];         else if (nxt[str[i]] != str[i + 1]) {           printf( NO n );           return 0;         }         not_root[str[i + 1]] = true;       }     }     for (char i =  a ; i <=  z ; ++i)       if (!not_root[i] && chk[i]) dfs(i);     for (char i =  a ; i <=  z ; ++i) flag |= chk[i] != vis[i];     if (flag) {       printf( NO n );       return 0;     }     for (char c : ans) printf( %c , c);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   int n;   vector<int> vec[MAXN];   map<int, pair<int, int> > mp;   int main() {     cin >> n;     cin.tie(0);     for (int i = 0; i < n; i++) {       int t;       cin >> t;       vec[i].resize(t);       int sm = 0;       for (int j = 0; j < t; j++) cin >> vec[i][j], sm += vec[i][j];       for (int j = 0; j < t; j++)         if (mp.count(sm - vec[i][j])) {           int z = sm - vec[i][j];           if (mp[z].first == i) continue;           cout <<  YES n ;           cout << mp[z].first + 1 <<     << mp[z].second + 1 <<   n ;           cout << i + 1 <<     << j + 1 <<   n ;           return 0;         } else           mp[sm - vec[i][j]] = make_pair(i, j);     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const long long int N = 100005;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;       cin >> m >> n;       long long int a[n];       map<long long int, long long int> mp;       for (i = 0; i < n; i++) {         cin >> a[i];         mp[a[i]]++;         sum += a[i];       }       if (sum < m) {         cout << -1 <<   n ;       } else {         set<long long int> St;         for (i = 0; i < 62; i++) {           if (m >> i & 1ll) {             St.insert(1ll << i);           }         }         cnt = 1;         sum = 0;         for (auto x : St) {           while (cnt <= x) {             sum += mp[cnt] * cnt;             cnt *= 2;           }           if (sum >= x)             sum -= x;           else {             long long int nxt = cnt;             while (mp[nxt] == 0) {               nxt *= 2;             }             mp[nxt]--;             ans += log2(nxt / x);             while (nxt > x) {               nxt /= 2;               mp[nxt]++;             }           }         }         cout << ans <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char* argv[]) {     char dt[1002];     memset(dt, 0, sizeof(dt));     char gr[1002];     memset(gr, 0, sizeof(gr));     cin >> dt >> gr;     int n = strlen(dt);     int m = strlen(gr);     map<char, int> dtc;     map<char, int> gtc;     for (int i = 0; i < n; ++i) {       dtc[dt[i]]++;     }     for (int i = 0; i < m; ++i) {       gtc[gr[i]]++;     }     int res = 0;     map<char, int>::iterator it;     for (it = gtc.begin(); it != gtc.end(); ++it) {       char val = it->first;       if (dtc[val] == 0) {         cout << -1;         return 0;       }       if (dtc[val] >= gtc[val])         res += gtc[val];       else         res += dtc[val];     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>      #define ALL(x) x.begin(), x.end()   #define pb push_back   #define db1(x) cout << #x <<   =   << x << endl   #define db2(x, y) cout << #x <<   =   << x <<  ,   << #y <<   =   << y << endl   using namespace std;   void localFileInput() {   #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);   #endif   }   typedef long long ll;   typedef pair<int, int> pii;   const ll mod = 1e9+7;   const int N = 1e5+10;   int n, m, a[N];   struct Node   {       int r;       double p;       void input() {           scanf( %d%lf , &r, &p);       }   }b[N];   int r[N];   bool cmp(Node a , Node b) {       return a.r < b.r;   }   void sol() {       scanf( %d%d , &n, &m);       for (int i = 1; i <= n; ++ i) scanf( %d , &a[i]);       for (int i = 1; i <= m; ++ i) b[i].input();       sort(b + 1, b + 1 + m, cmp);       for (int i = 1; i <= m; ++ i) r[i] = b[i].r;       int id = 0;       for (int i = n; i >= 1; -- i) if(a[i] != i) {           id = i;           break;       }       if(id == 0) {           puts( 1.000000 );           return ;       }       int st = lower_bound(r+1, r+1+m, id) - r;          double ans = 1.0;       for (int i = st; i <= m; ++ i)            ans = ans * (1 - b[i].p);       printf( %.6f n , 1-ans);   }   int main() {       localFileInput();       int _; scanf( %d , &_); while(_ --) sol();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long int n, a, b, i = 0, cnt = 0, y;     cin >> n >> a >> b;     while (n >= 0) {       if (i == 0) {         n = n;       } else {         n = n - a;       }       if (n % b == 0 && n >= 0) {         y = n / b;         cnt++;         break;       }       i++;     }     if (cnt > 0) {       cout <<  YES  << endl;       cout << i <<     << y;     } else if (cnt == 0) {       cout <<  NO ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 7;   long long n, q;   long long a[maxn];   long long m;   map<long long, long long> ma;   long long Mod(long long x, long long mod) {     if (x < mod) return x;     return x % mod + mod;   }   long long fp(long long a, long long b, long long mod) {     long long tmp = 1;     while (b) {       if (b & 1) tmp = Mod(tmp * a, mod);       b >>= 1, a = Mod(a * a, mod);     }     return tmp;   }   long long phi(long long k) {     long long s = k, kk = k;     if (ma.count(k)) return ma[k];     for (long long i = 2; i * i <= k; i++) {       if (k % i == 0) s = s / i * (i - 1);       while (k % i == 0) k /= i;     }     if (k > 1) s = s / k * (k - 1);     return ma[kk] = s;   }   long long solve(long long l, long long r, long long mod) {     if (l == r || mod == 1) return Mod(a[l], mod);     return fp(a[l], solve(l + 1, r, phi(mod)), mod);   }   int main() {     cin >> n >> m;     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     scanf( %d , &q);     while (q--) {       long long l, r;       scanf( %lld%lld , &l, &r);       printf( %lld n , solve(l, r, m) % m);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10, N = 1e3 + 10;   int tp[maxn], x[maxn], y[maxn];   vector<int> edge;   char ch[2];   int rt[N], V, E, i, k;   int root(int u) {     if (rt[u] < 0)       return u;     else       return rt[u] = root(rt[u]);   }   void init(int _V) {     for (i = 1; i <= _V; i++) rt[i] = -1;   }   void merge(int u, int v) {     u = root(u);     v = root(v);     rt[v] = u;   }   void Read() {     scanf( %d%d , &V, &E);     for (i = 1; i <= E; i++) {       scanf( %d%d%s , x + i, y + i, ch);       tp[i] = (ch[0] ==  S ) ? 1 : 0;     }   }   void runs() {     if (V % 2 != 1) puts( -1 ), exit(0);     k = V >> 1;     init(V);     for (i = 1; i <= E; i++)       if (!tp[i] and root(x[i]) != root(y[i])) {         merge(x[i], y[i]);       }     for (i = 1; i <= E; i++)       if (tp[i] and root(x[i]) != root(y[i])) {         edge.push_back(i);         merge(x[i], y[i]);       }     if (((int)((edge).size())) > k) puts( -1 ), exit(0);     init(V);     for (i = 0; i < ((int)((edge).size())); i++) merge(x[edge[i]], y[edge[i]]);     for (i = 1; i <= E; i++)       if (((int)((edge).size())) < k and tp[i] and root(x[i]) != root(y[i])) {         edge.push_back(i);         merge(x[i], y[i]);       }     if (((int)((edge).size())) < k) puts( -1 ), exit(0);     for (i = 1; i <= E; i++)       if (!tp[i] and root(x[i]) != root(y[i])) {         edge.push_back(i);         merge(x[i], y[i]);       }     if (((int)((edge).size())) < 2 * k) puts( -1 ), exit(0);     printf( %d n , V - 1);     for (i = 0; i < ((int)((edge).size())); i++) printf( %d  , edge[i]);     puts(  );   }   int main() {     Read();     runs();   }


#include <bits/stdc++.h>   using namespace std;   int n, c;   int a[1 << 20], b[1 << 20];   char tp[1 << 20];   int hs[128][128];   int ne[128][128];   int li[128];   bool getDP(int l, int r) {     if (hs[l][r] != -1) return hs[l][r];     if (l >= r) {       return (hs[l][r] = 1);     }     hs[l][r] = 0;     for (int k = l; k <= r; ++k) {       bool ok = true;       for (int i = 0; i < c; ++i) {         if (a[i] == l) {           if (tp[i] ==  L  && b[i] > k) ok = false;           if (tp[i] ==  R  && b[i] <= k) ok = false;         }         if (a[i] > l && a[i] <= k) {           if (b[i] > k) ok = false;         }       }       if (ok) {         if (getDP(l + 1, k) && getDP(k + 1, r)) {           ne[l][r] = k;           hs[l][r] = 1;           break;         }       }     }     return hs[l][r];   }   int le[1 << 20], ri[1 << 20];   void cli(int l, int r) {     if (l >= r) return;     li[l] = ne[l][r];     if (li[l] > l) {       le[l] = l + 1;       cli(l + 1, li[l]);     }     if (li[l] < r) {       ri[l] = li[l] + 1;       cli(li[l] + 1, r);     }   }   vector<int> sl;   void sol(int v) {     if (le[v] != -1) sol(le[v]);     sl.push_back(v);     if (ri[v] != -1) sol(ri[v]);   }   int main() {     scanf( %d %d , &n, &c);     char buf[32];     for (int i = 0; i < c; i++) {       scanf( %d %d %s , &a[i], &b[i], buf);       tp[i] = buf[0];       if (a[i] >= b[i]) {         printf( IMPOSSIBLE n );         return 0;       }     }     memset(hs, -1, sizeof(hs));     bool ok = getDP(1, n);     if (!ok) {       printf( IMPOSSIBLE n );       return 0;     }     memset(li, -1, sizeof(li));     memset(le, -1, sizeof(le));     memset(ri, -1, sizeof(ri));     cli(1, n);     sol(1);     for (int i = 0; i + 1 < (int)sl.size(); i++) {       printf( %d  , sl[i]);     }     printf( %d n , sl.back());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class PriorityQueue {    private:     vector<pair<int, int> > d1, d2;     int l1 = 0, l2 = 0, r1 = 0, r2 = 0;     inline bool takeFirst() { return l1 != r1 && (l2 == r2 || d1[l1] < d2[l2]); }       public:     inline void prepare() {       swap(d1, d2);       swap(l1, l2);       swap(r1, r2);     }     inline void push(const pair<int, int> &p) {       d1.push_back(p);       r1++;     }     inline void repush(const pair<int, int> &p) {       if (l1) {         d1[--l1] = p;       } else {         d2[--l2] = p;       }     }     inline bool empty() { return l1 == r1 && l2 == r2; }     inline pair<int, int> top() { return takeFirst() ? d1[l1] : d2[l2]; }     inline void pop() { (takeFirst() ? l1 : l2)++; }   };   static const int heavyLim = 500;   struct FreqAnalyzer {     int n, m;     vector<int> items;     vector<int> freqs;     vector<int> heavy;     inline void add(int v, int sign) {       if (items[v]) {         freqs[items[v]]--;       }       items[v] += sign;       if (items[v]) {         freqs[items[v]]++;       }     }     inline int64_t huffmanSum() {       PriorityQueue pq;       for (int i = 0; i < min(n + 1, heavyLim); i++) {         if (freqs[i]) {           pq.push({i, freqs[i]});         }       }       vector<pair<int, int> > heavies;       for (int i : heavy) {         if (items[i] >= heavyLim) {           heavies.emplace_back(items[i], 1);         }       }       sort(heavies.begin(), heavies.end());       for (auto it : heavies) {         pq.push(it);       }       pq.prepare();       int64_t ans = 0;       while (!pq.empty()) {         auto it = pq.top();         pq.pop();         while (!pq.empty() && pq.top().first == it.first) {           it.second += pq.top().second;           pq.pop();         }         if (it.second == 1) {           if (pq.empty()) {             break;           } else {             auto jt = pq.top();             int q = jt.first;             jt.second--;             pq.pop();             if (jt.second) {               pq.repush(jt);             }             ans += it.first + q;             pq.push({it.first + q, 1});           }         } else {           int64_t a = it.first * 2, b = it.second / 2;           ans += a * b;           pq.push({a, b});           if (it.second & 1) {             pq.repush({it.first, 1});           }         }       }       return ans;     }     FreqAnalyzer(int n, int m, const vector<int> &heavy)         : n(n), m(m), items(m), freqs(n + 1), heavy(heavy) {}   };   const int moBlock = 300;   struct Query {     int l, r, idx;     inline pair<int, int> toPair() const {       auto res = make_pair(l / moBlock, r);       if (res.first & 1) {         res.second *= -1;       }       return res;     }   };   inline bool operator<(const Query &a, const Query &b) {     return a.toPair() < b.toPair();   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<int> a(n);     int m = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       m = max(m, a[i]);       a[i]--;     }     vector<int> knt(m);     vector<int> heavy;     for (int i = 0; i < n; i++) {       knt[a[i]]++;     }     for (int i = 0; i < m; i++) {       if (knt[i] >= heavyLim) {         heavy.push_back(i);       }     }     int q;     cin >> q;     vector<Query> qry(q);     for (int i = 0; i < q; i++) {       cin >> qry[i].l >> qry[i].r;       qry[i].l--;       qry[i].r--;       qry[i].idx = i;     }     sort(qry.begin(), qry.end());     FreqAnalyzer fa(n, m, heavy);     int l = 0, r = -1;     vector<int64_t> ans(q);     for (Query q : qry) {       while (l > q.l) {         fa.add(a[--l], +1);       }       while (r < q.r) {         fa.add(a[++r], +1);       }       while (l < q.l) {         fa.add(a[l++], -1);       }       while (r > q.r) {         fa.add(a[r--], -1);       }       ans[q.idx] = fa.huffmanSum();     }     for (int i = 0; i < q; i++) {       cout << ans[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void debug(int* a, int n, bool flag = false) {     if (!flag) printf( array  );     for (int i = 1; i <= n; i++) printf( %d  , a[i]);     printf(  n );   }   void debug(vector<int>& E, bool flag = false) {     if (!flag) printf( vector  );     for (auto it : E) printf( %d  , it);     printf(  n );   }   const int maxn = 2e5 + 5;   int a[maxn];   set<pair<int, int> > s;   int n, m;   map<int, bool> Map;   vector<int> res;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       Map[a[i]] = true;       s.insert(make_pair(i, -1));       s.insert(make_pair(i, 1));     }     sort(a + 1, a + 1 + n);     long long ans = 0;     long long k = 1;     while (m) {       auto it = s.begin();       while (it != s.end()) {         long long val = a[it->first] + k * it->second;         if (!Map.count(val)) {           res.push_back(val);           ans += k;           it++;           m--;           Map[val] = true;           if (!m) break;         } else           s.erase(it++);       }       k++;     }     printf( %I64d n , ans);     debug(res, true);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct lr {     long long left, right;     lr() : left(-1), right(1e7) {}     lr(long long _l, long long _r) : left(_l), right(_r) {}   };   int main() {     long long n;     scanf( %lld , &n);     vector<long long> arr(n);     for (int i = 0; i < n; i++) scanf( %lld , &arr[i]);     vector<lr> mn(n, lr(0, n - 1));     vector<lr> mx(n, lr(0, n - 1));     stack<lr> mxs, mns;     for (int i = 0; i < n; i++) {       while (!mxs.empty()) {         if (mxs.top().left >= arr[i]) {           break;         } else {           lr it = mxs.top();           mxs.pop();           mx[it.right].right = i - 1;         }       }       mxs.push(lr(arr[i], i));       while (!mns.empty()) {         if (mns.top().left <= arr[i]) {           break;         } else {           lr it = mns.top();           mns.pop();           mn[it.right].right = i - 1;         }       }       mns.push(lr(arr[i], i));     }     while (!mxs.empty()) mxs.pop();     while (!mns.empty()) mns.pop();     for (int i = n - 1; i >= 0; i--) {       while (!mxs.empty()) {         if (mxs.top().left > arr[i]) {           break;         } else {           lr it = mxs.top();           mxs.pop();           mx[it.right].left = i + 1;         }       }       mxs.push(lr(arr[i], i));       while (!mns.empty()) {         if (mns.top().left < arr[i]) {           break;         } else {           lr it = mns.top();           mns.pop();           mn[it.right].left = i + 1;         }       }       mns.push(lr(arr[i], i));     }     long long res = 0;     for (int i = 0; i < n; i++) {       long long x1 = mx[i].right - i + 1, x2 = mn[i].right - i + 1,                 n1 = i - mx[i].left + 1, n2 = i - mn[i].left + 1;       long long a = x1 * n1;       long long b = x2 * n2;       res += a * arr[i];       res -= b * arr[i];     }     cout << res << endl;     return 0;   }


#include <iostream>     #include <bits/stdc++.h>    #include <stdio.h>   #include <cstdlib>   #include<algorithm>    #include<string.h>   #include<map>   #include<math.h>   using namespace std;    int main()   {   int t;   cin>>t;   while(t--)   {   int n,k;   cin>>n>>k;int count =0;   vector <int> v,a(n);int max=n;   if(k>(n-1)/2)    {   cout<< -1 <<endl;       continue;   }   for(int i=1;i<n;i+=2)   {       if(count==k)           break;       a[i]=max;       max--;           count++;      }   for(int i=0;i<n;i++)   {       if(a[i]!=0)           continue;       a[i]=max;       max--;   }   for(int i=0;i<n;i++)   {       cout<<a[i]<<   ;   }      cout<<endl;   }   }


#include <bits/stdc++.h>   using namespace std;   const long long maxM = 22, maxN = ((1LL << 20) + 1);   long long M, mod, T, N;   int bc[maxM][maxM];   int dp[maxN][2][maxM];   int arr[maxN], b[maxM], p2[maxM];   struct Matrix {     long long n, m;     long long val[maxM][maxM];     Matrix() {       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) val[i][j] = 0;     }   };   Matrix operator*(Matrix A, Matrix B) {     Matrix ret;     ret.n = 1, ret.m = M;     for (int k = 0; k <= M; k++) {       long long vl = 0;       for (int i = 0; i <= (M - k); i++) {         long long vl2 = 0;         for (int j = 0; j <= k; j++) {           vl2 = (vl2 +                  (((bc[k][j] * A.val[0][i + j]) % mod) * B.val[0][i + k - j])) %                 mod;         }         vl = (vl + (bc[M - k][i] * vl2)) % mod;       }       vl %= mod;       if (vl < 0) vl += mod;       ret.val[0][k] = vl;     }     return ret;   }   Matrix Pow(Matrix A, long long b) {     Matrix Temp;     Temp.n = 1;     Temp.m = M;     for (int i = 0; i <= M; i++) Temp.val[0][i] = (int(i == 0));     if (b == 0) return Temp;     Matrix res = A;     for (b--; b; b >>= 1, A = A * A)       if (b & 1) res = res * A;     return res;   }   int main() {     scanf( %lld%lld%lld , &M, &T, &mod);     N = (1LL << M);     for (int i = 0; i < N; i++) {       scanf( %lld , &arr[i]);       arr[i] %= mod;       dp[i][0][0] = arr[i];     }     bc[0][0] = 1;     p2[0] = 1;     for (int i = 1; i <= (M + 1); i++) {       p2[i] = 2 * p2[i - 1];       for (int j = 0; j <= i; j++) {         if (j == 0 || j == i)           bc[i][j] = 1;         else           bc[i][j] = (bc[i - 1][j - 1] + bc[i - 1][j]);       }     }     for (int i = 0; i <= M; i++) {       scanf( %lld , &b[i]);       b[i] %= mod;     }     long long J = 1;     for (int j = 1; j <= M; j++) {       for (int i = 0; i < N; i++) {         for (int k = 0; k <= M; k++) {           if (j == 0 && k == 0) {             dp[i][J][k] = arr[i];           } else if (j == 0)             dp[i][J][k] = 0;           else {             dp[i][J][k] = (dp[i][1 - J][k] + dp[i ^ (p2[j - 1])][1 - J][k - 1]);             if (dp[i][J][k] >= mod) dp[i][J][k] -= mod;           }         }       }       J = 1 - J;     }     Matrix B;     B.n = 1;     B.m = M;     for (int i = 0; i <= M; i++) B.val[0][i] = b[i];     Matrix c = Pow(B, T);     long long MD = M % 2, ans;     for (int i = 0; i < N; i++) {       ans = 0;       for (int j = 0; j <= M; j++)         ans = (ans + (c.val[0][j] * dp[i][MD][j])) % mod;       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double n, l, k, v1, v2;   int main() {     cout << fixed << setprecision(10);     cin >> n >> l >> v1 >> v2 >> k;     if (v2 <= v1) {       cout << l / (v1 * 1.0) << endl;       return 0;     }     if (n <= k) {       cout << l / (v2 * 1.0) << endl;       return 0;     }     int T = ceil(n / (k * 1.0));     long double K = ((T - 1) * (1 + (v2 - v1) / ((v2 + v1) * 1.0))) * v1;     K += v2;     K = l / K;     long double ans = K * (T + (T - 1) * (v2 - v1) / (v2 + v1));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int start[200010], finish[200010], cnt, n, m, a[200010], dis[200010];   long long tree[2][8 * 200010];   vector<int> adj[200010], nodes[2], starts[2];   void update(int v, int vl, int vr, int l, int r, int val, int k) {     int mid = (vl + vr) / 2;     if (l <= vl && vr <= r) {       tree[k][v] += val;       return;     }     if (vr < l || r < vl) return;     update(2 * v, vl, mid, l, r, val, k);     update(2 * v + 1, mid + 1, vr, l, r, val, k);   }   long long getVal(int v, int vl, int vr, int idx, int k) {     int mid = (vl + vr) / 2;     if (vl == vr) return tree[k][v];     if (idx <= mid)       return tree[k][v] + getVal(2 * v, vl, mid, idx, k);     else       return tree[k][v] + getVal(2 * v + 1, mid + 1, vr, idx, k);   }   void dfs(int v, int f, int d) {     cnt++;     start[v] = cnt;     nodes[d % 2].push_back(v);     starts[d % 2].push_back(start[v]);     dis[v] = d % 2;     for (__typeof(((int)((adj[v]).size())) - 1) i = (0);          i <= (((int)((adj[v]).size())) - 1); i++)       if (adj[v][i] != f) dfs(adj[v][i], v, d + 1);     finish[v] = cnt;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (__typeof(n) i = (1); i <= (n); i++) cin >> a[i];     for (__typeof(n - 1) i = (1); i <= (n - 1); i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(1, -1, 0);     for (__typeof(n) i = (1); i <= (n); i++) {       int k = dis[i];       int idx = lower_bound((starts[k]).begin(), (starts[k]).end(), start[i]) -                 starts[k].begin();       idx++;       update(1, 1, n, idx, idx, a[i], k);     }     starts[0].push_back((1000 * 1000 * 1000));     starts[1].push_back((1000 * 1000 * 1000));     for (__typeof(m) i = (1); i <= (m); i++) {       int q, x, v;       cin >> q;       if (q == 1) {         cin >> x >> v;         int k = dis[x];         int idx1 = lower_bound((starts[k]).begin(), (starts[k]).end(), start[x]) -                    starts[k].begin();         int idx2 =             lower_bound((starts[k]).begin(), (starts[k]).end(), finish[x]) -             starts[k].begin();         int idx3 = lower_bound((starts[1 - k]).begin(), (starts[1 - k]).end(),                                start[x]) -                    starts[1 - k].begin();         int idx4 = lower_bound((starts[1 - k]).begin(), (starts[1 - k]).end(),                                finish[x]) -                    starts[1 - k].begin();         if (idx2 >= ((int)((nodes[k]).size())) ||             finish[nodes[k][idx2]] != finish[x])           idx2--;         if (idx4 >= ((int)((nodes[1 - k]).size())) ||             finish[nodes[1 - k][idx4]] != finish[x])           idx4--;         idx1++;         idx2++;         idx3++;         idx4++;         if (idx1 <= idx2) update(1, 1, n, idx1, idx2, v, k);         if (idx3 <= idx4) update(1, 1, n, idx3, idx4, -v, 1 - k);       } else {         cin >> x;         int k = dis[x];         int idx = lower_bound((starts[k]).begin(), (starts[k]).end(), start[x]) -                   starts[k].begin();         idx++;         cout << getVal(1, 1, n, idx, k) << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 7;   const long long INF = 1e18 + 7;   int n, a[MAXN], d[MAXN] = {}, k, l = 1, r = 0;   long long f[MAXN], g[MAXN], ans = 0;   void add(long long &a, const long long &b) {     a += b;     if (a > INF) a = INF;   }   long long sum(long long a, long long b) {     a += b;     if (a > INF) a = INF;     return a;   }   void cost(int newl, int newr) {     while (r < newr) ans += 1LL * (d[a[++r]]++);     while (r > newr) ans -= 1LL * (--d[a[r--]]);     while (l > newl) ans += 1LL * (d[a[--l]]++);     while (l < newl) ans -= 1LL * (--d[a[l++]]);   }   void calc(int l, int r, int mn, int mx) {     if (l > r) return;     int mid = (l + r) >> 1, opt = 0;     for (int i = min(mx, mid - 1); i >= mn; --i) {       cost(i + 1, mid);       if (g[i] + ans < f[mid]) f[mid] = g[i] + ans, opt = i;     }     calc(l, mid - 1, mn, opt);     calc(mid + 1, r, opt, mx);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; ++i) cin >> a[i];     g[0] = f[0] = 0;     for (int i = 1; i <= n; ++i) f[i] = g[i] = INF;     for (int i = 1; i <= k; ++i) {       calc(1, n, 0, n);       for (int j = 1; j <= n; ++j) g[j] = f[j], f[j] = INF;     }     cout << g[n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long mx = 2e6 + 10;   int posx[] = {1, -1, 0, 0};   int posy[] = {0, 0, 1, -1};   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1, n, k, m, a, b, c, d, h;     cin >> t;     while (t--) {       cin >> h >> c >> k;       if (h + c >= 2 * k) {         cout << 2 << endl;         continue;       }       long long x = (c - k) / (h + c - (2 * k));       long long y = x + 1;       long double xx = ((h * x) + (c * (x - 1))) / ((1.0) * 2 * x - 1);       long double yy = ((h * y) + (c * (y - 1))) / ((1.0) * 2 * y - 1);       if (abs(xx - k) <= abs(yy - k))         cout << 2 * x - 1 << endl;       else         cout << 2 * y - 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};   long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};   using namespace std;   class pa3 {    public:     long long x;     long long y, z;     pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}     bool operator<(const pa3 &p) const {       if (x != p.x) return x < p.x;       if (y != p.y) return y < p.y;       return z < p.z;     }     bool operator>(const pa3 &p) const {       if (x != p.x) return x > p.x;       if (y != p.y) return y > p.y;       return z > p.z;     }     bool operator==(const pa3 &p) const {       return x == p.x && y == p.y && z == p.z;     }     bool operator!=(const pa3 &p) const {       return !(x == p.x && y == p.y && z == p.z);     }   };   class pa4 {    public:     long long x;     long long y, z, w;     pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)         : x(x), y(y), z(z), w(w) {}     bool operator<(const pa4 &p) const {       if (x != p.x) return x < p.x;       if (y != p.y) return y < p.y;       if (z != p.z) return z < p.z;       return w < p.w;     }     bool operator>(const pa4 &p) const {       if (x != p.x) return x > p.x;       if (y != p.y) return y > p.y;       if (z != p.z) return z > p.z;       return w > p.w;     }     bool operator==(const pa4 &p) const {       return x == p.x && y == p.y && z == p.z && w == p.w;     }   };   class pa2 {    public:     long long x, y;     pa2(long long x = 0, long long y = 0) : x(x), y(y) {}     pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }     pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }     bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; }     bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; }     bool operator==(const pa2 &p) const {       return abs(x - p.x) == 0 && abs(y - p.y) == 0;     }     bool operator!=(const pa2 &p) const {       return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);     }   };   string itos(long long i) {     ostringstream s;     s << i;     return s.str();   }   long long gcd(long long v, long long b) {     if (v > b) return gcd(b, v);     if (v == b) return b;     if (b % v == 0) return v;     return gcd(v, b % v);   }   long long mod;   long long extgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long d = extgcd(b, a % b, y, x);     y -= a / b * x;     return d;   }   pair<long long, long long> operator+(const pair<long long, long long> &l,                                        const pair<long long, long long> &r) {     return {l.first + r.first, l.second + r.second};   }   pair<long long, long long> operator-(const pair<long long, long long> &l,                                        const pair<long long, long long> &r) {     return {l.first - r.first, l.second - r.second};   }   long long pr[1001010];   long long inv[1001010];   long long beki(long long wa, long long rr, long long warukazu) {     if (rr == 0) return 1 % warukazu;     if (rr == 1) return wa % warukazu;     wa %= warukazu;     if (rr % 2 == 1)       return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;     long long zx = beki(wa, rr / 2, warukazu);     return (zx * zx) % warukazu;   }   long long comb(long long nn, long long rr) {     if (rr < 0 || rr > nn || nn < 0) return 0;     long long r = pr[nn] * inv[rr];     r %= mod;     r *= inv[nn - rr];     r %= mod;     return r;   }   void gya(long long ert) {     pr[0] = 1;     for (long long i = 1; i <= ert; i++) {       pr[i] = (pr[i - 1] * i) % mod;     }     inv[ert] = beki(pr[ert], mod - 2, mod);     for (long long i = ert - 1; i >= 0; i--) {       inv[i] = inv[i + 1] * (i + 1) % mod;     }   }   struct edge {     long long to, cap, num, moto;   };   vector<edge> ZANYO[100020];   long long label[100020];   queue<pair<long long, long long> > qu_bfs;   long long dokomade[100020];   void add_edge(long long s_point, long long t_point, long long capa) {     ZANYO[s_point].push_back(         (edge){t_point, capa, (long long)ZANYO[t_point].size(), 0});     ZANYO[t_point].push_back(         (edge){s_point, 0, (long long)ZANYO[s_point].size() - 1, 1});   }   void bfs_dinic(long long s_point, long long N) {     for (long long i = 0; i < N; i++) label[i] = -1;     while (qu_bfs.size()) qu_bfs.pop();     qu_bfs.push(make_pair(s_point, 0));     while (qu_bfs.size()) {       pair<long long, long long> z = qu_bfs.front();       qu_bfs.pop();       if (label[z.first] >= 0) continue;       label[z.first] = z.second;       for (auto &v : ZANYO[z.first]) {         if (v.cap == 0) continue;         if (v.cap < 0) {           cout <<  error  << endl;           exit(0);         }         if (label[v.to] >= 0) continue;         qu_bfs.push(make_pair(v.to, z.second + 1));       }     }     return;   }   long long dfs_dinic(long long s_point, long long t_point, long long ima_flow) {     if (s_point == t_point) return ima_flow;     while (1) {       if (dokomade[s_point] >= ZANYO[s_point].size()) break;       long long edanum = dokomade[s_point];       if (ZANYO[s_point][edanum].cap < 0) {         cout <<  minus  << endl;         exit(0);       }       if (ZANYO[s_point][edanum].cap <= 0 ||           label[s_point] + 1 != label[ZANYO[s_point][edanum].to]) {         dokomade[s_point]++;         continue;       }       long long W = dfs_dinic(ZANYO[s_point][edanum].to, t_point,                               min(ZANYO[s_point][edanum].cap, ima_flow));       if (W > 0) {         ZANYO[s_point][edanum].cap -= W;         ZANYO[ZANYO[s_point][edanum].to][ZANYO[s_point][edanum].num].cap += W;         return W;       }       dokomade[s_point]++;     }     return 0;   }   long long max_flow_dinic(long long s_point, long long t_point, long long N) {     long long jougen = 1000000007 * 1000000000ll;     long long ans_flow = 0;     while (1) {       bfs_dinic(s_point, N);       if (label[t_point] == -1) break;       for (long long i = 0; i < N; i++) dokomade[i] = 0;       while (1) {         long long ima_flow = dfs_dinic(s_point, t_point, jougen + 1);         if (ima_flow == 0) break;         ans_flow += ima_flow;       }     }     return ans_flow;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(false);     vector<pa4> ve;     long long n, q;     cin >> n >> q;     set<long long> sex, sey;     sex.insert(0);     sex.insert(n);     sey.insert(0);     sey.insert(n);     for (long long i = 0; i < q; i++) {       long long x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       x1--, y1--;       sex.insert(x1);       sex.insert(x2);       sey.insert(y1);       sey.insert(y2);       ve.push_back((pa4){x1, x2, y1, y2});     }     vector<long long> vx, vy;     for (auto it = sex.begin(); it != sex.end(); it++) vx.push_back(*it);     for (auto it = sey.begin(); it != sey.end(); it++) vy.push_back(*it);     long long a = vx.size() - 1;     long long b = vy.size() - 1;     set<pair<long long, long long> > se;     for (long long i = 0; i < a; i++)       for (long long j = 0; j < b; j++) {         long long sx = vx[i], sy = vx[i + 1];         long long tx = vy[j], ty = vy[j + 1];         bool bo = 0;         for (auto v : ve) {           if (sx >= v.x && v.y >= sy && tx >= v.z && v.w >= ty) bo = 1;         }         if (bo) se.insert(make_pair(i, j));       }     long long A[200], B[200];     for (long long i = 0; i < a; i++) {       long long sx = vx[i], sy = vx[i + 1];       add_edge(0, 1 + i, sy - sx);       A[i] = sy - sx;     }     for (long long j = 0; j < b; j++) {       long long tx = vy[j], ty = vy[j + 1];       add_edge(a + 1 + j, a + b + 1, ty - tx);       B[j] = ty - tx;     }     for (auto it = se.begin(); it != se.end(); it++) {       pair<long long, long long> z = *it;       add_edge(1 + z.first, a + 1 + z.second, min(A[z.first], B[z.second]));     }     cout << max_flow_dinic(0, a + b + 1, a + b + 5) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename _T>   inline void read(_T &f) {     f = 0;     _T fu = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) fu = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       f = (f << 3) + (f << 1) + (c & 15);       c = getchar();     }     f *= fu;   }   template <typename T>   void print(T x) {     if (x < 0) putchar( - ), x = -x;     if (x < 10)       putchar(x + 48);     else       print(x / 10), putchar(x % 10 + 48);   }   template <typename T>   void print(T x, char t) {     print(x);     putchar(t);   }   const int N = 1005;   int a[N][N], ans[N][N], ans2[N][N], b[N];   int n, m;   int main() {     read(n);     read(m);     for (register int i = 1; i <= n; i++) {       for (register int j = 1; j <= m; j++) {         read(a[i][j]);       }     }     for (register int i = 1; i <= n; i++) {       for (register int j = 1; j <= m; j++) b[j] = a[i][j];       sort(b + 1, b + m + 1);       int big = unique(b + 1, b + m + 1) - b - 1;       for (register int j = 1; j <= m; j++) {         int tmp = lower_bound(b + 1, b + big + 1, a[i][j]) - b;         ans[i][j] = max(ans[i][j], tmp);         ans2[i][j] = max(ans2[i][j], big - tmp);       }     }     for (register int j = 1; j <= m; j++) {       for (register int i = 1; i <= n; i++) b[i] = a[i][j];       sort(b + 1, b + n + 1);       int big = unique(b + 1, b + n + 1) - b - 1;       for (register int i = 1; i <= n; i++) {         int tmp = lower_bound(b + 1, b + big + 1, a[i][j]) - b;         ans[i][j] = max(ans[i][j], tmp);         ans2[i][j] = max(ans2[i][j], big - tmp);       }     }     for (register int i = 1; i <= n; i++) {       for (register int j = 1; j <= m; j++) {         print(ans[i][j] + ans2[i][j], j == m ?   n  :    );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void optimise() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   long long int mod = 1e9 + 7;   void normalize(long long int &a) { a = (a + mod) % mod; }   long long int modmul(long long int a, long long int b) {     a = a % mod;     b = b % mod;     normalize(a);     normalize(b);     return (a * b) % mod;   }   long long int modadd(long long int a, long long int b) {     a = a % mod;     b = b % mod;     normalize(a);     normalize(b);     return (a + b) % mod;   }   long long int modsub(long long int a, long long int b) {     a = a % mod;     b = b % mod;     normalize(a);     normalize(b);     return (a - b + mod) % mod;   }   long long int me(long long int x, long long int n) {     x %= mod;     if (n == 0) return 1;     long long int u = me(x, n / 2) % mod;     u = (u * u) % mod;     if (n % 2) u = (u * x) % mod;     return u;   }   long long int me1(long long int x, long long int n) {     if (n == 0) return 1;     long long int u = me1(x, n / 2);     u = u * u;     if (n % 2) u = u * x;     return u;   }   inline long long int modInv(long long int a) { return me(a, mod - 2); }   inline long long int modDiv(long long int a, long long int b) {     return modmul(a, modInv(b));   }   float power(float x, long long int y) {     float temp;     if (y == 0) return 1;     temp = power(x, y / 2);     if (y % 2 == 0)       return temp * temp;     else {       if (y > 0)         return x * temp * temp;       else         return (temp * temp) / x;     }   }   long long int __gcd(long long int a, long long int b) {     if (b == 0)       return a;     else       return __gcd(b, a % b);   }   pair<long long int, long long int> operator+(       pair<long long int, long long int> a,       pair<long long int, long long int> b) {     return {a.first + b.first, a.second + b.second};   }   pair<long long int, long long int> operator-(       pair<long long int, long long int> a,       pair<long long int, long long int> b) {     return {a.first - b.first, a.second - b.second};   }   pair<long long int, long long int> operator*(       pair<long long int, long long int> a,       pair<long long int, long long int> b) {     return {a.first * b.first, a.second * b.second};   }   template <typename T>   ostream &operator<<(ostream &os, const set<T> &v) {     for (auto it : v) os << it <<    ;     return os;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     for (long long int i = 0; i < v.size(); ++i) os << v[i] <<    ;     return os;   }   bool compare(string s1, string s2) { return s1.size() < s2.size(); }   void solve() {     long long int n, k;     cin >> n >> k;     vector<pair<long long int, long long int> > v;     if (n > k * (k - 1)) {       cout <<  No ;       return;     }     cout <<  Yes           <<   n ;     for (long long int i = 0; i < k; i++) {       for (long long int j = i + 1; j < k; j++) {         n--;         cout << i + 1 <<     << j + 1 <<   n ;         if (n == 0) return;         n--;         cout << j + 1 <<     << i + 1 <<   n ;         if (n == 0) return;       }     }   }   signed main() {     optimise();     long long int t;     t = 1;     cout << fixed << setprecision(5);     while (t--) {       solve();       cout <<   n ;     }     cerr <<   n           <<  Time Elasped :   << 1.0 * clock() / CLOCKS_PER_SEC <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;     inline bool operator<(const point &a) const { return x < a.x; }   } arr[100005];   struct query_tp {     int col, l, r, delta;     query_tp() {}     query_tp(int _col, int _l, int _r, int _delta) {       col = _col;       l = _l;       r = _r;       delta = _delta;     }     inline bool operator<(const query_tp &a) const { return col < a.col; }   };   long long ans[100005];   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d%d , &arr[i].x, &arr[i].y);     sort(arr, arr + n);     vector<query_tp> query;     vector<int> app;     for (int i = 0; i < n; i++) {       int l = arr[i].x - k;       if (app.size()) l = max(l, *app.rbegin());       for (int j = l + 1; j <= arr[i].x; j++) app.push_back(j);       query.push_back(query_tp(arr[i].y - k + 1, arr[i].x - k + 1, arr[i].x, 1));       query.push_back(query_tp(arr[i].y + 1, arr[i].x - k + 1, arr[i].x, -1));     }     sort(query.begin(), query.end());     int cnt = app.size(), q_cnt = query.size();     for (int i = 0; i < q_cnt; i++) {       query[i].l = lower_bound(app.begin(), app.end(), query[i].l) - app.begin();       query[i].r = lower_bound(app.begin(), app.end(), query[i].r) - app.begin();     }     app.clear();     vector<int>().swap(app);     vector<int> lst(cnt, -1e9), num(cnt, 0);     for (int i = 0; i < q_cnt; i++) {       for (int j = query[i].l; j <= query[i].r; j++) {         if (lst[j] != -1e9) ans[num[j]] += query[i].col - lst[j];         num[j] += query[i].delta;         lst[j] = query[i].col;       }     }     for (int i = 1; i <= n; i++) printf( %lld%c , ans[i],    n [i == n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int lim = 200005;   long long int n;   vector<vector<long long int> > ans;   vector<long long int> ed;   void solve(int test) {     cin >> n;     for (int(i) = 0; (i) < n; (i)++) {       long long int v;       cin >> v;       vector<long long int>::iterator ind =           upper_bound((ed).begin(), (ed).end(), -v);       if (ind == ed.end()) {         ed.push_back(-v);         ans.push_back(vector<long long int>(1, v));       } else {         int x = ind - ed.begin();         ed[x] = -v;         ans[x].push_back(v);       }     }     for (int(i) = 0; (i) < int((ans).size()); (i)++) {       for (int(j) = 0; (j) < int((ans[i]).size()); (j)++) {         cout << ans[i][j] <<    ;       }       cout << endl;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int test = 1;     for (int(i) = 0; (i) < test; (i)++) {       solve(i + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long nxt(long long x) {     long long mn = 10;     long long mx = 0;     long long xx = x;     while (x > 0) {       mn = min(mn, x % 10);       mx = max(mx, x % 10);       x /= 10;     }     return xx + mn * mx;   }   signed main() {     long long t;     cin >> t;     while (t--) {       long long a1, k;       cin >> a1 >> k;       k--;       while (k--) {         if (a1 == nxt(a1)) break;         a1 = nxt(a1);       }       cout << a1 <<   n ;     }   }


#include <bits/stdc++.h>   const int MAX = 2000010, mod = 1000000007;   int n, m, mp[MAX], c[MAX], d[MAX], ds[MAX];   bool ty[MAX];   void upd(int i, int x) {     x > d[i] ? d[i] = x, ds[i] = 1 : x == d[i] ? ds[i]++ : 0;   }   int main() {     scanf( %d , &n);     for (int p; n--;) scanf( %d , &p), c[p > m ? m = p : p]++;     for (int i = 2; i <= m; i++)       if (!mp[i])         for (int j = i; j <= m; j += i)           if (!mp[j]) mp[j] = i;     int s = 1, t = 0;     for (int i = m; i; i--)       for (int j = c[i]; j--;) {         if (d[i]) {           for (int x = i - 1; x > 1;) {             int p = mp[x], c = 0;             for (; x % p == 0; x /= p) c++;             for (int e = d[p]; e < c; e++) s = 1ll * s * p % mod;             upd(p, c);           }           ty[i] = 1;         } else           upd(i, 1), s = 1ll * s * i % mod;       }     for (int i = m; i; i--)       if (ty[i]) {         bool fl = 1;         for (int x = i - 1; x > 1;) {           int p = mp[x], c = 0;           for (; x % p == 0; x /= p) c++;           if (c == d[p] && ds[p] == 1) fl = 0;         }         if (fl) t = 1;       }     printf( %d n , (s + t) % mod);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 10e5;   int minc, maxk = 0;   long long int a[maxn], c[maxn];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cout << setprecision(30);     int n, s;     cin >> n >> s;     for (int i = 0; i < n; i++) cin >> a[i];     int l = 0, r = n + 1;     while (r - l > 1) {       long long int mid = (l + r) / 2, tc = 0;       for (int i = 0; i < n; i++) c[i] = mid * (i + 1) + a[i];       sort(c, c + n);       for (int i = 0; i < mid; i++) tc += c[i];       if (tc > s) {         r = mid;       }       if (tc <= s) {         minc = tc;         maxk = mid;         l = mid;       }     }     cout << maxk <<     << minc;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMax = 100005;   vector<int> G[NMax];   long double Array[NMax];   int DP[NMax], N;   void Read() {     cin >> N;     for (int i = 1; i < N; i++) {       int x;       cin >> x;       G[x].push_back(i + 1);     }   }   void buildDP(int node) {     DP[node] = 1;     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       buildDP(neighb);       DP[node] += DP[neighb];     }   }   void DFS(int node) {     int sz = G[node].size();     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       Array[neighb] =           Array[node] + 0.5 * ((long double)DP[node] - 1.0 - DP[neighb]) + 1.0;       DFS(neighb);     }   }   void Print() {     for (int i = 1; i <= N; i++)       cout << fixed << setprecision(2) << Array[i] <<    ;     cout <<   n ;   }   int main() {     Read();     buildDP(1);     Array[1] = 1.0;     DFS(1);     Print();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void no() {     cout <<  IMPOSSIBLE  << endl;     exit(0);   }   int main() {     cin.sync_with_stdio(false);     int n, m;     cin >> n >> m;     int answer = 0;     int last_day = 0, last_h = 0;     for (int i = 0; i < m; i++) {       int cur, h;       cin >> cur >> h;       if (i == 0) {         answer = h + cur - 1;       } else {         answer = max(answer, h);         int dif = abs(h - last_h);         int days = cur - last_day;         if (dif > days) no();         if (h >= last_h) {           answer = max(answer, h + (days - dif) / 2);         } else {           answer = max(answer, last_h + (days - dif) / 2);         }       }       last_day = cur;       last_h = h;     }     answer = max(answer, last_h + n - last_day);     cout << answer << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<pair<long long, long long> > s[17];   long long a[100002];   long long g[100002];   signed main() {     ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long n, m;     cin >> n >> m;     for (long long i = 0; i <= n; i++) g[i] = (i * (i + 1)) / 2LL;     long long ans = 0;     for (long long i = 0; i <= n - 1; i++) cin >> a[i];     for (long long j = 0; j <= 16; j++) {       for (long long i = 0; i <= n - 1; i++) {         if (a[i] & (1LL << j)) {           long long len = 1;           long long st = i;           i++;           while (1) {             if (i == n or !(a[i] & (1LL << j))) {               s[j].insert({st, len});               ans += (g[len] * (1LL << j));               break;             }             len++;             i++;           }         }       }     }     while (m--) {       long long idx, val, pval;       cin >> idx >> val;       idx--;       pval = a[idx];       for (long long j = 0; j <= 16; j++) {         if (val & (1LL << j) and !(pval & (1LL << j))) {           auto it = s[j].lower_bound({idx, 1e9});           if (it == s[j].end() or (*it).first != idx + 1)             s[j].insert({idx, 1}), ans += (1LL << j);           else             ans += (1LL << j) * ((*it).second + 1),                 s[j].insert({idx, (*it).second + 1}), s[j].erase(it);           it = s[j].lower_bound({idx, -1});           if (it != s[j].begin()) {             auto pt = prev(it);             if ((*pt).second + (*pt).first - 1 == idx - 1) {               ans -= (1LL << j) * (g[(*pt).second] + g[(*it).second]),                   ans += (1LL << j) * g[(*it).second + (*pt).second],                   s[j].insert({(*pt).first, (*pt).second + (*it).second}),                   s[j].erase(it), s[j].erase(pt);             }           }         } else if (pval & (1LL << j) and !(val & (1LL << j))) {           auto it = s[j].lower_bound({idx, -1});           if (it != s[j].end() and (*it).first == idx) {             if ((*it).second == 1)               ans -= (1LL << j), s[j].erase({idx, 1});             else               ans -= (1LL << j) * ((*it).second),                   s[j].insert({idx + 1, (*it).second - 1}), s[j].erase(it);           } else {             it = prev(it);             if ((*it).first + (*it).second - 1 == idx)               ans -= (1LL << j) * ((*it).second),                   s[j].insert({(*it).first, (*it).second - 1}), s[j].erase(it);             else {               ans -= (1LL << j) * g[(*it).second],                   ans += (1LL << j) * g[idx - 1 - (*it).first + 1],                   ans +=                   (1LL << j) * g[(*it).first + (*it).second - 1 - idx - 1 + 1];               s[j].insert({(*it).first, idx - 1 - (*it).first + 1}),                   s[j].insert(                       {idx + 1, (*it).first + (*it).second - 1 - idx - 1 + 1}),                   s[j].erase(it);             }           }         }       }       a[idx] = val;       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long aa[33];   long long a[33], b[33], k[33], p[33];   long long n, u, r;   long long ans = -1e10;   void dfs(long long d, long long pre) {     if ((u - d) % 2 == 0) {       long long t = 0;       for (int i = 1; i <= n; i++) t = t + a[i] * k[i];       ans = max(ans, t);     }     if (d == u) return;     long long tmp[33];     for (int i = 1; i <= n; i++) tmp[i] = a[i];     for (int i = 1; i <= n; i++) aa[i] = a[i];     if (pre != 1) {       for (int i = 1; i <= n; i++) a[i] = a[i] ^ b[i];       dfs(d + 1, 1);       for (int i = 1; i <= n; i++) a[i] = tmp[i];     }     for (int i = 1; i <= n; i++) aa[i] = a[p[i]] + r;     for (int i = 1; i <= n; i++) a[i] = aa[i];     dfs(d + 1, -1);     for (int i = 1; i <= n; i++) a[i] = tmp[i];   }   int main() {     cin >> n >> u >> r;     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= n; i++) scanf( %I64d , &b[i]);     for (int i = 1; i <= n; i++) scanf( %I64d , &k[i]);     for (int i = 1; i <= n; i++) scanf( %I64d , &p[i]);     dfs(0, -1);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     int a[n];     map<int, int> mp;     for (int i = 0; i < n; i++) {       cin >> a[i];       mp[a[i]]++;     }     long long ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 32; j++) {         int p = 1 << j;         if (p < a[i]) {           continue;         }         ans += mp[p - a[i]];         if (a[i] == p - a[i]) {           ans--;         }       }     }     ans /= 2;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, m;     scanf( %d %d , &n, &m);     int msg1[n];     int msg2[m];     for (int i = 0; i < n; i++) {       scanf( %d , &msg1[i]);     }     for (int i = 0; i < m; i++) {       scanf( %d , &msg2[i]);     }     for (int i = 1; i < n; i++) {       msg1[i] = msg1[i] + msg1[i - 1];     }     for (int i = 1; i < m; i++) {       msg2[i] = msg2[i] + msg2[i - 1];     }     int count = 0;     int i = 0, j = 0;     while (i < n && j < m) {       if (msg1[i] < msg2[j])         i++;       else if (msg1[i] > msg2[j])         j++;       else {         i++;         j++;         count++;       }     }     printf( %d , count);   }


#include<bits/stdc++.h>      using namespace std;      const int N = 2000 + 10;      int n;   bool flag[N];      bool Query(int l1, int r1, int l2, int r2) {    printf( ? %d %d n , r1 - l1 + 1, r2 - l2 + 1);    for (int i = l1; i <= r1; i++) printf( %d%c , i,    n [i == r1]);    for (int i = l2; i <= r2; i++) printf( %d%c , i,    n [i == r2]);    fflush(stdout);    int res; scanf( %d , &res);    return res;   }      void Solve() {    scanf( %d , &n);    for (int i = 1; i <= n; i++) flag[i] = 0;    for (int i = 2; i <= n; i++) {     if (Query(1, i - 1, i, i)) {      // printf( i = %d n , i);      flag[i] = 1;      for (int j = i + 1; j <= n; j++) {       if (Query(i, i, j, j)) {        flag[j] = 1;       }      }      int L = 1, R = i - 1, ans = 0;      while (L <= R) {       // printf( L = %d, R = %d n , L, R);       int M = (L + R) >> 1;       if (Query(1, M, i, i)) R = M - 1, ans = M;       else L = M + 1;      }      // printf( ans = %d n , ans);      flag[ans] = 1;      break;     }    }    // puts( ans );    // for (int i = 1; i <= n; i++) {       // }    vector <int> ans;    for (int i = 1; i <= n; i++) {     if (!flag[i]) {      ans.push_back(i);     }    }    printf( ! %d n , ans.size());    for (auto x : ans) {     printf( %d  , x);    } puts(  );    fflush(stdout);   }      int main(){       srand(time(0));   #ifndef ONLINE_JUDGE       // freopen( data.in ,  r , stdin);       // freopen( tst.out ,  w , stdout);       clock_t st = clock();   #endif       int T = 1;    scanf( %d , &T);       while (T--) {     Solve();       }   #ifndef ONLINE_JUDGE       clock_t ed = clock();       printf( time : %.6lfs n , 1.0 * (ed - st) / CLOCKS_PER_SEC);   #endif       return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   int m = 0;   int block[5005];   long long f[5005];   int main() {     int n, tmp;     int num_of_for = 0;     cin >> n;     tmp = n;     while (tmp--) {       char c;       cin >> c;       if (c ==  s ) {         block[m] = num_of_for;         num_of_for = 0;         m++;       } else {         num_of_for++;       }     }     for (int k = 0; k < n; k++) {       f[k] = 1;     }     for (int i = m - 1; i >= 0; i--) {       f[0] = f[block[i]];       for (int k = 1; k < n; k++) {         if (block[i] + k < n) {           f[k] = f[k - 1] + f[block[i] + k];         } else {           f[k] = f[k - 1];         }         f[k] %= mod;       }     }     cout << (f[0] + mod) % mod;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007LL;   long long N;   vector<long long> E[100000];   long long dp[100000][2];   long long ans = 0;   long long mod_inv(long long n) {     long long t = MOD - 2, p = 1, q = n;     while (t > 0) {       if (t % 2 == 1) p = (p * q) % MOD;       q = (q * q) % MOD;       t /= 2;     }     return p % MOD;   }   void dfs(long long v, long long p) {     bool leaf = true;     for (long long u : E[v])       if (u != p) {         dfs(u, v);         leaf = false;       }     if (leaf) {       dp[v][0] = 1;       dp[v][1] = 1;     } else {       long long ev = 1, od = 1;       for (long long u : E[v])         if (u != p) {           (ev *= dp[u][0]) %= MOD;           (od *= dp[u][1]) %= MOD;         }       dp[v][0] = (ev + od) % MOD;       dp[v][1] = (od + ev) % MOD;     }   }   void dfs2(long long v, long long p, long long ev, long long od) {     long long _ev = 1, _od = 1;     for (long long u : E[v])       if (u != p) {         (_ev *= dp[u][0]) %= MOD;         (_od *= dp[u][1]) %= MOD;       }     if (p != -1) {       long long d = _ev * ev % MOD + _od * od % MOD;       ans += d;       ans %= MOD;     } else {       ans += dp[v][0];       ans %= MOD;     }     long long cnt = 0;     for (long long u : E[v])       if (u != p) {         cnt++;       }     for (long long u : E[v])       if (u != p) {         long long nev, nod, s;         if (cnt == 1) {           s = ev + od;           nev = s;           nod = s;         } else if (p == -1) {           s = _ev * mod_inv(dp[u][0]) % MOD + _od * mod_inv(dp[u][1]) % MOD;           nev = s;           nod = s;         } else {           s = _ev * mod_inv(dp[u][0]) % MOD * ev % MOD +               _od * mod_inv(dp[u][1]) % MOD * od % MOD;           nev = s;           nod = s;         }         nev %= MOD;         nod %= MOD;         dfs2(u, v, nev, nod);       }   }   int main(void) {     cin >> N;     for (long long i = ((long long)0); i < ((long long)N - 1); i++) {       long long A, B;       cin >> A >> B;       A--;       B--;       E[A].push_back(B);       E[B].push_back(A);     }     dfs(0, -1);     dfs2(0, -1, 1, 0);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long int Lpow(long long int a, long long int b) {     long long int ans = 1;     while (b > 0) {       if (b % 2) ans = (ans * a);       a = (a * a);       b = b / 2;     }     return ans;   }   long long int Lpow(long long int a, long long int b, long long int M) {     long long int ans = 1;     while (b > 0) {       if (b % 2) ans = (ans * a) % M;       a = (a * a) % M;       b = b / 2;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n;     cin >> n;     vector<int> brr;     int f1 = 0;     int f2 = 0;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       int sum = 0;       int y;       int sum2 = 0;       for (int j = 0; j < x; j++) {         cin >> y;         if (j > ((x - 1) / 2)) sum += y;         if (j < (x / 2)) sum2 += y;         if (x % 2 == 1 && j == x / 2) brr.push_back(y);       }       f1 += sum2;       f2 += sum;     }     sort(brr.begin(), brr.end());     reverse(brr.begin(), brr.end());     for (int i = 0; i < brr.size(); i += 2) f1 += brr[i];     for (int i = 1; i < brr.size(); i -= -2) f2 += brr[i];     cout << f1 <<     << f2 <<   n ;     return 0;   }


#include <bits/stdc++.h>   const int MAX_N = 102;   using namespace std;   string s;   int dp[52][11];   long long numberOfways(int i, int digit) {     if (i == s.length()) return 1;     if (dp[i][digit] != -1) return dp[i][digit];     dp[i][digit] = 0;     int curDigit = s[i] -  0 ;     int newDigit = curDigit + digit;     if (newDigit % 2) {       return dp[i][digit] += numberOfways(i + 1, newDigit / 2) +                              numberOfways(i + 1, newDigit / 2 + 1);     } else {       return dp[i][digit] += numberOfways(i + 1, newDigit / 2);     }   }   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(NULL);     };     memset(dp, -1, sizeof(dp));     cin >> s;     long long ans = 0;     for (int i = 0; i <= 9; i++) {       ans += numberOfways(1, i);     }     bool ownNumber = true;     for (int i = 0; i < s.length() - 1; i++) {       int digit = abs(s[i] - s[i + 1]);       if (digit > 1) {         ownNumber = false;         break;       }     }     if (ownNumber) ans--;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> w(n);     for (int i = 0; i < n; i++) cin >> w[i];     int x = 0;     vector<int> ord(n);     vector<int> rd(m);     vector<bool> put(n, false);     for (int i = 0; i < m; i++) {       cin >> rd[i];       rd[i]--;       if (put[rd[i]])         continue;       else {         ord[x] = rd[i];         put[rd[i]] = true;         x++;       }     }     for (int i = 0; i < n; i++) {       if (!put[i]) {         ord[x] = i;         x++;       }     }     int lift = 0;     for (int i = 0; i < m; i++) {       int in = 0;       for (int j = 0; j < n; j++) {         if (ord[j] == rd[i]) {           in = j;         }       }       for (int j = in; j > 0; j--) {         ord[j] = ord[j - 1];         lift += w[ord[j]];       }       ord[0] = rd[i];     }     cout << lift << endl;   }