#include <bits/stdc++.h> n using namespace std; n namespace ioput { n template <typename T> n T read() { n   T x = 0, f = 1; n   char c = getchar_unlocked(); n   while ((c <  0 ) || (c >  9 )) { n     if (c ==  - ) f = -1; n     c = getchar_unlocked(); n   } n   while ((c >=  0 ) && (c <=  9 )) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar_unlocked(); n   return x * f; n } n template <typename T> n void write(T x, char c) { n   static char t[20]; n   static int tlen; n   t[tlen = 1] = c; n   if (x < 0) putchar_unlocked( - ), x = -x; n   do t[++tlen] = x % 10 +  0 , x /= 10; n   while (x); n   while (tlen) putchar_unlocked(t[tlen--]); n } n }  // namespace ioput n using namespace ioput; n const int maxn = 100005; n struct seg { n   int id, pos, t; n   bool operator<(const seg &A) const { n     return pos - t == A.pos - A.t ? pos < A.pos : pos - t < A.pos - A.t; n   } n } a[maxn], b[maxn], tmp[maxn]; n int a_cnt, b_cnt; n int p[maxn], n, W, H; n pair<int, int> ans[maxn]; n int main() { n   n = read<int>(), W = read<int>(), H = read<int>(); n   int type, pos, t; n   for (int i = 1, iend = n; i <= iend; ++i) { n     type = read<int>(), pos = read<int>(), t = read<int>(); n     if (type == 1) n       a[++a_cnt] = (seg){i, pos, t}; n     else n       b[++b_cnt] = (seg){i, pos, t}; n   } n   sort(a + 1, a + 1 + a_cnt); n   sort(b + 1, b + 1 + b_cnt); n   int cur = 1; n   for (int i = 1, iend = a_cnt; i <= iend; ++i) { n     static stack<int> stk; n     int j = i; n     while ((j < a_cnt) && (a[j + 1].pos - a[j + 1].t == a[i].pos - a[i].t)) ++j; n     int pos = j, tmpcur; n     while ((cur <= b_cnt) && (b[cur].pos - b[cur].t < a[i].pos - a[i].t)) ++cur; n     tmpcur = cur; n     for (int k = j, kend = i; k >= kend; --k) stk.push(a[k].id); n     while ((cur <= b_cnt) && (b[cur].pos - b[cur].t == a[i].pos - a[i].t)) n       stk.push(b[cur].id), ++cur; n     for (int k = i, kend = j; k <= kend; ++k) a[k].id = stk.top(), stk.pop(); n     for (int k = cur - 1, kend = tmpcur; k >= kend; --k) n       b[k].id = stk.top(), stk.pop(); n     i = j; n   } n   for (int i = 1, iend = a_cnt; i <= iend; ++i) n     ans[a[i].id] = make_pair(a[i].pos, H); n   for (int i = 1, iend = b_cnt; i <= iend; ++i) n     ans[b[i].id] = make_pair(W, b[i].pos); n   for (int i = 1, iend = n; i <= iend; ++i) n     printf( %d %d n , ans[i].first, ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ipow(int base, int exp) { n   int result = 1; n   for (;;) { n     if (exp & 1) result *= base; n     exp >>= 1; n     if (!exp) break; n     base *= base; n   } n   return result; n } n void show(int a[], int array_size) { n   for (int i = 0; i < array_size; ++i) cout << a[i] <<    ; n   cout <<   n ; n } n int cc(int target, int q, int current) { n   if (q == 0) { n     if (target == current) n       return 1; n     else n       return 0; n   } else n     return (cc(target, q - 1, current + 1) + cc(target, q - 1, current - 1)); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int x, y; n   cin >> x >> y; n   int v, i; n   cin >> v; n   long long o = 0; n   for (i = 0; i < v; i++) { n     int x1, y1, X, Y; n     cin >> x1 >> y1; n     if (x1 == 0) n       X = 1000000005; n     else n       X = max((1 - x) / x1, (n - x) / x1); n     if (y1 == 0) n       Y = 1000000005; n     else n       Y = max((1 - y) / y1, (m - y) / y1); n     int d = min(X, Y); n     o = o + (long long)d; n     x += d * x1; n     y += d * y1; n   } n   cout << o <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tt = 1; n   while (tt--) { n     int n, a, b, c; n     cin >> n >> a >> b >> c; n     int mv = min(min(a, b), c); n     int q = n / mv, ans = 0; n     for (int x = 0; x <= q; x++) { n       for (int y = 0; y <= q - x; y++) { n         float z = ((n - x * a - y * b) * 1.0) / c; n         if (z == (int)z && z >= 0) ans = max(ans, x + y + (int)z); n       } n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a, b, t, points[100000]; n int n; n double d[100000]; n pair<double, int> s1[100000], s2[100000]; n double dist(pair<int, int> first, pair<int, int> second) { n   long long x = first.first - second.first; n   long long y = first.second - second.second; n   return sqrt(x * x + y * y); n } n int main() { n   cin >> a.first >> a.second >> b.first >> b.second >> t.first >> t.second >> n; n   double ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> points[i].first >> points[i].second; n     d[i] = dist(points[i], t); n     ans += 2 * d[i]; n     s1[i] = pair<double, int>(dist(a, points[i]) - d[i], i); n     s2[i] = pair<double, int>(dist(b, points[i]) - d[i], i); n   } n   sort(s1, s1 + n); n   sort(s2, s2 + n); n   double ans1; n   double ans2; n   double ans3; n   ans1 = ans + s1[0].first; n   ans2 = ans + s2[0].first; n   if (s1[0].second != s2[0].second) { n     ans3 = ans + s1[0].first + s2[0].first; n   } else n     ans3 = ans + min(s1[0].first + s2[1].first, s1[1].first + s2[0].first); n   cout << setprecision(9); n   cout << min(ans1, min(ans2, ans3)); n } n


#include <bits/stdc++.h> n using namespace std; n int a[102]; n int b[102]; n int main() { n   int n, m; n   cin >> n >> m; n   int num = 0; n   for (int i = 1; i <= m; i++) { n     int q = 0; n     for (int j = 1; j <= n; j++) { n       cin >> a[j]; n       q = max(q, a[j]); n     } n     for (int l = 1; l <= n; l++) { n       if (a[l] == q) { n         b[l]++; n         break; n       } n     } n   } n   int t = 0; n   for (int j = 1; j <= 100; j++) { n     t = max(t, b[j]); n   } n   for (int l = 1; l <= 100; l++) { n     if (t == b[l]) { n       cout << l; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 300005; n int n, m; n int p[max_n]; n int rk[max_n]; n bool vis[max_n]; n vector<int> G[max_n]; n set<int> s; n void dfs(int u) { n   for (auto v : G[u]) s.erase(rk[v]); n   vector<int> can; n   for (auto v : s) { n     if (v < rk[u]) { n       v = p[v]; n       can.push_back(v); n       vis[rk[v]] = true; n     } else n       break; n   } n   for (auto v : G[u]) n     if (!vis[rk[v]]) s.insert(rk[v]); n   for (auto v : can) s.erase(rk[v]); n   for (auto v : can) dfs(v); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , p + i), rk[p[i]] = i; n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= n - 1; i++) s.insert(i); n   vis[n] = true; n   dfs(p[n]); n   printf( %d n , s.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, a, b; n   cin >> n >> a >> b; n   if (abs(a - b) > 1 || (a + b) > n - 2) { n     cout <<  -1  << endl; n     return; n   } n   int l = 1, r = n; n   int arr[n + 5]; n   for (int i = 1; i <= n; i++) arr[i] = i; n   if (a > b) n     for (int i = 1; i <= a; i++) swap(arr[r], arr[r - 1]), r -= 2; n   else if (b > a) n     for (int i = 1; i <= b; i++) swap(arr[l], arr[l + 1]), l += 2; n   else { n     if (b > 0) n       swap(arr[l], arr[l + 1]), l += 2, swap(arr[r], arr[r - 1]), r -= 2, b--; n     for (int i = 1; i <= b; i++) swap(arr[l], arr[l + 1]), l += 2; n   } n   for (int i = 1; i <= n; i++) cout << arr[i] <<    ; n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[m], b[m]; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   int min = 0; n   for (int i = 0; i < n; i++) { n     int t1 = INT_MAX; n     int loc = -1; n     for (int j = 0; j < m; j++) { n       if (a[j] < t1 && a[j] > 0) { n         t1 = a[j]; n         loc = j; n       } n     } n     a[loc] -= 1; n     min += t1; n   } n   int max = 0; n   for (int i = 0; i < n; i++) { n     int t = INT_MIN; n     int loc = -1; n     for (int j = 0; j < m; j++) { n       if (b[j] > t && b[j] > 0) { n         t = b[j]; n         loc = j; n       } n     } n     max += t; n     b[loc] -= 1; n   } n   cout << max <<     << min << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf(1010101010); n const long long P(29); n long long pp[100005]; n long long ch[100005]; n inline bool eq(int start1, int start2, int le) { n   long long h1(ch[start1 + le] - ch[start1]); n   long long h2(ch[start2 + le] - ch[start2]); n   return h1 * pp[start2] == h2 * pp[start1]; n } n int n, a[100005]; n map<int, vector<int> > startpos; n int shortest[100005]; n int cmin[100005]; n int main() { n   cin >> n; n   for (int i(0); i < (n); i++) cin >> a[i], startpos[a[i]].push_back(i); n   ch[0] = 0; n   pp[0] = 1; n   int i(1); n   for (; i <= (n); i++) { n     ch[i] = ch[i - 1] + pp[i - 1] * a[i - 1]; n     pp[i] = pp[i - 1] * P; n   } n   for (int i(0); i < (n); i++) { n     shortest[i] = inf; n     int startnum(a[i]); n     vector<int> const& v(startpos[startnum]); n     for (__typeof((v).end()) it((v).begin()); it != ((v).end()); ++it) { n       int j(*it); n       if (j <= i) continue; n       int le(j - i); n       if (j + le <= n && eq(i, j, le)) shortest[i] = min(shortest[i], le); n     } n   } n   cmin[n] = inf; n   for (int i(n - 1); i >= (0); i--) { n     cmin[i] = min(cmin[i + 1], shortest[i]); n   } n   int pt(0); n   for (; pt < (n);) { n     int le(cmin[pt]); n     if (le == inf) break; n     int loc(find(shortest + pt, shortest + n, le) - shortest); n     pt = loc + le; n   } n   cout << (n - pt) << endl; n   for (int i(pt); i < (n); i++) cout << a[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char a[10][10]; n int main(void) { n   for (int i = 0; i < 8; i++) { n     cin >> a[i]; n   } n   int b = 10, w = 10; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       if (a[i][j] ==  B ) { n         int f = 0; n         for (int k = i + 1; k < 8; k++) { n           if (a[k][j] !=  . ) f = 1; n         } n         if (!f) b = min(8 - i - 1, b); n       } n       if (a[i][j] ==  W ) { n         int f = 0; n         for (int k = i - 1; k >= 0; k--) { n           if (a[k][j] !=  . ) f = 1; n         } n         if (!f) w = min(i, w); n       } n     } n   } n   if (b < w) n     cout <<  B ; n   else n     cout <<  A ; n } n


#include <bits/stdc++.h> n using namespace std; n int e, s, n, m, st, q[4][200000 + 10]; n pair<int, int> a[200000 * 2 + 10], f[200000 * 2 + 10], ans[200000 + 10]; n int main() { n   int i, front[4], rear[4], j; n   memset(f, 0xff, sizeof f); n   memset(ans, 0xff, sizeof ans); n   scanf( %d%d%d%d , &e, &s, &n, &m); n   for (i = 1; i <= n; i++) scanf( %d%d , &a[i].second, &a[i].first); n   for (i = 1; i <= m; i++) { n     scanf( %d , &a[i + n].first); n     a[i + n].second = 3 + i; n   } n   sort(a + 1, a + n + m + 1); n   for (i = 1; i <= n + m; i++) n     if (a[i].first > e) break; n   if (a[i].first == e) i--; n   a[i].first = e, a[i].second = 3; n   e = i; n   f[e] = make_pair(0, 0); n   q[3][0] = e; n   front[3] = rear[3] = 0; n   front[2] = front[1] = 0; n   rear[2] = rear[1] = -1; n   for (i = e - 1; i; i--) { n     int t = 0x7fffffff; n     while (front[3] <= rear[3] && a[q[3][front[3]]].first - a[i].first > s) n       front[3]++; n     while (front[2] <= rear[2] && a[q[2][front[2]]].first - a[i].first > s) n       front[2]++; n     while (front[1] <= rear[1] && a[q[1][front[1]]].first - a[i].first > s) n       front[1]++; n     if (a[i].second > 3) { n       for (j = 3; j > 0; j--) n         if (front[j] <= rear[j]) { n           if (j == 1) n             ans[a[i].second - 3] = n                 make_pair(f[q[j][front[j]]].first - s + n                               a[q[j][front[j]]].first - a[i].first, n                           f[q[j][front[j]]].second); n           else if (j == 2) n             ans[a[i].second - 3] = n                 make_pair(f[q[j][front[j]]].first, n                           f[q[j][front[j]]].second - s + n                               a[q[j][front[j]]].first - a[i].first); n           else n             ans[a[i].second - 3] = n                 make_pair(f[q[j][front[j]]].first, f[q[j][front[j]]].second); n           break; n         } n       if (!i) ans[a[i].second] = make_pair(-1, -1); n       continue; n     } n     for (j = 3; j >= a[i].second; j--) n       if (front[j] <= rear[j]) t = min(t, q[j][rear[j]]); n     if (t == 0x7fffffff) n       for (; j; j--) n         if (front[j] <= rear[j]) { n           t = q[j][front[j]]; n           break; n         } n     if (t == 0x7fffffff) break; n     f[i] = f[t]; n     if (a[t].second >= a[i].second) { n       if (a[i].second == 1) n         f[i].first += a[t].first - a[i].first; n       else if (a[i].second == 2) n         f[i].second += a[t].first - a[i].first; n     } else { n       if (a[i].second == 2) { n         f[i].second += s; n         f[i].first -= s - a[t].first + a[i].first; n       } else if (a[i].second == 3) { n         if (a[t].second == 2) n           f[i].second -= s - a[t].first + a[i].first; n         else n           f[i].first -= s - a[t].first + a[i].first; n       } n     } n     q[a[i].second][++rear[a[i].second]] = i; n   } n   for (i = 1; i <= m; i++) printf( %d %d n , ans[i].first, ans[i].second); n } n


#include <bits/stdc++.h> n using namespace std; n #define mp make_pair n #define pb push_back n #define x first n #define y second n typedef pair<int,int> pii; n typedef long long ll; n typedef unsigned long long ull; n template <typename T> void chkmax(T &x,T y){x<y?x=y:T();} n template <typename T> void chkmin(T &x,T y){y<x?x=y:T();} n template <typename T> void readint(T &x) n { n  x=0;int f=1;char c; n  for(c=getchar();!isdigit(c);c=getchar())if(c== - )f=-1; n  for(;isdigit(c);c=getchar())x=x*10+(c- 0 ); n  x*=f; n } n const int MOD1=1010101039,MOD2=1000020001; n const int MAXN=300005,MAXK=19,MAXV=MAXN*21; n  n int n,a[MAXN]; n vector<int> G[MAXN]; n int anc[MAXN][MAXK],dep[MAXN]; n int stot,vs[MAXN]; n pii h[MAXV],pw[MAXN]; n int lch[MAXV],rch[MAXV]; n pii operator *(pii a,pii b){return mp(1ll*a.x*b.x%MOD1,1ll*a.y*b.y%MOD2);} n pii operator +(pii a,pii b){return mp((a.x+b.x)%MOD1,(a.y+b.y)%MOD2);} n int modify(int id,int l,int r,int x) n { n  int rt=++stot; n  if(l==r) n  { n   h[rt]=mp(1-h[id].x,1-h[id].y); n   return rt; n  } n  int mid=(l+r)>>1; n  if(x<=mid)lch[rt]=modify(lch[id],l,mid,x),rch[rt]=rch[id]; n  else rch[rt]=modify(rch[id],mid+1,r,x),lch[rt]=lch[id]; n  h[rt]=h[lch[rt]]*pw[r-mid]+h[rch[rt]]; n  return rt; n } n int query(int u,int v,int l,int r,int ql,int qr) n { n  if(h[u]==h[v])return -1; n  if(l==r)return l; n  int mid=(l+r)>>1; n  if(qr<=mid)return query(lch[u],lch[v],l,mid,ql,qr); n  if(ql>mid)return query(rch[u],rch[v],mid+1,r,ql,qr); n  int s=query(lch[u],lch[v],l,mid,ql,mid); n  if(s>0)return s; n  return query(rch[u],rch[v],mid+1,r,mid+1,qr); n } n void dfs(int u,int pa) n { n  for(int j=1;j<MAXK;++j)anc[u][j]=anc[anc[u][j-1]][j-1]; n  for(auto v:G[u]) n   if(v!=pa) n   { n    anc[v][0]=u; n    dep[v]=dep[u]+1; n    vs[v]=modify(vs[u],1,n,a[v]); n    dfs(v,u); n   } n } n int lca(int u,int v) n { n  if(dep[u]>dep[v])swap(u,v); n  for(int i=MAXK-1;i>=0;--i)if(dep[anc[v][i]]>=dep[u])v=anc[v][i]; n  if(u==v)return u; n  for(int i=MAXK-1;i>=0;--i)if(anc[u][i]!=anc[v][i])u=anc[u][i],v=anc[v][i]; n  return anc[u][0]; n } n  n int main() n { n  #ifdef LOCAL n  freopen( code.in , r ,stdin); n // freopen( code.out , w ,stdout); n  #endif n  int Q,u,v,l,r; n  readint(n),readint(Q); n  for(int i=1;i<=n;++i)readint(a[i]); n  for(int i=1;i<n;++i)readint(u),readint(v),G[u].pb(v),G[v].pb(u); n  pw[0]=mp(1,1);pw[1]=mp(29,101); n  for(int i=2;i<=n;++i)pw[i]=pw[i-1]*pw[1];  n  vs[1]=modify(0,1,n,a[1]); n  dep[1]=1; n  dfs(1,-1); n  while(Q--) n  { n   readint(u),readint(v),readint(l),readint(r); n   int d=lca(u,v); n   if(a[d]<l || a[d]>r) n   { n    int t=query(vs[u],vs[v],1,n,l,r); n    printf( %d n ,t); n   } n   else n   { n    int t=(a[d]==l?-1:query(vs[u],vs[v],1,n,l,a[d]-1)); n    if(t<0) n    { n     t=(a[d]==r?-1:query(vs[u],vs[v],1,n,a[d]+1,r)); n     if(t<0) n     { n      t=query(vs[u],vs[v],1,n,a[d],a[d]); n      if(t==-1)t=a[d]; n      else t=-1; n     } n    } n    printf( %d n ,t); n   } n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int price[100005]; n int sum[100005]; n vector<int> tempo; n int main() { n   int n, t; n   scanf( %d , &n); n   tempo.push_back(0); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &t); n     if (i == 1) { n       price[i] = 20; n       sum[i] = 20; n       tempo.push_back(t); n       continue; n     } n     int fim = i - 1; n     int ini = (int)(lower_bound(tempo.begin(), tempo.end(), t - 1440 + 1) - n                     tempo.begin()); n     ini = max(ini - 1, 0); n     int p = (sum[fim] - sum[ini] >= 120 ? 0 : 120 - (sum[fim] - sum[ini])); n     ini = (int)(lower_bound(tempo.begin(), tempo.end(), t - 90 + 1) - n                 tempo.begin()); n     ini = max(ini - 1, 0); n     int aux = (sum[fim] - sum[ini] >= 50 ? 0 : 50 - (sum[fim] - sum[ini])); n     price[i] = min(p, min(aux, 20)); n     sum[i] = sum[i - 1] + price[i]; n     tempo.push_back(t); n   } n   for (int i = 1; i <= n; i++) { n     printf( %d n , price[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   long long ax, ay, bx, by, cx, cy, ab, bc; n   double slopeAB, slopeBC; n   cin >> ax >> ay >> bx >> by >> cx >> cy; n   if (ax == cx && ay == cy) { n     cout <<  Yes n ; n   } else { n     ab = (ax - bx) * (ax - bx) + (ay - by) * (ay - by); n     bc = (cx - bx) * (cx - bx) + (cy - by) * (cy - by); n     slopeAB = (double)(ay - by) / (double)(ax - bx); n     slopeBC = (double)(by - cy) / (double)(bx - cx); n     if (ab == bc && slopeAB != slopeBC) { 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 long long bigmod(long long b, long long p, long long m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     long long temp = bigmod(b, p / 2, m); n     return (temp * temp) % m; n   } n   return (bigmod(b, p - 1, m) * (b % m)) % m; n } n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   long long cnt = 0; n   long long ans = 0; n   long long mod = 1e9 + 7; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  a ) { n       cnt++; n     } else { n       if (cnt > 0) ans += (bigmod(2, cnt, mod) - 1); n       ans %= mod; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<bool> was; n vector<vector<int> > es; n vector<int> cst; n vector<int> cols; n vector<vector<int> > cycs; n void dfs(int v, int par = -1) { n   was[v] = true; n   cst.push_back(v); n   for (int i = 0; i < ((int)(es[v]).size()); i++) { n     int b = es[v][i]; n     if (b == par) continue; n     if (was[b]) { n       if (cols[v] >= 0) { n         assert(cols[v] == cols[b]); n       } else { n         int cid = ((int)(cycs).size()); n         cycs.push_back(vector<int>()); n         for (int i2 = ((int)(cst).size()) - 1; i2 >= 0; i2--) { n           cycs[cid].push_back(cst[i2]); n           assert(cols[cst[i2]] < 0); n           cols[cst[i2]] = cid; n           if (cst[i2] == b) break; n         } n       } n     } else n       dfs(b, v); n   } n   cst.pop_back(); n } n int dfs2(int v, int par = -1) { n   int cid = cols[v]; n   int res = ((int)(cycs[cid]).size()) > 1; n   int sumcnt = 0; n   int nzcnt = 0; n   for (int i = 0; i < ((int)(cycs[cid]).size()); i++) { n     int a = cycs[cid][i]; n     int subcnt = 0; n     for (int i2 = 0; i2 < ((int)(es[a]).size()); i2++) { n       int b = es[a][i2]; n       if (cols[b] == cid) continue; n       int cres = -1; n       if (b != par) { n         cres = dfs2(b, a); n       } else { n         cres = 1; n       } n       res += cres; n       subcnt++; n     } n     res -= subcnt / 2; n     sumcnt += subcnt; n     if (subcnt) nzcnt++; n   } n   if (((int)(cycs[cid]).size()) > 1) { n     if (nzcnt >= 2) { n       res -= 1; n     } n   } n   return res; n } n int main() { n   int n, m; n   while (scanf( %d%d , &n, &m) >= 1) { n     es = vector<vector<int> >(n); n     for (int i = 0; i < m; i++) { n       int a, b; n       scanf( %d%d , &a, &b), a--, b--; n       es[a].push_back(b); n       es[b].push_back(a); n     } n     was = vector<bool>(n, false); n     cst.clear(); n     cols = vector<int>(n, -1); n     cycs = vector<vector<int> >(); n     dfs(0); n     for (int i = 0; i < n; i++) n       if (cols[i] < 0) { n         int cid = ((int)(cycs).size()); n         cycs.push_back(vector<int>(1, i)); n         cols[i] = cid; n       } n     int ans = dfs2(0); n     printf( %d %d n , ans, m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int T = 7; n int prosti[] = {114689, 163841, 147457, 188417, 151553, 270337, 286721}; n int proots[] = {3, 3, 10, 3, 3, 10, 11}; n int pows[7][300000]; n void fft_trivial(vector<int>& a, int id) { n   int n = a.size(); n   int mod = prosti[id]; n   int step = (mod - 1) / n; n   vector<int> b(n); n   for (int i = 0; i < n; i++) { n     for (int k = 0, w = 0; k < n; k++, w = (w + i * step) % (mod - 1)) { n       b[i] += a[k] * 1ll * pows[id][w] % mod; n       if (b[i] >= mod) b[i] -= mod; n     } n   } n   swap(a, b); n } n void fft(vector<int>& a, int id) { n   int n = a.size(); n   if (n & 1) { n     fft_trivial(a, id); n     return; n   } n   int mod = prosti[id]; n   int step = (mod - 1) / n; n   int h = n >> 1; n   vector<int> b(h), c(h); n   for (int i = 0; i < h; i++) { n     b[i] = a[2 * i]; n     c[i] = a[2 * i + 1]; n   } n   fft(b, id); n   fft(c, id); n   for (int i = 0, w = 0; i < h; i++, w += step) { n     int bb = b[i]; n     int cc = c[i] * 1ll * pows[id][w] % mod; n     int u = bb + cc; n     int v = bb - cc; n     if (u >= mod) u -= mod; n     if (v < 0) v += mod; n     a[i] = u; n     a[i + h] = v; n   } n } n void init() { n   for (int i = 0; i < T; i++) { n     int p = 1, q = proots[i]; n     for (int j = 0; j < 300000; j++) { n       pows[i][j] = p; n       p = p * 1ll * q % prosti[i]; n     } n   } n } n int hsh(string s, int mod, int b) { n   int z = 0; n   for (int i = 0, p = 1; i < (int)s.size(); i++, p = p * 1ll * b % mod) n     z = (z + s[i] * 1ll * p) % mod; n   return z; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   init(); n   int n, m; n   string a, b; n   cin >> n >> m >> a >> b; n   for (int i = 0; i < T; i++) { n     int mod = prosti[i]; n     vector<int> va(mod - 1, 0), vb(mod - 1, 0); n     for (int j = 0; j < n; j++) { n       va[j] = a[j]; n       vb[j] = b[j]; n     } n     fft(va, i); n     fft(vb, i); n     for (int r = 0; r < mod - 1; r++) { n       int y = pows[i][r]; n       if (va[r] == vb[r] && y > 1 && y < mod - 1) { n         cout << mod <<     << y <<   n ; n         return 0; n       } n     } n   } n   cout <<  Failed n ; n   return 1; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:536870912 ) n #pragma warning(disable : 4996) n using namespace std; n const int N = 5e5 + 100; n int q[N], a[N], b[N], w[N], used[N], in[N]; n bool ok[N]; n vector<int> tr[N], d[N]; n vector<pair<int, vector<int>>> gr[N]; n vector<pair<int, int>> ed; n int col = 0, cg = 0; n int find(int v) { return q[v] < 0 ? v : q[v] = find(q[v]); } n bool un(int a, int b) { n   a = find(a), b = find(b); n   if (a != b) { n     if (q[a] > q[b]) swap(a, b); n     q[a] += q[b]; n     q[b] = a; n     return true; n   } n   return false; n } n bool dfs(int node, int p) { n   used[node] = col; n   for (int to : tr[node]) { n     if (to == p) continue; n     if (used[to] == col) n       return true; n     else if (dfs(to, node)) n       return true; n   } n   return false; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   memset(q, -1, sizeof(q)); n   ; n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &a[i], &b[i], &w[i]); n     a[i]--, b[i]--; n     ed.push_back({w[i], i}); n   } n   sort(ed.begin(), ed.end()); n   int t; n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 0; j < k; j++) { n       scanf( %d , &in[j]); n       in[j]--; n       if (gr[w[in[j]]].empty() || !gr[w[in[j]]].back().second.empty()) { n         gr[w[in[j]]].push_back({cg, vector<int>()}); n         d[i].push_back(cg); n         cg++; n       } n     } n     for (int j = 0; j < k; j++) { n       gr[w[in[j]]].back().second.push_back(in[j]); n     } n   } n   for (int i = 0; i < ed.size(); i++) { n     if (i == 0 || ed[i].first != ed[i - 1].first) { n       for (auto &p : gr[ed[i].first]) { n         for (int j : p.second) { n           int x = find(a[j]), y = find(b[j]); n           tr[x].push_back(y); n           tr[y].push_back(x); n         } n         col++; n         ok[p.first] = 1; n         for (int j : p.second) { n           int x = find(a[j]); n           if (used[x] != col && dfs(x, -1)) { n             ok[p.first] = 0; n             break; n           } n         } n         for (int j : p.second) { n           int x = find(a[j]), y = find(b[j]); n           tr[x].clear(); n           tr[y].clear(); n         } n       } n     } n     un(a[ed[i].second], b[ed[i].second]); n   } n   for (int i = 0; i < t; i++) { n     bool f = true; n     for (int j : d[i]) { n       if (!ok[j]) { n         f = false; n         break; n       } n     } n     printf( %s n , f ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   string str; n   for (int i = 1; i <= t; i++) { n     cin >> str; n     string str1; n     str1.append(str, 0, 1); n     int j = 1, l = str.length(); n     while (j < l) { n       str1.append(str, j, 1); n       j += 2; n     } n     cout << str1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n int a[100010], cnt[100010]; n int queries[100010][2]; n bool res[100010]; n bool xpeHb[100010]; n int main() { n   int t; n   scanf( %d , &t); n   for (int y = 0; y < t; y++) { n     int m, k; n     scanf( %d %d , &m, &k); n     for (int i = 1; i <= k; i++) { n       scanf( %d , &a[i]); n     } n     int t, r; n     int count = 0; n     bool flag = false; n     for (int i = 0; i < m - 1; i++) { n       scanf( %d %d , &queries[i][0], &queries[i][1]); n       if (queries[i][1] == 1) flag = true; n       if (flag) { n         if (queries[i][0]) xpeHb[queries[i][0]] = true; n       } n     } n     flag = true; n     for (int i = 0; i < m - 1; i++) { n       t = queries[i][0]; n       r = queries[i][1]; n       if (r == 1 && flag) { n         flag = false; n         int max = 0; n         for (int j = 1; j <= k; j++) { n           if (cnt[j] + count >= a[j] && !xpeHb[j]) { n             res[j] = true; n             if (max < cnt[j] + count - a[j]) max = cnt[j] + count - a[j]; n           } n         } n         count = max; n       } n       if (t == 0) n         count++; n       else { n         cnt[t]++; n       } n     } n     for (int j = 1; j <= k; j++) { n       if (cnt[j] + count >= a[j]) { n         res[j] = true; n       } n       if (res[j]) n         printf( Y ); n       else n         printf( N ); n     } n     printf(  n ); n     memset(cnt, 0, (k + 1) * 4); n     memset(res, 0, (k + 1) * 1); n     memset(a, 0, (k + 1) * 4); n     memset(xpeHb, 0, (k + 1) * 1); n     memset(queries[0], 0, (m + 1) * 4); n     memset(queries[1], 0, (m + 1) * 4); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace fast { n inline char nc() { n   static char buf[100000], *L = buf, *R = buf; n   return L == R && (R = (L = buf) + fread(buf, 1, 100000, stdin), L == R) n              ? EOF n              : *L++; n } n template <class orz> n inline void qread(orz &x) { n   x = 0; n   char ch = nc(); n   bool f = 0; n   while (ch <  0  || ch >  9 ) (ch ==  - ) && (f = 1), ch = nc(); n   while (ch >=  0  && ch <=  9 ) x = (x << 3) + (x << 1) + (ch ^ 48), ch = nc(); n   f && (x = -x); n } n }  // namespace fast n using namespace fast; n template <class orz> n inline void read(orz &x) { n   x = 0; n   bool f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) (ch ==  - ) && (f = 1), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   f && (x = -x); n } n template <class orz> n inline void out(orz x) { n   (x < 0) && (putchar( - ), x = -x); n   if (x > 9) out(x / 10); n   putchar(x % 10 +  0 ); n } n const double PI = acos(-1); n const double eps = 1e-8; n const int INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const int maxn = 1e3 + 5; n int a[maxn][maxn]; n int n, m; n int r[maxn], c[maxn]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) read(r[i]); n   for (int i = 0; i < m; i++) read(c[i]); n   for (int i = 0; i < n; i++) { n     if (r[i] == 0) { n       a[i][0] = -1; n     } else { n       for (int j = 0; j < r[i]; j++) { n         a[i][j] = 1; n       } n       a[i][r[i]] = -1; n     } n   } n   int flag = 0; n   long long sum = 1; n   for (int i = 0; i < m; i++) { n     if (c[i] == 0) { n       if (a[0][i] == 0 || a[0][i] == -1) n         a[0][i] = -1; n       else { n         flag = 1; n         break; n       } n     } else { n       for (int j = 0; j < c[i]; j++) { n         if (a[j][i] == 0 || a[j][i] == 1) n           a[j][i] = 1; n         else { n           flag = 1; n           break; n         } n       } n       if (a[c[i]][i] == 0 || a[c[i]][i] == -1) n         a[c[i]][i] = -1; n       else { n         flag = 1; n         break; n       } n     } n   } n   if (!flag) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (a[i][j] == 0) sum = sum * 2 % mod; n       } n     } n     cout << sum << endl; n   } else n     cout <<  0  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int sum = 0; n   int i = 0; n   while (i < n) { n     int x; n     cin >> x; n     sum += x; n     ++i; n   } n   cout << fixed << setprecision(12); n   cout << (long double)sum / n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   unsigned long long c; n   for (int i = 0; i < q; i++) { n     cin >> c; n     if ((c < 4) | (c % 2 != 0 & c < 13 & c != 9)) n       cout << -1 << endl; n     else { n       if (c % 2 != 0) n         cout << (c - 9) / 4 + 1 << endl; n       else n         cout << c / 4 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int tc; n   cin >> tc; n   while (tc--) { n     int arr[103][103]; n     long long cnt = 0; n     int n, m; n     cin >> n >> m; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) cin >> arr[i][j]; n     for (int i = 1; i <= n / 2; i++) { n       vector<int> a; n       for (int j = 1; j <= m / 2; j++) { n         a.push_back(arr[i][j]); n         a.push_back(arr[n + 1 - i][j]); n         a.push_back(arr[i][m + 1 - j]); n         a.push_back(arr[n + 1 - i][m + 1 - j]); n         sort(a.begin(), a.end()); n         for (int tmp : a) cnt += abs(tmp - a[1]); n         a.clear(); n       } n       if (m % 2) cnt += abs(arr[i][m / 2 + 1] - arr[n + 1 - i][m / 2 + 1]); n     } n     if (n % 2) { n       for (int j = 1; j <= m / 2; j++) n         cnt += abs(arr[n / 2 + 1][j] - arr[n / 2 + 1][m + 1 - j]); n     } n     cout << cnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, hours_digit = 1, minutes_digit = 1; n   cin >> n >> m; n   long long int temp = 0; n   for (long long int a = 7; a < n; a *= 7) hours_digit += 1; n   for (long long int b = 7; b < m; b *= 7) minutes_digit += 1; n   if (hours_digit + minutes_digit > 7) { n     cout << 0 << endl; n     return 0; n   } n   long long int final_ans = 0; n   for (int i = 0; i < n; i++) { n     vector<int> arr(7, 0); n     temp = i; n     int temp2 = 0; n     while (temp > 0) { n       arr[temp % 7]++; n       temp = temp / 7; n       temp2++; n     } n     arr[0] += hours_digit - temp2; n     for (int j = 0; j < m; j++) { n       vector<int> arr2(7, 0); n       temp = j; n       temp2 = 0; n       while (temp > 0) { n         arr2[temp % 7]++; n         temp = temp / 7; n         temp2++; n       } n       arr2[0] += minutes_digit - temp2; n       int flag = 0; n       for (int k = 0; k < 7; k++) { n         if (arr2[k] + arr[k] > 1) { n           flag = 1; n           break; n         } n       } n       if (flag == 0) { n         final_ans++; n       } n     } n   } n   cout << final_ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long size, seg1[2 * 200005], seg2[2 * 200005], a[200005], b[200005], n     seg3[2 * 200005], seg4[2 * 200005]; n long long temp = 0; n long long f(long long u, long long v) { return min(u, v); } n long long query(long long l, long long r, long long u, long long v, n                 long long seg[]) { n   long long mid = (u + v) / 2; n   if ((l == u) && (r == v)) n     return seg[(size + u) / (r - l + 1) - 1]; n   else if (l > mid) n     return query(l, r, mid + 1, v, seg); n   else if (r <= mid) n     return query(l, r, u, mid, seg); n   else n     return f(query(l, mid, u, mid, seg), query(mid + 1, r, mid + 1, v, seg)); n } n void update(long long ind, long long num, long long seg[]) { n   long long pos = size + ind - 1; n   seg[pos] = num; n   while (pos) { n     seg[(pos - 1) / 2] = f(seg[pos], seg[pos + pos % 2 * 2 - 1]); n     pos--; n     pos /= 2; n   } n } n int main() { n   ios::sync_with_stdio(false); n   long long i, t, j, k, l, m, n; n   cin >> n; n   long long INF = 1; n   for (size = 1; size < n; size *= 2) n     ; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     update(i, a[i], seg1); n   } n   long long maxi = -INF, cnt = 0; n   for (i = 0; i < n - 1; i++) { n     maxi = max(a[i], maxi); n     if (maxi <= query(i + 1, n - 1, 0, size - 1, seg1)) { n       cnt++; n       maxi = -INF; n     } n   } n   cout << cnt + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   cin >> n >> s; n   vector<int> a(n); n   --s; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   int ans = 0; n   if (a[s] != 0) { n     a[s] = 0; n     ++ans; n   } n   for (int i = 0; i < n; ++i) n     if ((i != s) && (a[i] == 0)) a[i] = n; n   map<int, int> workers; n   for (int i = 0; i < n; ++i) ++workers[a[i]]; n   int count = n - 1; n   int max = n; n   while (!workers.count(max)) --max; n   for (int i = 1; count; ++i) { n     if (workers[i]) n       count -= workers[i]; n     else { n       ++ans; n       --count; n       --workers[max]; n       while (!workers[max]) --max; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n char ibuf[4096]; n int ipt = 4096; n int readUInt() { n   while (ipt < 4096 && ibuf[ipt] <  0 ) ipt++; n   if (ipt == 4096) { n     fread(ibuf, 1, 4096, stdin); n     ipt = 0; n     while (ipt < 4096 && ibuf[ipt] <  0 ) ipt++; n   } n   int n = 0; n   char neg = 0; n   if (ipt != 0 && ibuf[ipt - 1] ==  - ) neg = 1; n   while (ipt < 4096 && ibuf[ipt] >=  0 ) n = (n * 10) + (ibuf[ipt++] -  0 ); n   if (ipt == 4096) { n     fread(ibuf, 1, 4096, stdin); n     ipt = 0; n     while (ipt < 4096 && ibuf[ipt] >=  0 ) n = (n * 10) + (ibuf[ipt++] -  0 ); n   } n   return neg ? -n : n; n } n int testcase; n void solve() { n   int n; n   scanf( %d , &n); n   ; n   int d; n   scanf( %d , &d); n   ; n   int l; n   scanf( %d , &l); n   ; n   int v[n]; n   int d1 = d; n   if (d > 0) { n     for (int i = 0; i < n; ++i) { n       if (i % 2 == 0) { n         if (d1 > 0) { n           v[i] = min(l, d1); n           d1 -= v[i]; n         } else { n           v[i] = 1; n           d1 -= 1; n         } n       } else { n         v[i] = 1; n         d1 += 1; n       } n     } n   } else { n     for (int i = 0; i < n; ++i) { n       if (i % 2 == 0) { n         v[i] = 1; n         d1 -= 1; n       } else { n         if (d1 < 0) { n           v[i] = min(l, -d1); n           d1 += v[i]; n         } else { n           d1 += 1; n         } n       } n     } n   } n   if (d1 == 1 && l > 1) { n     for (int i = 0; i < n; i++) { n       if (i % 2 == 0 && v[i] < l) { n         v[i]++; n         d1 = 0; n         break; n       } else if (i % 2 == 1 && v[i] > 1) { n         v[i]--; n         d1 = 0; n         break; n       } n     } n   } n   if (d1 == -1 && l > 1) { n     for (int i = 0; i < n; i++) { n       if (i % 2 == 0 && v[i] > 1) { n         v[i]--; n         d1 = 0; n         break; n       } else if (i % 2 == 1 && v[i] < l) { n         v[i]++; n         d1 = 0; n         break; n       } n     } n   } n   if (d1 != 0) { n     printf( -1 n ); n   } else { n     for (int i = 0; i < n; ++i) { n       if (i) printf(   ); n       printf( %d , v[i]); n     } n     printf(  n ); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int res = 0; n   register char c = getchar(), f = 1; n   while (c < 48 || c > 57) { n     if (c ==  - ) f = 0; n     c = getchar(); n   } n   while (c >= 48 && c <= 57) n     res = (res << 3) + (res << 1) + (c & 15), c = getchar(); n   return f ? res : -res; n } n inline void write(long long x) { n   register char c[21], len = 0; n   if (!x) return putchar( 0 ), void(); n   if (x < 0) x = -x, putchar( - ); n   while (x) c[++len] = x % 10, x /= 10; n   while (len) putchar(c[len--] + 48); n } n const int N = 1e5 + 10, L = 20; n struct node { n   int sz, a[L]; n   inline node(int x = -1) { n     if (~x) { n       sz = 1; n       for (int i = 0; i < L; ++i, x >>= 1) a[i] = x & 1; n     } else n       sz = 0, memset(a, 0, sizeof(a)); n   } n   inline node operator+(const node& p) const { n     node r = *this; n     r.sz += p.sz; n     for (int i = 0; i < L; ++i) r.a[i] += p.a[i]; n     return r; n   } n   inline node operator^(int x) const { n     node r = *this; n     for (int i = 0; i < L; ++i, x >>= 1) r.a[i] = x & 1 ? sz - a[i] : a[i]; n     return r; n   } n } a[N], sum[N]; n inline void write(const node& x) { n   long long r = 0; n   for (int i = 0; i < L; ++i) r += (long long)x.a[i] << i; n   write(r); n } n int n, m, rt, w[N], lc[N], rc[N], tag[N], tot = 0; n inline void pu(int x) { sum[x] = sum[lc[x]] + sum[rc[x]] + a[x]; } n int build(int l, int r) { n   if (l > r) return 0; n   int mid = (l + r) >> 1, x = mid; n   w[x] = ++tot; n   lc[x] = build(l, mid - 1), rc[x] = build(mid + 1, r); n   pu(x); n   return x; n } n inline void add(int x, int t) { n   a[x] = a[x] ^ t, sum[x] = sum[x] ^ t, tag[x] ^= t; n } n inline void pd(int x) { n   if (tag[x]) add(lc[x], tag[x]), add(rc[x], tag[x]), tag[x] = 0; n } n int merge(int x, int y) { n   if (!x || !y) return x | y; n   if (w[x] < w[y]) { n     pd(x), rc[x] = merge(rc[x], y), pu(x); n     return x; n   } else { n     pd(y), lc[y] = merge(x, lc[y]), pu(y); n     return y; n   } n } n void split(int rt, int k, int& x, int& y) { n   if (!rt) return x = y = 0, void(); n   pd(rt); n   if (sum[lc[rt]].sz < k) { n     x = rt; n     split(rc[x], k - sum[lc[x]].sz - 1, rc[x], y); n     pu(x); n   } else { n     y = rt; n     split(lc[y], k, x, lc[y]); n     pu(y); n   } n } n int op, l, r, x, y, z; n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) sum[i] = a[i] = read(); n   m = read(); n   rt = build(1, n); n   while (m--) { n     op = read(), l = read(), r = read(); n     if (op == 1) { n       split(rt, r, x, z); n       split(x, l - 1, x, y); n       write(sum[y]), putchar(  n ); n       rt = merge(merge(x, y), z); n     } else { n       split(rt, r, x, z); n       split(x, l - 1, x, y); n       add(y, read()); n       rt = merge(merge(x, y), z); n     } n   } n } n


#include <bits/stdc++.h> n int main(int argc, char** argv) { n   int32_t num_queries; n   std::cin >> num_queries; n   for (int32_t i = 0; i < num_queries; i++) { n     int32_t num_sticks; n     std::cin >> num_sticks; n     if (num_sticks == 2) n       std::cout << 2 << std::endl; n     else if (num_sticks % 2 == 0) n       std::cout << 0 << std::endl; n     else n       std::cout << 1 << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(long long a = 0) { n   freopen( in ,  r , stdin); n   if (a) freopen( out ,  w , stdout); n } n vector<vector<pair<long long, long long> > > ad; n long long over; n long long dfs(long long a) { n   over = a; n   if (ad[a].size() == 0) return 1000000007; n   return min(ad[a][0].second, dfs(ad[a][0].first)); n } n int main() { n   long long n; n   cin >> n; n   long long m; n   ad = vector<vector<pair<long long, long long> > >(n + 1); n   cin >> m; n   bool vis[10000] = {}, good[10000] = {}; n   while (m--) { n     long long x, y, d; n     cin >> x >> y >> d; n     x--, y--; n     vis[y] = 1; n     good[x] = 1; n     ad[x].push_back(make_pair(y, d)); n   } n   vector<pair<pair<long long, long long>, long long> > ans; n   for (int i = 0; i < n; i++) { n     if (!vis[i] && good[i]) { n       pair<pair<long long, long long>, long long> tmp; n       tmp.first.first = i; n       tmp.second = dfs(i); n       tmp.first.second = over; n       ans.push_back(tmp); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first.first + 1 <<     << ans[i].first.second + 1 <<     n          << ans[i].second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename U, typename V> n void Min(U &a, const V &b) { n   if (a > b) a = b; n } n template <typename U, typename V> n void Max(U &a, const V &b) { n   if (a < b) a = b; n } n template <typename U, typename V> n void add(U &a, const V &b) { n   a = (a + b) % 1000000007; n } n template <typename U> n U gcd(U a, U b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a >= b) n     return gcd(a % b, b); n   else n     return gcd(a, b % a); n } n int pow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % 1000000007; n     a = 1LL * a * a % 1000000007; n     b >>= 1; n   } n   return ans; n } n int pow(int a, long long b, int c) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % c; n     a = 1LL * a * a % c; n     b >>= 1; n   } n   return ans; n } n pair<int, int> a[100005]; n int main() { n   int ca = 0, T, i, j, k, m, n, K; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &m); n       int mx = 0; n       for (int i = 0; i < m; i++) scanf( %d , &k), k -= i, Max(mx, k); n       a[i] = {mx, m}; n     } n     sort(a, a + n); n     int ans = 0; n     long long cur = 0; n     for (int i = 0; i < n; i++) { n       if (cur >= a[i].first) { n         cur += a[i].second; n       } else { n         ans += a[i].first - cur; n         cur = a[i].first + a[i].second; n       } n     } n     printf( %d n , ans + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct __timestamper {}; n const int MAXN = 100100; n int val[MAXN]; n long long dp[MAXN][4]; n vector<int> g[MAXN]; n void dfs(int v, int p) { n   for (int u : g[v]) n     if (u != p) dfs(u, v); n   long long max0 = 0, smax0 = 0, tmax0 = 0; n   int max0v = -1, smax0v = -1; n   long long max1 = 0, smax1 = 0; n   int max1v = -1; n   long long max2 = 0, smax2 = 0; n   int max2v = -1; n   long long max3 = 0, smax3 = 0; n   for (int u : g[v]) n     if (u != p) { n       long long t = dp[u][0]; n       int tv = u; n       if (t > max0) { n         swap(t, max0); n         swap(max0v, tv); n       } n       if (t > smax0) { n         swap(t, smax0); n         swap(smax0v, tv); n       } n       if (t > tmax0) { n         swap(t, tmax0); n       } n       t = dp[u][1]; n       if (t > max1) { n         swap(t, max1); n         max1v = u; n       } n       if (t > smax1) swap(t, smax1); n       t = dp[u][2]; n       if (t > max2) { n         swap(t, max2); n         max2v = u; n       } n       if (t > smax2) swap(t, smax2); n       t = dp[u][3]; n       if (t > max3) { n         swap(t, max3); n       } n       if (t > smax3) swap(t, smax3); n     } n   dp[v][0] = max0 + val[v]; n   dp[v][1] = max(max1, max0 + smax0 + val[v]); n   dp[v][2] = max2 + val[v]; n   if (max0v == max1v) { n     dp[v][2] = max(dp[v][2], max(max0 + smax1, max1 + smax0) + val[v]); n   } else { n     dp[v][2] = max(dp[v][2], max0 + max1 + val[v]); n   } n   dp[v][3] = max3; n   dp[v][3] = max(dp[v][3], max1 + smax1); n   if (max0v == max2v) { n     dp[v][3] = max(dp[v][3], max(max0 + smax2, max2 + smax0) + val[v]); n   } else { n     dp[v][3] = max(dp[v][3], max0 + max2 + val[v]); n   } n   for (int u : g[v]) n     if (u != p) { n       long long cur = val[v] + dp[u][1]; n       if (u == max0v) { n         cur += smax0 + tmax0; n       } else if (u == smax0v) { n         cur += max0 + tmax0; n       } else { n         cur += max0 + smax0; n       } n       dp[v][3] = max(dp[v][3], cur); n     } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &val[i]); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a, --b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(0, -1); n   printf( %lld n , *max_element(dp[0], dp[0] + 4)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 7e4; n const int INF = 1e9; n struct d { n   int ad, xr; n } a[N]; n stack<int> s; n vector<pair<int, int>> v; n int n; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n; n   int A, B; n   for (int i = 0; i < n; ++i) { n     cin >> A >> B; n     if (A == 0) continue; n     if (A == 1) s.push(i); n     a[i] = {A, B}; n   } n   while (!s.empty()) { n     int x = s.top(); n     s.pop(); n     if (a[x].ad == 1) { n       v.push_back({x, a[x].xr}); n       a[x].ad--; n       --a[a[x].xr].ad; n       a[a[x].xr].xr ^= x; n       if (a[a[x].xr].ad == 1) s.push(a[x].xr); n     } n   } n   cout << v.size() <<   n ; n   for (auto x : v) cout << x.first <<     << x.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n ostream& operator<<(ostream& out, const pair<T1, T2>& p) { n   return (out << p.first <<     << p.second << endl); n } n const int INF = 1000000017; n const long long LLINF = 1000000000000000017LL; n const long double inf = 1.0 / 0.0; n const long double eps = 1e-10; n const int MOD = 1e9 + 7; n const int MOD2 = 1e9 + 9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     int c, d; n     cin >> c >> d; n     if (c == d) { n       if (c == 0) { n         cout << 0; n       } else { n         cout << 1; n       } n     } else if (abs(c - d) % 2 == 0) { n       cout << 2; n     } else { n       cout << -1; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3, MOD = 1e9 + 7, UNDEF = -1; n int readInt() { n   bool minus = false; n   int result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus = true; n   else n     result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = result * 10 + (ch -  0 ); n   } n   if (minus) n     return -result; n   else n     return result; n } n char readChar() { n   char result = getchar(); n   while (isspace(result)) { n     result = getchar(); n   } n   return result; n } n char w[N + 1][N + 1]; n pair<pair<short, short>, pair<short, short> > cur; n int n, m; n int dp1[2 * N + 1][2 * N + 1], dp2[2 * N + 1][2 * N + 1]; n int S[2 * N + 1], cnt[2 * N + 1]; n int curx, cury; n bool ispal(string s) { n   string d = s; n   reverse(s.begin(), s.end()); n   return d == s; n } n int o(int x1, int y1) { return x1 + S[x1 + y1] - S[x1 + y1 - 1]; } n void add(int x1, int y1, int x2, int y2) { n   dp1[curx][cury] += dp2[o(x1, y1)][o(x2, y2)]; n   if (dp1[curx][cury] >= MOD) { n     dp1[curx][cury] -= MOD; n   } n } n vector<pair<short, short> > p1[2 * N + 1], p2[2 * N + 1]; n int main() { n   n = readInt(), m = readInt(); n   int s = (n + m - 2) / 2; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cnt[i + j]++; n       w[i][j] = readChar(); n       p1[i + j - 2].push_back(make_pair(i, j)); n       p2[n + m - 2 - (i + j - 2)].push_back(make_pair(i, j)); n     } n   } n   for (int i = 2; i <= n + m; i++) { n     S[i] = S[i - 1] + cnt[i]; n   } n   if (w[1][1] != w[n][m]) { n     cout << 0; n     return 0; n   } n   int ans = n * n + 2 * (n - 1) * n; n   for (int i = s; i >= 0; i--) { n     for (int j = 0; j < p1[i].size(); j++) { n       for (int k = 0; k < p2[i].size(); k++) { n         int x1, y1, x2, y2; n         x1 = p1[i][j].first; n         y1 = p1[i][j].second; n         x2 = p2[i][k].first; n         y2 = p2[i][k].second; n         if (w[x1][y1] == w[x2][y2]) { n           if (x1 <= x2 && y1 <= y2) { n             if (x2 - x1 + y2 - y1 <= 1) { n               dp1[o(x1, y1)][o(x2, y2)] = 1; n             } else { n               curx = o(x1, y1); n               cury = o(x2, y2); n               if (x1 == x2) { n                 add(x1, y1 + 1, x2, y2 - 1); n               } else if (y1 == y2) { n                 add(x1 + 1, y1, x2 - 1, y2); n               } else { n                 add(x1, y1 + 1, x2 - 1, y2); n                 add(x1 + 1, y1, x2, y2 - 1); n                 if (y2 - 1 >= y1 + 1) { n                   add(x1, y1 + 1, x2, y2 - 1); n                 } n                 if (x2 - 1 >= x1 + 1) { n                   add(x1 + 1, y1, x2 - 1, y2); n                 } n               } n             } n           } n         } n       } n     } n     for (int i = 0; i <= 2 * n + m; i++) { n       for (int j = 0; j <= 2 * n + m; j++) { n         dp2[i][j] = dp1[i][j]; n         dp1[i][j] = 0; n       } n     } n   } n   cout << dp2[o(1, 1)][o(n, m)]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   stringstream ss; n   string x, s1, s2, s3; n   long long int a1 = 0, a2 = 0, a3 = 0, sum = -1, q = 0; n   cin >> x; n   int z = x.size(); n   for (int i = 1; i < (z - 1); i++) { n     for (int j = 1; j < (z - i); j++) { n       q = 0; n       stringstream ss1, ss2, ss3; n       s1 = x.substr(0, i); n       s2 = x.substr(i, j); n       s3 = x.substr((i + j), (z - i - j)); n       if ((s1[0] ==  0  && s1.size() > 1) || (s2[0] ==  0  && s2.size() > 1) || n           (s3[0] ==  0  && s3.size() > 1)) n         continue; n       { n         ss1 << s1; n         ss1 >> a1; n         ss1.clear(); n         ss2 << s2; n         ss2 >> a2; n         ss2.clear(); n         ss3 << s3; n         ss3 >> a3; n         ss3.clear(); n         if (a1 > 1000000 || a2 > 1000000 || a3 > 1000000) continue; n         q = a1 + a2 + a3; n         if (q > sum) sum = q; n       } n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n const int MOD = 998244353; n int prefix[505]; n int dp[505]; n void addmod(int& x, int y) { x = (x + y) % MOD; } n int main() { n   int N, K; n   scanf( %d %d , &N, &K); n   dp[0] = 1; n   for (int k = 1; k <= N; k++) { n     for (int i = 1; i <= N; i++) { n       dp[i] = 0; n       for (int j = 1; j <= std::min(i, k); j++) { n         addmod(dp[i], dp[i - j]); n       } n     } n     prefix[k] = dp[N]; n   } n   int j = N; n   int total = 0; n   for (int i = 1; i <= N; i++) { n     while (i * j >= K) j--; n     addmod(total, int64_t(prefix[i] + MOD - prefix[i - 1]) * prefix[j] % MOD); n   } n   printf( %d n , total * 2 % MOD); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, n, l, miao, lala; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   cin.ignore(); n   while (n--) { n     char s[105]; n     miao = 0; n     lala = 0; n     cin.getline(s, 105); n     int l = strlen(s); n     if (l > 4 && s[0] ==  m  && s[1] ==  i  && s[i + 2] ==  a  && n         s[i + 3] ==  o  && s[i + 4] ==  . ) n       miao = 1; n     if (l > 4 && s[l - 5] ==  l  && s[l - 4] ==  a  && s[l - 3] ==  l  && n         s[l - 2] ==  a  && s[l - 1] ==  . ) n       lala = 1; n     if (miao == 1 && lala == 1 || miao == 0 && lala == 0) n       cout <<  OMG>.< I don t know! n ; n     else if (miao == 1) n       cout <<  Rainbow s n ; n     else if (lala == 1) n       cout <<  Freda s n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int kval(int a) { n   if (a == 0) return 10; n   int kvala = a; n   if (kvala % 10) n     kvala = 10 + ((kvala / 10) * 10) - kvala; n   else n     kvala = 0; n   return kvala; n } n int compare(int a, int b) { n   if (kval(a) >= kval(b)) n     return 0; n   else n     return 1; n } n int main() { n   int num, k, t; n   vector<int> v2; n   cin >> num >> t; n   long long sum = 0; n   for (int i = 0; i < num; i++) { n     cin >> k; n     sum = sum + (k / 10); n     if (k < 100) v2.push_back(k); n   } n   int i; n   int size = v2.size(); n   sort(v2.begin(), v2.end(), compare); n   for (i = 0; i < size; i++) { n     if (v2[i] % 10) { n       if (kval(v2[i]) <= t) { n         t = t - kval(v2[i]); n         v2[i] += kval(v2[i]); n         sum++; n       } else n         break; n     } n   } n   if (i == size) { n     sort(v2.begin(), v2.end()); n     for (int i = size - 1; i >= 0; i--) { n       if (v2[i] < 100) { n         int val = v2[i] + t; n         if (val >= 100) val = 100; n         sum += ((val - v2[i]) / 10); n         t = t - (val - v2[i]); n       } else n         continue; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int z; n struct pi { n   bool operator()(const pair<int, int>& p, const pair<int, int>& q) { n     if (0) { n     } else if (abs(p.first - z) < abs(q.first - z)) { n       return 1; n     } else if (abs(p.first - z) > abs(q.first - z)) { n       return 0; n     } else { n       if (0) { n       } else if (p.first < q.first) { n         return 0; n       } else if (p.first > q.first) { n         return 1; n       } else { n         if (0) { n         } else if (p.first > z) { n           if (p.second < q.second) { n             return 1; n           } else { n             return 0; n           } n         } else if (p.first < z) { n           if (p.second < q.second) { n             return 0; n           } else { n             return 1; n           } n         } else { n           return 0; n         } n       } n     } n   } n }; n int main() { n   int i; n   int n, k; n   while (cin >> n >> k) { n     string s[10]; n     cin >> s[0]; n     for (i = 1; i < 10; ++i) s[i] = s[0]; n     pair<int, int> a[10][10100]; n     for (i = 0; i < n; ++i) a[0][i] = make_pair(s[0][i] -  0 , i); n     for (i = 1; i < 10; ++i) copy(a[0], a[0] + n, a[i]); n     int mn = 1 << 30, mnz; n     for (z = 0; z < 10; ++z) { n       sort(a[z], a[z] + n, pi()); n       int sm = 0; n       for (i = 0; i < k; ++i) { n         sm += abs(a[z][i].first - z); n         s[z][a[z][i].second] =  0  + z; n       } n       if (0) { n       } else if (mn > sm) { n         mn = sm; n         mnz = z; n       } else if (mn < sm) { n       } else { n         if (s[mnz] > s[z]) mnz = z; n       } n     } n     cout << mn << endl; n     cout << s[mnz] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n], b[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     for (long long i = 0; i < n; i++) cin >> b[i]; n     sort(a, a + n); n     sort(b, b + n); n     for (long long i = 0; i < n; i++) { n       cout << a[i] <<    ; n     } n     cout << endl; n     for (long long i = 0; i < n; i++) cout << b[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, y; n long long c; n string s; n long long ans = 1e16; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> x >> y; n   cin >> s; n   s =  1  + s; n   for (long long i = 1; i <= n; i++) n     if (s[i] ==  0  and s[i - 1] ==  1 ) c++; n   for (long long i = 0; i < c; i++) ans = min(ans, i * x + (c - i) * y); n   if (c == 0) ans = 0; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long N = 2005, inf = 1e15; n long long dp[N][N], suffix1[N][26], suffix2[N][26]; n long long n; n string s, t; n long long dpsolve(long long i, long long j) { n   if (j == 0) { n     return 0; n   } n   if (dp[i][j] != -1) return dp[i][j]; n   long long ans = inf; n   if (i > 0) { n     ans = min(ans, 1 + dpsolve(i - 1, j)); n     if (s[i] == t[j]) ans = min(ans, dpsolve(i - 1, j - 1)); n   } n   if (suffix1[i + 1][t[j] -  a ] >= suffix2[j][t[j] -  a ]) n     ans = min(ans, dpsolve(i, j - 1)); n   dp[i][j] = ans; n   return ans; n } n void solve() { n   cin >> n; n   cin >> s; n   cin >> t; n   s =     + s; n   t =     + t; n   for (long long i = n; i >= 1; i--) { n     for (long long j = 0; j < 26; j++) { n       suffix1[n + 1][j] = suffix2[n + 1][j] = 0; n       suffix1[i][j] = (s[i] -  a  == j) + suffix1[i + 1][j]; n       suffix2[i][j] = (t[i] -  a  == j) + suffix2[i + 1][j]; n     } n   } n   for (long long i = 0; i <= n; i++) n     for (long long j = 0; j <= n; j++) dp[i][j] = -1; n   bool flag = true; n   for (long long j = 0; j < 26; j++) n     if (suffix1[1][j] != suffix2[1][j]) flag = false; n   if (!flag) n     cout <<  -1 n ; n   else { n     long long ans = dpsolve(n, n); n     cout << ans <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105; n bool dmy[MAXN][MAXN][MAXN]; n void dfs(int d, int m, int y, int bd, int bm, int by) { n   if (dmy[bd][bm][by]) return; n   dmy[bd][bm][by] = true; n   if (bm > 12) return; n   if (bd > 31) return; n   if (!(by % 4) && bm == 2 && bd > 29) return; n   if ((by % 4) && bm == 2 && bd >= 29) return; n   if (y - by == 18) { n     if (bm < m) { n       puts( YES ); n       exit(0); n     } else { n       if (bm == m && bd <= d) { n         puts( YES ); n         exit(0); n       } n     } n   } n   if (y - by > 18) { n     puts( YES ); n     exit(0); n   } n   dfs(d, m, y, by, bd, bm); n   dfs(d, m, y, bm, by, bd); n   dfs(d, m, y, bd, by, bm); n   dfs(d, m, y, by, bm, bd); n   dfs(d, m, y, bm, bd, by); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int d, m, y, bd, bm, by; n   string s; n   cin >> s; n   d = (s[0] -  0 ) * 10 + (s[1] -  0 ); n   m = (s[3] -  0 ) * 10 + (s[4] -  0 ); n   y = (s[6] -  0 ) * 10 + (s[7] -  0 ); n   cin >> s; n   bd = (s[0] -  0 ) * 10 + (s[1] -  0 ); n   bm = (s[3] -  0 ) * 10 + (s[4] -  0 ); n   by = (s[6] -  0 ) * 10 + (s[7] -  0 ); n   dfs(d, m, y, by, bd, bm); n   dfs(d, m, y, bm, by, bd); n   dfs(d, m, y, bd, by, bm); n   dfs(d, m, y, by, bm, bd); n   dfs(d, m, y, bm, bd, by); n   dfs(d, m, y, bd, bm, by); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n long long n, sum, arr[N], cnt; n int main() { n   scanf( %lld , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &arr[i]); n     sum += arr[i]; n   } n   sort(arr, arr + n); n   for (int i = 0; i < n - 1; ++i) { n     if (arr[i] == arr[i + 1]) { n       cnt++; n       if (i && arr[i - 1] == arr[i] - 1) return puts( cslnb ), 0; n       if (cnt > 1) return puts( cslnb ), 0; n       if (arr[i] == 0) return puts( cslnb ), 0; n     } n   } n   sum -= n * (n - 1) / 2; n   if (sum & 1) n     puts( sjfnb ); n   else n     puts( cslnb ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   if (a > b) swap(a, b); n   if (b - a >= c) { n     printf( %d n , (a + c) * 2); n     return 0; n   } n   if (b - a < c) { n     c -= b - a; n     printf( %d n , b * 2 + (c / 2) * 2); n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[200050]; n int b[200050]; n int c[200050]; n int now[200050]; n int tot[200050]; n int ans[200050]; n int n, m, cnt, p; n int main() { n   int i, j; n   while (scanf( %d %d %d , &n, &m, &p) != EOF) { n     for (i = 0; i < n; i++) scanf( %d , a + i); n     for (i = 0; i < m; i++) scanf( %d , b + i); n     if (1 + (long long)p * (m - 1) > n) { n       printf( 0 n ); n       continue; n     } n     sort(b, b + m); n     for (i = 0; i < m; i++) c[i] = b[i]; n     int sz = unique(c, c + m) - c; n     for (i = 0; i < sz; i++) { n       tot[i] = upper_bound(b, b + m, c[i]) - lower_bound(b, b + m, c[i]); n     } n     cnt = 0; n     for (i = 0; i < p; i++) { n       memset(now, 0, sizeof(now)); n       int l, r; n       l = i; n       r = i - p; n       while (r < n) { n         r += p; n         if (r >= n) continue; n         int k = lower_bound(c, c + sz, a[r]) - c; n         if (a[r] != c[k]) { n           while (l != r) { n             int kk = lower_bound(c, c + sz, a[l]) - c; n             now[kk] = 0; n             l += p; n           } n           l += p; n           continue; n         } n         now[k]++; n         if (now[k] > tot[k]) { n           while (now[k] > tot[k]) { n             int kk = lower_bound(c, c + sz, a[l]) - c; n             now[kk]--; n             l += p; n           } n         } n         if ((r - l) / p + 1 == m) { n           ans[cnt++] = l; n           int kk = lower_bound(c, c + sz, a[l]) - c; n           now[kk]--; n           l += p; n         } n       } n     } n     printf( %d n , cnt); n     if (cnt) { n       sort(ans, ans + cnt); n       printf( %d , ans[0] + 1); n       for (i = 1; i < cnt; i++) printf(  %d , ans[i] + 1); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int Maxn = 500010; n using namespace std; n multiset<long long> S; n int n, m; n long long t[Maxn]; n void add(int pos, int x) { n   for (; pos <= m; pos += pos & -pos) { n     t[pos] += x; n   } n } n long long query(int pos) { n   long long ans = 0; n   for (; pos; pos ^= pos & -pos) ans += t[pos]; n   return ans; n } n char c[Maxn]; n int num[Maxn], b[Maxn]; n int Get_ans() { n   if (S.size() == 0) return 0; n   multiset<long long>::iterator x = S.begin(), y; n   int cnt = 0; n   while (x != S.end()) { n     int pos = lower_bound(b + 1, b + m + 1, *x) - b; n     if (x != S.begin()) { n       long long sum = query(pos - 1); n       if (sum * 2 < *x) cnt++; n     } n     long long sum = query(pos); n     x = S.upper_bound(sum * 2); n     if (x == S.end()) break; n   } n   return S.size() - cnt - 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     cin >> c[i] >> num[i]; n     b[i] = num[i]; n   } n   sort(b + 1, b + n + 1); n   m = unique(b + 1, b + n + 1) - b - 1; n   for (int i = 1; i <= n; i++) { n     int w = lower_bound(b + 1, b + m + 1, num[i]) - b; n     if (c[i] ==  + ) { n       S.insert(num[i]); n       add(w, num[i]); n       printf( %d n , Get_ans()); n     } else { n       multiset<long long>::iterator ite = S.find(num[i]); n       S.erase(ite); n       add(w, -num[i]); n       printf( %d n , Get_ans()); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec, ves; n int main() { n   int n, m, f = 0, w = 0; n   bool p = false, t = false; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> m; n     if (m == 0) t = true; n     for (int j = 0; j < vec.size(); j++) { n       p = false; n       if (m == vec[j]) { n         ves[j]++; n         p = true; n         break; n       } n     } n     if (p == false) { n       vec.push_back(m); n       ves.push_back(1); n     } n   } n   for (int i = 0; i < vec.size(); i++) { n     if (vec[i] != 0 && ves[i] > 2) { n       cout << -1; n       return 0; n     } else if (ves[i] == 2 && vec[i] != 0) n       w++; n   } n   cout << w; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> q[200010], g[200010]; n int n, m, a[200010], b[200010]; n unsigned long long A, B, C, ans; n unsigned long long calc(unsigned long long a, unsigned long long b, n                         unsigned long long c) { n   unsigned long long s[3] = {a, b, c}; n   sort(s, s + 3); n   return s[0] * A + s[1] * B + s[2] * C; n } n int main() { n   scanf( %d%d , &n, &m); n   scanf( %lld%lld%lld , &A, &B, &C); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &a[i], &b[i]); n     if (a[i] > b[i]) swap(a[i], b[i]); n     q[a[i]].push_back(b[i]), q[b[i]].push_back(a[i]); n   } n   for (int i = 0; i < n; i++) { n     ans += (unsigned long long)i * A * n            ((unsigned long long)(n - i - 1) * (n - i - 2) / 2LL); n     ans += (unsigned long long)i * B * ((unsigned long long)(i) * (n - i - 1)); n     ans += n         (unsigned long long)i * C * ((unsigned long long)(i) * (i - 1) / 2LL); n   } n   for (int i = 1; i <= m; i++) { n     ans -= (unsigned long long)a[i] * a[i] * B + n            (unsigned long long)a[i] * b[i] * C + n            (unsigned long long)a[i] * (a[i] - 1) / 2LL * A; n     ans -= (unsigned long long)(b[i] - a[i] - 1) * a[i] * A + n            (unsigned long long)(b[i] - a[i] - 1) * b[i] * C + n            (unsigned long long)(b[i] - a[i] - 1) * (a[i] + b[i]) / 2LL * B; n     ans -= (unsigned long long)(n - b[i] - 1) * a[i] * A + n            (unsigned long long)(n - b[i] - 1) * b[i] * B + n            (unsigned long long)(n - b[i] - 1) * (n + b[i]) / 2LL * C; n   } n   for (int i = 0; i < n; i++) sort(q[i].begin(), q[i].end()); n   for (int i = 0; i < n; i++) { n     unsigned long long pre = 0, pre_tot = 0; n     for (int j = 0; j < q[i].size(); j++) { n       int to = q[i][j]; n       if (to > i) continue; n       ans += pre_tot * i * C + pre * A + to * pre_tot * B; n       pre += to; n       pre_tot++; n     } n     unsigned long long nex = 0, nex_tot = 0; n     for (int j = 0; j < q[i].size(); j++) { n       int to = q[i][j]; n       if (to < i) continue; n       ans += nex_tot * i * A + nex * B + to * nex_tot * C; n       nex += to; n       nex_tot++; n     } n     ans += pre_tot * nex_tot * i * B + pre_tot * nex * C + nex_tot * pre * A; n   } n   for (int i = 1; i <= m; i++) { n     if (q[a[i]].size() < q[b[i]].size()) n       g[a[i]].push_back(b[i]); n     else n       g[b[i]].push_back(a[i]); n   } n   for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end()); n   for (int i = 1; i <= m; i++) { n     int t1 = 0, t2 = 0; n     while (t1 < g[a[i]].size() && t2 < g[b[i]].size()) { n       if (g[a[i]][t1] < g[b[i]][t2]) n         t1++; n       else if (g[a[i]][t1] > g[b[i]][t2]) n         t2++; n       else { n         ans -= calc(a[i], b[i], g[a[i]][t1]); n         t1++, t2++; n       } n     } n   } n   printf( %llu n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100005]; n int main() { n   ; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   if (a[n - 1] == 1) n     a[n - 1] = 2; n   else n     a[n - 1] = 1; n   sort(a, a + n); n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2010; n int n; n string s; n int a[N], tot, vis[N]; n bitset<N> d, ba[N], sel[N]; n void Trans(string s) { n   reverse(s.begin(), s.end()); n   while (s.size() % 8) s +=  0 ; n   int L = s.size() / 8; n   for (int i = 0; i < L; i++) { n     a[i] = 0; n     for (int j = 7; ~j; j--) a[i] = a[i] * 10 + s[i * 8 + j] -  0 ; n   } n   long long S = 0; n   tot = 0; n   while (L) { n     S = 0; n     for (int i = L - 1; ~i; i--) { n       S = S * 100000000LL + a[i]; n       a[i] = S >> 32; n       S %= (1LL << 32); n     } n     for (int i = 0; i < 32; i++) d[tot++] = ((S >> i) & 1); n     while (L && a[L - 1] == 0) --L; n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     Trans(s); n     bitset<N> ns; n     int flg = 1; n     for (int j = tot - 1; ~j; j--) n       if (d[j]) n         if (vis[j]) { n           d ^= ba[j]; n           ns ^= sel[j]; n         } else { n           flg = 0; n           ns.flip(i); n           ba[j] = d; n           sel[j] = ns; n           vis[j] = 1; n           break; n         } n     if (!flg) { n       cout << 0 << endl; n       continue; n     } else { n       cout << ns.count() <<    ; n       for (int i = 0; i < N; i++) n         if (ns[i]) cout << i <<    ; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const long long mod = 1000000007; n const int N = 201000; n int n, y[N]; n double k; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &y[i]); n   int b = -1; n   for (int i = 2; i < n; ++i) n     if ((long long)(y[i] - y[0]) * (i - 1) != (long long)(y[i] - y[1]) * i) { n       b = i; n       break; n     } n   if (~b) { n     bool ans = 0, tans = 1; n     for (int i = 2; i < n; ++i) n       if (i != b) { n         bool t = n             (long long)(y[i] - y[0]) * (i - 1) != (long long)(y[i] - y[1]) * i; n         bool s = (long long)(y[i] - y[b]) != (long long)(y[1] - y[0]) * (i - b); n         if (s && t) { n           tans = 0; n           break; n         } n       } n     ans |= tans; n     tans = 1; n     for (int i = 2; i < n; ++i) n       if (i != b) { n         bool t = n             (long long)(y[i] - y[0]) * (i - b) != (long long)(y[i] - y[b]) * i; n         bool s = n             (long long)(y[i] - y[1]) * b != (long long)(y[b] - y[0]) * (i - 1); n         if (s && t) { n           tans = 0; n           break; n         } n       } n     ans |= tans; n     tans = 1; n     for (int i = 2; i < n; ++i) n       if (i != b) { n         bool t = (long long)(y[i] - y[1]) * (i - b) != n                  (long long)(y[i] - y[b]) * (i - 1); n         bool s = n             (long long)(y[i] - y[0]) * (b - 1) != (long long)(y[b] - y[1]) * i; n         if (s && t) { n           tans = 0; n           break; n         } n       } n     ans |= tans; n     puts(ans ?  Yes  :  No ); n   } else { n     puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n inline T lowbit(T x) { n   return x & -x; n } n template <typename S, typename T> n void debug(S s, T t) { n   cerr << s <<   ==   << t <<   n ; n } n template <typename T> n void debug(T t) { n   cerr << t <<   n ; n } n template <typename T> n void debug(T t[], long long st, long long ed) { n   for (long long i = st; i <= ed; ++i) { n     cerr << t[i] <<    ; n   } n   cerr <<   n ; n } n template <typename T> n void debug(const vector<T>& t) { n   for (long long i = 0; i < t.size(); ++i) cerr << t[i] <<    ; n   cerr <<   n ; n } n const long long N = 2e5 + 10; n void solve() { n   long long x, y; n   cin >> x >> y; n   if (y < x) n     cout << x + y <<   n ; n   else if (x == y) n     cout << x <<   n ; n   else n     cout << y - (y % x) / 2 <<   n ; n } n signed main() { n   long long _; n   cin >> _; n   while (_--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], tmp[100005], b[100005]; n bool die[100005]; n int n, m; n vector<long long> candidate; n bool solve(int pos) { n   candidate.clear(); n   for (int i = 1; i <= n; i++) n     if ((!die[i]) && (a[i] & (1ll << pos))) { n       candidate.push_back(a[i]); n       die[i] = true; n     } n   for (int i = 1; i <= m; i++) tmp[i] = b[i]; n   int len_tmp = m; n   int cur = 1; n   m = 0; n   for (int i = 0; i < candidate.size(); i++) { n     b[++m] = candidate[i]; n     while (cur <= len_tmp && !(tmp[cur] & (1ll << pos))) { n       b[++m] = tmp[cur]; n       cur++; n     } n     if (cur > len_tmp && i != candidate.size() - 1) return false; n     b[++m] = tmp[cur]; n     cur++; n   } n   while (cur <= len_tmp) { n     b[++m] = tmp[cur]; n     cur++; n   } n   return true; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 59; i >= 0; i--) n     if (!solve(i)) { n       printf( No ); n       return 0; n     } n   cout <<  Yes n ; n   for (int i = 1; i <= n; i++) cout << b[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b, long long m = 998244353) { n   a %= m; n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long bpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long modinv(long long a) { return modpow(a, 998244353 - 2, 998244353); } n void graph(vector<vector<int> > &adj, int m) { n   int x, y; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     adj[x - 1].push_back(y - 1); n     adj[y - 1].push_back(x - 1); n   } n } n const int N = 23; n struct NTT { n   int root, root_1, root_pw = 1 << N, c = (998244353 - 1) >> N; n   NTT(bool init = true) { n     if (init) { n       vector<int> pf; n       int n = 998244353 - 1, phi = n; n       for (int i = 2; i * i <= n; i++) { n         if (n % i == 0) { n           pf.push_back(i); n           while (n % i == 0) n /= i; n         } n       } n       if (n > 1) pf.push_back(n); n       for (int r = 2; r <= phi; r++) { n         bool ok = true; n         for (auto &x : pf) { n           if (modpow(r, phi / x) == 1) { n             ok = false; n             break; n           } n         } n         if (ok) { n           root = modpow(r, c), root_1 = modinv(root); n           break; n         } n       } n     } n   } n   void transform(vector<int> &a, bool invert) { n     int n = a.size(); n     for (int i = 1, j = 0; i < n; i++) { n       int bit = n >> 1; n       for (; j & bit; bit >>= 1) j ^= bit; n       j ^= bit; n       if (i < j) swap(a[i], a[j]); n     } n     for (int len = 2; len <= n; len <<= 1) { n       int wlen = invert ? root_1 : root; n       for (int i = len; i < root_pw; i <<= 1) n         wlen = (int)(1LL * wlen * wlen % 998244353); n       for (int i = 0; i < n; i += len) { n         int w = 1; n         for (int j = 0; j < len / 2; j++) { n           int u = a[i + j], v = (int)(1LL * a[i + j + len / 2] * w % 998244353); n           a[i + j] = u + v < 998244353 ? u + v : u + v - 998244353; n           a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + 998244353; n           w = (int)(1LL * w * wlen % 998244353); n         } n       } n     } n     if (invert) { n       int n_1 = modinv(n); n       for (int &x : a) x = (int)(1LL * x * n_1 % 998244353); n     } n   } n   vector<int> multiply(vector<int> const &a, vector<int> const &b) { n     vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end()); n     int n = 1, total = a.size() + b.size(); n     while (n < total) n <<= 1; n     fa.resize(n); n     fb.resize(n); n     transform(fa, false); n     transform(fb, false); n     for (int i = 0; i < n; i++) fa[i] = (fb[i] * 1LL * fa[i]) % 998244353; n     transform(fa, true); n     fa.resize(total - 1); n     return fa; n   } n }; n NTT ntt; n vector<int> exp(vector<int> &a, int n) { n   if (n == 1) return a; n   vector<int> res = exp(a, n / 2); n   res = ntt.multiply(res, res); n   if (n & 1) { n     res = ntt.multiply(res, a); n   } n   return res; n } n void solve() { n   int n, k, x; n   cin >> n >> k; n   vector<int> coeff(10, 0); n   for (int i = 0; i < k; i++) cin >> x, coeff[x] = 1; n   x = n / 2; n   vector<int> power = exp(coeff, x); n   long long ans = 0; n   ; n   for (auto &k : power) ans = (ans + k * 1LL * k) % 998244353; n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1, tt = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int st, en; n } TV[200010]; n bool cmp(node a, node b) { n   if (a.st == b.st) { n     return a.en < b.en; n   } n   return a.st < b.st; n } n int main() { n   int n; n   int tv1, tv2; n   int flag; n   cin >> n; n   tv1 = -1; n   tv2 = -1; n   flag = 1; n   for (int i = 0; i < n; i++) { n     cin >> TV[i].st >> TV[i].en; n   } n   sort(TV, TV + n, cmp); n   for (int i = 0; i < n && flag; i++) { n     if (tv1 < TV[i].st) { n       tv1 = TV[i].en; n     } else { n       if (tv2 < TV[i].st) { n         tv2 = TV[i].en; n       } else { n         flag = 0; n       } n     } n   } n   if (flag) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n ifstream fin( input.txt ); n ofstream fout( output.txt ); n bool isPrime(int x) { n   if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; n   for (int i = 5; i * i <= x; i += 6) n     if (x % i == 0 || x % (i + 2) == 0) return 0; n   return 1; n } n int NumDigits(int x) { n   return ( n       x < 10 n           ? 1 n           : (x < 100 n                  ? 2 n                  : (x < 1000 n                         ? 3 n                         : (x < 10000 n                                ? 4 n                                : (x < 100000 n                                       ? 5 n                                       : (x < 1000000 n                                              ? 6 n                                              : (x < 10000000 n                                                     ? 7 n                                                     : (x < 100000000 n                                                            ? 8 n                                                            : (x < 1000000000 n                                                                   ? 9 n                                                                   : 10))))))))); n } n inline void boostIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n } n long long int Pow10[19] = { n     (long long int)1,    (long long int)1e1,  (long long int)1e2, n     (long long int)1e3,  (long long int)1e4,  (long long int)1e5, n     (long long int)1e6,  (long long int)1e7,  (long long int)1e8, n     (long long int)1e9,  (long long int)1e10, (long long int)1e11, n     (long long int)1e12, (long long int)1e13, (long long int)1e14, n     (long long int)1e15, (long long int)1e16, (long long int)1e17, n     (long long int)1e18}; n long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n long long int lcm(long long int a, long long int b) { n   return a * b / gcd(a, b); n } n long long Mod = 998244353; n template <class T> n struct Fenwick { n   vector<T> V; n   Fenwick(long long n) : V(n) {} n   void add(size_t i, T x) { n     for (; i < V.size(); i |= i + 1) V[i] += x; n   } n   T sum(long long i) { n     T r = T(); n     for (--i; i >= 0; i = (i & (i + 1)) - 1) r += V[i]; n     return r; n   } n   T sum(long long l, long long r) { return sum(r) - sum(l); } n }; n long long mul(long long a, long long b, long long c = 1, long long d = 1, n               long long e = 1, long long g = 1) { n   a %= Mod; n   b %= Mod; n   c %= Mod; n   d %= Mod; n   e %= Mod; n   g %= Mod; n   return (a * b % Mod * c % Mod * d % Mod * e % Mod * g % Mod); n } n long long Pow(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 0) { n     long long x = Pow(a, b / 2); n     return mul(x, x); n   } else { n     long long x = Pow(a, b / 2); n     return mul(a, x, x); n   } n } n long long inf = 1e18; n struct My { n   long long a = inf; n   long long b = inf; n   long long solved = 0; n   long long fee = 0; n }; n struct comp { n   bool operator()(My const& a, My const& b) const {} n }; n struct Tree { n   long long n = 1; n   vector<long long> t; n   Tree(long long nn) { n     while (n < nn) n *= 2; n     t.assign(n * 2, 0); n   } n   Tree(vector<long long>& A) { n     while (n < A.size()) n *= 2; n     t.assign(n * 2, 0); n     for (long long i = 0; i < (A.size()); ++i) { n       t[i + n] = A[i]; n     } n     for (long long i = A.size() - 1; i >= 1; --i) { n       t[i] = t[i + i] + t[i + i + 1]; n     } n   } n   long long get(long long i) { n     long long res = t[i += n]; n     for (i /= 2; i; i /= 2) { n       res += t[i]; n     } n     return res; n   } n   void set(long long l, long long r, long long x) { n     for (l += n, r += n; l <= r; l /= 2, r /= 2) { n       if (l & 1) t[l++] += x; n       ; n       if (~r & 1) t[r--] += x; n     } n   } n }; n long long gcdex(long long a, long long b, long long& x, long long& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = gcdex(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n long long mypow(long long a, long long b = Mod - 2, long long k = Mod) { n   if (b == 0) return 1; n   long long tmp = mypow(a, b / 2, k); n   if ((b % 2) == 1) n     return mul(tmp, tmp, a); n   else n     return mul(tmp, tmp); n } n long long mypow2(long long a, long long m = Mod) { n   long long x, y; n   long long g = gcdex(a, m, x, y); n   x = (x % m + m) % m; n   return x; n } n int32_t main() { n   boostIO(); n   long long T; n   cin >> T; n   for (long long t = 0; t < T; ++t) { n     long long n; n     cin >> n; n     vector<long long> A(n); n     long long Sum = 0; n     long long Xor = 0; n     for (long long i = 0; i < (n); ++i) { n       cin >> A[i]; n       Sum += A[i]; n       Xor ^= A[i]; n     } n     vector<long long> ans; n     bitset<64> B = Xor; n     long long Add = 0; n     for (long long i = 1; i < 55; ++i) { n       if (B[i] == 0) { n         Add += (1LL << i); n       } n     } n     if (B[0] == 1) Add += 1; n     ans.push_back(Add); n     Sum += Add; n     Xor ^= Add; n     long long dif = Xor * 2 - Sum; n     ans.push_back(dif / 2); n     ans.push_back(dif / 2); n     Sum += dif / 2; n     Sum += dif / 2; n     Xor ^= dif / 2; n     Xor ^= dif / 2; n     if (Xor * 2 != Sum) { n       cerr <<  ERROR  <<   n ; n       return 0; n     } n     cerr <<  ans =  ; n     cout << ans.size() <<   n ; n     for (auto x : ans) { n       cout << x <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n const int Maxn=2021; n const int Mod=998244353; n int C[Maxn<<1|5][Maxn<<1|5]; n int w[Maxn+5][Maxn+5]; n int sum[Maxn+5][Maxn+5]; n int ans; n void init(){ n  C[0][0]=1; n  for(int i=1;i<=(Maxn<<1);i++){ n   C[i][0]=C[i][i]=1; n   for(int j=1;j<i;j++){ n    C[i][j]=(C[i-1][j]+C[i-1][j-1])%Mod; n   } n  } n } n void solve(int n,int m,int k){ n  for(int i=1;i<=n;i++){ n   for(int j=1;j<=m;j++){ n    int t_1=C[i-1+m-j][m-j],t_2=C[i+j-1][j-1]; n    w[i][j]=1ll*t_1*t_2%Mod; n    sum[i][j]=(sum[i][j-1]+w[i][j])%Mod; n   } n  } n  for(int i=1;i<n;i++){ n   for(int j=1;j<m;j++){ n    ans=(ans+1ll*w[i][j]*sum[n-i][m-j])%Mod; n    if(k==1){ n     ans=(ans-1ll*w[i][j]*w[n-i][m-j]%Mod+Mod)%Mod; n    } n   } n  } n } n int main(){ n  init(); n  int n,m; n  scanf( %d%d ,&n,&m); n  solve(n,m,1); n  solve(m,n,0); n  ans=(ans<<1)%Mod; n  printf( %d n ,ans); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N_ = 1050; n int N; n int P[N_]; n char S[N_]; n int group[N_]; n int find_group(int x) { n   if (x == group[x]) return x; n   return group[x] = find_group(group[x]); n } n void merge_group(int a, int b) { n   a = find_group(a); n   b = find_group(b); n   if (a != b) group[a] = b; n } n vector<int> pos[N_]; n int cnt[N_]; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) group[i] = i; n   for (int i = 1; i <= N; i++) scanf( %d , &P[i]); n   for (int i = 1; i <= N; i++) { n     scanf( %s , S + 1); n     for (int j = 1; j <= N; j++) n       if (S[j] ==  1 ) merge_group(i, j); n   } n   for (int i = 1; i <= N; i++) pos[find_group(i)].push_back(P[i]); n   for (int i = 1; i <= N; i++) sort(pos[i].begin(), pos[i].end()); n   for (int i = 1; i <= N; i++) { n     int g = find_group(i); n     printf( %d%c , pos[g][cnt[g]++], (i == N) ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b, c, d, n, i, cnt = 0, snt = 0; n float f, flo, fload = 0; n string s, h, o, name; n int main() { n   ios_base::sync_with_stdio(0); n   setlocale(LC_ALL,  Russian ); n   ; n   string t; n   size_t i; n   getline(cin, t,   n ); n   while ((i = t.find(   )) != string::npos) t.erase(i, 1); n   s = t; n   reverse(s.begin(), s.end()); n   for (int i = 0; i < s.size(); i++) s[i] = tolower(s[i]); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] >= 65 and s[i] <= 122) { n       if (s[i] ==  a  or s[i] ==  e  or s[i] ==  u  or s[i] ==  i  or n           s[i] ==  y  or s[i] ==  o ) { n         cout <<  YES ; n         ; n         return 0; n         ; n       } else { n         cout <<  NO ; n         ; n         return 0; n         ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = int(3e5) + 5, inf = int(1e9) + 5; n int H[maxn], sz[maxn], start[maxn], en[maxn], res[maxn]; n pair<int, int> st[4 * maxn]; n vector<pair<pair<int, int>, int>> Q; n vector<int> dis, graph[maxn]; n inline int left(int node) { return (node << 1); } n inline int right(int node) { return (node << 1) + 1; } n void upd(int node, int L, int R, int idx, pair<int, int> v) { n   if (L == R) { n     st[node] = v; n   } else { n     if (idx <= (L + R) / 2) n       upd(left(node), L, (L + R) / 2, idx, v); n     else n       upd(right(node), (L + R) / 2 + 1, R, idx, v); n     st[node] = max(st[left(node)], st[right(node)]); n   } n } n pair<int, int> qry(int node, int L, int R, int a, int b) { n   if (a > R || b < L) n     return {-inf, -inf}; n   else if (a <= L && R <= b) n     return st[node]; n   else n     return max(qry(left(node), L, (L + R) / 2, a, b), n                qry(right(node), (L + R) / 2 + 1, R, a, b)); n } n void dfs(int node, int ht) { n   sz[node] = 1, H[node] = ht; n   start[node] = int(dis.size()); n   dis.push_back(node); n   for (auto it : graph[node]) { n     dfs(it, ht + 1); n     sz[node] += sz[it]; n   } n   en[node] = int(dis.size()) - 1; n } n int main(void) { n   int n, q, p, node; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < 4 * maxn; i++) st[i] = {-inf, -inf}; n   for (int i = 1; i < n; i++) { n     scanf( %d , &p); n     p--; n     graph[p].push_back(i); n   } n   dfs(0, 0); n   for (int i = 0; i < q; i++) { n     scanf( %d , &node); n     node--; n     Q.push_back({{sz[node] / 2, node}, i}); n   } n   vector<pair<int, int>> szs; n   for (int i = 0; i < n; i++) szs.push_back({sz[i], i}); n   sort(szs.rbegin(), szs.rend()), sort(Q.rbegin(), Q.rend()); n   int ptr = 0; n   for (auto it : Q) { n     if (ptr != n) { n       while (szs[ptr].first > it.first.first) { n         int node = szs[ptr].second; n         upd(1, 0, int(dis.size()) - 1, start[node], {H[node], node}); n         ptr++; n         if (ptr == n) break; n       } n     } n     res[it.second] = qry(1, 0, int(dis.size()) - 1, start[it.first.second], n                          en[it.first.second]) n                          .second; n   } n   for (int i = 0; i < q; i++) printf( %d n , res[i] + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, a, c1 = 0, c2 = 0; n   vector<int> v; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (x & 1) { n       x /= 2; n       for (int j = 1; j <= x; j++) { n         cin >> a; n         c1 += a; n       } n       cin >> a; n       v.push_back(a); n       for (int j = 1; j <= x; j++) { n         cin >> a; n         c2 += a; n       } n     } else { n       x /= 2; n       for (int j = 1; j <= x; j++) { n         cin >> a; n         c1 += a; n       } n       for (int j = 1; j <= x; j++) { n         cin >> a; n         c2 += a; n       } n     } n   } n   sort(v.begin(), v.end()); n   int i = v.size() - 1; n   while (i >= 0) { n     c1 += v[i]; n     i--; n     if (i == -1) break; n     c2 += v[i]; n     i--; n   } n   cout << c1 <<     << c2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   long long x, y; n   Point() {} n   Point(long long a, long long b) : x(a), y(b) {} n   Point operator-(Point b) { return Point(x - b.x, y - b.y); } n }; n long long cross(Point x, Point y) { return x.x * y.y - x.y * y.x; } n Point st[100005]; n int top; n long long calc(Point x, long long k) { return x.x * k + x.y; } n void insert(Point p) { n   while (top > 1 && cross(p - st[top], st[top] - st[top - 1]) >= 0) top--; n   st[++top] = p; n } n int a[100005], b[100005], num[100005]; n long long up[100005]; n void convex(int n, int m, long long lb, long long rb) { n   long long s = 0; n   for (int i = 1; i <= n; i++) s += a[i]; n   top = 0; n   for (int i = n; i >= 0; i--) { n     insert(Point(n - i, s)); n     s -= a[i]; n   } n   int r = 1; n   for (int i = 1; i <= m; i++) { n     while (r < top && calc(st[r + 1], m - i) <= calc(st[r], m - i)) r++; n     up[i] = max(up[i], lb - calc(st[r], m - i)); n   } n   s = 0; n   for (int i = 1; i <= n; i++) s += b[i]; n   top = 0; n   for (int i = n; i >= 0; i--) { n     insert(Point(n - i, s)); n     s -= b[i]; n   } n   r = 1; n   for (int i = 1; i <= m; i++) { n     while (r < top && calc(st[r + 1], m - i) <= calc(st[r], m - i)) r++; n     up[i] = max(up[i], rb - calc(st[r], m - i)); n   } n } n bool solve1(int n, long long rb) { n   long long s1 = 0, s2 = 0, maxn = 0; n   int last2 = 0; n   for (int i = 1; i <= n; i++) n     if (num[i] != -1) { n       if (maxn > num[i]) return 0; n       s1 += num[i]; n       s2 += (long long)(i - last2 - 1) * (num[i] - maxn); n       if (s1 < up[i]) { n         if (s1 + s2 < up[i]) return 0; n         s2 -= up[i] - s1; n         s1 = up[i]; n       } n       last2 = i; n       maxn = num[i]; n     } else { n       s1 += maxn; n       if (s1 < up[i]) { n         if (s1 + s2 >= up[i]) { n           s2 -= up[i] - s1; n           s1 = up[i]; n         } else { n           long long v = up[i] - s1 - s2, t = (v + i - last2 - 1) / (i - last2); n           maxn += t; n           s1 = up[i]; n           s2 = t * (i - last2) - v; n         } n       } n     } n   return s1 <= rb && (s1 + s2 >= rb || num[n] != -1); n } n int solve2(int n, int k, long long rb) { n   long long s1 = 0, s2 = 0, maxn = 0; n   int last2 = 0; n   for (int i = 1; i <= n - k; i++) n     if (num[i] != -1) { n       s1 += num[i]; n       s2 += (long long)(i - last2 - 1) * (num[i] - maxn); n       if (s1 < up[i]) { n         s2 -= up[i] - s1; n         s1 = up[i]; n       } n       last2 = i; n       maxn = num[i]; n     } else { n       s1 += maxn; n       if (s1 < up[i]) { n         if (s1 + s2 >= up[i]) { n           s2 -= up[i] - s1; n           s1 = up[i]; n         } else { n           long long v = up[i] - s1 - s2, t = (v + i - last2 - 1) / (i - last2); n           maxn += t; n           s1 = up[i]; n           s2 = t * (i - last2) - v; n         } n       } n     } n   long long t = -1; n   for (int i = n - k + 1; i <= n; i++) n     if (num[i] != -1) { n       if (t == -1) n         t = num[i]; n       else if (t != num[i]) n         return -1; n     } n   if (t == -1) t = (rb - s1) / k; n   long long v = rb - (long long)t * k; n   if (s1 > v || maxn > t) return -1; n   return (s1 + s2 + (long long)(n - k - last2) * (t - maxn) >= v) ? t : -1; n } n int main() { n   memset(num, 255, sizeof(num)); n   int n, m; n   scanf( %d%d , &n, &m); n   long long lb = 0, s = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i], &b[i]); n     lb += a[i]; n     s += b[i]; n   } n   sort(a + 1, a + n + 1); n   sort(b + 1, b + n + 1); n   int k; n   scanf( %d , &k); n   for (int i = 1; i <= k; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     num[m - x + 1] = y; n   } n   long long rb = 0; n   scanf( %lld , &rb); n   if (lb > rb || s < rb) { n     puts( -1 -1 ); n     return 0; n   } n   convex(n, m, lb, rb); n   if (!solve1(m, rb)) { n     puts( -1 -1 ); n     return 0; n   } n   int l = 1, r = m; n   while (l < r) { n     int mid = ((l + r) >> 1) + 1; n     if (solve2(m, mid, rb) != -1) n       l = mid; n     else n       r = mid - 1; n   } n   int ans = solve2(m, l, rb); n   printf( %d %d n , l, ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long q; n   scanf( %ld , &q); n   while (q--) { n     long n; n     scanf( %ld , &n); n     long div = n / 4; n     if (n <= 3 || n == 5 || n == 7 || n == 11) { n       puts( -1 ); n       continue; n     } n     if (n & 1) { n       --div; n     } n     printf( %ld n , div); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int mynum(int x, int y) { n   int temp; n   stringstream ss; n   ss << (s.substr(x, y - x)); n   ss >> temp; n   if (temp > 1000000 || (y - x != 1 && s[x] ==  0 ) || x == y) return -1; n   return temp; n } n int main() { n   int mx = -1, x, y, z; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     for (int j = i + 1; j < s.length(); j++) { n       x = mynum(0, i); n       y = mynum(i, j); n       z = mynum(j, s.length()); n       if (x != -1 && y != -1 && z != -1 && x + y + z > mx) mx = x + y + z; n     } n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 * 100 + 5; n int n, l, k; n long long a[maxn]; n void input() { n   scanf( %d %d , &n, &l); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   scanf( %d , &k); n } n long long first() { n   long long javab = 0, MAX = 0, x; n   int y; n   set<pair<long long, int> > second, t; n   for (int i = 0; i < l; i++) n     if (a[i] < 0) second.insert(make_pair(a[i], i)); n   while ((int)second.size() > k) { n     t.insert(*(--second.end())); n     second.erase(--second.end()); n   } n   for (int i = 0; i < l; i++) { n     if (second.find(make_pair(a[i], i)) == second.end()) n       javab += a[i]; n     else n       javab -= a[i]; n   } n   MAX = abs(javab); n   for (int i = l; i < n; i++) { n     if (second.find(make_pair(a[i - l], i - l)) == second.end()) { n       t.erase(make_pair(a[i - l], i - l)); n       javab -= a[i - l]; n     } else { n       javab += a[i - l]; n       second.erase(make_pair(a[i - l], i - l)); n     } n     if (a[i] < 0) { n       javab -= a[i]; n       second.insert(make_pair(a[i], i)); n     } else n       javab += a[i]; n     if ((int)t.size()) { n       x = t.begin()->first; n       y = t.begin()->second; n       javab -= (2 * x); n       second.insert(make_pair(x, y)); n       t.erase(make_pair(x, y)); n     } n     while ((int)second.size() > k) { n       x = (--second.end())->first; n       y = (--second.end())->second; n       t.insert(make_pair(x, y)); n       javab += (2 * x); n       second.erase(make_pair(x, y)); n     } n     MAX = max(MAX, javab); n   } n   return MAX; n } n void output() { n   long long javab = first(); n   for (int i = 0; i < n; i++) a[i] *= -1; n   printf( %lld n , max(javab, first())); n } n int main() { n   ios::sync_with_stdio(false); n   input(); n   output(); n   return 0; n } n