#include <bits/stdc++.h> n using namespace std; n long long DP[100005][25], arr[100005], freq[100005], l = 1, r, ans; n void alter_range(long long l1, long long r1) { n   while (l1 < l) { n     l--; n     ans += freq[arr[l]]++; n   } n   while (r < r1) { n     r++; n     ans += freq[arr[r]]++; n   } n   while (l < l1) { n     ans -= --freq[arr[l]]; n     l++; n   } n   while (r1 < r) { n     ans -= --freq[arr[r]]; n     r--; n   } n } n void make_layer(long long layer, long long l1, long long r1, long long a, n                 long long b) { n   if (l1 > r1) return; n   long long i, j, mid = (l1 + r1) >> 1, &curr = DP[mid][layer], best = mid, n                   better; n   for (i = a; i <= min(b, mid); i++) { n     alter_range(i, mid); n     better = DP[i - 1][layer - 1] + ans; n     if (curr > better) { n       curr = better; n       best = i; n     } n   } n   make_layer(layer, l1, mid - 1, a, best); n   make_layer(layer, mid + 1, r1, best, b); n } n int main() { n   long long i, j, n, k; n   scanf( %lld %lld , &n, &k); n   for (i = 1; i <= n; i++) scanf( %lld , &arr[i]); n   for (i = 0; i <= n; i++) n     for (j = 0; j <= k; j++) DP[i][j] = 1e18; n   DP[0][0] = 0; n   for (i = 1; i <= k; i++) make_layer(i, 1, n, 1, n); n   printf( %lld n , DP[n][k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fuckAbs(long long int a) { return a >= 0 ? a : -a; } n int main(void) { n   long long int t, n, m, ans, dx, dy, sx, sy, x1, y1, x2, y2; n   cin >> t; n   while (t--) { n     cin >> n >> m >> x1 >> y1 >> x2 >> y2; n     ans = n * m; n     dx = n - max(x1, x2) + min(x1, x2); n     dy = m - max(y1, y2) + min(y1, y2); n     ans -= dx * dy * 2; n     sx = sy = 0; n     if (dx > fuckAbs(x1 - x2)) sx = dx - fuckAbs(x1 - x2); n     if (dy > fuckAbs(y1 - y2)) sy = dy - fuckAbs(y1 - y2); n     ans += sx * sy; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int x; n   cin >> x; n   bool f1 = 0, f2 = 0; n   long long su = 0; n   for (int i = 0; i < n; i++) { n     int k; n     cin >> k; n     su += k; n     if (k == x) { n       f1 = 1; n     } else n       f2 = 1; n   } n   if (!f1 && su == n * x) { n     cout << 1; n     return; n   } n   if (f1 && f2) cout << 1; n   if (f1 && !f2) cout << 0; n   if (!f1 && f2) cout << 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   cout << fixed << setprecision(12); n   int t = 1; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, m, s, ans; n long long n, k, t; n double sum; n int main() { n   scanf( %lld%lld , &n, &k); n   sum = (1.0 + k) * k / 2; n   if (sum > n) { n     puts( -1 ); n     return 0; n   } n   t = sum; n   for (int i = 1; (long long)i * i <= n; i++) n     if (n % i == 0) { n       int k = i; n       if (n / k - t >= 0) ans = max(ans, k); n       k = n / i; n       if (n / k - t >= 0) ans = max(ans, k); n     } n   for (int i = 1; i < k; i++) { n     printf( %lld  , (long long)i * ans); n     n -= (long long)i * ans; n   } n   printf( %lld n , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void pg() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int32_t main() { n   pg(); n   long long t; n   cin >> t; n   while (t--) { n     long long n, x; n     cin >> n >> x; n     long long arr[n]; n     long long sum = 0; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n       sum += arr[i]; n     } n     sort(arr, arr + n); n     long long cum = 0; n     bool flag = 0; n     for (long long i = 0; i < n; i++) { n       if (((sum - cum) / (n - i)) >= x) { n         cout << n - i << endl; n         flag = 1; n         break; n       } n       cum += arr[i]; n     } n     if (flag == 0) { n       cout << 0 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, x; n     cin >> n >> x; n     long long int a[n]; n     a[0] = 0; n     string s; n     cin >> s; n     for (int i = 0; i < n; i++) { n       char ch; n       ch = s[i]; n       if (i == 0) { n         if (ch ==  1 ) n           a[i]--; n         else n           a[i]++; n       } else { n         if (ch ==  1 ) n           a[i] = a[i - 1] - 1; n         else n           a[i] = a[i - 1] + 1; n       } n     } n     long long int as = 0; n     for (int i = 0; i < n; i++) { n       long long int k = (x - a[i]) * a[n - 1]; n       if (k >= 0) { n         if (abs(x - a[i]) == 0 || n             (a[n - 1] != 0 && abs(x - a[i]) % abs(a[n - 1]) == 0)) { n           as++; n         } n       } n     } n     long long int count = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] == x) { n         count++; n       } n     } n     as = max(as, count); n     if (as != 0 && a[n - 1] == 0) { n       cout << -1 <<   n ; n     } else { n       if (x == 0) as++; n       cout << as <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e7 + 10, maxl = 20, mod = 1e9 + 7, base = 737, n                 inf = 2e18 + 13; n int a, n, res[maxn], t[maxn]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> a >> n; n   for (int i = 2; i < a + n; i++) n     if (!t[i]) { n       t[i] = i; n       for (long long j = 1ll * i * i; j < a + n; j += i) t[j] = i; n     } n   res[1] = 1; n   for (int i = 2; i < a + n; i++) { n     res[i] = res[i / t[i]]; n     if (res[i] % t[i]) n       res[i] *= t[i]; n     else n       res[i] /= t[i]; n   } n   long long ans = 0; n   for (int i = a; i < a + n; i++) ans += res[i]; n   cout << ans << endl; n   return 0; n } n


#include <algorithm>   n #include <iostream>   n #include <sstream>   n #include <string>   n #include <vector>   n #include <queue>   n #include <set>   n #include <map>   n #include <cstdio>   n #include <cstdlib>   n #include <cctype>   n #include <cmath>   n #include <cstring> n #include <list>   n #include <cassert> n #include <climits> n #include <bitset> n #include <chrono> n #include <random> n using namespace std; n  n #define PB push_back   n #define MP make_pair   n #define SZ(v) ((int)(v).size())   n #define FOR(i,a,b) for(int i=(a);i<(b);++i)   n #define REP(i,n) FOR(i,0,n)   n #define FORE(i,a,b) for(int i=(a);i<=(b);++i)   n #define REPE(i,n) FORE(i,0,n)   n #define FORSZ(i,a,v) FOR(i,a,SZ(v))   n #define REPSZ(i,v) REP(i,SZ(v))   n std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count()); n typedef long long ll; n ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } n  n template<typename Item, typename Sum, typename Lazy> struct SplayTree { n  struct Node { int par, ch[2]; Item item; Sum sum; Lazy lazy; Node(Item item, Sum sum, Lazy lazy) :par(-1), item(item), sum(sum), lazy(lazy) { ch[0] = ch[1] = -1; } }; n  vector<Node> nodes; n  n  void reset() { nodes.clear(); } n  void apply(int x, Lazy lazy) { nodes[x].item += lazy; nodes[x].sum += lazy; nodes[x].lazy += lazy; } n  void push(int x) { REP(z, 2) if (nodes[x].ch[z] != -1) apply(nodes[x].ch[z], nodes[x].lazy); nodes[x].lazy = Lazy(); } n  void update(int x) { nodes[x].sum = Sum(); if (nodes[x].ch[0] != -1) nodes[x].sum += nodes[nodes[x].ch[0]].sum; nodes[x].sum += Sum(nodes[x].item); if (nodes[x].ch[1] != -1) nodes[x].sum += nodes[nodes[x].ch[1]].sum; } n  void connect(int x, int p, int z) { if (x != -1) nodes[x].par = p; if (p != -1) nodes[p].ch[z] = x; } n  int disconnect(int p, int z) { int x = nodes[p].ch[z]; nodes[p].ch[z] = -1; if (x != -1) nodes[x].par = -1; return x; } n  void rotate(int x) { n   int p = nodes[x].par, g = nodes[p].par, z = nodes[p].ch[0] == x ? 0 : 1, y = nodes[x].ch[1 - z]; push(p), push(x); n   connect(y, p, z), connect(p, x, 1 - z), connect(x, g, g == -1 ? -1 : nodes[g].ch[0] == p ? 0 : 1); update(p); n  } n  void splay(int x, int y = -1) { if (nodes[x].par == y) return; while (nodes[x].par != y) { int p = nodes[x].par, g = nodes[p].par; if (g != y) rotate((nodes[p].ch[0] == x) == (nodes[g].ch[0] == p) ? p : x); rotate(x); } update(x); } n  int first(int x) { if (x == -1) return x; splay(x); while (nodes[x].ch[0] != -1) x = nodes[x].ch[0]; splay(x); return x; } n  int last(int x) { if (x == -1) return x; splay(x); while (nodes[x].ch[1] != -1) x = nodes[x].ch[1]; splay(x); return x; } n  n  int add(Item item) { nodes.PB(Node(item, Sum(item), Lazy())); return SZ(nodes) - 1; }; n  int join(int l, int r) { if (l == -1) return r; l = last(l); push(l); connect(r, l, 1); update(l); return l; } n  void split(int x, int v, int& l, int& r) { n   if (x == -1) { l = r = -1; return; } else splay(x); n   l = r = -1; n   while (x != -1) { push(x); if (nodes[x].item.l() < v) l = x, x = nodes[x].ch[1]; else r = x, x = nodes[x].ch[0]; } n   if (l != -1) splay(l); if (r != -1) splay(r, l); n   if (l == -1) return; n   assert(nodes[l].par == -1 && nodes[l].ch[1] == r && (r == -1 || nodes[r].ch[0] == -1)); n   push(l); disconnect(l, 1); update(l); n   if (nodes[l].item.r() < v) return; n   Item splitted = nodes[l].item.split(v); n   if (nodes[l].ch[0] != -1 && nodes[nodes[l].ch[0]].item.r() == nodes[l].item.r()) l = disconnect(l, 0); n   if (r == -1 || splitted.l() != nodes[r].item.l()) r = join(add(splitted), r); n   update(l); n  } n  void gather(int x, vector<Item>& ret) { push(x); if (nodes[x].ch[0] != -1) gather(nodes[x].ch[0], ret); ret.PB(nodes[x].item); if (nodes[x].ch[1] != -1) gather(nodes[x].ch[1], ret); } n  vector<Item> all(int x) { vector<Item> ret; if (x != -1) splay(x), gather(x, ret); return ret; } n }; n  n const int MAXRECT = 200000; n const int INF = 1000000000; n struct Rect { int lt, rt, lx, rx; }; n  n int nrect, sx; n Rect rect[MAXRECT]; n  n struct Line { n  int ly, ry, lcost, slope; n  Line() {} n  Line(int ly, int ry, int lcost, int slope) :ly(ly), ry(ry), lcost(lcost), slope(ly == ry ? 0 : slope) {} n  int rcost() { return lcost + slope * (ry - ly); } n  int len() { return ry - ly; } n  void setly(int nly) { lcost += (nly - ly) * slope; ly = nly; if (ly == ry) slope = 0; } n  void setry(int nry) { ry = nry; if (ly == ry) slope = 0; } n  int l() { return ly; } n  int r() { return ry; } n  Line split(int y) { n   assert(ly < y && y <= ry); n   auto ret = Line(y, ry, lcost + (y - ly) * slope, slope); n   setry(y - 1); n   return ret; n  } n }; n struct SumLine { SumLine() {} SumLine(Line line) {} }; n SumLine& operator+=(SumLine& a, const SumLine& b) { return a; } n struct LazyLine {}; n Line& operator+=(Line& a, const LazyLine& b) { return a; } n SumLine& operator+=(SumLine& a, const LazyLine& b) { return a; } n LazyLine& operator+=(LazyLine& a, const LazyLine& b) { return a; } n  n SplayTree<Line, SumLine, LazyLine> linetree; n  n void printfunc(int lineroot,int tline) { n  if (lineroot == -1) { n   printf(  BLOCKED ); n  } else { n   auto alllines = linetree.all(lineroot); n   for (auto line : alllines) printf(  (%d,%d)..(%d,%d) , line.ly == -INF ? -INF : line.ly + tline, line.ly == -INF ? INF : line.lcost, line.ry == +INF ? +INF : line.ry + tline, line.ry == +INF ? INF : line.rcost()); n  } n  puts(  ); n } n  n int t; n  n int rtrimfunc(int node, int nry) { n  while(true) { n   node = linetree.last(node); n   if (linetree.nodes[node].item.ry <= nry) break; n   if (linetree.nodes[node].item.ly < nry || linetree.nodes[node].item.ly == nry && linetree.nodes[node].ch[0] == -1) { n    linetree.nodes[node].item.setry(nry); n    break; n   } n   node = linetree.disconnect(node, 0); n   assert(node != -1); n  } n  return node; n } n int lgrowfunc(int node, int nly) { n  node = linetree.first(node); n  assert(nly <= linetree.nodes[node].item.ly); n  if (nly == linetree.nodes[node].item.ly) return node; n  if (linetree.nodes[node].item.slope == -1 || linetree.nodes[node].item.ly == linetree.nodes[node].item.ry) { n   linetree.nodes[node].item.slope = -1; n   linetree.nodes[node].item.setly(nly); n   linetree.update(node); n  } else { n   Line line(nly, linetree.nodes[node].item.ly, linetree.nodes[node].item.lcost + linetree.nodes[node].item.ly - nly, -1); n   node = linetree.join(linetree.add(line), node); n  } n  return node; n } n int rgrowfunc(int node, int nry) { n  node = linetree.last(node); n  assert(nry >= linetree.nodes[node].item.ry); n  if (nry == linetree.nodes[node].item.ry) return node; n  if (linetree.nodes[node].item.slope == +1 || linetree.nodes[node].item.ly == linetree.nodes[node].item.ry) { n   linetree.nodes[node].item.slope = +1; n   linetree.nodes[node].item.setry(nry); n   linetree.update(node); n  } else { n   Line line(linetree.nodes[node].item.ry, nry, linetree.nodes[node].item.rcost(), +1); n   node = linetree.join(node, linetree.add(line)); n  } n  return node; n } n int shiftfunc(int node, int dt) { n  if (dt == 0) return node; n  node = linetree.first(node); n  int ply = linetree.nodes[node].item.ly; n  if (ply != -INF) node = lgrowfunc(node, ply - dt); n  node = linetree.last(node); n  int pry = linetree.nodes[node].item.ry; n  if (pry != +INF) node = rtrimfunc(node, pry - dt); n  return node; n } n int mergefunc(int l, int r) { n  //printf( merging n ); n  //printfunc(l, t); n  //printfunc(r, t); n  l = linetree.last(l); n  r = linetree.first(r); n  while (l != -1 && r != -1 && linetree.nodes[l].item.rcost() > linetree.nodes[r].item.lcost) { n   assert(linetree.nodes[l].item.ry == linetree.nodes[r].item.ly); n   if (linetree.nodes[l].item.slope == -1 || linetree.nodes[l].item.ly == linetree.nodes[l].item.ry) { n    r = lgrowfunc(r, linetree.nodes[l].item.ly); n    l = linetree.disconnect(l, 0); n   } else { n    assert(linetree.nodes[l].item.slope == 1); n    if (linetree.nodes[r].item.lcost + linetree.nodes[l].item.len() <= linetree.nodes[l].item.lcost) { n     r = lgrowfunc(r, linetree.nodes[l].item.ly); n     l = linetree.disconnect(l, 0); n    } else { n     int y = linetree.nodes[r].item.lcost - linetree.nodes[l].item.lcost + linetree.nodes[l].item.ly + linetree.nodes[l].item.ry; assert(y % 2 == 0); y /= 2; n     assert(y > linetree.nodes[l].item.ly && y < linetree.nodes[l].item.ry); n     r = lgrowfunc(r, y); n     linetree.nodes[l].item.setry(y); n    } n   } n   l = linetree.last(l); n   r = linetree.first(r); n  } n  while (l != -1 && r != -1 && linetree.nodes[l].item.rcost() < linetree.nodes[r].item.lcost) { n   assert(linetree.nodes[l].item.ry == linetree.nodes[r].item.ly); n   if (linetree.nodes[r].item.slope == +1 || linetree.nodes[r].item.ly == linetree.nodes[r].item.ry) { n    l = rgrowfunc(l, linetree.nodes[r].item.ry); n    r = linetree.disconnect(r, 1); n   } else { n    assert(linetree.nodes[r].item.slope == -1); n    if (linetree.nodes[l].item.rcost() + linetree.nodes[r].item.len() <= linetree.nodes[r].item.rcost()) { n     l = rgrowfunc(l, linetree.nodes[r].item.ry); n     r = linetree.disconnect(r, 1); n    } else { n     int y = linetree.nodes[r].item.rcost() - linetree.nodes[l].item.rcost() + linetree.nodes[r].item.ly + linetree.nodes[r].item.ry; assert(y % 2 == 0); y /= 2; n     assert(y > linetree.nodes[r].item.ly && y < linetree.nodes[r].item.ry); n     l = rgrowfunc(l, y); n     linetree.nodes[r].item.setly(y); n    } n   } n   l = linetree.last(l); n   r = linetree.first(r); n  } n  int ret = linetree.join(l, r); n  //printf( ->  ); printfunc(ret, t); n  return ret; n } n  n struct Region { n  int lx, rx, blockcnt, lineroot, tline; n  Region(int lx, int rx, int blockcnt, int lineroot, int tline) :lx(lx), rx(rx), blockcnt(blockcnt), lineroot(lineroot), tline(tline) {}; n  int l() { return lx; } n  int r() { return rx; } n  void norm() { if (lineroot != -1) { lineroot = shiftfunc(lineroot, t - tline); tline = t; } } n  Region split(int x) { n   assert(lx < x && x <= rx);  n   norm(); n   auto ret = Region(x, rx, blockcnt, -1, tline); n   rx = x - 1; n   if (lineroot != -1) linetree.split(lineroot, x - t, lineroot, ret.lineroot); n   return ret; n  }; n }; n struct SumRegion { n  int mnblockcnt; n  SumRegion() { mnblockcnt = INT_MAX; } n  SumRegion(Region region) { mnblockcnt = region.lineroot == -1 ? INT_MAX : region.blockcnt; } n }; n SumRegion& operator+=(SumRegion& a, const SumRegion& b) { a.mnblockcnt = min(a.mnblockcnt, b.mnblockcnt); return a; } n struct LazyRegion { n  int lazyblockcnt; n  LazyRegion() { lazyblockcnt = 0; } n  LazyRegion(int lazyblockcnt) :lazyblockcnt(lazyblockcnt) {} n }; n Region& operator+=(Region& a, const LazyRegion& b) { a.blockcnt += b.lazyblockcnt; return a; } n SumRegion& operator+=(SumRegion& a, const LazyRegion& b) { if (a.mnblockcnt != INT_MAX) a.mnblockcnt += b.lazyblockcnt; return a; } n LazyRegion& operator+=(LazyRegion& a, const LazyRegion& b) { a.lazyblockcnt += b.lazyblockcnt; return a; } n  n  n SplayTree<Region, SumRegion, LazyRegion> regiontree; n  n void print(int regionroot) { n  auto allregions = regiontree.all(regionroot); n  for (auto region : allregions) { n   //region.norm(); n   printf( [%d..%d] = %d: , region.lx, region.rx, region.blockcnt); if (region.lineroot!=-1 && region.tline != t) printf(  (delay %d) , t - region.tline); n   printfunc(region.lineroot, region.tline); n  } n } n  n int killzeroes(int node) { n  regiontree.splay(node); n  while (regiontree.nodes[node].sum.mnblockcnt == 0) { n   //printf( killing n ); print(node); n   while (regiontree.nodes[node].item.blockcnt != 0 || regiontree.nodes[node].item.lineroot == -1) { n    //printf( node=%d n , node); n    if (regiontree.nodes[node].ch[0] != -1 && regiontree.nodes[regiontree.nodes[node].ch[0]].sum.mnblockcnt == 0) { n     node = regiontree.nodes[node].ch[0]; n    } else { n     node = regiontree.nodes[node].ch[1]; n    } n    assert(node != -1 && regiontree.nodes[node].sum.mnblockcnt == 0); n   } n   //printf( found node=%d n , node); n   assert(regiontree.nodes[node].item.lineroot != -1); n   regiontree.splay(node); n   regiontree.nodes[node].item.lineroot = -1; n   regiontree.update(node); n  } n  return node; n } n  n int rgrow(int node, int x) { n  int l, r; regiontree.split(node, x + 1, l, r); n  l = regiontree.last(l); n  r = regiontree.first(r); n  //printf( growing n ); n  //printf( l n ); print(l); n  //printf( r n ); print(r); n  if (l == -1 || regiontree.nodes[l].item.lineroot == -1) return regiontree.join(l, r); n  while (r != -1 && regiontree.nodes[r].item.lineroot == -1 && regiontree.nodes[r].item.blockcnt == 0) { n   //printf( step n ); n   assert(regiontree.nodes[l].item.rx + 1 == regiontree.nodes[r].item.lx); n   regiontree.nodes[l].item.rx = regiontree.nodes[r].item.rx; n   r = regiontree.disconnect(r, 1); n   r = regiontree.first(r); n  } n  if (r != -1 && regiontree.nodes[r].item.lineroot != -1) { n   assert(regiontree.nodes[l].item.rx + 1 == regiontree.nodes[r].item.lx); n   regiontree.nodes[l].item.rx = regiontree.nodes[r].item.lx; n   regiontree.nodes[l].item.norm(); n   regiontree.nodes[l].item.lineroot = rgrowfunc(regiontree.nodes[l].item.lineroot, regiontree.nodes[l].item.rx - t); n   regiontree.nodes[r].item.norm(); n   regiontree.nodes[l].item.lineroot = mergefunc(regiontree.nodes[l].item.lineroot, regiontree.nodes[r].item.lineroot); n   regiontree.nodes[l].item.rx = regiontree.nodes[r].item.rx; n   r = regiontree.disconnect(r, 1); n  } else { n   regiontree.nodes[l].item.norm(); n   regiontree.nodes[l].item.lineroot = rgrowfunc(regiontree.nodes[l].item.lineroot, regiontree.nodes[l].item.rx - t); n  } n  int ret = regiontree.join(l, r); n  //printf( after rgrow n ); print(ret); n  return ret; n } n  n int lgrow(int node, int x) { n  //printf( lgrow %d n , x); n  int l, r; regiontree.split(node, x, l, r); n  l = regiontree.last(l); n  r = regiontree.first(r); n  if (r == -1 || regiontree.nodes[r].item.lineroot == -1) return regiontree.join(l, r); n  while (l != -1 && regiontree.nodes[l].item.lineroot == -1 && regiontree.nodes[l].item.blockcnt == 0) { n   assert(regiontree.nodes[l].item.rx + 1 == regiontree.nodes[r].item.lx); n   regiontree.nodes[r].item.lx = regiontree.nodes[l].item.lx; n   l = regiontree.disconnect(l, 0); n   l = regiontree.last(l); n  } n  if (l != -1 && regiontree.nodes[l].item.lineroot != -1) { n   assert(regiontree.nodes[l].item.rx + 1 == regiontree.nodes[r].item.lx); n   regiontree.nodes[l].item.rx = regiontree.nodes[r].item.lx; n   regiontree.nodes[l].item.norm(); n   regiontree.nodes[l].item.lineroot = rgrowfunc(regiontree.nodes[l].item.lineroot, regiontree.nodes[l].item.rx - t); n   regiontree.nodes[r].item.norm(); n   regiontree.nodes[l].item.lineroot = mergefunc(regiontree.nodes[l].item.lineroot, regiontree.nodes[r].item.lineroot); n   regiontree.nodes[l].item.rx = regiontree.nodes[r].item.rx; n   r = regiontree.disconnect(r, 1); n  } else { n   regiontree.nodes[r].item.norm(); n   regiontree.nodes[r].item.lineroot = lgrowfunc(regiontree.nodes[r].item.lineroot, regiontree.nodes[r].item.lx - t); n  } n  return regiontree.join(l, r); n } n  n int solve() { n  //REP(i, nrect) printf( t=%d..%d y=%d..%d to %d..%d n , rect[i].lt, rect[i].rt, rect[i].lx - rect[i].lt, rect[i].rx - rect[i].lt, rect[i].lx - rect[i].rt, rect[i].rx - rect[i].rt); n  n  linetree.reset(); n  regiontree.reset(); n  int linedn = linetree.add(Line(-INF, sx, abs(-INF - sx), -1)); n  int lineup = linetree.add(Line(sx, +INF, 0, +1)); n  int lineroot = linetree.join(linedn, lineup); n  int regionroot = regiontree.add(Region(-INF, +INF, 0, lineroot, 0)); n  n  vector<pair<int, int>> e; n  REP(i, nrect) e.PB(MP(2 * rect[i].lt + 1, i)), e.PB(MP(2 * rect[i].rt + 0, i)); n  sort(e.begin(), e.end()); n  //printf( INITIAL n ); print(regionroot); n  REPSZ(i, e) { n   t = e[i].first >> 1; int kind = e[i].first & 1, idx = e[i].second; n   if (kind == 0) { // release rect n    int l, m, r; n    regiontree.split(regionroot, rect[idx].lx + 1, l, m); n    regiontree.split(m, rect[idx].rx, m, r); n    regiontree.apply(m, LazyRegion(-1)); n    regionroot = regiontree.join(regiontree.join(l, m), r); n    regionroot = rgrow(regionroot, rect[idx].lx); n    regionroot = lgrow(regionroot, rect[idx].rx); n   } n   if (kind == 1) { // block rect n    int l, m, r; n    regiontree.split(regionroot, rect[idx].lx + 1, l, m); n    //printf( L n ); print(l); n    //printf( M n ); print(m); n    regiontree.split(m, rect[idx].rx, m, r); n    //printf( M n ); print(m); n    //printf( R n ); print(r); n    m = killzeroes(m); n    //printf( M n ); print(m); n    regiontree.apply(m, LazyRegion(1)); n    regionroot = regiontree.join(regiontree.join(l, m), r); n   } n   //printf( after %s rect %d (t=%d) n , kind == 0 ?  releasing  :  blocking , idx + 1, t); print(regionroot); n   //if (i == 0) exit(0); n  } n  //printf( FINAL (t=%d) n ,t); print(regionroot); n  n  auto finalregions = regiontree.all(regionroot); n  assert(SZ(finalregions) == 1); n  auto finalregion = finalregions[0]; n  finalregion.norm(); n  assert(finalregion.lineroot != -1); n  auto finalfunc = linetree.all(finalregion.lineroot); n  int ret = INT_MAX; n  for (auto func : finalfunc) ret = min(ret, min(func.lcost, func.rcost())); n  return ret; n } n  n void run() { n  scanf( %d , &nrect); n  scanf( %d , &sx); n  REP(i, nrect) scanf( %d%d%d%d , &rect[i].lt, &rect[i].rt, &rect[i].lx, &rect[i].rx), --rect[i].lt, ++rect[i].rt, --rect[i].lx, ++rect[i].rx; n  printf( %d n , solve()); n } n  n void stress() { n  int mxrect = 100, mxdim = 100; n  REP(rep, 10000) { n   nrect = rnd() % mxrect + 1; n   int tdim = rnd() % mxdim + 1; n   int xdim = rnd() % mxdim + 1; n   sx = rnd() % (xdim + 1); n   REP(i, nrect) { n    rect[i].lt = rnd() % tdim; rect[i].rt = rnd() % tdim; if (rect[i].lt > rect[i].rt) swap(rect[i].lt, rect[i].rt); rect[i].rt += 2; n    rect[i].lx = rnd() % xdim; rect[i].rx = rnd() % xdim; if (rect[i].lx > rect[i].rx) swap(rect[i].lx, rect[i].rx); rect[i].rx += 2; n   } n   //printf( %d n%d n , nrect, sx); REP(i, nrect) printf( %d %d %d %d n , rect[i].lt + 1, rect[i].rt - 1, rect[i].lx + 1, rect[i].rx - 1); n   solve(); n   printf( . ); n  } n } n  n int main() { n  run(); n  //stress(); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> l(n + 5), r(n + 5); n   for (long long i = 1; i < n + 1; ++i) cin >> l[i]; n   for (long long i = 1; i < n + 1; ++i) cin >> r[i]; n   vector<pair<long long, long long> > v; n   for (long long i = 1; i < n + 1; ++i) v.push_back({l[i] + r[i], i}); n   sort(v.begin(), v.end()); n   long long x = n, ans[n + 5]; n   for (long long i = 0; i < (long long)v.size(); ++i) { n     ans[v[i].second] = x; n     while (i + 1 < (long long)v.size() && v[i].first == v[i + 1].first) { n       ++i; n       ans[v[i].second] = x; n     } n     x--; n   } n   for (long long i = 1; i < n + 1; ++i) { n     long long c1 = 0; n     for (long long j = 1; j < i; ++j) n       if (ans[i] < ans[j]) ++c1; n     long long c2 = 0; n     for (long long j = i + 1; j < n + 1; ++j) n       if (ans[j] > ans[i]) ++c2; n     if (c1 == l[i] && c2 == r[i]) continue; n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (long long i = 1; i < n + 1; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n constexpr int LOGMAX = 31 - __builtin_clz(MAX) + 5; n int N; n char T[MAX]; n int sa[MAX], tsa[MAX], ra[MAX], tra[MAX]; n int lcp[MAX], plcp[MAX], phi[MAX], c[MAX], st[LOGMAX][MAX]; n long long pref[MAX]; n void sort(int k) { n   memset(c, 0, sizeof c); n   for (int i = int(0); i < int(N); i++) { n     c[i + k < N ? ra[i + k] : 0]++; n   } n   int sum = 0, maxi = max(300, N); n   for (int i = int(0); i < int(maxi); i++) { n     int t = c[i]; n     c[i] = sum; n     sum += t; n   } n   for (int i = int(0); i < int(N); i++) { n     int cur = sa[i] + k < N ? ra[sa[i] + k] : 0; n     tsa[c[cur]++] = sa[i]; n   } n   memcpy(sa, tsa, sizeof(int) * N); n } n void build_sa() { n   for (int i = int(0); i < int(N); i++) { n     ra[i] = T[i]; n     sa[i] = i; n   } n   for (int k = 1; k < N; k *= 2) { n     sort(k); n     sort(0); n     int r = tra[sa[0]] = 0; n     for (int i = int(1); i < int(N); i++) { n       if (ra[sa[i]] == ra[sa[i - 1]]) { n         int cur = (sa[i] + k) < N ? ra[sa[i] + k] : 0; n         int prev = (sa[i - 1] + k) < N ? ra[sa[i - 1] + k] : 0; n         if (cur == prev) { n           tra[sa[i]] = r; n         } else { n           tra[sa[i]] = ++r; n         } n       } else { n         tra[sa[i]] = ++r; n       } n     } n     if (tra[sa[N - 1]] == N - 1) { n       break; n     } n     memcpy(ra, tra, sizeof(int) * N); n   } n } n void build_lcp() { n   phi[sa[0]] = -1; n   for (int i = int(1); i < int(N); i++) { n     phi[sa[i]] = sa[i - 1]; n   } n   int l = 0; n   for (int i = int(0); i < int(N); i++) { n     if (phi[i] == -1) { n       plcp[i] = 0; n       continue; n     } n     while (T[i + l] == T[phi[i] + l]) { n       l++; n     } n     plcp[i] = l; n     l = max(l - 1, 0); n   } n   for (int i = int(0); i < int(N); i++) { n     lcp[i] = plcp[sa[i]]; n   } n } n inline long long in_range(int left, int right) { n   return pref[right] - pref[left - 1]; n } n inline int min_query(int left, int right) { n   int sz = right - left + 1; n   int lg = 31 - __builtin_clz(sz); n   return min(st[lg][left], st[lg][right - (1 << lg) + 1]); n } n string roll(int l, int r, int len, long long k) { n   int left = l + 1, right = r; n   int edge = l; n   while (left <= right) { n     int mid = (left + right) / 2; n     if (min_query(l + 1, mid) >= len) { n       edge = mid; n       left = mid + 1; n     } else { n       right = mid - 1; n     } n   } n   long long total = in_range(l, edge) - 1LL * (len - 1) * (edge - l + 1); n   if (total < k) { n     return roll(edge + 1, r, len, k - total); n   } else if (k > (edge - l + 1)) { n     return roll(l, r, len + 1, k - (edge - l + 1)); n   } n   int pos = l + k - 1; n   string ans =   ; n   for (int i = int(0); i < int(len); i++) { n     ans += T[sa[pos] + i]; n   } n   return ans; n } n int main() { n   scanf( %s , T); n   N = strlen(T); n   long long k; n   scanf( %lld , &k); n   if ((1LL * N * (N + 1)) / 2 < k) { n     puts( No such line. ); n     return 0; n   } n   T[N++] =  $ ; n   build_sa(); n   build_lcp(); n   for (int i = int(1); i < int(N); i++) { n     pref[i] = pref[i - 1] + N - sa[i] - 1; n     st[0][i] = lcp[i]; n   } n   for (int i = int(1); i < int(LOGMAX); i++) { n     for (int j = 1; j + (1 << i) - 1 < N; j++) { n       st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); n     } n   } n   printf( %s n , roll(1, N - 1, 1, k).c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e16; n int a[2][111111]; n int main(void) { n   int n; n   cin >> n; n   for (int e = 0; e < 2; e++) n     for (int p = 0; p < n; p++) cin >> a[e][p]; n   long long int tot = 0; n   for (int e = 0; e < n; e++) tot += a[0][e]; n   sort(a[1], a[1] + n); n   long long int kk = 0; n   kk += a[1][n - 1] + a[1][n - 2]; n   if (kk >= tot) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout << 1 <<     << 1 << endl << 1 << endl; n     return 0; n   } n   int value = n * 2 - 2; n   cout << value <<     << 2 << endl << 1 <<     << 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2][100010], bit, n; n long long a[100010]; n inline int comp(long long x, long long y) { n   return (x % (1ll << (bit + 1))) > (y % (1ll << (bit + 1))); n } n int main() { n   long long mx = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     mx = max(mx, a[i]); n   } n   int cur = 0; n   memset(dp, 0x3f, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 0; i < 63; i++) { n     cur ^= 1; n     int cnt1 = 0, cnt2 = 0; n     memset(dp[cur], 0x3f, sizeof(dp[cur])); n     for (int j = 0; j < n; j++) { n       if (a[j] & (1ll << i)) cnt2++; n     } n     for (int j = 0; j <= n; j++) { n       if (dp[cur ^ 1][j] != 0x3f3f3f3f) { n         dp[cur][j + cnt2] = n             min(dp[cur][j + cnt2], dp[cur ^ 1][j] + j - cnt1 + cnt2); n         dp[cur][cnt1] = n             min(dp[cur][cnt1], dp[cur ^ 1][j] + cnt1 + (n - j - cnt2)); n       } n       if (a[j] & (1ll << i)) { n         cnt1++; n         cnt2--; n       } n     } n     if ((1ll << i) > mx) break; n     bit = i; n     sort(a, a + n, comp); n   } n   printf( %d n , dp[cur][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, ar[1000005], br[1000005]; n int main() { n   scanf( %lld %lld , &n, &k); n   long long min_ = 0, max_ = 0; n   for (int i = 1; i < n + 1; i++) { n     ar[i] = br[i] = i; n     min_ += i; n     max_ += max((long long)i, n - i + 1); n   } n   if (k < min_) { n     cout <<  -1 ; n     return 0; n   } else { n     long long tot = min_; n     long long t = n - 1; n     k -= min_; n     for (int i = 1; i < n + 1; i++) { n       if (i >= n + 1 - i) break; n       if (k > t) { n         tot += t; n         min_ += t; n         swap(br[i], br[n + 1 - i]); n       } else { n         tot += k; n         min_ += k; n         swap(br[i], br[i + k]); n         break; n       } n       k -= t; n       t -= 2; n     } n     cout << tot <<   n ; n   } n   for (int i = 1; i < n + 1; i++) cout << ar[i] <<    ; n   cout <<   n ; n   for (int i = 1; i < n + 1; i++) cout << br[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int read(); n int n; n char t[102]; n int w[102][102], rnk[102][102], pos[102]; n vector<int> p[102]; n queue<int> q; n void init(int t) { n   for (int i = 1; i <= n; ++i) { n     p[i].clear(), p[i + n].clear(); n     for (int j = 1; j <= n; ++j) p[i].push_back(j + n), p[i + n].push_back(j); n     sort(p[i].begin(), p[i].end(), n          [&](int a, int b) { return w[i][a - n] > w[i][b - n] ^ t; }); n     sort(p[i + n].begin(), p[i + n].end(), n          [&](int a, int b) { return w[a][i] < w[b][i] ^ t; }); n     q.push(i), pos[i] = 0, pos[i + n] = n; n     for (int j = 0; j < n; ++j) n       rnk[j][p[i][j]] = j, rnk[i + n][p[i + n][j]] = j; n   } n   while (!q.empty()) { n     int u = q.front(), v; n     while (pos[v = p[u][pos[u]]] < rnk[v][u]) ++pos[u]; n     if (pos[v] < n) q.push(p[v][pos[v]]); n     pos[v] = rnk[v][u], q.pop(); n   } n } n int main() { n   for (int T = read(); T; --T) { n     n = read(); n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) w[i][j] = read(); n     puts( B ), fflush(stdout), scanf( %s , t); n     if (t[0] ==  I ) n       for (int i = 1; i <= n; ++i) n         for (int j = 1; j <= n; ++j) w[i][j] = -w[i][j]; n     int u; n     for (init((u = read()) > n); ~u; u = read()) n       printf( %d n , p[u][pos[u]]), fflush(stdout); n   } n   return 0; n } n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) f = (c ==  - ) ? -1 : f, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   int out = n; n   int cur = n / 2; n   for (int i = 0; i < n / 2 && cur < n; i++) { n     while (true) { n       if (v[i] * 2 <= v[cur]) { n         out--, cur++; n         break; n       } else { n         cur++; n         if (cur == n) break; n       } n     } n   } n   cout << out << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n struct node { n   int x, y; n } f[2203456], ans[2203456]; n bool cmp(node x, node y) { return x.x == y.x ? x.y > y.y : x.x < y.x; } n int main() { n   int n, k; n   cin >> n >> k; n   int tot = 0; n   for (int i = 0; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     f[tot].x = a, f[tot].y = 1; n     tot++; n     f[tot].x = b, f[tot].y = -1; n     tot++; n   } n   sort(f, f + tot, cmp); n   int cnt = 0, ansi = 0, start = 0; n   for (int i = 0; i < tot; i++) { n     if (cnt == k - 1 && f[i].y == 1) { n       start = f[i].x; n     } n     if (cnt == k && f[i].y == -1) { n       ans[ansi].x = start; n       ans[ansi].y = f[i].x; n       ansi++; n     } n     cnt += f[i].y; n   } n   cout << ansi << endl; n   for (int i = 0; i < ansi; i++) { n     printf( %d %d n , ans[i].x, ans[i].y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, t; n   cin >> n >> t; n   long long arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   float mxdif = INT_MIN; n   for (int i = 1; i < n; i++) { n     if (arr[i] - arr[i - 1] > mxdif) { n       mxdif = arr[i] - arr[i - 1]; n     } n   } n   double z = (mxdif + 0.000) / 2; n   double maxcnr = max(arr[0] - 0, t - arr[n - 1]); n   printf( %.9lf , max(z, maxcnr)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long memo[4][1001][1001]; n long long mp[1001][1001]; n int n, m; n long long REC(int x, int y, int st) { n   if (x >= n || y >= m || x < 0 || y < 0) return 0; n   if (st == 0 && x == n - 1 && y == m - 1) return mp[x][y]; n   if (st == 1 && x == 0 && y == 0) return mp[x][y]; n   if (st == 2 && x == 0 && y == m - 1) return mp[x][y]; n   if (st == 3 && x == n - 1 && y == 0) return mp[x][y]; n   if (~memo[st][x][y]) return memo[st][x][y]; n   long long a, b; n   if (st == 0) { n     a = REC(x + 1, y, st) + mp[x][y]; n     b = REC(x, y + 1, st) + mp[x][y]; n     return memo[st][x][y] = a >= b ? a : b; n   } else if (st == 1) { n     a = REC(x - 1, y, st) + mp[x][y]; n     b = REC(x, y - 1, st) + mp[x][y]; n     return memo[st][x][y] = a >= b ? a : b; n   } else if (st == 2) { n     a = REC(x - 1, y, st) + mp[x][y]; n     b = REC(x, y + 1, st) + mp[x][y]; n     return memo[st][x][y] = a >= b ? a : b; n   } else if (st == 3) { n     a = REC(x + 1, y, st) + mp[x][y]; n     b = REC(x, y - 1, st) + mp[x][y]; n     return memo[st][x][y] = a >= b ? a : b; n   } n } n long long check(int x, int y) { n   long long a, b, aa, bb, res = 0, tmp; n   b = memo[3][x + 1][y]; n   a = memo[1][x][y - 1]; n   bb = memo[2][x - 1][y]; n   aa = memo[0][x][y + 1]; n   tmp = aa + bb + a + b; n   res = tmp >= res ? tmp : res; n   b = memo[3][x][y - 1]; n   a = memo[1][x - 1][y]; n   bb = memo[2][x][y + 1]; n   aa = memo[0][x + 1][y]; n   tmp = aa + bb + a + b; n   res = tmp >= res ? tmp : res; n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> mp[i][j]; n     } n   } n   memset(memo, -1, sizeof memo); n   REC(0, 0, 0); n   REC(n - 1, m - 1, 1); n   REC(n - 1, 0, 2); n   REC(0, m - 1, 3); n   long long maxi = 0; n   for (int i = 1; i < n - 1; i++) { n     for (int j = 1; j < m - 1; j++) { n       long long tmp = check(i, j); n       maxi = tmp >= maxi ? tmp : maxi; n     } n   } n   cout << maxi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const int NMAX = 5005; n const int INF = 2e5; n int n; n vector<int> arr; n vector<vector<int>> dp(NMAX, vector<int>(NMAX, INF)); n int rec(int i, int j) { n   if (dp[i][j] != INF) return dp[i][j]; n   if (i == j) return dp[i][j] = 0; n   if (arr[i] == arr[j]) return dp[i][j] = 1 + rec(i + 1, j - 1); n   return dp[i][j] = 1 + min(rec(i + 1, j), rec(i, j - 1)); n } n void solve() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int tmp; n     cin >> tmp; n     if (!i) n       arr.push_back(tmp); n     else if (tmp != arr[(int)(arr.size()) - 1]) n       arr.push_back(tmp); n   } n   n = (int)(arr.size()); n   cout << rec(0, n - 1) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int x, y, z; n int sum = 0; n map<int, pair<char, int> > ans; n map<int, pair<char, int> >::iterator t; n int main() { n   scanf( %d%d , &n, &q); n   ans[0] = { U , 0}; n   ans[n + 1] = { L , 0}; n   for (int i = 1; i <= q; i++) { n     scanf( %d%d%c%c , &x, &y, &z, &z); n     t = ans.lower_bound(x); n     if (t->first == x) { n       printf( 0 n ); n       continue; n     } n     if (z ==  L ) t--; n     sum = abs(x - t->first); n     if (t->second.first == z) sum += t->second.second; n     ans[x] = {z, sum}; n     printf( %d n , sum); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long qr() { n   register long long ret = 0, f = 0; n   register char c = getchar(); n   while (c < 48 || c > 57) f |= c == 45, c = getchar(); n   while (c >= 48 && c <= 57) ret = ret * 10 + c - 48, c = getchar(); n   return f ? -ret : ret; n } n const int maxn = 1e5 + 5; n int n, m, p; n int dis[maxn]; n long long sumd[maxn]; n long long sumdata[maxn]; n long long x[maxn]; n long long y[maxn]; n long long ans; n deque<int> q; n struct NODE { n   int pos, time; n   long long limit; n   NODE() { ans = pos = time = 0; } n   inline bool operator<(const NODE& a) const { return limit < a.limit; } n   inline void scan() { n     pos = qr(); n     time = qr(); n     limit = time - sumd[pos]; n   } n } data[maxn]; n long long dp[101][maxn]; n inline void wk() { n   memset(dp, 5, sizeof dp); n   dp[0][0] = 0; n   deque<int>::iterator ita; n   for (register int i = 0; i <= m; ++i) x[i] = i; n   for (register int t = 1; t <= p; ++t) { n     for (register int i = 0; i <= m; ++i) { n       dp[t][i] = dp[t - 1][i]; n       y[i] = dp[t - 1][i] + sumdata[i]; n     } n     q.clear(); n     for (register int i = 1; i <= m; ++i) { n       q.push_back(i - 1); n       ita = q.begin(); n       while (q.size() > 1 && y[*(ita + 1)] - y[*ita] <= n                                  1ll * data[i].limit * ((*(ita + 1)) - (*ita))) n         q.pop_front(), ita = q.begin(); n       register int j = q.front(); n       dp[t][i] = min(dp[t][i], dp[t - 1][j] + 1ll * (i - j) * data[i].limit - n                                    (sumdata[i] - sumdata[j])); n       ita = q.end() - 1; n       while (q.size() > 1 && n              (y[*ita] - 1ll * y[*(ita - 1)]) * (i - (*ita)) >= n                  1ll * (y[i] - y[*ita]) * ((*ita) - (*(ita - 1)))) n         q.pop_back(), ita = q.end() - 1; n       ; n     } n   } n   cout << dp[p][m] << endl; n } n int main() { n   n = qr(); n   m = qr(); n   p = qr(); n   for (register int t = 2; t <= n; ++t) sumd[t] = (dis[t] = qr()) + sumd[t - 1]; n   for (register int t = 1; t <= m; ++t) data[t].scan(); n   sort(data + 1, data + m + 1); n   for (register int t = 1; t <= m; ++t) n     sumdata[t] = data[t].limit + sumdata[t - 1]; n   wk(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n namespace _buff { n const size_t BUFF = 1 << 19; n char ibuf[BUFF], *ib = ibuf, *ie = ibuf; n char getc() { n   if (ib == ie) { n     ib = ibuf; n     ie = ibuf + fread(ibuf, 1, BUFF, stdin); n   } n   return ib == ie ? -1 : *ib++; n } n }  // namespace _buff n LL read() { n   using namespace _buff; n   LL ret = 0; n   bool pos = true; n   char c = getc(); n   for (; (c <  0  || c >  9 ) && c !=  - ; c = getc()) { n     assert(~c); n   } n   if (c ==  - ) { n     pos = false; n     c = getc(); n   } n   for (; c >=  0  && c <=  9 ; c = getc()) { n     ret = (ret << 3) + (ret << 1) + (c ^ 48); n   } n   return pos ? ret : -ret; n } n const size_t N = 2e2 + 5; n const size_t AL = 21; n const size_t V = N * AL; n const size_t K = 5e2 + 5; n const int MOD = 1e9 + 7; n void iadd(int &dp, int val) { n   dp += val; n   if (dp >= MOD) { n     dp -= MOD; n   } n } n int val[V], ch[V][AL], fail[V], tot = 1; n int dp[2][V][K][2], m, k; n int new_node(int &x) { n   if (!x) x = ++tot; n   return x; n } n void add(const vector<int> &vc, int v) { n   int cur = 1; n   for (int dig : vc) { n     cur = new_node(ch[cur][dig]); n   } n   val[cur] += v; n } n void build() { n   queue<int> que; n   for (int i = 0; i < m; ++i) { n     int &x = ch[1][i]; n     if (x) { n       fail[x] = 1; n       que.emplace(x); n     } else { n       x = 1; n     } n   } n   while (!que.empty()) { n     int x = que.front(); n     que.pop(); n     val[x] += val[fail[x]]; n     for (int i = 0; i < m; ++i) { n       if (ch[x][i]) { n         fail[ch[x][i]] = ch[fail[x]][i]; n         que.emplace(ch[x][i]); n       } else { n         ch[x][i] = ch[fail[x]][i]; n       } n     } n   } n } n int solve(const vector<int> &vc) { n   int pi = 0, ci = 1; n   memset(dp[pi], 0, sizeof dp[pi]); n   for (unsigned i = 0; i < vc.size(); ++i) { n     memset(dp[ci], 0, sizeof dp[ci]); n     int rb = i ? m : vc[0]; n     for (int j = 1, _ = min(rb, m - 1); j <= _; ++j) { n       int nxt = ch[1][j]; n       iadd(dp[ci][nxt][val[nxt]][j < rb], 1); n     } n     for (int x = 1; x <= tot; ++x) { n       for (int d = 0; d < m; ++d) { n         int y = ch[x][d]; n         for (int k1 = 0, k2 = val[y]; k2 <= k; ++k1, ++k2) { n           if (d <= vc[i] && dp[pi][x][k1][0]) { n             if (d < vc[i]) { n               iadd(dp[ci][y][k2][1], dp[pi][x][k1][0]); n             } else { n               iadd(dp[ci][y][k2][0], dp[pi][x][k1][0]); n             } n           } n           if (dp[pi][x][k1][1]) { n             iadd(dp[ci][y][k2][1], dp[pi][x][k1][1]); n           } n         } n       } n     } n     swap(pi, ci); n   } n   int ans = 0; n   for (int x = 1; x <= tot; ++x) { n     for (int i = 0; i <= k; ++i) { n       iadd(ans, dp[pi][x][i][0]); n       iadd(ans, dp[pi][x][i][1]); n     } n   } n   return ans; n } n int main() { n   int n = read(); n   m = read(); n   k = read(); n   vector<int> lb; n   { n     vector<int> vc(read()); n     generate(vc.begin(), vc.end(), read); n     while (!vc.back()) { n       lb.emplace_back(m - 1); n       vc.pop_back(); n     } n     --vc.back(); n     lb.insert(lb.begin(), vc.begin(), vc.end()); n     while (!lb.empty() && lb.front() == 0) { n       lb.erase(lb.begin()); n     } n   } n   vector<int> rb; n   { n     rb = vector<int>(read()); n     generate(rb.begin(), rb.end(), read); n   } n   while (n--) { n     vector<int> vc(read()); n     generate(vc.begin(), vc.end(), read); n     add(vc, read()); n   } n   build(); n   int ans = solve(rb); n   iadd(ans, MOD - solve(lb)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 5; n int now; n int n, m, c, a[MAX]; n int main() { n   cin >> n >> m >> c; n   c /= 2; n   while (m--) { n     int x; n     scanf( %d , &x); n     if (x <= c) { n       for (int i = 1; i <= n; i++) { n         if (a[i] == 0 || a[i] > x) { n           a[i] = x; n           printf( %d n , i); n           fflush(stdout); n           break; n         } n       } n     } else { n       for (int i = n; i >= 1; i--) { n         if (a[i] == 0 || a[i] < x) { n           a[i] = x; n           printf( %d n , i); n           fflush(stdout); n           break; n         } n       } n     } n     now = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i] != 0) now++; n     } n     if (now == n) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n long long grundy(long long k) { n   long long i = 1; n   long long sum = 1; n   while (sum <= k) { n     i++; n     sum += i; n   } n   return i - 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long stxor = 0; n   for (long long i = 0; i < n; i++) { n     long long num; n     cin >> num; n     stxor ^= grundy(num); n   } n   if (stxor == 0) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int distance(char a, char b) { n   int d = abs(a - b); n   int d1 = 26 - d; n   return min(d, d1); n } n int main() { n   int n; n   string A; n   int cursor; n   cin >> n >> cursor >> A; n   cursor--; n   n--; n   cursor = min(cursor, n - cursor); n   int middle = n / 2; n   int leftmost = cursor, rightmost = cursor; n   for (int i = cursor; i <= middle; i++) n     if (A[i] != A[n - i]) rightmost = i; n   for (int i = cursor; i >= 0; i--) n     if (A[i] != A[n - i]) leftmost = i; n   int vertical_moves = 0; n   for (int i = leftmost; i <= rightmost; i++) n     vertical_moves += min(abs(A[i] - A[n - i]), 26 - abs(A[i] - A[n - i])); n   int horizontal_moves = min((cursor - leftmost) + (rightmost - leftmost), n                              (rightmost - cursor) + (rightmost - leftmost)); n   int total_moves = horizontal_moves + vertical_moves; n   cout << total_moves; n   return 0; n } n


#include <bits/stdc++.h> n int n, m, x, tab[11][1001][1001], mx[11][1001][1001], i = 1, j = 1, ij = 1; n char a[1001], b[1001], q; n inline int max(int a, int b) { n   if (a >= b) return a; n   return b; n } n int main() { n   scanf( %d %d %d , &n, &m, &x); n   q = getchar(); n   while (i <= n) { n     a[i] = getchar(); n     i++; n   } n   q = getchar(); n   i = 1; n   while (i <= m) { n     b[i] = getchar(); n     i++; n   } n   i = 1; n   while (ij <= x) { n     while (i <= n) { n       while (j <= m) { n         if (a[i] == b[j]) n           tab[ij][i][j] = n               max(tab[ij][i - 1][j - 1] + 1, mx[ij - 1][i - 1][j - 1] + 1); n         else n           tab[ij][i][j] = 0; n         mx[ij][i][j] = max(mx[ij][i - 1][j], mx[ij][i][j - 1]); n         mx[ij][i][j] = max(mx[ij][i][j], tab[ij][i][j]); n         j++; n       } n       i++, j = 1; n     } n     i = 1, j = 1, ij++; n   } n   printf( %d , mx[x][n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long int arr[n]; n     set<long long int> s; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     for (int i = 0; i < n; i++) { n       s.insert(arr[i]); n     } n     if (s.size() == 1) n       cout << n <<   n ; n     else n       cout << 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(long long x, long long y) { n   if (x >= y) return true; n   return false; n } n bool compare(const pair<long long, long long> &i, n              const pair<long long, long long> &j) { n   return i.first <= j.first; n } n bool compare1(const pair<long long, long long> &i, n               const pair<long long, long long> &j) { n   return i.first > j.first; n } n string decitobinary(long long n, long long bit) { n   char c[bit]; n   memset(c,  0 , sizeof(c)); n   long long i = 0; n   while (n > 0) { n     c[i] = n % 2 + 48; n     n = n / 2; n     i++; n   } n   string s =   ; n   for (long long j = bit - 1; j >= 0; j--) s += c[j]; n   return s; n } n long long binarytodeci(string n) { n   string num = n; n   long long dec_value = 0; n   long long base = 1; n   long long len = num.length(); n   for (long long i = len - 1; i >= 0; i--) { n     if (num[i] ==  1 ) dec_value += base; n     base = base * 2; n   } n   return dec_value; n } n long double intlog(long double base, long double x) { n   return (log(x) / log(base)); n } n long long fact(long long n) { n   long long res = 1; n   for (long long i = 2; i <= n; i++) res = res * i; n   return res; n } n long long ncr(long long n, long long k) { n   long long res = 1; n   if (k > n - k) k = n - k; n   for (long long i = 0; i < k; ++i) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n bool ispalindrome(string str, long long l, long long h) { n   while (h > l) { n     if (str[l++] != str[h--]) return 0; n   } n   return 1; n } n long long compute_hash(string s) { n   long long p = 31, hash = 0, po = 1; n   for (char c : s) { n     hash += (c -  a  + 1) * po % 1000000007; n     po = (po * p) % 1000000007; n   } n   return hash; n } n vector<vector<long long>> group_identical_strings(vector<string> const &s) { n   long long n = s.size(); n   vector<pair<long long, long long>> hashes(n); n   for (long long i = 0; i < n; i++) hashes[i] = {compute_hash(s[i]), i}; n   sort(hashes.begin(), hashes.end()); n   vector<vector<long long>> groups; n   for (long long i = 0; i < n; i++) { n     if (i == 0 || hashes[i].first != hashes[i - 1].first) groups.emplace_back(); n     groups.back().push_back(hashes[i].second); n   } n   return groups; n } n bool isarraysort(long long a[], long long i, long long j) { n   if (j == i) return 1; n   if (a[j] < a[j - 1]) return 0; n   return isarraysort(a, i, j - 1); n } n void sieve(long long n, long long prime[]) { n   for (long long i = 1; i <= n; i++) prime[i] = 1; n   for (long long p = 2; p * p <= n; p++) { n     if (prime[p] == 1) { n       for (long long i = p * p; i <= n; i += p) prime[i] = 0; n     } n   } n } n bool isprime(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n long long pow_mod(long long base, long long exp) { n   long long res = 1; n   while (exp > 0) { n     if (exp % 2 == 1) res = (res * base) % 1000000007; n     exp = exp >> 1; n     base = (base * base) % 1000000007; n   } n   return res; n } n const long long N = 1e6 + 3; n vector<vector<long long>> adj; n bool visited[N]; n long long depth[N] = {0}; n long long size[N] = {0}; n long long parent[N]; n void dfs(long long v) { n   visited[v] = true; n   for (long long u : adj[v]) { n     if (!visited[u]) { n       dfs(u); n     } n   } n } n long long time_in[N], time_out[N]; n long long dfs_timer = 0; n long long color[N]; n void dfs_time(long long v) { n   time_in[v] = ++dfs_timer; n   color[v] = 1; n   for (long long u : adj[v]) { n     if (color[u] == 0) { n       dfs_time(u); n     } n   } n   color[v] = 2; n   time_out[v] = ++dfs_timer; n } n long long count1[N]; n void numberOfNodes(long long s) { n   count1[s] = 1; n   for (long long u : adj[s]) { n     if (u == parent[s]) continue; n     numberOfNodes(u); n     count1[s] += count1[u]; n   } n } n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long r[n], l[n]; n   for (long long i = 0; i < n; i++) { n     r[i] = 1; n     l[i] = 1; n   } n   for (long long i = 1; i < n; i++) { n     if (a[i] > a[i - 1]) r[i] = r[i - 1] + 1; n   } n   for (long long i = n - 2; i >= 0; i--) { n     if (a[i] < a[i + 1]) l[i] = l[i + 1] + 1; n   } n   long long mx = r[0]; n   for (long long i = 1; i < n; i++) n     if (r[i] > mx) mx = r[i]; n   long long ans[n]; n   for (long long i = 0; i < n; i++) ans[i] = 0; n   for (long long i = 1; i < n - 1; i++) { n     if (a[i - 1] < a[i + 1]) ans[i] = r[i - 1] + l[i + 1]; n   } n   sort(ans, ans + n); n   cout << max(mx, ans[n - 1]); n } n int32_t main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> primes; n void SOE(long long int n) { n   primes.erase(primes.begin(), primes.end()); n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (long long int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (long long int p = 2; p <= n; p++) n     if (prime[p]) { n       primes.push_back(p); n     } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long int i, c = 0, j, k, t, f = 0, q, n, f1 = 0, y, x, m, l = 0, r, p; n   string s, s1, s2; n   cin >> t; n   while (t--) { n     cin >> n; n     long long int a[n]; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     a[0] < a[n - 1] ? cout <<  YES n  : cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string ans =   ; n set<int> s[26]; n map<int, bool> vis; n void solve(char a) { n   ans += a; n   vis[a -  a ] = true; n   for (auto it : s[a -  a ]) { n     if (!vis[it -  a ]) { n       solve(it); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string str; n     cin >> str; n     int n = str.length(); n     string alp =  abcdefghijklmnopqrstuvwxyz ; n     if (n == 1) { n       cout <<  YES  << endl; n       cout << alp << endl; n       continue; n     } n     for (int i = 0; i < 26; i++) s[i].clear(); n     vis.clear(); n     ans =   ; n     for (long long int i = 1; i < n - 1; i++) { n       s[str[i] -  a ].insert(str[i - 1]); n       s[str[i] -  a ].insert(str[i + 1]); n     } n     s[str[0] -  a ].insert(str[1]); n     s[str[n - 1] -  a ].insert(str[n - 2]); n     int flag = 0, cnt = 0; n     char st; n     for (long long int i = 0; i < 26; i++) { n       if (s[i].size() > 2) { n         flag = 1; n       } else if (s[i].size() == 1) { n         cnt++; n         st = alp[i]; n       } n     } n     if (flag or cnt < 2) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     solve(st); n     for (int i = 0; i < 26; i++) { n       if (!vis[alp[i] -  a ]) ans += alp[i]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1e9 + 7; n string bin(long long n) { n   string ans =   ; n   while (n > 0) { n     if (n % 2 == 0) n       ans =  0  + ans; n     else n       ans =  1  + ans; n     n /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long pmd(long long a, long long b) { n   if (b == 1) return (a + md) % md; n   if (b % 2 == 0) n     return (pmd(a * a, b / 2) + md) % md; n   else n     return (a * pmd(a * a, b / 2) + md) % md; n } n const int xn = 7e1 + 10; n const int xm = 1e2 + 10; n map<pair<pair<int, int>, int>, pair<pair<int, int>, int> > mp; n map<pair<pair<int, int>, int>, pair<int, int> > ans; n map<pair<pair<int, int>, int>, int> m; n vector<pair<int, int> > vec; n char a[xn][xn]; n pair<pair<int, int>, int> srt(pair<pair<int, int>, int> g) { n   int sm = g.first.first + g.first.second + g.second; n   pair<pair<int, int>, int> ns; n   int mn = g.first.first; n   mn = min(mn, g.first.second); n   mn = min(mn, g.second); n   int mx = g.first.first; n   mx = max(mx, g.first.second); n   mx = max(mx, g.second); n   ns.first.first = mn, ns.second = mx, ns.first.second = sm - (mn + mx); n   return ns; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   int s1, s2, s3; n   cin >> s1 >> s2 >> s3; n   for (int i = 1; i <= n; i++) { n     string g; n     cin >> g; n     for (int j = 1; j <= n; j++) a[i][j] = g[j - 1]; n   } n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       for (int k = j + 1; k <= n; k++) m[{{i, j}, k}] = -1; n   queue<pair<pair<int, int>, int> > q; n   q.push(srt({{s1, s2}, s3})); n   m[srt({{s1, s2}, s3})] = 0; n   while (q.size()) { n     int v = q.front().first.first, u = q.front().first.second, n         s = q.front().second; n     q.pop(); n     for (int i = 1; i <= n; i++) { n       if (i == v || i == u || i == s) continue; n       if (a[v][i] == a[u][s] && m[srt({{i, u}, s})] == -1) n         q.push(srt({{i, u}, s})), n             mp[srt({{i, u}, s})] = {{v, u}, s}, ans[srt({{i, u}, s})] = {v, i}, n                             m[srt({{i, u}, s})] = m[{{v, u}, s}] + 1; n     } n     for (int i = 1; i <= n; i++) { n       if (i == v || i == u || i == s) continue; n       if (a[u][i] == a[v][s] && m[srt({{i, v}, s})] == -1) n         q.push(srt({{i, v}, s})), n             mp[srt({{i, v}, s})] = {{v, u}, s}, ans[srt({{i, v}, s})] = {u, i}, n                             m[srt({{i, v}, s})] = m[{{v, u}, s}] + 1; n     } n     for (int i = 1; i <= n; i++) { n       if (i == v || i == u || i == s) continue; n       if (a[s][i] == a[u][v] && m[srt({{i, u}, v})] == -1) n         q.push(srt({{i, u}, v})), n             mp[srt({{i, u}, v})] = {{v, u}, s}, ans[srt({{i, u}, v})] = {s, i}, n                             m[srt({{i, u}, v})] = m[{{v, u}, s}] + 1; n     } n   } n   if (m[{{1, 2}, 3}] == -1) { n     cout << -1 <<   n ; n     return 0; n   } n   pair<pair<int, int>, int> d; n   d = {{1, 2}, 3}; n   cout << m[{{1, 2}, 3}] <<   n ; n   while (m[srt(d)] > 0) { n     vec.push_back(ans[srt(d)]); n     d = srt(mp[srt(d)]); n   } n   for (int i = vec.size() - 1; i >= 0; i--) n     cout << vec[i].first <<     << vec[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxF = (long long)1e9 * 1e9; n const int maxn = 100, maxm = 100, maxlen = maxn + maxm + 1; n int n, m, len, map[maxn + 5][maxm + 5], pri[maxlen], cir[maxlen]; n long long dp[maxlen][(maxlen >> 1) + 1], k; n long long cal() { n   memset(dp, 0, sizeof(dp)); n   dp[0][1] = 1; n   int up = len >> 1; n   for (int p = 1; p < len; p++) n     for (int d = 0; d <= up; d++) { n       if (cir[p] != 1 && d < up) dp[p][d] += dp[p - 1][d + 1]; n       if (cir[p] != -1 && d > 0) dp[p][d] += dp[p - 1][d - 1]; n       if (dp[p][d] >= maxF) dp[p][d] = maxF; n     } n   return dp[len - 1][0]; n } n int main() { n   cin >> n >> m >> k; n   memset(pri, -1, sizeof(pri)); n   for (int i = 0, t; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> t; n       pri[i + j] = pri[i + j] == -1 ? t : min(pri[i + j], t); n     } n   len = n + m - 1; n   memset(cir, 0, sizeof(cir)); n   cir[0] = 1; n   cir[len - 1] = -1; n   for (int t = 1; t < len - 1; t++) { n     int pos, minPri = 100000000; n     for (int p = 1; p < len - 1; p++) n       if (cir[p] == 0 && minPri > pri[p]) pos = p, minPri = pri[p]; n     cir[pos] = 1; n     long long s = cal(); n     if (k > s) k -= s, cir[pos] = -1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout << (cir[i + j] == 1 ?  (  :  ) ); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T checkmin(T &a, T b) { n   return (a < b) ? a : a = b; n } n template <class T> n inline T checkmax(T &a, T b) { n   return (a > b) ? a : a = b; n } n template <class T> n T GCD(T a, T b) { n   if (a < 0) return GCD(-a, b); n   if (b < 0) return GCD(a, -b); n   return (a == 0) ? b : GCD(b % a, a); n } n template <class T> n T LCM(T a, T b) { n   if (a < 0) return LCM(-a, b); n   if (b < 0) return LCM(a, -b); n   return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b; n } n template <class T> n inline T sqr(T X) { n   return X * X; n } n namespace Poor { n const int MaxiL = 1000005; n const int MaxiN = 205; n const int MaxiM = 205; n const int MaxiQL = 3005; n int N, L; n char St[MaxiL]; n char Rule[MaxiQL]; n int SeqX[MaxiM], SeqY[MaxiM]; n int Stack[MaxiL]; n inline bool Same(const int a, const int b, const int c, const int d) { n   if (b - a != d - c) return 0; n   for (register int i = 0; i < b - a; ++i) n     if (St[a + i] != Rule[c + i]) return 0; n   return 1; n } n void Run() { n   gets(St); n   L = strlen(St); n   scanf( %d n , &N); n   for (int i = 0; i < N; ++i) { n     gets(Rule); n     int M = 0; n     for (int p = 0; Rule[p] !=   0 ; ++p) n       if (isalpha(Rule[p])) { n         int q = p; n         for (; isalpha(Rule[q]); ++q) n           ; n         SeqX[M] = p; n         SeqY[M] = q; n         ++M; n         p = q - 1; n       } n     register int res = 0; n     register int *Top = Stack; n     *Top = 0; n     ++Top; n     for (register int p = 0, q = 0; p < L; p = q + 1) { n       for (q = p; St[q] !=  > ; ++q) n         ; n       if (St[p + 1] ==  / ) { n         --Top; n         continue; n       } n       if (St[q - 1] ==  / ) { n         if (*(Top - 1) >= M - 1 && Same(p + 1, q - 1, SeqX[M - 1], SeqY[M - 1])) n           ++res; n         continue; n       } n       *Top = *(Top - 1); n       if (*Top < M - 1) { n         if (Same(p + 1, q, SeqX[*Top], SeqY[*Top])) ++*Top; n       } else { n         if (Same(p + 1, q, SeqX[M - 1], SeqY[M - 1])) ++res; n       } n       ++Top; n     } n     printf( %d n , res); n   } n } n }  // namespace Poor n int main() { n   Poor::Run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long seg[200005], h[100005], dp[100005]; n int n, cnt, arr[100005], a[100005], b[100005]; n map<int, int> mn; n map<int, long long> tot; n inline void modify(int pos, long long x) { n   for (seg[pos += cnt] += x; pos; pos >>= 1) n     seg[pos >> 1] = max(seg[pos], seg[pos ^ 1]); n } n inline long long query(int l, int r) { n   long long res = 0; n   for (l += cnt, r += cnt; l < r; l >>= 1, r >>= 1) { n     if (l & 1) res = max(res, seg[l++]); n     if (r & 1) res = max(res, seg[--r]); n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a, b, h; n     scanf( %d%d%d , &a, &b, &h); n     if (mn.count(b)) n       mn[b] = min(mn[b], a); n     else n       mn[b] = a; n     tot[b] += h; n   } n   for (map<int, int>::iterator it = mn.begin(); it != mn.end(); it++) { n     arr[cnt] = it->first; n     a[cnt] = it->second; n     b[cnt] = it->first; n     h[cnt++] = tot[it->first]; n   } n   long long ans = 0; n   for (int i = 0; i < cnt; i++) { n     dp[i] = query(lower_bound(arr, arr + cnt, a[i] + 1) - arr, n                   lower_bound(arr, arr + cnt, b[i]) - arr + 1) + n             h[i]; n     modify(lower_bound(arr, arr + cnt, b[i]) - arr, dp[i]); n     ans = max(ans, dp[i]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; n const int Nmax = 200010; n int n, k; n long long a[20000], pref[20000]; n long long f[60][500][500]; n long long diff[500][2]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     pref[i] = pref[i - 1] + a[i]; n   }; n   for (int i = 0; i <= n; i++) diff[i][0] = diff[i][1] = -2000000000; n   for (int i = 1; i <= n; i++) n     for (int j = i; j <= n; j++) { n       diff[j][0] = max(diff[j][0], pref[j] - pref[i - 1]); n       diff[j][1] = max(pref[i - 1] - pref[j], diff[j][1]); n     }; n   long long answer = 0; n   for (int t = 2; t <= k; t++) { n     for (int i = 1; i <= n; i++) n       for (int j = i; j <= n; j++) n         f[t][i][j] = max(diff[i - 1][0] - pref[j] + pref[i - 1], n                          diff[i - 1][1] - pref[i - 1] + pref[j]); n     for (int i = 0; i < n; i++) diff[i][0] = diff[i][1] = -2000000000; n     for (int i = 1; i <= n; i++) n       for (int j = i; j <= n; j++) { n         diff[j][0] = max(diff[j][0], f[t][i][j] + pref[j] - pref[i - 1]); n         diff[j][1] = max(diff[j][1], f[t][i][j] - pref[j] + pref[i - 1]); n       }; n   }; n   for (int i = 1; i <= n; i++) n     for (int j = i; j <= n; j++) answer = max(answer, (long long)f[k][i][j]); n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long add(long long a, long long b, long long p = 1000000007) { n   long long c = a + b; n   if (c >= p) c -= p; n   return c; n } n inline long long sub(long long a, long long b, long long p = 1000000007) { n   long long c = a - b; n   if (c < 0) c += p; n   return c; n } n inline long long mul(long long a, long long b, long long p = 1000000007) { n   return ((a % p) * (b % p)) % p; n } n long long power(long long x, long long y, long long p = 1000000007) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n vector<int> g[200005]; n int t = 0; n int tim[500005]; n void dfs(int u, int v = -1) { n   tim[t++] = u; n   for (auto x : g[u]) { n     if (x == v) continue; n     dfs(x, u); n   } n   tim[t++] = u; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   int a; n   cin >> n; n   int p[n + 1]; n   for (int i = 2; i <= n; i++) { n     cin >> a; n     g[i].push_back(a); n     g[a].push_back(i); n   } n   dfs(1); n   map<int, int> mp; n   for (int i = 0; i <= 2 * n - 1; i++) { n     mp[tim[i]]++; n     if (tim[i] == n) break; n   } n   for (auto it = mp.begin(); it != mp.end(); it++) { n     if (it->second == 1) { n       cout << it->first <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long infll = powl(2, 63) - 1; n struct cup { n   long long a, b, c; n }; n bool comp(cup x, cup y) { return x.c < y.c; } n bool cmp1(cup x, cup y) { n   if (x.a == y.a) return x.c < y.c; n   return x.a > y.a; n } n int32_t main() { n   long long n, tea; n   cin >> n >> tea; n   vector<cup> arr(n); n   for (long long i = 0; i < (long long)(n); ++i) cin >> arr[i].a; n   for (long long i = 0; i < (long long)(n); ++i) { n     arr[i].b = ceil(arr[i].a / 2.0); n     arr[i].c = i; n     tea -= arr[i].b; n     if (tea < 0) { n       cout << -1 << endl; n       return 0; n     } n   } n   sort((arr).begin(), (arr).end(), cmp1); n   for (long long i = 0; i < (long long)(n); ++i) { n     long long temp = min(tea, arr[i].a - arr[i].b); n     arr[i].b += temp; n     tea -= temp; n     if (tea == 0) break; n   } n   sort((arr).begin(), (arr).end(), comp); n   for (long long i = 0; i < (long long)(n); ++i) cout << arr[i].b <<    ; n   cout << 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   long long x, y, m; n   cin >> x >> y >> m; n   long long sol = 0ll; n   if (x >= m || y >= m) { n     cout <<  0 n ; n     return 0; n   } n   if (x <= 0ll && y <= 0ll) { n     cout <<  -1 n ; n     return 0; n   } n   if (x > y) { n     swap(x, y); n   } n   if (x < 0ll) { n     sol += ((-x) / y); n     x = -((-x) % y); n   } n   while (x < m && y < m) { n     if (x <= y) { n       x += y; n     } else { n       y += x; n     } n     sol++; n   } n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using std::bitset; n const double Pi = acos(-1); n namespace fastIO { n template <class T> n inline void read(T &x) { n   x = 0; n   bool fu = 0; n   char ch = 0; n   while (ch >  9  || ch <  0 ) { n     ch = getchar(); n     if (ch ==  - ) fu = 1; n   } n   while (ch <=  9  && ch >=  0 ) x = (x * 10 - 48 + ch), ch = getchar(); n   if (fu) x = -x; n } n inline int read() { n   int x = 0; n   bool fu = 0; n   char ch = 0; n   while (ch >  9  || ch <  0 ) { n     ch = getchar(); n     if (ch ==  - ) fu = 1; n   } n   while (ch <=  9  && ch >=  0 ) x = (x * 10 - 48 + ch), ch = getchar(); n   return fu ? -x : x; n } n char _n_u_m_[40]; n template <class T> n inline void write(T x) { n   if (x == 0) { n     putchar( 0 ); n     return; n   } n   T tmp = x > 0 ? x : -x; n   if (x < 0) putchar( - ); n   register int cnt = 0; n   while (tmp > 0) { n     _n_u_m_[cnt++] = tmp % 10 +  0 ; n     tmp /= 10; n   } n   while (cnt > 0) putchar(_n_u_m_[--cnt]); n } n template <class T> n inline void write(T x, char ch) { n   write(x); n   putchar(ch); n } n }  // namespace fastIO n using namespace fastIO; n const int MAXN = 1000005; n int len[MAXN], ch[MAXN][26], fail[MAXN], cnt, lst, up[MAXN], orz[MAXN]; n long long qwq[MAXN], ans[MAXN]; n char c[MAXN], ccf[MAXN]; n int now, n; n inline void insert(int pos) { n   while (c[pos] != c[pos - len[lst] - 1]) lst = fail[lst]; n   if (!ch[lst][c[pos] - 97]) { n     len[++cnt] = len[lst] + 2; n     register int i = fail[lst]; n     while (c[pos] != c[pos - len[i] - 1]) i = fail[i]; n     fail[cnt] = ch[i][c[pos] - 97]; n     ch[lst][c[pos] - 97] = cnt; n     orz[cnt] = len[cnt] - len[fail[cnt]]; n     if (orz[cnt] == orz[fail[cnt]]) n       up[cnt] = up[fail[cnt]]; n     else n       up[cnt] = fail[cnt]; n   } n   lst = ch[lst][c[pos] - 97]; n } n int main() { n   len[1] = -1; n   fail[0] = 1; n   up[0] = 1; n   ans[0] = 1; n   cnt = 1; n   scanf( %s , ccf + 1); n   n = strlen(ccf + 1); n   if (n % 2) { n     cerr <<  Fuck CCF!!!  << endl; n     cout << 0; n     return 0; n   } n   for (register int i = 1, iend = n / 2; i <= iend; ++i) { n     c[2 * i - 1] = ccf[i]; n     c[i << 1] = ccf[n - i + 1]; n   } n   for (register int i = 1, iend = n; i <= iend; ++i) { n     insert(i); n     for (register int j = lst; j; j = up[j]) { n       qwq[j] = ans[i - len[up[j]] - orz[j]]; n       if (up[j] != fail[j]) qwq[j] += qwq[fail[j]]; n       qwq[j] %= 1000000007ll; n       if (!(i & 1)) ans[i] += qwq[j], ans[i] %= 1000000007ll; n     } n   } n   cout << ans[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int T, n; n long long W; n pair<long long, int> w[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   for (cin >> T; T--;) { n     vector<int> id; n     cin >> n >> W; n     long long x = W; n     for (int i = 1; i <= n; ++i) cin >> w[i].first, w[i].second = i; n     sort(w + 1, w + n + 1, greater<pair<int, int> >()); n     for (int i = 1; i <= n; ++i) n       if (w[i].first <= x) x -= w[i].first, id.push_back(w[i].second); n     if (W - x < (W + 1) / 2) n       cout <<  -1 n ; n     else { n       cout << id.size() <<   n ; n       for (int i : id) cout << i <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2004; n const int INF = 0x3f3f3f3f; n char s[MAXN][MAXN]; n int u[MAXN][MAXN], d[MAXN][MAXN]; n int l[MAXN][MAXN], r[MAXN][MAXN]; n int dp[MAXN][MAXN]; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %s , s[i]); n   for (int i = 0; i < n; ++i) { n     l[i][0] = 1; n     r[i][m - 1] = 1; n     dp[i][0] = dp[i][m - 1] = 1; n     for (int j = 1; j < m; ++j) { n       if (s[i][j - 1] == s[i][j]) n         l[i][j] = l[i][j - 1] + 1; n       else n         l[i][j] = 1; n     } n     for (int j = m - 2; j >= 0; --j) { n       if (s[i][j + 1] == s[i][j]) n         r[i][j] = r[i][j + 1] + 1; n       else n         r[i][j] = 1; n       dp[i][j] = min(l[i][j], r[i][j]); n     } n   } n   for (int j = 0; j < m; ++j) { n     u[0][j] = 1; n     d[n - 1][j] = 1; n     for (int i = 1; i < n; ++i) { n       if (s[i - 1][j] == s[i][j]) n         u[i][j] = min(dp[i][j], u[i - 1][j] + 1); n       else n         u[i][j] = 1; n     } n     for (int i = n - 2; i >= 0; --i) { n       if (s[i + 1][j] == s[i][j]) n         d[i][j] = min(dp[i][j], d[i + 1][j] + 1); n       else n         d[i][j] = 1; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       ans += min(u[i][j], d[i][j]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n int n, siz[N]; n vector<int> e[N]; n double f[N][N], sav[N], C[N][N], fac[N]; n void dfs(int x, int fa) { n   siz[x] = 0, f[x][0] = 1; n   for (const int &v : e[x]) n     if (v != fa) { n       dfs(v, x), ++siz[v]; n       double sum = 0, o; n       for (int a = (0); a <= (siz[v]); a++) n         o = f[v][a], f[v][a] = f[v][a] * (siz[v] - a) + sum / 2, sum += o; n       for (int a = (0); a <= (siz[x]); a++) n         for (int b = (0); b <= (siz[v]); b++) n           sav[a + b] += C[a + b][a] * n                         C[(siz[x] - a) + (siz[v] - b)][siz[x] - a] * f[x][a] * n                         f[v][b]; n       siz[x] += siz[v]; n       for (int a = (0); a <= (siz[x]); a++) f[x][a] = sav[a], sav[a] = 0; n     } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.precision(12), cout << fixed; n   cin >> n; n   fac[0] = C[0][0] = 1; n   for (int i = (1); i <= (n); i++) fac[i] = fac[i - 1] * i, C[i][0] = 1; n   for (int i = (1); i <= (n); i++) n     for (int j = (1); j <= (i); j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n   for (int i = 1, u, v; i < n; i++) n     cin >> u >> v, e[u].push_back(v), e[v].push_back(u); n   for (int i = (1); i <= (n); i++) n     memset(f, 0, sizeof(f)), dfs(i, -1), n         cout << f[i][n - 1] / fac[n - 1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M = 1e9 + 7; n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> a(n); n   long long int sum = 0; n   long long int x = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n     x = x ^ a[i]; n   } n   cout << 2 <<   n ; n   cout << x <<     << sum + x <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 305000; n const int INF = 105000; n int main() { n   long long n, k; n   cin >> n >> k; n   long long g = (n / 2) / (k + 1); n   long long d = g * k; n   long long ost = n - d - g; n   cout << g <<     << d <<     << ost; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, vector<string> > graph; n unsigned long long int countSetBits(long long int n) { n   unsigned long long int count = 0; n   while (n) { n     n &= (n - 1); n     count++; n   } n   return count; n } n bool valid(set<string>& t) { n   for (auto i = t.begin(); i != t.end(); i++) { n     for (auto k : graph[*i]) { n       if (t.find(k) != t.end()) { n         return false; n       } n     } n   } n   return true; n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   vector<string> players(n); n   for (long long int i = 0; i < n; i++) { n     cin >> players[i]; n   } n   for (long long int i = 0; i < m; i++) { n     string a, b; n     cin >> a >> b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   set<string> ans; n   for (long long int i = 1; i < (1 << (n + 1)); i++) { n     set<string> tmp; n     for (long long int j = 0; j <= log2(i); j++) { n       if (i & (1 << j)) { n         tmp.insert(players[min(n - 1, j)]); n       } n     } n     if (ans.size() < tmp.size() && valid(tmp)) { n       ans = tmp; n     } n     tmp.clear(); n   } n   cout << ans.size() <<   n ; n   for (string s : ans) { n     cout << s <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int T = 1; n   while (T--) { n     solve(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LLINF = 1ll << 60; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(25); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     int cnt = 1; n     vector<int> v; n     for (int i = 1; i < n; i++) { n       if (s[i] == s[i - 1]) n         cnt++; n       else { n         v.push_back(cnt); n         cnt = 1; n       } n     } n     v.push_back(cnt); n     int ans = 0; n     int l = (int)v.size(); n     for (int i = 0; i < l; i++) ans = min(i + 1, ans + v[i] - 1); n     cout << ans + (l - ans + 1) / 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8; n bool vis[N][N]; n int n, m, ans, a[8]; n vector<pair<int, int> > edges; n vector<int> v; n int calc() { n   memset(vis, 0, sizeof vis); n   for (auto X : edges) { n     int x = X.first; n     int y = X.second; n     vis[a[x]][a[y]] = vis[a[y]][a[x]] = 1; n   } n   int ret = 0; n   for (int i = 1; i <= 6; i++) n     for (int j = i; j <= 6; j++) ret += vis[i][j]; n   return ret; n } n void bt(int i) { n   if (i == n + 1) { n     for (int i = 1; i <= n; i++) a[i] = v[i]; n     ans = max(ans, calc()); n     return; n   } n   for (int j = 1; j <= 6; j++) { n     v.push_back(j); n     bt(i + 1); n     v.pop_back(); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     edges.push_back({a, b}); n   } n   if (n <= 6) { n     printf( %d n , m); n     return 0; n   } n   v.push_back(69); n   bt(1); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 3; n int a[MAXN], b[MAXN], p[MAXN]; n int read() { n   int x = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c - 48; n     c = getchar(); n   } n   return x * w; n } n int main() { n   int n = read(), ans = 0; n   for (int i = 1; i <= n; i++) a[i] = read(), p[a[i]] = 0; n   for (int i = 1; i <= n; i++) b[i] = read(), p[b[i]] = i; n   if (p[1]) { n     int i, j; n     for (i = 1; p[i] == p[1] + i - 1; i++) n       ; n     if (p[i - 1] == n) { n       for (j = i; j <= n && p[j] <= j - i; j++) n         ; n       if (j > n) { n         printf( %d n , n - i + 1); n         return 0; n       } n     } n   } n   for (int i = 1; i <= n; i++) ans = max(ans, p[i] - (i - 1) + n); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 501000; n int n, a[N]; n bool check(int p) { n   for (int i = p - 1; i >= 0; i--) { n     if (a[i] * 2 > a[n - p + i]) return false; n   } n   return true; n } n int work() { n   int l = 0, r = n >> 1; n   while (l <= r) { n     int mid = l + r >> 1; n     if (check(mid)) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return n - (l - 1); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   printf( %d n , work()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); n template <class T = int> n T __rand(T range = numeric_limits<T>::max()) { n   return (T)(rng() % range); n } n int debug = 0; n int __db_level = 0; n struct debug_block { n   string name; n   debug_block(const string& name_) : name(name_) { n     if (debug) cerr << string(__db_level * 2,    ) <<  {   << name << endl; n     ++__db_level; n   } n   ~debug_block() { n     --__db_level; n     if (debug) cerr << string(__db_level * 2,    ) <<  }   << name << endl; n   } n }; n template <class U, class V> n ostream& operator<<(ostream& out, const pair<U, V>& p) { n   return out <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class A, class B> n ostream& operator<<(ostream& out, const tuple<A, B>& t) { n   return out <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ) ; n } n template <class A, class B, class C> n ostream& operator<<(ostream& out, const tuple<A, B, C>& t) { n   return out <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ,   << get<2>(t) n              <<  ) ; n } n template <class T> n ostream& operator<<(ostream& out, const vector<T>& container) { n   out <<  { ; n   if (((int)container.size())) out << container[0]; n   for (int i = 0; i++ < ((int)container.size()) - 1;) n     out <<  ,   << container[i]; n   return out <<  } ; n } n template <class x> n vector<typename x::value_type> $v(const x& a) { n   return vector<typename x::value_type>(a.begin(), a.end()); n } n template <class u> n vector<typename iterator_traits<u>::value_type> $v(u a, u b) { n   return vector<typename iterator_traits<u>::value_type>(a, b); n } n int main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int ntest; n   cin >> ntest; n   for (int testcase = 0; testcase++ < ntest;) { n     ; n     int n, m, k; n     cin >> n >> m >> k; n     vector<int> a(n); n     for (auto& i : a) cin >> i; n     int ans = 0; n     k = min(k, m - 1); n     int left = m - 1 - k; n     int width = n - k - left; n     if (debug) n       cerr << string(__db_level * 2,    ) <<  [  n            <<  width  n            <<  ] = [  << width <<  ]  << endl; n     for (int i = -1; ++i < k + 1;) { n       int cur_ans = INT_MAX; n       for (int f = -1; ++f < left + 1;) { n         int t = max(a[i + f], a[i + f + width - 1]); n         if (debug) n           cerr << string(__db_level * 2,    ) <<  [  n                <<  i _ f _ t  n                <<  ] = [  << i <<  ,   << f <<  ,   << t <<  ]  << endl; n         cur_ans = min(cur_ans, t); n       } n       if (debug) n         cerr << string(__db_level * 2,    ) <<  [  n              <<  i _ cur_ans  n              <<  ] = [  << i <<  ,   << cur_ans <<  ]  << endl; n       ans = max(ans, cur_ans); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char dat[1048576]; n long long countSub(long long a, long long b, long long left, long long right, n                    long long d) { n   long long low = b * d - a; n   if (low < 0) { n     if (a % b != 0) { n       low = b - a % b; n     } else { n       low = 0; n     } n   } n   long long high = left + right; n   if (low > high) return 0; n   high -= (high - low) % b; n   if (left > right) swap(left, right); n   long long ans = 0; n   long long firstcnt = 0, fb = low; n   long long secondcnt = 0, sb = 0; n   long long thirdcnt = 0, tb = 0; n   if (fb <= left) { n     if (high <= left) { n       firstcnt = (high - fb) / b + 1; n     } else { n       firstcnt = (left - fb) / b + 1; n     } n   } n   sb = fb + firstcnt * b; n   if (high >= sb && sb <= right) { n     if (high <= right) { n       secondcnt = (high - sb) / b + 1; n     } else if (high > right) { n       secondcnt = (right - sb) / b + 1; n     } n   } n   tb = sb + secondcnt * b; n   if (high >= tb) { n     thirdcnt = (high - tb) / b + 1; n   } n   if (firstcnt > 0) n     ans += ((fb + 1) + (fb + (firstcnt - 1) * b + 1)) * firstcnt / 2; n   if (secondcnt > 0) ans += ((left + 1) + (left + 1)) * secondcnt / 2; n   if (thirdcnt > 0) n     ans += ((left + right + 1 - (tb)) + n             (left + right + 1 - (tb + (thirdcnt - 1) * b))) * n            thirdcnt / 2; n   return ans; n } n int main() { n   scanf( %s , dat); n   int n = (int)strlen(dat); n   int n0 = 0, n1 = 0; n   for (int i = 0; i < n; i++) { n     if (dat[i] ==  0 ) { n       n0++; n     } else { n       n1++; n     } n   } n   long long ans = 0; n   int maxratio = 0; n   for (int ratio = 0; ratio <= 100 || 12 * ratio * ratio <= n; ratio++) { n     maxratio = ratio; n     long long depth = 0; n     unordered_map<long long, int> cnt; n     cnt.reserve(n + 10); n     cnt[0] = 1; n     for (int i = 0; i < n; i++) { n       if (dat[i] ==  0 ) { n         depth--; n       } else { n         depth += ratio; n       } n       ans += (cnt[depth]++); n     } n   } n   vector<int> ones; n   for (int i = 0; i < n; i++) n     if (dat[i] ==  1 ) ones.emplace_back(i); n   for (int i = 0; i < ones.size(); i++) { n     for (int j = i; j < ones.size(); j++) { n       int onecnt = j - i + 1; n       if (onecnt * maxratio > n0) break; n       int left = (i == 0 ? ones[0] : (ones[i] - ones[i - 1] - 1)); n       int right = (j + 1 == ones.size() ? (n - 1 - ones[j]) n                                         : (ones[j + 1] - ones[j] - 1)); n       int zerocnt = ones[j] - ones[i] + 1 - onecnt; n       ans += countSub(zerocnt, onecnt, left, right, maxratio + 1); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, s[200005], d[200005], k; n   while (cin >> n >> t) { n     int maxx = 0x3f3f3f3f; n     for (int i = 1; i <= n; i++) { n       cin >> s[i] >> d[i]; n       int change = s[i]; n       while (change < t) { n         change += d[i]; n       } n       if (change - t < maxx) { n         maxx = change - t; n         k = i; n       } n     } n     cout << k << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e4 + 5; n const double eps = 1e-10; n int n, m, cnt, v[N], tr[N << 1]; n double a[N], b[N]; n pair<double, int> e[N << 1]; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n int lowbit(int x) { return x & (-x); } n void add(int x, int y) { n   while (x <= cnt) tr[x] += y, x += lowbit(x); n } n int query(int x) { n   int res = 0; n   while (x) res += tr[x], x -= lowbit(x); n   return res; n } n void work(double r) { n   cnt = 0; n   for (int i = 1; i <= n; i++) { n     double a = ::a[i], b = ::b[i], n            d = 4.0 * (a * a * b * b - (a * a + 1.0) * (b * b - r * r)); n     if (d < eps) continue; n     d = sqrt(d); n     double x = (-2.0 * a * b - d) / ((a * a + 1.0) * 2.0), n            xx = (-2.0 * a * b + d) / ((a * a + 1.0) * 2.0); n     double y = a * x + b, yy = a * xx + b; n     e[++cnt] = make_pair(atan2(y, x), i); n     e[++cnt] = make_pair(atan2(yy, xx), i); n   } n   sort(e + 1, e + cnt + 1); n   for (int i = 1; i <= cnt; i++) v[e[i].second] = i; n } n int check(double r) { n   work(r); n   for (int i = 1; i <= cnt; i++) tr[i] = 0; n   long long ans = 0; n   for (int i = 1; i <= cnt; i++) n     if (v[e[i].second] != i) { n       ans += query(v[e[i].second]) - query(i - 1); n       add(v[e[i].second], 1); n     } n   return ans >= m; n } n double calc(int i, int j) { n   double x = (b[j] - b[i]) / (a[i] - a[j]), y = a[i] * x + b[i]; n   return sqrt(x * x + y * y); n } n double calc(double r) { n   work(r); n   set<pair<double, int> > s; n   double ans = 0; n   int tot = 0; n   for (int i = 1; i <= cnt; i++) n     if (v[e[i].second] != i) { n       auto it = s.upper_bound(make_pair(i + 1, 0)); n       while (it != s.end() && it->first < v[e[i].second]) n         ans += calc(e[i].second, it->second), it++, tot++; n       s.insert(make_pair(v[e[i].second], e[i].second)); n     } n   return ans + (m - tot) * r; n } n int main() { n   n = read(); n   double p = (double)read() / 1000.0, q = (double)read() / 1000.0; n   m = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = (double)read() / 1000.0; n     b[i] = (double)read() / 1000.0; n     b[i] += a[i] * p - q; n   } n   double l = 0, r = 1e10; n   for (int i = 1; i <= 100; i++) { n     double mid = (l + r) / 2.0; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %.10f n , max((double)0, calc(r))); n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   string s; n   string t; n   cin >> s >> t; n   string result(s); n   int carry = 1; n   for (int ii = s.length() - 1; ii >= 0; --ii) { n     result[ii] = (char)(s[ii] + carry); n     if (result[ii] >  z ) { n       result[ii] =  a ; n       carry = 1; n     } else { n       carry = 0; n     } n   } n   if (result == t) { n     cout <<  No such string  << endl; n   } else { n     cout << result << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long s[100007]; n long long l[100007]; n int main() { n   long long n, k, r = 0; n   cin >> n >> k; n   map<int, int> m; n   map<int, int> e[10]; n   long long ki = 0; n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &s[i]); n     r += s[i] / 10; n     if (s[i] != 100) { n       m[10 - (s[i] % 10)]++; n     } n     if ((s[i] % 10) == 0) { n       ki += (10 - s[i] / 10); n     } n     if (s[i] / 10 <= 9) { n       e[s[i] / 10][10 - (s[i] % 10)]++; n     } n   } n   map<int, int>::iterator it; n   for (it = m.begin(); it != m.end() && k > 0; it++) { n     long long gap = it->first; n     if (gap == 10) { n       break; n     } n     long long tot = it->second; n     if (gap * tot <= k) { n       if (gap < 10) { n         k -= gap * tot; n         r += tot; n         m[10] += tot; n         m[gap] = 0; n         for (int i = 9; i > 0; i--) { n           e[i][10] += e[i - 1][gap]; n           ki += (10 - i) * e[i - 1][gap]; n         } n         m[10] -= e[9][gap]; n       } else if (gap == 10) { n         r += tot; n       } n     } else { n       r += (k / gap); n       m[10] += (k / gap); n       k = 0; n     } n   } n   if (it != m.end() && k > 0) { n     long long gap = it->first; n     long long tot = it->second; n     if (gap * ki <= k) { n       r += ki; n     } else { n       r += (k / gap); n     } n   } n   cout << r; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[10000], b, c, d, e, f, i, j, k, l, t; n   cin >> t; n   while (t--) { n     d = 0; n     cin >> b >> c; n     for (i = 0; i < b; i++) { n       cin >> a[i]; n       d += a[i]; n     } n     if (d < c) { n       cout << d << endl; n     } else if (d >= c) { n       cout << c << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a, b; n void perform(int len) { n   for (int i = 0; i < len / 2; ++i) { n     if (a[i] == a[len - 1 - i]) { n       a[i] =  0  +  1  - a[i]; n       a[len - 1 - i] =  0  +  1  - a[len - 1 - i]; n     } n   } n   if (len & 1) a[len / 2] =  0  +  1  - a[len / 2]; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> a >> b; n     int k = 0; n     vector<int> ops; n     for (int i = n - 1; i >= 0; --i) { n       if (a[i] != a[0]) perform(1), ++k, ops.push_back(1); n       if (a[i] != b[i]) perform(i + 1), ++k, ops.push_back(i + 1); n     } n     cout << k <<    ; n     for (int x : ops) cout << x <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3,unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n mt19937 rnd(time(nullptr)); n const int N = 1e5 + 5; n unordered_set<int> g[N]; n bool check(int v) { n   for (auto i : g[v]) n     for (auto j : g[v]) n       if (i != j && !g[i].count(j)) return false; n   cout <<  2 n  << v <<    ; n   for (auto i : g[v]) cout << i <<    ; n   cout <<   n ; n   return true; n } n void solve() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) g[i].clear(); n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].insert(v), g[v].insert(u); n   } n   set<pair<int, int> > st; n   for (int i = 1; i <= n; ++i) { n     st.insert({g[i].size(), i}); n   } n   if ((long long)k * (k - 1) > 2 * m) { n     cout <<  -1 n ; n     return; n   } n   while (st.size()) { n     pair<int, int> v = *st.begin(); n     if (v.first >= k) break; n     if (v.first == k - 1 && check(v.second)) return; n     st.erase(st.begin()); n     for (auto to : g[v.second]) { n       st.erase({g[to].size(), to}); n       g[to].erase(v.second); n       st.insert({g[to].size(), to}); n     } n   } n   if (st.size()) { n     cout <<  1   << st.size() <<   n ; n     for (auto v : st) cout << v.second <<    ; n     cout <<   n ; n   } else n     cout <<  -1 n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, h, l, r, dp[2005][2005]; n int main() { n   scanf( %d%d%d%d , &n, &h, &l, &r); n   memset(dp, 0xc0, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 1, x; i <= n; i++) { n     scanf( %d , &x); n     for (int j = 0; j < h; j++) { n       dp[i][j] = max(dp[i][j], dp[i - 1][(j - x + h) % h] + (j >= l && j <= r)); n       dp[i][j] = n           max(dp[i][j], dp[i - 1][(j - x + 1 + h) % h] + (j >= l && j <= r)); n     } n   } n   int ans = 0; n   for (int i = 0; i < h; i++) ans = max(ans, dp[n][i]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void File() { n   freopen( output.txt ,  w , stdout); n   freopen( input.txt ,  r , stdin); n } n int countDivisors(long long n) { n   int cnt = 0; n   for (long long i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         cnt++; n       else n         cnt += 2; n     } n   } n   return cnt; n } n int const N = 5e5 + 9, MOD = 1e9 + 7, oo = 1e9 + 7; n vector<vector<int>> adj(N); n vector<bool> visited(N); n vector<int> start(N), finish(N); n int n = 5; n long long arr[N]; n int one, zero; n int main() { n   cin >> n; n   for (int i = 0; i < int(n); i++) cin >> arr[i]; n   sort(arr, arr + n); n   for (int i = n - 1; i >= 0; i--) { n     swap(arr[i], arr[n - 1]); n     int l = i - 1; n     int r = i + 1; n     if (r == n) r = 0; n     if (l == -1) l = n - 1; n     if (arr[l] + arr[r] > arr[i]) { n       cout <<  YES n ; n       for (int i = 0; i < int(n); i++) cout << arr[i] <<    ; n       return 0; n     } n     swap(arr[i], arr[n - 1]); n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   if (n % 3 == 0 || n % 3 == 1) { n     cout << 1 <<     << 1 <<     << n - 2; n   } else { n     cout << 1 <<     << 2 <<     << n - 3; n   } n   return 0; n } n


#ifdef LOCAL n #include <prettyprint.hpp> n #endif n #include<bits/stdc++.h> n #define endl   n  n #define all(x) x.begin(), x.end() n #define rall(x) x.rbegin(), x.rend() n #define lg2(x) __lg(x) n #define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end()) n using namespace std; n  n typedef long long ll; n typedef double db; n typedef pair<int,int> ii; n #define x first n #define y second n  n mt19937 rand32(chrono::steady_clock::now().time_since_epoch().count()); n mt19937_64 rand64(chrono::steady_clock::now().time_since_epoch().count()); n  n const db eps = 1e-9; n const int maxn = (int)2e5 + 5; n const ll mod = (int)1e9 + 7; // 998244353 n  n int main() { n     ios_base::sync_with_stdio(0); cin.tie(0); n     cout.setf(ios::fixed); cout.precision(0); n  n     int n, m, p; n     cin >> n >> m >> p; n     vector<ll> a(n); n     for(auto &x:a){ n         x = 0; n         for(int i = 0; i < m; i++){ n             char j; cin >> j; n             if(j ==  1 ) n                 x |= (1ll<<i); n         } n     } n  n     ll ans = 0; n  n     for(int iter = 0; iter < 50; iter++){ n  n         ll s = a[rand32() % n]; n         vector<int> bits; n  n         for(int i = 0; i < m; i++) n             if(s & (1ll<<i)) n                 bits.push_back(i); n  n         int p = bits.size();     n         vector<int>cnt(1<<p, 0), cnt2(1<<p, 0); n  n         for(auto &x:a){ n             int y = 0; n             for(int i = 0; i < p; i++){ n                 if(x & (1ll<<bits[i])) n                     y |= (1<<i); n             } n             cnt[y]++; n         } n  n         for(int mask = (1<<p) - 1; mask >= 0; mask--){   n             for(int s = mask; s; s = (s - 1) & mask){ n                 cnt2[s] += cnt[mask]; n             } n         }  n  n         for(int mask = 0; mask < (1<<p); mask++){ n             if(2*cnt2[mask] >= n){ n                 if(__builtin_popcountll(ans) < __builtin_popcount(mask)){ n                     ans = 0; n                     for(int i = 0; i < p; i++) n                         if(mask & (1ll<<i)) n                             ans |= (1ll<<bits[i]); n                 } n             } n         } n     }    n  n  n     for(int i = 0; i < m; i++) n         cout << ((ans >> i) & 1); n      n     return 0; n }