#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } else { n     return b; n   } n } n long long power(long long b, long long e) { n   if (e == 0) return 1; n   if (e % 2) n     return ((b * power((b) * (b), (e - 1) / 2))); n   else n     return power((b) * (b), e / 2); n } n long long modpower(long long b, long long e, long long q) { n   long long MOD = q; n   if (e == 0) return 1; n   if (e % 2) n     return ((b % MOD) * modpower((b % MOD) * (b % MOD), (e - 1) / 2, q)) % MOD; n   else n     return modpower((b % MOD) * (b % MOD), e / 2, q) % MOD; n } n void dpv(vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n void dpv(vector<pair<long long, long long> > v) { n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i].first <<     << v[i].second << endl; n   } n } n void dpv(set<long long> v) { n   for (auto i : v) { n     cout << i <<    ; n   } n   cout << endl; n } n long long dp[31][31][51]; n long long f(long long a, long long b, long long c) { n   if (dp[a][b][c] || a * b == c || !c) { n     return dp[a][b][c]; n   } n   long long ans = 1e18; n   for (long long i = 1; i <= a - i; i++) { n     for (long long j = 0; j <= c; j++) { n       ans = min(ans, b * b + f(a - i, b, j) + f(i, b, c - j)); n     } n   } n   for (long long i = 1; i <= b - i; i++) { n     for (long long j = 0; j <= c; j++) { n       ans = min(ans, a * a + f(a, i, j) + f(a, b - i, c - j)); n     } n   } n   return dp[a][b][c] = ans; n } n void oblivious() { n   long long a, b, c; n   cin >> a >> b >> c; n   cout << f(a, b, c) << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(dp, 0, sizeof(dp)); n   long long t = 1; n   cin >> t; n   while (t--) { n     oblivious(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   unordered_map<char, int> m; n   char last = s[0]; n   int streak = 1; n   int cur_max = 0; n   if (k == 1) { n     cur_max = 1; n     m[last] = 1; n     streak = 0; n   } n   for (int i = 1; i < s.size(); i++) { n     if (s[i] == last) { n       streak++; n     } else { n       streak = 1; n     } n     if (streak == k) { n       streak = 0; n       if (m.find(s[i]) == m.end()) { n         m[s[i]] = 1; n       } else { n         m[s[i]]++; n       } n       cur_max = m[s[i]] > cur_max ? m[s[i]] : cur_max; n     } n     last = s[i]; n   } n   cout << cur_max << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename TT> n ostream &operator<<(ostream &s, pair<T, TT> t) { n   return s <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &s, vector<T> t) { n   s <<  { ; n   for (int i = 0; i < t.size(); i++) n     s << t[i] << (i == t.size() - 1 ?    :  , ); n   return s <<  }  << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, a1, b1, a2, b2; n   cin >> n >> m >> a1 >> b1 >> a2 >> b2; n   if (abs(a1 - a2) + abs(b1 - b2) <= 4) n     cout <<  First ; n   else if (abs(a1 - a2) + abs(b1 - b2) == 5 && n            min(abs(a1 - a2), abs(b1 - b2)) >= 1) n     cout <<  First ; n   else if (abs(a1 - a2) + abs(b1 - b2) == 6 && n            min(abs(a1 - a2), abs(b1 - b2)) >= 2) n     cout <<  First ; n   else n     cout <<  Second ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int maxn = 100010; n static const int INF = 0x3f3f3f3f; n static const int mod = (int)1e9 + 7; n static const double eps = 1e-6; n static const double pi = acos(-1); n void redirect() {} n char s[maxn]; n int cnt[26]; n int main() { n   redirect(); n   int T; n   scanf( %d , &T); n   while (T--) { n     memset(cnt, 0, sizeof(cnt)); n     scanf( %s , s); n     for (int i = 0; s[i]; i++) cnt[s[i] -  a ]++; n     bool flag = true; n     for (int i = 0; i < 26; i++) n       if (cnt[i] > 1) flag = false; n     int i = 0; n     while (i < 26 && cnt[i] == 0) i++; n     while (i < 26 && cnt[i] == 1) i++; n     for (; i < 26; i++) n       if (cnt[i]) flag = false; n     if (flag) n       puts( Yes ); n     else n       puts( No ); n   } n   return 0; n } n


/* n Problem :  n Algorithm :  n Status :  n */ n #include<bits/stdc++.h> n #include<iostream> n #include<cstring> n #include<cstdio> n #include<algorithm> n #include<cstdlib> n #define DEBUG cerr <<  Passing Line   << __LINE__<<   in Function [  << __FUNCTION__ <<  ]. n ; n using namespace std; n typedef long long ll; n typedef pair<int,int> pii; n template<class T> inline bool checkMax(T &a,const T &b) {return a < b ? a = b,1 : 0;} n template<typename T, typename...Args> inline void checkMax(T &a,const Args...arg) {checkMax(a,max(arg...));} n template<class T> inline bool checkMin(T &a,const T &b) {return a > b ? a = b,1 : 0;} n template<typename T, typename...Args> inline void checkMin(T &a,const Args...arg) {checkMin(a,min(arg...));} n  n namespace FastIO{ n     const int SIZE = 1 << 21; n     char ch; n     char in[SIZE],*inS = in,*inT = in + SIZE; n     char out[SIZE],*outS = out,*outT = out + SIZE; n  n     #define flush() fwrite(out,1,outS - out,stdout), outS = out n     #define getchar() (inS == inT && (inT = (inS = in) + fread(in,1,SIZE,stdin),inS == inT) ? EOF : *inS++) n     void putchar(char c) {*outS++ = c; if(outS == outT) flush();} n  n     struct Flusher {~Flusher() {flush();}} flusher; n  n     template<typename T> inline void read(T &x){ n         static int f; x = 0; f = 1; ch = getchar(); n         while(!isdigit(ch)) {if(ch ==  - ) f = -1; ch = getchar();} n         while(isdigit(ch)) {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} x *= f; n     } n  n     inline void read(char* s){ n         static int len; len = 0; ch = getchar(); n         while(ch ==   n  || ch ==     || ch ==   0 ) ch = getchar(); n         while(ch !=   n  && ch !=     && ch != EOF) s[len++] = ch, ch = getchar(); s[len] =   0 ; n     }     n  n     template<typename T> inline void write(T x){ n         if(!x) putchar( 0 ); n         if(x < 0) {x = -x; putchar( - );} n         static int sta[20]; int tot = 0; n         while(x) {sta[tot++] = x % 10; x /= 10;} n         while(tot) putchar(sta[--tot] + 48); n     } n  n     inline void write(char c) {putchar(c);} n     template<typename T> inline void writeln(T c) {write(c); putchar(  n );} n  n     template<typename T,typename... Args> inline void read(T &t, Args &...args) {read(t); read(args...);} n } n  n using FastIO::read; n using FastIO::write; n using FastIO::writeln; n  n const int INF = 0x3f3f3f3f; n const ll llINF = 1e18; n const int MAXN = 6e4 + 5; n const int MAXC = 5; n  n int n,C,Q,T; n int cnt[MAXC][MAXN],p[MAXN]; n ll comb[MAXN][MAXC],w[MAXC][MAXN * MAXC]; n  n struct Node{ n     int l,r; ll w; n     Node(int _l = 0,int _r = 0,ll _w = 0) : l(_l), r(_r), w(_w) {} n }; n  n struct Deque{ n     int head,tail; n     Node a[MAXN * MAXC * 2 + 100]; n  n     Node& operator [] (const int &i) {return a[head + i - 1];} n  n     void push_front(Node u) {a[--head] = u; assert(head >= 1);} n     void push_back(Node u) {a[++tail] = u; assert(tail < MAXN * MAXC * 2 + 100);} n     void clear() {head = MAXN * MAXC + 5, tail = MAXN * MAXC + 4;} n     int size() {return tail - head + 1;} n     Deque() {clear();} n } q[MAXC]; n  n void Init(int n){ n     comb[0][0] = 1; n     for(int i = 1;i <= n;i++){ n         comb[i][0] = 1; n         for(int j = 1;j <= min(MAXC - 1,i);j++) n             comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j]; n     } n     for(int i = 0;i <= n;i++){ n         for(int j = 1;j < MAXC;j++) comb[i][j] += comb[i][j - 1]; n     } n } n  n ll calc(int n,int c){ n     if(n <= 0) return 1; n     assert(n < MAXN); n     return comb[n - 1][c]; n } n  n bool cmp(const Node &x,const Node &y){ n     return p[x.r] < p[y.r]; n } n  n Node GetNxt(int c,int pos,ll rk){ n     int num = cnt[c][pos]; n     int l = num + 1, r = q[c].size(), res = 0; n     while(l <= r){ n         int mid = (l + r) >> 1; n         if(w[c][mid] - w[c][num] >= rk) res = mid, r = mid - 1; n         else l = mid + 1; n     } n     assert(res >= 1 && res <= q[c].size()); n     return Node(q[c][res].l,q[c][res].r,w[c][res - 1] - w[c][num]); n } n  n void Solve(){ n     read(n,C,Q); n     for(int i = 1;i <= n;i++) read(p[i]); n     for(int c = 1;c <= C;c++){ n         q[c].clear(); vector<Node> front,back;  n         for(int i = 1;i <= n;i++) cnt[c][i] = 0; n         q[c].push_back(Node(n,n,1)); n         for(int i = n - 1;i >= 1;i--){ n             front.clear(); back.clear(); n             for(int j = 1;j <= min(c,n - i);j++){ n                 if(p[i + j] < p[i]) n                     front.emplace_back(i,i + j,calc(n - i - j,c - j)), cnt[c][i + 1] += 1; n                 else back.emplace_back(i,i + j,calc(n - i - j,c - j)); n             } n             sort(front.begin(),front.end(),cmp); reverse(front.begin(),front.end()); n             sort(back.begin(),back.end(),cmp); n             for(auto x : front) q[c].push_front(x); n             for(auto x : back) q[c].push_back(x); n         } n         for(int i = 1;i <= n;i++) cnt[c][i] += cnt[c][i - 1]; n         assert(q[c].size() <= MAXN * MAXC + 5); n         for(int i = 1, sz = q[c].size();i <= sz;i++) n             w[c][i] = w[c][i - 1] + q[c][i].w; n     } n     while(Q--){ n         int pos; ll rk; read(pos,rk); n         if(rk > calc(n,C)) {writeln(-1); continue;} n         int x = 1, c = C; vector<pii> vec; vec.clear(); n         while(c && x <= n){ n             Node res = GetNxt(c,x,rk); n             assert(res.w < rk); n             rk -= res.w; c -= (res.r - res.l); x = res.r + 1; n             vec.emplace_back(res.l,res.r); n         } n         int flag = 0; n         for(auto pi : vec){ n             if(pos >= pi.first && pos <= pi.second){ n                 writeln(p[pi.second - pos + pi.first]); n                 flag = 1; break; n             } n         } n         if(!flag) writeln(p[pos]); n     } n } n  n int main(){ n //    freopen( data.in , r ,stdin); n //    freopen( data.out , w ,stdout); n     Init(MAXN - 1); read(T); while(T--) Solve(); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n struct MCMF { n   enum { MAXN = 6500 }; n   struct Edge { n     int x, y; n     int cap, cost; n   }; n   vector<Edge> E; n   vector<int> adj[MAXN]; n   int N, prev[MAXN]; n   int dist[MAXN], phi[MAXN]; n   MCMF(int NN) : N(NN) {} n   void add(int x, int y, int cap, int cost) { n     Edge e1 = {x, y, cap, cost}, e2 = {y, x, 0, -cost}; n     adj[e1.x].push_back(E.size()); n     E.push_back(e1); n     adj[e2.x].push_back(E.size()); n     E.push_back(e2); n   } n   vector<queue<int> > Q; n   void mcmf(int s, int t, int &flowVal, int &flowCost) { n     int x; n     flowVal = flowCost = 0; n     memset(phi, 0, sizeof(phi)); n     Q.resize(6500); n     while (true) { n       for (x = 0; x < N; x++) prev[x] = -1; n       for (x = 0; x < N; x++) dist[x] = 1000000000; n       dist[s] = prev[s] = 0; n       Q[dist[s]].push(s); n       for (int i = 0; i < 6400; i++) { n         while (!Q[i].empty()) { n           x = Q[i].front(); n           Q[i].pop(); n           if (dist[x] < i) continue; n           vector<int>::iterator it; n           for (it = adj[x].begin(); it != adj[x].end(); it++) { n             const Edge &e = E[*it]; n             if (e.cap <= 0) continue; n             int cc = e.cost + phi[x] - phi[e.y]; n             if (dist[x] + cc < dist[e.y]) { n               dist[e.y] = dist[x] + cc; n               prev[e.y] = *it; n               Q[dist[e.y]].push(e.y); n             } n           } n         } n       } n       if (prev[t] == -1) break; n       int z = 1000000000; n       for (x = t; x != s; x = E[prev[x]].x) z = min(z, E[prev[x]].cap); n       for (x = t; x != s; x = E[prev[x]].x) { n         E[prev[x]].cap -= z; n         E[prev[x] ^ 1].cap += z; n       } n       flowVal += z; n       flowCost += z * (dist[t] - phi[s] + phi[t]); n       for (x = 0; x < N; x++) n         if (prev[x] != -1) phi[x] += dist[x]; n     } n   } n } * mc; n int a[90][90]; n int main() { n   int i, j, k, n, m; n   scanf( %d %d , &n, &m); n   mc = new MCMF(n * m + 10); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) cin >> a[i][j]; n   int src = n * m + 1; n   int dest = n * m + 2; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if ((i + j) % 2 == 0) n         mc->add(src, i * m + j, 1, 0); n       else { n         mc->add(i * m + j, dest, 1, 0); n         continue; n       } n       for (k = 0; k < 4; k++) { n         int ni = i + dx[k]; n         int nj = j + dy[k]; n         if (ni < n && nj < m && ni >= 0 && nj >= 0) { n           mc->add(i * m + j, ni * m + nj, 1, a[i][j] != a[ni][nj]); n         } n       } n     } n   } n   int fval, fcost; n   mc->mcmf(src, dest, fval, fcost); n   cout << fcost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3005; n const int mod = 1e9 + 7; n int dp1[N][N], dp2[N][N], a[N]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     memset(dp1, 0, sizeof(dp1)); n     memset(dp2, 0, sizeof(dp2)); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         dp1[i][j] = dp1[i - 1][j]; n       } n       dp1[i][a[i]]++; n     } n     for (int i = n; i >= 1; i--) { n       for (int j = 1; j <= n; j++) { n         dp2[i][j] = dp2[i + 1][j]; n       } n       dp2[i][a[i]]++; n     } n     long long ans = 0; n     for (int i = 2; i <= n; i++) { n       for (int j = i + 1; j < n; j++) { n         ans += dp1[i - 1][a[j]] * dp2[j + 1][a[i]]; n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool SR(int &x) { return scanf( %d , &x) == 1; } n bool SR(long long &x) { return scanf( %lld , &x) == 1; } n bool SR(double &x) { return scanf( %lf , &x) == 1; } n bool SR(char *s) { return scanf( %s , s) == 1; } n bool RI() { return true; } n template <typename I, typename... T> n bool RI(I &x, T &...tail) { n   return SR(x) && RI(tail...); n } n void SP(const int x) { printf( %d , x); } n void SP(const long long x) { printf( %lld , x); } n void SP(const double x) { printf( %.16lf , x); } n void SP(const char *s) { printf( %s , s); } n void PL() { puts(  ); } n template <typename I, typename... T> n void PL(const I x, const T... tail) { n   SP(x); n   if (sizeof...(tail)) putchar(   ); n   PL(tail...); n } n const int maxn = 3e3 + 3; n const int second = 200; n struct Node { n   static Node mem[maxn + 514], *pmem; n   int dep; n   Node *fail, *to[2]; n   int who; n   Node() : dep(-1), fail(0), to{NULL, NULL}, who(-1) {} n   Node(int _d, int _w) : dep(_d), fail(0), to{NULL, NULL}, who(_w) {} n } Node::mem[maxn + 514], *Node::pmem = Node::mem; n int pos[maxn]; n struct AC { n   AC() { root = new (Node::pmem++) Node(-1, -1); } n   Node *root; n   void add(const string &s, int id) { n     Node *pt = root; n     for (int i = 0; i < int(((int)(s).size())); i++) { n       if (!pt->to[s[i] -  a ]) n         pt->to[s[i] -  a ] = new (Node::pmem++) Node(1 + pt->dep, -1); n       pt = pt->to[s[i] -  a ]; n     } n     pt->who = id; n     pos[id] = pt - Node::mem; n   } n   void build() { n     queue<Node *> q; n     q.push(root); n     root->fail = root; n     while (((int)(q).size())) { n       Node *pt = q.front(); n       q.pop(); n       for (int i = 0; i < int(2); i++) n         if (pt->to[i]) q.push(pt->to[i]); n       if (pt->dep == -1) { n         for (int i = 0; i < int(2); i++) n           if (pt->to[i]) pt->to[i]->fail = root; n         for (int i = 0; i < int(2); i++) n           if (!pt->to[i]) pt->to[i] = root; n       } else { n         for (int i = 0; i < int(2); i++) n           if (pt->to[i]) { n             Node *&cur = pt->to[i]->fail; n             for (cur = pt->fail; cur != root && !cur->to[i]; cur = cur->fail) n               ; n             if (cur->to[i]) cur = cur->to[i]; n           } n         for (int i = 0; i < int(2); i++) n           if (!pt->to[i]) { n             Node *&cur = pt->to[i]; n             for (cur = pt->fail; cur != root && !cur->to[i]; cur = cur->fail) n               ; n             if (cur->to[i]) cur = cur->to[i]; n           } n       } n     } n   } n } ac; n int n, m; n string s[maxn]; n void read() { n   RI(n, m); n   for (int i = 0; i < int(m); i++) { n     static char in[maxn]; n     RI(in); n     s[i] = in; n   } n } n const int MOD = 1e9 + 7; n string os[maxn]; n int sig; n int f[maxn][maxn]; n int _c[2][maxn][maxn / second + 1]; n int (*prv)[maxn / second + 1], (*cur)[maxn / second + 1]; n int big; n int om; n void build() { n   om = m; n   for (int i = 0; i < int(m); i++) os[i] = s[i]; n   sort(s, s + m); n   m = unique(s, s + m) - s; n   sort(s, s + m, [&](const string &a, const string &b) { n     return make_pair(((int)(a).size()), a) > make_pair(((int)(b).size()), b); n   }); n   for (big = 0; big < m && ((int)(s[big]).size()) > second; big++) n     ; n   for (int i = 0; i < int(m); i++) ac.add(s[i], i); n   ac.build(); n   sig = Node::pmem - Node::mem; n } n int ans[maxn]; n void sol_big() { n   for (int i = 0; i < int(sig); i++) f[1][i] = Node::mem[i].to[0] - Node::mem; n   for (int i = 0; i < int(sig); i++) f[2][i] = Node::mem[i].to[1] - Node::mem; n   for (int i = (3); i <= int(n); i++) n     for (int j = 0; j < int(sig); j++) f[i][j] = f[i - 2][f[i - 1][j]]; n   prv = _c[0]; n   cur = _c[1]; n   for (int i = 0; i < int(sig); i++) n     for (int j = 0; j < int(big); j++) { n       Node *pt = Node::mem + i; n       for (pt = pt->to[0]; pt->who != j && pt->dep != -1; pt = pt->fail) n         ; n       prv[i][j] = (pt->who == j); n     } n   for (int i = 0; i < int(sig); i++) n     for (int j = 0; j < int(big); j++) { n       Node *pt = Node::mem + i; n       for (pt = pt->to[1]; pt->who != j && pt->dep != -1; pt = pt->fail) n         ; n       cur[i][j] = (pt->who == j); n     } n   for (int _ = (3); _ <= int(n); _++) { n     static int tmp[maxn][maxn / second + 1]; n     auto &m1 = cur, &m2 = prv; n     for (int i = 0; i < int(sig); i++) n       for (int j = 0; j < int(big); j++) n         tmp[i][j] = m1[i][j] + m2[f[_ - 1][i]][j]; n     for (int i = 0; i < int(sig); i++) n       for (int j = 0; j < int(big); j++) n         if (tmp[i][j] >= MOD) tmp[i][j] -= MOD; n     swap(prv, cur); n     for (int i = 0; i < int(sig); i++) n       for (int j = 0; j < int(big); j++) cur[i][j] = tmp[i][j]; n   } n   for (int i = 0; i < int(big); i++) ans[i] = cur[0][i]; n } n string h[maxn], t[maxn]; n int ap[maxn][maxn]; n void merge(const string &p, const string &q, int res[]) { n   for (int i = 0; i < int(((int)(p).size())); i++) { n     Node *pt = ac.root; n     for (int j = (i); j <= int(((int)(p).size()) - 1); j++) n       if (!(pt = pt->to[p[j] -  a ])) n         while (1) n           ; n     for (int j = 0; j < int(((int)(q).size())); j++) { n       if (!(pt = pt->to[q[j] -  a ])) n         while (1) n           ; n       if (big <= pt->who && pt->who < m && n           pt->dep == (((int)(p).size()) - i) + j) n         res[pt->who]++; n     } n   } n   for (int i = (big); i <= int(m - 1); i++) n     if (res[i] >= MOD) res[i] -= MOD; n } n void sol_small() { n   h[1] =  a ; n   h[2] =  b ; n   for (int i = (3); i <= int(15); i++) h[i] = h[i - 1] + h[i - 2]; n   for (int i = (1); i <= int(15); i++) t[i] = h[i]; n   if (n <= 15) { n     for (int j = (big); j <= int(m - 1); j++) n       for (size_t o = h[n].find(s[j]); ~o; o = h[n].find(s[j], o + 1)) ans[j]++; n   } else { n     for (int i = (14); i <= int(15); i++) n       for (int j = (big); j <= int(m - 1); j++) n         for (size_t o = h[i].find(s[j]); ~o; o = h[i].find(s[j], o + 1)) n           ap[i][j]++; n     for (int i = (14); i <= int(15); i++) n       h[i].resize(second), t[i].erase(t[i].begin(), t[i].end() - second); n     for (int i = (16); i <= int(n); i++) { n       for (int j = (big); j <= int(m - 1); j++) n         ap[i][j] = ap[i - 1][j] + ap[i - 2][j]; n       for (int j = (big); j <= int(m - 1); j++) n         if (ap[i][j] >= MOD) ap[i][j] -= MOD; n       h[i] = h[i - 1]; n       t[i] = t[i - 2]; n       merge(t[i - 1], h[i - 2], ap[i]); n     } n     for (int i = (big); i <= int(m - 1); i++) ans[i] = ap[n][i]; n   } n } n void sol() { n   sol_big(); n   sol_small(); n   for (int i = 0; i < int(om); i++) { n     int id = lower_bound(s, s + m, os[i], n                          [&](const string &a, const string &b) { n                            return make_pair(((int)(a).size()), a) > n                                   make_pair(((int)(b).size()), b); n                          }) - n              s; n     PL(ans[id]); n   } n } n int main() { n   read(); n   build(); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   int mx = -1, my = -1; n   while (t--) { n     char c; n     int t1, t2; n     cin >> c >> t1 >> t2; n     if (t1 > t2) swap(t1, t2); n     if (c ==  + ) { n       mx = max(mx, t1); n       my = max(my, t2); n     } else { n       if (mx <= t1 && my <= t2) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-5; n bool board[8][8]; n const int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1}; n const int dy[8] = {2, 1, -1, -2, -2, -1, 1, 2}; n void check(int x, int y, int n) { n   board[x][y] = true; n   if (n == 1) { n     for (int i = (0); i < (int)(8); ++i) { n       board[i][y] = true; n       board[x][i] = true; n     } n   } n   for (int r = (0); r < (int)(8); ++r) { n     if (x + dx[r] < 8 && x + dx[r] >= 0) { n       if (y + dy[r] < 8 && y + dy[r] >= 0) { n         board[x + dx[r]][y + dy[r]] = true; n       } n     } n   } n } n int main(void) { n   int x, y, count = 0; n   string line; n   cin >> line; n   x = line[0] -  a ; n   y = line[1] -  1 ; n   check(x, y, 1); n   cin >> line; n   x = line[0] -  a ; n   y = line[1] -  1 ; n   check(x, y, 2); n   for (int x = (0); x < (int)(8); ++x) { n     for (int y = (0); y < (int)(8); ++y) { n       if (board[x][y]) count++; n     } n   } n   cout << 64 - count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 10; n int SL[MAXN], SR[MAXN]; n int A[MAXN][2], n; n int main() { n   while (~scanf( %d , &n)) { n     SL[0] = SR[0] = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d%d , &A[i][0], &A[i][1]); n       SL[i] = A[i][0] + SL[i - 1]; n       SR[i] = A[i][1] + SR[i - 1]; n     } n     int maxn = abs(SL[n] - SR[n]); n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       int _SL = SL[n] - A[i][0] + A[i][1]; n       int _SR = SR[n] - A[i][1] + A[i][0]; n       int _ans = abs(_SL - _SR); n       if (maxn < _ans) { n         maxn = _ans; n         ans = i; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[300005], tp, lt, ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= n; i++) { n     tp = min(a[i] / 2, lt); n     lt -= tp; n     a[i] -= tp * 2; n     ans += tp + a[i] / 3; n     lt += a[i] % 3; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char my_array[50][50]; n bool visited[50][50]; n bool dfs(int i, int j, int c_i, int c_j) { n   if (visited[i][j]) { n     return true; n   } n   visited[i][j] = true; n   char col = my_array[i][j]; n   bool res = false; n   if (i > 0 && col == my_array[i - 1][j] && c_i != i - 1) { n     res |= dfs(i - 1, j, i, j); n   } n   if (j > 0 && col == my_array[i][j - 1] && c_j != j - 1) { n     res |= dfs(i, j - 1, i, j); n   } n   if (i < n - 1 && col == my_array[i + 1][j] && c_i != i + 1) { n     res |= dfs(i + 1, j, i, j); n   } n   if (j < m - 1 && col == my_array[i][j + 1] && c_j != j + 1) { n     res |= dfs(i, j + 1, i, j); n   } n   return res; n } n int main(int argc, const char* argv[]) { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> my_array[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (!visited[i][j] && dfs(i, j, -1, -1)) { n         cout <<  Yes ; n         return 0; n       } n     } n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e6, N2 = 5e3, delta = 46639, mod = 1e9 + 7, oo = 1e16; n const long double PI = 3.141592653589793; n int main() { n   long long int n, cnt1 = 1, cnt2 = 1, mx = 0; n   string s; n   cin >> n >> s; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = i; j < n; j++) { n       string t = s; n       long long int cnt = 0, trn = 0, ans = 0; n       swap(t[i], t[j]); n       for (long long int k = 0; k < n; k++) { n         if (t[k] ==  ( ) n           cnt++; n         else { n           if (cnt <= 0) n             trn++; n           else n             cnt--; n         } n       } n       for (long long int k = n - 1; k >= 0; k--) { n         if (cnt == 0 && trn == 0) ans++; n         if (t[k] ==  ( ) n           cnt--, trn--; n         else n           cnt++, trn++; n       } n       mx = max(mx, ans); n       if (mx == ans) cnt1 = i + 1, cnt2 = j + 1; n     } n   } n   cout << mx <<   n  << cnt1 <<     << cnt2; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2002; n int main() { n   int h, m; n   char c1, c2, c3, c4; n   double r1, r2; n   cin >> c1 >> c2 >> c3 >> c3 >> c4; n   h = (c1 -  0 ) * 10 + (c2 -  0 ); n   m = (c3 -  0 ) * 10 + (c4 -  0 ); n   h %= 12; n   double hh = h + double(m) / 60; n   r1 = hh / 12.0 * 360; n   r2 = double(m) / 60.0 * 360; n   printf( %0.10f %0.10f n , r1, r2); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const long double PI = 3.14159265358979323846; n const long double eps = 1e-5; n const int INF = numeric_limits<int>::max(); n const long long LINF = numeric_limits<long long>::max(); n const long long mod = 1000 * 1000 * 1000 + 7; n struct moves { n   int r1, c1, r2, c2; n   moves(int _r1, int _c1, int _r2, int _c2) n       : r1(_r1), c1(_c1), r2(_r2), c2(_c2) {} n }; n vector<moves> solve(int n, int m, vector<vector<int> > b, n                     vector<pair<int, int> > p) { n   vector<moves> res; n   int c = -1; n   for (int j = 0; j < n; ++j) { n     for (int i = 0; i < n; ++i) { n       if (b[i][j] == -1) { n         continue; n       } n       ++c; n       int d = c - j; n       if (d < 0) { n         for (int k = 0; k < -d; ++k) { n           swap(b[i][j - k], b[i][j - 1 - k]); n           res.push_back(moves(i, j - k, i, j - 1 - k)); n         } n       } n       if (d == 0) { n         continue; n       } n       if (d > 0) { n         --c; n         int l = j + 1; n         while (b[i][l] != -1) ++l; n         for (int k = l - 1; k >= j; --k) { n           swap(b[i][k], b[i][k + 1]); n           res.push_back(moves(i, k, i, k + 1)); n         } n       } n     } n   } n   for (int j = 0; j < n; ++j) { n     if (b[0][j] != -1) { n       swap(b[0][j], b[1][j]); n       res.push_back(moves(0, j, 1, j)); n     } n   } n   for (int i = 2; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (b[i][j] != -1) { n         for (int k = i - 1; k >= 1; --k) { n           swap(b[k + 1][j], b[k][j]); n           res.push_back(moves(k + 1, j, k, j)); n         } n       } n     } n   } n   for (int t = 0; t < m; ++t) { n     int cl = 0; n     while (b[1][cl] != t) ++cl; n     swap(b[1][cl], b[0][cl]); n     res.push_back(moves(1, cl, 0, cl)); n     for (int j = cl; j > t; --j) { n       swap(b[0][j], b[0][j - 1]); n       res.push_back(moves(0, j, 0, j - 1)); n     } n     for (int j = cl; j > t; --j) { n       swap(b[1][j], b[1][j - 1]); n       res.push_back(moves(1, j - 1, 1, j)); n     } n   } n   return res; n } n void solve() { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > b1(n, vector<int>(n, -1)), b2(n, vector<int>(n, -1)); n   vector<pair<int, int> > p1(m), p2(m); n   for (int i = 0; i < m; ++i) { n     int r, c; n     cin >> r >> c; n     --r, --c; n     p1[i] = pair<int, int>(r, c); n     b1[r][c] = i; n   } n   for (int i = 0; i < m; ++i) { n     int r, c; n     cin >> r >> c; n     --r, --c; n     p2[i] = pair<int, int>(r, c); n     b2[r][c] = i; n   } n   if (n == 1) { n     cout << 0 << endl; n     return; n   } n   vector<moves> r1 = solve(n, m, b1, p1); n   vector<moves> r2 = solve(n, m, b2, p2); n   cout << r1.size() + r2.size() <<   n ; n   for (int i = 0; i < r1.size(); ++i) { n     cout << r1[i].r1 + 1 <<     << r1[i].c1 + 1 <<     << r1[i].r2 + 1 <<     n          << r1[i].c2 + 1 <<   n ; n   } n   for (int i = r2.size() - 1; i >= 0; --i) { n     cout << r2[i].r2 + 1 <<     << r2[i].c2 + 1 <<     << r2[i].r1 + 1 <<     n          << r2[i].c1 + 1 <<   n ; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool grid[3][10004]; n int n; n long long dp[10004][8]; n long long calc(int i, int mask) { n   int mask2 = 0; n   if (grid[0][i]) mask2 |= 1; n   if (grid[1][i]) mask2 |= 2; n   if (grid[2][i]) mask2 |= 4; n   if ((mask & mask2) != 0) { n     return 0; n   } n   mask |= mask2; n   long long ret = dp[i][mask]; n   if (ret != -1) { n     return ret; n   } n   if (i == n - 1) { n     if (mask == 1 || mask == 4 || mask == 7) { n       return 1; n     } else n       return 0; n   } n   switch (mask) { n     case 0: n       ret = calc(i + 1, 4); n       ret += calc(i + 1, 1); n       ret += calc(i + 1, 7); n       ret %= 1000000007; n       break; n     case 1: n       ret = calc(i + 1, 6); n       ret += calc(i + 1, 0); n       ret %= 1000000007; n       break; n     case 2: n       ret = calc(i + 1, 5); n       break; n     case 3: n       ret = calc(i + 1, 4); n       break; n     case 4: n       ret = calc(i + 1, 3); n       ret += calc(i + 1, 0); n       ret %= 1000000007; n       break; n     case 5: n       ret = calc(i + 1, 2); n       break; n     case 6: n       ret = calc(i + 1, 1); n       break; n     case 7: n       ret = calc(i + 1, 0); n       break; n     default: n       break; n   } n   dp[i][mask] = ret; n   return ret; n } n int main() { n   cin >> n; n   string a, b, c; n   cin >> a >> b >> c; n   memset(grid, true, sizeof grid); n   memset(dp, -1, sizeof dp); n   for (int i = 0; i < n; i++) { n     if (a[i] ==  . ) { n       grid[0][i] = false; n     } n   } n   for (int i = 0; i < n; i++) { n     if (b[i] ==  . ) { n       grid[1][i] = false; n     } n   } n   for (int i = 0; i < n; i++) { n     if (c[i] ==  . ) { n       grid[2][i] = false; n     } n   } n   if (a.find( O ) == string::npos && c.find( O ) == string::npos && n       b.find( O ) != 0 && b.find( O ) != n - 1) { n     long long x = calc(0, 0); n     int p = b.find( O ); n     memset(dp, -1, sizeof dp); n     grid[1][p - 1] = true; n     grid[2][p - 1] = true; n     grid[0][p + 1] = true; n     grid[1][p + 1] = true; n     long long y = calc(0, 0); n     grid[0][p - 1] = true; n     grid[2][p - 1] = false; n     grid[0][p + 1] = false; n     grid[2][p + 1] = true; n     memset(dp, -1, sizeof(dp)); n     long long z = calc(0, 0); n     cout << (x - y - z + 1000000007 + 1000000007) % 1000000007 << endl; n   } else { n     cout << calc(0, 0) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > vec; n   vector<pair<int, int> > event; n   multiset<int> s; n   for (int i = 0; i < m; i++) { n     int l, r, t; n     gn(l, r); n     gn(t); n     event.push_back(pair<int, int>(l, t)); n     event.push_back(pair<int, int>(r, -t - 1)); n   } n   sort(event.begin(), event.end()); n   for (pair<int, int> e : event) { n     if (!s.empty()) vec.push_back(pair<int, int>(*s.begin() - e.first, 1)); n     if (e.second < 0) n       s.erase(s.find(-e.second - 1)); n     else n       s.insert(e.second); n     if (!s.empty()) vec.push_back(pair<int, int>(*s.begin() - e.first, -1)); n   } n   sort(vec.begin(), vec.end()); n   long long now = 0, d = 0; n   int cur = 0; n   int pre = -1e9; n   for (int i = 0; i < n; i++) { n     int t; n     gn(t); n     while (cur < vec.size() && vec[cur].first < t) { n       now += d * (vec[cur].first - pre); n       d += vec[cur].second; n       pre = vec[cur].first; n       cur++; n     } n     printf( %I64d n , now + d * (t - pre)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, INF = 0X3F3F3F3F; n int n, tot; n char s[N]; n vector<int> ans; n set<int> s1, s2; n int main() { n   scanf( %s , s + 1), n = strlen(s + 1); n   for (int i = (1); i <= (n); ++i) { n     if (s[i] ==  L ) n       s1.insert(i); n     else n       s2.insert(i); n   } n   s1.insert(INF), s2.insert(INF); n   if (make_pair(-((int)(s1).size()), *s1.begin()) > n       make_pair(-((int)(s2).size()), *s2.begin())) n     swap(s1, s2); n   int go = *s1.begin(); n   for (int i = (1); i <= (n); ++i) { n     if (i & 1) { n       if (go == INF || (*s2.lower_bound(go) == INF && *s1.begin() != go && n                         *s1.begin() < *s2.begin())) { n         ++tot; n         go = *s1.begin(); n       } n       s1.erase(go); n       ans.push_back(go); n       go = *s2.lower_bound(go); n     } else { n       if (go == INF || (*s1.lower_bound(go) == INF && *s2.begin() != go && n                         *s2.begin() < *s1.begin())) { n         ++tot; n         go = *s2.begin(); n       } n       s2.erase(go); n       ans.push_back(go); n       go = *s1.lower_bound(go); n     } n   } n   printf( %d n , tot); n   for (auto x : ans) printf( %d  , x); n   return 0; n } n


#include <iostream> n #include <vector> n #include <array> n #define vi vector<int> n #define pii pair<int, int> n #define fst first n #define snd second n  n using namespace std; n  n const int SZ = (1 << 19) + 5, INF = (1e9) + 7; n  n int N, ans[200001]; n vi idx[200001]; n  n array<int, 4> seg[2][SZ]; n int lz[2][SZ]; n  n array<int, 4> merge(const array<int, 4>& L, const array<int, 4>& R) n { n  return {min(L[0], R[0]), max(L[1], R[1]), min(L[2], R[2]), max(L[3], R[3])}; n } n  n inline void prop(int t, int idx, int L, int R) n { n  if (lz[t][idx]) n  { n   for (int i = 0; i < 4; i++)  n   { n    if (seg[t][idx][i] > -INF && seg[t][idx][i] < INF) seg[t][idx][i] += lz[t][idx]; n   } n   if (L < R) n   { n    lz[t][2 * idx] += lz[t][idx]; n    lz[t][2 * idx + 1] += lz[t][idx]; n   } n   lz[t][idx] = 0; n  } n } n  n void build(int t, int idx, int L, int R) n { n  seg[t][idx][0] = seg[t][idx][2] = INF; n  seg[t][idx][1] = seg[t][idx][3] = -INF; n  if (L == R)  n  { n   seg[t][idx][(L & 1) << 1] = 0; n   seg[t][idx][(L & 1) << 1 | 1] = 0; n  } n  else if (L < R) n  { n   int M = L + R >> 1; n   build(t, 2 * idx, L, M); n   build(t, 2 * idx + 1, M + 1, R); n   seg[t][idx] = merge(seg[t][2 * idx], seg[t][2 * idx + 1]); n  } n } n  n void upd(int t, int idx, int L, int R, int l, int r, int v) n { n  l = max(L, l); r = min(R, r); prop(t, idx, L, R); n  if (l <= r) n  { n   if (L == l && R == r) {lz[t][idx] += v; prop(t, idx, L, R);} n   else n   { n    int M = L + R >> 1; n    upd(t, 2 * idx, L, M, l, r, v); n    upd(t, 2 * idx + 1, M + 1, R, l, r, v); n    seg[t][idx] = merge(seg[t][2 * idx], seg[t][2 * idx + 1]); n   } n  } n } n  n array<int, 4> qry(int t, int idx, int L, int R, int l, int r) n { n  l = max(L, l); r = min(R, r); n  if (l <= r) n  { n   prop(t, idx, L, R); n   if (L == l && R == r) {return seg[t][idx];} n   else n   { n    int M = L + R >> 1; n    return merge(qry(t, 2 * idx, L, M, l, r), qry(t, 2 * idx + 1, M + 1, R, l, r)); n   } n  } n  return {INF, -INF, INF, -INF}; n } n  n int main() n { n  cin >> N; n  for (int i = 1; i <= N; i++) n  { n   int x; cin >> x; n   idx[x].push_back(i); n  } n  build(0, 1, 0, N); n  build(1, 1, 0, N); n  for (int i = 1; i <= N; i++) n  { n   upd(i & 1, 1, 0, N, i, N, -1); n  } n  for (int i = 1; i <= N; i++) n  { n   for (const auto &x : idx[i]) n   { n    for (int j = 0; j < 2; j++) n    { n     array<int, 4> A = qry(j, 1, 0, N, 0, x - 1), B = qry(j, 1, 0, N, x, N); n     ans[x] = max(ans[x], max(B[1], B[3]) - A[j << 1]); n     ans[x] = max(ans[x], A[j << 1 | 1] - min(B[0], B[2]));   n    }  n   } n   for (const auto &x : idx[i])  n   { n    for (int j = 0; j < 2; j++) upd(j, 1, 0, N, x, N, 1); n   } n   for (const auto &x : idx[i]) n   { n    for (int j = 0; j < 2; j++) n    { n     upd(j, 1, 0, N, x, N, -1); n     array<int, 4> A = qry(j, 1, 0, N, 0, x - 1), B = qry(j, 1, 0, N, x, N); n     ans[x] = max(ans[x], max(B[1], B[3]) - A[j << 1]); n     ans[x] = max(ans[x], A[j << 1 | 1] - min(B[0], B[2])); n     upd(j, 1, 0, N, x, N, 1); n    } n   } n  } n  for (int i = 1; i <= N; i++) n  { n   cout << ans[i] <<    n [i == N]; n  } n  return 0; n }


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n template <typename T> n inline T Abs(T x) { n   return (x >= 0) ? x : -x; n } n template <typename T> n inline T sqr(T x) { n   return x * x; n } n template <typename T> n string toStr(T x) { n   stringstream st; n   st << x; n   string s; n   st >> s; n   return s; n } n const int INF = (int)1E9; n const long long INF64 = (long long)1E18; n const long double EPS = 1E-9; n const long double PI = 3.1415926535897932384626433832795; n map<pair<long long, long long>, long long> d; n long long solve(long long n, long long t) { n   if (d.count(make_pair(n, t))) return d[make_pair(n, t)]; n   if (n == 0) return d[make_pair(n, t)] = 0; n   long long l = 1, p = 0; n   while (2 * l <= n) { n     l *= 2; n     p++; n   } n   if (t == 1) return d[make_pair(n, t)] = p + 1; n   if (n <= 2) return d[make_pair(n, t)] = 0; n   long long ans = solve(l / 2 - 1, t); n   if (t % 2 == 0) ans += solve(l / 2 - 1, t / 2); n   if (n != l && t % 2 == 0) ans += solve(n - l, t / 2); n   return d[make_pair(n, t)] = ans; n } n int main() { n   long long n, t; n   cin >> n >> t; n   long long ans = solve(n + 1, t); n   if (t == 1) ans--; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 710; n const int MOD = 1000000007; n int n; n int mate[MAXN]; n long long int dp[MAXN][MAXN][3][3]; n char v[MAXN]; n stack<int, vector<int> > s; n long long int solve(int l, int r, int bl, int br) { n   long long int& ans = dp[l][r][bl][br]; n   if (ans != -1) return ans; n   if (l > r) return ans = 1; n   ans = 0; n   int mateL = mate[l]; n   for (int colorL = 1; colorL <= 2; colorL++) { n     if (colorL == bl) continue; n     long long int left = solve(l + 1, mateL - 1, colorL, 0); n     long long int right = solve(mateL + 1, r, 0, br); n     ans += left * right; n   } n   for (int colorL = 1; colorL <= 2; colorL++) { n     if (mateL == r && colorL == br) continue; n     long long int left = solve(l + 1, mateL - 1, 0, colorL); n     long long int right = solve(mateL + 1, r, colorL, br); n     ans += left * right; n   } n   ans %= MOD; n   return ans; n } n int main() { n   scanf(  %s , v); n   n = strlen(v); n   for (int i = 0; i < n; i++) { n     if (v[i] ==  ( ) n       s.push(i + 1); n     else { n       int p = s.top(); n       s.pop(); n       mate[p] = i + 1; n       mate[i + 1] = p; n     } n   } n   memset(dp, -1, sizeof(dp)); n   printf( %lld n , solve(1, n, 0, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, z, cnt, c[100100], d[100100]; n priority_queue<pair<int, int>, vector<pair<int, int> >, n                greater<pair<int, int> > > n     pq; n vector<pair<int, pair<int, int> > > graph[100100]; n vector<pair<int, pair<int, int> > > edge, v; n bool mark[100100]; n pair<int, int> p[100100]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) d[i] = 1e9; n   for (int i = 0; i < m; i++) { n     cin >> x >> y >> z; n     graph[x].push_back(make_pair(y, make_pair(z, i))); n     graph[y].push_back(make_pair(x, make_pair(z, i))); n     edge.push_back(make_pair(z, make_pair(x, y))); n     if (z) cnt++; n   } n   d[1] = 0; n   pq.push(make_pair(0, 1)); n   while (!pq.empty()) { n     x = pq.top().second; n     pq.pop(); n     for (int i = 0; i < graph[x].size(); i++) { n       y = graph[x][i].first; n       if (d[x] + 1 <= d[y]) { n         if (d[x] + 1 < d[y]) p[y] = make_pair(x, graph[x][i].second.second); n         d[y] = d[x] + 1; n         if (c[x] + graph[x][i].second.first > c[y]) { n           p[y] = make_pair(x, graph[x][i].second.second); n           c[y] = c[x] + graph[x][i].second.first; n         } n         pq.push(make_pair(d[y], y)); n       } n     } n   } n   x = n; n   while (x != 1) { n     y = x; n     x = p[y].first; n     if (edge[p[y].second].first == 0) v.push_back(edge[p[y].second]); n     mark[p[y].second] = 1; n   } n   for (int i = 0; i < edge.size(); i++) { n     if (!mark[i] && edge[i].first) v.push_back(edge[i]); n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) { n     cout << v[i].second.first <<     << v[i].second.second <<     n          << (!v[i].first) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, ans = 128, x[128], sum[128]; n int main() { n   cin >> n >> d; n   for (int i = 0; i < n; ++i) cin >> x[i]; n   sort(x, x + n); n   for (int l = 0; l < n; ++l) n     for (int r = l; r < n; ++r) n       if (x[r] - x[l] <= d) ans = min(ans, l + n - 1 - r); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int mod = 1e9 + 7, oo = 1e9; n const ll loo = 1e18; n ll modpow(ll a, ll b) { n   ll ans = 1; n   for (; b; b /= 2, a = a * a % mod) n     if (b & 1) ans = (ans * a) % mod; n   return ans; n } n ll gcd(ll a, ll b) { n   while (a) b %= a, swap(a, b); n   return b; n } n const bool DEBUG = 1; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   int n = s.length(); n   int pre[n][3]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) pre[i][j] = i ? pre[i - 1][j] : -oo; n     pre[i][s[i] -  a ] = i; n   } n   stringstream ss; n   int i = 0, j = n - 1; n   int cnt = 0; n   while (i <= j) { n     int jj = pre[j][s[i] -  a ]; n     if (jj - i >= 0 && (jj - i - 1) / 2 + cnt + (1 + (i != jj)) >= n / 2) { n       j = jj - 1; n       cnt += 1 + (i != jj); n       ss << s[i]; n     } n     i++; n   } n   string t = ss.str(); n   string r; n   if (cnt & 1) n     r = t.substr(0, t.length() - 1); n   else n     r = s.substr(0, t.length()); n   reverse(r.begin(), r.end()); n   cout << t << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n int n, a, b, da, db; n vector<int> nei[N + 1]; n int dep[N + 1], dp[N + 1]; n int ans; n void dfs(int x = a, int fa = 0) { n   vector<int> v; n   v.push_back(dep[x]); n   dp[x] = dep[x]; n   for (int i = 0; i < nei[x].size(); i++) { n     int y = nei[x][i]; n     if (y == fa) continue; n     dep[y] = dep[x] + 1; n     dfs(y, x); n     v.push_back(dp[y]); n     dp[x] = max(dp[x], dp[y]); n   } n   sort(v.begin(), v.end(), greater<int>()); n   if (v.size() >= 2) ans = max(ans, v[0] + v[1] - 2 * dep[x]); n } n void mian() { n   scanf( %d%d%d%d%d , &n, &a, &b, &da, &db); n   for (int i = 1; i <= n; i++) nei[i].clear(); n   for (int i = 1; i <= n; i++) dep[i] = dp[i] = 0; n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     nei[x].push_back(y); n     nei[y].push_back(x); n   } n   if (db < 2 * da + 1) return puts( Alice ), void(); n   ans = 0; n   dfs(); n   if (ans >= 2 * da + 1 && dep[b] > da) n     puts( Bob ); n   else n     puts( Alice ); n } n int main() { n   int testnum; n   cin >> testnum; n   while (testnum--) mian(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using minpq = priority_queue<T, vector<T>, greater<T>>; n struct pt { n   long long x, y; n   pt(long long x = 0, long long y = 0) : x(x), y(y) {} n   pt operator-(const pt &o) const { return pt(x - o.x, y - o.y); } n   long long cross(const pt &o) const { return x * o.y - y * o.x; } n }; n struct city { n   pt p; n   bool b; n   int id; n }; n struct tri { n   city a, b, c; n   bool ins(const pt &p) { n     return (b.p - a.p).cross(p - a.p) > 0 && (c.p - b.p).cross(p - b.p) > 0 && n            (a.p - c.p).cross(p - c.p) > 0; n   } n }; n const int N = 1e3 + 5; n int n; n vector<city> v; n bool used[N]; n vector<city> upperhull(const vector<city> &v, int n) { n   vector<city> u; n   for (int m, i = 0; i < n; i++) { n     while ((m = u.size()) > 1) { n       pt p = u[m - 1].p, q = u[m - 2].p; n       if ((p - q).cross(v[i].p - p) > 0) break; n       u.pop_back(); n     } n     u.push_back(v[i]); n   } n   return u; n } n vector<city> hull(vector<city> &v, int n) { n   vector<city> u = upperhull(v, n); n   reverse(v.begin(), v.end()); n   vector<city> l = upperhull(v, n); n   reverse(v.begin(), v.end()); n   for (int i = 1; i + 1 < l.size(); i++) u.push_back(l[i]); n   return u; n } n vector<pair<int, int>> ans; n void solve(tri T, vector<city> v) { n   v.erase(remove_if((v).begin(), (v).end(), n                     [&](city c) { return used[c.id] || !T.ins(c.p); }), n           v.end()); n   int cnt[2] = {0, 0}; n   for (city c : {T.a, T.b, T.c}) { n     cnt[c.b]++; n   } n   bool b = (cnt[0] > cnt[1]); n   for (city &c : v) { n     if (c.b == b) { n       for (city d : {T.a, T.b, T.c}) { n         if (c.b == d.b) { n           ans.emplace_back(c.id, d.id); n           break; n         } n       } n       used[c.id] = true; n       solve({T.a, T.b, c}, v); n       solve({T.b, T.c, c}, v); n       solve({T.c, T.a, c}, v); n       return; n     } n   } n   city c1 = (T.a.b != b ? T.a : T.b.b != b ? T.b : T.c); n   for (city &c : v) { n     ans.emplace_back(c.id, c1.id); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = (0); i < (n); i++) { n     pt p; n     bool b; n     cin >> p.x >> p.y >> b; n     v.push_back({p, b, i}); n   } n   if (n == 1 || (n == 2 && v[0].b != v[1].b)) { n     cout <<  0 n ; n     return 0; n   } else if (n == 2) { n     cout <<  1 n0 1 n ; n     return 0; n   } n   sort((v).begin(), (v).end(), [&](city &a, city &b) { n     return make_pair(a.p.x, a.p.y) < make_pair(b.p.x, b.p.y); n   }); n   vector<city> h = hull(v, n); n   vector<int> ve; n   for (int i = (0); i < (((int)(h).size())); i++) { n     if (h[i].b != h[(i + 1) % ((int)(h).size())].b) { n       ve.push_back(i); n     } else { n       ans.emplace_back(h[i].id, h[(i + 1) % ((int)(h).size())].id); n     } n   } n   if (((int)(ve).size()) > 2) { n     cout <<  Impossible n ; n     return 0; n   } n   vector<tri> tris; n   for (city &c : h) { n     used[c.id] = true; n   } n   if (ve.empty()) { n     ans.pop_back(); n     bool flag = false; n     for (city &c : v) { n       if (!used[c.id] && c.b != h[0].b) { n         used[c.id] = true; n         for (int i = (0); i < (((int)(h).size())); i++) { n           tris.push_back({h[i], h[(i + 1) % ((int)(h).size())], c}); n         } n         flag = true; n         break; n       } n     } n     if (!flag) { n       cout << n - 1 <<   n ; n       for (int i = (1); i < (n); i++) { n         cout << 0 <<     << i <<   n ; n       } n       return 0; n     } n   } else { n     assert(((int)(ve).size()) == 2); n     for (int i = (ve[0] + 1) % ((int)(h).size()); i != ve[1]; n          i = (i + 1) % ((int)(h).size())) { n       tris.push_back({h[ve[0]], h[i], h[(i + 1) % ((int)(h).size())]}); n     } n     for (int i = (ve[1] + 1) % ((int)(h).size()); i != ve[0]; n          i = (i + 1) % ((int)(h).size())) { n       tris.push_back({h[ve[1]], h[i], h[(i + 1) % ((int)(h).size())]}); n     } n   } n   for (tri &T : tris) { n     solve(T, v); n   } n   cout << ((int)(ans).size()) <<   n ; n   for (pair<int, int> &pa : ans) { n     cout << pa.first <<     << pa.second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long power(long long a, long long n) { n   long long ret = 1; n   while (n) { n     if (n & 1) { n       ret = ret * a % mod; n     } n     a = a * a % mod; n     n >>= 1; n   } n   return ret; n } n vector<long long> st; n long long C(long long n, long long k) { n   long long ret = st[n]; n   ret = ret * power(st[k], mod - 2) % mod; n   ret = ret * power(st[n - k], mod - 2) % mod; n   return ret; n } n long long solve(long long n, long long k) { n   if (k >= n) return 0; n   st.resize(n + 1, 1); n   for (long long i = 1; i <= n; i++) st[i] = st[i - 1] * i % mod; n   long long ret = 0, c = n - k; n   for (long long i = 0; i <= c; i++) { n     if ((i & 1) == (c & 1)) n       ret = (ret + mod + (C(c, i) * power(i, n))) % mod; n     else n       ret = (ret + mod - (C(c, i) * power(i, n))) % mod; n   } n   ret = ret * C(n, c) % mod; n   return k > 0 ? ret * 2 % mod : ret; n } n int32_t main() { n   long long n, k; n   cin >> n >> k; n   cout << solve(n, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long MAX = 8e18; n const long long MIN = -8e18; n int n; n long long x[N]; n long long y[N]; n long long z[N]; n vector<long long> ansX; n vector<long long> ansY; n vector<long long> ansZ; n inline long long llabs(const long long &a, const long long &b) { n   long long res = a - b; n   return (res >= 0 ? res : -res); n } n inline long long calDist(long long z0, pair<long long, long long> &xy) { n   long long minA, maxA, minB, maxB, h, dist2, a, b; n   int i, j, k; n   minA = minB = MAX; n   maxA = maxB = MIN; n   for (i = 0; i < n; i++) { n     h = llabs(z0, z[i]); n     minA = min(minA, x[i] + y[i] - h); n     maxA = max(maxA, x[i] + y[i] + h); n     minB = min(minB, x[i] - y[i] - h); n     maxB = max(maxB, x[i] - y[i] + h); n   } n   dist2 = max(maxA - minA, maxB - minB); n   a = (maxA + minA) / 2; n   b = (maxB + minB) / 2; n   xy.first = (a + b) / 2; n   xy.second = (a - b) / 2; n   return dist2; n } n inline void solve() { n   int i; n   long long bestZ, bestY, bestX, bestDist, curDist, curY, curX, initX, initY, n       curZ, d; n   bestDist = MAX; n   pair<long long, long long> xy; n   bestZ = 0; n   bestDist = calDist(bestZ, xy); n   d = 1e18; n   while (d >= 1) { n     curZ = bestZ + d; n     curDist = calDist(curZ, xy); n     if (curDist < bestDist) { n       bestZ = curZ; n       bestDist = curDist; n       continue; n     } n     curZ = bestZ - d; n     curDist = calDist(curZ, xy); n     if (curDist < bestDist) { n       bestZ = curZ; n       bestDist = curDist; n       continue; n     } n     d /= 2; n   } n   calDist(bestZ, xy); n   initX = xy.first; n   initY = xy.second; n   bestDist = MAX; n   for (curX = initX - 1; curX <= initX + 1; curX++) { n     for (curY = initY - 1; curY <= initY + 1; curY++) { n       curDist = 0; n       for (i = 0; i < n; i++) { n         curDist = max(curDist, llabs(x[i] - curX) + llabs(y[i] - curY) + n                                    llabs(bestZ - z[i])); n       } n       if (curDist < bestDist) { n         bestDist = curDist; n         bestX = curX; n         bestY = curY; n       } n     } n   } n   ansX.push_back(bestX); n   ansY.push_back(bestY); n   ansZ.push_back(bestZ); n } n int main() { n   int t, i, k; n   scanf( %d , &t); n   for (k = 0; k < t; k++) { n     scanf( %d , &n); n     for (i = 0; i < n; i++) { n       scanf( n            %l  n            ld  n            %l  n            ld  n            %l  n            ld , n           &x[i], &y[i], &z[i]); n     } n     solve(); n   } n   for (i = 0; i < t; i++) { n     printf( n          %l  n          ld  n             n          %l  n          ld  n             n          %l  n          ld  n           n , n         ansX[i], ansY[i], ansZ[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = 1e16; n const long long ninf = LLONG_MIN; n const double eps = 1e-12; n const long long N = 1000005; n const double PI = 3.14159265358979323846; n const long long LOGN = 19; n long long n, m; n long long s[N], t[N]; n char a[N]; n long long dp[(1 << 21)]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> s[i]; n   cin >> m; n   sort(s, s + n); n   reverse(s, s + n); n   for (int i = 0; i < m; i++) cin >> a[i] >> t[i]; n   for (long long mask = (1 << m) - 1; mask >= 0; mask--) { n     long long done = __builtin_popcount(mask); n     if (done == m) { n       dp[mask] = 0; n       continue; n     } n     if (a[done] ==  p ) { n       for (int j = 0; j < m; j++) { n         if ((mask & (1 << j)) == 0) { n           dp[mask] = (t[done] == 1 ? s[j] : -s[j]) + dp[mask | (1 << j)]; n           break; n         } n       } n     } else { n       if (t[done] == 1) { n         dp[mask] = -inf; n         for (int j = 0; j < m; j++) { n           dp[mask] = max(dp[mask], dp[mask | (1 << j)]); n         } n       } else { n         dp[mask] = inf; n         for (int j = 0; j < m; j++) { n           dp[mask] = min(dp[mask], dp[mask | (1 << j)]); n         } n       } n     } n   } n   cout << dp[0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5, M = 350; n long long dp[5][N], B, a[N], b[N], t[N], n, m, d, minans = 1e9 + 10, dpL[N], n                                                   dpR[N]; n int last, ldel; n deque<int> stac; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> d; n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i] >> t[i]; n     B += b[i]; n   } n   for (int j = 0; j < n; j++) dp[0][j] = abs(a[0] - (j + 1)); n   for (int i = 1; i < m; i++) { n     last = (i - 1) % 2; n     for (int j = 0; j < n; j++) { n       ldel = j; n       while (!stac.empty() && stac.front() < (j - ((t[i] - t[i - 1]) * d))) n         stac.pop_front(); n       while (!stac.empty() && dp[last][stac.back()] >= dp[last][j]) n         ldel = stac.back(), stac.pop_back(); n       if (stac.empty()) n         dpL[j] = dp[last][ldel]; n       else n         dpL[j] = dp[last][stac.front()]; n       stac.push_back(j); n     } n     for (int j = n - 1; j >= 0; j--) { n       ldel = j; n       while (!stac.empty() && stac.front() > (j + ((t[i] - t[i - 1]) * d))) n         stac.pop_front(); n       while (!stac.empty() && dp[last][stac.back()] >= dp[last][j]) n         ldel = stac.back(), stac.pop_back(); n       if (stac.empty()) n         dpR[j] = dp[last][ldel]; n       else n         dpR[j] = dp[last][stac.front()]; n       stac.push_back(j); n     } n     for (int j = 0; j < n; j++) n       dp[i % 2][j] = n           min(min(dpR[j], dpL[j]), dp[last][j]) + abs(a[i] - (j + 1)); n     stac.clear(); n   } n   for (int i = 0; i < n; i++) minans = min(minans, dp[(m - 1) % 2][i]); n   cout << B - minans; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long st, en; n   node() {} n   node(long long _st, long long _en) : st(_st), en(_en) {} n }; n int main() { n   int n, b, t, d; n   long long etime[200000]; n   long long ftime; n   int qlen = 0; n   cin >> n >> b; n   cin >> t >> d; n   etime[0] = t + d; n   ftime = 1ll * t + d; n   int len = 0; n   queue<node> ws; n   node temp; n   cout << etime[0] <<    ; n   for (int i = 1; i < n; i++) { n     cin >> t >> d; n     while (!ws.empty()) { n       temp = ws.front(); n       if (t >= temp.st) { n         ws.pop(); n         len--; n       } else n         break; n     } n     if (t < ftime) { n       if (len < b) { n         ws.push(node(ftime, ftime + d)); n         len++; n         etime[i] = ftime + d; n         ftime += d; n       } else { n         etime[i] = -1; n       } n     } else { n       etime[i] = 1ll * t + d; n       ftime = 1ll * t + d; n     } n     cout << etime[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<long long, long long>, pair<long long, long long> > Mp; n pair<long long, long long> DFS(pair<long long, long long> u) { n   if (u.second < 10) n     return pair<long long, long long>(u.first || u.second, n                                       u.second - max(u.second, u.first)); n   if (Mp[u].first) return Mp[u]; n   long long ans = 0, p = 1; n   while (p <= u.second / 10) p *= 10; n   pair<long long, long long> res = n       DFS(pair<long long, long long>(max(u.first, u.second / p), u.second % p)); n   ans += res.first; n   res = DFS(pair<long long, long long>(u.first, u.second / p * p + res.second)); n   return Mp[u] = pair<long long, long long>(res.first + ans, res.second); n } n int main() { n   long long n; n   scanf( %lld , &n); n   printf( %lld n , DFS(pair<long long, long long>(0, n))); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n const int LOG = 18; n int n, m, par[MAX][LOG], h[MAX]; n vector<int> graph[MAX]; n int par_tree[MAX], mini[MAX]; n void lca_dfs(int node, int p) { n   if (p + 1) h[node] = h[p] + 1; n   par[node][0] = p; n   for (int k = 1; k < LOG; k++) n     if (par[node][k - 1] + 1) par[node][k] = par[par[node][k - 1]][k - 1]; n   for (auto& target : (graph[node])) { n     if (target != p) lca_dfs(target, node); n   } n } n int get_lca(int a, int b) { n   if (h[b] > h[a]) swap(a, b); n   for (int k = LOG - 1; k >= 0; k--) { n     if (par[a][k] + 1 && h[par[a][k]] >= h[b]) a = par[a][k]; n   } n   if (a == b) return a; n   for (int k = LOG - 1; k >= 0; k--) n     if (par[a][k] != par[b][k]) { n       a = par[a][k]; n       b = par[b][k]; n     } n   return par[a][0]; n } n int dist(int a, int b) { return h[a] + h[b] - 2 * h[get_lca(a, b)]; } n int processed[MAX], sizes[MAX]; n int get_sizes(int node, int p) { n   sizes[node] = 1; n   for (auto& target : (graph[node])) { n     if (!processed[target] && target != p) n       sizes[node] += get_sizes(target, node); n   } n   return sizes[node]; n } n int cent, cent_cost; n void select_cent(int node, int p, int current) { n   int node_max = sizes[current] - sizes[node]; n   for (auto& target : (graph[node])) { n     if (!processed[target] && target != p) { n       node_max = max(node_max, sizes[target]); n       select_cent(target, node, current); n     } n   } n   if (node_max < cent_cost) { n     cent = node; n     cent_cost = node_max; n   } n } n int process(int current) { n   get_sizes(current, -1); n   if (sizes[current] <= 1) return current; n   cent = current; n   cent_cost = ((int)1e9 + 10); n   select_cent(current, -1, current); n   int centroid = cent; n   processed[centroid] = 1; n   for (auto& target : (graph[centroid])) { n     if (!processed[target]) { n       int next_cent = process(target); n       par_tree[next_cent] = centroid; n     } n   } n   return centroid; n } n void update(int node) { n   int cur = node; n   while (cur > 0) { n     mini[cur] = min(mini[cur], dist(cur, node)); n     cur = par_tree[cur]; n   } n } n int query(int node) { n   int cur = node, ans = ((int)1e9 + 10); n   while (cur > 0) { n     ans = min(ans, mini[cur] + dist(node, cur)); n     cur = par_tree[cur]; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   for (int(i) = (0); (i) < (MAX); (i)++) mini[i] = ((int)1e9 + 10); n   memset(par, -1, sizeof(par)); n   cin >> n >> m; n   int a, b; n   for (int(i) = (0); (i) < (n - 1); (i)++) { n     cin >> a >> b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   lca_dfs(1, -1); n   int root = process(1); n   par_tree[root] = -1; n   int type, node; n   update(1); n   for (int(k) = (0); (k) < (m); (k)++) { n     cin >> type >> node; n     if (type == 1) { n       update(node); n     } else { n       cout << (query(node)) <<   n ; n       ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n vector<int> g[300005]; n bool vis[300005]; n int child[300005]; n int mostchild[300005], par[300005]; n int dfs(int u) { n   vis[u] = true; n   child[u]++; n   for (int i = 0; i < g[u].size(); i++) { n     if (!vis[g[u][i]]) { n       vis[g[u][i]] = true; n       int x = dfs(g[u][i]); n       child[u] += x; n       if (child[mostchild[u]] < x) mostchild[u] = g[u][i]; n     } n   } n   return child[u]; n } n int res[300005]; n void traverse(int u) { n   if (u == 0) return; n   vis[u] = true; n   if (child[u] == 1) { n     res[u] = u; n     return; n   } n   traverse(mostchild[u]); n   if (2 * child[mostchild[u]] > child[u]) { n     int m = res[mostchild[u]]; n     while (2 * (child[u] - child[m]) > child[u]) m = par[m]; n     res[u] = m; n   } else n     res[u] = u; n } n int main() { n   int n, q; n   cin >> n >> q; n   par[1] = 1; n   for (int i = 2; i < n + 1; i++) { n     scanf( %d , &par[i]); n     g[par[i]].push_back(i); n   } n   memset(vis, false, sizeof(vis)); n   memset(mostchild, 0, sizeof(mostchild)); n   child[0] = 0; n   dfs(1); n   memset(vis, false, sizeof(vis)); n   for (int i = 1; i < n + 1; i++) { n     if (!vis[i]) traverse(i); n   } n   for (int i = 0; i < q; i++) { n     int a; n     scanf( %d , &a); n     printf( %d n , res[a]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int x, n, m; n int main() { n   cin >> t; n   while (t--) { n     cin >> x >> n >> m; n     while (x > 20 && n) { n       x = x / 2 + 10; n       n--; n     } n     while (m) x -= 10, m--; n     if (x > 0) n       printf( NO n ); n     else n       printf( YES n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, s; n   cin >> a >> s; n   sort(s.begin(), s.end()); n   int ind = s.length() - 1, n = a.length(); n   for (int i = 0; i < n; i++) { n     if (ind >= 0 && s[ind] > a[i]) { n       a[i] = s[ind]; n       ind--; n     } n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using vi = vector<int>; n using vl = vector<ll>; n using pii = pair<int, int>; n using vpii = vector<pair<int, int>>; n template <typename T> n using pque = priority_queue<T, vector<T>, greater<T>>; n constexpr int N = 212345, M = 16; n class Node { n  public: n   int a = 0; n   Node *c[2] = {NULL, NULL}; n   Node *next(int a) { n     if (c[a] == NULL) c[a] = new Node(); n     return c[a]; n   } n } *head = new Node(); n vpii g[N]; n vi v[M][M]; n int p[N] = {}; n template <class T> n void dfs(int a, int b, T func) { n   if (a > b) { n     func(); n     return; n   }; n   for (int i = (0); i < ((a)); i++) { n     for (int j : v[a][i]) p[j]++; n     dfs(a + 1, b, func); n     for (int j : v[a][i]) p[j]--; n   } n } n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = (0); i < ((m)); i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     g[a].emplace_back(c, b); n   } n   for (int i = (1); i < (n + 1); i++) { n     sort((g[i]).begin(), (g[i]).end()); n     for (int j = (0); j < (((int)g[i].size())); j++) { n       v[g[i].size()][j].emplace_back(g[i][j].second); n     } n   } n   for (int i = (1); i < (k); i++) n     for (int j = (0); j < ((i)); j++) { n       ; n       for (int h : v[i][j]) n         ; n       ; n     } n   dfs(1, min(6, k), [&]() { n     for (int i = (1); i < (n + 1); i++) n       ; n     ; n     Node *node = head; n     for (int i = (1); i < (n + 1); i++) { n       if (p[i] >= 2) return; n       node = node->next(p[i]); n     } n     node->a++; n   }); n   ; n   int ans = 0; n   dfs(min(7, k), k <= 6 ? 0 : k, [&]() { n     Node *node = head; n     for (int i = (1); i < (n + 1); i++) { n       if (p[i] >= 2) return; n       node = node->next(1 - p[i]); n     } n     ans += node->a; n     ; n   }); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int arr[200005] = {0}; n   long long int ans[200005] = {0}; n   int x, y, n, k, q; n   cin >> n >> k >> q; n   while (n--) { n     cin >> x >> y; n     arr[x]++; n     arr[y + 1]--; n   } n   for (int i = 1; i <= 200000; i++) { n     arr[i] += arr[i - 1]; n   } n   for (int i = 1; i <= 200000; i++) { n     if (arr[i] >= k) n       ans[i] = ans[i - 1] + 1; n     else n       ans[i] = ans[i - 1]; n   } n   while (q--) { n     cin >> x >> y; n     cout << ans[y] - ans[x - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:20000007 ) n #pragma GCC optimize( unroll-loops ) n const int N = (int)1e6 + 100; n const int maxn = (int)1e3 + 100; n const int base = (int)1e9; n const int mod = (int)1e9 + 7; n const int inf = INT_MAX; n const long long ll_inf = LLONG_MAX; n const long double pi = acos((long double)-1.0); n const long double eps = 1e-8; n void yes() { exit(!(printf( YES n ))); } n void no() { exit(!(printf( NO n ))); } n void solve() { n   string s; n   cin >> s; n   int n = s.size(); n   if (n < 4) no(); n   set<char> S; n   vector<int> Cnt(26); n   for (char c : s) Cnt[c -  a ]++, S.insert(c); n   int L = S.size(); n   if (L > 4) no(); n   if (3 <= L && L <= 4) yes(); n   if (L == 1) no(); n   if (L == 2) { n     if (!any_of(Cnt.begin(), Cnt.end(), n                 [&](int Count) -> bool { return Count >= 2; })) n       no(); n     vector<int> others; n     for (int i = 0; i < 26; ++i) n       if (Cnt[i] >= 2) others.push_back(i); n     if (others.size() >= 2) n       yes(); n     else n       no(); n   } n   no(); n } n int main() { n   int T = 1; n   for (; T; --T) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cout <<  { ; n   __print(x.first); n   cout <<  , ; n   __print(x.second); n   cout <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i); n   cout <<  } ; n } n void _print() { cout <<   n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n const int mod = 1e9 + 7, mxn = 1e5 + 1; n int solve() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<long long> v(n + 1); n   for (long long i = (1); i < (n + 1); ++i) cin >> v[i]; n   vector<long long> pref(n + 1, 0); n   pref[0] = 0; n   for (long long i = (1); i < (n + 1); ++i) pref[i] = pref[i - 1] + v[i]; n   long long dp[n + 1][k + 1]; n   memset(dp, 0ll, sizeof(dp)); n   for (long long i = (1); i < (n + 1); ++i) { n     for (long long j = (1); j < (k + 1); ++j) { n       if (i - m >= 0) n         dp[i][j] = max(dp[i - 1][j], dp[i - m][j - 1] + pref[i] - pref[i - m]); n       else n         dp[i][j] = dp[i - 1][j]; n     } n   } n   cout << dp[n][k] <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n constexpr int maxn = 1e5 + 10; n vector<int> topo; n vector<vector<pii>> pilha; n int ans[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   for (auto& it : a) scanf( %d , &it); n   for (int i = 0; i < n; i++) { n     auto it = lower_bound((topo).begin(), (topo).end(), a[i]); n     if (it == topo.end()) n       topo.push_back(a[i]), pilha.push_back({{a[i], i}}); n     else { n       int pos = (int)(it - topo.begin()); n       topo[pos] = a[i]; n       pilha[pos].push_back({a[i], i}); n     } n   } n   vector<pii> valid = pilha.back(); n   if ((int)((valid).size()) > 1) n     for (auto it : valid) ans[it.second] = 2; n   else n     ans[valid[0].second] = 3; n   for (int i = (int)((pilha).size()) - 2; i >= 0; i--) { n     assert((int)((valid).size())); n     sort((valid).begin(), (valid).end()); n     vector<int> suf((int)((valid).size())), comp((int)((valid).size())); n     suf.back() = valid.back().second; n     for (int pos = (int)((valid).size()) - 2; pos >= 0; pos--) n       suf[pos] = max(suf[pos + 1], valid[pos].second); n     for (int j = 0; j < (int)((valid).size()); j++) comp[j] = valid[j].first; n     valid.clear(); n     for (int j = 0; j < (int)((pilha[i]).size()); j++) { n       auto it = upper_bound((comp).begin(), (comp).end(), pilha[i][j].first); n       if (it == comp.end()) n         ans[pilha[i][j].second] = 1; n       else { n         int pos = (int)(it - comp.begin()); n         if (suf[pos] < pilha[i][j].second) n           ans[pilha[i][j].second] = 1; n         else n           valid.push_back(pilha[i][j]); n       } n     } n     if ((int)((valid).size()) > 1) n       for (auto it : valid) ans[it.second] = 2; n     else n       ans[valid[0].second] = 3; n   } n   for (int i = 0; i < n; i++) printf( %d , ans[i]); n   printf(  n ); n } n


#include<bits/stdc++.h> n using namespace std; n  n void solve(){ n  int n; n  cin >> n; n  int a[n]; n  for(int i=0;i<n;i++){ n   cin >> a[i]; n  } n   n  vector<int> ans; n  ans.push_back(0); n   n  int current = a[0]; n  for(int i=1;i<n;i++){ n   int cans = (current|a[i])^a[i]; n   ans.push_back(cans); n   current = current|a[i]; n  } n   n  for(int i=0;i<n;i++){ n   cout << ans[i] <<    ; n  } n   n  cout << endl; n } n  n int main(){ n  int t=1; n  cin >> t; n  while(t--){ n   solve(); n  } n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int len = s.length(); n   for (int i = 0; i < len;) { n     if (s[i] !=  1 ) { n       cout <<  NO  << endl; n       return 0; n     } n     if (i + 3 <= len) { n       if (s[i + 1] ==  4  and s[i + 2] ==  4 ) { n         i += 3; n       } else if (s[i + 1] ==  4 ) { n         i += 2; n       } else n         ++i; n     } else if (i + 2 <= len) { n       if (s[i + 1] ==  4 ) { n         i += 2; n       } else n         ++i; n     } else n       ++i; n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n class graph { n  public: n   struct edge { n     int from; n     int to; n     T cost; n   }; n   vector<edge> edges; n   vector<vector<int>> g; n   int n; n   graph(int _n) : n(_n) { g.resize(n); } n   virtual int add(int from, int to, T cost) = 0; n }; n template <typename T> n class undigraph : public graph<T> { n  public: n   using graph<T>::edges; n   using graph<T>::g; n   using graph<T>::n; n   undigraph(int _n) : graph<T>(_n) {} n   int add(int from, int to, T cost = 1) { n     assert(0 <= from && from < n && 0 <= to && to < n); n     int id = (int)edges.size(); n     g[from].push_back(id); n     g[to].push_back(id); n     edges.push_back({from, to, cost}); n     return id; n   } n }; n template <typename T> n vector<vector<int>> find_cycles(const graph<T> &g, int bound_cnt = 1 << 30, n                                 int bound_size = 1 << 30) { n   vector<int> was(g.n, -1); n   vector<int> st; n   vector<vector<int>> cycles; n   int total_size = 0; n   function<void(int, int)> dfs = [&](int v, int pe) { n     if ((int)cycles.size() >= bound_cnt || total_size >= bound_size) { n       return; n     } n     was[v] = (int)st.size(); n     for (int id : g.g[v]) { n       if (id == pe) { n         continue; n       } n       auto &e = g.edges[id]; n       int to = e.from ^ e.to ^ v; n       if (was[to] >= 0) { n         vector<int> cycle(1, id); n         for (int j = was[to]; j < (int)st.size(); j++) { n           cycle.push_back(st[j]); n         } n         cycles.push_back(cycle); n         total_size += (int)cycle.size(); n         if ((int)cycles.size() >= bound_cnt || total_size >= bound_size) { n           return; n         } n         continue; n       } n       if (was[to] == -1) { n         st.push_back(id); n         dfs(to, id); n         st.pop_back(); n       } n     } n     was[v] = -2; n   }; n   for (int i = 0; i < g.n; i++) { n     if (was[i] == -1) { n       dfs(i, -1); n     } n   } n   return cycles; n } n template <typename T> n vector<int> edges_to_vertices(const graph<T> &g, n                               const vector<int> &edge_cycle) { n   int sz = (int)edge_cycle.size(); n   vector<int> vertex_cycle; n   if (sz <= 2) { n     vertex_cycle.push_back(g.edges[edge_cycle[0]].from); n     if (sz == 2) { n       vertex_cycle.push_back(g.edges[edge_cycle[0]].to); n     } n   } else { n     for (int i = 0; i < sz; i++) { n       int j = (i + 1) % sz; n       auto &e = g.edges[edge_cycle[i]]; n       auto &other = g.edges[edge_cycle[j]]; n       if (other.from == e.from || other.to == e.from) { n         vertex_cycle.push_back(e.to); n       } else { n         vertex_cycle.push_back(e.from); n       } n     } n   } n   return vertex_cycle; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   undigraph<int> g(n); n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     g.add(x, y); n   } n   vector<vector<int>> cycles = find_cycles(g); n   vector<vector<int>> events(m); n   for (auto &cycle : cycles) { n     int sz = (int)cycle.size(); n     int pos = (int)(min_element(cycle.begin(), cycle.end()) - cycle.begin()); n     rotate(cycle.begin(), cycle.begin() + pos, cycle.end()); n     pos = (int)(max_element(cycle.begin(), cycle.end()) - cycle.begin()); n     bool ok = true; n     for (int i = 0; i < pos - 1; i++) { n       if (cycle[i] > cycle[i + 1]) { n         ok = false; n         break; n       } n     } n     for (int i = pos; i < sz - 1; i++) { n       if (cycle[i] < cycle[i + 1]) { n         ok = false; n         break; n       } n     } n     if (ok) { n       events[cycle[pos]].push_back(cycle[0]); n     } n   } n   vector<int> sub(m, 0); n   vector<int> ans(n, 1); n   for (int i = m - 1; i >= 0; i--) { n     auto &e = g.edges[i]; n     int x = e.from; n     int y = e.to; n     int cur = ans[x] + ans[y] - sub[i]; n     ans[x] = ans[y] = cur; n     for (int j : events[i]) { n       sub[j] += cur; n     } n   } n   for (int i = 0; i < n; i++) { n     if (i > 0) { n       cout <<    ; n     } n     cout << ans[i] - 1; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   cout << 0 <<     << 0 <<     << n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int res = 0, c; n   while (c = getchar(), c <  0  || c >  9 ) n     ; n   do { n     res = (res << 3) + (res << 1) + (c ^ 48); n   } while (c = getchar(), c >=  0  && c <=  9 ); n   return res; n } n const int inf = 0x3f3f3f3f; n const int maxn = 100010; n const int maxd = 8000010; n int T[maxd], s[maxn], lson[maxd], rson[maxd], val[maxd], tot; n int l[maxn], r[maxn], d[maxn]; n vector<int> edge[maxn], lay[maxn]; n int N, R, last, dep, dfn; n void dfs(int x, int dep) { n   l[x] = ++dfn; n   d[x] = dep; n   ::dep = max(dep, ::dep); n   lay[dep].push_back(x); n   for (auto i : edge[x]) n     if (!l[i]) { n       dfs(i, dep + 1); n     } n   r[x] = dfn; n } n void update(int to, int L, int R, int& rt) { n   if (L == R) { n     val[rt = ++tot] = s[to]; n     return; n   } n   int mid = L + R >> 1; n   lson[++tot] = lson[rt]; n   rson[tot] = rson[rt]; n   rt = tot; n   if (l[to] <= mid) n     update(to, L, mid, lson[rt]); n   else n     update(to, mid + 1, R, rson[rt]); n   val[rt] = min(val[lson[rt]], val[rson[rt]]); n } n int query(int l, int r, int L, int R, int rt) { n   if (L >= l && R <= r) { n     return val[rt]; n   } n   int mid = L + R >> 1, res = inf; n   if (mid >= l) res = query(l, r, L, mid, lson[rt]); n   if (mid < r) res = min(res, query(l, r, mid + 1, R, rson[rt])); n   return res; n } n int main() { n   N = read(), R = read(); n   for (int i = 1; i <= N; i++) s[i] = read(); n   for (int i = 1; i < N; i++) { n     int x = read(), y = read(); n     edge[x].push_back(y); n     edge[y].push_back(x); n   } n   dfs(R, 1); n   T[0] = lson[0] = rson[0] = 0; n   val[0] = inf; n   for (int d = 1; d <= dep; d++) { n     T[d] = ++tot; n     lson[tot] = lson[T[d - 1]]; n     rson[tot] = rson[T[d - 1]]; n     for (auto i : lay[d]) update(i, 1, N, T[d]); n   } n   int Q = read(); n   while (Q--) { n     int X = (last + read()) % N + 1, K = (read() + last) % N; n     printf( %d n , last = query(l[X], r[X], 1, N, T[min(dep, d[X] + K)])); n   } n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   std::cin >> n; n   int arr[2 * n]; n   for (int i = 0; i < 2 * n; i++) std::cin >> arr[i]; n   int swc = 0; n   for (int i = 0; i < 2 * n; i += 2) { n     int pos = -1; n     for (int j = i + 1; j < 2 * n; j++) { n       if (arr[j] == arr[i]) { n         pos = j; n         break; n       } n     } n     for (int j = pos; j > i + 1; j--) { n       swc++; n       std::swap(arr[j], arr[j - 1]); n     } n   } n   std::cout << swc <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const long long inf = 1e18; n int a[maxn]; n vector<int> g[maxn]; n bool c[maxn], ok[maxn]; n int dpdown[maxn], dpup[maxn]; n int salem[maxn]; n int mxdown[maxn], mxup[maxn]; n int cnt[maxn]; n int sum[maxn]; n int n; n int dip[maxn]; n void dfsdown(int v, int p) { n   ok[v] = c[v]; n   cnt[v] = 1; n   sum[v] = c[v]; n   for (auto u : g[v]) n     if (u != p) dfsdown(u, v), cnt[v] += cnt[u], sum[v] += sum[u]; n   if (!c[v]) return; n   for (auto u : g[v]) n     if (u != p) n       if (ok[u]) n         salem[v] += cnt[u]; n       else { n         ok[v] = 0; n         mxdown[v] = max(mxdown[v], dpdown[u]); n       } n   dpdown[v] = mxdown[v] + salem[v] + 1; n } n void dfsup(int v, int p) { n   int _mx = -1; n   for (auto u : g[v]) n     if (u != p && c[u] && c[v]) n       mxup[u] = max(mxup[u], _mx), _mx = max(_mx, (1 - ok[u]) * dpdown[u]); n   reverse(g[v].begin(), g[v].end()); n   _mx = -1; n   for (auto u : g[v]) n     if (u != p && c[u] && c[v]) n       mxup[u] = max(mxup[u], _mx), _mx = max(_mx, (1 - ok[u]) * dpdown[u]); n   for (auto u : g[v]) n     if (u != p) n       if (c[u]) n         if (c[v]) n           if (sum[1] - sum[v] == n - cnt[v]) n             dpup[u] = 1 + salem[v] - (ok[u] ? cnt[u] : 0) + mxup[u] + dpup[v]; n           else n             dpup[u] = n                 1 + salem[v] - (ok[u] ? cnt[u] : 0) + max(mxup[u], dpup[v]); n   for (auto u : g[v]) n     if (u != p) n       if (c[u]) n         if (c[v]) n           if (sum[1] - sum[u] == n - cnt[u]) n             dip[u] = 1 + salem[u] + dpup[u] + mxdown[u]; n           else n             dip[u] = 1 + salem[u] + max(dpup[u], mxdown[u]); n   for (auto u : g[v]) n     if (u != p) dfsup(u, v); n } n int check(int mid) { n   for (int i = 1; i <= n; ++i) n     dip[i] = salem[i] = ok[i] = cnt[i] = dpdown[i] = dpup[i] = mxup[i] = n         mxdown[i] = 0; n   for (int i = 1; i <= n; ++i) c[i] = (a[i] >= mid); n   dfsdown(1, 0); n   dfsup(1, 0); n   int mx = 0; n   for (int i = 1; i <= n; ++i) n     mx = max(max(mx, dip[i]), max(dpdown[i], dpup[i])); n   return mx; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int k; n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   int l = 1, r = 1e6 + 85; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (check(mid) >= k) n       l = mid; n     else n       r = mid; n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int PI = acos(-1); n const int MAXN = 1e6; n unsigned long long comb(unsigned long long n, unsigned long long k) { n   if (k > n) return 0; n   unsigned long long u = 1; n   for (unsigned long long i = 1; i <= k; i++) { n     u *= n--; n     u /= i; n   } n   return u; n } n int main() { n   unsigned long long n, m, t; n   while (cin >> n >> m >> t) { n     unsigned long long ans = 0; n     for (unsigned long long boys = 4; boys <= n; boys++) { n       unsigned long long girls = t - boys; n       if (girls > m) continue; n       if (t < 1 + boys) break; n       ans += comb(n, boys) * comb(m, girls); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v, cnt, ans; n int t[1100000], nxt[1100000], to[1100000]; n int a[1100000], b[1100000], c[1100000], f[1100000]; n int vis[1100000]; n void add(int u, int v) { nxt[cnt] = t[u], t[u] = cnt, to[cnt++] = v; } n void dfs(int x) { n   vis[x] = true; n   for (int e = t[x]; ~e; e = nxt[e]) { n     int y = to[e]; n     to[e] = to[e ^ 1] = 0; n     if (y) { n       if (!vis[y]) dfs(y); n       if (f[y]) n         a[++ans] = x, b[ans] = y, c[ans] = f[y], f[y] = 0; n       else if (f[x]) n         a[++ans] = f[x], b[ans] = x, c[ans] = y, f[x] = 0; n       else n         f[x] = y; n     } n   } n } n int main() { n   memset(t, 0xff, sizeof(t)); n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) cin >> u >> v, add(u, v), add(v, u); n   vis[0] = true; n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) dfs(i); n   printf( %d n , ans); n   for (int i = 1; i <= ans; ++i) printf( %d %d %d n , a[i], b[i], c[i]); n   return 0; n } n


#include <bits/stdc++.h> n const long double pi = acos(-1); n const long long mod = 1e9 + 7, N = 1e5 + 5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long q; n   cin >> q; n   while (q--) { n     long long a, b; n     cin >> a >> b; n     if (a > b) { n       if ((a % 2 == 0 && b % 2 == 0) || (a % 2 != 0 && b % 2 != 0)) n         cout << 1 << endl; n       else n         cout << 2 << endl; n     } else if (a < b) { n       if ((a % 2 == 0 && b % 2 == 0) || (a % 2 != 0 && b % 2 != 0)) n         cout << 2 << endl; n       else n         cout << 1 << endl; n     } else n       cout << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int val[300000]; n vector<pair<int, int> > ed[300000]; n int fa[300000][20], maxt[300000][20], dfn[300000], dep[300000]; n void dfs_bas(int u, int f) { n   dep[u] = dep[f] + 1, dfn[u] = ++dfn[0]; n   fa[u][0] = f; n   for (int i = 1; i < 20; i++) n     fa[u][i] = fa[fa[u][i - 1]][i - 1], n     maxt[u][i] = max(maxt[u][i - 1], maxt[fa[u][i - 1]][i - 1]); n   for (int i = 0; i < ed[u].size(); i++) { n     int v = ed[u][i].first; n     if (v == f) continue; n     maxt[v][0] = ed[u][i].second; n     dfs_bas(v, u); n   } n } n int LCA(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 19; i >= 0; i--) n     if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; n   if (x == y) return x; n   for (int i = 19; i >= 0; i--) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n int query_anc(int x, int d) { n   int ans = 0; n   for (int i = 19; i >= 0; i--) n     if (d & (1 << i)) { n       ans = max(ans, maxt[x][i]); n       x = fa[x][i]; n     } n   return ans; n } n int query_path(int x, int y) { n   int l = LCA(x, y); n   return max(query_anc(x, dep[x] - dep[l]), query_anc(y, dep[y] - dep[l])); n } n struct Tree { n   int n; n   vector<int> ed[500000]; n   int val[500000], calc[500000], arg[500000]; n   int fa[500000][20], dep[500000], dfn[500000], sz[500000], ev[500000]; n   void dfs_bas(int u, int f) { n     fa[u][0] = f, dfn[u] = ++dfn[0], sz[u] = 1, dep[u] = dep[f] + 1; n     for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; n     for (int i = 0; i < ed[u].size(); i++) { n       int v = ed[u][i]; n       if (v == f) continue; n       dfs_bas(v, u); n       sz[u] += sz[v]; n     } n   } n   int LCA(int x, int y) { n     if (dep[x] < dep[y]) swap(x, y); n     for (int i = 19; i >= 0; i--) n       if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; n     if (x == y) return x; n     for (int i = 19; i >= 0; i--) n       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n     return fa[x][0]; n   } n } Y; n struct BCJ { n   int fa[500000]; n   void init(int n) { n     for (int i = 1; i <= n; i++) fa[i] = i; n   } n   int fnd(int x) { return x == fa[x] ? x : fa[x] = fnd(fa[x]); } n   void conn(int x, int y) { fa[fnd(x)] = fnd(y); } n } B; n struct e { n   int a, b, c, t; n } ined[300000]; n bool ccmp(e A, e B) { return A.c < B.c; } n bool tcmp(e A, e B) { return A.t < B.t; } n bool ydfncmp(int x, int y) { return Y.dfn[x] < Y.dfn[y]; } n int main() { n   int n = 0, q = 0; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   for (int i = 1; i < n; i++) n     scanf( %d%d%d%d , &ined[i].a, &ined[i].b, &ined[i].c, &ined[i].t), n         ed[ined[i].a].push_back(make_pair(ined[i].b, ined[i].t)), n         ed[ined[i].b].push_back(make_pair(ined[i].a, ined[i].t)); n   dfs_bas(1, 0); n   int c = n; n   B.init(2 * n - 1); n   sort(ined + 1, ined + n, ccmp); n   for (int i = 1; i <= n; i++) Y.val[i] = val[i], Y.arg[i] = i; n   for (int i = n - 1; i >= 1; i--) { n     if (B.fnd(ined[i].a) != B.fnd(ined[i].b)) { n       int u = B.fnd(ined[i].a), v = B.fnd(ined[i].b); n       c++; n       Y.ed[c].push_back(u), Y.ed[c].push_back(v); n       B.conn(u, c), B.conn(v, c); n       Y.val[c] = max(Y.val[u], Y.val[v]); n       Y.ev[c] = ined[i].c; n       if (Y.val[c] != Y.val[u]) { n         Y.calc[c] = Y.calc[v], Y.arg[c] = Y.arg[v]; n       } else if (Y.val[c] != Y.val[v]) { n         Y.calc[c] = Y.calc[u], Y.arg[c] = Y.arg[u]; n       } else { n         Y.calc[c] = n             max(max(Y.calc[u], Y.calc[v]), query_path(Y.arg[u], Y.arg[v])); n         Y.arg[c] = Y.arg[u]; n       } n     } n   } n   Y.dfs_bas(2 * n - 1, 0); n   while (q--) { n     int x = 0, v = 0; n     scanf( %d%d , &v, &x); n     int u = x; n     for (int i = 19; i >= 0; i--) n       if (Y.fa[u][i] && Y.ev[Y.fa[u][i]] >= v) u = Y.fa[u][i]; n     printf( %d %d n , Y.val[u], max(Y.calc[u], query_path(x, Y.arg[u]))); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxint = 999999999; n string s; n int cnt[30]; n int k; n int main() { n   int i; n   while (cin >> s) { n     cin >> k; n     int l = s.length(); n     if (l <= k) { n       printf( 0 n ); n       printf(  n ); n       continue; n     } n     for (i = 0; i < l; i++) { n       cnt[s[i] -  a ]++; n     } n     int Min, u; n     while (1) { n       if (k == 0) break; n       Min = maxint; n       u = -1; n       for (i = 0; i < 26; i++) { n         if (cnt[i] > 0 && cnt[i] < Min) { n           Min = cnt[i]; n           u = i; n         } n       } n       if (Min > k) { n         k = 0; n         cnt[u] -= k; n       } else { n         cnt[u] = 0; n         k -= Min; n       } n     } n     int ans = 0; n     for (i = 0; i < 26; i++) { n       if (cnt[i] > 0) ans++; n     } n     printf( %d n , ans); n     for (i = 0; i < l; i++) { n       if (cnt[s[i] -  a ] > 0) printf( %c , s[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma Optimize( O3 ) n using namespace std; n int n, x, a, b, y, m, ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   vector<int> v(n); n   vector<int> a; n   for (int i = 0; i < n; i++) cin >> v[i]; n   for (int i = 0; i < n; i += 2) a.push_back(v[i]); n   for (int i = 1; i < n; i += 2) a.push_back(v[i]); n   a.insert(a.end(), a.begin(), a.end()); n   long long sumtmp = 0; n   for (int i = 0; i < (n + 1) / 2; i++) sumtmp += a[i]; n   long long sum = sumtmp; n   for (int i = (n + 1) / 2; i < a.size(); i++) { n     sumtmp += a[i] - a[i - (n + 1) / 2]; n     sum = max(sum, sumtmp); n   } n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[4]; n   for (int i = 0; i < 4; i++) cin >> a[i]; n   sort(a, a + 4); n   for (int i = 0; i < 2; i++) n     if (a[i] + a[i + 1] > a[i + 2]) return 0 * printf( TRIANGLE ); n   for (int i = 0; i < 2; i++) n     if (a[i] + a[i + 1] == a[i + 2]) return 0 * printf( SEGMENT ); n   return 0 * printf( IMPOSSIBLE ); n } n


#include <bits/stdc++.h> 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 long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = ((res % p) * (x % p)) % p; n     y = y >> 1; n     x = ((x % p) * (x % p)) % p; n   } n   return res; n } n long long raichu(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = ((res) * (x)); n     y = y >> 1; n     x = ((x) * (x)); n   } n   return res; n } n bool isprime(long long n) { n   if (n < 2) n     return false; n   else if (n == 2) n     return true; n   else if (n % 2 == 0) n     return false; n   else { n     long long z = sqrt(n); n     for (long long i = 0; i < z - 1; i++) n       if (n % (i + 2) == 0) return false; n     return true; n   } n } n const long long prime = 13313; n namespace FFT { n const long long N = 20; n const long long MAXN = (1 << N); n const double PI = acos(-1.0); n class cmplx { n  private: n   double x, y; n  n  public: n   cmplx() : x(0.0), y(0.0) {} n   cmplx(double a) : x(a), y(0.0) {} n   cmplx(double a, double b) : x(a), y(b) {} n   double get_real() { return this->x; } n   double get_img() { return this->y; } n   cmplx conj() { return cmplx(this->x, -(this->y)); } n   cmplx operator=(const cmplx& a) { n     this->x = a.x; n     this->y = a.y; n     return *this; n   } n   cmplx operator+(const cmplx& b) { n     return cmplx(this->x + b.x, this->y + b.y); n   } n   cmplx operator-(const cmplx& b) { n     return cmplx(this->x - b.x, this->y - b.y); n   } n   cmplx operator*(const double& num) { n     return cmplx(this->x * num, this->y * num); n   } n   cmplx operator/(const double& num) { n     return cmplx(this->x / num, this->y / num); n   } n   cmplx operator*(const cmplx& b) { n     return cmplx(this->x * b.x - this->y * b.y, this->y * b.x + this->x * b.y); n   } n   cmplx operator/(const cmplx& b) { n     cmplx temp(b.x, -b.y); n     cmplx n = (*this) * temp; n     return n / (b.x * b.x + b.y * b.y); n   } n }; n cmplx w[MAXN]; n cmplx f1[MAXN]; n long long rev[MAXN]; n void ReserveBits(long long k) { n   static long long rk = -1, lim; n   if (k == rk) return; n   rk = k, lim = 1 << k; n   for (long long i = 1; i <= lim; ++i) { n     long long j = rev[i - 1], t = k - 1; n     while (t >= 0 && ((j >> t) & 1)) { n       j ^= 1 << t; n       --t; n     } n     if (t >= 0) { n       j ^= 1 << t; n       --t; n     } n     rev[i] = j; n   } n } n void fft(cmplx* a, long long k) { n   ReserveBits(k); n   long long n = 1 << k; n   for (long long i = 0; i < n; ++i) n     if (rev[i] > i) swap(a[i], a[rev[i]]); n   for (long long l = 2, m = 1; l <= n; l += l, m += m) { n     if (w[m].get_real() == 0 && w[m].get_img() == 0) { n       double angle = PI / m; n       cmplx ww(cos(angle), sin(angle)); n       if (m > 1) { n         for (long long j = 0; j < m; ++j) { n           if (j & 1) n             w[m + j] = w[(m + j) / 2] * ww; n           else n             w[m + j] = w[(m + j) / 2]; n         } n       } else n         w[m] = cmplx(1, 0); n     } n     for (long long i = 0; i < n; i += l) { n       for (long long j = 0; j < m; ++j) { n         cmplx v = a[i + j], u = a[i + j + m] * w[m + j]; n         a[i + j] = v + u; n         a[i + j + m] = v - u; n       } n     } n   } n } n vector<long long> multiply(const vector<long long>& a, n                            const vector<long long>& b) { n   long long k = 1; n   while ((1 << k) < (a.size() + b.size())) ++k; n   long long n = (1 << k); n   for (long long i = 0; i < n; ++i) f1[i] = cmplx(0, 0); n   for (long long i = 0; i < a.size(); ++i) f1[i] = f1[i] + cmplx(a[i], 0); n   for (long long i = 0; i < b.size(); ++i) f1[i] = f1[i] + cmplx(0, b[i]); n   fft(f1, k); n   for (long long i = 0; i <= n / 2; ++i) { n     cmplx p = f1[i] + f1[(n - i) % n].conj(); n     cmplx _q = f1[(n - i) % n] - f1[i].conj(); n     cmplx q(_q.get_img(), _q.get_real()); n     f1[i] = (p * q) * 0.25; n     if (i > 0) f1[(n - i)] = f1[i].conj(); n   } n   for (long long i = 0; i < n; ++i) f1[i] = f1[i].conj(); n   fft(f1, k); n   vector<long long> ans(a.size() + b.size() - 1); n   for (long long i = 0; i < ans.size(); ++i) { n     ans[i] = (long long)(f1[i].get_real() / n + 0.5); n   } n   return ans; n } n };  // namespace FFT n using namespace FFT; n vector<long long> pfft(vector<long long>& a, long long k) { n   vector<long long> res; n   res.push_back(1); n   while (k > 0) { n     if (k & 1) res = multiply(res, a); n     k = k >> 1; n     a = multiply(a, a); n     for (long long i = 0; i < (long long)a.size(); i++) n       if (a[i] > 0) a[i] = 1; n     for (long long i = 0; i < (long long)res.size(); i++) n       if (res[i] > 0) res[i] = 1; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   vector<long long> z(1001, 0); n   for (long long i = 0; i < n; i++) z[a[i]] = 1; n   vector<long long> ans = pfft(z, k); n   for (long long i = 0; i < (long long)ans.size(); i++) n     if (ans[i] > 0) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 2e5 + 5; n const int MOD = 1e9 + 7; n using namespace std; n int n; n char str1[N], str2[N]; n vector<int> lt[30], rt[30]; n vector<pair<int, int> > ans; n int main() { n   scanf( %d , &n); n   scanf(  %s , str1); n   scanf(  %s , str2); n   for (int a = 0; a < n; a++) { n     if (str1[a] ==  ? ) n       lt[26].push_back(a); n     else n       lt[(int)(str1[a] -  a )].push_back(a); n     if (str2[a] ==  ? ) n       rt[26].push_back(a); n     else n       rt[(int)(str2[a] -  a )].push_back(a); n   } n   for (int a = 0; a < 26; a++) { n     while (lt[a].size() > 0 && rt[a].size() > 0) { n       ans.push_back({lt[a][lt[a].size() - 1] + 1, rt[a][rt[a].size() - 1] + 1}); n       lt[a].pop_back(); n       rt[a].pop_back(); n     } n   } n   int idx = 0; n   while (lt[26].size() > 0 && idx < 26) { n     while (lt[26].size() > 0 && rt[idx].size() > 0) { n       ans.push_back( n           {lt[26][lt[26].size() - 1] + 1, rt[idx][rt[idx].size() - 1] + 1}); n       lt[26].pop_back(); n       rt[idx].pop_back(); n     } n     idx++; n   } n   idx = 0; n   while (rt[26].size() > 0 && idx < 26) { n     while (rt[26].size() > 0 && lt[idx].size() > 0) { n       ans.push_back( n           {lt[idx][lt[idx].size() - 1] + 1, rt[26][rt[26].size() - 1] + 1}); n       lt[idx].pop_back(); n       rt[26].pop_back(); n     } n     idx++; n   } n   while (rt[26].size() > 0 && lt[26].size() > 0) { n     ans.push_back( n         {lt[26][lt[26].size() - 1] + 1, rt[26][rt[26].size() - 1] + 1}); n     lt[26].pop_back(); n     rt[26].pop_back(); n   } n   printf( %d n , (int)ans.size()); n   for (auto v : ans) printf( %d %d n , v.first, v.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   iota(b.begin(), b.end(), 0); n   sort(b.begin(), b.end(), [&](int i, int j) { return a[i] < a[j]; }); n   vector<vector<int>> c(n + 1); n   for (int i = 0; i <= n; ++i) c[i].resize(n); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < a[b[i]]; ++j) c[(n + 1 + i - j) % (n + 1)][b[i]] = 1; n   cout << n + 1 <<   n ; n   for (int i = 0; i < n + 1; ++i, cout <<   n ) n     for (int j = 0; j < n; ++j) cout << c[i][j]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n; n   cin >> n; n   int ind = -1; n   int indn = -1; n   int l = n.length(); n   for (int i = l - 1; i >= 0; i--) { n     if (n[i] ==  0  || n[i] ==  2  || n[i] ==  4  || n[i] ==  6  || n         n[i] ==  8 ) { n       if (n[i] < n[l - 1]) { n         indn = i; n       } n     } n   } n   for (int i = 0; i < l - 1; i++) { n     if (n[i] ==  0  || n[i] ==  2  || n[i] ==  4  || n[i] ==  6  || n         n[i] ==  8 ) { n       if (n[i] > n[l - 1]) { n         ind = i; n       } n     } n   } n   if (indn != -1) { n     swap(n[indn], n[l - 1]); n   } else if (indn == -1 && ind != -1) { n     swap(n[ind], n[l - 1]); n   } else if (indn == -1 && ind == -1) { n     cout << -1; n     return 0; n   } n   cout << n; n } n