#include <bits/stdc++.h>   using namespace std;   int c[105], sum[105];   pair<int, int> p[105];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &c[i]);       sum[i] = sum[i - 1] + c[i];     }     int st = 1, tot = 0;     for (int i = 1; i <= n; i++) {       if (sum[i] - sum[st - 1] < 0 && (c[i + 1] > 0 || i == n))         p[++tot].first = st, p[tot].second = i, st = i + 1;       else if (sum[i] - sum[st - 1] > 0 && (c[i + 1] < 0 || i == n))         p[++tot].first = st, p[tot].second = i, st = i + 1;     }     if (st != n + 1)       printf( NO );     else {       printf( YES n%d n , tot);       for (int i = 1; i <= tot; i++) printf( %d %d n , p[i].first, p[i].second);     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   std::pair<int, int> DR[] = {{-1, 0}, {0, 1},   {1, 0}, {0, -1},                               {-1, 1}, {-1, -1}, {1, 1}, {1, -1}};   using namespace std;   int n, x, a[1 << 19];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cerr.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     multiset<int> ms;     for (int i = 1; i <= n; i++) {       cin >> x;       ms.insert(x);     }     for (int i = 1; i <= n; i++) {       auto it = ms.begin();       auto itt = ms.end();       itt--;       int x = 0;       for (int j = 29; j + 1; j--) {         if (a[i] & (1LL << j)) {           if (*itt >= x + (1LL << j)) {             x += (1LL << j);             auto luh = ms.lower_bound(x);             if (*luh > *it) it = luh;           }         } else {           if (*it < x + (1LL << j)) {             auto luh = ms.lower_bound(x + (1LL << j));             luh--;             if (*luh < *itt) itt = luh;           } else             x += (1LL << j);         }       }       cout << (a[i] ^ (*it)) <<    ;       ms.erase(it);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 512, M = 20000 + 10;   struct Stage {     int p[N];   } stage[N * 2];   struct Edge {     int u, v;     void init() { scanf( %d%d , &u, &v); }   } edge[M];   int preStg[M], postStg[M], nstg;   int p[N], num[N];   int n, m;   int findr(int x, int* P) {     int r = x;     while (P[r]) r = P[r];     while (P[x]) {       int t = P[x];       P[x] = r;       x = t;     }     return r;   }   void join(int x, int y, int* P) {     int r1 = findr(x, P);     int r2 = findr(y, P);     if (r1 == r2) return;     if (num[r1] > num[r2]) {       num[r1] += num[r2];       P[r2] = r1;     } else {       num[r2] += num[r1];       P[r1] = r2;     }   }   int l, r;   int solve() {     Stage st1 = stage[preStg[l]];     Stage st2 = stage[postStg[r]];     memset(num, 0, sizeof(num));     for (int i = 1; i <= n; ++i) {       join(i, st2.p[i], st1.p);     }     int sum = 0;     for (int i = 1; i <= n; ++i) {       sum += (findr(i, st1.p) == i);     }     return sum;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; ++i) {       edge[i].init();     }     memset(p, 0, sizeof(p));     memset(num, 0, sizeof(num));     for (int i = 1; i <= n; ++i) {       stage[nstg].p[i] = p[i];     }     nstg++;     for (int i = 1; i <= m; ++i) {       preStg[i] = nstg - 1;       if (findr(edge[i].u, p) != findr(edge[i].v, p)) {         join(edge[i].u, edge[i].v, p);         for (int j = 1; j <= n; ++j) {           stage[nstg].p[j] = p[j];         }         nstg++;       }     }     memset(p, 0, sizeof(p));     memset(num, 0, sizeof(num));     for (int i = 1; i <= n; ++i) {       stage[nstg].p[i] = p[i];     }     nstg++;     for (int i = m; i >= 1; --i) {       postStg[i] = nstg - 1;       if (findr(edge[i].u, p) != findr(edge[i].v, p)) {         join(edge[i].u, edge[i].v, p);         for (int j = 1; j <= n; ++j) {           stage[nstg].p[j] = p[j];         }         nstg++;       }     }     int q;     scanf( %d , &q);     while (q--) {       scanf( %d %d , &l, &r);       printf( %d n , solve());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, k, i, x, y, s, p;     cin >> n >> m;     s = 0;     k = 1;     for (i = 1; i <= n; i++) {       cin >> x >> y;       k = x - k;       p = k % m;       x -= p;       s += y - x + 1;       k = y + 1;     }     cout << s << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, a = 0, b = 0, c = 0, d = 0, i, e = 0, mine = 0, mn = 0;     cin >> n;     string s;     cin >> s;     for (i = 0; i < s.size(); i++) {       if (s[i] ==  z ) a++;       if (s[i] ==  e ) b++;       if (s[i] ==  r ) c++;       if (s[i] ==  o ) d++;       if (s[i] ==  n ) e++;     }     mn = min(min(d, c), min(a, b));     if (b > mn && d > mn && e > 0) {       mine = min(min((b - mn), (d - mn)), e);       for (i = 0; i < mine; i++) {         cout <<  1               <<    ;       }     }     if (mn > 0) {       for (i = 0; i < mn; i++) {         cout <<  0               <<    ;       }     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int hm(string s) {     int c = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || s[i] ==  u )         c++;     }     return c;   }   int main() {     string s1, s2, s3;     getline(cin, s1);     getline(cin, s2);     getline(cin, s3);     if (hm(s1) == 5 && hm(s2) == 7 && hm(s3) == 5)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long oll = 0;   bool comp(const pair<int, int> &a, const pair<int, int> &b) {     return a.second < b.second;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<long long> first, second;     long long fr = 0, sc = 0;     int lst = -1;     for (long long i = 0; i < n; i++) {       int temp;       cin >> temp;       if (temp > 0) {         if (i == n - 1) {           lst = 1;         }         first.push_back(temp);         fr += temp;       } else {         if (i == n - 1) {           lst = 2;         }         temp *= -1;         second.push_back(temp);         sc += (temp);       }     }     if (fr > sc) {       cout <<  first             <<   n ;     } else if (fr < sc) {       cout <<  second             <<   n ;     } else {       int win = -1;       for (long long i = 0; i < min(first.size(), second.size()); i++) {         if (first[i] > second[i]) {           win = 1;           break;         } else if (first[i] < second[i]) {           win = 2;           break;         }       }       if (win == 1) {         cout <<  first               <<   n ;       } else if (win == 2) {         cout <<  second               <<   n ;       } else {         if (lst == 1) {           cout <<  first                 <<   n ;         } else {           cout <<  second                 <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dr[] = {-1, 0, 1, 0, -1, 1, 1, -1};   int dc[] = {0, 1, 0, -1, 1, 1, -1, -1};   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     while (cin >> n) {       vector<int> arr;       int st = 1;       int ans = 0;       for (int i = (0); i <= (2 * n - 1); i += (1)) {         string s;         cin >> s;         if (s ==  remove ) {           if (arr.empty()) {             st++;             continue;           }           if (st == arr[(int)arr.size() - 1]) {             st++;             arr.pop_back();           } else {             arr.clear();             st++;             ans++;           }         } else {           int nn;           cin >> nn;           arr.push_back(nn);         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     char a[100010];     int k;     cin >> s;     k = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] == a[k])         k--;       else {         k++;         a[k] = s[i];       }     }     if (k == 0)       cout <<  Yes ;     else       cout <<  No ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T abs(T x) {     if (x < 0) return -x;     return x;   }   template <class T>   T sqr(T a) {     return a * a;   }   const double pi = acos(-1.0);   const double eps = 1e-8;   int ar[150010];   int flag[150010];   int i, n;   int main() {     while (1 == scanf( %d , &n)) {       memset(flag, 0, sizeof flag);       for (i = 0; i < n; i++) scanf( %d , &ar[i]);       sort(&ar[0], &ar[n]);       for (i = 0; i < n; i++) {         if (flag[ar[i] - 1] == 0 && ar[i] > 1)           flag[ar[i] - 1]++;         else if (flag[ar[i]] == 0)           flag[ar[i]]++;         else if (flag[ar[i] + 1] == 0)           flag[ar[i] + 1]++;       }       int cnt = 0;       for (i = 1; i <= 150001; i++)         if (flag[i]) cnt++;       printf( %d n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2000000000;   const long double eps = 1e-07;   int n, m;   int a[2000];   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d %d , &x, &y);       ++a[x];       ++a[y];     }     for (int i = 0; i < n; ++i)       if (a[i + 1] == 0) {         printf( %d n , n - 1);         for (int j = 1; j <= n; ++j) {           if (j != (i + 1)) printf( %d %d n , i + 1, j);         }         return 0;       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, L, a;   int t[int(1e5 + 9)], l[int(1e5 + 9)];   int ans;   int main() {     cin >> n >> L >> a;     for (int i = 0; i < n; i++) {       cin >> t[i] >> l[i];     }     for (int i = 0; i + 1 < n; i++) {       int st = t[i] + l[i];       int en = t[i + 1];       if (en - st >= a) {         ans += (en - st) / a;       }     }     if (t[0] >= a) ans += t[0] / a;     if (L - (t[n - 1] + l[n - 1]) >= a) ans += (L - (t[n - 1] + l[n - 1])) / a;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void _print(long long t) { cerr << t; }   void _print(string t) { cerr << t; }   void _print(char t) { cerr << t; }   void _print(long double t) { cerr << t; }   void _print(double t) { cerr << t; }   void _print(unsigned long long t) { cerr << t; }   template <class T, class V>   void _print(pair<T, V> p);   template <class T>   void _print(vector<T> v);   template <class T>   void _print(set<T> v);   template <class T, class V>   void _print(map<T, V> v);   template <class T>   void _print(multiset<T> v);   template <class T, class V>   void _print(pair<T, V> p) {     cerr <<  { ;     _print(p.first);     cerr <<  , ;     _print(p.second);     cerr <<  } ;   }   template <class T>   void _print(vector<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void _print(set<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void _print(multiset<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T, class V>   void _print(map<T, V> v) {     cerr <<  [  ;     for (auto i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   long long n = 0;   const long long mod = 1e9 + 7;   long long tt = 0;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long power(long long x, long long y, long long m) {     if (y == 0) return 1;     if (y == 1) return x;     long long cur = power(x, y / 2, m);     cur = (cur % m * cur % m);     if ((y & 1)) cur = (cur % m * x % m) % m;     return cur;   }   long long modinv(long long x, long long m) { return power(x, m - 2, m) % m; }   void pre_solve() {}   void sol(long long tc) {     cin >> n;     vector<vector<long long> > arr(n, vector<long long>(3));     for (long long i = 0; i < n; i++) {       cin >> arr[i][0] >> arr[i][1];       arr[i][2] = i;     }     sort(arr.begin(), arr.end());     long long y = arr[0][1], idx = 0;     for (idx = 1; idx < n; idx++) {       if (arr[idx][0] > y)         break;       else {         y = max(arr[idx][1], y);       }     }     if (idx == n)       cout << -1 << endl;     else {       long long ans[n];       for (long long i = 0; i < idx; i++) {         ans[arr[i][2]] = 1;       }       for (long long i = idx; i < n; i++) ans[arr[i][2]] = 2;       for (long long i = 0; i < n; i++) cout << ans[i] <<    ;       cout << endl;     }     return;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long tc = 1;     cin >> tc;     while (tc--) {       tt++;       sol(tc);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MODULO = 1000000007;   void ftime() {}   void print_arr(int n, bool arr[]) {     for (int i = 0; i < n; i++) {       cout << arr[i] <<    ;     }     cout <<   n ;   }   void print_arr(int n, int arr[]) {     for (int i = 0; i < n; i++) {       cout << arr[i] <<    ;     }     cout <<   n ;   }   void print_vec(vector<int> &vec) {     for (auto e : vec) {       cout << e <<    ;     }     cout <<   n ;   }   void print_vec(vector<long long> &vec) {     for (auto e : vec) {       cout << e <<    ;     }     cout <<   n ;   }   void print_vec(vector<bool> &vec) {     for (auto e : vec) {       cout << e <<    ;     }     cout <<   n ;   }   static int speed_increase = []() {     std::ios::sync_with_stdio(false);     std::cin.tie(NULL);     return 0;   }();   void print_map(unordered_map<long long, long long> &comp) {     for (auto e : comp) {       cout << e.first <<   :   << e.second <<   n ;     }     cout <<   n ;   }   vector<long long> fdp(1000, -1);   long long fact(long long val) {     if (fdp[val] > 0) {       return fdp[val];     }     if (val < 2) return 1;     fdp[val] = (val * fact(val - 1)) % MODULO;     return fdp[val];   }   long long modexp(long long base, long long exponent) {     long long res = 1;     base = (base % MODULO + MODULO) % MODULO;     while (exponent) {       if (exponent & 1) res = (res * base) % MODULO;       exponent >>= 1;       base = (base * base) % MODULO;     }     return res;   }   int main() {     int n, m;     cin >> n >> m;     vector<long long> vals(m + 1, 0);     vector<long long> lights(m, 0);     for (auto &x : lights) {       cin >> x;     }     sort(lights.begin(), lights.end());     int last = 0;     int curr;     for (int i = 0; i < m; i++) {       curr = lights[i];       vals[i] = curr - last - 1;       last = curr;     }     vals[m] = n - last;     long long sum = accumulate(vals.begin(), vals.end(), 0ll);     long long ans = fact(sum) % MODULO;     for (int i = 0; i < m + 1; i++) {       if (vals[i] > 1) {         ans *= modexp(fact(vals[i]), MODULO - 2);         ans %= MODULO;       }       if (i != 0 && i != m && vals[i] > 1) {         ans *= modexp(2, vals[i] - 1);         ans %= MODULO;       }     }     cout << ans;     ftime();     return 0;   }


#include <bits/stdc++.h>   int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }   int main() {     int a[55], b[55], m, i, j, map[55], n, x, y, s;     while (~scanf( %d , &m)) {       for (i = 0; i < m - 1; i++) {         scanf( %d , &a[i]);       }       for (i = 0; i < m - 1; i++) {         scanf( %d , &b[i]);       }       j = 0;       for (i = 0; i < m; i++) {         scanf( %d , &s);         for (x = i; x < m - 1; x++) {           s += b[x];         }         for (y = 0; y < i; y++) {           s += a[y];         }         map[j++] = s;       }       qsort(map, j, sizeof(map[0]), cmp);       printf( %d n , map[0] + map[1]);     }   }


#include <bits/stdc++.h>   using namespace std;   string to_string(string s) { return     + s +    ; }   template <typename T>   string to_string(vector<T>);   template <typename U, typename V>   string to_string(pair<U, V> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename T>   string to_string(vector<T> v) {     string r =  [ ;     for (int i = 0; i < (int)v.size() - 1; i++) r += to_string(v[i]) +  ,  ;     if (!v.empty()) r += to_string(v.back());     return r +  ] ;   }   int main() {     int a, b, c, d, k, tc;     scanf( %d , &tc);     while (tc--) {       scanf( %d%d%d%d%d , &a, &b, &c, &d, &k);       int x = (a - 1) / c + 1;       int y = (b - 1) / d + 1;       if (x + y > k)         printf( -1 n );       else         printf( %d %d n , x, y);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, a;   vector<int> v1, v2;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a;       v1.push_back(a);       v2.push_back(a);     }     sort(v2.begin(), v2.end());     a = 0;     for (int i = 0; i < n; i++) {       a += v1[i] != v2[i];     }     (a < 3) ? cout <<  YES  << endl : cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 7, M = 20, MOD = 1000 * 1000 * 1000 + 7;   int sum(int a, int b) {     a += b;     if (a < 0)       a += MOD;     else if (a >= MOD)       a -= MOD;     return a;   }   void _sum(int &a, int b) { a = sum(a, b); }   int mul(int a, int b) { return 1LL * a * b % MOD; }   void _mul(int &a, int b) { a = mul(a, b); }   int power(int a, int b) {     int res = 1;     while (b) {       if (b & 1) _mul(res, a);       _mul(a, a);       b >>= 1;     }     return res;   }   int inv(int x) { return power(x, MOD - 2); }   int n, f[1 << N], p[N][N], dp[N][1 << M], pro[N][1 << N];   vector<int> vec[N];   void input() {     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < n; j++) {         cin >> p[i][j];         _mul(p[i][j], inv(100));       }       for (int mask = 0; mask < (1 << n); mask++) {         pro[i][mask] = 1;         for (int j = 0; j < n; j++)           if (mask & (1 << j))             _mul(pro[i][mask], p[i][j]);           else             _mul(pro[i][mask], sum(1, -p[i][j]));       }     }   }   void pre_pro() {     for (int i = 0; i < (1 << n); i++) {       f[i] = vec[__builtin_popcount(i)].size();       vec[__builtin_popcount(i)].push_back(i);     }   }   void act(int i, int mask, int nei) {     int nxt = 0;     vector<int> a, b;     for (int j = 0; j < vec[i].size(); j++)       if (mask & (1 << j)) a.push_back(vec[i][j]);     for (int j = 0; j < n; j++)       if (nei & (1 << j)) b.push_back(j);     for (int x : a)       for (int y : b)         if ((x & (1 << y)) == false) nxt |= (1 << f[x | (1 << y)]);     _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei]));   }   void solve() {     dp[0][1] = 1;     for (int i = 0; i < n; i++)       for (int mask = 0; mask < (1 << vec[i].size()); mask++)         if (dp[i][mask])           for (int nei = 0; nei < (1 << n); nei++) act(i, mask, nei);   }   int main() {     ios::sync_with_stdio(false), cin.tie(0);     input();     pre_pro();     solve();     cout << dp[n][1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 23;   const int MOD = 1e9 + 7;   const int N = 1000 + 10;   int brow[N], bcol[N];   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < (m); ++i) {       int x, y;       cin >> x >> y;       brow[x] = true;       bcol[y] = true;     }     int ans = 0;     for (int i = (2); i < (n); ++i) {       if (i == n - i + 1)         ans += !brow[i] || !bcol[i];       else {         ans += !brow[i];         ans += !bcol[i];       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int dp[100002];   int f(int x) {     if (dp[x] != -1) return dp[x];     bool nim[100] = {};     for (int i = 2; i * (i - 1) < 2 * x; i++) {       if ((2 * x - i * (i - 1)) % (2 * i) == 0) {         int tmp = 0, xx = (2 * x - i * (i - 1)) / (2 * i);         for (int j = 0; j < i; j++) tmp ^= f(xx + j);         nim[tmp] = 1;         if (tmp == 0 && x == n) {           printf( %d n , i);           return 1;         }       }     }     int ret = 0;     while (nim[ret]) ret++;     return dp[x] = ret;   }   int main() {     scanf( %d , &n);     memset(dp, -1, sizeof dp);     if (f(n))       ;     else       printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int test;     scanf( %d , &(test));     for (int t = 1; t <= test; t++) {       int n;       scanf( %d , &(n));       int a[200], cnt = 0;       int sum = 0;       for (int i = 0; i < n; i++) {         scanf( %d , &(a[i]));         if (a[i] == 0) {           a[i] = 1;           cnt++;         }         sum = sum + a[i];       }       if (sum != 0)         cout << cnt << endl;       else {         cout << cnt + 1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 500005;   const long long MOD = 1000000007;   void setIO(string name) {     ios_base::sync_with_stdio(0);     cin.tie(0);     freopen((name +  .in ).c_str(),  r , stdin);     freopen((name +  .out ).c_str(),  w , stdout);   }   int t;   int n;   void solve() {     cin >> n;     long long x = 0, even = 0, odd = 0;     long long sum = 0, improv = 0;     for (int i = 0; i < (n); i++) {       int a;       cin >> a;       if (i % 2 == 0) {         x -= a;         improv = max(improv, x - even);         even = min(x, even);         sum += a;       }       if (i % 2 == 1) {         x += a;         improv = max(improv, x - odd);         odd = min(x, odd);       }     }     cout << sum + improv << endl;   }   int main() {     cin >> t;     for (int i = 0; i < (t); i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, sol = 0, modul = 1000000007;   vector<long long> parent, depth;   long long where(long long pos) {     if (parent[pos] == -1) return pos;     long long a = depth[pos];     long long b = parent[pos];     long long c = where(parent[pos]);     parent[pos] = c;     depth[pos] = (a + depth[b]) % modul;     return c;   }   void uniona(long long a, long long x, long long b) {     long long wa = where(a);     if (wa == b) return;     sol = (sol + x + depth[a]) % modul;     depth[wa] = (x + depth[a]) % modul;     parent[wa] = b;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     parent.assign(n, -1);     depth.assign(n, 0);     for (long long i = 0; i < n; i++) {       long long k;       cin >> k;       for (long long j = 0; j < k; j++) {         long long a, b;         cin >> a >> b;         a--;         uniona(a, b, i);       }     }     cout << (sol + modul) % modul << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long ans = 0;     for (int i = (0); i < (n); ++i) {       long long t, T, x, cost;       cin >> t >> T >> x >> cost;       if (t >= T) {         ans += cost + m * x;         continue;       }       long long aux1 = cost;       if (m > (T - t)) aux1 += m * x;       long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;       aux2 *= cost;       ans += min(aux1, aux2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   const int N = 3e5 + 5;   const int INF = 1e9 + 7;   vector<int> graph[N];   multiset<int, greater<int>> st;   int n, a, b, dat[N], ans, cur;   void rem(int val) {     auto it = st.find(val);     st.erase(it);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     ans = INF;     cin >> n;     for (int i = (0); i < (n); i++) {       cin >> dat[i];       st.insert(dat[i]);     }     for (int i = (0); i < (n - 1); i++) {       cin >> a >> b;       --a;       --b;       graph[a].push_back(b);       graph[b].push_back(a);     }     for (int i = (0); i < (n); i++) {       rem(dat[i]);       for (auto it : graph[i]) rem(dat[it]);       cur = -INF;       cur = max(cur, dat[i]);       if (!st.empty()) cur = max(cur, *st.begin() + 2);       st.insert(dat[i]);       for (auto it : graph[i]) {         st.insert(dat[it]);         cur = max(cur, dat[it] + 1);       }       ans = min(ans, cur);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   bool fort[100007];   vector<int> v[100007];   struct tuhla {     int vertex;     double val;   };   double f[100007];   bool used[100007];   double bestval;   int id;   bool operator<(struct tuhla p1, struct tuhla p2) { return (p1.val > p2.val); }   priority_queue<struct tuhla> q;   void calc() {     int i, j;     for (i = 1; i <= n; i++) {       if (fort[i] == true) {         used[i] = true;       } else {         used[i] = false;       }     }     for (i = 1; i <= n; i++) {       if (fort[i] == true) {         continue;       }       int sz = v[i].size();       double br = 0.00000;       for (j = 0; j < sz; j++) {         if (fort[v[i][j]] == false) {           br += 1.000000;         }       }       f[i] = (br / sz);       struct tuhla u;       u.vertex = i;       u.val = f[i];       q.push(u);     }     bestval = -1.00000000;     id = -1;     int tot = 0;     while (q.empty() == false) {       struct tuhla cur = q.top();       q.pop();       if (fort[cur.vertex] == true || used[cur.vertex] == true) {         continue;       }       if (cur.val > f[cur.vertex] + 0.0000001) {         continue;       }       if (cur.val < f[cur.vertex] - 0.0000001) {         continue;       }       if (bestval < cur.val) {         bestval = cur.val;         id = tot;       }       tot++;       used[cur.vertex] = true;       int sz = v[cur.vertex].size();       for (i = 0; i < sz; i++) {         if (used[v[cur.vertex][i]] == false) {           struct tuhla u;           f[v[cur.vertex][i]] =               f[v[cur.vertex][i]] - (1.00000 / (v[v[cur.vertex][i]].size()));           u.vertex = v[cur.vertex][i];           u.val = f[v[cur.vertex][i]];           q.push(u);         }       }     }   }   void recreate(int br) {     int i, j;     for (i = 1; i <= n; i++) {       if (fort[i] == true) {         used[i] = true;       } else {         used[i] = false;       }     }     for (i = 1; i <= n; i++) {       int sz = v[i].size();       double br = 0.00000;       for (j = 0; j < sz; j++) {         if (fort[v[i][j]] == false) {           br += 1.000000;         }       }       f[i] = (br / sz);       struct tuhla u;       u.vertex = i;       u.val = f[i];       q.push(u);     }     int tot = 0;     while (q.empty() == false) {       struct tuhla cur = q.top();       q.pop();       if (fort[cur.vertex] == true || used[cur.vertex] == true) {         continue;       }       if (cur.val > f[cur.vertex] + 0.0000001) {         continue;       }       if (cur.val < f[cur.vertex] - 0.0000001) {         continue;       }       if (br == 0) {         break;       }       br--;       used[cur.vertex] = true;       int sz = v[cur.vertex].size();       for (i = 0; i < sz; i++) {         if (used[v[cur.vertex][i]] == false) {           struct tuhla u;           f[v[cur.vertex][i]] =               f[v[cur.vertex][i]] - (1.00000 / (v[v[cur.vertex][i]].size()));           u.vertex = v[cur.vertex][i];           u.val = f[v[cur.vertex][i]];           q.push(u);         }       }     }   }   void input() {     scanf( %d%d%d , &n, &m, &k);     int i;     for (i = 1; i <= k; i++) {       int x;       scanf( %d , &x);       fort[x] = true;     }     for (i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       v[x].push_back(y);       v[y].push_back(x);     }   }   void solve() {     calc();     recreate(id);     vector<int> ret;     int i;     for (i = 1; i <= n; i++) {       if (used[i] == false) {         ret.push_back(i);       }     }     int sz = ret.size();     printf( %d n , sz);     for (i = 0; i < sz; i++) {       printf( %d , ret[i]);       if (i == sz - 1) {         printf(  n );       } else {         printf(   );       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int k;     cin >> k;     int x;     cin >> x;     int* a;     a = new int[n + 1];     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int index;     index = n;     while (k > 0) {       a[index] = x;       index--;       k--;     }     int sum;     sum = 0;     for (int i = 1; i <= n; i++) {       sum = sum + a[i];     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     vector<int> V;     int px = 0;     while (px < n) {       if (a[px] == 1) {         if (px < n && a[px + 1] == 1) {           px += 2;           continue;         }         V.push_back(px);       }       px++;     }     px = 0;     cout << (n - V.size()) <<   n ;     for (int i = 0; i < n; i++) {       while (px < (V.size()) && V[px] < i) px++;       if (px < (V.size()) && V[px] == i) continue;       cout << a[i] <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t) {       t--;       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long a[4];     for (long long i = 0; i < 4; i++) cin >> a[i];     sort(a, a + 4);     if ((a[3] + a[0] == a[2] + a[1]) || (a[3] == a[0] + a[1] + a[2]))       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string str[51], temp;   int main() {     int t, f, a;     int cnt = 0, ans = 0x3f3f3f3f;     scanf( %d , &t);     getchar();     for (int i = 0; i < t; i++) {       cin >> str[i];     }     for (int i = 0; i < t; i++) {       cnt = 0;       for (int j = 0; j < t; j++) {         temp = str[j] + str[j];         if (temp.find(str[i]) == string::npos) {           cout << -1 << endl;           return 0;         }         cnt += temp.find(str[i]);       }       ans = min(ans, cnt);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 2e5 + 5;   vector<int> adj[Maxn];   int sz[Maxn];   int n;   long long ret, ans;   void dfs_init(int u, int f) {     sz[u] = 1;     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (v == f) continue;       dfs_init(v, u);       sz[u] += sz[v];     }     ret += sz[u];   }   void dfs(int u, int f, long long ret) {     ans = max(ans, ret);     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (v == f) continue;       dfs(v, u, ret + n - 2 * sz[v]);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     ans = ret = 0;     dfs_init(1, 0);     dfs(1, 0, ret);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   long long int sum[N << 2][11], sum2[11], a[N];   int lazy[N << 2][11], vis[11];   void pushdown(int rt) {     for (int i = 0; i < 10; i++)       vis[i] = lazy[rt << 1][i], sum2[i] = sum[rt << 1][i];     for (int i = 0; i < 10; i++)       if (lazy[rt][i] != i) {         for (int j = 0; j < 10; j++)           if (lazy[rt << 1][j] == i) vis[j] = lazy[rt][i];         sum2[lazy[rt][i]] += sum[rt << 1][i];         sum2[i] -= sum[rt << 1][i];       }     for (int i = 0; i < 10; i++)       sum[rt << 1][i] = sum2[i], lazy[rt << 1][i] = vis[i];     for (int i = 0; i < 10; i++)       vis[i] = lazy[rt << 1 | 1][i], sum2[i] = sum[rt << 1 | 1][i];     for (int i = 0; i < 10; i++)       if (lazy[rt][i] != i) {         for (int j = 0; j < 10; j++)           if (lazy[rt << 1 | 1][j] == i) vis[j] = lazy[rt][i];         sum2[lazy[rt][i]] += sum[rt << 1 | 1][i];         sum2[i] -= sum[rt << 1 | 1][i];       }     for (int i = 0; i < 10; i++)       sum[rt << 1 | 1][i] = sum2[i], lazy[rt << 1 | 1][i] = vis[i];     for (int i = 0; i < 10; i++) lazy[rt][i] = i;   }   void pushup(int rt) {     for (int i = 0; i < 10; i++)       sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][i];   }   void build(int rt, int l, int r) {     for (int i = 0; i < 10; i++) sum[rt][i] = 0;     for (int i = 0; i < 10; i++) lazy[rt][i] = i;     if (l == r) {       for (long long int t = 1; a[l]; a[l] /= 10, t *= 10)         sum[rt][a[l] % 10] += t;       return;     }     int m = r + l >> 1;     build(rt << 1, l, m);     build(rt << 1 | 1, m + 1, r);     pushup(rt);   }   void update(int rt, int l, int r, int L, int R, int x, int y) {     if (L <= l && r <= R) {       for (int i = 0; i < 10; i++)         if (lazy[rt][i] == x) {           sum[rt][y] += sum[rt][x];           sum[rt][x] = 0;           lazy[rt][i] = y;         }       return;     }     pushdown(rt);     int m = r + l >> 1;     if (L <= m) update(rt << 1, l, m, L, R, x, y);     if (R > m) update(rt << 1 | 1, m + 1, r, L, R, x, y);     pushup(rt);   }   long long int query(int rt, int l, int r, int L, int R) {     if (L <= l && r <= R) {       long long int ans = 0;       for (long long int i = 1; i < 10; i++) ans += sum[rt][i] * i;       return ans;     }     pushdown(rt);     int m = r + l >> 1;     long long int ans = 0;     if (L <= m) ans += query(rt << 1, l, m, L, R);     if (R > m) ans += query(rt << 1 | 1, m + 1, r, L, R);     pushup(rt);     return ans;   }   int n, m;   int main() {     while (~scanf( %d%d , &n, &m)) {       for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);       build(1, 1, n);       int op, l, r, x, y;       for (int i = 1; i <= m; i++) {         scanf( %d%d%d , &op, &l, &r);         if (1 == op) {           scanf( %d%d , &x, &y);           if (x == y) continue;           update(1, 1, n, l, r, x, y);         } else           printf( %lld n , query(1, 1, n, l, r));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long labs(long long a) { return a < 0 ? (-a) : a; }   vector<pair<long long, long long> > v;   vector<long long> c;   vector<bool> was;   int main() {     int n, k;     cin >> n >> k;     long long b;     cin >> b;     v.resize(n);     c.resize(n);     for (int(i) = 0; (i) < (n); (i)++) {       cin >> c[i];       v[i].first = c[i];       v[i].second = i;     }     v.pop_back();     sort((v).begin(), (v).end());     reverse((v).begin(), (v).end());     was.assign(n, false);     long long sum = 0;     for (int(i) = 0; (i) < (k - 1); (i)++) {       was[v[i].second] = true;       sum += v[i].first;     }     int res = n - 1;     for (int(i) = 0; (i) < (n); (i)++) {       if (was[i]) {         if (sum + v[k - 1].first > b) {           res = min(i, res);         }       } else {         if (sum + c[i] > b) {           res = min(i, res);         }       }     }     cout << res + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int a[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     int lim = n / 2 - 1, res = 1e9 + 7;     for (int i = 0; i <= lim; i++) {       res = min(res, a[n - (lim - i)] - a[i + 1]);     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     long long x, y;     cin >> x >> y;     long long n;     cin >> n;     long long ans;     switch (n % 6) {       case 1:         ans = x;         break;       case 2:         ans = y;         break;       case 3:         ans = y - x;         break;       case 4:         ans = -x;         break;       case 5:         ans = -y;         break;       case 0:         ans = x - y;         break;     }     cout << (ans + 2 * mod) % mod;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300000;   const long long INF = (1 << 30) - 1;   int n;   long long a[N + 9], sum[N + 9];   void into() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &a[i]);       sum[i] = a[i] + sum[i - 1];     }   }   int ans;   void Get_ans(int l, int r) {     if (l <= 0 || r > n) return;     for (; 2333;) {       if ((a[l] ^ a[r]) == sum[r - 1] - sum[l]) ++ans;       int le = 1, ri = n, len = n + 1;       for (int mid; le <= ri;) {         mid = le + ri >> 1;         int nl = l - mid, nr = r + mid;         if (nl <= 0 || nr > n) {           len = mid;           ri = mid - 1;           continue;         }         sum[nr] - sum[nl - 1] - sum[r - 1] + sum[l] >= sum[r - 1] - sum[l]             ? (len = mid, ri = mid - 1)             : le = mid + 1;       }       l -= len;       r += len;       if (l <= 0 || r > n) return;       if (sum[r - 1] - sum[l] > INF) return;     }   }   void Get_ans() {     for (int i = 1; i <= n; ++i) Get_ans(i - 1, i + 1);     for (int i = 1; i < n; ++i) Get_ans(i - 1, i + 2);   }   void work() { Get_ans(); }   void outo() { printf( %d n , ans); }   int main() {     int T = 1;     for (; T--;) {       into();       work();       outo();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100000];   vector<int> p;   long long fact[100001];   long long inv[100001];   long long finv[100001];   void init(int n) {     int i;     fact[0] = 1;     for (i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % 1000000007;     inv[0] = 1;     inv[1] = 1;     for (i = 2; i <= n; i++)       inv[i] = 1000000007 - (1000000007 / i) * inv[1000000007 % i] % 1000000007;     finv[0] = 1;     for (i = 1; i <= n; i++) finv[i] = finv[i - 1] * inv[i] % 1000000007;   }   long long C(int n, int k) {     return fact[n] * (finv[k] * finv[n - k] % 1000000007) % 1000000007;   }   long long pow(int x, int p) {     long long ans = 1;     for (int i = 0; i < p; i++) ans = ans * x % 1000000007;     return ans;   }   int next(int s, int t, int m) {     if (s < 0 || s >= m) return t;     if (t < 0 || t >= m) return s;     return a[p[s]] < a[p[t]] ? s : t;   }   int pos(int i, int m, int n) {     if (i < 0) return -1;     if (i >= m) return n;     return p[i];   }   long long solve(int cnt, int l, int r, int n) {     int s = cnt - 1, t = cnt + 1;     if (l <= pos(s, p.size(), n) || r >= pos(t, p.size(), n)) return 0;     long long ans = 1;     while (!(s == -1 && t == p.size())) {       int nxt = next(s, t, p.size());       int w = r - l + 1;       if (nxt == t) {         int d = p[nxt] - r - 1;         if (d > a[p[nxt]] - w - 1) return 0;         d = a[p[nxt]] - w - 1 - d;         l -= d;         if (l <= pos(s, p.size(), n)) return 0;         r = p[nxt];         ans = ans * C(a[p[nxt]] - w - 1, d) % 1000000007;         t++;       }       if (nxt == s) {         int d = l - p[nxt] - 1;         if (d > a[p[nxt]] - w - 1) return 0;         d = a[p[nxt]] - w - 1 - d;         r += d;         if (r >= pos(t, p.size(), n)) return 0;         l = p[nxt];         ans = ans * C(a[p[nxt]] - w - 1, d) % 1000000007;         s--;       }     }     ans = ans * C(n - (r - l + 1), l) % 1000000007;     return ans;   }   int main() {     int n, i;     scanf( %d , &n);     init(n);     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (a[i] > 0) p.push_back(i);     }     if (p.size() == 0) {       printf( %I64d n , pow(2, n - 1));       return 0;     }     if (p.size() == 1) {       if (a[p[0]] == 1)         printf( %I64d n , C(n - 1, p[0]));       else {         int t = a[p[0]] - 1;         long long vl = (p[0] >= t ? C(n - t - 1, p[0] - t) : 0);         long long vr = (n - p[0] - 1 >= t ? C(n - t - 1, p[0]) : 0);         printf( %I64d n , pow(2, t - 1) * (vl + vr) % 1000000007);       }       return 0;     }     int cnt = 0;     while (cnt + 1 < p.size() && a[p[cnt]] > a[p[cnt + 1]]) cnt++;     for (i = cnt; i + 1 < p.size(); i++)       if (a[p[i]] > a[p[i + 1]]) {         puts( 0 );         return 0;       }     int l, r;     long long ans;     if (a[p[cnt]] == 1) {       l = p[cnt];       r = p[cnt];       ans = solve(cnt, l, r, n);     } else {       l = p[cnt] - (a[p[cnt]] - 1);       r = p[cnt];       ans = solve(cnt, l, r, n);       l = p[cnt];       r = p[cnt] + (a[p[cnt]] - 1);       ans = (ans + solve(cnt, l, r, n)) % 1000000007;     }     printf( %I64d n , ans * pow(2, r - l - 1) % 1000000007);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> X[105], Y[105];   char s[105];   int main() {     int n, i, j;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %s , s);       for (j = 0; j < n; j++) {         if (s[j] ==  . ) {           X[i].push_back(j);           Y[j].push_back(i);         }       }     }     for (i = 0; i < n; i++) {       if (X[i].size() == 0) break;     }     if (i == n) {       for (i = 0; i < n; i++) {         printf( %d %d n , i + 1, X[i][0] + 1);       }       return 0;     }     for (i = 0; i < n; i++) {       if (Y[i].size() == 0) break;     }     if (i < n) {       printf( -1 n );       return 0;     }     for (i = 0; i < n; i++) {       printf( %d %d n , Y[i][0] + 1, i + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool umin(T& a, T b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool umax(T& a, T b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   int main() {     int t;     cin >> t;     while (t--) {       int n;       string s;       cin >> n >> s;       int a = n, b = n;       for (int i = 0; i < n; i++)         if (s[i] ==  > ) {           a = i;           break;         }       for (int i = n - 1; i >= 0; i--)         if (s[i] ==  < ) {           b = n - i - 1;           break;         }       printf( %d n , min(a, b));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long p1, p2, p3;       cin >> p1 >> p2 >> p3;       cout << (p1 + p2 + p3) / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int MOD = 1e9 + 7;   const long long M = 1e18;   long long qpow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       int n, q;       cin >> n >> q;       int a[n + 1];       map<int, int> mn, mx, ct;       for (int i = 1; i <= n; i++) {         cin >> a[i];         if (mn[a[i]] == 0) mn[a[i]] = i;         mx[a[i]] = i;         ct[a[i]]++;       }       int l = 1, r = 1;       int ans = 0;       for (int i = 1; i <= n; i++) {         r = max(r, mx[a[i]]);         if (i == r) {           int ans1 = N;           for (int j = l; j <= r; j++) {             ans1 = min(ans1, (r - l + 1) - ct[a[j]]);           }           ans += ans1;           l = r + 1;           r++;         }       }       cout << ans;     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   bool f, a[(int)1e6 + 9];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < (n == 3 ? 2 : (n >> 1)); i++) {       if (n % i == 0) {         for (int j = 0; j < i; j++) {           if (!a[j]) continue;           int k = j + i;           f = 1;           while (k != j) {             if (!a[k]) {               f = 0;               break;             }             k = (k + i) % n;           }           if (f) break;         }         if (f) break;       }     }     if (f)       printf( YES );     else       printf( NO );     return 0;   }


#include<bits/stdc++.h>   #define int long long   #define float double   #define itn int   #define f(j,n) for(int i=j;i<n;i++)   #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)   #define endl   n    #define Endl   n    #define ednl   n    #define Ednl   n    #define pb push_back   #define mp make_pair   #define mod 1000000000000000000   #define mset(arr, i) memset(arr, i, sizeof(arr))   using namespace std;       typedef vector<pair<int, int>> vpi;   typedef vector<vector<int>> vvi;   typedef pair<int, int> pii;   typedef vector<int> vi;   typedef map<int,int> mii;   typedef priority_queue<int> pqi;   typedef priority_queue<int, vector<int>, greater<int>> pqim;       long long binpow(long long a, long long b) { a %= mod; long long res = 1; while (b > 0){if (b & 1)res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}      int LCSubStr(string X, string  Y, int m, int n)   {        int LCSuff[m + 1][n + 1];       int result = 0;        for (int i = 0; i <= m; i++)       {           for (int j = 0; j <= n; j++)           {               if (i == 0 || j == 0)                   LCSuff[i][j] = 0;                   else if (X[i - 1] == Y[j - 1]) {                   LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;                   result = max(result, LCSuff[i][j]);               }               else                   LCSuff[i][j] = 0;           }       }       return result;   }      int32_t main()   {       int t1;       fastio;       cin>>t1;        //t1=1;       while(t1--)       {           string a, b;           cin>>a>>b;           int n1=a.length(), n2 = b.length();           int ans = LCSubStr(a, b, n1, n2);           cout<<n1+n2-2*ans<<endl;       }          }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int a[100000];     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a, a + n);     cout << min(a[n - 1] - a[1], a[n - 2] - a[0]) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   signed main(signed argc, const char* argv[]) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int cnt = 0;     for (int i = 0; cnt < n; ++i) {       cout << 3 <<     << i << endl;       ++cnt;       if (cnt == n) break;       if (i % 2) {         ++cnt;         cout << 0 <<     << i << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mn = 410, mm = 101000, inf = 1 << 30;   bool vis[mn];   int X[mm], Y[mm], du[mn];   int n, m, K, t, S, T, ans[mm], answer;   int dis[mm], pre[mm], Q[mm];   struct network {     int tt, la[mn], next[mm], y[mm], f[mm], co[mm];     void clear() {       tt = 1;       memset(la, 0, (T + 2) * 4);     }     void add(int i, int j, int u, int v) {       ++tt, next[tt] = la[i], la[i] = tt, y[tt] = j, f[tt] = u, co[tt] = v;       ++tt, next[tt] = la[j], la[j] = tt, y[tt] = i, f[tt] = 0, co[tt] = -v;     }   } e;   int spfa() {     memset(vis, 0, T + 2);     memset(pre, 0, (T + 2) * 4);     fill(dis + 1, dis + 1 + T, inf);     dis[S] = 0;     int st = 1, ed = 2;     Q[1] = S, vis[S] = 1;     while (st != ed) {       int r = Q[st];       for (int p = e.la[r], i; p; p = e.next[p]) {         if (!e.f[p]) continue;         i = e.y[p];         if (dis[r] + e.co[p] < dis[i]) {           dis[i] = dis[r] + e.co[p], pre[i] = p;           if (!vis[i]) {             Q[ed] = i, vis[i] = 1;             ed = (ed + 1) % (T + 2);           }         }       }       st = (st + 1) % (T + 2);       vis[r] = 0;     }     return dis[T];   }   void repair() {     for (int i = T, p; i != S; i = e.y[p ^ 1]) {       p = pre[i];       --e.f[p], ++e.f[p ^ 1];     }   }   int main() {     scanf( %d%d%d%d , &n, &m, &K, &t);     for (int i = 1; i <= K; ++i) {       scanf( %d%d , X + i, Y + i);       ++du[X[i]], ++du[Y[i] + n];     }     for (int i = 1; i <= n + m; ++i) answer += (du[i] % t) != 0;     S = n + m + 1, T = S + 1;     while (t) {       e.clear();       for (int i = 1; i <= K; ++i) {         if (ans[i]) continue;         e.add(X[i], Y[i] + n, 1, 0);       }       for (int i = 1; i <= n; ++i) {         e.add(S, i, du[i] / t, 0);         if (du[i] % t != 0) e.add(S, i, 1, 1);       }       for (int i = n + 1; i <= n + m; ++i) {         e.add(i, T, du[i] / t, 0);         if (du[i] % t != 0) e.add(i, T, 1, 1);       }       while (spfa() < inf) repair();       int tot = 0;       for (int i = 1; i <= K; ++i) {         if (ans[i]) continue;         ++tot;         if (e.f[tot * 2] == 0) {           ans[i] = t;           --du[X[i]], --du[Y[i] + n];         }       }       --t;     }     printf( %d n , answer);     for (int i = 1; i <= K - 1; ++i) printf( %d  , ans[i]);     printf( %d n , ans[K]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool debug = 0;   int n, m, k;   int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};   string direc =  RDLU ;   long long ln, lk, lm;   char s[105][105];   int cal(int x) { return x * (x - 1) / 2; }   void fmain() {     scanf( %d , &n);     for (int(i) = 1; (i) <= (int)(n); (i)++) scanf( %s , s[i] + 1);     for (int(i) = 1; (i) <= (int)(n); (i)++) {       m = 0;       for (int(j) = 1; (j) <= (int)(n); (j)++)         if (s[i][j] ==  C ) m++;       k += cal(m);     }     for (int(j) = 1; (j) <= (int)(n); (j)++) {       m = 0;       for (int(i) = 1; (i) <= (int)(n); (i)++)         if (s[i][j] ==  C ) m++;       k += cal(m);     }     printf( %d n , k);   }   int main() {     fmain();     return 0;   }


#include <algorithm>   #include <climits>   #include <cmath>   #include <cstdio>   #include <cstring>   #include <fstream>   #include <functional>   #include <iomanip>   #include <iostream>   #include <map>   #include <queue>   #include <set>   #include <stack>   #include <string>   #include <vector>   #define pb push_back   #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)   #define mem(a) memset(a, 0, sizeof(a))   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef long double ld;   typedef double db;   typedef pair<ll, ll> P;   const int maxn = 2e5 + 10;   int n, m;   vector<int> vec[maxn];   int dis[maxn];   bool vis[maxn];   int dp[maxn];   void bfs()   {       queue<P> q;       dis[1] = 0;       q.push(P{1, 0});       while (q.size())       {           P p = q.front();           q.pop();           int po = p.first;           int d = p.second;           if (vis[po])               continue;           vis[po] = 1;           dis[po] = d;           for (auto v : vec[po])           {               q.push(P(v, d + 1));           }       }   }   void dfs(int x)   {       vis[x] = 1;       dp[x] = dis[x];       for(auto u:vec[x])       {           if(!vis[u]&& dis[x]<dis[u])               dfs(u);           if(dis[x]<dis[u])               dp[x] = min(dp[x], dp[u]);           else               dp[x] = min(dp[x], dis[u]);       }   }   void solve()   {       cin >> n >> m;       for (int i = 1; i <= n; i++)           vec[i].clear(), dis[i] = 0, vis[i] = 0, dp[i] = 1e7;       for (int i = 1; i <= m; i++)       {           int x, y;           cin >> x >> y;           vec[x].pb(y);       }       bfs();       for (int i = 1; i <= n; i++)           vis[i] = 0;       dfs(1);       for (int i = 1; i <= n;i++)           cout << dp[i] <<    ;       cout << endl;   }   int main()   {       IOS;       int t;       cin >> t;       while (t--)           solve();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int ar[n], i, b;       b = n;       for (i = 0; i < n; i++) cin >> ar[i];       sort(ar, ar + n);       for (i = 0; i < n; i++) {         if (ar[i] < b)           b--;         else           break;       }       cout << b <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string str, result =   , tmp;     map<string, int> array;     cin >> str;     for (long long i = 0; i < 10; i++) cin >> tmp, array[tmp] = i;     tmp =   ;     for (long long i = 0; i < 80; i++) {       tmp += str[i];       if ((i + 1) % 10 == 0) result += array[tmp] +  0 , tmp =   ;     }     cout << result << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   int32_t main() {     fast();     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       priority_queue<long long> b;       priority_queue<long long, vector<long long>, greater<long long>> a;       long long d;       long long sum = 0;       for (long long i = 0; i < n; i++) {         cin >> d;         a.push(d);         sum += d;       }       for (long long i = 0; i < n; i++) {         cin >> d;         b.push(d);       }       for (long long i = 0; i < k; i++) {         if (a.top() < b.top()) {           long long a1 = a.top();           long long b1 = b.top();           a.pop();           b.pop();           b.push(a1);           a.push(b1);           sum = sum - a1 + b1;         } else           break;       }       cout << sum << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int r, h;   int main() {     scanf( %d%d , &r, &h);     double tt = h + r * 1.0 / 2;     double re = tt / r - (int)(tt / r);     int ans;     if (fabs(re) < 0.00001) {       ans = (int)(tt / r) * 2;     } else {       ans = (int)(tt / r) * 2;       re = h + r - (int)(tt / r) * r;       double mm = re - sqrt(3.0) / 2 * r;       if (mm > 0.00001) ans++;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     long long int a, b, n;     while (t--) {       cin >> a >> b >> n;       n %= 3;       if (n == 0)         cout << a << endl;       else if (n == 1)         cout << b << endl;       else         cout << (a ^ b) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     while (q != 0) {       int n;       cin >> n;       int one = 0;       if (n == 1)         cout <<  FastestFinger  << endl;       else if (n == 2)         cout <<  Ashishgup  << endl;       else if (n % 2 != 0)         cout <<  Ashishgup  << endl;       else {         int t = n;         int count = 0, last = n;         set<int> s;         while (n != 1) {           for (int i = 2; i <= sqrt(n); i++) {             if ((n % i) == 0) {               if (i % 2 != 0) {                 s.insert(i);                 count++;               }               n = (n / i);             }           }           if (n == last) break;           last = n;         }         if (n % 2 != 0) {           s.insert(n);           count++;         }         for (auto a : s) {           t = (t / a);         }         if ((t == 2 && count != 1) || t == 1)           cout <<  Ashishgup  << endl;         else if (t == 2 && count == 1)           cout <<  FastestFinger  << endl;         else if (count != 0)           cout <<  Ashishgup  << endl;         else           cout <<  FastestFinger  << endl;       }       q--;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0, f = 0;     char ch = 0;     while (!isdigit(ch)) f |= (ch ==  - ), ch = getchar();     while (isdigit(ch)) (x *= 10) += (ch ^ 48), ch = getchar();     return f ? -x : x;   }   void Ot(int x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) Ot(x / 10);     putchar(x % 10 + 48);   }   void Print(int x, char til =   n ) {     Ot(x);     putchar(til);   }   int Max(int x, int y) { return x > y ? x : y; }   int Min(int x, int y) { return x < y ? x : y; }   int Abs(int x) { return x < 0 ? -x : x; }   vector<int> G, P;   int n, ans;   char Getch() {     char c = getchar();     while (c !=  .  && c !=  *  && c !=  P ) c = getchar();     return c;   }   bool check(int x) {     int t = 1, pos = G[t - 1];     for (register int i = 0; i < P.size(); i++) {       int way = P[i] - pos;       if (way > 0 && way > x) return 0;       while (1) {         int len = P[i] - pos;         if (1ll * way * len < 0) {           if (Abs(len) < Abs(way))             len = Abs(len) * 2 + Abs(way);           else             len = Abs(len) + Abs(way) * 2;         } else           len = Abs(len);         if (len <= x)           t++, pos = G[t - 1];         else           break;         if (t > G.size()) return 1;       }     }     return 0;   }   signed main() {     n = read();     for (register int i = 1; i <= n; i++) {       char ch = Getch();       if (ch ==  P )         P.push_back(i);       else if (ch ==  * )         G.push_back(i);     }     if (!G.size()) return Print(0), 0;     int L = 0, R = 2 * n;     while (L <= R) {       register int mid = (L + R) >> 1;       if (check(mid))         ans = mid, R = mid - 1;       else         L = mid + 1;     }     Print(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int N = 3e5 + 10;   const long long mod = 998244353;   const double PI = acos(-1);   inline int ab(int x) { return x < 0 ? -x : x; }   inline int mm(int x, int p) { return x >= p ? x - p : x < 0 ? x + p : x; }   int main() {     int n, ans = 0, now = 0, pre = INF;     scanf( %d , &n);     for (int i = 1, x; i <= n; ++i) {       scanf( %d , &x);       if (x > pre * 2) {         ans = max(ans, now);         pre = x;         now = 1;       } else {         now++;         pre = x;       }     }     ans = max(ans, now);     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> graph[(int)2e5 + 5];   int visited[(int)2e5 + 5];   vector<int64_t> values;   int64_t dfs(int node, int depth) {     if (visited[node] == 1) return 0;     visited[node] = 1;     int64_t child = 0;     for (int i = 0; i < graph[node].size(); i++) {       if (visited[graph[node][i]] == 0) child += dfs(graph[node][i], depth + 1);     }     values.push_back(depth - (int64_t)child);     return child + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int t = 1;     while (t--) {       int total_city, num_of_industry, u, v;       cin >> total_city >> num_of_industry;       for (int i = 1; i < total_city; i++) {         cin >> u >> v;         graph[u].push_back(v);         graph[v].push_back(u);       }       dfs(1, 0);       sort(values.begin(), values.end(), greater<int>());       int64_t sum = 0;       for (int i = 0; i < num_of_industry; i++) {         sum += values[i];       }       cout << sum;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, t;     cin >> n >> k >> t;     cout << (k >= t ? t : (n >= t ? k : k - (t - n)));   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1024;   int dp[N];   int n, k;   vector<int> ans;   int f(int i) {     if (i >= n) return 0;     if (i + k >= n) return 1e9;     if (dp[i] != -1) return dp[i];     return dp[i] = f(i + 2 * k + 1) + 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     memset(dp, -1, sizeof dp);     int r = 1e9, best = -1;     for (int i = 0, ThxDem = k + 1; i < ThxDem; ++i)       if (r > f(i + k + 1) + 1) {         r = f(i + k + 1) + 1;         best = i;       }     while (best < n) {       ans.push_back(best + 1);       best += 2 * k + 1;     }     assert(((int)(ans).size()) == r);     cout << r <<   n ;     for (int x : ans) cout << x <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int a, int b) {     if (a > b) {       return true;     } else {       return false;     }   }   int main() {     int n, k, le[26], cnt = 1;     for (int i = 0; i < 26; i++) {       le[i] = 0;     }     char c, ch;     cin >> n >> k;     getchar();     cin >> ch;     if (n == k & n == 1) {       cout << 1;       return 0;     }     for (int i = 0; i < n - 1; i++) {       cin >> c;       if (c == ch) {         cnt++;       }       if (c != ch || i == n - 2) {         le[ch -  a ] += cnt / k;         cnt = 1;         ch = c;       }     }     sort(le, le + 26, cmp);     cout << le[0];   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = 2e5 + 5;   struct node {     int u, v, ne;   } e[maxn << 1];   int n;   int head[maxn], len;   int ve[maxn], b[maxn];   void add(int u, int v) {     e[len].u = u;     e[len].v = v;     e[len].ne = head[u];     head[u] = len++;   }   void dfs(int x, int p, int k) {     ve[x] = 1;     b[x] = k;     for (int i = head[x]; ~i; i = e[i].ne) {       int v = e[i].v;       if (v == p) continue;       dfs(v, x, k ^ 1);       ve[x] += ve[v];     }     return;   }   int main() {     memset(head, -1, sizeof(head));     cin >> n;     for (int i = 1, x, y; i < n; i++) {       scanf( %d %d , &x, &y);       add(x, y);       add(y, x);     }     dfs(1, 0, 0);     long long ans = 0, sum = 0;     for (int i = 1; i <= n; i++) {       ans += 1ll * ve[i] * (n - ve[i]);       sum += b[i];     }     ans += sum * (n - sum);     cout << ans / 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0;     char ch = getchar();     bool f = 0;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = 1;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return f ? -x : x;   }   void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writeln(long long x) {     write(x);     puts(  );   }   void writep(long long x) {     write(x);     putchar(   );   }   const int N = 1000005;   int n, cnt;   struct info {     long long x, y, z;     int id;   } a[N], b[N];   bool cmp(info a, info b) {     if (a.x != b.x) return a.x < b.x;     if (a.y != b.y) return a.y < b.y;     return a.z < b.z;   }   void solve(bool d) {     cnt = 0;     sort(a + 1, a + n + 1, cmp);     for (int i = 1, j = 1; i <= n; i = j + 1) {       j = i;       if (d) {         while (j + 1 <= n && a[j + 1].x == a[j].x && a[j + 1].y == a[j].y) j++;       } else         while (j + 1 <= n && a[j + 1].x == a[j].x) j++;       for (int k = i; k <= j; k += 2) {         if (k + 1 > j) break;         writep(a[k].id);         writeln(a[k + 1].id);       }       if ((j - i + 1) % 2) b[++cnt] = a[j];     }     if (cnt == 0) exit(0);     n = cnt;     for (int i = (int)(1); i <= (int)(n); i++) a[i] = b[i];   }   int main() {     n = read();     for (int i = (int)(1); i <= (int)(n); i++)       a[i].x = read(), a[i].y = read(), a[i].z = read(), a[i].id = i;     solve(1);     solve(0);     for (int i = 1; i <= n; i += 2) writep(a[i].id), writeln(a[i + 1].id);   }


#include <bits/stdc++.h>   using namespace std;   using ll = int_fast64_t;   constexpr int mod = 1e9 + 7;   constexpr int inf = (1 << 30) - 1;   constexpr ll infll = (1LL << 61) - 1;   #pragma GCC target( avx2 )   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   int main() {     ios::sync_with_stdio(false), cin.tie(nullptr);     int N;     cin >> N;     map<pair<int, int>, int> mp;     vector<pair<int, int>> v(N);     for (int i = 0; i < N; i++) {       cin >> v[i].first >> v[i].second;       if (mp[v[i]] != 0) {         cout << mp[v[i]] <<     << i + 1 <<   n ;         return 0;       }       mp[v[i]] = i + 1;       v[i].second *= -1;     }     sort(v.begin(), v.end());     set<int> st;     int ans1 = -1, ans2 = -1;     for (int i = 0; i < N; i++) {       v[i].second *= -1;       st.erase(st.begin(), st.lower_bound(v[i].first));       if (!st.empty() && st.lower_bound(v[i].second) != st.end()) {         ans1 = i;       }       st.insert(v[i].second);     }     if (ans1 == -1) {       cout << -1 <<     << -1 <<   n ;       return 0;     }     for (int i = 0; i < N; i++) {       if (i == ans1) continue;       if (v[i].first <= v[ans1].first && v[ans1].second <= v[i].second) ans2 = i;     }     cout << mp[v[ans1]] <<     << mp[v[ans2]] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3010;   long long a[N], b[N], n, m;   long long sa = 0, sb = 0, bestSa, bestSb;   pair<int, int> ansOne, ansTwo;   vector<long long> va, vb;   void add(long long *arr, int size, vector<long long> &v) {     for (int i = 0; i < size; ++i)       for (int j = i + 1; j < size; ++j) v.push_back(arr[i] + arr[j]);   }   long long one() {     long long ba = sa;     long long bb = sb;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         long long nsa = sa - a[i] + b[j], nsb = sb - b[j] + a[i];         if (llabs(nsa - nsb) < llabs(ba - bb)) {           ba = nsa;           bb = nsb;           ansOne = pair<int, int>(i + 1, j + 1);         }       }     }     return llabs(ba - bb);   }   long long two() {     long long diff = llabs(sa - sb);     for (int i = 0; i < (int)va.size(); ++i) {       long long x = va[i];       long long y = x + ((sb - sa) / 2);       int at = lower_bound(vb.begin(), vb.end(), y) - vb.begin();       for (int j = at - 1; j <= at; ++j) {         if (j >= 0 and j < (int)vb.size()) {           long long nsa = sa - x + vb[j];           long long nsb = sb - vb[j] + x;           if (llabs(nsa - nsb) < diff) {             diff = llabs(nsa - nsb);             ansTwo = pair<int, int>(x, vb[j]);           }         }       }     }     return diff;   }   pair<int, int> find(long long e, long long *arr, int size) {     for (int i = 0; i < size; ++i)       for (int j = i + 1; j < size; ++j)         if (arr[i] + arr[j] == e) return pair<int, int>(i + 1, j + 1);   }   void solve() {     long long d0 = llabs(sa - sb), d1 = one(), d2 = d1;     if (n > 1 and m > 1) {       d2 = two();     }     long long best = min(d0, min(d1, d2));     cout << best <<   n ;     if (best == d0) {       cout <<  0 n ;     } else if (best == d1) {       cout <<  1 n ;       cout << ansOne.first <<     << ansOne.second <<   n ;     } else {       vector<pair<int, int> > pans;       pans.push_back(find(ansTwo.first, a, n));       pans.push_back(find(ansTwo.second, b, m));       cout <<  2 n ;       cout << pans[0].first <<     << pans[1].first <<   n ;       cout << pans[0].second <<     << pans[1].second <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];       sa += a[i];     }     cin >> m;     for (int i = 0; i < m; ++i) {       cin >> b[i];       sb += b[i];     }     add(a, n, va);     add(b, m, vb);     sort(vb.begin(), vb.end());     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   const int mod = (int)1e9 + 7;   const int MAXN = (int)3e5 + 5;   vector<int> primes[1000005];   int x, p, k;   vector<pair<int, int> > v(1 << 7);   int calc(int a) {     int cnt = 1;     int num = 0;     int tmp = a;     a = 0;     for (int i = 1; i < (1 << primes[p].size()); i++) {       if (cnt * 2 == i) {         cnt *= 2;         num++;       }       v[i] = {v[i - cnt].first * primes[p][num], v[i - cnt].second + 1};       if (v[i].second % 2) {         a += tmp / v[i].first;       } else {         a -= tmp / v[i].first;       }     }     return tmp - a;   }   void solve() {     cin >> x >> p >> k;     k += calc(x);     int l = x + 1, r = 6e6 + 5e5 + 3e4 + 4e3 + 5e2;     while (l < r) {       int m = (l + r) / 2;       if (calc(m) >= k) {         r = m;       } else {         l = m + 1;       }     }     cout << l <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     for (int i = 2; i < 1000005; i++) {       if (!primes[i].size()) {         for (int j = i; j < 1000005; j += i) {           primes[j].push_back(i);         }       }     }     v[0] = {1, 0};     int t = 1;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     int t[n];     for (int i = 0; i < n; i++) {       cin >> t[i];     }     sort(t, t + n);     bool b = true;     int j = 0;     if (t[0] > 1) {       cout << 1;     } else {       while ((b == true) && (j < n)) {         if (t[j + 1] == t[j] + 1) {           j++;         } else {           b = false;         }       }       cout << t[j] + 1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e6 + 500;   const long double eps = (1e-12);   const int logn = 21;   const double Pi = acos(-1.0);   const long long pp = 998244353;   int lowbit(int x) { return x & (-x); }   const long long INF = 1e11 + 7;   long long dp[3][205][4];   long long a[MAXN];   long long pre[MAXN];   long long lst[MAXN];   signed main() {     long long n;     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     dp[0][1][2] = 1;     long long now = 0;     for (long long i = 1; i <= n; i++) {       now ^= 1;       for (long long j = 1; j <= 200; j++) {         pre[j] = lst[j] = 0;         for (long long k = 0; k <= 2; k++) dp[now][j][k] = 0;       }       for (long long k = 0; k <= 2; k++) pre[1] += dp[now ^ 1][1][k];       for (long long j = 2; j <= 200; j++) {         for (long long k = 0; k <= 2; k++) pre[j] += dp[now ^ 1][j][k];         pre[j] %= pp;         pre[j] += pre[j - 1];         pre[j] %= pp;       }       for (long long k = 1; k <= 2; k++) lst[200] += dp[now ^ 1][200][k];       for (long long j = 199; j > 0; j--) {         for (long long k = 1; k <= 2; k++) lst[j] += dp[now ^ 1][j][k];         lst[j] %= pp;         lst[j] += lst[j + 1];         lst[j] %= pp;       }       for (long long j = 1; j <= 200; j++) {         dp[now][j][0] += pre[j - 1];         dp[now][j][0] %= pp;         dp[now][j][1] +=             dp[now ^ 1][j][0] + dp[now ^ 1][j][1] + dp[now ^ 1][j][2];         dp[now][j][1] %= pp;         dp[now][j][2] += lst[j + 1];         dp[now][j][2] %= pp;       }       if (a[i] != -1) {         for (long long j = 1; j <= 200; j++) {           if (j == a[i]) continue;           for (long long k = 0; k <= 2; k++) {             dp[now][j][k] = 0;           }         }       }     }     long long ans = 0;     for (long long j = 1; j <= 200; j++) {       for (long long k = 1; k <= 2; k++) {         ans += dp[now][j][k];         ans %= pp;       }     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   int min(int a, int b) { return a < b ? a : b; }   int next[1 + (200 * (200 - 1) / 2)], jj[1 + (200 * (200 - 1) / 2)];   int link(int l, int j) {     static int l_ = 1;     next[l_] = l;     jj[l_] = j;     return l_++;   }   int ao[200], cc[200], dd[200], qq[3][200];   int solve(int n, int c_) {     static int dd_[200], head[3], cnt[3];     int i, j, l, k, t, c;     memcpy(dd_, dd, n * sizeof *dd);     memset(head, 0, sizeof head);     memset(cnt, 0, sizeof cnt);     for (i = 0; i < n; i++)       if (dd_[i] == 0) {         c = cc[i];         qq[c][head[c] + cnt[c]++] = i;       }     k = 0, t = 0;     while (k < n) {       while (cnt[c_]) {         i = qq[c_][cnt[c_]--, head[c_]++];         k++, t++;         for (l = ao[i]; l; l = next[l]) {           j = jj[l];           if (--dd_[j] == 0) {             c = cc[j];             qq[c][head[c] + cnt[c]++] = j;           }         }       }       c_ = (c_ + 1) % 3;       t++;     }     return t - 1;   }   int main() {     int n, i, j, k;     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &cc[i]), cc[i]--;     for (i = 0; i < n; i++) {       scanf( %d , &k);       while (k--) {         scanf( %d , &j), j--;         ao[j] = link(ao[j], i);         dd[i]++;       }     }     printf( %d n , min(solve(n, 0), min(solve(n, 1), solve(n, 2))));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000;   const long long INF = 1000000000000000;   int a[N];   int main() {     int t;     cin >> t;     for (int q = 0; q < t; q++) {       int n, k;       cin >> n >> k;       for (int j = 0; j < n; j++) {         cin >> a[j];       }       long long answer = -INF;       for (long long j = 1; j < n; j++) {         long long i = j - 1;         while (i >= 0 and (i + 1) * (j + 1) - k * a[j] > answer) {           long long result = (i + 1) * (j + 1) - k * (a[i] | a[j]);           answer = max(answer, result);           i--;         }       }       cout << answer << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pw[20][120], p[120], V, B, fA, fB, fC, ans;   int num[120], n, T, i, j;   void dfs_B(int l, long long x) {     if ((double)x * x > V) return;     if (l == n + 1) {       B = max(B, x);       return;     }     for (int i = 0; i <= num[l]; ++i) dfs_B(l + 1, x * pw[l][i]);   }   void dfs_A(int l, long long A) {     if ((double)A * A * A > V) return;     if (l == n + 1) {       long long x = (int)sqrt(V / A);       if (2 * A * x + V / A >= ans) return;       V /= A;       B = 0;       dfs_B(1, 1);       if (A * B + A * (V / B) + V < ans) {         ans = A * B + A * (V / B) + V;         fA = A;         fB = B;         fC = V / B;       }       V *= A;       return;     }     for (int i = num[l]; i >= 0; --i) {       num[l] -= i;       dfs_A(l + 1, A * pw[l][i]);       num[l] += i;     }   }   int main() {     for (scanf( %d , &T); T--;) {       scanf( %d , &n);       V = 1;       for (i = 1; i <= n; ++i) {         scanf( %I64d%d , &p[i], &num[i]);         pw[i][0] = 1;         for (j = 1; j <= num[i]; ++j) pw[i][j] = pw[i][j - 1] * p[i];         V *= pw[i][num[i]];       }       ans = 1ll << 62;       dfs_A(1, 1);       printf( %I64d %I64d %I64d %I64d n , ans * 2, fA, fB, fC);     }   }


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


#include <bits/stdc++.h>   using namespace std;   inline bool smax(long long &a, long long b) {     if (a < b) {       a = b;       return true;     } else       return false;   }   const long long MAXN = 1e5 + 100;   long long dp[MAXN][4], par[MAXN][4], choose[MAXN][4], id[MAXN];   pair<long long, long long> p[MAXN], s;   set<long long> sz;   map<long long, long long> cost, toid;   bool byP(long long x, long long y) {     return greater<pair<long long, long long> >()(p[x], p[y]);   }   pair<long long, long long> ans[MAXN];   long long ansN;   void get(long long i, long long j) {     if (i) get(i - 1, par[i][j]);     if (choose[i][j] != -1)       ans[ansN++] = pair<long long, long long>(i - 1, choose[i][j]);   }   int main() {     ios::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     memset(dp, -63, sizeof dp);     memset(choose, -1, sizeof choose);     long long m;     cin >> m;     for (long long i = 0; i < m; i++) {       cin >> s.second >> s.first;       sz.insert(s.first);       cost[s.first] = s.second;       toid[s.first] = i;     }     long long n;     cin >> n;     for (long long i = 0; i < n; i++) cin >> p[i].second >> p[i].first, id[i] = i;     p[n++] = pair<long long, long long>(-1, -1);     id[n - 1] = n - 1;     sort(id, id + n, byP);     sort(p, p + n, greater<pair<long long, long long> >());     long long tmp = 0;     if (sz.count(p[0].first)) tmp += 1;     if (sz.count(p[0].first + 1)) tmp += 2;     dp[0][tmp] = 0;     cerr << bitset<2>(tmp) << endl;     cost[-1] = 0;     for (long long i = 0; i < n - 1; i++)       for (long long j = 0; j < 4; j++)         if (dp[i][j] >= 0) {           long long cur = p[i].first;           vector<long long> t;           t.push_back(-1);           if (j & 1) t.push_back(cur);           if (j >> 1 & 1) t.push_back(cur + 1);           for (long long k = 0; k < t.size(); k++)             if (p[i].second >= cost[t[k]]) {               long long newmask = 0, next = p[i + 1].first;               if (sz.count(next) && t[k] != next) newmask += 1;               if (sz.count(next + 1) && t[k] != next + 1) newmask += 2;               if (next == cur) newmask &= j;               if (next == cur - 1 && j % 2 == 0) newmask -= newmask / 2 * 2;               if (smax(dp[i + 1][newmask], dp[i][j] + cost[t[k]])) {                 par[i + 1][newmask] = j;                 choose[i + 1][newmask] = t[k];               }             }         }     get(n - 1, 0);     cout << dp[n - 1][0] <<   n  << ansN <<   n ;     for (long long i = 0; i < ansN; i++)       cout << id[ans[i].first] + 1 <<     << toid[ans[i].second] + 1 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3010, Mo = 1000000007;   int tt, n, D;   int f[N][N], rev[N * 2], head[N], to[N], nxt[N];   inline int gi() {     int x = 0, o = 1;     char ch = getchar();     while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar();     if (ch ==  - ) o = -1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return x * o;   }   inline void dfs(int x) {     for (int i = 1; i <= n; i++) f[x][i] = 1;     for (int i = head[x]; i; i = nxt[i]) {       int y = to[i];       dfs(y);       for (int j = 1; j <= n; j++) f[x][j] = 1LL * f[x][j] * f[y][j] % Mo;     }     for (int i = 2; i <= n; i++) f[x][i] = (f[x][i - 1] + f[x][i]) % Mo;   }   inline int Pow(int x, int y) {     int ret = 1;     while (y) {       if (y & 1) ret = 1LL * ret * x % Mo;       x = 1LL * x * x % Mo, y >>= 1;     }     return ret;   }   int main() {     cin >> n >> D;     for (int i = 2, x; i <= n; i++)       to[++tt] = i, nxt[tt] = head[x = gi()], head[x] = tt;     dfs(1);     if (D <= n) return cout << f[1][D], 0;     int ans = 0;     for (int i = -n; i <= n; i++) rev[i + n] = Pow(i, Mo - 2);     for (int i = 0; i <= n; i++) {       int sum = f[1][i];       for (int j = 0; j <= n; j++)         if (i != j) sum = 1LL * sum * (D - j) % Mo * rev[i - j + n] % Mo;       (ans += sum) %= Mo;     }     cout << (ans + Mo) % Mo;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> g[300005];   long long int n, m, flag = 1;   map<pair<long long int, long long int>, long long int> ans;   long long int visit[300005];   long long int color[300005];   void dfs1(long long int src, long long int col) {     visit[src] = 1;     color[src] = col;     for (long long int i = 0; i < g[src].size(); i++) {       if (visit[g[src][i]] == 0) {         long long int x;         if (col == 0)           x = 1;         else           x = 0;         dfs1(g[src][i], x);       }     }   }   void dfs2(long long int src) {     visit[src] = 1;     for (long long int i = 0; i < g[src].size(); i++) {       if (color[g[src][i]] == color[src]) {         flag = 0;         break;       }       if (color[src] == 0) {         ans[{src, g[src][i]}] = 1;       } else {         ans[{src, g[src][i]}] = 0;       }       if (visit[g[src][i]] == 0) {         dfs2(g[src][i]);       }     }   }   int32_t main() {     cin >> n >> m;     pair<long long int, long long int> arr[m];     for (long long int i = 0; i < m; i++) {       long long int a, b;       cin >> a >> b;       a--;       b--;       arr[i].first = a;       arr[i].second = b;       g[a].push_back(b);       g[b].push_back(a);     }     dfs1(0, 0);     for (long long int i = 0; i < n; i++) visit[i] = false;     dfs2(0);     if (flag == 0)       cout <<  NO  << endl;     else {       cout <<  YES  << endl;       for (long long int i = 0; i < m; i++) {         cout << ans[{arr[i].first, arr[i].second}];       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 998244353;   double eps = 1e-12;   void solve() {     long long n, x;     cin >> n >> x;     int a;     map<int, int> mr;     map<int, int> mc;     for (int i = 0; i < n; i++) {       for (int j = 0; j < x; j++) {         cin >> a;         if (a) {           mr[i + 1]++;           mc[j + 1]++;         }       }     }     int k1 = n - mr.size(), k2 = x - mc.size();     int l = min(k1, k2);     if (l & 1)       cout <<  Ashish  << endl;     else       cout <<  Vivek  << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     long long s = 0, x = 1;     long long a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       s = s + a[i];     }     s = s + m;     sort(a, a + n);     int y = max((s / n + min(x, s % n)), a[n - 1]);     cout << y;     cout <<     << a[n - 1] + m << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 1;   int N, Q, nums[MAXN], ans[MAXN], BIT[MAXN + 5];   vector<pair<int, int> > queries[MAXN];   void upd(int i) {     i++;     while (i > 0) {       BIT[i]++;       i -= i & -i;     }   }   int get(int i) {     i++;     int ret = 0;     while (i < N + 5) {       ret += BIT[i];       i += i & -i;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> N >> Q;     for (int i = 1; i <= N; i++) {       cin >> nums[i];       nums[i] = i - nums[i];     }     for (int i = 0; i < Q; i++) {       int x, y;       cin >> x >> y;       if (x + y >= N) {         ans[i] = 0;         continue;       }       queries[N - y].push_back(make_pair(1 + x, i));     }     for (int i = 1; i <= N; i++) {       if (nums[i] >= 0 && get(1) >= nums[i]) {         int lo = 1;         int hi = i;         if (nums[i] == 0) lo = i;         while (lo < hi - 1) {           int m = (lo + hi) / 2;           if (get(m) >= nums[i]) {             lo = m;           } else {             hi = m;           }         }         upd(lo);       }       for (auto it : queries[i]) {         ans[it.second] = get(it.first);       }     }     for (int i = 0; i < Q; i++) {       cout << ans[i] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   double start_moment = 0;   double get_runtime() { return 1.0 * clock() / CLOCKS_PER_SEC; }   void reset_timer() { start_moment = get_runtime(); }   double timer_time() { return get_runtime() - start_moment; }   void runtime() { cout << fixed << setprecision(5) << get_runtime() << endl; }   template <class T>   void read(vector<T> &a, int n) {     T x;     a.clear();     for (int i = 0; i < n; i++) {       cin >> x;       a.push_back(x);     }   }   template <class T>   void write(vector<T> &a) {     for (T x : a) cout << x <<    ;     cout <<   n ;   }   const int N = 2003;   const int INF = 1000000000;   int T, len;   string second, t;   int cnt1[26], cnt2[26];   int have_s[26][N];   int have_t[26][N];   int get(int *have, int x) {     if (x < 0) return 0;     return have[x];   }   int get(int *have, int l, int r) { return get(have, r) - get(have, l - 1); }   int dp[N][N];   int go(int x, int y) {     if (dp[x][y] != INF) return dp[x][y];     if (second[x - 1] == t[y - 1]) dp[x][y] = min(dp[x][y], go(x - 1, y - 1));     dp[x][y] = min(dp[x][y], go(x - 1, y) + 1);     if (get(have_s[t[y - 1] -  a ], x, len) > get(have_t[t[y - 1] -  a ], y, len))       dp[x][y] = min(dp[x][y], go(x, y - 1));     return dp[x][y];   }   int main() {     ios_base::sync_with_stdio(0);     cin >> T;     while (T--) {       memset(cnt1, 0, sizeof(cnt1));       memset(cnt2, 0, sizeof(cnt2));       cin >> len;       cin >> second >> t;       for (char c : second) cnt1[c -  a ]++;       for (char c : t) cnt2[c -  a ]++;       bool bad = 0;       for (int i = 0; i < 26; i++)         if (cnt1[i] != cnt2[i]) bad = 1;       if (bad) {         cout << -1 <<   n ;         continue;         ;       }       for (int i = 0; i <= len; i++)         for (int j = 0; j <= len; j++) dp[i][j] = INF;       memset(have_s, 0, sizeof(have_s));       memset(have_t, 0, sizeof(have_t));       for (char c =  a ; c <=  z ; c++) {         for (int i = 0; i < len; i++) {           have_s[c -  a ][i] = (c == second[i]);           have_t[c -  a ][i] = (c == t[i]);         }         for (int i = 1; i < len; i++) {           have_s[c -  a ][i] += have_s[c -  a ][i - 1];           have_t[c -  a ][i] += have_t[c -  a ][i - 1];         }       }       for (int i = 0; i <= len; i++) dp[0][i] = 0;       cout << go(len, len) <<   n ;     }   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 1e5 + 5;   int m, q;   int val[maxn], sum1[maxn], sum2[maxn], las[maxn], pre[maxn], fl[maxn],       res[maxn];   struct no {     int le, ri, id;   } qu[maxn];   bool cmp(no n1, no n2) { return n1.ri < n2.ri; }   void solve() {     int i;     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d , &val[i]);       pre[i] = las[val[i]];       las[val[i]] = i;       if (i - pre[i] == pre[i] - pre[pre[i]]) {         fl[i] = fl[pre[i]];       } else {         fl[i] = pre[pre[i]];       }     }     scanf( %d , &q);     for (i = 1; i <= q; i++) {       scanf( %d%d , &qu[i].le, &qu[i].ri);       qu[i].id = i;     }     sort(qu + 1, qu + q + 1, cmp);     int j, k = 1;     for (i = 1; i <= m; i++) {       if (i == 10) {         i++;         i--;       }       for (j = i; j; j -= (j & (-j))) {         sum1[j]++;       }       for (j = pre[i]; j; j -= (j & (-j))) {         sum1[j]--;       }       for (j = fl[i]; j; j -= (j & (-j))) {         sum2[j]++;       }       for (j = fl[pre[i]]; j; j -= (j & (-j))) {         sum2[j]--;       }       while (qu[k].ri == i && k <= q) {         int su1 = 0, su2 = 0;         for (j = qu[k].le; j <= i; j += (j & (-j))) {           su1 += sum1[j];           su2 += sum2[j];         }         res[qu[k].id] = su1 + (su1 == su2);         k++;       }     }     for (i = 1; i <= q; i++) {       printf( %d n , res[i]);     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   #define MOD 998244353   using namespace std;      #define rep(i, a, b) for(int i = a; i < (b); ++i)   #define rrep(i, a, b) for(int i = (a-1); i >= (b); --i)   #define trav(a, x) for(auto& a : x)   #define all(x) begin(x), end(x)   #define sz(x) (int)(x).size()   typedef long long ll;   typedef pair<int, int> pii;   typedef vector<int> vi;      void solve() {    int n;    cin >> n;    vector<vector<int>> a(2*n, vector<int>(n));    vector<vector<set<int>>> S(n, vector<set<int>>(n));    rep(i,0,2*n) {     rep(j,0,n) {      cin >> a[i][j];      --a[i][j];      S[j][a[i][j]].insert(i);     }    }    ll ans = 1;    vector<bool> used(2*n, false);    vector<int> side(2*n);    int I = 0;    while (true) {     bool any = false;     int r;     int i = I;     while (!any) {      rep(j,0,n) {       if (sz(S[i][j]) == 1) {        any = true;        r = *S[i][j].begin();        break;       }      }      if (any)       break;      i = (i+1)%n;      if (i == I)       break;     }     I = i;     if (!any) {      rep(i,0,2*n)       if (!used[i]) {        r = i;        any = true;       }      if (!any)       break;      ans = (ans*2) % MOD;     }     used[r] = true;     side[r] = 0;     set<int> toRemove;     rep(l,0,n) {      for (int k : S[l][a[r][l]]) {       toRemove.insert(k);      }     }     for (int k : toRemove) {      used[k] = true;      side[k] = k == r;      rep(l,0,n)       S[l][a[k][l]].erase(k);     }    }    cout << ans << endl;    rep(i,0,2*n) {     if (side[i] == 1) {      cout << i+1 <<    ;     }    }    cout << endl;   }      int main() {    cin.sync_with_stdio(0); cin.tie(0);    cin.exceptions(cin.failbit);    int t;    cin >> t;    while (t--)     solve();   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     vector<long long int> v1;     vector<long long int> v2;     long long int n, mo = 0, x, y, f;     scanf( %lld , &n);     for (long long int i = 0; i < n; i++) {       scanf( %lld , &f);       v1.push_back(f);     }     for (long long int i = 0; i < n; ++i) {       v2.push_back(v1[i]);       if (i + 1 == n) break;       x = v1[i], y = v1[i + 1];       if (x < y) swap(x, y);       if (gcd(x, y) != 1) {         mo++;         v2.push_back(1);       }     }     printf( %lld n , mo);     for (long long int i = 0; i < v2.size(); ++i) printf( %lld  , v2[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d, n, m;   const int M = 2e5;   pair<int, int> gas[M + 7];   bool possible = true;   int pendingD, running;   int currentGas;   long long res = 0;   struct cmp {     bool operator()(int c1, int c2) {       if (gas[c1].second != gas[c2].second)         return gas[c1].second > gas[c2].second;       else         return gas[c1].first < gas[c2].first;     }   };   priority_queue<int, vector<int>, cmp> heap;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> d >> n >> m;     int r1, r2;     for (int i = 1; i <= m; i++) {       cin >> r1 >> r2;       gas[i] = {r1, r2};     }     sort(gas + 1, gas + m + 1);     gas[0] = {0, 0};     gas[m + 1] = {d, 0};     for (int i = 1; i <= m + 1; i++)       if (gas[i].first - gas[i - 1].first > n) {         possible = false;         break;       }     if (!possible) {       cout << -1;       exit(0);     }     pendingD = 0;     running = 1;     currentGas = 0;     while (pendingD <= m) {       for (int i = running; i <= m + 1; i++)         if (gas[i].first - gas[pendingD].first > n) {           running = i;           break;         } else {           heap.push(i);           if (gas[i].second < gas[pendingD].second) {             running = i + 1;             break;           }         }       while (heap.top() <= pendingD) heap.pop();       if (gas[heap.top()].second <= gas[pendingD].second) {         res += 1LL * gas[pendingD].second *                (gas[heap.top()].first - gas[pendingD].first - currentGas);         currentGas = 0;       } else {         res += 1LL * gas[pendingD].second * (n - currentGas);         currentGas = n - (gas[heap.top()].first - gas[pendingD].first);       }       pendingD = heap.top();       heap.pop();       running = max(running, pendingD + 1);     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, sum = 0;     int num[1000];     int x, y;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> num[i];       sum += num[i];     }     cin >> x >> y;     int cur = 0;     for (int i = 1; i <= n; i++) {       if (cur >= x && cur <= y && (sum - cur) >= x && (sum - cur) <= y) {         cout << i << endl;         return 0;       }       cur += num[i];     }     cout << 0 << endl;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int a, b;     scanf( %d%d , &a, &b);     int c[3] = {0, 0, 0};     for (int i = 1; i <= 6; ++i) {       int d1 = abs(i - a), d2 = abs(i - b);       if (d1 < d2) c[0]++;       if (d1 == d2) c[1]++;       if (d1 > d2) c[2]++;     }     for (int i = 0; i < 3; ++i) {       printf( %d  , c[i]);     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, const U &b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, const U &b) {     if (a < b) a = b;   }   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T1, class T2>   inline void gn(T1 &x1, T2 &x2) {     gn(x1), gn(x2);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   const int mod = 1000000007;   int pt[100100], p2[100100];   int sum[100100], dp[100100], mn[100100], mx[100100];   char s[100100], t[100100];   int calc(char *s, int n) {     int ans = 0, val = 0, pre = 0;     for (int i = 0; i < n; i++) {       int v = (val + 4LL * pt[n - i - 1]) % mod;       if (s[i] ==  7 ) {         ans = (ans + (long long)v * v % mod * (p2[n - i - 1] - 1) +                (long long)v *                    (2LL * sum[n - i - 1] - mn[n - i - 1] - mx[n - i - 1]) +                dp[n - i - 1]) %               mod;         ans = (ans + mod) % mod;         if (pre)           ans = (ans + (long long)(pre + mx[n - i]) * (v + mn[n - i - 1])) % mod;       }       if (s[i] ==  7 )         pre = (val + 4LL * pt[n - i - 1]) % mod;       else if (pre)         pre = (pre + 7LL * pt[n - i - 1]) % mod;       val = (val + (long long)(s[i] -  0 ) * pt[n - i - 1]) % mod;     }     ans = (ans + (long long)val * pre) % mod;     return ans;   }   int main() {     scanf( %s %s , s, t);     int n = strlen(s);     for (int i = pt[0] = p2[0] = 1; i <= n; i++)       pt[i] = 10LL * pt[i - 1] % mod, p2[i] = p2[i - 1] * 2 % mod;     for (int i = 1; i <= n; i++) {       dp[i] =           ((long long)pt[i - 1] * pt[i - 1] % mod * (4 * 4 + 7 * 7) % mod *                (p2[i - 1] - 1) +            11LL * pt[i - 1] % mod * (2LL * sum[i - 1] - mn[i - 1] - mx[i - 1]) +            2LL * dp[i - 1] +            (4LL * pt[i - 1] + mx[i - 1]) % mod *                (7LL * pt[i - 1] % mod + mn[i - 1])) %           mod;       dp[i] = (dp[i] + mod) % mod;       mn[i] = (10LL * mn[i - 1] + 4) % mod;       mx[i] = (10LL * mx[i - 1] + 7) % mod;       sum[i] = (2LL * sum[i - 1] + 11LL * pt[i - 1] % mod * p2[i - 1]) % mod;     }     cout << (calc(t, n) - calc(s, n) + mod) % mod << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int p[n];     for (int i = 0; i < n; i++) cin >> p[i];     sort(p, p + n);     map<string, int> mm;     string x;     for (int i = 0; i < m; ++i) {       cin >> x;       if (mm.find(x) == mm.end())         mm.insert(make_pair(x, 1));       else         mm[x]++;     }     vector<int> v;     for (auto i = mm.begin(); i != mm.end(); i++) {       v.push_back(i->second);     }     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     long long int sum = 0;     for (int i = 0; i < v.size(); i++) {       sum += v[i] * p[i];     }     cout << sum <<    ;     sum = 0;     for (int i = 0; i < v.size(); i++) {       sum += v[i] * p[n - 1 - i];     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   vector<vector<int> > adj(maxn), adj2(maxn);   vector<pair<int, int> > vs;   bool vis[maxn];   void dfs(int pv, int v) {     vis[v] = 1;     for (int i = 0; i < adj[v].size(); i++) {       int u = adj[v][i];       if (vis[u]) continue;       vs.push_back(make_pair(v, u));       adj2[pv].push_back(vs.size() - 1);       dfs(vs.size() - 1, u);     }   }   int main() {     int n, a, b;     cin >> n;     for (int i = 1; i <= n - 1; i++) {       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     memset(vis, 0, maxn);     vs.push_back(make_pair(-1, -1));     int s;     for (int i = 1; i <= n; i++)       if (adj[i].size() == 1) {         s = i;         break;       }     vis[s] = 1;     for (int i = 0; i < adj[s].size(); i++) {       int u = adj[s][i];       vs.push_back(make_pair(s, u));       dfs(vs.size() - 1, u);     }     cout << n - 1 << endl;     for (int i = 1; i <= n - 1; i++) {       cout <<  2   << vs[i].first <<     << vs[i].second << endl;     }     for (int i = 1; i <= n - 1; i++) {       for (int j = 0; j < adj2[i].size(); j++)         cout << i <<     << adj2[i][j] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long z[3000019], arr[3000019], pre[3000019];   void fnc(string s) {     memset(z, 0, sizeof(z));     long long l = 0, r = 0;     long long n = s.size();     for (long long i = 1; i <= n; i++) {       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] <= n && s[z[i]] == s[i + z[i]]) z[i]++;       if (i + z[i] - 1 > r) {         l = i;         r = i + z[i] - 1;       }     }     z[0] = n;     return;   }   signed main() {     std::ios::sync_with_stdio(false);     long long n, m, q, i, l, r;     string s, t, str;     cin >> n >> m >> q;     cin >> s >> t;     str = t +  $  + s;     fnc(str);     for (i = m + 1; i <= n + m; i++) arr[i - m] = (z[i] == m);     for (i = 1; i <= n; i++) pre[i] = arr[i] + pre[i - 1];     while (q--) {       cin >> l >> r;       if (r - l + 1 < m) {         cout << 0 << endl;         continue;       }       cout << pre[r - m + 1] - pre[l - 1] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int tc, cs = 1, n, k, a, b, c, d;     while (cin >> n >> k) {       cin >> a >> b >> c >> d;       if (k < n + 1 || n == 4) {         printf( %d n , -1);       } else {         printf( %d %d  , a, c);         for (int i = 1; i < n + 1; i++) {           if (i != a && i != b && i != c && i != d) {             printf( %d  , i);           }         }         printf( %d %d n , d, b);         printf( %d %d  , c, a);         for (int i = 1; i < n + 1; i++) {           if (i != a && i != b && i != c && i != d) {             printf( %d  , i);           }         }         printf( %d %d n , b, d);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void File() {     freopen( output.txt ,  w , stdout);     freopen( input.txt ,  r , stdin);   }   void fast() {     ios::sync_with_stdio(NULL);     cout.tie(NULL);     cin.tie(NULL);   }   const double pi = 2 * acos(0.0);   const int nn = 5e2 + 5;   const int oo = 0x3f3f3f3f;   int main() {     fast();     int n, ans = 0;     pair<int, int> arr[nn];     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;     for (int i = 0; i < n; i++) {       int cnt[4] = {};       for (int j = 0; j < n; j++) {         if (i == j) continue;         if (arr[i].first > arr[j].first && arr[i].second == arr[j].second)           cnt[0]++;         else if (arr[i].first < arr[j].first && arr[i].second == arr[j].second)           cnt[1]++;         else if (arr[i].first == arr[j].first && arr[i].second > arr[j].second)           cnt[2]++;         else if (arr[i].first == arr[j].first && arr[i].second < arr[j].second)           cnt[3]++;         if (cnt[0] && cnt[1] && cnt[2] && cnt[3]) {           ans++;           break;         }       }     }     cout << ans <<   n ;     return 0;   }