#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n long long max(long long a, long long b) { return a > b ? a : b; } n struct SegmentTree { n  public: n   SegmentTree() { n     null_value = -INF; n     null_modifier = 0; n   } n   void insert_data(vector<long long> values) { n     size = 1; n     while (size < values.size()) { n       size *= 2; n     } n     modifier_tree.assign(size * 2 - 1, null_modifier); n     tree.assign(size * 2 - 1, null_value); n     for (int i = 0; i < values.size(); i++) { n       tree[size + i - 1] = values[i]; n     } n     for (int i = size - 2; i >= 0; i--) { n       tree[i] = merge(tree[l_child(i)], tree[r_child(i)]); n     } n   } n   void modify_segment(int begin, int end, long long modifier) { n     modify_segment(begin, end, modifier, 0, 0, size); n   } n   long long get_segment(int begin, int end) { n     return get_segment(begin, end, 0, 0, size); n   } n  n  protected: n   vector<long long> tree; n   vector<long long> modifier_tree; n   int size; n   long long null_value; n   long long null_modifier; n   long long merge(long long segment_1, long long segment_2) { n     if (segment_1 != null_value && segment_2 != null_value) { n       return max(segment_1, segment_2); n     } else if (segment_1 != null_value) { n       return segment_1; n     } else { n       return segment_2; n     } n   } n   long long modify(long long segment, long long modifier) { n     if (modifier != null_modifier) { n       return segment + modifier; n     } else { n       return segment; n     } n   } n   long long combine(long long new_modifier, long long old_modifier) { n     if (new_modifier != null_modifier && old_modifier != null_modifier) { n       return new_modifier + old_modifier; n     } else if (new_modifier != null_modifier) { n       return new_modifier; n     } else { n       return old_modifier; n     } n   } n   void add_modifier(int inode, long long modifier) { n     if (modifier != null_modifier) { n       modifier_tree[inode] = combine(modifier, modifier_tree[inode]); n       tree[inode] = modify(tree[inode], modifier); n     } n   } n   int l_child(int inode) { return inode * 2 + 1; } n   int r_child(int inode) { return inode * 2 + 2; } n   void push(int inode) { n     if (modifier_tree[inode] != null_modifier) { n       add_modifier(l_child(inode), modifier_tree[inode]); n       add_modifier(r_child(inode), modifier_tree[inode]); n       modifier_tree[inode] = null_modifier; n     } n   } n   void modify_segment(int begin, int end, long long modifier, int inode, n                       int lnode, int rnode) { n     if (lnode < rnode) { n       if (lnode >= begin && rnode <= end) { n         add_modifier(inode, modifier); n       } else if (lnode >= end || rnode <= begin) { n         return; n       } else { n         push(inode); n         int mnode = (lnode + rnode) / 2; n         modify_segment(begin, end, modifier, l_child(inode), lnode, mnode); n         modify_segment(begin, end, modifier, r_child(inode), mnode, rnode); n         tree[inode] = merge(tree[l_child(inode)], tree[r_child(inode)]); n       } n     } n   } n   long long get_segment(int begin, int end, int inode, int lnode, int rnode) { n     if (lnode >= begin && rnode <= end) { n       return tree[inode]; n     } else if (lnode >= end || rnode <= begin) { n       return null_value; n     } else { n       push(inode); n       int mnode = (lnode + rnode) / 2; n       long long l_segment = n           get_segment(begin, end, l_child(inode), lnode, mnode); n       long long r_segment = n           get_segment(begin, end, r_child(inode), mnode, rnode); n       return merge(l_segment, r_segment); n     } n   } n }; n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   int n, m, p; n   cin >> n >> m >> p; n   vector<pair<long long, long long>> atk(n), def(m); n   vector<pair<long long, pair<long long, long long>>> mon(p); n   for (int i = 0; i < n; i++) { n     cin >> atk[i].first >> atk[i].second; n   } n   for (int i = 0; i < m; i++) { n     cin >> def[i].first >> def[i].second; n   } n   for (int i = 0; i < p; i++) { n     cin >> mon[i].first >> mon[i].second.first >> mon[i].second.second; n   } n   sort(atk.begin(), atk.end()); n   sort(def.begin(), def.end()); n   sort(mon.begin(), mon.end()); n   vector<long long> b(m), cb(m); n   for (int i = 0; i < m; i++) { n     b[i] = def[i].first; n     cb[i] = -def[i].second; n   } n   SegmentTree tree; n   tree.insert_data(cb); n   int imon = 0; n   long long ans = -INF; n   for (int i = 0; i < n; i++) { n     while (imon < p && mon[imon].first < atk[i].first) { n       int rb = n           upper_bound(b.begin(), b.end(), mon[imon].second.first) - b.begin(); n       tree.modify_segment(rb, m, mon[imon].second.second); n       imon++; n     } n     ans = max(ans, tree.get_segment(0, m) - atk[i].second); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define int               long long n #define ld                long double n #define ll                unsigned int n #define endl                n     n #define mp                make_pair n #define hcf               __gcd n #define test              cout<< hello ; n #define lcm(m,n)          m*(n/__gcd(m,n)) n #define maxa(v)           *(max_element(all(v)) n #define mina(v)           *(min_element(all(v)) n  n #define pb                push_back n #define ppb               pop_back n #define pf                push_front n #define ppf               pop_front n #define ff                first  n #define ss                second n #define doge              cout<< *  n  n #define mod               (int)1e9 n #define all(x)            (x).begin(),(x).end() n #define uniq(v)           (v).erase(unique(all(v)),(v).end()) n #define sz(x)             (int)((x).size()) n #define asort(v)          sort(v.begin(),v.end()) n #define dsort(v)          sort(v.begin(),v.end(),greater<int>()) n #define present(c,e)      (c.find(e) != c.end()) n #define vfind(c,e)     (find(all(c),e) != c.end()) n #define tr(c,it)          for(typeof(c.begin()) it = c.begin(); it != c.end(); it++) n  n  n  n ////////////////////////////////////////////////////////////////////////////////////////////////////////// n  n  n   n     bool isPrime(ll n)  n     {  n         if (n <= 1) return false;  n         if (n <= 3) return true;  n       n         if (n%2 == 0 || n%3 == 0) return false;  n       n         for (ll i=5; i*i<=n; i=i+6)  n             if (n%i == 0 || n%(i+2) == 0)  n             return false;  n       n         return true;  n     }  n       n       n     long long power(int n,int k) n     { n         if(k==0)    return 1; n         else return (n*power(n,k-1)%1000000007); n     } n       n     long long nCr(int n,int r) n     { n         if(n==r)    return 1; n         else if (r==0)  return 1; n         else return ((nCr(n-1,r-1)+nCr(n-1,r))%998244353); n     } n  n     long long sumDigit(ll x) n     { n         ll ans=0; n         while(x) n         { n             ans+=x%10; n             x/=10; n         } n         return ans;vector<int> v; n     } n   n  n     int bs(int a[],int b,int l,int r) n     { n         int mid=(l+r)/2; n         if(l>r) return (int)-1; n         if(a[mid]==b||l==r) return mid; n         if(a[mid]>b)    return bs(a,b,l,mid-1); n         else return bs(a,b,mid+1,r); n     } n  n     int divideandconquerbc(int a[],int l,int r) n     { n         if(l>=r)    return 0; n         int mid=(l+r)/2,ans=0; n         ans+=divideandconquerbc(a,l,mid); n         ans+=divideandconquerbc(a,mid+1,r); n         int x=r-l+1,y=mid-l+1; n         int c[x]; n         int i=0,j=0; n         for(int k=0;k<x;k++) n         { n             if((a[l+i]<=a[mid+1+j]&&i<y)||j>=x-y)   {c[k]=a[l+i];i++;} n             else {c[k]=a[mid+1+j];j++;ans+=y-i;} n         } n         for(int k=l;k<=r;k++) n             a[k]=c[k-l]; n         return ans; n     } n  n     long long  gcd(ll a,ll b) n     { n         if (b==0) return a; n         return gcd(b,a%b); n     } n  n  n  n  n  n //////////////////////////////////////////////////////////////////////////////////////////////////////////////// n  n // count sum count1, cnt,cnt1 sum1 carry tip mx mn a1 a2 a3  n // may the force be with us n  n  n void solve(){ n     int n,q; n     cin>>n>>q; n     set<int>s; n     map<int,int>m; n     for (int i = 0; i < n; ++i) n     { n         int x; n         cin>>x; n         if(!present(s,x)){ n             m[x]=i+1; n             s.insert(x); n         } n     } n     for (int i = 0; i < q; ++i) n     { n         int z; n         cin>>z; n         cout<<m[z]<<   ; n         for(auto i:m){ n             if(i.ss<m[z]){ n                 m[i.ff]++; n             } n         } n         m[z]=1; n     } n  n      n  n  n  n  n  n  n   n cout<<endl;} n signed main(){ n  n     ios_base::sync_with_stdio(false); n     cin.tie(0);cout.tie(0); n     #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n     #endif n  n     int tsc=1; n    // cin>>tsc; n     while(tsc--) solve(); n     return 0; n      n } n


#include <bits/stdc++.h> n using uint = unsigned int; n using cat = long long; n using dbl = long double; n constexpr dbl pi = 3.14159265358979323846; n using namespace std; n template <typename T> n T abs(T x) { n   return (x < 0) ? (-x) : x; n } n cat pw(cat a, cat e, cat mod) { n   if (e <= 0) return 1; n   cat x = pw(a, e / 2, mod); n   x = x * x % mod; n   return (e & 1) ? x * a % mod : x; n } n struct fin { n   vector<int> T; n   fin(int N) { T.resize(N + 10, 0); } n   int lastone(int x) { return x & (x ^ (x - 1)); } n   void put(int pos, int val) { n     for (int i = pos + 1; i < (int)T.size(); i += lastone(i)) T[i] += val; n   } n   int get(int pos) { n     int ret = 0; n     for (int i = pos + 1; i > 0; i -= lastone(i)) ret += T[i]; n     return ret; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   int N; n   cat K; n   cin >> N >> K; n   K = 1LL * N * (N - 1) / 2 - K + 1; n   vector<pair<cat, cat> > P(N); n   for (int i = 0; i < N; i++) cin >> P[i].first >> P[i].second; n   dbl ans_le = 0, ans_gt = 1e5; n   while (ans_gt - ans_le > 1e-8) { n     dbl R = (ans_le + ans_gt) / 2; n     vector<pair<dbl, int> > events; n     for (int i = 0; i < N; i++) { n       dbl d = sqrt(P[i].first * P[i].first + P[i].second * P[i].second); n       if (d < R + 1e-8) continue; n       dbl ang_c = atan2(P[i].second, P[i].first); n       if (ang_c < 0) ang_c = 2 * pi + ang_c; n       dbl ang_d = acos(R / d); n       if (ang_c - ang_d < 1e-8) { n         events.push_back({ang_c + ang_d, 2 * i}); n         events.push_back({ang_c - ang_d, 2 * i + 1}); n         events.push_back({ang_c - ang_d + 2 * pi, 2 * i + 1}); n       } else if (ang_c + ang_d > 2 * pi - 1e-8) { n         events.push_back({ang_c + ang_d - 2 * pi, 2 * i}); n         events.push_back({ang_c - ang_d, 2 * i + 1}); n         events.push_back({ang_c - ang_d - 2 * pi, 2 * i + 1}); n       } else { n         events.push_back({ang_c + ang_d, 2 * i}); n         events.push_back({ang_c - ang_d, 2 * i + 1}); n       } n     } n     sort(begin(events), end(events)); n     vector<int> last_open(N, -1); n     cat cnt = 0; n     fin F(events.size()); n     for (int i = 0; i < (int)events.size(); i++) { n       int id = events[i].second / 2; n       if (events[i].second % 2 == 0) { n         if (last_open[id] != -1) { n           F.put(events.size() - last_open[id], -1); n           cnt += F.get(events.size() - last_open[id]); n           last_open[id] = -1; n         } n       } else { n         last_open[id] = i; n         F.put(events.size() - i, 1); n       } n     } n     if (cnt >= K) n       ans_le = R; n     else n       ans_gt = R; n   } n   cout << ans_le <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5; n struct edg { n   int u, v, l, r; n   edg(int u = 0, int v = 0, int l = 0, int r = 0) : u(u), v(v), l(l), r(r) {} n   bool operator<(const edg &n1) const { return l > n1.l; } n }; n priority_queue<edg> q; n vector<edg> adj[maxn][2]; n int n, m; n int dp[maxn][2]; n inline void add(int u, int v, int l, int r) { n   int op = l % 2; n   if (dp[u][op] >= l) { n     if (v == n) { n       cout << l + 1 << endl; n       exit(0); n     } n     if (r + 1 > dp[v][op ^ 1]) { n       dp[v][op ^ 1] = r + 1; n       for (int i = 0; i < adj[v][op ^ 1].size(); i++) { n         edg t = adj[v][op ^ 1][i]; n         if (l + 1 <= t.r) q.push(edg(v, t.v, l + 1, t.r)); n       } n       adj[v][op ^ 1].clear(); n     } n   } else { n     adj[u][op].push_back(edg(u, v, l, r)); n   } n } n int main() { n   cin >> n >> m; n   memset(dp, 0xf3, sizeof(dp)); n   dp[1][0] = 0; n   for (int i = 1; i <= m; i++) { n     int u, v, l, r; n     scanf( %d%d%d%d , &u, &v, &l, &r); n     if ((r - l + 1) % 2 == 0) { n       q.push(edg(u, v, l, r - 1)); n       q.push(edg(v, u, l, r - 1)); n       if (l + 1 <= r - 2) { n         q.push(edg(u, v, l + 1, r - 2)); n         q.push(edg(v, u, l + 1, r - 2)); n       } n     } else { n       if (l <= r - 2) { n         q.push(edg(u, v, l, r - 2)); n         q.push(edg(v, u, l, r - 2)); n       } n       q.push(edg(u, v, l + 1, r - 1)); n       q.push(edg(v, u, l + 1, r - 1)); n     } n   } n   if (n == 1) { n     cout <<  0  << endl; n     return 0; n   } n   while (!q.empty()) { n     edg t = q.top(); n     q.pop(); n     add(t.u, t.v, t.l, t.r); n   } n   cout <<  -1  << endl; n   return 0; n } n


#include<iostream> n #include <bits/stdc++.h> n using namespace std; n #define ll long long n #define mod 1000000007 n #define INF 100000007 n #define MAX 1000000 n #define nl   n  n #define F first n #define S second n #define pb(a) push_back(a) n typedef vector<ll> vi; n typedef priority_queue<ll , vector<ll> , greater<ll>()> pqmx; n typedef priority_queue<ll> pqmn; n typedef pair<ll , ll> pi; n typedef vector< pair<int , int> > vpi; n #define fr(i,n) for(i=0;i<n;i++) n #define rep(i,a,n) for(i=a;i<n;i++) n int n , t , m , temp , temp2, root , p ,q , k , i , j; n  n struct comp {  n     bool operator()( pair<int, int> const& a, pair<int, int> const& b) {  n         return a.first < b.first || (a.first == b.first && a.second > b.second); n     }  n };  n  n void func(){ n     ll A , B , C; n     cin>>A>>B>>C; n     if((A + B + C)%9 != 0){ n         cout<< NO <<  n ; n         return; n     } n     ll p = (A + B + C) / 9; n     if(p > A || p > B || p > C){ n         cout<< NO <<  n ; n         return; n     } n     cout<< YES <<  n ; n } n  n  n  n int main(){ n #ifndef ONLINE_JUDGE n     freopen( input.txt  ,  r  , stdin); n     freopen( output.txt  ,  w  , stdout); n #endif n     cin>>t; n     while(t--){ n         func(); n     } n }


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n namespace Xrocks {} n using namespace Xrocks; n namespace Xrocks { n class in { n } user_input; n class out { n } output; n in& operator>>(in& X, int& Y) { n   scanf( %d , &Y); n   return X; n } n in& operator>>(in& X, char* Y) { n   scanf( %s , Y); n   return X; n } n in& operator>>(in& X, float& Y) { n   scanf( %f , &Y); n   return X; n } n in& operator>>(in& X, double& Y) { n   scanf( %lf , &Y); n   return X; n } n in& operator>>(in& X, char& C) { n   scanf( %c , &C); n   return X; n } n in& operator>>(in& X, string& Y) { n   cin >> Y; n   return X; n } n in& operator>>(in& X, long long& Y) { n   scanf( %lld , &Y); n   return X; n } n template <typename T> n in& operator>>(in& X, vector<T>& Y) { n   for (auto& x : Y) user_input >> x; n   return X; n } n template <typename T> n out& operator<<(out& X, T Y) { n   cout << Y; n   return X; n } n template <typename T> n out& operator<<(out& X, vector<T>& Y) { n   for (auto& x : Y) output << x <<    ; n   return X; n } n out& operator<<(out& X, const int& Y) { n   printf( %d , Y); n   return X; n } n out& operator<<(out& X, const char& C) { n   printf( %c , C); n   return X; n } n out& operator<<(out& X, const string& Y) { n   printf( %s , Y.c_str()); n   return X; n } n out& operator<<(out& X, const long long& Y) { n   printf( %lld , Y); n   return X; n } n out& operator<<(out& X, const float& Y) { n   printf( %f , Y); n   return X; n } n out& operator<<(out& X, const double& Y) { n   printf( %lf , Y); n   return X; n } n out& operator<<(out& X, const char Y[]) { n   printf( %s , Y); n   return X; n } n template <typename T> n T max(T A) { n   return A; n } n template <typename T, typename... args> n T max(T A, T B, args... S) { n   return max(A > B ? A : B, S...); n } n template <typename T> n T min(T A) { n   return A; n } n template <typename T, typename... args> n T min(T A, T B, args... S) { n   return min(A < B ? A : B, S...); n } n template <typename T> n void vectorize(int y, vector<T>& A) { n   A.resize(y); n } n template <typename T, typename... args> n void vectorize(int y, vector<T>& A, args&&... S) { n   A.resize(y); n   vectorize(y, S...); n } n long long fast(long long a, long long b, long long pr) { n   if (b == 0) return 1 % pr; n   long long ans = 1 % pr; n   while (b) { n     if (b & 1) ans = (ans * a) % pr; n     b >>= 1; n     a = (a * a) % pr; n   } n   return ans; n } n int readInt() { n   int n = 0; n   scanf( %d , &n); n   return n; n   int ch = getchar_unlocked(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar_unlocked(); n   } n   while (ch >=  0  && ch <=  9 ) n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar_unlocked(); n   n = n * sign; n   return n; n } n long long readLong() { n   long long n = 0; n   int ch = getchar_unlocked(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar_unlocked(); n   } n   while (ch >=  0  && ch <=  9 ) n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar_unlocked(); n   n = n * sign; n   return n; n } n long long inv_(long long val, n                long long pr = static_cast<long long>(163577857)) { n   return fast(val, pr - 2, pr); n } n }  // namespace Xrocks n int op1(int a, int b) { return a | b; } n int op2(int a, int b) { return a & b; } n int op3(int a, int b) { return a ^ b; } n class solve { n  public: n   solve() { n     vector<int (*)(int, int)> g = {op1, op2, op3}; n     vector<int> v{0, 1, 2}; n     vector<int> w(4); n     user_input >> w; n     int x = 0; n     do { n       vector<int (*)(int, int)> f = g; n       for (int i = 0; i < 3; i++) f[i] = g[v[i]]; n       if (x == 5) n         output << f[0](f[1](f[0](w[0], w[1]), f[2](w[2], w[3])), n                        f[2](f[1](w[1], w[2]), f[0](w[3], w[0]))) n                <<   n ; n       ++x; n     } while (next_permutation(v.begin(), v.end())); n   } n }; n int32_t main() { n   int t = 1, i = 1; n   if (0) scanf( %d , &t); n   while (t--) { n     if (0) printf( Case #%d:  , i++); n     new solve; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n template <class T, class R> n ostream& operator<<(ostream& os, pair<T, R>& v) { n   os <<  (  << v.first <<     << v.second <<  )  <<    ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto i : v) os << i <<    ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, set<T>& v) { n   for (auto i : v) os << i <<    ; n   return os; n } n unordered_map<string, string*> some; n struct Node { n   Node* parent; n   vector<Node*> children; n   int wordsCount; n   string* word; n   Node(Node* parent = nullptr) : parent(parent), wordsCount(0), word(nullptr) { n     children.resize(26, nullptr); n   } n   Node* getChild(char c) { n     if (children[c -  a ] == nullptr) { n       children[c -  a ] = new Node(this); n     } n     return children[c -  a ]; n   } n }; n struct Bor { n   unordered_set<string> words; n   Node* root; n   Bor() : root(new Node()) {} n   int addWord(string& s) { n     bool was = (words.find(s) != words.end()); n     Node* now = root; n     int ans = 0; n     int step = 1; n     int stepForStep = 1e9; n     int len = 0; n     bool gg = false; n     for (char& x : s) { n       len++; n       now = now->getChild(x); n       stepForStep--; n       if (stepForStep == 0) step = 1; n       ans += step; n       if (!gg && now->wordsCount == 1) { n         if (was && len != s.size()) { n           ans++; n           step = 0; n           stepForStep = 1e9; n         } else if (now->word != nullptr && now->word->size() != len && n                    *now->word == s.substr(0, now->word->size())) { n           ans++; n           step = 0; n           stepForStep = now->word->size() - len + 1; n         } n         gg = true; n       } n     } n     if (step != 0) { n       if (now->word == nullptr) now->word = new string(s); n       auto kek = now->word; n       while (now != root) { n         now->wordsCount += 1; n         if (now->wordsCount == 1) now->word = kek; n         now = now->parent; n       } n       if (now->wordsCount == 1) now->word = kek; n       now->wordsCount += 1; n     } n     words.insert(s); n     return ans; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   int ans = 0; n   int count = 0; n   Bor bor; n   int gl = 0; n   int vsego = 0; n   while (getline(cin, s)) { n     vsego += s.size() + 1; n     ans += 1; n     string now; n     for (char x : s) { n       if (x >=  a  && x <=  z  || x >=  A  && x <=  Z ) { n         now += x; n       } else { n         ans++; n         if (now !=   ) { n           int cnt = bor.addWord(now); n           ans += cnt; n           gl += cnt; n           now =   ; n         } n       } n     } n     if (now !=   ) { n       int cnt = bor.addWord(now); n       ans += cnt; n       gl += cnt; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using ul = std::uint32_t; n using li = std::int32_t; n using ull = std::uint64_t; n using ll = std::int64_t; n using vul = std::vector<ul>; n using vvul = std::vector<vul>; n using vb = std::vector<bool>; n using vvb = std::vector<vb>; n using vvvb = std::vector<vvb>; n using vull = std::vector<ull>; n using vvull = std::vector<vull>; n using vli = std::vector<li>; n using vll = std::vector<ll>; n using pulul = std::pair<ul, ul>; n using vpulul = std::vector<pulul>; n ul n; n ll s; n class point { n  public: n   li x = 0; n   li y = 0; n   point() = default; n   point(li a, li b) : x(a), y(b) {} n }; n point operator-(const point& a, const point& b) { n   return point(a.x - b.x, a.y - b.y); n } n point operator+(const point& a, const point& b) { n   return point(a.x + b.x, a.y + b.y); n } n ul region(const point& a) { n   if (a.x < 0) { n     if (a.y < 0) { n       return 3; n     } else { n       return 2; n     } n   } else { n     if (a.y < 0) { n       return 4; n     } else { n       return 1; n     } n   } n } n class lessbydir { n  public: n   bool operator()(const point& a, const point& b) { n     if (region(a) != region(b)) { n       return region(a) < region(b); n     } else { n       return ll(a.x) * b.y > ll(b.x) * a.y; n     } n   } n } less; n std::vector<point> points(2000); n vul order(2000); n vul pos(2000); n std::vector<std::pair<ul, ul>> vecs; n ll det(const point& a, const point& b) { return ll(a.x) * b.y - ll(b.x) * a.y; } n ll inner(const point& a, const point& b) { n   return ll(a.x) * b.x + ll(a.y) * b.y; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cin >> n >> s; n   s <<= 1; n   points.resize(n); n   order.resize(n); n   for (ul i = 0; i != n; ++i) { n     std::cin >> points[i].x >> points[i].y; n     order[i] = i; n   } n   for (ul i = 0; i != n; ++i) { n     for (ul j = 0; j != n; ++j) { n       if (i == j) { n         continue; n       } n       vecs.push_back(std::pair<ul, ul>(i, j)); n     } n   } n   std::sort(vecs.begin(), vecs.end(), [&](const pulul& a, const pulul& b) { n     return less(points[a.second] - points[a.first], n                 points[b.second] - points[b.first]); n   }); n   point dir0 = points[vecs[0].second] - points[vecs[0].first]; n   std::sort(order.begin(), order.end(), [&](ul a, ul b) { n     return det(dir0, points[a]) < det(dir0, points[b]); n   }); n   for (ul i = 0; i != n; ++i) { n     pos[order[i]] = i; n   } n   for (auto vec : vecs) { n     ul i = vec.first; n     ul j = vec.second; n     ul l = 0, r = n; n     point dir = points[j] - points[i]; n     while (l + 1 != r) { n       ul mid = l + r >> 1; n       if (det(dir, points[order[mid]] - points[i]) > s) { n         r = mid; n       } else { n         l = mid; n       } n     } n     if (det(points[j] - points[i], points[order[l]] - points[i]) == s) { n       std::cout <<  Yes  << std::endl; n       std::cout << points[i].x <<     << points[i].y << std::endl; n       std::cout << points[j].x <<     << points[j].y << std::endl; n       std::cout << points[order[l]].x <<     << points[order[l]].y << std::endl; n       return 0; n     } n     if (pos[j] > pos[i]) { n       std::swap(order[pos[j]], order[pos[i]]); n       std::swap(pos[j], pos[i]); n     } n   } n   std::cout <<  No  << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1000; n int n, m; n int cnt[MaxN + 5][MaxN + 5]; n bool Check(int x, int y) { n   if (x < 0) return false; n   if (y < 0) return false; n   if (x > n) return false; n   if (y > n) return false; n   return true; n } n int main() { n   while (~scanf( %d%d , &n, &m)) { n     memset(cnt, 0, sizeof(cnt)); n     for (int i = 1; i <= m; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       for (int nx = x - 2; nx <= x; nx++) n         for (int ny = y - 2; ny <= y; ny++) n           if (Check(nx, ny)) { n             cnt[nx][ny]++; n             if (cnt[nx][ny] == 9) { n               printf( %d n , i); n               return 0; n             } n           } n     } n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int www = 9; n const int Size = 10000; n char buffer[Size]; n int dr[] = {-1, 0, 1, 0}; n int dc[] = {0, 1, 0, -1}; n const int inf = 0x0fffffff; n const int size = 1000; n int d[size]; n int solution(int nTest) { n   int k, a, b; n   scanf( %d%d%d , &k, &a, &b); n   scanf( %s , buffer); n   string s = buffer; n   int n = s.size(); n   if (n / k >= a && (n + k - 1) / k <= b) { n     vector<string> res; n     int t = n / k; n     int tt = (n + k - 1) / k; n     int r = k - (n % k); n     for (int i = 0; i < k - (n % k); i++) { n       res.push_back(s.substr(i * t, t)); n     } n     for (int i = 0; i < n % k; i++) { n       res.push_back(s.substr(t * r + i * tt, tt)); n     } n     for (int i = 0; i < res.size(); i++) { n       puts(res[i].c_str()); n     } n     return 0; n   } n   puts( No solution ); n   return 0; n } n int main() { n   int i = 0, n = 9999; n   while (i < n && solution(i)) { n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int radix_len = 1e7 + 5; n long long cnt[radix_len]; n int main() { n   int n, k, a; n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   long long tot = 0; n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) { n     cin >> a; n     cnt[a]++; n     tot += a; n   } n   if (tot < k) { n     cout << -1 << endl; n     return 0; n   } n   auto bound = 1; n   auto sum = 0LL; n   for (int i = radix_len - 1; i > 0; i--) { n     sum += cnt[i]; n     if (sum >= k) { n       bound = i; n       break; n     } n   } n   for (int i = radix_len - 1; i > 1; i--) { n     if (i / 2 < bound) break; n     cnt[i / 2] += cnt[i]; n     cnt[i - i / 2] += cnt[i]; n     sum += cnt[i]; n     cnt[i] = 0; n     while (sum - cnt[bound] >= k) { n       sum -= cnt[bound]; n       bound++; n     } n   } n   cout << bound << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   if (k < n / 2) { n     cout << -1; n     return 0; n   } n   if (n == 1) { n     if (k) n       cout << -1; n     else n       cout << 1; n     return 0; n   } n   int x = k - (n - 2) / 2; n   cout << x <<     << 2 * x <<    ; n   for (int i = 2, j = 2 * x + 1; i < n; i++, j++) cout << j <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v, i, j, c[505], ca = 0, cb = 0, cc = 0, cnt, nt; n int main() { n   cin >> n >> m; n   vector<int> g[505]; n   for (i = 0; i < 505; i++) c[i] = 0; n   for (i = 0; i < m; i++) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n     if (g[u].size() == n - 1) { n       c[u] = 2; n       cb++; n     } n     if (g[v].size() == n - 1) { n       c[v] = 2; n       cb++; n     } n   } n   for (i = 1; i <= n; i++) { n     if (c[i] == 0) break; n   } n   if (i != n + 1) { n     c[i] = 1; n     ca++; n     for (j = 0; j < g[i].size(); j++) { n       v = g[i][j]; n       if (c[v] == 0) { n         c[v] = 1; n         ca++; n       } n     } n   } n   while (i <= n && c[i] != 0) i++; n   if (i != n + 1) { n     c[i] = 3; n     cc++; n     for (j = 0; j < g[i].size(); j++) { n       v = g[i][j]; n       if (c[v] == 2) continue; n       if (c[v] == 1) { n         cout <<  NO  << endl; n         return 0; n       } else { n         c[v] = 3; n         cc++; n       } n     } n   } n   while (i <= n) { n     if (c[i] == 0) { n       cout <<  NO  << endl; n       return 0; n     } n     i++; n   } n   for (i = 1; i <= n; i++) { n     u = i; n     if (c[i] == 1) { n       cnt = ca - 1 + cb; n       nt = 3; n     } else if (c[i] == 2) n       cnt = n - 1; n     else { n       cnt = cc - 1 + cb; n       nt = 1; n     } n     if (g[u].size() == n - 1) continue; n     if (g[u].size() != cnt) { n       cout <<  NO  << endl; n       return 0; n     } n     for (j = 0; j < g[u].size(); j++) { n       if (c[g[u][j]] == nt) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   for (i = 1; i <= n; i++) { n     if (c[i] == 1) n       cout <<  a ; n     else if (c[i] == 2) n       cout <<  b ; n     else n       cout <<  c ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> Map; n string s[181], sn; n int n, i; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> sn; n   cin >> n; n   s[1] =  January ; n   s[2] =  February ; n   s[3] =  March ; n   s[4] =  April ; n   s[5] =  May ; n   s[6] =  June ; n   s[7] =  July ; n   s[8] =  August ; n   s[9] =  September ; n   s[10] =  October ; n   s[11] =  November ; n   s[12] =  December ; n   for (i = 13; i <= 24; i++) s[i] = s[i - 12]; n   for (i = 1; i <= 12; i++) Map[s[i]] = i; n   cout << s[Map[sn] + n % 12] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, h; n struct Node { n   int m, v; n   int id; n   bool operator<(const Node &x) const { n     if (m != x.m) return m < x.m; n     return v < x.v; n   } n } q[100005]; n bool ok(double times) { n   int i, j; n   for (j = 1, i = 0; i < n; i++) { n     if (q[i].v * times - (double)j * h >= -1e-9) j++; n     if (j > k) return true; n   } n   return false; n } n int main() { n   int i, j; n   cin >> n >> k >> h; n   for (i = 0; i < n; i++) cin >> q[i].m; n   for (i = 0; i < n; i++) { n     cin >> q[i].v; n     q[i].id = i + 1; n   } n   sort(q, q + n); n   double l, r, mid; n   l = 0, r = 1e20; n   for (int i = 0; i < 200; i++) { n     mid = (l + r) * 0.5; n     if (!ok(mid)) n       l = mid; n     else n       r = mid; n   } n   j = 1; n   for (i = 0; i < n; i++) { n     if (q[i].v * r - (double)j * h >= -1e-9) { n       cout << q[i].id <<    ; n       j++; n     } n     if (j > k) break; n   } n } n


#include <bits/stdc++.h> n int n, a[1000][1000]; n int main() { n   for (int i = 1; i <= 100; i++) n     for (int j = 1; j <= 100; j++) a[i][j] = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int t, v; n     scanf( %d , &t); n     for (int j = 1; j <= t; j++) { n       scanf( %d , &v); n       a[i][v] = 1; n     } n   } n   bool f1; n   for (int i = 1; i <= n; i++) { n     f1 = true; n     bool f; n     for (int j = 1; j <= n; j++) n       if (i != j) { n         f = false; n         for (int k = 1; k <= 100; k++) n           if (a[j][k] == 1 && a[i][k] == 0) { n             f = true; n             break; n           } n         if (f == false) { n           f1 = false; n           break; n         } n       } n     if (f1 == true) n       printf( YES ); n     else n       printf( NO ); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N[10]; n string shirt[5] = { S ,  M ,  L ,  XL ,  XXL }; n map<string, int> mp; n int main() { n   int K, i, d; n   for ((i) = 0; (i) < (int)(5); (i)++) cin >> N[i]; n   cin >> K; n   for ((i) = 0; (i) < (int)(5); (i)++) mp[shirt[i]] = i; n   for ((i) = 0; (i) < (int)(K); (i)++) { n     string s; n     cin >> s; n     int x = mp[s]; n     int ans = -1; n     for (d = 0;; d++) { n       if (x + d >= 0 && x + d < 5 && N[x + d] > 0) { n         ans = x + d; n         break; n       } n       if (x - d >= 0 && x - d < 5 && N[x - d] > 0) { n         ans = x - d; n         break; n       } n     } n     N[ans]--; n     cout << shirt[ans] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T Abs(T a) { n   return a > 0 ? a : -a; n } n template <typename T> n inline T sqr(T a) { n   return a * a; n } n template <typename T> n inline void relaxMin(T &a, T b) { n   if (b < a) a = b; n } n template <typename T> n inline void relaxMax(T &a, T b) { n   if (b > a) a = b; n } n const int INF = (int)1E9; n const long double EPS = 1E-6; n const long double PI = 3.1415926535897932384626433832795; n int dp[2000][2000]; n int mod = 1000000007; n void solve() { n   int n; n   cin >> n; n   int k; n   int x = 0, y = 0; n   for (int i(0), _bb(n); i < _bb; ++i) { n     cin >> k; n     if (k == 1) n       x++; n     else n       y++; n   } n   dp[0][0] = 1; n   dp[1][0] = dp[1][1] = 1; n   for (int i(2), _bb(1001); i < _bb; ++i) { n     dp[i][0] = (dp[i - 1][0] + (i - 1) * 1LL * dp[i - 2][0]) % mod; n     for (int j(1), _bb(1001); j < _bb; ++j) n       dp[i][j] = (dp[i - 1][j - 1] * 1LL * i) % mod; n   } n   cout << dp[n][y] << endl; n } n int main() { n   srand(0xA1B2C3D4); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n void update(int *c, int x, int v) { n   while (x <= m) { n     c[x] += v; n     x += x & -x; n   } n } n long long sum(int *c, int x) { n   long long ret = 0; n   while (x > 0) { n     ret += c[x]; n     x -= x & -x; n   } n   return ret; n } n long long calc(int *c, int L, int R) { return sum(c, R) - sum(c, L - 1); } n int c[3060 + 3060][3060], L[3060][3060], R[3060][3060], Ld[3060][3060]; n char s[3060][3060]; n vector<pair<int, int> > g[3060]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (s[i][j] ==  . ) n         L[i][j] = 0; n       else n         L[i][j] = L[i][j - 1] + 1; n     } n   for (int i = n; i > 0; --i) n     for (int j = m; j > 0; --j) { n       if (s[i][j] ==  . ) n         R[i][j] = 0; n       else n         R[i][j] = R[i][j + 1] + 1; n       if (s[i][j] ==  . ) n         Ld[i][j] = 0; n       else n         Ld[i][j] = Ld[i + 1][j - 1] + 1; n     } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) g[j + R[i][j] - 1].push_back(make_pair(i, j)); n   long long ans = 0; n   for (int j = m; j > 0; --j) { n     for (int i = 0; i < g[j].size(); ++i) { n       int x = g[j][i].first; n       int y = g[j][i].second; n       update(c[x + y], y, 1); n     } n     for (int i = 1; i <= n; ++i) { n       int mi = min(L[i][j], Ld[i][j]); n       if (!mi) continue; n       ans += calc(c[i + j], j - mi + 1, j); n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n template <int mod> n struct ModInt { n   int x; n   ModInt() : x(0) {} n   ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} n   ModInt &operator+=(const ModInt &p) { n     if ((x += p.x) >= mod) x -= mod; n     return *this; n   } n   ModInt &operator-=(const ModInt &p) { n     if ((x += mod - p.x) >= mod) x -= mod; n     return *this; n   } n   ModInt &operator*=(const ModInt &p) { n     x = (int)(1LL * x * p.x % mod); n     return *this; n   } n   ModInt &operator/=(const ModInt &p) { n     *this *= p.inverse(); n     return *this; n   } n   ModInt operator-() const { return ModInt(-x); } n   ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } n   ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } n   ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } n   ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } n   bool operator==(const ModInt &p) const { return x == p.x; } n   bool operator!=(const ModInt &p) const { return x != p.x; } n   ModInt inverse() const { n     int a = x, b = mod, u = 1, v = 0, t; n     while (b > 0) { n       t = a / b; n       swap(a -= t * b, b); n       swap(u -= t * v, v); n     } n     return ModInt(u); n   } n   ModInt pow(int64_t n) const { n     ModInt ret(1), mul(x); n     while (n > 0) { n       if (n & 1) ret *= mul; n       mul *= mul; n       n >>= 1; n     } n     return ret; n   } n   friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } n   friend istream &operator>>(istream &is, ModInt &a) { n     int64_t t; n     is >> t; n     a = ModInt<mod>(t); n     return (is); n   } n   static int get_mod() { return mod; } n }; n using mint = ModInt<MOD>; n long long mod_pow(long long x, long long n) { n   if (n == 0) return 1; n   long long res = mod_pow(x * x % MOD, n / 2); n   if (n & 1) res = res * x % MOD; n   return res; n } n long long power(long long num, long long pw) { n   if (pw == 0) return 1; n   long long res = power(num * num, pw / 2); n   if (pw & 1) res *= pw; n   return res; n } n long long ln(long long num, long long base) { n   int steps = 0; n   while (num > 1) { n     num /= base; n     steps++; n   } n   return steps; n } n int S = 100001; n vector<int> Seive(S); n void seive() { n   fill(Seive.begin(), Seive.end(), 0); n   Seive[0] = 0; n   Seive[1] = 0; n   for (long long i = 2; i <= S; i++) { n     if (Seive[i]) continue; n     for (long long j = i * i; j <= S; j += i) Seive[j] = 1; n   } n } n long long gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(int a, int b) { return (a / gcd(a, b)) * b; } n bool sortbysec(const pair<long long, long long> &a, n                const pair<long long, long long> &b) { n   if (a.second == b.second) return a.first < b.first; n   return (a.second < b.second); n } n bool compare(const pair<string, int> &a, const pair<string, int> &b) { n   for (int i = 0; i < a.first.length(); i++) { n     if (i % 2 == 0) { n       if (a.first[i] < b.first[i]) return true; n       if (a.first[i] > b.first[i]) return false; n     } else { n       if (a.first[i] > b.first[i]) return true; n       if (a.first[i] < b.first[i]) return false; n     } n   } n } n int binarySearch(vector<pair<int, int>> a) { n   int r = a.size(); n   int l = 1; n   int ans = r; n   while (l <= r) { n     int mid = (l + r) / 2; n     int rem = mid, curr = 0; n     for (int i = 0; i < a.size(); i++) { n       if (a[i].first >= rem - 1) { n         if (a[i].second >= curr) { n           curr++; n           rem--; n         } n       } n     } n     if (rem <= 0) { n       ans = mid; n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   return ans; n } n void toggle(bool &x) { x ^= 1; } n void solve() { n   int q; n   cin >> q; n   map<int, list<int>> m; n   int i = 0; n   while (q--) { n     int num; n     cin >> num; n     if (num == 1) { n       int x; n       cin >> x; n       m[x].push_back(i); n       i++; n     } else { n       int x, y; n       cin >> x >> y; n       if (x == y) continue; n       if (m[x].size() > m[y].size()) m[x].swap(m[y]); n       for (auto &i : m[x]) { n         m[y].push_back(i); n       } n       m.erase(x); n     } n   } n   vector<pair<int, int>> v; n   for (auto x : m) { n     for (auto y : x.second) { n       v.push_back({x.first, y}); n     } n   } n   sort(v.begin(), v.end(), sortbysec); n   for (auto x : v) { n     cout << x.first <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   for (int T = 1; T <= t; T++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long pow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) { n       res = (res * a) % 1000000007; n     } n     b >>= 1; n     a = (a * a) % 1000000007; n   } n   return res; n } n int main() { n   long long n, k; n   cin >> n >> k; n   long long ans = (pow(k, k - 1) * pow(n - k, n - k)) % 1000000007; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n + 1]; n   int b[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     b[a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     cout << b[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Yahia74() { n   ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n } n const int N = 3e5 + 74, OO = 0x3f3f3f3f, MOD = (int)1e9 + 7; n int main() { n   Yahia74(); n   long long n, d; n   cin >> n >> d; n   long long ans = n, c = 9, t = 10; n   while (c <= n) { n     long long st = 0, ed = 1e18 / t; n     while (st < ed) { n       long long m = st + (ed - st + 1) / 2; n       if (m * t + c <= n) n         st = m; n       else n         ed = m - 1; n     } n     if (st * t + c <= n && st * t + c >= n - d) ans = st * t + c; n     t *= 10; n     c = c * 10 + 9; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long n, p; n   scanf( %I64d , &n); n   if (n % 2 == 0 && n != 0) n     p = n + 1; n   else n     p = (n + 1) / 2; n   printf( %I64d , p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isopen(char ch) { n   if (ch ==  <  || ch ==  {  || ch ==  (  || ch ==  [ ) return true; n   return false; n } n int main() { n   int k, sum = 0, ans = 0; n   string s; n   cin >> k >> s; n   int c[10] = {0}; n   for (long long int i = 0; i < s.size(); i++) { n     sum = sum + s[i] -  0 ; n     int d = s[i] -  0 ; n     c[d]++; n   } n   for (int i = 0; i < 9; i++) n     for (int j = 0; j < c[i]; j++) { n       if (k > sum) { n         sum = sum + 9 - i; n         ans++; n       } n     } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void scan(int &x) { n   bool neg = false; n   register int c; n   x = 0; n   c = getchar(); n   if (c ==  - ) { n     neg = true; n     c = getchar(); n   } n   for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; n   if (neg) x *= -1; n } n int main() { n   string s; n   cin >> s; n   int k; n   cin >> k; n   int n = s.size(); n   if (k > n) n     cout <<  impossible ; n   else { n     bool ch[26] = {false}; n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       if (!ch[s[i] -  a ]) { n         cnt++; n         ch[s[i] -  a ] = true; n       } n     } n     if (k - cnt >= 0) n       cout << k - cnt; n     else n       cout <<  0 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dy[] = {-1, 1, 0, 0}; n int dx[] = {0, 0, -1, 1}; n int n, m; n char mp[1005][1005]; n int k; n pair<int, int> p[30]; n int row[1005][1005]; n int col[1005][1005]; n pair<int, int> d[100005]; n int return_dir(char dir) { n   if (dir ==  N ) n     return 0; n   else if (dir ==  S ) n     return 1; n   else if (dir ==  W ) n     return 2; n   else n     return 3; n } n int main() { n   for (int i = 0; i < 30; i++) p[i].first = -1; n   char ch; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       ch = getchar(); n       while (ch == 32 || ch ==   n ) ch = getchar(); n       mp[i][j] = ch; n       if (mp[i][j] >=  A  && mp[i][j] <=  Z ) n         p[mp[i][j] -  A ] = make_pair(i, j); n     } n   } n   int c = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (mp[i][j] ==  # ) n         c++; n       else n         row[i][j] = c; n     } n   } n   c = 0; n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < n; i++) { n       if (mp[i][j] ==  # ) n         c++; n       else n         col[i][j] = c; n     } n   } n   scanf( %d , &k); n   for (int i = 0; i < k; i++) { n     string s; n     cin >> s; n     int a; n     cin >> a; n     d[i].first = return_dir(s[0]); n     d[i].second = a; n   } n   bool was = false; n   for (int i = 0; i < 30; i++) { n     if (p[i].first == -1) continue; n     bool ok = true; n     int si = p[i].first; n     int sj = p[i].second; n     for (int j = 0; j < k; j++) { n       int ni = si + dy[d[j].first] * d[j].second; n       int nj = sj + dx[d[j].first] * d[j].second; n       if (ni >= n || nj >= m || ni < 0 || nj < 0) { n         ok = false; n         break; n       } n       if (d[j].first < 2) { n         if (col[ni][nj] != col[si][sj] || mp[ni][nj] ==  # ) ok = false; n       } else { n         if (row[ni][nj] != row[si][sj] || mp[ni][nj] ==  # ) ok = false; n       } n       si = ni; n       sj = nj; n     } n     if (ok) { n       was = true; n       putchar( A  + i); n     } n   } n   if (!was) cout <<  no solution ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, b; n   cin >> n >> b; n   long long a[n + 1]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   a[n] = b; n   sort(a, a + n + 1); n   if (a[0] == b) { n     cout << a[n - 1] - a[0]; n   } else { n     if (a[n] == b) { n       cout << a[n] - a[1]; n     } else { n       long long s1, s2, s3, s4, ans; n       s1 = (b - a[0]) + (a[n - 1] - a[0]); n       ans = s1; n       s2 = (a[n] - b) + (a[n] - a[1]); n       ans = min(ans, s2); n       s3 = (b - a[1]) + (a[n] - a[1]); n       ans = min(ans, s3); n       s4 = (a[n - 1] - b) + (a[n - 1] - a[0]); n       ans = min(ans, s4); n       cout << ans; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n long long int n, a[100005], dis[100005] = {0}, vis[100050] = {0}, cnt = 0; n vector<pair<long long int, long long int> > v[100005]; n void dfs(long long int s, long long int f) { n   vis[s] = 1; n   long long int i; n   for (i = 0; i < v[s].size(); i++) { n     long long int x = v[s][i].first, y = v[s][i].second; n     if (!vis[x]) { n       if (f == 0) { n         dis[x] = max(y + dis[s], y); n         if (dis[x] > a[x]) { n           cnt++; n           dfs(x, 1); n         } else n           dfs(x, 0); n       } else { n         cnt++; n         dfs(x, f); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   memset(dis, 0, sizeof(dis[0])); n   memset(vis, 0, sizeof(vis[0])); n   long long int i, j, x, y; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 2; i <= n; i++) { n     cin >> x >> y; n     v[i].push_back({x, y}); n     v[x].push_back({i, y}); n   } n   for (i = 1; i <= n; i++) { n     if (!vis[i]) dfs(i, 0); n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n int p[N], u[N]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= k; i++) { n     int x; n     cin >> x; n     if (p[x] == 0) p[x] = i; n     u[x] = i; n   } n   long long rez = 0; n   for (int i = 1; i <= n; i++) { n     if (p[i] == 0) { n       if (i == 1 || i == n) n         rez += 2; n       else n         rez += 3; n     } else { n       if (i < n && (p[i] > u[i + 1])) rez++; n       if (i > 1 && (p[i] > u[i - 1])) rez++; n     } n   } n   cout << rez; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n const long long INF = 1LL << 60; n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n struct UnionFind { n   vector<long long> par, w; n   UnionFind(long long n) : par(n, -1), w(n, 0) {} n   void init(long long n) { n     par.assign(n, -1); n     w.assign(n, 0); n   } n   long long root(long long x) { n     if (par[x] < 0) n       return x; n     else n       return par[x] = root(par[x]); n   } n   bool issame(long long x, long long y) { return root(x) == root(y); } n   bool merge(long long x, long long y) { n     x = root(x); n     y = root(y); n     if (x == y) { n       ++w[x]; n       return false; n     } n     if (par[x] > par[y]) swap(x, y); n     par[x] += par[y]; n     par[y] = x; n     w[x] += w[y]; n     ++w[x]; n     return true; n   } n   long long size(long long x) { return -par[root(x)]; } n   long long wei(long long x) { return w[root(x)]; } n }; n void solve() { n   long long n, m, q; n   cin >> n >> m >> q; n   UnionFind uf(n + m); n   long long cnt = n + m; n   for (long long i = 0; i < q; ++i) { n     long long r, c; n     cin >> r >> c; n     --r, --c; n     if (!uf.issame(r, c + n)) { n       uf.merge(r, c + n); n       --cnt; n     } n   } n   cout << cnt - 1 <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct union_find { n   vector<int> uf; n   vector<int> num_segments; n   vector<bool> used; n   union_find(int n) { n     uf.assign(n, -1); n     num_segments.assign(n, 0); n     used.assign(n, false); n     for (int i = 0; i < n; ++i) { n       if (i % 2 == 0) num_segments[i] = 1; n     } n   } n   int find(int i) { n     if (uf[i] < 0) n       return i; n     else n       return uf[i] = find(uf[i]); n   } n   bool merge(int a, int b) { n     a = find(a); n     b = find(b); n     if (a == b) return false; n     if (uf[a] <= uf[b]) { n       uf[b] = a; n       if (uf[a] == uf[b]) { n         uf[a]--; n       } n       num_segments[a] = n           min(num_segments[a] + num_segments[b], 2 * (int)uf.size()); n       used[a] = used[a] || used[b]; n     } else if (uf[b] < uf[a]) { n       uf[a] = b; n       num_segments[b] = n           min(num_segments[a] + num_segments[b], 2 * (int)uf.size()); n       used[b] = used[a] || used[b]; n     } n     return true; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int N, K; n   cin >> N >> K; n   string s; n   cin >> s; n   vector<vector<int>> groups(K); n   vector<vector<int>> groups_for_bit(N); n   for (int i = 0; i < K; ++i) { n     int k; n     cin >> k; n     groups[i].resize(k); n     for (int j = 0; j < k; ++j) { n       cin >> groups[i][j]; n       groups[i][j]--; n       groups_for_bit[groups[i][j]].push_back(i); n     } n   } n   auto inverse = [](int x) { return x ^ 1; }; n   union_find uf(2 * K); n   int num_segments = 0; n   auto unset = [&](int i) { n     if (uf.used[i]) { n       uf.used[i] = false; n       num_segments -= uf.num_segments[i]; n       ; n     } n   }; n   auto set = [&](int i) { n     assert(!uf.used[i]); n     uf.used[i] = true; n     num_segments += uf.num_segments[i]; n     ; n   }; n   for (int i = 0; i < N; ++i) { n     ; n     int g = -1; n     if (groups_for_bit[i].size() == 0) { n       ; n     } else if (groups_for_bit[i].size() == 1) { n       g = uf.find(2 * groups_for_bit[i][0]); n       int ig = uf.find(inverse(g)); n       ; n       unset(g); n       unset(ig); n       if (s[i] ==  0 ) { n         uf.num_segments[ig] = N * 3; n       } else { n         uf.num_segments[g] = N * 3; n       } n     } else { n       g = uf.find(2 * groups_for_bit[i][0]); n       int h = uf.find(2 * groups_for_bit[i][1]); n       int ig = uf.find(inverse(g)); n       int ih = uf.find(inverse(h)); n       ; n       ; n       unset(g); n       unset(ig); n       unset(h); n       unset(ih); n       if (s[i] ==  0 ) { n         uf.merge(ig, h); n         uf.merge(g, ih); n       } else { n         uf.merge(g, h); n         uf.merge(ig, ih); n       } n     } n     if (g >= 0) { n       g = uf.find(g); n       int ig = uf.find(inverse(g)); n       set(uf.num_segments[g] <= uf.num_segments[ig] ? g : ig); n     } n     cout << num_segments <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const pair<int, int> Dir[8] = {{0, 1}, {0, -1}, {1, 0},   {-1, 0}, n                                {1, 1}, {1, -1}, {-1, -1}, {-1, 1}}; n template <typename A, size_t N, typename T> n void Fill(A (&array)[N], const T &val) { n   std::fill((T *)array, (T *)(array + N), val); n } n signed main() { n   long long n; n   scanf( %lld , &n); n   long long dp[2] = {1, 2}; n   long long turn = 1; n   while (1) { n     long long next = dp[0] + dp[1]; n     if (next > n) { n       break; n     } n     dp[0] = dp[1]; n     dp[1] = next; n     turn++; n   } n   printf( %lld n , turn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, ad, a[444444], u[444444]; n char str[11]; n int getnum(char str[]) { n   int ret = 0, neg = -1, i = 1; n   if (str[0] !=  - ) { n     neg = 1; n     i = 0; n   } n   for (; str[i]; i++) { n     ret = ret * 10 + str[i] -  0 ; n   } n   ret *= neg; n   return ret; n } n int main() { n   int i, j, fg = 0, nd, pd, ll, lt, rt, ds, mid, ld; n   scanf( %d%d , &n, &k); n   ad = k; n   for (i = 1; i <= n; i++) { n     scanf( %s , str); n     if (str[0] ==  ? ) n       a[i + ad] = 1; n     else n       u[i + ad] = getnum(str); n   } n   for (i = 1; i <= ad; i++) { n     u[i] = -1010000000 + i; n     u[ad + n + i] = 1010000000 + i; n   } n   for (i = 1; i <= k; i++) { n     if (fg) break; n     nd = pd = -1; n     ll = u[i]; n     for (j = i + k; j <= n + 2 * ad; j += k) { n       if (a[j]) continue; n       if (u[j] <= ll) { n         fg = 1; n         break; n       } n       ll = u[j]; n     } n     if (fg) break; n     for (j = i; j <= n + 2 * ad; j += k) { n       if (pd == -1 && !a[j] && u[j] >= 0) { n         pd = j; n         break; n       } n     } n     for (j = pd; j >= 1; j -= k) { n       if (nd == -1 && !a[j] && u[j] < 0) { n         nd = j; n         break; n       } n     } n     for (j = pd + k; j <= n + 2 * ad; j += k) { n       ld = j - k; n       if (!a[j]) { n         if (u[ld] >= u[j]) fg = 1; n         continue; n       } n       if (fg) break; n       a[j] = 0; n       u[j] = u[ld] + 1; n     } n     if (fg) break; n     for (j = nd - k; j >= 1; j -= k) { n       ld = j + k; n       if (!a[j]) { n         if (u[j] >= u[ld]) fg = 1; n         continue; n       } n       if (fg) break; n       a[j] = 0; n       u[j] = u[ld] - 1; n     } n     mid = (pd - nd) / k - 1; n     if (mid > u[pd] - u[nd]) { n       fg = 1; n       break; n     } n     lt = -((mid - 1) / 2); n     rt = mid / 2; n     if (u[pd] <= rt) { n       ds = rt - u[pd] + 1; n       rt -= ds; n       lt -= ds; n     } n     if (u[nd] >= lt) { n       ds = u[nd] - lt + 1; n       rt += ds; n       lt += ds; n     } n     for (j = nd + k; j < pd; j += k) { n       u[j] = lt; n       lt++; n     } n   } n   if (fg) n     puts( Incorrect sequence ); n   else { n     for (i = k + 1; i <= k + n; i++) printf( %d  , u[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int kMod = 10007; n char buf[222]; n int m, n, l; n int dp[222][222][222]; n int f[222]; n int s; n struct Matrix { n   int v[444][444]; n   void clear() { memset(v, 0, sizeof(v)); } n } I, A, F; n Matrix operator*(const Matrix &a, const Matrix &b) { n   Matrix c; n   c.clear(); n   for (int i = 0; i < s; ++i) n     for (int k = 0; k <= i; ++k) n       if (a.v[i][k]) n         for (int j = 0; j <= k; ++j) n           if (b.v[k][j]) c.v[i][j] = (c.v[i][j] + a.v[i][k] * b.v[k][j]) % kMod; n   return c; n } n Matrix powMod(const Matrix &a, int b) { n   Matrix res = I, tmp = a; n   while (b) { n     if (b & 1) res = res * tmp; n     b >>= 1; n     tmp = tmp * tmp; n   } n   return res; n } n int main() { n   scanf( %s , buf + 1); n   m = strlen(buf + 1); n   scanf( %d , &n); n   l = m + n; n   if (buf[1] != buf[m]) { n     dp[0][0][1] = 1; n   } else { n     dp[0][0][0] = 1; n   } n   for (int l = 0; l < m; ++l) { n     for (int r = 0; r < m - l; ++r) { n       for (int k = 0; k < 222; ++k) n         if (dp[l][r][k]) { n           if (buf[l + 1] != buf[m - r]) { n             int i, j; n             i = l + 1, j = r; n             if (buf[i + 1] != buf[m - j]) { n               dp[i][j][k + 1] += dp[l][r][k]; n               dp[i][j][k + 1] %= kMod; n             } else { n               dp[i][j][k] += dp[l][r][k]; n               dp[i][j][k] %= kMod; n             } n             i = l, j = r + 1; n             if (buf[i + 1] != buf[m - j]) { n               dp[i][j][k + 1] += dp[l][r][k]; n               dp[i][j][k + 1] %= kMod; n             } else { n               dp[i][j][k] += dp[l][r][k]; n               dp[i][j][k] %= kMod; n             } n           } else { n             int i, j; n             i = l + 1, j = r + 1; n             if (i + j >= m) { n               f[k] += dp[l][r][k]; n               f[k] %= kMod; n             } else { n               if (buf[i + 1] != buf[m - j]) { n                 dp[i][j][k + 1] += dp[l][r][k]; n                 dp[i][j][k + 1] %= kMod; n               } else { n                 dp[i][j][k] += dp[l][r][k]; n                 dp[i][j][k] %= kMod; n               } n             } n           } n         } n     } n   } n   s = (m - 1) + ((m - 1) / 2 + 1) * 2; n   for (int i = 0; i < s; ++i) I.v[i][i] = 1; n   for (int i = 0; i < m - 1; ++i) { n     A.v[i + 1][i]++; n     A.v[i][i] += 24; n   } n   for (int i = m - 1; i < (m - 1) + ((m - 1) / 2 + 1); ++i) { n     if (i + 1 < (m - 1) + ((m - 1) / 2) + 1) A.v[i + 1][i]++; n     A.v[i][i] += 25; n     A.v[(m - 1) + ((m - 1) / 2 + 1) + (i - (m - 1))][i]++; n   } n   for (int i = (m - 1) + ((m - 1) / 2 + 1); i < s; ++i) { n     A.v[i][i] += 26; n   } n   for (int i = 0; i < m; ++i) { n     F.v[i][i] = 1; n   } n   F = powMod(A, (l - 1) / 2 + 1) * F; n   int ans = 0; n   for (int k = 0; k < 222; ++k) n     if (f[k]) { n       int n24 = k, n25 = (m - k - 1) / 2 + 1; n       int from = (m - 1) - k; n       int to = (m - 1) + ((m - 1) / 2 + 1) + n25 - 1; n       ans = (ans + F.v[to][from] * f[k]) % kMod; n     } n   if (l & 1) { n     F.clear(); n     for (int i = 0; i < m; ++i) { n       F.v[i][i] = 1; n     } n     F = powMod(A, (l - 1) / 2) * F; n     for (int k = 0; k < 222; ++k) n       if (f[k]) { n         int n24 = k, n25 = (m - k - 1) / 2 + 1; n         if ((m - n24) % 2 == 0) { n           int from = (m - 1) - k; n           int to = (m - 1) + n25 - 1; n           ans -= F.v[to][from] * f[k] % kMod; n           if (ans < 0) ans += kMod; n         } n       } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int c, t, n, a[1000005], b[1000005], used[100005]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       used[i] = 0; n     } n     for (int i = 1; i <= n; i++) cin >> b[i]; n     if (n % 2 == 1 && a[n / 2 + 1] != b[n / 2 + 1]) { n       cout <<  NO  << endl; n       continue; n     } n     c = 1; n     long long int l, r; n     if (n % 2 == 1) n       l = n / 2, r = n / 2 + 2; n     else n       l = n / 2, r = n / 2 + 1; n     for (int j = l; j >= 1; j--) { n       long long int ok = 1; n       for (int i = 1; i <= n / 2; i++) { n         if (used[i] == 1) continue; n         if ((a[i] == b[l] && a[n - i + 1] == b[r]) || n             (a[i] == b[r] && a[n - i + 1] == b[l])) { n           used[i] = 1; n           ok = 0; n           break; n         } n       } n       if (ok == 1) { n         c = 0; n         break; n       } n       l--; n       r++; n     } n     if (c == 1) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n #define mp make_pair n #define fi first n #define se second n #define pb push_back n #define eb emplace_back n #define all(x) (x).begin(), (x).end() n #define rall(x) (x).rbegin(), (x).rend() n #define forn(i, n) for (int i = 0; i < (int)(n); ++i) n #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) n #define ms(x, y) memset(x, y, sizeof(x)) n #define SZ(x) int(x.size()) n #define fk cout<< fk <<endl n #define db(x) cout<<(#x)<< = <<(x)<<endl n #define db2(x,y) cout<<(#x)<< = <<(x)<<   <<(#y)<< = <<(y)<<endl n using namespace std; n typedef pair<int, int> pii; n typedef vector<int> vi; n typedef long long i64; n template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } n template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } n void fastio(){ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout<<fixed;} n //1.integer overflow (1e5 * 1e5) (2e9 + 2e9) n //2.runtime error n //3.boundary condition n int tc,n,k,cnt[30],need[30]; n string s; n int main() { n     fastio(); n  n     cin>>tc; n     while(tc--){ n         cin>>n>>k; n         cin>>s; n         ms(cnt,0); n         for(int i=0; i<n; ++i){ n             int num=int(s[i])-int( a ); n             ++cnt[num]; n         } n         bool ok=true; n         for(int i=0; i<26; ++i){ n             if(cnt[i]%k!=0) ok=false; n         } n         if(ok){ n             cout<<s<<  n ; n             continue; n         } n         bool have=false; n         string ans=  ; n         for(int i=n-1; i>=0; --i){ n             string res=  ; n             int cur=int(s[i])-int( a ); n             --cnt[cur]; n             if(cur==25) continue; n             int all_need=0; n             ms(need,0); n             for(int j=0; j<26; ++j){ n                 int goal=(cnt[j]+k-1)/k*k; n                 need[j]=goal-cnt[j]; n                 all_need+=goal-cnt[j]; n             } n             if(all_need<=n-i){ n                 if(all_need==n-i){ n                     int find=-1; n                     for(int j=cur+1; j<26; ++j){ n                         if(need[j]>0){ n                             find=j; n                             break; n                         } n                     } n                     if(find!=-1){ n                         have=true; n                         for(int j=0; j<i; ++j) res+=s[j]; n                         char ch=char(int( a )+find); n                         res+=ch; n                         --need[find]; n                         for(int j=0; j<26; ++j){ n                             while(need[j]>0){ n                                 char ch_temp=char(int( a )+j); n                                 res+=ch_temp; n                                 need[j]--; n                             } n                         } n                         ans=res; n                         break; n                     } n                 } else{ n                     if(((n-i)-all_need)%k==0){ n                         int find=-1; n                         for(int j=cur+1; j<26; ++j){ n                             if(need[j]>0){ n                                 find=j; n                                 break; n                             } n                         } n                         if(find==cur+1){ n                             need[0]+=(n-i)-all_need; n                             have=true; n                             for(int j=0; j<i; ++j) res+=s[j]; n                             res+=char(int( a )+cur+1); n                             --need[cur+1]; n                             for(int j=0; j<26; ++j){ n                                 while(need[j]>0){ n                                     res+=char(int( a )+j); n                                     --need[j]; n                                 } n                             } n                             ans=res; n                             break; n                         } else{ n                             need[cur+1]+=k; n                             need[0]+=(n-i)-all_need-k; n                             have=true; n                             for(int j=0; j<i; ++j) res+=s[j]; n                             res+=char(int( a )+cur+1); n                             --need[cur+1]; n                             for(int j=0; j<26; ++j){ n                                 while(need[j]>0){ n                                     res+=char(int( a )+j); n                                     --need[j]; n                                 } n                             } n                             ans=res; n                             break; n                         } n                     } n                 } n             } n         } n      //   if(have) assert(SZ(ans)==n); n         if(have) cout<<ans<<  n ; n         else cout<<-1<<  n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   if (n > m) swap(n, m); n   int ans; n   if (n == 1) n     ans = m; n   else if (n == 2) { n     if (m % 4 <= 2) n       ans = m + m % 4; n     else n       ans = m + 1; n   } else n     ans = (n * m + 1) / 2; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return ((a * b) / (gcd(a, b))); } n bool compare(const pair<long long, long long> &a, n              const pair<long long, long long> &b) { n   return a.second < b.second; n } n long long ceil(long long a, long long b) { return (a + b - 1) / b; } n long long binpowmod(long long a, long long b, long long m) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) { n       res = ((res % m) * (a % m)) % m; n     } n     a = ((a % m) * (a % m)) % m; n     b >>= 1; n   } n   res %= m; n   return res; n } n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) { n       res = ((res) * (a)); n     } n     a = ((a) * (a)); n     b >>= 1; n   } n   return res; n } n void swap(long long *a, long long *b) { n   long long temp = *a; n   *a = *b; n   *b = temp; n } n long long sum_digits(long long x) { n   long long s = 0; n   while (x) { n     s += (x % 10); n     x /= 10; n   } n   return s; n } n bool is_perfect_square(long long n) { n   return (floor(double(sqrt(n))) == ceil(double(sqrt(n)))); n } n vector<int> pr; n void prime() { n   const int N = 10000000; n   int lp[N + 1] = {0}; n   for (int i = 2; i <= N; ++i) { n     if (lp[i] == 0) { n       lp[i] = i; n       pr.push_back(i); n     } n     for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j) n       lp[i * pr[j]] = pr[j]; n   } n } n long long bs(vector<long long> &arr, long long l, long long r, long long x) { n   long long mid = 0; n   if (r >= l) { n     mid = l + (r - l) / 2; n     if (arr[mid] == x) return mid; n     if (arr[mid] > x) return bs(arr, l, mid - 1, x); n     return bs(arr, mid + 1, r, x); n   } n   return -1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   std::vector<long long> v; n   int c1 = 0, c2 = 0; n   for (int i = (0); i < (n); i++) { n     int x; n     cin >> x; n     if (x == 1) n       c1++; n     else n       c2++; n   } n   if (c1 == 0) n     v = std::vector<long long>(n, 2); n   else if (c2 == 0) n     v = std::vector<long long>(n, 1); n   else { n     v.push_back(2); n     c2--; n     v.push_back(1); n     c1--; n     while ((c2)--) v.push_back(2); n     while ((c1)--) v.push_back(1); n   } n   for (int i = (0); i < (n); i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200100]; n int sub[200100]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     sort(a + 1, a + n + 1); n     for (int i = 1; i <= n; i++) { n       sub[i] = sub[i - 1] + a[i - 1]; n       a[i] = a[i] - sub[i]; n     } n     sort(a + 1, a + 1 + n); n     cout << a[n] << endl; n     memset(sub, 0, sizeof(sub)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007ll; n const long long int MOD = 1000000009ll; n int local_debug = 0; n long long int bit_count(long long int _x) { n   long long int _ret = 0; n   while (_x) { n     if (_x % 2 == 1) _ret++; n     _x /= 2; n   } n   return _ret; n } n long long int bit(long long int _mask, long long int _i) { n   long long int x = 1; n   return (_mask & (x << _i)) == 0 ? 0 : 1; n } n long long int powermod(long long int _a, long long int _b, n                        long long int _m = mod) { n   long long int _r = 1; n   while (_b) { n     if (_b % 2 == 1) _r = (_r * _a) % _m; n     _b /= 2; n     _a = (_a * _a) % _m; n   } n   return _r; n } n long long int power(long long int _a, long long int _b) { n   long long int _r = 1; n   while (_b) { n     if (_b % 2 == 1) _r = (_r * _a); n     _b /= 2; n     _a = (_a * _a); n   } n   return _r; n } n long long int add(long long int a, long long int b, long long int m = mod) { n   a %= m; n   b %= m; n   long long int x = a + b; n   while (x >= m) x -= m; n   return x; n } n long long int sub(long long int a, long long int b, long long int m = mod) { n   long long int x = a - b; n   while (x < 0) x += m; n   return x; n } n long long int mul(long long int a, long long int b, long long int m = mod) { n   a %= m; n   b %= m; n   long long int x = a * b; n   x %= m; n   return x; n } n long long int gcd(long long int a, long long int b) { n   if (a < b) { n     a = a ^ b; n     b = a ^ b; n     a = a ^ b; n   } n   if (b == 0) return a; n   long long int t; n   while (b) { n     t = b; n     b = a % b; n     a = t; n   } n   return a; n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int maxSubArraySum(vector<long long int> a, long long int size) { n   long long int max_so_far = a[0]; n   long long int curr_max = a[0]; n   for (long long int i = 1; i < size; i++) { n     curr_max = max(a[i], curr_max + a[i]); n     max_so_far = max(max_so_far, curr_max); n   } n   return max_so_far; n } n int main() { n   long long int n; n   scanf( %lld , &n); n   ; n   vector<long long> hrr(n); n   vector<long long> grr(n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &hrr[i]); n     ; n     scanf( %lld , &grr[i]); n     ; n   } n   int count = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= n; j++) { n       if (hrr[i] == grr[j]) { n         count++; n       } n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int atk[N], atk2[N], def2[N]; n bool mark[N]; n int main() { n   int n, len; n   while (scanf( %d%d , &n, &len) != EOF) { n     int lena = 0, lend = 0, i, j; n     for (i = 0; i < n; i++) { n       char s[5]; n       int v; n       scanf(  %s%d , s, &v); n       if (s[0] ==  A ) n         atk2[lena++] = v; n       else n         def2[lend++] = v; n     } n     for (int i = 0; i < len; i++) scanf( %d , &atk[i]); n     sort(atk, atk + len); n     sort(atk2, atk2 + lena); n     sort(def2, def2 + lend); n     memset(mark, false, sizeof(mark)); n     int ans1 = 0, ans2 = 0; n     for (i = 0, j = 0; i < lend && j < len; i++) { n       while (j < len && (mark[j] || atk[j] <= def2[i])) j++; n       if (j == len) break; n       mark[j] = true; n     } n     if (j < len) { n       for (i = 0, j = 0; i < lena && j < len; i++) { n         while (j < len && (mark[j] || atk[j] < atk2[i])) j++; n         if (j == len) break; n         mark[j] = true; n         ans1 += atk[j] - atk2[i]; n       } n       if (j < len) { n         for (i = 0; i < len; i++) n           if (!mark[i]) ans1 += atk[i]; n       } else n         ans1 = 0; n     } n     for (i = len - 1, j = 0; i >= 0 && j < lena && atk[i] >= atk2[j]; n          i--, j++) { n       ans2 += atk[i] - atk2[j]; n     } n     printf( %d n , max(ans1, ans2)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a[10000], first = -1, val, t = 1; n   cin >> n; n   cin >> a[0]; n   for (int i = 1; i < n; i++) { n     cin >> a[i]; n     if (a[i - 1] > a[i] || a[i - 1] == a[i]) t = 0; n   } n   if (t == 1) { n     cout <<  YES ; n     return 0; n   } n   for (int i = 0; i < n - 1; i++) { n     if (a[i] > a[i + 1] || a[i] == a[i + 1]) { n       first = i; n       val = a[i]; n       break; n     } n   } n   if (first == -1) { n     first == n - 1; n     val = a[n - 1]; n   } n   for (int i = 0; a[i] != val; i++) { n     if (a[i] == a[i + 1]) { n       cout <<  NO ; n       return 0; n     } n   } n   for (int i = first; i < n - 1; i++) { n     if (a[i] != a[i + 1]) { n       first = i; n       break; n     } n     if (a[i + 1] == val) { n       first = i + 1; n     } n   } n   for (int i = first; i < n - 1; i++) { n     if (a[i] < a[i + 1] || a[i] == a[i + 1]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, a, k; n char s[2000007], res[2000007]; n bool vis[2000007]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   long long int maxn = -1; n   for (long long int i = 0; i < n; ++i) { n     cin >> s >> k; n     long long int len = strlen(s); n     long long int cur = INT_MIN; n     for (long long int j = 0; j < k; ++j) { n       cin >> a; n       a--; n       maxn = max(maxn, a + len); n       for (long long int jj = max(cur, a); jj <= a + len - 1; ++jj) { n         if (!vis[jj]) { n           vis[jj] = 1; n           res[jj] = s[jj - a]; n         } n       } n       cur = max(cur, a + len - 1); n     } n   } n   for (long long int i = 0; i < maxn; ++i) n     if (!res[i]) n       cout <<  a ; n     else n       cout << res[i]; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimaze( Ofast ); n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long maxk = 1e8 + 113; n const long long maxb = 1e18 + 113; n const long long inf = 2e18; n struct line { n   long long k, b; n   line() { n     this->k = 0; n     this->b = 0; n   } n   line(long long k, long long b) { n     this->k = k; n     this->b = b; n   } n   long long func(long long x) { return k * x + b; } n }; n struct LiChao { n   vector<line> ms; n   vector<int> le, re; n   long long range; n   LiChao() { n     this->range = maxk - 113; n     ms.push_back(line()); n     le.push_back(-1); n     re.push_back(-1); n   } n   LiChao(long long range) { n     this->range = range; n     ms.push_back(line()); n     le.push_back(-1); n     re.push_back(-1); n   } n   void clear() { n     ms.clear(); n     le.clear(); n     re.clear(); n     ms.push_back(line()); n     le.push_back(-1); n     re.push_back(-1); n   } n   void push(int v, bool e) { n     if (!e && le[v] == -1) { n       le[v] = ms.size(); n       ms.push_back(ms[v]); n       le.push_back(-1); n       re.push_back(-1); n     } n     if (e && re[v] == -1) { n       re[v] = ms.size(); n       ms.push_back(ms[v]); n       le.push_back(-1); n       re.push_back(-1); n     } n   } n   void add(int v, long long l, long long r, line in) { n     long long m = (l + r) >> 1; n     if (l > r) return; n     if (in.func(m) > ms[v].func(m)) swap(in, ms[v]); n     if (l == r) return; n     if (in.func(l) > ms[v].func(l)) { n       push(v, 0); n       add(le[v], l, m, in); n     } else { n       push(v, 1); n       add(re[v], m + 1, r, in); n     } n   } n   void add(line in) { add(0, -range, range, in); } n   long long get(int v, long long l, long long r, long long x) { n     if (l >= r) return ms[v].func(x); n     long long m = (l + r) >> 1; n     if (x <= m) { n       if (le[v] == -1) return ms[v].func(x); n       return max(get(le[v], l, m, x), ms[v].func(x)); n     } else { n       if (re[v] == -1) return ms[v].func(x); n       return max(get(re[v], m + 1, r, x), ms[v].func(x)); n     } n   } n   long long get(long long x) { return get(0, -range, range, x); } n }; n signed main() { n   srand(1); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.setf(ios::fixed); n   cout.precision(15); n   long long n, s; n   cin >> n >> s; n   long long df = 0; n   vector<pair<long long, long long>> ed; n   for (int i = 0; i < n; i++) { n     long long v, c; n     cin >> v >> c; n     df = max(df, v); n     ed.push_back({c, -v}); n   } n   ed.push_back({s, 1}); n   sort(ed.begin(), ed.end()); n   long long range = 1e9 + 113 + s / df; n   LiChao ms(range); n   long long le = -1; n   for (int i = 0; i < ed.size(); i++) { n     ed[i].second *= -1; n     long long r = max(le, 1LL); n     while (ms.get(r) < ed[i].first) r <<= 1; n     r = min(r, range); n     while (le + 1 < r) { n       long long m = (le + r) / 2; n       if (ms.get(m) < ed[i].first) n         le = m; n       else n         r = m; n     } n     if (ed[i].second != -1) { n       long long k = ed[i].second; n       ms.add(line(k, -r * k + ms.get(r) - ed[i].first)); n       le = r; n     } else { n       cout << r; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void read() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n string a1, a2, b1, b2; n void check() { n   if (a1[0] ==  X ) n     swap(a1[0], a1[1]); n   else if (a1[1] ==  X ) n     swap(a1[1], a2[1]); n   else if (a2[1] ==  X ) n     swap(a2[1], a2[0]); n   else if (a2[0] ==  X ) n     swap(a2[0], a1[0]); n } n void solve() { n   cin >> a1 >> a2 >> b1 >> b2; n   for (int i = 0; i < 65; ++i) { n     check(); n     if (a1 == b1 && a2 == b2) { n       cout <<  YES ; n       return; n     } n   } n   cout <<  NO ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   while (T--) { n     solve(); n     cout <<   n ; n   } n   cerr <<   nTime elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[1000000]; n   cin >> s; n   int l = strlen(s); n   int a[26][2]; n   char x = 0; n   int diff = 0; n   for (int i = 0; i < 26; i++) { n     a[i][0] = 0; n     a[i][1] = 0; n   } n   for (int i = 0; s[i] !=   0 ; i++) { n     x = (int)s[i]; n     x = x - 97; n     diff = i - a[x][1]; n     if (a[x][0] == 0) { n       diff++; n     } n     if (diff > a[x][0]) { n       a[x][0] = diff; n     } n     a[x][1] = i; n   } n   for (int i = 0; i < 26; i++) { n     diff = l - a[i][1]; n     if (diff > a[i][0]) { n       a[i][0] = diff; n     } n   } n   int min = 1000000; n   for (int i = 0; i < 26; i++) { n     if ((a[i][0] < min) && (a[i][0] != 0)) { n       min = a[i][0]; n     } n   } n   cout << min << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void debug(T a, T b) { n   ; n } n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n namespace std { n template <class S, class T> n ostream& operator<<(ostream& out, const pair<S, T>& a) { n   out <<  (  << a.first <<  ,  << a.second <<  ) ; n   return out; n } n }  // namespace std n const int INF = 7e8; n int n; n int ar[300005]; n int sum[300005]; n vector<int> Tpos, Opos; n int solve1(int a, int b, int A, int B) { n   if (B == INF) return INF; n   if (b == INF) return INF; n   int Tlen = B - b; n   int tmp = sum[b] * 1000; n   int pt = sum[b] * 100; n   int Trange = Tlen * 2000; n   tmp += max(0, Trange - pt); n   pt -= min(Trange, pt); n   tmp += 1000; n   pt += 100; n   int rest = (sum[n] - sum[B + 1]) * 1000; n   tmp += max(0, rest - pt); n   return tmp; n } n int solve2(int a, int b, int A, int B) { n   if (a == -1) return INF; n   if (A == -1) return INF; n   int Tlen = a - A; n   int tmp = sum[A] * 1000; n   int pt = sum[A] * 100; n   tmp += max(0, 1000 - pt); n   pt -= min(pt, 1000); n   int Trange = Tlen * 2000; n   tmp += Trange; n   pt += Tlen * 200; n   int rest = (sum[n] - sum[a + 1]) * 1000; n   tmp += max(0, rest - pt); n   return tmp; n } n int main() { n   cin >> n; n   Opos.push_back(-1); n   Tpos.push_back(-1); n   for (int i = 0; i < (n); ++i) { n     int a; n     scanf( %d , &a); n     ar[i] = a / 1000; n     if (ar[i] == 1) n       Opos.push_back(i); n     else n       Tpos.push_back(i); n   } n   Tpos.push_back(INF); n   Opos.push_back(INF); n   for (int i = 0; i < (n); ++i) sum[i + 1] = sum[i] + ar[i]; n   int res = INF; n   for (int i = 0; i < (n + 1); ++i) { n     int tmp = sum[i] * 1000; n     int pt = sum[i] * 100; n     int rest = (sum[n] - sum[i]) * 1000; n     tmp += max(0, rest - pt); n     chmin(res, tmp); n   } n   int prev = res; n   for (int i = 0; i < ((int)Tpos.size() - 1); ++i) { n     int a = Tpos[i], b = Tpos[i + 1]; n     int B = *lower_bound((Opos).begin(), (Opos).end(), b); n     int A = *(--lower_bound((Opos).begin(), (Opos).end(), a)); n     int X = solve1(a, b, A, B); n     int Y = solve2(a, b, A, B); n     chmin(res, X); n     chmin(res, Y); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 100, inf = 0x3f3f3f3f; n const int mod97 = 1e9 + 7, mod33 = 998244353; n const int drc[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n struct edge { n   int to, cap, dis, nt; n } e[MAX]; n int tot, head[555]; n int MinCost, MaxFlow; n int n, m, K, Q, H; n int ans[205][205]; n int a[105][105]; n int b[105]; n void add(int x, int y, int cap, int dis) { n   e[tot].to = y; n   e[tot].cap = cap; n   e[tot].dis = dis; n   e[tot].nt = head[x]; n   head[x] = tot; n   tot++; n } n bool Bellman_Ford(int S, int T) { n   int pre[T + 1], flow[T + 1], dis[T + 1]; n   bool in[T + 1]; n   memset(dis, 0x3f, sizeof(dis)); n   memset(in, 0, sizeof(in)); n   dis[S] = 0; n   flow[S] = inf; n   pre[S] = -1; n   queue<int> q; n   q.push(S); n   in[S] = 1; n   while (q.size()) { n     int tmp = q.front(); n     q.pop(); n     in[tmp] = 0; n     for (int i = head[tmp]; ~i; i = e[i].nt) { n       int j = e[i].to; n       if (e[i].cap > 0 && dis[j] > dis[tmp] + e[i].dis) { n         dis[j] = dis[tmp] + e[i].dis; n         flow[j] = min(flow[tmp], e[i].cap); n         pre[j] = i ^ 1; n         if (!in[j]) { n           q.push(j); n           in[j] = 1; n         } n       } n     } n   } n   if (dis[T] == inf) return 0; n   MinCost += dis[T] * flow[T]; n   MaxFlow += flow[T]; n   for (int i = T; i > 0; i = e[pre[i]].to) { n     if (i > 0 && i < (n << 1 | 1)) { n       ans[e[pre[i]].to][i] += flow[T]; n     } n     e[pre[i]].cap += flow[T]; n     e[pre[i] ^ 1].cap -= flow[T]; n   } n   return 1; n } n void Solve() { n   MaxFlow = MinCost = tot = 0; n   while (Bellman_Ford(0, n << 1 | 1)) n     ; n   int sum = 0; n   for (int i = 1; i <= n; ++i) sum += b[i]; n   if (sum != MaxFlow) { n     puts( NO ); n     return; n   } n   puts( YES ); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       printf( %d  , ans[i][j + n] - ans[j + n][i]); n     } n     puts(  ); n   } n   return; n } n int Main() { n   int t = 1; n   while (t--) { n     while (~scanf( %d%d , &n, &m)) { n       memset(head, -1, sizeof(head)); n       int sum1 = 0; n       for (int i = 1; i <= n; ++i) { n         int x; n         scanf( %d , &x); n         sum1 += x; n         add(0, i, x, 0); n         add(i, 0, 0, 0); n         add(i, i + n, inf, 0); n         add(i + n, i, 0, 0); n       } n       for (int i = 1; i <= n; ++i) { n         int x; n         scanf( %d , &x); n         sum1 -= x; n         b[i] = x; n         add(i + n, n << 1 | 1, x, 0); n         add(n << 1 | 1, i + n, 0, 0); n       } n       for (int i = 1; i <= m; ++i) { n         int x, y; n         scanf( %d%d , &x, &y); n         add(x, y + n, inf, 0); n         add(y + n, x, 0, 0); n         add(y, x + n, inf, 0); n         add(x + n, y, 0, 0); n       } n       if (sum1) { n         puts( NO ); n         continue; n       } n       Solve(); n     } n   } n   return 0; n } n int main() { return Main(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int k = 0, l; n   int n = s.length(); n   if (n <= 20) { n     cout <<  1  n          <<     << n << endl; n     cout << s; n   } else { n     int m; n     if (n % 20 == 0) n       m = n / 20; n     else n       m = (n / 20) + 1; n     if (n % m == 0) { n       cout << m <<     << (n / m) << endl; n       for (int i = 0; i < m; i++) { n         for (int j = k; j < (n / m) + k; j++) { n           cout << s[j]; n         } n         cout <<   n ; n         k = k + (n / m); n       } n     } else { n       cout << m <<     << (n / m) + 1 << endl; n       l = n % m; n       for (int i = 0; i < l; i++) { n         for (int j = k; j <= (n / m) + k; j++) { n           cout << s[j]; n         } n         cout <<    << endl; n         k = k + (n / m) + 1; n       } n       for (int y = 0; y < (m - l); y++) { n         for (int z = k; z < k + (n / m); z++) { n           cout << s[z]; n         } n         k = k + n / m; n         cout <<  *  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastscan(int& number) { n   bool negative = false; n   register int c; n   number = 0; n   c = getchar(); n   while (c == 10) c = getchar(); n   if (c ==  - ) { n     negative = true; n     c = getchar(); n   } n   for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48; n   if (negative) number *= -1; n } n const int N = 150005; n struct line { n   long long k, b; n   line(long long K = 0, long long B = -1000000007ll * 1000000007ll) { n     k = K; n     b = B; n   } n }; n long long f(line p, long long x) { return p.k * x + p.b; } n line t[4 * N]; n void cl(int k) { n   k++; n   k *= 4; n   for (int i = 1; i <= k; i++) t[i] = line(); n } n void add(int v, int l, int r, line p) { n   int m = (l + r) / 2; n   bool M = f(p, m) > f(t[v], m); n   bool L = f(p, l) > f(t[v], l); n   if (M) swap(p, t[v]); n   if (r - l == 1) return; n   if (L != M) n     add(v * 2, l, m, p); n   else n     add(v * 2 + 1, m, r, p); n } n long long get(int v, int l, int r, int pos) { n   if (r - l == 1) return f(t[v], pos); n   int m = (l + r) / 2; n   if (pos <= m) n     return max(f(t[v], pos), get(v * 2, l, m, pos)); n   else n     return max(f(t[v], pos), get(v * 2 + 1, m, r, pos)); n } n int n, used[N], color[N], cnt[N], col; n long long a[N], ans; n vector<int> g[N]; n void dfsCnt(int v, int p) { n   cnt[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (used[to] || to == p) continue; n     dfsCnt(to, v); n     cnt[v] += cnt[to]; n   } n } n int centr(int u) { n   dfsCnt(u, 0); n   col++; n   queue<int> q; n   q.push(u); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     color[v] = col; n     int can = 1; n     if (!can) continue; n     for (int i = 0; i < g[v].size(); i++) { n       int to = g[v][i]; n       if (used[to] || color[to] == col) continue; n       if (!(cnt[u] >= 2 * cnt[to])) { n         q.push(to); n         can = 0; n       } n     } n     if (can) { n       used[v] = 1; n       return v; n     } n   } n   assert(0); n } n vector<line> upd; n long long pr[N], sum1[N], sum2[N], st; n int k; n void dfs(int v, int p, int d) { n   pr[v] = pr[p] + a[v]; n   sum1[v] = sum1[p] + d * a[v]; n   sum2[v] = sum2[p] + pr[v]; n   cnt[v] = 1; n   ans = max(ans, get(1, 0, k, d - 1) + sum2[v] - st * d); n   ans = max(ans, sum1[v]); n   upd.push_back(line(pr[v], sum1[v])); n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (used[to] || to == p) continue; n     dfs(to, v, d + 1); n     cnt[v] += cnt[to]; n   } n } n void solve(int v, int K) { n   v = centr(v); n   k = max(K, 1); n   pr[v] = sum1[v] = sum2[v] = st = a[v]; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (used[to]) continue; n     dfs(to, v, 2); n     while (!upd.empty()) { n       add(1, 0, k, upd.back()); n       upd.pop_back(); n     } n   } n   cl(k); n   for (int i = g[v].size() - 1; i >= 0; i--) { n     int to = g[v][i]; n     if (used[to]) continue; n     dfs(to, v, 2); n     while (!upd.empty()) { n       add(1, 0, k, upd.back()); n       upd.pop_back(); n     } n   } n   cl(k); n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (used[to]) continue; n     solve(to, cnt[to] / 2 + 1); n   } n } n int main() { n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int a, b; n     fastscan(a); n     fastscan(b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     int x; n     fastscan(x); n     a[i] = x; n   } n   solve(1, n / 2 + 1); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   stack<char> stack; n   int left = 0, right = 0, size = 0; n   for (int i = 0; i < s.length(); i++) { n     stack.push(s[i]); n     if (s[i] ==  ( ) { n       left++; n       continue; n     } n     if (s[i] ==  )  && left != 0) { n       left--; n       stack.pop(); n       continue; n     } n     if (s[i] ==  )  && left == 0) { n       right++; n       continue; n     } n   } n   cout << left <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const int maxn = 1000000 + 100; n long long n, ans; n long long inv[maxn], pro[maxn], invpro[maxn]; n void Prepare_C() { n   inv[1] = 1; n   for (int i = 2; i < maxn; ++i) { n     inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD; n   } n   pro[0] = invpro[0] = 1; n   for (int i = 1; i < maxn; ++i) { n     pro[i] = pro[i - 1] * i % MOD; n     invpro[i] = invpro[i - 1] * inv[i] % MOD; n   } n } n long long get_C(long long n, long long m) { n   if (n < m) { n     return 0; n   } n   return pro[n] * invpro[m] % MOD * invpro[n - m] % MOD; n } n long long Lucas(long long n, long long m) { n   if (n < MOD && m < MOD) { n     return get_C(n, m); n   } n   long long tmp = get_C(n % MOD, m % MOD); n   if (tmp == 0) { n     return 0; n   } n   return tmp * Lucas(n / MOD, m / MOD) % MOD; n } n long long fast_pow(long long x, long long n) { n   long long ans; n   for (ans = 1; n != 0; n >>= 1) { n     if ((n & 1) == 1) { n       ans = (ans * x) % MOD; n     } n     x = (x * x) % MOD; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   Prepare_C(); n   while (scanf( %I64d , &n) != EOF) { n     ans = 0; n     for (long long i = 1; i <= n; ++i) { n       long long first = 1; n       long long second = 1; n       if (i % 2 == 0) { n         first = (-1 * first % MOD + MOD) % MOD; n         second = first; n       } n       first = 2 * first % MOD * Lucas(n, i) % MOD * n               fast_pow(3, (n - i) * n + i) % MOD; n       long long tmp = ((fast_pow(3, n - i) - 1) % MOD + MOD) % MOD; n       tmp = ((fast_pow(tmp, n) - fast_pow(3, (n - i) * n)) % MOD + MOD) % MOD; n       second = 3 * second % MOD * Lucas(n, i) % MOD * tmp % MOD; n       ans = (ans + first + second) % MOD; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool myfunction(int i, int j) { return (i < j); } n void solve() { n   long long n; n   cin >> n; n   set<long long> st; n   long long a; n   for (long long int i = 0; i < n; i++) { n     cin >> a; n     st.insert(a); n   } n   if (st.size() > 3) { n     cout << -1; n     return; n   } else if (st.size() == 1) { n     cout << 0; n     return; n   } else if (st.size() == 2) { n     auto it = st.begin(); n     int a = *it; n     it++; n     int b = *it; n     if ((a + b) % 2 == 0) { n       cout << (b - a) / 2; n     } else n       cout << (b - a); n     return; n   } else { n     auto it = st.begin(); n     int a = *it; n     it++; n     int b = *it; n     it++; n     int c = *it; n     if (c - b != b - a) { n       cout <<  -1 ; n       return; n     } else n       cout << b - a; n     return; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int test = 1; n   while (test--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n #define int ll n  n set<int> s; n const int N = 200000 + 7; n const int M = 998244353; n  n int n; n int a[N], pre[2][N], total[N]; n  n int getsumx(int mod, int l, int r) { n   if (l > r) return 0; n   return pre[mod][r] - pre[mod][l - 1]; n } n  n int get(int pre, int fin) { n   int in = 0; n   in = getsumx((pre + 2) % 2, pre + 2, fin + 1); n   if (fin + 2 <= n) { n     in += total[n] - total[fin + 1]; n   } n   return in; n } n  n int megasmart(int nn, int vec[]) { n   n = nn; n   int sol = 0; n   int lim = 0; n   for (int i = 0; i < n; i++) { n     a[i] = vec[i + 1]; n     lim += a[i]; n   } n   reverse(a, a + n); n   for (int i = 1; i <= n; i++) { n     for (int m = 0; m <= 1; m++) { n       pre[m][i] = pre[m][i - 1]; n     } n     pre[i % 2][i] += a[i - 1]; n     total[i] = total[i - 1] + a[i - 1]; n   } n   lim--; n   lim /= 2; n   for (int it = 0; it < 2; it++) { n     for (int pre = it; pre < n; pre++) { n       int lo = 0, hi = (n - 1 - pre) / 2, first = n + 1; n       while (lo <= hi) { n         int j = (lo + hi) / 2; n         int fin = pre + 2 * j; n         bool ok = 0; n         if (get(pre, fin) <= lim) { n           ok = 1; n         } n         if (ok) { n           first = j; n           hi = j - 1; n         } else { n           lo = j + 1; n         } n       } n       hi = (n - 1 - pre) / 2; n       if (first <= hi) { n         sol += hi - first + 1; n       } n       if (n - 4 - pre >= 0) { n         lo = 0, hi = (n - 4 - pre) / 2; n         first = n + 1; n         while (lo <= hi) { n           int j = (lo + hi) / 2; n           int fin = pre + 2 * j; n           bool ok = 0; n           if (get(pre, fin) - a[n - 1] <= lim) { n             ok = 1; n           } n           if (ok) { n             first = j; n             hi = j - 1; n           } else { n             lo = j + 1; n           } n         } n         hi = (n - 4 - pre) / 2; n         if (first <= hi) { n           sol += hi - first + 1; n         } n       } n     } n     lim -= a[0]; n   } n   lim += 2 * a[0]; n   { n     int in = a[0]; n     for (int i = 1; i < n; i++) { n       in += a[i]; n       sol += (in <= lim); n     } n   } n   return sol % M; n } n  n int vec[N]; n  n signed main() { n   ios::sync_with_stdio(0); cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1LL; i <= n; i++) { n       cin >> vec[i]; n     } n     cout << megasmart(n, vec) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int in; n int a[105][105]; n int b[100000]; n int max(int l, int n) { n   int m = a[l][0]; n   int in = 0; n   for (int x = 0; x < n; x++) { n     if (m < a[l][x]) { n       m = a[l][x]; n       in = x; n     } n   } n   return in; n } n int main() { n   int n, m; n   cin >> n >> m; n   int i, j; n   for (i = 0; i < m; ++i) n     for (j = 0; j < n; ++j) cin >> a[i][j]; n   for (j = 0; j < m; ++j) { n     b[max(j, n)] += 1; n   } n   int m1 = b[0]; n   if (m != 1) n     for (i = 1; i < m; ++i) { n       if (m1 < b[i]) { n         m1 = b[i]; n         in = i; n       } n     } n   else n     in = max(0, n); n   cout << (in + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int a[51]; n int b[51]; n int d1[51]; n int d2[51]; n int main() { n   int n, l; n   int i, j, k; n   cin >> n >> l; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     if (i >= 2) d1[i] = a[i] - a[i - 1]; n   } n   d1[1] = a[1] + l - a[n]; n   for (i = 1; i <= n; i++) { n     cin >> b[i]; n     if (i >= 2) d2[i] = b[i] - b[i - 1]; n   } n   d2[1] = b[1] + l - b[n]; n   int same = 0; n   for (i = 1; i <= n; i++) { n     int flag = 1; n     for (k = 1, j = i; k <= n; j++, k++) { n       if (j > n) j = 1; n       if (d1[j] != d2[k]) { n         flag = 0; n         break; n       } n     } n     if (flag == 1) { n       same = 1; n       break; n     } n   } n   if (same == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i; n   cin >> n; n   long long int a[n], b[n], c[n + 1], x = n - 1; n   for (i = 0; i < n; i++) { n     b[i] = 0; n     c[i + 1] = 1; n   } n   c[0] = 1; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n     b[a[i]]++; n     if (a[i] == x) { n       while (b[x]) { n         if (x == 0 && b[x]) { n           x = -1; n           break; n         } n         x--; n         if (x == 0 && b[x]) { n           x = -1; n           break; n         } n       } n     } n     c[i + 1] += i + 1 - (n - 1 - x); n   } n   for (i = 0; i <= n; i++) cout << c[i] <<    ; n } n