#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n constexpr int MAXN = 100005; n int SQRTN; n int n, k; n int t[MAXN], a[MAXN]; n ll p[MAXN]; n ll ans[MAXN]; n ll cur = 0; n int ind[MAXN], left_ind[MAXN], right_ind[MAXN]; n int freq[3 * MAXN]; n struct query { n   int l, r, i; n } queries[MAXN]; n bool cmp(const query& a, const query& b) { n   return a.l / SQRTN < b.l / SQRTN || n          (a.l / SQRTN == b.l / SQRTN and a.r < b.r); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , t + i); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n   } n   vector<ll> vals; n   vals.push_back(0LL); n   vals.push_back(k); n   vals.push_back(-k); n   p[0] = 0LL; n   for (int i = 1; i <= n; ++i) { n     if (t[i] == 1) n       p[i] = p[i - 1] + a[i]; n     else n       p[i] = p[i - 1] - a[i]; n     vals.push_back(p[i]); n     vals.push_back(p[i] - k); n     vals.push_back(p[i] + k); n   } n   sort(begin(vals), end(vals)); n   vals.erase(unique(begin(vals), end(vals)), end(vals)); n   for (int i = 0; i <= n; ++i) { n     ind[i] = lower_bound(begin(vals), end(vals), p[i]) - begin(vals); n     left_ind[i] = lower_bound(begin(vals), end(vals), p[i] - k) - begin(vals); n     right_ind[i] = lower_bound(begin(vals), end(vals), p[i] + k) - begin(vals); n   } n   SQRTN = sqrt(n); n   int q; n   int l, r; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     scanf( %d %d , &l, &r); n     --l; n     queries[i] = {l, r, i}; n   } n   sort(queries, queries + q, cmp); n   int left_ptr = 0; n   int right_ptr = -1; n   for (int i = 0; i < q; ++i) { n     while (right_ptr < queries[i].r) { n       ++right_ptr; n       cur += freq[left_ind[right_ptr]]; n       ++freq[ind[right_ptr]]; n     } n     while (right_ptr > queries[i].r) { n       --freq[ind[right_ptr]]; n       cur -= freq[left_ind[right_ptr]]; n       --right_ptr; n     } n     while (left_ptr < queries[i].l) { n       --freq[ind[left_ptr]]; n       cur -= freq[right_ind[left_ptr]]; n       ++left_ptr; n     } n     while (left_ptr > queries[i].l) { n       --left_ptr; n       cur += freq[right_ind[left_ptr]]; n       ++freq[ind[left_ptr]]; n     } n     ans[queries[i].i] = cur; n   } n   for (int i = 0; i < q; ++i) { n     printf( %lld n , ans[i]); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n const int maxn=2e5+7; n const int modn = 1e9+7; n int a[maxn],b[maxn]; n map<int,int> mp,mp2; n int binaryl(int value,int l,int r){ n     int res = l-1; n     while(r >= l){ n         int mid = (r+l) >> 1; n         if(b[mid] < value){ n             res = mid; n             l = mid+1; n         } n         else r = mid-1; n     } n     return res; n } n int binaryr(int value,int l,int r){ n     int res = r + 1; n     while(r >= l){ n         int mid = (r+l) >> 1; n         if(b[mid] > value){ n             res = mid; n             r = mid-1; n         } n         else l = mid+1; n     } n     return res; n } n int main(){ n     int T; n     scanf( %d ,&T); n     while(T--){ n         mp.clear(); n         mp2.clear(); n         int n,m; n         scanf( %d%d ,&n,&m); n         int midn = n+1,midm = m+1; n         for(int i = 1;i <= n;i++){ n             scanf( %d ,&a[i]); n             mp[a[i]] = 1; n             if(midn == n+1 && a[i] > 0)midn = i; n         } n         for(int j = 1;j <= m;j++){ n             scanf( %d ,&b[j]); n             mp2[b[j]] = 1; n             if(midm == m+1 && b[j] > 0)midm = j; n         } n  n         int lstart = 0; n         for(int i = 1;i < midm;i++){ n             if(mp[b[i]])lstart++; n         } n         int ans1 = lstart,ans2 = 0; n         int now = midn-1; n         int len = 0; n         for(int i = midm-1;i >= 1;i--){ n //            printf( %d n ,i); n             int res = lstart; n             int bb = b[i]-len; n             while(now && bb <= a[now]){ n                 if(mp2[a[now]])res--; n                 len++; n                 bb--; n                 now--; n             } n             if(!len)continue; n             int where = b[i] - len+1; n             where = binaryl(where,1,i); n //            printf( %d: where==%d res==%d len==%d n ,i,where,res,len); n             lstart = res; n             res += i - where; n             ans1 = max(ans1,res); n         } n  n         int rstart = 0; n         for(int i = midm;i <= m;i++){ n             if(mp[b[i]])rstart++; n         } n         ans2 = rstart; n         now = midn; n         len = 0; n         for(int i = midm;i <= m;i++){ n             int res = rstart; n             int bb = b[i] + len; n             while(now <= n && bb >= a[now]){ n                 if(mp2[a[now]])res--; n                 len++; n                 bb++; n                 now++; n             } n             if(len == 0)continue; n             int where = b[i] + len-1; n             where = binaryr(where,i,m); n //            printf( %d: where==%d res==%d len==%d n ,i,where,res,len); n             rstart = res; n             res += where - i; n             ans2 = max(ans2,res); n         } n //        printf( %d %d n ,ans1,ans2); n         printf( %d n ,ans1+ans2); n     } n } n /* n 1000 n 5 5 n -5 -3 -1 8 100 n -9 -7 -3 1000 1001 n  n 1 5 n -5 n -10 -9 -8 -7 -4 n  n 5 5 n -5 -4 -3 -2 -1 n -100 -99 -96 -95 -94 n */ n


#include <bits/stdc++.h> n using namespace std; n string to_string(string s) { return     + s +    ; } n string to_string(char s) { return string(1, s); } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A> n string to_string(A); n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool f = 1; n   string r =  { ; n   for (const auto& x : v) { n     if (!f) r +=  ,  ; n     f = 0; n     r += to_string(x); n   } n   return r +  } ; n } n void debug_out() { cout <<   n ; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cout <<     << to_string(H); n   debug_out(T...); n } n const long long MOD = 1000000007; n long long binpow(long long b, long long p) { n   long long ans = 1; n   while (p > 0) { n     if (p & 1) ans = (ans * b) % MOD; n     b = (b * b) % MOD; n     p >>= 1; n   } n   return ans; n } n long long modinv(long long x) { return binpow(x, MOD - 2); } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, i, l, r; n   cin >> n >> m; n   vector<int> a(n), inc(n), dec(n); n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     inc[i] = dec[i] = i; n   } n   for (i = n - 2; i >= 0; i--) { n     if (a[i] <= a[i + 1]) inc[i] = inc[i + 1]; n     if (a[i] >= a[i + 1]) dec[i] = dec[i + 1]; n   } n   while (m--) { n     cin >> l >> r; n     l--; n     r--; n     if (dec[inc[l]] >= r) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n } n


#include <bits/stdc++.h> n using namespace ::std; n const long double PI = acos(-1); n const long long MOD = 1000000000 + 7; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int add(int a, int b, int m = MOD) { return (a + b + m + m) % m; } n int mul(long long a, long long b, int m = MOD) { return (a * b) % m; } n long long pow_mod(long long a, long long b, long long m = MOD) { n   long long res = 1LL; n   a = a % m; n   while (b) { n     if (b & 1) res = mul(res, a, m); n     b >>= 1; n     a = mul(a, a, m); n   } n   return res; n } n long long fastexp(long long a, long long b) { n   long long res = 1LL; n   while (b) { n     if (b & 1) res = res * a; n     b >>= 1; n     a *= a; n   } n   return res; n } n int gcdExtendido(int a, int b, int *x, int *y) { n   if (a == 0) { n     *x = 0; n     *y = 1; n     return b; n   } n   int x1, y1; n   int gcd = gcdExtendido(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n int modInverso(int a, int m) { n   int x, y; n   int g = gcdExtendido(a, m, &x, &y); n   if (g != 1) n     return -1; n   else n     return (x % m + m) % m; n } n const int N = 200000 + 5; n const int L = 2; n const unsigned int B[] = {257, 311}; n int n; n string minimal(string s) { n   if (s.size() & 1) return s; n   string s1 = minimal(s.substr(0, s.size() / 2)); n   string s2 = minimal(s.substr(s.size() / 2, s.size() / 2)); n   if (s1 < s2) return s1 + s2; n   return s2 + s1; n } n int main() { n   string a, b; n   cin >> a >> b; n   if (minimal(a) == minimal(b)) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, k, a, b, q, cnt[N]; n struct fenwick { n   int fen[N]; n   void add(int p, int x) { n     for (; p < N; p += p & -p) fen[p] += x; n   } n   int get(int p) { n     int ans = 0; n     for (; p; p -= p & -p) ans += fen[p]; n     return ans; n   } n } fen1, fen2; n void readInput() { cin >> n >> k >> a >> b >> q; } n void writeOutput() { n   for (int i = 0; i < q; i++) { n     int t; n     cin >> t; n     if (t & 1) { n       int d, x; n       cin >> d >> x; n       cnt[d] += x; n       fen1.add(d, max(min(b, cnt[d]) - cnt[d] + x, 0)); n       fen2.add(n - d + 1, max(min(a, cnt[d]) - cnt[d] + x, 0)); n     } else { n       int p; n       cin >> p; n       cout << fen1.get(p - 1) + fen2.get(n - p - k + 1) << endl; n     } n   } n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   readInput(), writeOutput(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n using namespace std; n long long int n, k, a[200005], dp[200005]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> k; n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   for (long long int i = 1; i <= n; i++) dp[i] = dp[i - 1] + a[i]; n   long long int i = 1, ans = -1; n   while (i <= n) { n     long long int j = i; n     while (j + 1 <= n && a[j + 1] == a[j]) { n       j++; n     } n     long long int cnt = j - i + 1; n     if (cnt >= k) { n       ans = 0; n       break; n     } n     long long int cur = 1e18; n     long long int s1 = i - 1; n     long long int s2 = n - j; n     long long int ans1 = 0, ans2 = 0; n     ans1 = s1 * (a[i] - 1) - dp[i - 1] + dp[0]; n     ans2 = dp[n] - dp[j] - s2 * (a[i] + 1); n     if (s2 + cnt >= k) { n       cur = min(cur, ans2); n     } n     if (s1 + cnt >= k) { n       cur = min(cur, ans1); n     } n     cur = min(cur, ans1 + ans2); n     cur = cur + k - cnt; n     if (ans == -1) { n       ans = cur; n     } else n       ans = min(ans, cur); n     i = j + 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_BUF_SIZE = 16384; n char BUFOR[MAX_BUF_SIZE]; n int BUF_SIZE, BUF_POS; n char ZZZ; n const int MXN = 1000010; n const int C = 262144; n const int INF = 1000000001; n void test() { n   int n; n   scanf( %d , &n); n   cout << ((long long int)(n - 1) * (n - 3) + 1) << endl; n } n int main() { n   int te = 1; n   while (te--) test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, x1, x2, m, c; n   cin >> n >> x1 >> x2; n   vector<pair<long long, long long>> v; n   for (int i = 0; i < n; ++i) { n     cin >> m >> c; n     v.push_back({m * x1 + c, m * x2 + c}); n   } n   sort(v.begin(), v.end()); n   long long p = v[0].second; n   for (auto i : v) { n     if (i.second < p) { n       cout <<  YES ; n       return 0; n     } n     p = i.second; n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n; n   map<int, vector<vector<int> > > m; n   vector<int> v, ans; n   map<int, int> s; n   for (int i = 0; i < n - 2; i++) { n     int f, g, h; n     cin >> f >> g >> h; n     v.push_back(f); n     v.push_back(g); n     v.push_back(h); n     s[f]++; n     s[g]++; n     s[h]++; n     m[f].push_back(v); n     m[g].push_back(v); n     m[h].push_back(v); n     v.clear(); n   } n   for (auto i = s.begin(); i != s.end(); i++) { n     if (i->second == 1) { n       ans.push_back(i->first); n       for (auto p = m[i->first].begin(); p != m[i->first].end(); p++) { n         auto j = *p; n         for (int u = 0; u < 3; u++) { n           if (s[j[u]] == 2) a = j[u]; n           if (s[j[u]] == 3) b = j[u]; n         } n       } n       ans.push_back(a); n       ans.push_back(b); n       break; n     } n   } n   while (ans.size() < n) { n     int x, y, z, t; n     t = ans.size(); n     x = ans[t - 1]; n     y = ans[t - 2]; n     z = ans[t - 3]; n     for (auto p = m[y].begin(); p != m[y].end(); p++) { n       auto j = *p; n       if (((j[0] == y && j[1] == x) || (j[0] == x && j[1] == y)) && j[2] != z) n         ans.push_back(j[2]); n       else if (((j[0] == y && j[2] == x) || (j[0] == x && j[2] == y)) && n                j[1] != z) n         ans.push_back(j[1]); n       else if (((j[1] == y && j[2] == x) || (j[1] == x && j[2] == y)) && n                j[0] != z) n         ans.push_back(j[0]); n     } n   } n   for (auto c : ans) { n     cout << c <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n char s[maxn], t[maxn]; n int p[maxn], q[maxn], r[maxn]; n int main() { n   int n, m, k, d; n   scanf( %s%d , s, &m); n   n = strlen(s); n   while (m--) { n     scanf( %d%d , &k, &d); n     for (int i = 0; i < n; i++) p[i] = q[i] = i; n     int id = 0; n     for (int i = 0; i < d; i++) { n       for (int j = i; j < k; j += d) p[id++] = j; n     } n     int last = p[0]; n     for (int i = 0; i < n - 1; i++) p[i] = p[i + 1]; n     p[n - 1] = last; n     int pw = n - k + 1; n     while (pw > 0) { n       if (pw & 1) { n         for (int i = 0; i < n; i++) r[i] = q[p[i]]; n         for (int i = 0; i < n; i++) q[i] = r[i]; n       } n       for (int i = 0; i < n; i++) r[i] = p[p[i]]; n       for (int i = 0; i < n; i++) p[i] = r[i]; n       pw >>= 1; n     } n     for (int i = 0; i < n; i++) t[i] = s[q[(k + i - 1) % n]]; n     for (int i = 0; i < n; i++) s[i] = t[i]; n     puts(s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(int& a, int& b) { return b < a; } n int main() { n   int n; n   cin >> n; n   vector<pair<int, int>> source(n), team1, team2; n   long long sum_team1 = 0, sum_team2 = 0; n   for (int i = 0; i < n; ++i) { n     cin >> source[i].first; n     source[i].second = i; n   } n   sort(source.begin(), source.end()); n   int i = 0, par = 1; n   while (!source.empty()) { n     if (par) { n       team1.push_back(source.back()); n       source.pop_back(); n       par = 0; n       sum_team1 += team1.back().first; n     } else { n       team2.push_back(source.back()); n       source.pop_back(); n       par = 1; n       sum_team2 += team2.back().first; n     } n   } n   cout << team1.size() <<   n ; n   for (int i = 0; i < team1.size(); ++i) cout << team1[i].second + 1 <<    ; n   cout <<   n ; n   cout << team2.size() <<   n ; n   for (int i = 0; i < team2.size(); ++i) cout << team2[i].second + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const long long INF = 1e18; n const int maxn = 1e6 + 6; n const int mod = 1e9 + 7; n const double eps = 1e-9; n inline bool read(long long &num) { n   char in; n   bool IsN = false; n   in = getchar(); n   if (in == EOF) return false; n   while (in !=  -  && (in <  0  || in >  9 )) in = getchar(); n   if (in ==  - ) { n     IsN = true; n     num = 0; n   } else n     num = in -  0 ; n   while (in = getchar(), in >=  0  && in <=  9 ) { n     num *= 10, num += in -  0 ; n   } n   if (IsN) num = -num; n   return true; n } n long long n, m; n struct node { n   int x, v; n   bool friend operator<(node a, node b) { return a.x < b.x; } n } save[maxn]; n vector<int> v; n long long a[maxn], b[maxn]; n int nn; n void update(long long *p, int pos, long long x) { n   while (pos <= nn) { n     p[pos] += x; n     pos += pos & -pos; n   } n } n long long GetSum(long long *p, long long x) { n   long long ans = 0; n   while (x > 0) { n     ans += p[x]; n     x -= x & -x; n   } n   return ans; n } n int getid(long long x) { n   return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) scanf( %d , &save[i].x); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &save[i].v); n     v.push_back(save[i].v); n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   nn = v.size(); n   sort(save + 1, save + 1 + n); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int id = getid(save[i].v); n     long long tempx = GetSum(b, id); n     long long tempy = GetSum(a, id); n     ans += tempx * save[i].x - tempy; n     update(b, id, 1); n     update(a, id, save[i].x); n   } n   printf( %lld n , ans); n   return 0; n } n


#include<bits/stdc++.h> /* n #include <ext/pb_ds/assoc_container.hpp> // Common file n #include <ext/pb_ds/tree_policy.hpp> // Including */ n using namespace std; n //using namespace __gnu_pbds; n  n //typedefs n  n typedef long long ll; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<vi> vvi; n typedef vector<vl> vvl; n typedef pair<int,int> pii; n typedef pair<double, double> pdd; n typedef pair<ll, ll> pll; n typedef vector<pii> vii; n typedef vector<pll> vll; n typedef vector<int>::iterator vit; n typedef set<int>::iterator sit; n  n /* n template<typename T> n using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; n template<typename F, typename S> n using ordered_map = tree<F, S, less<F>, rb_tree_tag, tree_order_statistics_node_update>; n */ n  n //#Defines n #define rep(i,a,b) for(i=a;i<=b;i++) n #define repR(i,a,b) for(i=a;i>=b;i--) n //#define pb push_back n #define pb emplace_back n #define F first n #define S second n #define mp make_pair n #define all(c)  c.begin(),c.end() n #define endl   n  n #define pf printf n #define sf scanf n //#define left __left n //#define right __right n //#define tree __tree n #define MOD 1000000007 n //#define harmonic(n) 0.57721566490153286l+log(n) n  n #define RESET(a,b) memset(a,b,sizeof(a)) n #define gcd(a,b) __gcd(a,b) n #define lcm(a,b) (a*(b/gcd(a,b))) n #define sqr(a) ((a) * (a)) n  n #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); n  n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const ll infLL = 9000000000000000000; n  n  n //Bit Operations n inline bool checkBit(ll n, int i) { return n&(1LL<<i); } n inline ll setBit(ll n, int i) { return n|(1LL<<i);; } n inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); } n  n int fx[] = {0, 0, +1, -1}; n int fy[] = {+1, -1, 0, 0}; n //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1}; n //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1}; n  n  n //Inline functions n  n inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; } n inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); } n inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } n inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; } n inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; } n inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } n inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } n inline ll modInverse(ll a) { return modPow(a, MOD-2); } n inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } n inline bool isInside(pii p,ll n,ll m) { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<m); } n inline bool isInside(pii p,ll n)  { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<n); } n inline bool isSquare(ll x)     { ll s = sqrt(x); return (s*s==x); } n inline bool isFib(ll x)     { return isSquare(5*x*x+4)|| isSquare(5*x*x-4); } n inline bool isPowerOfTwo(ll x)   { return ((1LL<<(ll)log2(x))==x); } n  n  n struct func n { n  //this is a sample overloading function for sorting stl n  bool operator()(pii const &a, pii const &b)  n  { n   if(a.F==b.F) n    return (a.S<b.S); n   return (a.F<b.F); n  } n }; n  n //Prime Number Generator n  n /* n #define M 100000000 n int marked[M/64 + 2]; n  n #define on(x) (marked[x/64] & (1<<((x%64)/2))) n #define mark(x)  marked[x/64] |= (1<<((x%64)/2)) n  n vl prime; n  n bool isPrime(int num) { n   return num > 1 && (num == 2 || ((num & 1) && !on(num)));  n } n  n void sieve(ll n) { n  for (ll i = 3; i * i < n; i += 2) { n   if (!on(i)) { n    for (ll j = i * i; j <= n; j += i + i) { n     mark(j); n    } n   } n  } n  prime.pb(2); n  for(ll i = 3; i <= n; i += 2) { n   if(!on(i)) prime.pb(i);  n  } n } n  n */ n  n // n //debug n #ifdef tajir n template < typename F, typename S > n ostream& operator << ( ostream& os, const pair< F, S > & p ) { n  return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const vector< T > &v ) { n  os <<  { ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n    os << *it; n  } n  return os <<  } ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const set< T > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n    os << *it; n  } n  return os <<  ] ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const multiset< T > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n   os << *it; n  } n  return os <<  ] ; n } n  n template < typename F, typename S > n ostream &operator << ( ostream & os, const map< F, S > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n   os << it -> first <<   =   << it -> second ; n   } n  return os <<  ] ; n } n  n #define dbg(args...) do {cerr << #args <<   :  ; faltu(args); } while(0) n  n clock_t tStart = clock(); n #define timeStamp dbg( Execution Time:  , (double)(clock() - tStart)/CLOCKS_PER_SEC) n  n void faltu () { n  cerr << endl; n } n  n template <typename T> n void faltu( T a[], int n ) { n  for(int i = 0; i < n; ++i) cerr << a[i] <<    ; n  cerr << endl; n } n  n template <typename T, typename ... hello> n void faltu( T arg, const hello &... rest) { n  cerr << arg <<    ; n  faltu(rest...); n } n #else n #define dbg(args...) n #endif // tajir n  n ll n,m,k,x,y; n  n ll get() { n  ll cnt = 0; n  cnt += (m-2) + 1ll + (m-2) + 1ll; n  return cnt; n } n  n int main() n { n  #ifdef tajir n  // freopen( input.txt ,  r , stdin); n  #else n   // online submission n  #endif n  optimize(); n  bool down, right, up, left; n  ll mi; n  bool pre = true; n  down = right = up = left = false; n  cin >> n >> m >> k; n  x = 1 , y = 1; n  down = true; n  ll cnt = get(); n  k = k%(n*m); n // cout << cnt << endl; n // cout << k << endl; n  if(down) { n   mi = min(k,(n-x)); n   k -= mi; n   x += mi; n   down = !down; n   right = !right; n  } n  if(right) { n   mi = min(k,(m-y)); n   k -= mi; n   y += mi; n   right = !right; n   up = !up; n  } n // cout << x <<     << y << endl; n  x -= 2*(k/cnt); n  k = k%cnt; n // cout << x <<     << y << endl; n  up = true; n  down = right = left = false; n  while(k > 0) { n   if(down) { n    mi = min(k,(n-x)); n    k -= mi; n    x += mi; n    down = !down; n    right = !right; n   } n   else if(right) { n    mi = min(k,(m-y)); n    k -= mi; n    y += mi; n    right = !right; n    up = !up; n   } n   else if(up) { n    mi = min(k,1ll); n    k -= mi; n    x -= mi; n    up = !up; n    if(pre) { n     left = !left; n     pre = !pre; n    } n    else { n     right = !right; n     pre = !pre; n    } n  n   } n   else { n    mi = min((y-2), k); n    k -= mi; n    y -= mi; n    if(x == 1) { n     mi = min(k,1ll); n     k -= mi; n     y -= mi; n     left = !left; n     down = !down; n    } n    else { n     left = !left; n     up = !up; n    } n   } n  // cout << x <<     << y <<     << k <<  endl; n  }  n  cout << x <<     << y << endl; n  return 0; n } n  n  n  n  n  n  n //? n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n long long tot[200010]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<pair<long long, int> > v; n   for (int i = 0; i < n; i++) { n     long long l, r; n     cin >> l >> r; n     v.push_back({l, -1}); n     v.push_back({r + 1, 1}); n   } n   sort(v.begin(), v.end()); n   long long open = 0; n   long long last = v[0].first; n   for (int i = 0; i < v.size();) { n     long long pos = v[i].first; n     tot[open] += pos - last; n     while (i < v.size() && v[i].first == pos) { n       open -= v[i].second; n       i++; n     } n     last = pos; n   } n   for (int i = 1; i <= n; i++) cout << tot[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fa[2005], fb[2005]; n long long mina[2005], minb[2005]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<long long> a(n), b(m); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     fa[i + 1] = fa[i] + a[i]; n   } n   for (int j = 0; j < m; j++) { n     cin >> b[j]; n     fb[j + 1] = fb[j] + b[j]; n   } n   long long x; n   cin >> x; n   for (int i = 0; i <= n; i++) { n     mina[i] = 2000000000; n     for (int k = 0; k + i <= n; k++) { n       mina[i] = min(mina[i], fa[k + i] - fa[k]); n     } n   } n   for (int j = 0; j <= m; j++) { n     minb[j] = 2000000000; n     for (int k = 0; k + j <= m; k++) { n       minb[j] = min(minb[j], fb[k + j] - fb[k]); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (mina[i] * minb[j] <= x) ans = max(ans, i * 1ll * j); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, mx = 0; n   cin >> n; n   map<int, long long int> mp1, mp2; n   map<pair<long long int, long long int>, int> mp; n   vector<long long int> a(n), b(n); n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     a[i] = x, b[i] = y; n     mp1[x]++, mp2[y]++; n     mp[{a[i], b[i]}]++; n     mx = max(mx, mp2[y]); n   } n   long long int operations = INT_MAX; n   for (int i = 0; i < n; i++) { n     long long int temp = mp1[a[i]] + mp2[a[i]] - mp[{a[i], a[i]}]; n     if (temp >= (n + 1) / 2) { n       operations = n           min(operations, max(min(temp, (n + 1) / 2) - mp1[a[i]], 0LL)); n     } n   } n   if (operations == INT_MAX && mx < (n + 1) / 2) { n     cout <<  -1 n ; n     return 0; n   } n   if (operations == INT_MAX) { n     operations = (n + 1) / 2; n   } n   cout << operations << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int n1 = n, dig, x = 0, y = 0, z = 0, r; n   while (n1 > 0) { n     dig = n1 % 10; n     if (dig != 4 && dig != 7) { n       x++; n       break; n     } n     n1 = n1 / 10; n   } n   if (x == 0) { n     printf( YES ); n     z++; n   } else if (x > 0) { n     for (int i = 1; i < n; i++) { n       y = 0; n       n1 = i; n       while (n1 > 0) { n         dig = n1 % 10; n         if (dig != 4 && dig != 7) { n           y++; n           break; n         } n         n1 = n1 / 10; n       } n       if (y == 0 && n % i == 0) { n         printf( YES ); n         z++; n         break; n       } n     } n   } n   if (z == 0) { n     printf( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ans[2000005]; n vector<int> lis[2000005]; n string Str[2000005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int m = 0; n   for (int i = 0; i < n; i++) { n     string str; n     cin >> str; n     Str[i] = str; n     int k; n     cin >> k; n     while (k--) { n       int a; n       cin >> a; n       lis[a].push_back(i); n       m = max(m, int(a + str.length() - 1)); n     } n   } n   int r = -1, p = 0, ind = 0; n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j < lis[i].size(); j++) { n       int a = Str[lis[i][j]].size(); n       int b = r - p; n       if (a > b) { n         r = Str[lis[i][j]].length(); n         p = 0; n         ind = lis[i][j]; n       } n     } n     if (p < r) { n       ans[i] = Str[ind][p]; n       p++; n     } else { n       ans[i] =  a ; n     } n     cout << ans[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 32; n int n, u, r, a[N][N], b[N], p[N], k[N]; n long long ans = -1e18; n void dfs(int x, bool z) { n   int i; n   if (x % 2 == u % 2) { n     long long t = 0; n     for (i = 1; i <= n; i = i + 1) t += (long long)a[i][x] * k[i]; n     ans = max(ans, t); n   } n   if (x >= u) return; n   for (i = 1; i <= n; i = i + 1) a[i][x + 1] = a[p[i]][x] + r; n   dfs(x + 1, 1); n   if (z) { n     for (i = 1; i <= n; i = i + 1) a[i][x + 1] = a[i][x] ^ b[i]; n     dfs(x + 1, 0); n   } n } n int main() { n   int i, j; n   cin >> n >> u >> r; n   for (i = 1; i <= n; i = i + 1) cin >> a[i][0]; n   for (i = 1; i <= n; i = i + 1) cin >> b[i]; n   for (i = 1; i <= n; i = i + 1) cin >> k[i]; n   for (i = 1; i <= n; i = i + 1) cin >> p[i]; n   dfs(0, 1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, long long> mp; n long long arr[2][1234]; n int main() { n   string s; n   long long idx1, idx2; n   char repeating; n   cin >> s; n   long long i; n   for (i = 0; i < s.size(); i++) mp[s[i]]++; n   for (auto it : mp) n     if (it.second == 2) repeating = it.first; n   0; n   long long c1 = 0, c2 = 0, c3 = 0, nc1, nc3; n   for (i = 0; i < s.size(); i++) n     if (s[i] == repeating) { n       idx1 = idx2; n       idx2 = i; n     } n   if (idx2 == idx1 + 1) { n     cout <<  Impossible ; n     return 0; n   } n   0; n   c2 = idx2 - idx1 - 1; n   c3 = 26 - idx2; n   c1 = idx1; n   0; n   0; n   if (c2 % 2 == 0) { n     long long sum = c1 + c3; n     nc3 = sum / 2; n     nc1 = nc3 + 1; n     if (nc1 < c1) { n       long long give = c1 - nc1; n       string tmp = s.substr(0, give); n       s = s.substr(give, 27 - tmp.size()); n       s += tmp; n       0; n     } else if (nc1 > c1) { n       long long give = nc1 - c1; n       string tmp = s.substr(27 - give, give); n       s = tmp + s.substr(0, 27 - give); n       0; n     } n   } else if (c2 % 2 == 1) { n     long long sum = c1 + c3; n     nc3 = sum / 2; n     nc1 = nc3; n     if (nc1 < c1) { n       long long give = c1 - nc1; n       string tmp = s.substr(0, give); n       s = s.substr(give, 27 - tmp.size()); n       s += tmp; n       0; n     } else if (nc1 > c1) { n       long long give = nc1 - c1; n       string tmp = s.substr(27 - give, give); n       s = tmp + s.substr(0, 27 - give); n       0; n     } n   } n   0; n   for (i = 0; i < s.size(); i++) n     if (s[i] == repeating) { n       idx1 = idx2; n       idx2 = i; n     } n   long long curr = 0; n   if (c2 % 2 == 0) { n     for (i = 0; i < idx1; i++) arr[1][curr++] = s[i] -  A ; n     curr--; n     0; n     arr[0][curr] = repeating -  A ; n     curr++; n     for (i = idx1 + 1; i <= (idx1 + c2 / 2); i++) arr[0][curr++] = s[i] -  A ; n     0; n     curr--; n     for (i = idx1 + c2 / 2 + 1; i <= (idx1 + c2); i++) n       arr[1][curr--] = s[i] -  A ; n     curr--; n     for (i = idx2 + 1; i < 27; i++) arr[0][curr--] = s[i] -  A ; n     for (i = 0; i < 2; i++) { n       for (long long j = 0; j < 13; j++) { n         cout << char(arr[i][j] +  A ); n       } n       cout << endl; n     } n   } else { n     for (i = 0; i < idx1; i++) arr[1][curr++] = s[i] -  A ; n     0; n     arr[1][curr] = repeating -  A ; n     for (i = idx1 + 1; i <= ((idx1 + c2 / 2) + 1); i++) n       arr[0][curr++] = s[i] -  A ; n     0; n     curr--; n     for (i = idx1 + c2 / 2 + 2; i <= (idx1 + c2); i++) n       arr[1][curr--] = s[i] -  A ; n     curr--; n     for (i = idx2 + 1; i < 27; i++) arr[0][curr--] = s[i] -  A ; n     for (i = 0; i < 2; i++) { n       for (long long j = 0; j < 13; j++) { n         cout << char(arr[i][j] +  A ); n       } n       cout << endl; n     } n   } n } n


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


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n int n,m,pre,N; n string s,t; n ll ans; n map<string,vector<string> >S; n map<string,bool>vis; n int main(){ n  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); n  cin>>n; n  for(int i=1;i<=n;i++){ n   cin>>s;t=s; n   sort(t.begin(),t.end()); n   S[t].push_back(s); n  } n  m=s.size(); n  for(auto it:S){ n   vector<string> v=it.second; n   N=v.size(),ans+=1337ll*pre*N,pre+=N; n   if(N<=200*m){ n    for(int i=0;i<N;i++)for(int j=i+1;j<N;j++){ n     int L=0,R=m-1; n     while(v[i][L]==v[j][L]&&L<=R)L++; n     while(v[i][R]==v[j][R]&&L<=R)R--; n     bool f1=1,f2=1; n     for(int k=L;k<R;k++)f1&=(v[i][k]<=v[i][k+1]),f2&=(v[j][k]<=v[j][k+1]); n     ans+=2; n     if(f1||f2)ans--; n    } n   }else{ n    ll cur=(ll)N*(N-1); n    vis.clear(); n    for(auto s:v)vis[s]=1; n    for(auto s:v)for(int i=0;i<m;i++)for(int j=i+2;j<=m;j++){ n     t=s,sort(t.begin()+i,t.begin()+j); n     if(t[i]!=s[i]&&t[j-1]!=s[j-1]&&vis.count(t))cur--; n    } n    ans+=cur; n   } n  } n  cout<<ans; n }


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


#include <bits/stdc++.h> n using namespace std; n int n, arr[111111]; n int main() { n   cin >> n; n   arr[0] = 0x3f3f3f3f; n   for (int i = 1; i <= n; ++i) cin >> arr[i]; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     int ii = n - i; n     if (arr[ii] < arr[ii - 1]) { n       ans = i; n       break; n     } n   } n   ans = n - ans - 1; n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct fenwick_tree_1D { n   long long n, log_n; n   vector<long long> tree; n   fenwick_tree_1D(long long _n) n       : n(_n), log_n((long long)(log2(_n) + 1.0L)), tree(_n + 5) {} n   void update(long long x, long long v) { n     while (x <= n) { n       tree[x] += v; n       x += x & (-x); n     } n   } n   long long prefix_sum(long long x) { n     long long r = 0; n     while (x) { n       r += tree[x]; n       x -= x & (-x); n     } n     return r; n   } n   long long range_sum(long long l, long long r) { n     return prefix_sum(r) - prefix_sum(l - 1); n   } n }; n const long long bs = 300; n fenwick_tree_1D bit(100005); n long long a[100005], nxt[100005], last[100005] = {0}, dl[2 * bs + 5], n                                   id[100005], dc = 0, result[100005] = {0}; n set<long long> s[100005]; n long long qc; n struct query { n   long long t, l, r, qno; n } q[bs + 5]; n long long q2c; n struct query2 { n   long long l, r, qno; n } q2[bs + 5]; n vector<long long> ans; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     s[a[i]].insert(i); n   } n   for (long long h = 0; h < m; h += bs) { n     qc = min(bs, m - h); n     for (long long j = 0; j < qc; j++) { n       cin >> q[j].t >> q[j].l >> q[j].r; n       q[j].qno = h + j; n       if (q[j].t == 1) { n         if (!id[a[q[j].l]]) dl[dc] = a[q[j].l], id[a[q[j].l]] = 1, dc++; n         if (!id[q[j].r]) dl[dc] = q[j].r, id[q[j].r] = 1, dc++; n       } else n         q2[q2c].l = q[j].l, q2[q2c].r = q[j].r, q2[q2c].qno = q[j].qno, n         ans.push_back(q[j].qno), q2c++; n     } n     for (long long i = n; i >= 1; i--) { n       if (id[a[i]]) { n         nxt[i] = 0; n         continue; n       } n       nxt[i] = last[a[i]]; n       if (nxt[i]) bit.update(nxt[i], nxt[i] - i); n       last[a[i]] = i; n     } n     sort(q2, q2 + q2c, n          [](const query2 &l, const query2 &r) { return l.l < r.l; }); n     long long pos = 1; n     for (long long i = 0; i < q2c; i++) { n       while (pos < q2[i].l) { n         if (id[a[pos]] || !nxt[pos]) { n           pos++; n           continue; n         } n         bit.update(nxt[pos], pos - nxt[pos]); n         pos++; n       } n       result[q2[i].qno] = bit.prefix_sum(q2[i].r); n     } n     for (long long i = 0; i < qc; i++) { n       if (q[i].t == 1) { n         s[a[q[i].l]].erase(q[i].l); n         a[q[i].l] = q[i].r; n         s[a[q[i].l]].insert(q[i].l); n       } else { n         for (long long j = 0; j < dc; j++) { n           auto it1 = s[dl[j]].lower_bound(q[i].l); n           auto it2 = s[dl[j]].upper_bound(q[i].r); n           if (it1 != s[dl[j]].end() && it2 != s[dl[j]].begin()) { n             it2--; n             if (*it2 > *it1) result[q[i].qno] += *it2 - *it1; n           } n         } n       } n     } n     for (long long i = 0; i < dc; i++) id[dl[i]] = 0; n     dc = 0; n     q2c = 0; n     for (long long i = 1; i <= n; i++) bit.tree[i] = 0, last[i] = 0; n   } n   for (auto &k : ans) cout << result[k] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int n; n   cin >> n; n   int k; n   cin >> k; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   int lift; n   cin >> lift; n   vector<int> c(n); n   for (int i = 0; i < n; i++) cin >> c[i]; n   set<int> second; n   ll ans = 0; n   int joker; n   for (int i = 0; i < n; i++) { n     second.insert(c[i]); n     if (v[i] <= k) n       continue; n     else { n       joker = (v[i] - k) / lift; n       if ((v[i] - k) % lift != 0) joker++; n       if (joker > second.size()) { n         ans = -1; n         break; n       } n       k += (joker * lift); n       int mini; n       while (joker--) { n         mini = *min_element(second.begin(), second.end()); n         ans += mini; n         second.erase(mini); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100004; n int n, m, c = 0; n char s[24][24]; n set<unsigned long long> st, ns; n bool fun(int x, int y) { n   st.clear(); n   for (int i = 0; i < n; i += x) n     for (int j = 0; j < m; j += y) { n       ns.clear(); n       unsigned long long sum = 0; n       for (int u = 0; u < x; u++) { n         unsigned long long p = 0; n         for (int v = 0; v < y; v++) p = p * 131 + s[i + u][j + v]; n         sum = sum * 997 + p; n       } n       ns.insert(sum); n       sum = 0; n       for (int u = x - 1; u >= 0; u--) { n         unsigned long long p = 0; n         for (int v = y - 1; v >= 0; v--) p = p * 131 + s[i + u][j + v]; n         sum = sum * 997 + p; n       } n       ns.insert(sum); n       if (x == y) { n         sum = 0; n         for (int v = y - 1; v >= 0; v--) { n           unsigned long long p = 0; n           for (int u = 0; u < x; u++) p = p * 131 + s[i + u][j + v]; n           sum = sum * 997 + p; n         } n         ns.insert(sum); n         sum = 0; n         for (int v = 0; v < y; v++) { n           unsigned long long p = 0; n           for (int u = x - 1; u >= 0; u--) p = p * 131 + s[i + u][j + v]; n           sum = sum * 997 + p; n         } n         ns.insert(sum); n       } n       for (auto it = ns.begin(); it != ns.end(); it++) { n         if (st.count(*it)) return 0; n         st.insert(*it); n       } n     } n   return 1; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   int x = 100, y = 100, ans = 40000; n   for (int i = 1; i <= n; i++) n     if (n % i == 0) n       for (int j = 1; j <= m; j++) n         if (m % j == 0) { n           if (fun(i, j)) { n             ++c; n             if (i * j < ans || i * j == ans && i < x) { n               ans = i * j; n               x = i; n               y = j; n             } n           } n         } n   printf( %d n%d %d n , c, x, y); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9223372036854775807; n const long long MOD = 1000000007; n const long long FMOD = 998244353; n const long long MAXN = 1e5 + 5; n const long double eps = 1e-9; n const long double PI = acos(-1); n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> a(n); n   vector<pair<char, int>> arr; n   int cnt = 0; n   for (long long i = 0; i < n; i++) { n     char c; n     cin >> c; n     int ind; n     cin >> ind; n     ind--; n     if (c ==  + ) { n       a[ind].first++; n     } else { n       a[ind].second++; n       cnt++; n     } n     arr.push_back({c, ind}); n   } n   bool ok = true; n   map<int, int> chorhaibc; n   for (long long i = 0; i < n; i++) { n     if (cnt - a[i].second + a[i].first == m) { n       chorhaibc[i] = 1; n     } n   } n   if (chorhaibc.size() == 1) { n     int killer = chorhaibc.begin()->first; n     for (long long i = 0; i < n; i++) { n       if (arr[i].first ==  + ) { n         if (arr[i].second == killer) n           cout <<  Truth ; n         else n           cout <<  Lie ; n       } n       if (arr[i].first ==  - ) { n         if (arr[i].second == killer) n           cout <<  Lie ; n         else n           cout <<  Truth ; n       } n       cout << endl; n     } n   } else { n     for (long long i = 0; i < n; i++) { n       if (arr[i].first ==  +  && chorhaibc[arr[i].second] == 0) { n         cout <<  Lie ; n       } else { n         if (arr[i].first ==  -  && chorhaibc[arr[i].second] == 0) { n           cout <<  Truth ; n         } else { n           cout <<  Not defined ; n         } n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ex_gcd(long long a, long long b, long long &x, long long &y) { n   int d = a; n   if (b == 0) { n     x = 1; n     y = 0; n   } else { n     d = ex_gcd(b, a % b, y, x); n     y -= a / b * x; n   } n   return d; n } n int main() { n   long long a, b, c; n   long long x, y; n   cin >> a >> b >> c; n   int gcd = ex_gcd(a, b, x, y); n   if (-c % gcd) n     cout << -1; n   else { n     long long k = (-c) / gcd; n     cout << k * x <<     << k * y; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n bool a[2000][2000], use[2000][2000][6][6]; n vector<pair<int, int> > go; n void dfs(int x, int y, int way, int cnt) { n   if (cnt == 3) return; n   use[x][y][way][cnt] = 1; n   for (int i = 1; i <= 4; i++) n     if (i == way) { n       if (a[x + go[i - 1].first][y + go[i - 1].second] && n           !use[x + go[i - 1].first][y + go[i - 1].second][i][cnt]) n         dfs(x + go[i - 1].first, y + go[i - 1].second, i, cnt); n     } else if (abs(i - way) != 2) { n       if (a[x + go[i - 1].first][y + go[i - 1].second] && n           !use[x + go[i - 1].first][y + go[i - 1].second][i][cnt + 1]) n         dfs(x + go[i - 1].first, y + go[i - 1].second, i, cnt + 1); n     } n } n signed main() { n   int n = 0, m = 0; n   cin >> n >> m; n   int si = 0, sj = 0, fi = 0, fj = 0; n   for (int i = 1; i <= n; i++) { n     string st =   ; n     cin >> st; n     for (int j = 1; j <= m; j++) n       if (st[j - 1] !=  * ) a[i][j] = 1; n     for (int j = 1; j <= m; j++) n       if (st[j - 1] ==  S ) { n         si = i; n         sj = j; n       } else if (st[j - 1] ==  T ) { n         fi = i; n         fj = j; n       } n   } n   go.push_back({1, 0}); n   go.push_back({0, 1}); n   go.push_back({-1, 0}); n   go.push_back({0, -1}); n   dfs(si, sj, 1, 0); n   dfs(si, sj, 2, 0); n   dfs(si, sj, 3, 0); n   dfs(si, sj, 4, 0); n   for (int way = 1; way <= 4; way++) n     for (int cnt = 0; cnt <= 2; cnt++) n       if (use[fi][fj][way][cnt]) { n         cout <<  YES ; n         return (0); n       } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 50, inf = 1e9; n int n, m, be[N], f[N], cnt, sub[N], SUB[N], vfa[N], q[N * 2], l, r; n bool vis[N], tp[N]; n vector<int> e[N], v[N], vv; n void cmax(int &x, int y) { y > x ? x = y : 0; } n void dfs1(int x) { n   vis[x] = 1; n   if (f[x]) e[x].erase(find(e[x].begin(), e[x].end(), f[x])); n   for (int i = 0, y; i < e[x].size(); i++) { n     if (!vis[y = e[x][i]]) n       f[y] = x, dfs1(y); n     else if (!be[y]) { n       be[y] = ++cnt; n       tp[y] = 1; n       v[cnt].push_back(y); n       for (int z = x; z != y; z = f[z]) be[z] = cnt, v[cnt].push_back(z); n     } n   } n   if (!be[x]) be[x] = ++cnt, tp[x] = 1, v[cnt].push_back(x); n } n void calc1(int x) { n   int id = be[x], sz = v[id].size(); n   for (int i = 0; i < sz; i++) { n     int mx0 = 0, mx1 = -inf; n     int z = v[id][i]; n     for (int j = 0, y; j < e[z].size(); j++) n       if (be[y = e[z][j]] != id) { n         int val = SUB[y] + 1; n         if (val > mx0) n           mx1 = mx0, mx0 = val; n         else if (val > mx1) n           mx1 = val; n       } n     sub[z] = mx0; n     cmax(SUB[x], mx0 + min(i, sz - i)); n     for (int j = 0, y; j < e[z].size(); j++) n       if (be[y = e[z][j]] != id) n         cmax(vfa[y], SUB[y] + 1 != mx0 ? mx0 + 1 : mx1 + 1); n   } n } n void dfs2(int x) { n   vis[x] = 1; n   for (int i = 0, y; i < e[x].size(); i++) n     if (!vis[y = e[x][i]]) dfs2(y); n   if (tp[x]) calc1(x); n } n void calc2(int x) { n   cmax(sub[x], vfa[x]); n   vv.clear(); n   int id = be[x], sz = v[id].size(); n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < v[id].size(); j++) vv.push_back(v[id][j]); n   l = 1; n   r = 0; n   for (int i = 0; i < vv.size(); i++) { n     if (i - q[l] > sz / 2) l++; n     if (l <= r) cmax(vfa[vv[i]], i - q[l] + sub[vv[q[l]]]); n     while (l <= r && sub[vv[q[r]]] - q[r] < sub[vv[i]] - i) r--; n     q[++r] = i; n   } n   l = 1; n   r = 0; n   for (int i = vv.size() - 1; ~i; i--) { n     if (q[l] - i > sz / 2) l++; n     if (l <= r) cmax(vfa[vv[i]], q[l] - i + sub[vv[q[l]]]); n     while (l <= r && sub[vv[q[r]]] + q[r] < sub[vv[i]] + i) r--; n     q[++r] = i; n   } n   for (int i = 0; i < v[id].size(); i++) n     for (int z = v[id][i], j = 0, y; j < e[z].size(); j++) n       if (be[y = e[z][j]] != id) cmax(vfa[y], vfa[z] + 1); n } n void dfs3(int x) { n   vis[x] = 1; n   if (tp[x]) calc2(x); n   for (int i = 0, y; i < e[x].size(); i++) n     if (!vis[y = e[x][i]]) dfs3(y); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, x, y; i <= m; i++) n     scanf( %d%d , &x, &y), e[x].push_back(y), e[y].push_back(x); n   dfs1(1); n   memset(vis, 0, sizeof(vis)); n   dfs2(1); n   memset(vis, 0, sizeof(vis)); n   dfs3(1); n   for (int i = 1; i <= n; i++) printf( %d  , max(sub[i], vfa[i])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void out(long long arr[], long long length) { n   for (int i = 0; i < length; i++) cout << arr[i] <<    ; n } n int main() { n   int tt; n   cin >> tt; n   while (tt--) { n     int x1, y1, z1, x2, y2, z2, ans; n     cin >> x1 >> y1 >> z1; n     cin >> x2 >> y2 >> z2; n     if (y2 >= z1) { n       int t1; n       ans = (z1 * 2); n       int d1 = (y2 - z1); n       if ((d1 + x2) >= y1) n         cout << ans; n       else { n         int d2 = y1 - (d1 + x2); n         t1 = (min(d2, z2) * 2); n         ans = ans - t1; n         cout << ans; n       } n     } else if (y2 < z1) { n       int t1; n       ans = (y2 * 2); n       int d1 = (z1 - y2); n       if ((d1 + x1) >= z2) n         cout << ans; n       else { n         int d2 = z2 - (d1 + x1); n         t1 = (min(d2, z2) * 2); n         ans = ans - t1; n         cout << ans; n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long getint() { n   long long _x = 0, _tmp = 1; n   char _tc = getchar(); n   while ((_tc <  0  || _tc >  9 ) && _tc !=  - ) _tc = getchar(); n   if (_tc ==  - ) _tc = getchar(), _tmp = -1; n   while (_tc >=  0  && _tc <=  9 ) _x *= 10, _x += (_tc -  0 ), _tc = getchar(); n   return _x * _tmp; n } n long long mypow(long long _a, long long _x, long long _mod) { n   if (_x == 0) return 1ll; n   long long _tmp = mypow(_a, _x / 2, _mod); n   _tmp = (_tmp * _tmp) % _mod; n   if (_x & 1) _tmp = (_tmp * _a) % _mod; n   return _tmp; n } n bool equal(double _x, double _y) { return _x > _y - 1e-7 && _x < _y + 1e-7; } n int __ = 1, cs; n int n, m, deg[100010]; n multiset<int> S[100010]; n vector<int> odd; n void build() {} n void init() { n   n = getint(); n   m = getint(); n   while (m--) { n     int u, v; n     u = getint(); n     v = getint(); n     S[u].insert(v); n     S[v].insert(u); n     deg[u]++; n     deg[v]++; n   } n   for (int i = 1; i <= n; i++) n     if (deg[i] % 2) odd.push_back(i); n   if ((int)odd.size() % 2) assert(1 == 0); n   for (int i = 0, j = (int)odd.size() - 1; i < j; i++, j--) { n     S[odd[i]].insert(odd[j]); n     S[odd[j]].insert(odd[i]); n   } n } n vector<pair<int, int> > ans; n int got; n void DFS(int now) { n   while (S[now].size()) { n     multiset<int>::iterator it = S[now].begin(); n     int des = *it; n     S[now].erase(S[now].find(des)); n     S[des].erase(S[des].find(now)); n     DFS(des); n     got++; n     if (got % 2) n       ans.push_back(make_pair(des, now)); n     else n       ans.push_back(make_pair(now, des)); n   } n } n void find_circle() { n   for (int i = 1; i <= n; i++) n     while ((int)S[i].size() > 0) { n       got = 0; n       DFS(i); n       if (got % 2) ans.push_back(make_pair(i, i)); n     } n } n void solve() { n   find_circle(); n   printf( %d n , (int)ans.size()); n   for (int i = 0; i < (int)ans.size(); i++) n     printf( %d %d n , ans[i].first, ans[i].second); n } n int main() { n   build(); n   while (__--) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const long long MOD = 1e9 + 7; n vector<int> g[N]; n bool col[N]; n int her[N], p[N]; n set<int> s; n queue<int> q; n bool mp[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   bool bl = false; n   for (int i = 0; i < (n); ++i) { n     int tv; n     cin >> tv; n     vector<int> vc; n     for (int j = 2; j * j <= tv; j++) { n       if (tv % j == 0) { n         int qan = 0; n         while (tv % j == 0) { n           tv /= j; n           qan++; n         } n         if (qan % 2) vc.push_back(j); n       } n     } n     if (tv != 1) vc.push_back(tv); n     if (vc.size() == 0) { n       cout << 1 << endl; n       return 0; n     } n     if (vc.size() == 1) vc.push_back(1); n     int z = vc[0] * vc[1]; n     if (mp[z]) bl = true; n     mp[z] = true; n     g[vc[0]].push_back(vc[1]); n     g[vc[1]].push_back(vc[0]); n     if (vc[0] <= 1000) s.insert(vc[0]); n     if (vc[1] <= 1000) s.insert(vc[1]); n   } n   if (bl) { n     cout << 2 << endl; n     return 0; n   } n   int ans = INT_MAX; n   for (auto tv : s) { n     q.push(tv); n     memset(her, 0, sizeof(her)); n     memset(col, 0, sizeof(col)); n     memset(p, 0, sizeof(p)); n     col[tv] = true; n     her[tv] = 0; n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (auto to : g[v]) { n         if (col[to]) { n           if (to == p[v]) continue; n           ans = min(ans, her[to] + her[v] + 1); n           continue; n         } n         q.push(to); n         col[to] = true; n         her[to] = her[v] + 1; n         p[to] = v; n       } n     } n   } n   if (ans == INT_MAX) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool maximize(T &x, const T &y) { n   if (x < y) n     x = y; n   else n     return false; n   return true; n } n const int N = 5e5 + 10; n int n, q, md; n int dx[N]; n int d[N][21]; n int query(int x, int y) { n   if (d[x][20] < y) return -1; n   int ret = 0; n   for (int i = 20; i >= 0; i--) n     if (d[x][i] < y) { n       x = d[x][i]; n       ret += 1 << i; n     } n   return ret + 1; n } n int main() { n   if (fopen( exam.inp ,  r )) n     freopen( exam.inp ,  r , stdin), freopen( exam.out ,  w , stdout); n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     maximize(dx[x], y); n     maximize(md, y); n   } n   for (int i = 1; i <= md; i++) maximize(dx[i], dx[i - 1]); n   for (int i = 0; i <= md; i++) d[i][0] = dx[i]; n   for (int j = 1; j <= 20; j++) n     for (int i = 0; i <= md; i++) d[i][j] = d[d[i][j - 1]][j - 1]; n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     printf( %d n , query(x, y)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n const T& max(const T& a, const T& b, const T& c) { n   return max(a, max(b, c)); n } n template <class T> n const T& min(const T& a, const T& b, const T& c) { n   return min(a, min(b, c)); n } n long long stoi(string& str) { n   istringstream second(str); n   long long i; n   second >> i; n   return i; n } n string itos(long long i) { n   stringstream second; n   second << i; n   return second.str(); n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long poww(long long a, long long b) { n   if (b == 0) return 1; n   long long tmp = poww(a, b / 2); n   return (b & 1 ? a * tmp * tmp : tmp * tmp); n } n long long sumOfDigs(string s) { n   long long sum = 0; n   for (int i = 0; i < s.length(); i++) sum += s[i] -  0 ; n   return sum; n } n long long sumOfDigs(long long n) { n   return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); n } n long long mod = poww(10, 9) + 7; n int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n using namespace std; n void t() { freopen( test.txt ,  r , stdin); } n long long a[1000 * 100 + 1][20]; n pair<long long, long long> aa[1000 * 100 + 1][20]; n int main() { n   string t1, t2, s, tt, second; n   cin >> t1 >> t2; n   s += t1; n   reverse(t2.begin(), t2.end()); n   s += t2; n   cin >> t1 >> t2; n   tt += t1; n   reverse(t2.begin(), t2.end()); n   tt += t2; n   second = s; n   if (second == tt) { n     return cout <<  YES  << endl, 0; n   } n   int pos = second.find( X ); n   if (pos < 3) { n     swap(second[pos], second[pos + 1]); n     pos++; n   } else { n     swap(second[pos], second[0]); n     pos = 0; n   } n   if (second == tt) { n     return cout <<  YES  << endl, 0; n   } n   while (second != s) { n     if (pos < 3) { n       swap(second[pos], second[pos + 1]); n       pos++; n     } else { n       swap(second[pos], second[0]); n       pos = 0; n     } n     if (second == tt) { n       return cout <<  YES  << endl, 0; n     } n   } n   return cout <<  NO  << endl, 0; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( O3 ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, x; n     cin >> n >> x; n     string s; n     cin >> s; n     int a[100001] = {0}; n     int b[100001] = {0}; n     int f = 0; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  0 ) { n         a[i + 1] = a[i] + 1; n         b[i + 1] = b[i]; n       } else { n         a[i + 1] = a[i]; n         b[i + 1] = b[i] + 1; n       } n     } n     long long ans = 0; n     int c0 = 1; n     for (int i = 1; i <= n; i++) { n       if (a[i] - b[i] == 0) { n         c0++; n       } n       int g = a[i] - b[i]; n       long double h; n       h = (x - g); n       h *= 1.0; n       if (a[n] - b[n] == 0) { n         if (g == x) ans++; n         continue; n       } n       h /= (long double)(a[n] - b[n]); n       if (h < 0) continue; n       if (ceil(h) == floor(h)) { n         ans++; n       } n     } n     if (a[n] - b[n] == 0) { n       for (int i = 1; i <= n; i++) n         if (a[i] - b[i] == x) { n           cout << -1 <<   n ; n           f = 1; n           break; n         } n     } n     if (x == 0) ans++; n     if (f) continue; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int v[1100][1100]; n int main() { n   int n, k; n   cin >> n >> k; n   if (n > k + k) { n     cout << n * k << endl; n     for (int i = 1; i <= n; i++) { n       int t = k; n       for (int j = 1; j <= n; j++) n         if (i != j && !v[i][j] && !v[j][i] && t) { n           t--; n           v[i][j] = v[j][i] = 1; n           printf( %d %d n , i, j); n         } n     } n   } else n     printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e18; n long long int mul(long long int a, long long int b) { n   return ((a % mod) * (b % mod)) % mod; n } n long long int add(long long int a, long long int b) { n   return ((a % mod) + (b % mod)) % mod; n } n long long int sub(long long int a, long long int b) { n   return ((a % mod) - (b % mod) + mod) % mod; n } n long long int gcdExtended(long long int a, long long int b, long long int *x, n                           long long int *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long int x1, y1; n   long long int gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long int modInverse(long long int b) { n   long long int x, y; n   long long int g = gcdExtended(b, mod, &x, &y); n   if (g != 1) return -1; n   return (x % mod + mod) % mod; n } n long long int modDivide(long long int a, long long int b) { n   a = a % mod; n   long long int inv = modInverse(b); n   if (inv == -1) n     return -1; n   else n     return (inv * a) % mod; n } n long long int expFactor2(long long int n, long long int p) { n   long long int exponent = 0; n   while (n % p == 0) { n     exponent++; n     n /= p; n   } n   return exponent; n } n void sieve(long long int n, vector<bool> &prime, vector<long long int> &p) { n   prime[1] = 0; n   for (long long int i = 2; i <= n; i++) { n     if (prime[i]) { n       for (long long int j = i; j <= n; j += i) { n         if (j > i) prime[j] = 0; n         p[j] += expFactor2(j, i); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, t, j, inp, n, a, b; n   cin >> t; n   vector<long long int> p(5000001, 0); n   vector<bool> prime(5000001, 1); n   sieve(5000001, prime, p); n   for (i = 2; i <= 5000000; i++) { n     p[i] += p[i - 1]; n   } n   for (i = 0; i < t; i++) { n     cin >> a >> b; n     long long int ans = 0; n     ans = p[a] - p[b]; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int n, k, m; n int a[N]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n); n   long long ans = 0; n   ans += 1ll * (m / (k + 1)) * (1ll * k * a[n] + a[n - 1]); n   ans += 1ll * (m % (k + 1)) * a[n]; n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n int val[10010], nval[10010], pos[10010], buf[10010]; n int main() { n   scanf( %d%d , &N, &K); n   for (int i = 0; i < N - 1; ++i) val[i] = 1; n   int br = 0; n   for (;;) { n     for (int i = 0; i < N; ++i) pos[i] = -1, nval[i] = val[i]; n     for (int i = N - 1; i >= 0; --i) n       if (pos[val[i]] == -1) pos[val[i]] = i; n     if (pos[N - 1] != -1) break; n     ++br; n     for (int i = N - 1; i >= 0; --i) { n       int tar = N - 1 - i; n       if (val[i] == tar) n         buf[i] = N - 1; n       else { n         if (pos[tar - val[i]] != -1) n           buf[i] = pos[tar - val[i]], nval[i] = tar; n         else n           buf[i] = buf[i + 1], nval[i] += val[buf[i]]; n       } n     } n     for (int i = 0; i < N; ++i) n       printf( %d%c , buf[i] + 1, (i + 1 == N ?   n  :    )); n     for (int i = 0; i < N; ++i) val[i] = nval[i]; n   } n   while (br < K) { n     ++br; n     for (int i = 0; i < N; ++i) printf( %d%c , N, (i + 1 == N ?   n  :    )); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const double PI = 3.14159265358979323846; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   vector<int> bit(64); n   LL sum = 0, ans = 0; n   for (int i = 0; i < int(n); ++i) { n     LL num; n     cin >> num; n     sum += num; n     ans ^= num; n   } n   cout << 2 << endl; n   cout << ans <<     << ans + sum << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const mxsz = 509; n long long w[mxsz][mxsz]; n bool ver[mxsz]; n int main() { n   vector<int> er; n   vector<long long> res; n   int n; n   scanf( %d , &n); n   er.resize(n); n   for (int i = 0; i < n; ++i) { n     int x; n     for (int j = 0; j < n; ++j) { n       scanf( %d , &x); n       w[i][j] = x; n     } n   } n   for (int i = 0; i < n; ++i) scanf( %d , &er[i]); n   reverse(er.begin(), er.end()); n   for (int k = 0; k < n; ++k) { n     er[k]--; n     ver[er[k]] = 1; n     long long R = 0; n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < n; ++j) n         w[i][j] = min(w[i][j], w[i][er[k]] + w[er[k]][j]); n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < n; ++j) n         if (ver[i] && ver[j]) R += w[i][j]; n     res.push_back(R); n   } n   for (int i = n - 1; i >= 0; --i) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n vector<vector<long long>> g; n vector<long long> a; n vector<long long> ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   g = vector<vector<long long>>(n + 1); n   a = vector<long long>(n + 1); n   ans = vector<long long>(n + 1, LLONG_MAX); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (i - a[i] >= 1) { n       g[i - a[i]].push_back(i); n     } n     if (i + a[i] <= n) { n       g[i + a[i]].push_back(i); n     } n   } n   set<long long> toUp; n   for (int i = 1; i <= n; i++) { n     for (auto x : g[i]) { n       if (a[x] % 2 != a[i] % 2) { n         ans[x] = 1; n         toUp.insert(x); n       } n     } n   } n   while (!toUp.empty()) { n     vector<long long> temp(toUp.begin(), toUp.end()); n     toUp.clear(); n     for (auto i : temp) { n       for (auto x : g[i]) { n         if (ans[x] > ans[i] + 1) { n           ans[x] = ans[i] + 1; n           toUp.insert(x); n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (ans[i] == LLONG_MAX) n       cout <<  -1  ; n     else n       cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n char map[101][10001]; n long long mic[101][10001]; n int n, m; n long long max = 1000000000; n int check = 0; n void find(int x) { n   int sum = 0; n   for (int i = 0; i < n; i++) { n     sum += mic[i][x]; n     if (sum > max) return; n   } n   max = sum; n } n int main() { n   scanf( %d %d  , &n, &m); n   for (int i = 0; i < n; i++) { n     gets(map[i]); n     int temp = 0; n     int mul = 0; n     for (int j = 0; j < m; j++) mic[i][j] = 1000000000; n     for (int j = 0; j <= m * 2; j++) { n       if (map[i][j % m] ==  1 ) { n         temp = 0; n         mul = 1; n       } n       if ((temp < mic[i][j % m] && mul == 1) || mic[i][j % m] == 0) n         mic[i][j % m] = temp; n       temp += mul; n     } n     temp = 0; n     mul = 0; n     for (int j = (2 * m); j >= 0; j--) { n       if (map[i][j % m] ==  1 ) { n         temp = 0; n         mul = 1; n       } n       if ((temp < mic[i][j % m] && mul == 1) || mic[i][j % m] == 0) n         mic[i][j % m] = temp; n       temp += mul; n     } n   } n   for (int i = 0; i < m; i++) find(i); n   if (max >= 1000000000) n     printf( -1 ); n   else n     printf( %d , max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000003; n int a[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int ans = 0; n   int N = 1000000; n   while (n--) { n     int x; n     cin >> x; n     a[x]++; n     ans = max(ans, a[x]); n     for (int i = x * 2; i <= N; i += x) a[i] = max(a[i], a[x]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return x * f; n } n inline long long readll() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return x * f; n } n inline void write(int x) { n   if (!x) putchar( 0 ); n   if (x < 0) x = -x, putchar( - ); n   static int sta[20]; n   register int tot = 0; n   while (x) sta[tot++] = x % 10, x /= 10; n   while (tot) putchar(sta[--tot] + 48); n } n const int maxn = 2007, INF = 0x3f3f3f3f; n const int MOD = 998244353; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n const long long P = 19260817; n inline int mod1(int x) { return x < MOD ? x : x - MOD; } n inline int mod2(int x) { return x < 0 ? x + MOD : x; } n inline void add(int &x, int y) { x = mod1(x + y); } n inline void sub(int &x, int y) { x = mod2(x - y); } n struct node { n   int x, y, dis; n   friend bool operator<(const node &a, const node &b) { n     if (a.dis != b.dis) return a.dis < b.dis; n     if (a.x != b.x) return a.x < b.x; n     return a.y < b.y; n   } n }; n inline void chkmin(node &a, node b) { a = min(a, b); } n int n, m, K; n int fal[maxn][maxn]; n int far[maxn][maxn]; n int fau[maxn][maxn]; n int fad[maxn][maxn]; n int find(int x, int *fa) { return x == fa[x] ? x : fa[x] = find(fa[x], fa); } n void solve(int x, int y) { n   if (fal[x][y] == y) { n     printf( %d %d n , x, y); n     fal[x][y] = y - 1; n     far[x][y] = y + 1; n     fau[y][x] = x - 1; n     fad[y][x] = x + 1; n     return; n   } n   node ans = (node){0, 0, INF}; n   for (int i = x; i <= min(x + (int)sqrt(K), n); i++) { n     int t = y; n     t = find(t, fal[i]); n     if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)}); n     t = y; n     t = find(t, far[i]); n     if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)}); n     if (y == fal[i][y]) break; n   } n   for (int i = x; i >= max(x - (int)sqrt(K), 1); i--) { n     int t = y; n     t = find(t, fal[i]); n     if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)}); n     t = y; n     t = find(t, far[i]); n     if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)}); n     if (y == fal[i][y]) break; n   } n   for (int i = y; i <= min(y + (int)sqrt(K), m); i++) { n     int t = x; n     t = find(t, fau[i]); n     if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)}); n     t = x; n     t = find(t, fad[i]); n     if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)}); n     if (x == fau[i][x]) break; n   } n   for (int i = y; i >= max(y - (int)sqrt(K), 1); i--) { n     int t = x; n     t = find(t, fau[i]); n     if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)}); n     t = x; n     t = find(t, fad[i]); n     if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)}); n     if (x == fau[i][x]) break; n   } n   printf( %d %d n , ans.x, ans.y); n   fal[ans.x][ans.y] = ans.y - 1; n   far[ans.x][ans.y] = ans.y + 1; n   fau[ans.y][ans.x] = ans.x - 1; n   fad[ans.y][ans.x] = ans.x + 1; n } n int main() { n   scanf( %d%d%d , &n, &m, &K); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       fal[i][j] = j, far[i][j] = j; n       fau[j][i] = i, fad[j][i] = i; n     } n   } n   for (int i = 1, x, y; i <= K; i++) { n     scanf( %d%d , &x, &y); n     solve(x, y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   bool op = false; n   char c = getchar(); n   while (!isdigit(c)) op |= (c ==  - ), c = getchar(); n   while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   return op ? -x : x; n } n const int N = 2e5 + 10; n int n, m, B; n int dif[N], d[500][500], lst[N]; n struct Node { n   int x, y; n   Node() {} n   Node(int x, int y) : x(x), y(y) {} n } a[N]; n void update(int t, int x, int v) { n   int p = a[x].x + a[x].y; n   int l = (t + a[x].x) % p, r = (t - 1) % p; n   if (l <= r) n     for (int i = l; i <= r; i++) d[p][i] += v; n   else { n     for (int i = 0; i <= r; i++) d[p][i] += v; n     for (int i = l; i < p; i++) d[p][i] += v; n   } n } n int main() { n   n = read(); n   m = read(); n   B = sqrt(m); n   for (int i = 1; i <= n; i++) { n     int x = read(), y = read(); n     a[i] = Node(x, y); n   } n   int ans = 0; n   for (int t = 1; t <= m; t++) { n     int op = read(), x = read(); n     if (op == 1) { n       if (a[x].x + a[x].y > B) { n         for (int i = t + a[x].x; i <= m; i += a[x].x + a[x].y) { n           dif[i]++; n           if (i + a[x].y <= m) dif[i + a[x].y]--; n         } n       } else n         update(t, x, 1); n       lst[x] = t; n     } else { n       if (a[x].x + a[x].y > B) { n         for (int i = lst[x] + a[x].x; i <= m; i += a[x].x + a[x].y) { n           dif[i]--; n           if (i + a[x].y <= m) dif[i + a[x].y]++; n           if (i < t && i + a[x].y >= t) ans--; n         } n       } else n         update(lst[x], x, -1); n     } n     int res = 0; n     ans += dif[t]; n     for (int i = 1; i <= B; i++) res += d[i][t % i]; n     printf( %d n , res + ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long LLINF = (long long)1000000000 * 1000000010; n const int MAXN = 410; n int g[MAXN], g1[MAXN]; n int c[MAXN]; n int M, m; n int solve(int x, int *a) { n   int ans = 0; n   for (int i = 0; i < m; i++) { n     a[i] = x / c[i]; n     x %= c[i]; n     ans += a[i]; n   } n   return ans; n } n int main() { n   int t, good; n   while (scanf( %d , &m) != EOF) { n     for (int i = 0; i < m; i++) scanf( %d , &c[i]); n     sort(c, c + m, greater<int>()); n     good = 1; n     long long best = LLINF; n     for (int i = 1; i < m; i++) { n       solve(c[i - 1] - 1, g); n       for (int j = i; j < m; j++) { n         long long w = 0, v = 0; n         for (int k = 0; k < j; k++) { n           w += (long long)g[k] * c[k]; n           v += g[k]; n         } n         w += (long long)(g[j] + 1) * c[j]; n         v += g[j] + 1; n         if (solve(w, g1) > v) { n           good = 0; n           if (w < best) best = w; n         } n       } n     } n     if (good) n       cout << -1 << endl; n     else n       cout << best << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n, h, x, r = 0; n   cin >> n >> h; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x > h) { n       r += 2; n     } else { n       r += 1; n     } n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1E3 + 5; n int n; n struct line { n   int x, y, w, num; n   line(int a = 0, int b = 0, int c = 0, int d = 0) : x(a), y(b), w(c), num(d) {} n   bool operator<(const line& A) const { return w > A.w; } n } a[maxn], b[maxn]; n vector<line> ans; n inline bool inter(line x, line y) { n   int l1 = min(x.x, x.y), r1 = max(x.x, x.y); n   int l2 = min(y.x, y.y), r2 = max(y.x, y.y); n   return (l1 < l2 && l2 < r1 && r1 < r2) || (l2 < l1 && l1 < r2 && r2 < r1); n } n inline int dis(line A, int x) { n   int l = min(A.x, A.y), r = max(A.x, A.y); n   if (l <= x && x <= r) return n - (r - l); n   return r - l; n } n inline void solve(int x, int y) { n   vector<line> wait; n   for (int i = 1; i <= n - 3; ++i) n     if (inter(a[i], line(x, y))) n       wait.push_back(line(a[i].x, a[i].y, dis(a[i], x), i)); n   if (wait.size() == 0) return; n   sort(wait.begin(), wait.end()); n   for (int i = 0; i < wait.size() - 1; ++i) { n     ans.push_back(wait[i]); n     int p = wait[i].num; n     if (wait[i].x == wait[i + 1].x) { n       a[p].x = wait[i + 1].y; n       a[p].y = x; n     } else if (wait[i].y == wait[i + 1].x) { n       a[p].y = wait[i + 1].y; n       a[p].x = x; n     } else if (wait[i].x == wait[i + 1].y) { n       a[p].x = wait[i + 1].x; n       a[p].y = x; n     } else { n       a[p].y = wait[i + 1].x; n       a[p].x = x; n     } n   } n   ans.push_back(wait[wait.size() - 1]); n   int p = wait[wait.size() - 1].num; n   a[p].x = x, a[p].y = y; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n - 3; ++i) cin >> a[i].x >> a[i].y; n   for (int i = 1; i <= n - 3; ++i) cin >> b[i].x >> b[i].y; n   random_shuffle(b + 1, b + n - 3 + 1); n   for (int i = 1; i <= n - 3; ++i) solve(b[i].x, b[i].y); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); ++i) n     cout << ans[i].x <<     << ans[i].y <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 100; n const long long INF = 1e13; n int u, v, c, b, laz[MAXN], C[MAXN], n, m, st[MAXN]; n bool vis[MAXN]; n multiset<pair<int, int> > Q[MAXN]; n multiset<pair<int, int> >::iterator it; n vector<pair<int, int> > W[MAXN]; n vector<int> G[MAXN]; n pair<int, int> k; n void merg(int v, int u) { n   if (Q[v].size() >= Q[u].size()) { n     while (!(Q[u].empty())) { n       it = Q[u].begin(); n       int i = (*(it)).second; n       C[i] += laz[u] - laz[v]; n       c = (*it).first; n       Q[v].insert({c + laz[u] - laz[v], i}); n       Q[u].erase(it); n     } n     return; n   } n   merg(u, v); n   Q[v].swap(Q[u]); n   laz[v] = laz[u]; n   return; n } n long long ans = 0; n int TIM = 0; n bool dfs(int v, int f = -1) { n   st[v] = TIM; n   TIM++; n   for (auto u : G[v]) { n     if (u == f) { n       continue; n     } n     bool r = dfs(u, v); n     if (!r) { n       return 0; n     } n     merg(v, u); n   } n   for (auto k : W[v]) { n     if (st[k.first] < st[v]) { n       C[k.second] -= laz[v]; n       Q[v].insert({C[k.second], k.second}); n     } else { n       Q[v].erase({C[k.second], k.second}); n     } n   } n   if (!v) { n     return 1; n   } n   if (!(Q[v].size())) { n     return 0; n   } n   int h = (*(Q[v].begin())).first; n   ans += (h + laz[v]); n   laz[v] -= (h + laz[v]); n   return 1; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     u--; n     v--; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 0; i < m; i++) { n     cin >> u >> v >> C[i]; n     u--; n     v--; n     W[u].push_back({v, i}); n     W[v].push_back({u, i}); n   } n   bool r = dfs(0); n   if (!r) { n     cout << -1 << endl; n     return 0; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int v, d; n   bool operator<(const node &a) const { n     if (d == a.d) return v < a.v; n     return a.d < d; n   } n }; n set<node> s; n vector<int> adj[200005]; n int d[200005], p[200005]; n bool vis[200005]; n void dfs(int u) { n   int l = adj[u].size(); n   node nd; n   vis[u] = true; n   for (int a = 0; a < l; a++) { n     int v = adj[u][a]; n     if (!vis[v] && v != p[u]) dfs(v); n   } n   nd.v = u, nd.d = d[u]; n   s.erase(nd); n } n int main() { n   int n, ans = 0; n   cin >> n; n   for (int a = 1; a < n; a++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   queue<int> q; n   node nd; n   q.push(1); n   d[1] = p[1] = 0, vis[1] = true; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     nd.v = u, nd.d = d[u]; n     s.insert(nd); n     int l = adj[u].size(); n     for (int a = 0; a < l; a++) { n       int v = adj[u][a]; n       if (vis[v]) continue; n       d[v] = 1 + d[u], p[v] = u; n       q.push(v), vis[v] = true; n     } n   } n   memset(vis, false, sizeof(vis)); n   while (!s.empty()) { n     nd = *(s.begin()); n     if (nd.d > 2) { n       dfs(p[nd.v]), ans++; n       nd.v = p[p[nd.v]]; n       nd.d = d[nd.v]; n       s.erase(nd); n     } else n       break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[1000005]; n int n, L[1000005], R[1000005], ind; n void dfs(int u, int pre) { n   for (int i = G[u].size() - 1; i >= 0; i--) { n     int v = G[u][i]; n     if (v != pre) L[v] = ++ind; n   } n   R[u] = ++ind; n   for (auto v : G[u]) n     if (v != pre) dfs(v, u); n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   L[1] = ++ind; n   dfs(1, 1); n   for (int i = 1; i <= n; i++) cout << L[i] <<     << R[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long A, B; n     cin >> A >> B; n     long long hb = (floor(log10(B + 1)) == log10(B + 1)) ? (floor(log10(B)) + 1) n                                                          : floor(log10(B)); n     cout << hb * A << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1010][1010]; n int p[1010][1010]; n int w[1010]; n int n, m; n int ok(int d) { n   int cur, i, j, k; n   for (i = 0; i < m; i++) n     for (j = 0; j < m; j++) p[i][j] = 0; n   for (i = 0; i < n; i++) { n     cur = 0; n     for (j = 0; j < m; j++) n       if (a[i][j] >= d) w[cur++] = j; n     for (j = 0; j < cur; j++) { n       for (k = 0; k < j; k++) { n         if (1 == p[w[k]][w[j]]) return 1; n         p[w[k]][w[j]] = 1; n       } n     } n   } n   return 0; n } n int main() { n   int i, j; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) scanf( %d , &a[i][j]); n   int l, r, res, mid; n   res = 0; n   l = 0; n   r = 1000000000; n   ok(11); n   while (l < r) { n     mid = (l + r + 1) / 2; n     if (1 == ok(mid)) { n       res = mid; n       l = mid; n     } else n       r = mid - 1; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n int main() { n   cin.tie(nullptr)->sync_with_stdio(false); n   int T; n   vector<int> f(3e5 + 5); n   f[0] = 0; n   for (int i = 1; i < 3e5 + 5; i++) f[i] = f[i - 1] ^ i; n   for (cin >> T; T; T--) { n     int a, b; n     cin >> a >> b; n     int ans = a; n     if (f[a - 1] == 0) { n       if (f[a - 1] == b) n         ans = ans; n       else if (a == b) n         ans += 2; n       else n         ans++; n     } else { n       if (f[a - 1] == b) n         ans = ans; n       else if ((f[a - 1] ^ b) != a) n         ans++; n       else n         ans += 2; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> S; n string line[1010], a[1010], s; n int n = 0, ans = 0, file = 0; n int main() { n   while (getline(cin, s)) line[++n] = s; n   for (int i = 1; i <= n; i++) { n     int cur = 0; n     a[i] =   ; n     for (int j = 0; j < line[i].size(); j++) { n       a[i] += line[i][j]; n       if (line[i][j] ==     ) cur++; n       if (cur == 2) break; n     } n   } n   for (int i = 1; i <= n; i++) { n     string fd = a[i]; n     S.clear(); n     int files = 0, F = 0; n     for (int j = 1; j <= n; j++) n       if (a[j] == fd) { n         files++; n         string v =   ; n         int cur = 0; n         for (int k = 0; k < line[j].size(); k++) { n           v += line[j][k]; n           if (line[j][k] ==     ) cur++; n           if (line[j][k] ==      && cur > 2) n             if (!S.count(v)) S.insert(v), F++; n         } n       } n     ans = max(ans, F); n     file = max(file, files); n   } n   cout << ans <<     << file << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   stack<pair<int, int> > S; n   S.push(make_pair(0, -1)); n   vector<pair<int, int> > ans; n   for (int i = 0; i <= n; i++) { n     int a; n     if (i < n) n       cin >> a; n     else n       a = 0; n     while (S.top().first > a) { n       pair<int, int> last = S.top(); n       S.pop(); n       ans.push_back(make_pair(last.second, i - 1)); n       last.first--; n       if (last.first > S.top().first) S.push(last); n     } n     if (S.top().first != a) S.push(make_pair(a, i)); n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first + 1 <<     << ans[i].second + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool ok[10]; n bool judge(char str[], int k) { n   int len = strlen(str); n   memset(ok, false, sizeof(ok)); n   for (int i = 0; i < len; i++) { n     ok[(str[i] -  0 )] = true; n   } n   for (int i = 0; i <= k; i++) { n     if (!ok[i]) { n       return false; n     } n   } n   return true; n } n int main() { n   int n, k; n   char str[50]; n   ios::sync_with_stdio(false); n   while (cin >> n >> k) { n     int res = 0; n     for (int i = 0; i < n; i++) { n       cin >> str; n       if (judge(str, k)) { n         res++; n       } n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   long long sum = 0; n   for (int i = 0; i < n; ++i) { n     int t1; n     cin >> t1; n     sum += t1; n   } n   if (sum % n) n     cout << n - 1 << endl; n   else n     cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long& i : a) cin >> i; n   vector<long long> pre(n), suf(n); n   long long cnt{1}; n   pre[0] = 1; n   for (long long i = 1; i < n; i++) { n     if (a[i] > a[i - 1]) { n       cnt++; n     } else { n       cnt = 1; n     } n     pre[i] = cnt; n   } n   suf[n - 1] = 1; n   cnt = 1; n   for (long long i = n - 2; i >= 0; i--) { n     if (a[i] < a[i + 1]) { n       cnt++; n     } else { n       cnt = 1; n     } n     suf[i] = cnt; n   } n   long long mx{1}; n   for (long long i = 0; i < n - 2; i++) { n     long long x = pre[i]; n     long long y = suf[i + 2]; n     if (a[i] + 1 < a[i + 2]) { n       mx = max(mx, x + y + 1); n     } else { n       mx = max(mx, max(pre[i] + 1, suf[i + 2] + 1)); n     } n   } n   if (n >= 2) { n     mx = max(mx, suf[1] + 1); n     mx = max(mx, pre[n - 2] + 1); n   } n   cout << mx; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111111; n int size[3]; n int colour[N]; n bool ok = false; n bool used[N]; n long long ans = 0; n vector<int> edge[N]; n void dfs(int u) { n   used[u] = true; n   ans += size[colour[u]]; n   size[colour[u]]++; n   for (int i = 0; i < edge[u].size(); i++) { n     int temp = edge[u][i]; n     if (used[temp] == false) { n       colour[temp] = colour[u] ^ 1; n       dfs(temp); n     } else if (colour[temp] != (colour[u] ^ 1)) { n       ok = true; n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     edge[u].push_back(v); n     edge[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     if (used[i] == false) { n       size[0] = size[1] = 0; n       dfs(i); n     } n   } n   if (ok == true) { n     cout <<  0 1  << endl; n   } else if (ans == 0) { n     if (m == 0) { n       ans = (long long)n * (n - 1) * (n - 2); n       ans /= 6; n       cout <<  3   << ans << endl; n     } else { n       cout <<  2   << (long long)m * (n - 2) << endl; n     } n   } else { n     cout <<  1   << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   vector<int> d(5000, 0); n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       ++d[abs(a[i] - a[j])]; n     } n   } n   vector<int> c(5000); n   c[4999] = 0; n   for (int i = 4998; i >= 0; --i) { n     c[i] = c[i + 1] + d[i + 1]; n   } n   long long good = 0; n   for (int d1 = 1; d1 < 5000; ++d1) { n     for (int d2 = 1; d2 < 5000 - d1; ++d2) { n       good += static_cast<long long>(d[d1]) * d[d2] * c[d1 + d2]; n     } n   } n   long long all = n * (n - 1) / 2; n   all = all * all * all; n   cout.precision(10); n   cout << fixed n        << static_cast<long double>(good) / static_cast<long double>(all); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 5; n const int mod = 1e9 + 7; n const double esp = 1e-10; n const long double pi = 4 * atan((long double)1); n int n; n long long d; n struct mido { n   long long l, r, val; n } a[M]; n vector<pair<long long, int> > v; n pair<int, int> f[12 * M]; n int trace[M]; n set<long long> s; n void upd(int id, int l, int r, int i, int k, int vt) { n   if (l > r) return; n   if (r < i || i < l) return; n   if (l == r) { n     if (f[id].first < k) f[id] = pair<int, int>(k, vt); n     return; n   } n   int mid = (l + r) / 2; n   upd(id * 2, l, mid, i, k, vt); n   upd(id * 2 + 1, mid + 1, r, i, k, vt); n   if (f[id * 2].first > f[id * 2 + 1].first) { n     f[id] = f[id * 2]; n   } else n     f[id] = f[id * 2 + 1]; n } n pair<int, int> get(int id, int l, int r, int u, int v) { n   if (l > r) return pair<int, int>(0, 0); n   if (r < u || v < l) return pair<int, int>(0, 0); n   if (u <= l && r <= v) { n     return f[id]; n   } n   int mid = (l + r) / 2; n   pair<int, int> k1 = get(id * 2, l, mid, u, v); n   pair<int, int> k2 = get(id * 2 + 1, mid + 1, r, u, v); n   if (k1.first > k2.first) return k1; n   return k2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> d; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].val; n     v.push_back(pair<long long, int>(a[i].val, i)); n     v.push_back(pair<long long, int>(a[i].val - d, i + n)); n     v.push_back(pair<long long, int>(a[i].val + d, i + 2 * n)); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < 3 * n; i++) { n     s.insert(v[i].first); n     int vt = v[i].second; n     if (vt <= n) n       a[vt].val = s.size(); n     else if (vt <= 2 * n) n       a[vt - n].l = s.size(); n     else n       a[vt - 2 * n].r = s.size(); n   } n   int ans = 0, last; n   for (int i = 1; i <= n; i++) { n     pair<int, int> k1 = get(1, 1, 3 * n, 1, a[i].l); n     pair<int, int> k2 = get(1, 1, 3 * n, a[i].r, 3 * n); n     if (k1.first > k2.first) { n       upd(1, 1, 3 * n, a[i].val, k1.first + 1, i); n       trace[i] = k1.second; n       if (ans < k1.first + 1) { n         ans = k1.first + 1; n         last = i; n       } n     } else { n       upd(1, 1, 3 * n, a[i].val, k2.first + 1, i); n       trace[i] = k2.second; n       if (ans < k2.first + 1) { n         ans = k2.first + 1; n         last = i; n       } n     } n   } n   cout << ans << endl; n   vector<int> res; n   while (last > 0) { n     res.push_back(last); n     last = trace[last]; n   } n   for (int i = res.size() - 1; i >= 0; i--) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200000; n bool active[MAXN + 10]; n int v[MAXN + 10]; n vector<int> factors[MAXN + 10]; n int cantidad[5000001]; n int main() { n   int n, q; n   scanf( %d %d , &n, &q); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i]); n     int aux = v[i]; n     for (int j = 2; j * j <= aux; j++) { n       if (aux % j == 0) { n         factors[i].push_back(j); n         while (aux % j == 0) aux /= j; n       } n     } n     if (aux > 1) factors[i].push_back(aux); n   } n   long long bad = 0; n   int cnt = 0, ones = 0; n   for (int i = 0; i < q; i++) { n     int x; n     scanf( %d , &x); n     x--; n     if (active[x]) { n       if (v[x] == 1) { n         ones--; n       } else { n         cnt--; n         int sz = factors[x].size(); n         for (int j = 1; j < (1 << sz); j++) { n           int counter = 0; n           int product = 1; n           for (int k = 0; k < sz; k++) { n             if (j & (1 << k)) { n               counter++; n               product = product * factors[x][k]; n             } n           } n           cantidad[product]--; n           if (counter & 1) n             bad -= cantidad[product]; n           else n             bad += cantidad[product]; n         } n       } n     } else { n       if (v[x] == 1) n         ones++; n       else { n         cnt++; n         int sz = (int)factors[x].size(); n         for (int j = 1; j < (1 << sz); j++) { n           int counter = 0; n           int product = 1; n           for (int k = 0; k < sz; k++) { n             if (j & (1 << k)) { n               counter++; n               product = product * factors[x][k]; n             } n           } n           if (counter & 1) n             bad += cantidad[product]; n           else n             bad -= cantidad[product]; n           cantidad[product]++; n         } n       } n     } n     active[x] = !active[x]; n     printf( %lld n , (1LL * cnt * (cnt - 1) / 2) - bad + (1LL * cnt * ones) + n                          1LL * ones * (ones - 1) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char **argv) { n   int n, k, value = 0; n   vector<pair<int, int>> set; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     set.push_back({x, y}); n   } n   for (int i = 0; i < n; ++i) { n     value += set[i].second - set[i].first + 1; n   } n   if (value % k == 0) { n     cout << 0; n     return 0; n   } n   cout << k - (value % k); n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000100]; n int n; n int d[1000100]; n int dp(int x) { n   if (d[x] != -1) return d[x]; n   int ret = 0; n   int ind = x + 1; n   while (s[ind] ==  M ) { n     ret += 1; n     ind++; n   } n   int w = 0; n   while (s[ind] ==  F  && ind < n) ind++, w++; n   if (ind == n) n     return d[x] = ret; n   else { n     int rety = dp(ind); n     if (w > rety) n       return d[x] = ret; n     else n       return d[x] = dp(ind) + 1 - w + ret; n   } n } n int main() { n   while (scanf( %s , s) != EOF) { n     n = strlen(s); n     memset(d, -1, sizeof(d)); n     int cntm = 0; n     for (int i = 0; i < n; i++) n       if (s[i] ==  M ) cntm++; n     for (int i = n - 1; i >= 0; i--) { n       if (s[i] ==  M ) n         d[i] = 0; n       else n         break; n     } n     int st = -1; n     for (int i = 0; i < n; i++) n       if (s[i] ==  M ) { n         st = i; n         break; n       } n     if (st == -1) { n       puts( 0 ); n       continue; n     } n     dp(st); n     printf( %d n , d[st] + (n - 1 - cntm + 1) - st); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ctos, modd = 1000000007, dos = 2; n long long P[4005], B[4005][4005], res; n long long bi(int n, int k) { n   if (n == k) return 1; n   if (k == 0) return 1; n   if (k > n) return 0; n   if (k == 1) return n; n   if (B[n][k] > 0) return B[n][k]; n   B[n][k] = (bi(n - 1, k - 1) + bi(n - 1, k)) % modd; n   return B[n][k]; n } n int main() { n   cin >> ctos; n   P[0] = 1; n   for (int i = 1; i <= ctos; i++) { n     for (int e = 0; e < i; e++) { n       P[i] += 1LL * P[i - e - 1] * bi(i - 1, e) % modd; n       P[i] %= modd; n     } n   } n   for (int i = 1; i <= ctos; i++) { n     res += P[ctos - i] * bi(ctos, i) % modd; n     res %= modd; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MaxN = 1000; n using namespace std; n char s[MaxN + 5]; n int n; n int vis[200 + 5]; n int main() { n   while (~scanf( %d , &n)) { n     scanf( %s , s + 1); n     int Max = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = i; j <= n; j++) { n         memset(vis, 0, sizeof(vis)); n         int ans = 0; n         bool ok = 1; n         for (int k = i; k <= j; k++) { n           if (s[k] >=  A  && s[k] <=  Z ) n             ok = 0; n           else { n             if (!vis[s[k]]) ans++, vis[s[k]] = 1; n           } n         } n         if (ok) Max = max(Max, ans); n       } n     } n     printf( %d n , Max); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long nmax = (long long)1e6 + 10; n inline long long up(char ch) { return  z  - ch; } n inline long long lo(char ch) { return ch -  a ; } n inline long long eq(char ch) { return 1; } n long long dp[2002][2002]; n long long tmp[2002]; n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   memset(dp, 0, sizeof dp); n   dp[n][0] = 1; n   for (long long i = n - 1; i >= 0; i--) { n     for (long long j = 0; j <= k; j++) { n       if (j - (n - i) >= 0) n         dp[i][j] = (up(s[i]) * dp[i + 1][j - (n - i)]) % 1000000007; n       dp[i][j] += (lo(s[i]) * dp[i + 1][j]) % 1000000007; n       dp[i][j] %= 1000000007; n       long long last = i; n       for (long long t = i + 1; t < n; t++) { n         if (j - (n - t) * (t - i + 1) >= 0) { n           dp[i][j] += n               (up(s[t]) * dp[t + 1][j - (n - t) * (t - i + 1)]) % 1000000007; n           dp[i][j] %= 1000000007; n           last = t; n         } else { n           break; n         } n       } n       for (long long t = n - 1; t > last; t--) { n         if (j - (n - t) * (t - i + 1) >= 0) { n           dp[i][j] += n               (up(s[t]) * dp[t + 1][j - (n - t) * (t - i + 1)]) % 1000000007; n           dp[i][j] %= 1000000007; n         } else { n           break; n         } n       } n       dp[i][j] += tmp[j]; n       dp[i][j] %= 1000000007; n       if (j == 0) { n         dp[i][j]++; n         dp[i][j] %= 1000000007; n       } n       tmp[j] = tmp[j] + (lo(s[i]) * dp[i + 1][j]) % 1000000007; n       tmp[j] %= 1000000007; n     } n   } n   cout << dp[0][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int maxn = 2000000; n int l, lsub; n char str[maxn + 1]; n long long dpw[maxn + 1], dpb[maxn + 1], now, nob, ans0, ansb, ans, ranansb, n     ranans0; n int main() { n   scanf( %d%d , &l, &lsub); n   scanf( %s , str); n   now = -1; n   nob = -1; n   for (long long i = 0; i < l; i++) ans0 = 1; n   for (long long i = 0; i < l; i++) { n     if (str[i] ==  X ) { n       long long temp = 0; n       if (i - now >= lsub) { n         temp = 1; n         if (i >= lsub) temp = dpw[i - lsub]; n       } n       ranans0 = ans0 * 2 - temp; n       while (ranans0 < 0) ranans0 += mod; n       long long temp1 = 0; n       if (i - nob >= lsub) { n         if (i >= lsub) temp1 = dpb[i - lsub]; n       } n       ranansb = ansb * 2 + temp - temp1; n       while (ranansb < 0) ranansb += mod; n       ans *= 2; n       ans %= mod; n       ans += temp1; n       ans %= mod; n       dpw[i] = ans0 % mod; n       dpb[i] = (ansb + temp) % mod; n       ans0 = ranans0 % mod; n       ansb = ranansb % mod; n     } n     if (str[i] ==  B ) { n       long long temp = 0; n       if (i - now >= lsub) { n         temp = 1; n         if (i >= lsub) temp = dpw[i - lsub]; n       } n       nob = i; n       ranans0 = ans0 - temp; n       while (ranans0 < 0) ranans0 += mod; n       ranansb = ansb + temp; n       dpw[i] = 0; n       dpb[i] = (ansb + temp) % mod; n       ans0 = ranans0 % mod; n       ansb = ranansb % mod; n     } n     if (str[i] ==  W ) { n       long long temp1 = 0; n       if (i - nob >= lsub) { n         if (i >= lsub) temp1 = dpb[i - lsub]; n       } n       now = i; n       ranans0 = ans0; n       ranansb = ansb - temp1; n       while (ranansb < 0) ranansb += mod; n       ans += temp1; n       ans %= mod; n       dpw[i] = ans0 % mod; n       dpb[i] = 0; n       ans0 = ranans0 % mod; n       ansb = ranansb % mod; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, max = 0; n   cin >> n; n   int arr[n]; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] > max) { n       max = arr[i]; n     } n   } n   for (int i = 0; i < n; i++) { n     ans += max - arr[i]; n   } n   cout << ans << endl; n   return 0; n } n