#include <bits/stdc++.h>   struct Edge {     int u, v;     int w;   };   struct RowEdges {     int sum;     std::vector<Edge> vs;     RowEdges() : sum(0) {}     Edge &back() { return vs.back(); }     Edge &front() { return vs.front(); }     Edge &operator[](int i) { return vs[i]; }     int size() { return (int)vs.size(); }     void pop_back() { vs.pop_back(); }     void push_back(Edge ae) { vs.push_back(ae); }     bool operator<(RowEdges ot) const { return sum < ot.sum; }   };   struct Graph {     Graph(int n) : ves(n), vals(nullptr) {}     ~Graph() {       if (vals) {         delete[] vals;         vals = nullptr;       }     }     void addTwoEdge(int a, int b, int awei = 0, int bwei = 0) {       int minWei = std::min(awei, bwei);       addEdge(a, b, minWei);       addEdge(b, a, minWei);     }     void removeTwoEdge(int a, int b) {       removeEdge(a, b);       removeEdge(b, a);     }     void removeEdge(int a, int b) {       auto &tv = ves[a];       for (int i = 0; i < (int)tv.size(); i++) {         if (tv[i].v == b) {           tv.sum -= std::min(vals[a], vals[b]);           tv[i] = tv.back();           tv.pop_back();           break;         }       }     }     void addEdge(int a, int b, int addWeight = 0) {       ves[a].push_back(Edge());       auto &bn = ves[a].back();       bn.u = a, bn.v = b;       ves[a].sum += addWeight;     }     void sort(int off = 0) {       std::sort(ves.begin() + off, ves.end());       std::vector<int> oldToNewId(ves.size());       std::vector<int> newToOldId(ves.size());       for (size_t i = off; i < ves.size(); i++) {         if (ves[i].size()) {           oldToNewId[ves[i].front().u] = i;           newToOldId[i] = ves[i].front().u;         }       }       for (size_t i = off; i < ves.size(); i++) {         for (int j = 0; j < ves[i].size(); j++) {           ves[i][j].u = oldToNewId[ves[i][j].u];           ves[i][j].v = oldToNewId[ves[i][j].v];         }       }       int n = ves.size();       int *varr = new int[n];       memcpy(varr, vals, n * sizeof(int));       for (int i = off; i < n; i++) {         int j = newToOldId[i];         vals[i] = varr[j];       }       delete[] varr;     }     int getAns() {       int ans = 0;       for (size_t i = 1; i < ves.size(); i++) {         sort(i);         if (ves[i].size()) {           ans += ves[i].sum;           while (ves[i].size()) {             removeTwoEdge(i, ves[i].front().v);           }         }       }       return ans;     }     std::vector<RowEdges> ves;     int *vals;   };   int main() {     int n = 0, m = 0, n1 = 0;     scanf( %d %d , &n, &m);     n1 = n + 1;     int *v = new int[n1];     v[0] = 0;     for (int i = 1; i < n1; i++) {       scanf( %d , v + i);     }     Graph g(n1);     g.vals = v;     int a = 0, b = 0;     for (int i = 0; i < m; i++) {       scanf( %d %d , &a, &b);       g.addTwoEdge(a, b, v[a], v[b]);     }     printf( %d , g.getAns());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     vector<pair<long long, long long>> ar(4);     for (long long i = 1; i <= 3; i++) {       long long a, b;       cin >> a >> b;       ar[i] = {a, b};     }     sort(ar.begin() + 1, ar.end());     set<pair<long long, long long>> ans;     for (long long i = ar[1].first; i <= ar[2].first; i++) {       ans.insert({i, ar[1].second});     }     long long dy = 1;     if (ar[2].second < ar[1].second) dy = -1;     for (long long i = ar[1].second; i != ar[2].second; i += dy) {       ans.insert({ar[2].first, i});     }     ans.insert({ar[2].first, ar[2].second});     long long low = ar[1].second, high = ar[2].second;     long long temp = min(high, low);     high = max(high, low);     low = temp;     if (ar[3].second >= low && ar[3].second <= high) {       for (long long i = ar[3].first; i >= ar[2].first; i--) {         ans.insert({i, ar[3].second});       }     } else if (ar[3].second > high) {       for (long long i = ar[3].first; i >= ar[2].first; i--) {         ans.insert({i, ar[3].second});       }       for (long long i = ar[3].second; i > high; i--) {         ans.insert({ar[2].first, i});       }     } else {       for (long long i = ar[3].first; i >= ar[2].first; i--) {         ans.insert({i, ar[3].second});       }       for (long long i = ar[3].second; i < low; i++) {         ans.insert({ar[2].first, i});       }     }     cout << signed(ans.size()) <<   n ;     for (auto i : ans) cout << i.first <<     << i.second <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   long long a[2001], b[2001], n, m, x, sa[2001], sb[2001], ans;   long long max(long long a, long long b) { return a > b ? a : b; }   long long min(long long a, long long b) { return a < b ? a : b; }   int main() {     std::cin >> n >> m;     for (int i = 1; i <= n; i++)       std::cin >> a[i], a[i] += a[i - 1], sa[i] = 100000000000;     for (int i = 1; i <= m; i++)       std::cin >> b[i], b[i] += b[i - 1], sb[i] = 100000000000;     std::cin >> x;     for (int i = 1; i <= n; i++)       for (int j = i; j <= n; j++)         sa[j - i + 1] = min(sa[j - i + 1], a[j] - a[i - 1]);     for (int i = 1; i <= m; i++)       for (int j = i; j <= m; j++)         sb[j - i + 1] = min(sb[j - i + 1], b[j] - b[i - 1]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (sa[i] * sb[j] <= x) ans = max(ans, i * j);     std::cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   char mp[1000];   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     for (int i = 0; i < n; i++) {       mp[s[i] -  a ]++;       if (mp[s[i] -  a ] > k) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int nex = 2e5 + 5;   void solve() {     int a, b;     cin >> a >> b;     string s;     cin >> s;     int cnt = 0;     vector<int> v;     for (int i = 0; i < ((int)s.size()); i++) {       if (s[i] ==  X ) {         if (cnt) v.push_back(cnt);         cnt = 0;       } else {         cnt++;       }     }     if (cnt) v.push_back(cnt);     cnt = 0;     int val = 0;     for (int x : v) {       if (x < a && x >= b) {         cout <<  NO n ;         return;       }       if (x >= a && x < 2 * b)         cnt++;       else if (x >= 2 * b) {         if (val) {           cout <<  NO n ;           return;         }         val = x;       }     }     cnt = cnt;     for (int i = 0; i <= (val - a); i++) {       int x = i, y = val - a - i, lcnt = cnt;       if (x < 2 * b && y < 2 * b) {         if (x < a && x >= b) continue;         if (y < a && y >= b) continue;         if (y >= a) lcnt++;         if (x >= a) lcnt++;         if (lcnt % 2 == 0) {           cout <<  YES n ;           return;         }       }     }     if (val == 0) {       if (cnt % 2)         cout <<  YES n ;       else         cout <<  NO n ;       return;     }     cout <<  NO n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cout << fixed << setprecision(11);     clock_t beg = clock();     int t = 1, n;     cin >> t;     while (t--) {       solve();     }     clock_t end = clock();     fprintf(stderr,  %lf sec n , (double)(end - beg) / (CLOCKS_PER_SEC));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace Quick_Function {   template <typename Temp>   void Read(Temp &x) {     x = 0;     char ch = getchar();     bool op = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) op = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     if (op) x = -x;   }   template <typename T, typename... Args>   void Read(T &t, Args &...args) {     Read(t);     Read(args...);   }   template <typename Temp>   Temp Max(Temp x, Temp y) {     return x > y ? x : y;   }   template <typename Temp>   Temp Min(Temp x, Temp y) {     return x < y ? x : y;   }   template <typename Temp>   Temp Abs(Temp x) {     return x < 0 ? (-x) : x;   }   template <typename Temp>   void Swap(Temp &x, Temp &y) {     x ^= y ^= x ^= y;   }   }  // namespace Quick_Function   using namespace Quick_Function;   const int MAXN = 5e6 + 5;   bool vis[MAXN];   int n, ans;   struct Splay_Node {     int son[2], fa, val, tag;   };   struct Splay_Tree {     Splay_Node t[MAXN];     int root, tot, Top, stk[MAXN];     int Ident(int pos) { return t[t[pos].fa].son[1] == pos ? 1 : 0; }     void Connect(int pos, int fa, int flag) {       t[fa].son[flag] = pos, t[pos].fa = fa;     }     void Push_Down(int pos) {       if (!t[pos].tag) return;       if (t[pos].son[0]) {         t[t[pos].son[0]].val += t[pos].tag, t[t[pos].son[0]].tag += t[pos].tag;       }       if (t[pos].son[1]) {         t[t[pos].son[1]].val += t[pos].tag, t[t[pos].son[1]].tag += t[pos].tag;       }       t[pos].tag = 0;     }     int New(int val, int fa) {       t[++tot].fa = fa, t[tot].val = val;       return tot;     }     void Build() {       root = New(-0x3f3f3f3f, 0);       t[root].son[1] = New(0x3f3f3f3f, root);     }     void Rotate(int pos) {       int fa = t[pos].fa, grand = t[fa].fa;       int flag1 = Ident(pos), flag2 = Ident(fa);       Connect(pos, grand, flag2);       Connect(t[pos].son[flag1 ^ 1], fa, flag1);       Connect(fa, pos, flag1 ^ 1);     }     void Splay(int pos, int to) {       int tmp = pos;       Top = 0;       stk[++Top] = tmp;       while (tmp) stk[++Top] = tmp = t[tmp].fa;       while (Top) Push_Down(stk[Top--]);       for (int fa = t[pos].fa; t[pos].fa != to; Rotate(pos), fa = t[pos].fa)         if (t[fa].fa != to) Ident(pos) == Ident(fa) ? Rotate(fa) : Rotate(pos);       if (!to) root = pos;     }     void Insert(int &pos, int val, int fa) {       if (!pos) {         ++ans;         pos = New(val, fa);         Splay(pos, 0);         return;       }       Push_Down(pos);       if (val < t[pos].val)         Insert(t[pos].son[0], val, pos);       else         Insert(t[pos].son[1], val, pos);     }     void Erase(int pos) {       Splay(pos, 0);       vis[pos] = 1;       int l = t[pos].son[0], r = t[pos].son[1];       while (t[l].son[1]) l = t[l].son[1];       while (t[r].son[0]) r = t[r].son[0];       Splay(l, 0);       Splay(r, l);       t[r].son[0] = 0;       --ans;     }     int Get_Pre(int val) {       int pos, res, newroot;       pos = newroot = root;       while (pos) {         Push_Down(pos);         if (t[pos].val < val) {           res = pos;           pos = t[pos].son[1];         } else           pos = t[pos].son[0];       }       Splay(newroot, 0);       return res;     }     int Get_Nxt(int val) {       int pos, res, newroot;       pos = newroot = root;       while (pos) {         Push_Down(pos);         if (t[pos].val > val) {           res = pos;           pos = t[pos].son[0];         } else           pos = t[pos].son[1];       }       Splay(newroot, 0);       return res;     }     void Move(int l, int r) {       int u = Get_Nxt(l - 1), v = Get_Pre(r);       if (t[u].val > t[v].val) return;       if (u == v)         t[u].val++;       else if (t[u].val < t[v].val) {         Splay(u, 0);         Splay(v, u);         int rson = t[v].son[0];         ++t[u].val;         ++t[v].val, t[rson].val++;         if (rson) ++t[rson].tag;       }     }     void Body() {       int pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }       pos = rand() % tot + 1;       if (!vis[pos]) {         Splay(pos, 0);         return;       }     }   };   Splay_Tree tree;   int main() {     srand(time(0));     Read(n);     tree.Build();     tree.Insert(tree.root, 0, 0);     ans = 0;     for (int i = 1, l, r; i <= n; i++) {       Read(l, r);       int pos = tree.Get_Nxt(r - 1);       if (pos && pos != 1 && pos != 2) tree.Erase(pos);       tree.Move(l, r);       tree.Insert(tree.root, l, 0);       tree.Body();     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d, n;     int s = 0;     cin >> a >> b >> c >> d >> n;     for (int i = 1; i <= n; i++) {       if (i % a && i % b && i % c && i % d) {         s++;       }     }     cout << n - s;   }


#include <bits/stdc++.h>   using namespace std;   int gr[1005][1005], n, m, a[1005], x, y;   long long g[1005];   bool vis[1005];   int main() {     scanf( %d , &n), scanf( %d , &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     int mi = 100000100, l1 = 0;     int ans = 0;     for (int i = 0; i < m; i++) {       scanf( %d , &x), scanf( %d , &y);       ans += min(a[x], a[y]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct _ {     _() { ios_base::sync_with_stdio(0); }   } _;   template <class A, class B>   ostream &operator<<(ostream &o, pair<A, B> t) {     o <<  (  << t.first <<  ,   << t.second <<  ) ;     return o;   }   template <class T>   string tostring(T first, int len = 0, char c =  0 ) {     stringstream ss;     ss << first;     string r = ss.str();     if (r.length() < len) r = string(len - r.length(), c) + r;     return r;   }   template <class T>   void PV(T a, T b, int n = 0, int w = 0, string s =    ) {     int c = 0;     while (a != b) {       cout << tostring(*a++, w,    );       if (a != b && (n == 0 || ++c % n))         cout << s;       else         cout << endl;     }   }   template <class T>   inline bool chmin(T &a, T b) {     return a > b ? a = b, 1 : 0;   }   template <class T>   inline bool chmax(T &a, T b) {     return a < b ? a = b, 1 : 0;   }   const long long linf = 0x3f3f3f3f3f3f3f3fLL;   const int inf = 0x3f3f3f3f;   const int mod = int(1e9) + 7;   const int N = 111111;   vector<int> f(int n) {     vector<int> r;     if (n == 1) {       r.push_back(1);       return r;     }     if (n == 2) {       r.push_back(3);       r.push_back(4);       return r;     }     for (int i = 0; i < n - 1; i++) r.push_back(2);     r.push_back(n - 2);     return r;   }   int main() {     int n, m;     cin >> n >> m;     vector<int> A = f(n);     vector<int> B = f(m);     for (int i : A) {       for (int j : B) cout << i * j <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,inline,unroll-loops,fast-math )   #pragma GCC target( popcnt,abm,mmx,tune=native )   using namespace std;   long long n, i, j;   int main() {     [=]() {       ios_base ::sync_with_stdio(false);       cin.tie(nullptr);     }();     cin >> n;     for (; i < n; i++) {       for (j ^= j; j < n; j++) cout <<  WB [(i + j) % 2];       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int cnt[2];   int main() {     int n, k, m;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , &m);       cnt[m % 2]++;     }     int r = n - k;     if (r == 0) {       if (cnt[1] % 2 == 1)         puts( Stannis );       else         puts( Daenerys );       return 0;     }     int sec = r / 2;     int fst = r - sec;     if (fst >= cnt[0] && k % 2 == 1) {       puts( Stannis );       return 0;     }     if (sec >= cnt[1]) {       puts( Daenerys );       return 0;     }     if (sec >= cnt[0] && k % 2 == 0) {       puts( Daenerys );       return 0;     }     if (r % 2 == 0) {       puts( Daenerys );       return 0;     }     puts( Stannis );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 317;   int t[100002], a[100002];   int l[100002], r[100002];   long long sum[2][100002];   long long ans[100002];   struct node {     int b, r, i;   };   bool cmp(node a, node b) {     if (a.b != b.b) return a.b < b.b;     return a.r < b.r;   }   int n, k, q;   long long res = 0;   node block[100002];   int val[3][100002];   int dp[300006];   void add1(int i) {     res += dp[val[1][i]];     dp[val[0][i]]++;   }   void del1(int i) {     dp[val[0][i]]--;     res -= dp[val[1][i]];   }   void add2(int i) {     res += dp[val[2][i]];     dp[val[0][i]]++;   }   void del2(int i) {     dp[val[0][i]]--;     res -= dp[val[2][i]];   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &t[i]), t[i]--;     vector<long long> v;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       sum[0][i] = sum[0][i - 1];       sum[1][i] = sum[1][i - 1];       sum[t[i]][i] += a[i];       v.emplace_back(sum[0][i] - sum[1][i]);       v.emplace_back(sum[0][i] - sum[1][i] - k);       v.emplace_back(sum[0][i] - sum[1][i] + k);     }     v.emplace_back(0);     v.emplace_back(-k);     v.emplace_back(+k);     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     for (int i = 0; i <= n; i++) {       val[0][i] =           lower_bound(v.begin(), v.end(), sum[0][i] - sum[1][i]) - v.begin() + 1;       val[1][i] = lower_bound(v.begin(), v.end(), sum[0][i] - sum[1][i] - k) -                   v.begin() + 1;       val[2][i] = lower_bound(v.begin(), v.end(), sum[0][i] - sum[1][i] + k) -                   v.begin() + 1;     }     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       scanf( %d%d , &l[i], &r[i]);       l[i]--;       block[i] = {l[i] / sz, r[i], i};     }     sort(block + 1, block + 1 + q, cmp);     int curl = 0, curr = 0;     dp[val[0][0]]++;     for (int i = 1; i <= q; i++) {       int idx = block[i].i;       int ll = l[idx], rr = r[idx];       while (curl < ll) del2(curl++);       while (curl > ll) add2(--curl);       while (curr < rr) add1(++curr);       while (curr > rr) del1(curr--);       ans[idx] = res;     }     for (int i = 1; i <= q; i++) printf( %lld n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int DAYS = 7;   int val[DAYS];   void solve() {     int k;     cin >> k;     int tot = 0;     for (int i = 0; i < DAYS; ++i) {       cin >> val[i];       tot += val[i];     }     int res = 1 << 30;     for (int start = 0; start < DAYS; ++start) {       if (val[start]) {         int tmp = k;         int passed = 0;         for (int j = start; j < DAYS && tmp > 0; ++j) {           passed++;           tmp -= val[j];         }         if (tmp > tot) {           tmp -= tot;           int cycles = tmp / tot;           passed += cycles * DAYS;           tmp %= tot;           tmp += tot;         }         int j = 0;         while (tmp > 0) {           tmp -= val[j];           passed++;           j = (j + 1) % DAYS;         }         res = min(res, passed);       }     }     cout << res << endl;   }   int main() {     ios::sync_with_stdio(false);     int T;     cin >> T;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 300000000 + 10;   bitset<MAXN> check;   void Get_prime(int n) {     check.set();     check[0] = check[1] = 0;     for (int i = 2; i <= sqrt(n + 0.5); ++i) {       if (check[i])         for (int j = 2; i * j <= n; ++j) check[i * j] = 0;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int l, r;     cin >> l >> r;     Get_prime(r);     int cnt = 0, i;     if (l <= 2 && 2 <= r) ++cnt;     if (l % 4 <= 1)       i = l / 4 * 4 + 1;     else       i = (l / 4 + 1) * 4 + 1;     for (i; i <= r; i += 4)       if (check[i]) ++cnt;     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 100010;   long long n, S[maxn], top, ans[maxn], cnt;   int main() {     cin >> n;     for (long long i = 1; i * i <= n; i++)       if (n % i == 0) {         S[++top] = i;         if (i * i != n) S[++top] = n / i;       }     for (long long i = 1; i <= top; i++) {       long long temp = (n - 1) / S[i];       temp++;       long long temp2 = (n - 1) / S[i];       temp2 = temp2 * S[i] + 2;       temp = temp * temp2;       ans[++cnt] = temp / 2;     }     sort(ans + 1, ans + 1 + cnt);     cnt = unique(ans + 1, ans + 1 + cnt) - ans - 1;     for (int i = 1; i <= cnt; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, c, res;   void mo() {     freopen( 1091A.inp ,  r , stdin);     freopen( 1091A.out ,  w , stdout);   }   void nhap() { cin >> a >> b >> c; }   void xuli() {     if (c - 1 <= b && c - 2 <= a) {       res = max(res, c + c - 1 + c - 2);     }     if (b + 1 <= c && b - 1 <= a) {       res = max(res, b + b + 1 + b - 1);     }     if (a + 1 <= b && a + 2 <= c) res = max(res, a + 1 + a + a + 2);     cout << res;   }   int main() {     nhap();     xuli();   }


#include <bits/stdc++.h>   using namespace std;      const int INF = 0x3f3f3f3f;      int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<long long> t(n), x(n);       for (int i = 0; i < n; i++) {         cin >> t[i] >> x[i];       }       long long start = 0, end = 0, from = 0, to = 0, ans = 0;       for (int i = 0; i < n; i++) {         if (end <= t[i]) {           start = t[i];           end = t[i] + llabs(to - x[i]);           from = to;           to = x[i];         }         if (min(from, to) <= x[i] && max(from, to) >= x[i]) {           long long arrival = start + abs(from - x[i]);           if (t[i] <= arrival && (i == n - 1 ? true : (arrival <= t[i + 1]))) {             ans++;           }         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> Max[3005][3005];   struct ans {     int a, b, c;     int i, j, k;     ans(int _a, int _b, int _c, int _i, int _j, int _k) {       a = _a;       b = _b;       c = _c;       i = _i;       j = _j;       k = _k;     }   };   bool operator<(const ans &a, const ans &b) {     if (a.a != b.a)       return a.a < b.a;     else if (a.b != b.b)       return a.b < b.b;     else       return a.c < b.c;   }   int main() {     int n;     scanf( %d , &n);     pair<int, int> a[3005];     memset(Max, -1, sizeof(Max));     for (int i = 0; i < n; i++) {       int first;       scanf( %d , &first);       a[i] = make_pair((first), (i));     }     sort(a, a + n);     reverse(a, a + n);     a[n] = make_pair((0), (n));     for (int i = 0; i < n; i++) {       for (int j = i + 1; j <= n; j++) {         Max[i][j - 1] =             max(Max[i][j - 2], make_pair((a[j - 1].first - a[j].first), (j)));       }     }     ans aa = ans{-1, -1, -1, 0, 0, 0};     for (int i = 1; i <= n; i++) {       for (int j = (i + 1) / 2; j <= i * 2 && i + j <= n; j++) {         int l = max((i + 1) / 2, (j + 1) / 2), r = min(i * 2, j * 2);         l += i + j;         r += i + j;         l--;         r--;         if (l >= n) continue;         if (r >= n) r = n - 1;         aa = max(aa, ans(a[i - 1].first - a[i].first,                          a[i + j - 1].first - a[i + j].first, Max[l][r].first, i,                          i + j, Max[l][r].second));       }     }     int ans[3005];     memset(ans, -1, sizeof(ans));     for (int i = 0; i < aa.i; i++) ans[a[i].second] = 1;     for (int i = aa.i; i < aa.j; i++) ans[a[i].second] = 2;     for (int i = aa.j; i < aa.k; i++) ans[a[i].second] = 3;     for (int i = 0; i < n; i++) printf( %d  , ans[i]);     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   long long read() {     long long x = 0;     int zf = 1;     char ch =    ;     while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar();     if (ch ==  - ) zf = -1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return x * zf;   }   long long val[1000005];   long long f[1000005];   int n;   long long cnt[9];   int main() {     long long w = read(), tmp = w, sum = 0;     for (int i = 1; i <= 8; ++i) cnt[i] = read(), sum += cnt[i] * i;     if (sum < w) {       printf( %lld , sum);       return 0;     }     if (w > 900) {       long long w2 = w - 900, use;       for (int i = 8; i >= 1 && w2; --i) {         if (cnt[i] * i <= w2)           w2 -= cnt[i] * i;         else {           use = (w2 / i) + 1;           cnt[i] -= use;           w2 -= use * i;           break;         }       }       w = 900 + w2;     }     for (int i = 1; i <= 8; ++i) {       long long j = 1;       while (cnt[i] >= j) {         cnt[i] -= j;         val[++n] = i * j;         j <<= 1;       }       if (cnt[i]) val[++n] = cnt[i] * i;     }     for (int i = 1; i <= n; ++i)       for (int j = w; j >= val[i]; --j) f[j] = max(f[j - val[i]] + val[i], f[j]);     printf( %lld , tmp - (w - f[w]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     if (n % 2 == 0)       cout <<  4   << n - 4;     else       cout <<  9   << n - 9;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     int k1, k2, i;   } node[105];   bool cmp1(const Node& a, const Node& b) { return a.k1 < b.k1; }   bool cmp2(const Node& a, const Node& b) { return a.i < b.i; }   int main() {     int n, w;     scanf( %d%d , &n, &w);     for (int i = 0; i < n; i++) {       scanf( %d , &node[i].k1);       node[i].i = i;     }     sort(node, node + n, cmp1);     for (int i = 0; i < n; i++) {       int f = node[i].k1 / 2;       if (node[i].k1 & 1) f++;       node[i].k2 = f;       w -= f;     }     if (w < 0) {       puts( -1 );       return 0;     }     for (int i = n - 1; i >= 0 && w; i--) {       if (w >= node[i].k1 - node[i].k2) {         w -= node[i].k1 - node[i].k2;         node[i].k2 = node[i].k1;       } else {         node[i].k2 += w;         w = 0;       }     }     sort(node, node + n, cmp2);     for (int i = 0; i < n; i++) {       if (i)         printf(  %d , node[i].k2);       else         printf( %d , node[i].k2);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int y, l, r, x, rem;     cin >> l >> r;     for (x = l; x <= r; x++) {       y = x;       set<int> s;       while (y != 0) {         rem = y % 10;         if (s.find(rem) == s.end())           s.insert(rem);         else           break;         y /= 10;       }       if (y == 0) break;     }     if (y == 0)       cout << x;     else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops )   using namespace std;   int a[100005];   int n;   int solve() {     sort(a, a + n);     int i = n - 1;     int sum = 0;     while (i >= 0 and a[i] >= 0) sum += a[i--];     if (sum % 2) return sum;     int j = i;     while (j >= 0 and abs(a[j]) % 2 == 0) j--;     i++;     while (i < n and a[i] % 2 == 0) i++;     if (j < 0) return sum - a[i];     if (i == n) return sum + a[j];     return sum - min(a[i], abs(a[j]));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t) {       cin >> n;       for (int i = 0; i < n; i++) cin >> a[i];       cout << solve();       t--;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     long long TC = 1;     cin >> TC;     while (TC--) {       int n, f = 1, c = 0;       cin >> n;       int a[n], feq[110] = {0};       for (int i = 0; i < n; i++) {         cin >> a[i];         feq[a[i]]++;       }       sort(a, a + n);       for (int i = 0; i < n - 1; i++) {         if (a[i + 1] - a[i] > 1) f = 0;       }       if (f)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long i, j, k, n, m, ans = 0;     string a, b;     cin >> n >> a >> b;     for (i = 0; i < n / 2; i++) {       int j = n - i - 1;       if (a[i] == b[i] && a[j] == b[j])         continue;       else if (a[i] == b[i] && a[j] != b[j])         ans++;       else if (a[i] != b[i] && a[j] == b[j])         ans++;       else {         set<char> se({a[i], b[i], a[j], b[j]});         if (se.size() == 4)           ans += 2;         else if (se.size() == 3) {           ans++;           if (a[i] == a[j]) ans++;         }       }     }     if (n % 2 == 1) ans += (a[n / 2] != b[n / 2]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, z, t1, t2, t3, ts, te;     cin >> x >> y >> z >> t1 >> t2 >> t3;     ts = abs(x - y) * t1;     te = t3 * 2 + abs(x - z) * t2 + t3 + abs(x - y) * t2;     if (ts >= te)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   typedef struct Segment {     int start;     int last;     int Xor;   } segment;   using namespace std;   vector<segment> V;   int score(segment s) {     if (s.last == s.start)       return s.Xor;     else {       int value = 0;       for (int i = s.start + 1; i < s.last;) {         if (V[i].start == -1)           i++;         else {           value += score(V[i]);           i = V[i].last + 1;         }       }       return max(value, s.Xor);     }   }   int cities_final[5010];   int cities_first[5010];   int cities[5010];   int main() {     int n;     scanf( %d , &n);     int people[n];     int cumulativeXOR[n + 1];     cumulativeXOR[0] = 0;     for (int i = 0; i < n; i++) {       scanf( %d , &people[i]);       if (!cities[people[i]])         cumulativeXOR[i + 1] = cumulativeXOR[i] ^ people[i];       else         cumulativeXOR[i + 1] = cumulativeXOR[i];       if (!cities[people[i]]) cities_first[people[i]] = i;       cities[people[i]] = 1;       cities_final[people[i]] = i;     }     int start;     int last;     int Xor;     for (int i = 0; i < n; i++) {       start = i;       last = i;       int flag = 0;       if (cities[people[i]]) {         for (int j = i; j <= last; j++) {           if (cities_first[people[j]] < start) {             flag = 1;           }           if (cities_final[people[j]] > last) last = cities_final[people[j]];         }         Xor = cumulativeXOR[last + 1] ^ cumulativeXOR[start];         segment s;         s.start = start;         s.last = last;         s.Xor = Xor;         if (flag) s.start = -1;         cities[people[i]] = 0;         V.push_back(s);       } else {         segment s;         s.start = -1;         V.push_back(s);       }     }     int ans = 0;     for (int i = 0; i < n;) {       ans += score(V[i]);       i = V[i].last + 1;     }     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   bool ch1(int t, int da, int db, int a, int b, int x) {     for (int i = 0; i <= t - 1; i++) {       for (int j = 0; j <= t - 1; j++) {         if (((a - i * da + b - j * db == x) &&              (a - i * da >= 0 && b - j * db >= 0)) ||             a - i * da == x || b - j * db == x)           return true;       }     }     return false;   }   int main() {     int x, a, b, t, da, db, total;     cin >> x >> t >> a >> b >> da >> db;     total = a + b;     if (x > total)       cout <<  NO n ;     else if (x == 0)       cout <<  YES n ;     else {       if (ch1(t, da, db, a, b, x)) {         cout <<  YES n ;       } else {         cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, f[2001] = {0};   bool p, a[2001][2001];   string st;   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> st;       for (int j = 0; j < m; j++)         if (st[j] ==  1 ) {           a[i][j + 1] = true;           f[j + 1]++;         } else           a[i][j + 1] = false;     }     for (int i = 1; i <= n; i++) {       p = true;       for (int j = 1; j <= m; j++)         if (f[j] == 1 && a[i][j] == 1) p = false;       if (p) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     fast();     long long n;     cin >> n;     char a[n + 10];     for (int i = 1; i <= n; ++i) cin >> a[i];     long long func[15];     for (int i = 1; i <= 9; ++i) {       cin >> func[i];     }     for (int i = 1; i <= n; ++i) {       if (func[a[i] -  0 ] > a[i] -  0 ) {         for (int j = i; j <= n; ++j) {           if (a[j] -  0  <= func[a[j] -  0 ])             a[j] = func[a[j] -  0 ] +  0 ;           else             break;         }         for (int j = 1; j <= n; ++j) cout << a[j];         return 0;       }     }     for (int i = 1; i <= n; ++i) cout << a[i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400005;   int n, m, k, s, t, a[N], b[N];   int ans;   long long p;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int j = 0; j < 25; ++j) {       for (int i = 1; i <= n; ++i) p += ((a[i] >> j) & 1) * (n - 1);       if (p & 1) ans += 1 << j;       p = 0;       for (int i = 1; i <= n; ++i) b[i] = a[i] % (1 << (j + 1));       sort(b + 1, b + 1 + n);       int r = n;       for (int l = 1; l < n; ++l) {         while (r > l && b[l] + b[r] >= (1 << (j + 1))) --r;         if (r <= l) r = l;         p += n - r;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int t;   long long l[200002], c[200002];   long long minx, maxx, miny, maxy;   string s;   bool check(long long l[], int n) {     int Min = 0, Max = 0;     int last = 0, first = 0;     for (int i = 1; i <= n; i++) {       if (l[i] < Min) {         first = i;         Min = l[i];       }       if (l[i] >= Max) {         last = i;         Max = l[i];       }     }     if (last < first) return 1;     Min = 0, Max = 0;     first = last = 0;     for (int i = 1; i <= n; i++) {       if (l[i] > Max) {         first = i;         Max = l[i];       }       if (l[i] <= Min) {         Min = l[i];         last = i;       }     }     if (last < first) return 1;     return 0;   }   int main() {     cin >> t;     while (t--) {       cin >> s;       minx = maxx = miny = maxy = 0;       long long x = 0, y = 0;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  D ) c[i + 1] = c[i] + 1;         if (s[i] ==  A ) c[i + 1] = c[i] - 1;         if (s[i] ==  W ) l[i + 1] = l[i] + 1;         if (s[i] ==  S ) l[i + 1] = l[i] - 1;         if (s[i] ==  W  || s[i] ==  S ) c[i + 1] = c[i];         if (s[i] ==  D  || s[i] ==  A ) l[i + 1] = l[i];         minx = min(minx, l[i + 1]);         miny = min(miny, c[i + 1]);         maxx = max(maxx, l[i + 1]);         maxy = max(maxy, c[i + 1]);       }       long long ans = (maxx - minx + 1) * (maxy - miny + 1);       if (check(l, s.size()) && maxx - minx + 1 > 2)         ans = min(ans, (maxx - minx) * (maxy - miny + 1));       if (check(c, s.size()) && maxy - miny + 1 > 2)         ans = min(ans, (maxx - minx + 1) * (maxy - miny));       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1);   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   int n, a, b, c;   string s;   map<pair<int, pair<int, int> >, int> dic;   bool bo1 = false, bo2 = false, bo3 = false;   void dfs(int a, int b, int c) {     pair<int, pair<int, int> > temp = make_pair(a, make_pair(b, c));     if (dic[temp]) return;     dic[temp] = 1;     if (a + b + c == 1) {       if (a == 1) bo1 = true;       if (b == 1) bo2 = true;       if (c == 1) bo3 = true;       return;     }     if (a > 0 && b > 0) dfs(a - 1, b - 1, c + 1);     if (a > 0 && c > 0) dfs(a - 1, b + 1, c - 1);     if (b > 0 && c > 0) dfs(a + 1, b - 1, c - 1);     if (a > 1) dfs(a - 1, b, c);     if (b > 1) dfs(a, b - 1, c);     if (c > 1) dfs(a, b, c - 1);   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     cin >> n;     cin >> s;     for (int i = 0; i < (int)s.size(); i++) {       if (s[i] ==  B ) a++;       if (s[i] ==  G ) b++;       if (s[i] ==  R ) c++;     }     dfs(a, b, c);     if (bo1) cout <<  B ;     if (bo2) cout <<  G ;     if (bo3) cout <<  R ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   using namespace std;   const int MX = 3003;   const int M = 998244353;   char s[MX], t[MX];   int n, m, ans, dp[MX][MX];   int f(int i, int j) {     if (i < 1 && j > n) return 1;     if (dp[i][j] == -1) {       int p = 0, q = 0, r = (i == j);       int k = j - i + r, x = (i < 1 && j > m);       if (i) {         if (i <= m)           p = (s[k] == t[i]) ? f(i - 1, j + r) : 0;         else           p = f(i - 1, j + r);       }       if (j <= n) {         if (j <= m)           q = (s[k] == t[j]) ? f(i - r, j + 1) : 0;         else           q = f(i - r, j + 1);       }       int y = (p + q < M) ? (p + q) : (p + q - M);       dp[i][j] = (x + y < M) ? (x + y) : (x + y - M);     }     return dp[i][j];   }   int main() {     scanf( %s %s , s + 1, t + 1);     n = strlen(s + 1), m = strlen(t + 1);     memset(dp, -1, sizeof(dp));     for (int i = 1; i <= n; i++) ans = (ans + f(i, i)) % M;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int day[13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};   int sec, n, k, dd, mm, h, s, m, now;   string str;   queue<int> q;   int main() {     cin >> sec >> k;     getline(cin, str);     while (scanf( 2012-%d-%d %d:%d:%d: , &mm, &dd, &h, &m, &s) != EOF) {       getline(cin, str);       now = (day[mm - 1] + dd - 1) * 24 * 60 * 60 + h * 60 * 60 + m * 60 + s;       q.push(now);       while (q.front() + sec <= now) q.pop();       if (q.size() >= k) {         printf( 2012-%02d-%02d %02d:%02d:%02d , mm, dd, h, m, s);         return 0;       }     }     cout << -1 << endl;     fclose(stdout);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:128000000 )   using namespace std;   map<string, int> a;   map<string, int>::iterator ii, jj;   string s;   int main() {     int k, i, v, u, j, n, m, d, d2, y, r, p, t1, t2, x, l, t, z, fl, to;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> s >> p;       if (a[s] <= p) a[s] = p;     }     k = 0;     for (ii = a.begin(); ii != a.end(); ii++) k++;     cout << k << endl;     for (ii = a.begin(); ii != a.end(); ii++) {       t = 0;       p = 0;       for (jj = a.begin(); jj != a.end(); jj++) {         if ((*ii).second >= (*jj).second) t++;         if ((*ii).second < (*jj).second) p++;       }       if ((double)p / (double)k > 0.5) {         cout << (*ii).first <<                  <<  noob  << endl;         continue;       }       if ((double)t / (double)k >= 0.5 && (double)p / (double)k > 0.2) {         cout << (*ii).first <<                  <<  random  << endl;         continue;       }       if ((double)t / (double)k >= 0.8 && (double)p / (double)k > 0.1) {         cout << (*ii).first <<                  <<  average  << endl;         continue;       }       if ((double)t / (double)k >= 0.9 && (double)p / (double)k > 0.01) {         cout << (*ii).first <<                  <<  hardcore  << endl;         continue;       }       if ((double)t / (double)k >= 0.99) {         cout << (*ii).first <<                  <<  pro  << endl;         continue;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     priority_queue<int> pq{};     for (int i{1}; i <= n; ++i) pq.push(i);     vector<pair<int, int>> ans{};     for (int i{0}; i < n - 1; ++i) {       int mx1{pq.top()};       pq.pop();       int mx2{pq.top()};       pq.pop();       ans.push_back({mx1, mx2});       pq.push((mx1 + mx2 + 1) / 2);     }     cout << pq.top() <<   n ;     for (auto& x : ans) cout << x.first <<     << x.second <<   n ;   }   void solve2() {     int n;     cin >> n;     cout <<  2 n ;     cout << n <<     << n - 1 <<   n ;     for (int i{n}; i >= 3; --i) cout << i <<     << i - 2 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) solve2();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p[200] = {0};   int main() {     string a;     int m;     cin >> m;     cin >> a;     int n = a.length();     for (int i = 0; i < n; i++) {       if (a[i] !=  ? ) p[a[i] -  a ] = 1;       if (a[i] !=  ?  && a[i] -  a  >= m) {         cout <<  IMPOSSIBLE  << endl;         return 0;       }     }     for (int i = 0; i < n; i++) {       if (a[i] ==  ?  && a[n - i - 1] !=  ? ) a[i] = a[n - i - 1];     }     for (int i = n / 2; i >= 0; i--) {       if (a[i] ==  ?  && a[n - i - 1] ==  ? ) {         for (int j = m - 1; j >= 0; j--) {           if (!p[j]) {             a[i] = j +  a ;             a[n - i - 1] = j +  a ;             p[j] = 1;             break;           }         }         if (a[i] ==  ? ) a[i] =  a , a[n - i - 1] =  a ;       }     }     for (int i = 0; i < n; i++) {       if (a[i] != a[n - i - 1]) {         cout <<  IMPOSSIBLE  << endl;         return 0;       }     }     for (int i = 0; i < m; i++)       if (!p[i]) {         cout <<  IMPOSSIBLE  << endl;         return 0;       }     cout << a << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200005;   int v[MAXN], l[MAXN], r[MAXN];   int ST[4 * MAXN];   int L[4 * MAXN];   int n;   void _update(int node, int lo, int hi, int i, int j, int val) {     if (L[node]) {       ST[node] = max(ST[node], L[node]);       if (hi != lo) {         L[2 * node] = max(L[node], L[2 * node]);         L[2 * node + 1] = max(L[node], L[2 * node + 1]);       }       L[node] = 0;     }     if (lo > j || hi < i) return;     if (lo >= i && hi <= j) {       ST[node] = max(ST[node], val);       if (hi != lo) {         L[2 * node] = max(val, L[2 * node]);         L[2 * node + 1] = max(val, L[2 * node + 1]);       }       return;     }     int mid = (lo + hi) / 2;     _update(2 * node, lo, mid, i, j, val);     _update(2 * node + 1, mid + 1, hi, i, j, val);     ST[node] = max(ST[2 * node], ST[2 * node + 1]);   }   int _query(int node, int l, int r, int i, int j) {     if (L[node]) {       ST[node] = max(ST[node], L[node]);       L[2 * node] = max(L[2 * node], L[node]);       L[2 * node + 1] = max(L[2 * node + 1], L[node]);       L[node] = 0;     }     if (r < i || l > j) return -1;     if (l >= i && r <= j) return ST[node];     int mid = (l + r) / 2;     int p1 = _query(2 * node, l, mid, i, j);     int p2 = _query(2 * node + 1, mid + 1, r, i, j);     return max(p1, p2);   }   int query(int i, int j) { return _query(1, 0, n, i, j); }   void update(int i, int j, int val) { _update(1, 0, n, i, j, val); }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &v[i]);     l[0] = -1;     for (int i = 1; i < n; i++) {       l[i] = i - 1;       while (v[l[i]] >= v[i] && l[i] != -1) l[i] = l[l[i]];     }     r[n - 1] = -1;     for (int i = n - 2; i >= 0; i--) {       r[i] = i + 1;       while (v[r[i]] >= v[i] && r[i] != -1) {         r[i] = r[r[i]];       }     }     for (int i = 0; i < n; i++) {       int ri = r[i], li = l[i];       if (ri == -1) ri = n;       if (li == -1) li = -1;       int segment_length = (ri - 1) - (li + 1) + 1;       update(1, segment_length, v[i]);     }     bool first = true;     for (int i = 1; i <= n; i++) {       if (first)         first = false;       else         printf(   );       printf( %d , query(i, i));     }     printf(  n );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   bool isin(       const pair<long long, tuple<long long, long long, long long> >& elem) {     return get<1>(elem.second) == 0;   }   bool isout(       const pair<long long, tuple<long long, long long, long long> >& elem) {     return get<0>(elem.second) == 0;   }   long long INF = 1e18;   int main() {     long long n, m, k;     cin >> n >> m >> k;     if (m == 0) {       cout << -1;       return 0;     }     vector<pair<long long, tuple<long long, long long, long long> > > flights;     long long d, f, t, c;     for (long long i = 0; i < m; i++) {       cin >> d >> f >> t >> c;       flights.push_back(           make_pair(d, tuple<long long, long long, long long>(f, t, c)));     }     sort(flights.begin(), flights.end());     long long days_cnt = flights.back().first + 2;     vector<bool> in(n + 1, false);     vector<bool> out(n + 1, false);     vector<long long> totalin(days_cnt, 0);     vector<long long> totalout(days_cnt, 0);     vector<long long> pre_min(days_cnt, INF);     vector<long long> suf_min(days_cnt, INF);     vector<long long> p_min(n + 1, INF);     vector<long long> p_min_rev(n + 1, INF);     long long cur = 0;     for (long long i = 0; i < flights.size(); i++) {       auto elem = flights[i];       long long d = elem.first;       long long f = get<0>(elem.second);       long long t = get<1>(elem.second);       long long c = get<2>(elem.second);       long long p = f;       if (p == 0) {         continue;       }       while (cur < d) {         cur++;         totalin[cur] = totalin[cur - 1];         pre_min[cur] = pre_min[cur - 1];       }       if (!in[p]) {         totalin[d]++;         in[p] = true;         p_min[p] = c;         pre_min[d] = (pre_min[d] == INF ? 0 : pre_min[d]) + p_min[p];       } else {         long long old_min = p_min[p];         p_min[p] = min(p_min[p], c);         if (old_min > p_min[p]) {           pre_min[d] -= old_min;           pre_min[d] += p_min[p];         }       }     }     while (cur < days_cnt) {       cur++;       if (cur == days_cnt) continue;       pre_min[cur] = pre_min[cur - 1];       totalin[cur] = totalin[cur - 1];     }     cur = days_cnt - 1;     for (long long i = flights.size() - 1; i >= 0; i--) {       auto elem = flights[i];       long long d = elem.first;       long long f = get<0>(elem.second);       long long t = get<1>(elem.second);       long long c = get<2>(elem.second);       long long p = t;       if (p == 0) {         continue;       }       while (cur > d) {         cur--;         totalout[cur] = totalout[cur + 1];         suf_min[cur] = suf_min[cur + 1];       }       if (!out[p]) {         totalout[d]++;         out[p] = true;         p_min_rev[p] = c;         suf_min[d] = (suf_min[d] == INF ? 0 : suf_min[d]) + p_min_rev[p];       } else {         long long old_min = p_min_rev[p];         p_min_rev[p] = min(p_min_rev[p], c);         if (old_min > p_min_rev[p]) {           suf_min[d] -= old_min;           suf_min[d] += p_min_rev[p];         }       }     }     while (cur >= 0) {       cur--;       if (cur < 0) continue;       suf_min[cur] = suf_min[cur + 1];       totalout[cur] = totalout[cur + 1];     }     long long ans = INF;     for (long long i = 0; i < suf_min.size(); i++) {       if (i + k + 1 >= totalout.size()) continue;       if (totalin[i] >= n && totalout[i + k + 1] >= n) {         ans = min(ans, pre_min[i] + suf_min[i + k + 1]);       }     }     cout << (ans >= INF ? -1 : ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 400010;   inline int max(int a, int b) { return a > b ? a : b; }   int head[maxn], cnt, sz[maxn], n, mx_sz, top[maxn], ans[maxn];   vector<int> vt;   struct ed {     int to, next;   } e[maxn * 2];   void ad(int x, int y) {     e[cnt] = (ed){y, head[x]};     head[x] = cnt++;     e[cnt] = (ed){x, head[y]};     head[y] = cnt++;   }   void get_vt(int u, int last) {     sz[u] = 1;     int mx = 0;     for (int k = head[u]; ~k; k = e[k].next) {       int v = e[k].to;       if (v != last) {         get_vt(v, u);         sz[u] += sz[v];         mx = max(mx, sz[v]);       }     }     mx = max(mx, n - sz[u]);     if (mx <= mx_sz) vt.push_back(u);   }   void dfs(int u, int last, int tp) {     sz[u] = 1, top[u] = tp;     for (int k = head[u]; ~k; k = e[k].next) {       int v = e[k].to;       if (v != last) {         dfs(v, u, tp);         sz[u] += sz[v];       }     }   }   void work(int u) {     ans[u] = 1;     memset(sz, 0, sizeof(sz));     int mx = 0, _mx = 0;     for (int k = head[u]; ~k; k = e[k].next) {       int v = e[k].to;       dfs(v, u, v);       if (sz[v] >= sz[mx])         _mx = mx, mx = v;       else if (sz[v] > sz[_mx])         _mx = v;     }     for (int i = 1; i <= n; i++) {       if (top[i] == mx)         ans[i] |= (n - sz[i] - sz[_mx] <= mx_sz);       else         ans[i] |= (n - sz[i] - sz[mx] <= mx_sz);     }   }   int main() {     while (~scanf( %d , &n)) {       cnt = 0;       mx_sz = n / 2;       vt.clear();       memset(sz, 0, sizeof(sz));       memset(ans, 0, sizeof(ans));       memset(head, -1, sizeof(head));       int u, v;       for (int i = 1; i < n; i++) {         scanf( %d%d , &u, &v);         ad(u, v);       }       get_vt(1, 0);       for (int i = 0; i < vt.size(); i++) work(vt[i]);       for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int x[3010], y[3010];   vector<int> py[3010];   int L[3010], R[3010], q, k;   void nxt(int &u, int k = 1) {     while (k-- > 0) u = R[u];   }   void pre(int &u, int k = 1) {     while (k-- > 0) u = L[u];   }   long long res = 0;   struct node {     int x, y;     bool operator<(const node a) const { return x < a.x; }   } p[3010];   int n, m;   void solve() {     res = 0;     int l = 0, r = 0;     nxt(r, k);     if (r == q + 1) return;     for (; r != q + 1; nxt(r)) {       nxt(l);       res += 1ll * p[l].x * (p[R[r]].x - p[r].x);     }   }   void del(int u) { R[L[u]] = R[u], L[R[u]] = L[u]; }   void erase(int u) {     int l = u;     pre(l, k + 1);     int l0 = l;     int r = l;     nxt(r, k);     for (; l < u && r < q + 1; nxt(r)) {       nxt(l);       res -= 1ll * p[l].x * (p[R[r]].x - p[r].x);     }     del(u);     l = r = l0;     nxt(r, k);     for (; R[l] < u && r < q + 1; nxt(r)) {       nxt(l);       res += 1ll * p[l].x * (p[R[r]].x - p[r].x);     }   }   int main() {     scanf( %d%d%d%d , &n, &m, &q, &k);     for (int i = 1; i <= q; i++) scanf( %d%d , &p[i].x, &p[i].y);     sort(p + 1, p + q + 1);     p[q + 1] = (node){n + 1, m + 1};     for (int i = 1; i <= q; i++) py[p[i].y].push_back(i);     long long ans = 0;     R[q + 1] = q + 1;     for (int y0 = 1; y0 <= m; y0++) {       int pr = 0;       for (int i = 1; i <= q; i++)         if (p[i].y <= y0)           L[i] = pr, pr = i;         else           L[i] = 0;       L[q + 1] = pr;       pr = q + 1;       for (int i = q; i >= 1; i--)         if (p[i].y <= y0)           R[i] = pr, pr = i;         else           R[i] = 0;       R[0] = pr;       solve();       ans += res;       for (int y = 1; y <= y0; y++) {         for (int u : py[y]) erase(u);         ans += res;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct flight {     int d, f, t, c;   };   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     vector<flight> flights(m);     for (int i = 0; i < m; i++) {       scanf( %d %d %d %d , &flights[i].d, &flights[i].f, &flights[i].t,             &flights[i].c);     }     sort(flights.begin(), flights.end(),          [](const flight &a, const flight &b) { return a.d < b.d; });     vector<long long> miniCostTo(n + 1, (1LL << 50));     vector<long long> totalTo(1000001, (1LL << 50));     vector<bool> haveTo(n + 1);     int toCounter = 0;     long long costTo = 0;     int toIndex = 0;     for (int i = 0; i <= 1000000; i++) {       if (toCounter == n) {         totalTo[i] = costTo;       }       while (toIndex < m && flights[toIndex].d == i) {         flight f = flights[toIndex];         if (!f.t) {           if (miniCostTo[f.f] > f.c) {             if (miniCostTo[f.f] != (1LL << 50)) costTo -= miniCostTo[f.f] - f.c;             miniCostTo[f.f] = f.c;           }           if (!haveTo[f.f]) {             haveTo[f.f] = true;             costTo += f.c;             toCounter++;           }         }         toIndex++;       }     }     vector<long long> miniCostFrom(n + 1, (1LL << 50));     vector<long long> totalFrom(1000001, (1LL << 50));     vector<bool> haveFrom(n + 1);     int fromCounter = 0;     long long costFrom = 0;     int fromIndex = m - 1;     for (int i = 1000000; i >= 0; i--) {       if (fromCounter == n) {         totalFrom[i] = costFrom;       }       while (fromIndex >= 0 && flights[fromIndex].d == i) {         flight f = flights[fromIndex];         if (!f.f) {           if (miniCostFrom[f.t] > f.c) {             if (miniCostFrom[f.t] != (1LL << 50))               costFrom -= miniCostFrom[f.t] - f.c;             miniCostFrom[f.t] = f.c;           }           if (!haveFrom[f.t]) {             haveFrom[f.t] = true;             costFrom += f.c;             fromCounter++;           }         }         fromIndex--;       }     }     long long cheapest = (1LL << 50);     for (int i = 0; i + k - 1 <= 1000000; i++) {       cheapest = min(cheapest, totalTo[i] + totalFrom[i + k - 1]);     }     if (cheapest == (1LL << 50))       printf( -1 n );     else       printf( %I64d n , cheapest);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class classcomp {    public:     bool operator()(const int &l, const int &r) const { return l < r; }   };   long long mod = 1000000007;   void matrixmul(int p, int q, int r, long long *a, long long *b, long long *c) {     int i, j, k;     for (i = 0; i < p; i++) {       for (j = 0; j < r; j++) {         c[i * r + j] = 0;         for (k = 0; k < q; k++)           c[i * r + j] = (c[i * r + j] + a[i * q + k] * b[k * r + j]) % mod;       }     }   }   void exp(int q, long long *a, long long *c, long long n) {     int i, j;     if (n == 0) {       for (i = 0; i < q; i++) {         for (j = 0; j < q; j++) c[i * q + j] = 0;         c[i * q + i] = 1;       }       return;     }     long long x[q * q];     exp(q, a, x, n / 2);     matrixmul(q, q, q, x, x, c);     if (n % 2 == 1) {       memcpy(x, c, sizeof(x));       matrixmul(q, q, q, a, x, c);     }     return;   }   int main() {     long long n, m, k;     cin >> n >> m >> k;     long long a[m * m];     int i, j, l;     char s[10];     for (i = 0; i < m; i++)       for (j = 0; j < m; j++) a[i * m + j] = 1;     for (i = 0; i < k; i++) {       scanf( %s , s);       if ( a  <= s[0] && s[0] <=  z )         j = s[0] -  a ;       else         j = s[0] -  A  + 26;       if ( a  <= s[1] && s[1] <=  z )         l = s[1] -  a ;       else         l = s[1] -  A  + 26;       a[l * m + j] = 0;     }     long long ans[m * m];     exp(m, a, ans, n - 1);     long long ret = 0;     for (i = 0; i < m; i++)       for (j = 0; j < m; j++) {         ret = (ret + ans[i * m + j]) % mod;       }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v[1010][10], rez[1010];   int main() {     int n, nr = 0, l = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int p = 0;       for (int j = 1; j <= 5; j++) scanf( %d , &v[i][j]);     }     if (n > 200) {       printf( 0 );       return 0;     }     for (int i = 1; i <= n; i++) {       int p = 0;       for (int j = 1; j <= n; j++)         if (i != j) {           for (int k = 1; k <= n; k++)             if (j != k && i != k) {               int c = 0;               for (int m = 1; m <= 5; m++)                 c += (v[j][m] - v[i][m]) * (v[k][m] - v[i][m]);               if (c > 0) {                 p = 1;                 break;               }             }           if (p == 1) break;         }       if (p == 0) rez[++l] = i;     }     printf( %d n , l);     for (int i = 1; i <= l; i++) printf( %d n , rez[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using vi = vector<int>;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     cin >> t;     vi v(t);     for (int i = 0; i < t; i++) {       cin >> v[i];     }     int m = t;     priority_queue<int> qi;     for (int i = 0; i < t; i++) {       if (v[i] == m) {         m--;         cout << v[i] <<    ;         while (!qi.empty() && (qi.top() == m)) {           m--;           cout << qi.top() <<    ;           qi.pop();         }       } else         qi.push(v[i]);       cout <<   n ;     }     cerr <<   ntime taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs           << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int s, v1, v2, p1, p2;     scanf( %d%d%d%d%d , &s, &v1, &v2, &p1, &p2);     if (v1 * s + p1 * 2 < v2 * s + p2 * 2) printf( First n );     if (v1 * s + p1 * 2 > v2 * s + p2 * 2) printf( Second n );     if (v1 * s + p1 * 2 == v2 * s + p2 * 2) printf( Friendship n );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int N = 10;   int n, k;   int a[1005];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     int nn = n;     for (int i = 0; i < k; i++) {       cin >> a[i];       while (a[i] >= 4 && nn > 0) {         a[i] -= 4;         nn--;       }     }     int n2 = n * 2 + nn;     for (int i = 0; i < k; i++) {       while (a[i] >= 2 && n2 > 0) {         a[i] -= 2;         n2--;       }     }     int sum = 0;     for (int i = 0; i < k; i++) {       sum += a[i];     }     if (sum > nn + n2) {       cout <<  NO  << endl;     } else {       cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long int n;     scanf( %ld , &n);     long long int a[n], i, x = 0, b[n], j;     for (i = 0; i < n; i++) {       scanf( %lld , &a[i]);     }     b[0] = 1;     for (i = n - 2, j = 1; i >= 0; i--, j++) {       if (a[i] < a[i + 1]) {         b[j] = b[j - 1] + 1;       } else         b[j] = 1;     }     sort(b, b + n);     printf( %lld n , b[n - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<long long int> v(n);       for (long long int &x : v) cin >> x;       for (int i = 0; i < n; i++) {         if (i % 2)           v[i] = -abs(v[i]);         else           v[i] = abs(v[i]);         cout << v[i] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char c[100005];     scanf( %s , c);     vector<char> v;     int count[26];     int instk[26];     memset(count, 0, sizeof(count));     memset(instk, 0, sizeof(instk));     int find = 0;     for (int i = 0; c[i] != 0; i++) count[c[i] -  a ]++;     while (count[find] == 0) find++;     for (int i = 0; c[i] != 0; i++) {       v.push_back(c[i]);       count[c[i] -  a ]--;       while (count[find] == 0) find++;       while (!v.empty() && v.back() -  a  <= find) {         printf( %c , v.back());         v.pop_back();       }     }     while (!v.empty()) {       printf( %c , v.back());       v.pop_back();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       long long a, b, x, y;       cin >> a >> b >> x >> y;       long long distX = x - a, distY = y - b;       cout << distX * distY + 1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   int n, m;   int id[N * 2][N * 2];   map<int, int> mp;   vector<int> l[N * 2], e[N * 2];   vector<int> ord;   pair<int, int> a[N];   int dp[N * 2][N * 2];   pair<int, int> DP[N * 2][N * 2];   void type(int i, int j) {     if (DP[i][j].second == 0) {       if (i != 0) printf( %d  , i);       return;     }     if (DP[i][j].first) {       type(i, a[DP[i][j].second].first);       type(DP[i][j].second, a[DP[i][j].second].second);     } else       type(i, DP[i][j].second);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d%d , &x, &y);       a[i] = make_pair(x - y, x + y);       mp[x - y] = 1;       mp[x + y] = 1;     }     for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)       it->second = ++m;     for (int i = 1; i <= n; i++) {       a[i] = make_pair(mp[a[i].first], mp[a[i].second]);       id[a[i].first][a[i].second] = i;       e[a[i].second].push_back(i);       l[a[i].second - a[i].first + 1].push_back(i);     }     a[0] = make_pair(1, m);     l[m].push_back(0);     for (int _ = 1; _ <= m; _++)       for (int __ = 0; __ < l[_].size(); __++) {         int i = l[_][__];         for (int j = a[i].first; j <= a[i].second; j++) {           dp[i][j] = dp[i][j - 1];           DP[i][j] = make_pair(0, j - 1);           for (int k = 0; k < e[j].size(); k++) {             int p = e[j][k];             if (a[p].first >= a[i].first &&                 dp[i][j] < dp[i][a[p].first] + dp[p][a[p].second]) {               dp[i][j] = dp[i][a[p].first] + dp[p][a[p].second];               DP[i][j] = make_pair(1, p);             }           }         }         for (int j = a[i].first; j <= a[i].second; j++) dp[i][j]++;       }     printf( %d n , dp[0][m] - 1);     type(0, m);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const long long mod = 998244353;   long long ksm(long long a, long long b) {     long long ans = 1LL;     while (b) {       if (b & 1) ans = ans * a % mod;       a = a * a % mod;       b >>= 1;     }     return ans;   }   int a[N], c[N], tmp[N], ans, tot;   bool vis[N];   bool judge(int x, int y) {     for (int i = 1; i <= x; i++) {       int t = i;       bool flag = 0;       for (int j = 1; j <= y; j++) {         if (c[tmp[t]] != c[tmp[i]]) {           flag = 1;           break;         }         t = (t + x - 1) % tot + 1;       }       if (!flag) return 1;     }     return 0;   }   void dfs(int x) {     tot = 0;     tmp[++tot] = x;     vis[x] = 1;     for (int i = a[x]; i != x; i = a[i]) tmp[++tot] = i, vis[i] = 1;     for (int i = 1; i * i <= tot; i++) {       if (tot % i == 0) {         if (judge(i, tot / i)) {           ans = min(ans, i);           return;         }         if (judge(tot / i, i)) ans = min(ans, tot / i);       }     }   }   void solve() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) scanf( %d , &c[i]);     ans = INT_MAX;     for (int i = 1; i <= n; i++)       if (!vis[i]) dfs(i);     for (int i = 1; i <= n; i++) vis[i] = 0;     printf( %d n , ans);   }   int main() {     int _;     scanf( %d , &_);     while (_--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> dvsr;   void get(long long n) {     for (int i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         dvsr.push_back(i);         if (i != n / i) dvsr.push_back(n / i);       }     }   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     long long a, b;     cin >> a >> b;     if (a == b) {       cout << 0;       return 0;     }     if (a > b) swap(a, b);     long long df = b - a;     get(df);     set<pair<long long, long long> > s;     for (auto it : dvsr) {       if (a % it == 0 and b % it == 0) {         s.insert({(a * b) / it, 0});       } else {         long long dif = it * (b / it + 1) - b;         long long x = a + dif, y = b + dif;         s.insert({(x * y) / it, dif});       }     }     long long ans = b;     auto i1 = s.begin();     cout << i1->second;   }


#include<bits/stdc++.h>   using namespace std;   #define int long long   #define fast_io ios_base::sync_with_stdio(0);cin.tie(0)    #define mod 1e9 + 7   #define pb push_back   #define vi vector<int>      int32_t main(){       fast_io;       int t;cin>>t;       while(t--){           int n;cin>>n;           set<int> s;           vi arr(n);           for(int i=0;i<n;i++){               int x;cin>>x;               arr[i] = x;               s.insert(x);           }           sort(arr.rbegin(),arr.rend());           int z = arr[n-1];           int ans = 0;           for(int i=0;i<n;i++){               if(arr[i]!=z){                   ans++;               }           }           cout<<ans<<endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string str;     cin >> str;     set<char> a;     for (int i = 0; i < n; i++) {       a.insert(tolower(str[i]));     }     if (a.size() == 26) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int h, m;     cin >> h >> m;     long long int t = h * 60 + m;     long long int a = 1440 - t;     cout << a << endl;   }   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     long long int t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2010;   int T, n, m, vis[maxn], cnt[maxn];   char s[maxn];   bitset<maxn> b[maxn];   queue<pair<int, int>> Q;   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d %d , &n, &m);       for (int i = 1; i <= n; i++) {         b[i].reset(), vis[i] = 0;       }       for (int i = 1; i <= m; i++) {         scanf( %s , s + 1), cnt[i] = 0;         for (int j = 1; j <= n; j++) {           if (s[j] ==  1 ) cnt[i]++;         }         if (cnt[i] == 1) continue;         for (int j = 1; j <= n; j++) {           if (s[j] ==  1 ) b[j].set(i);         }       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++)           if (i ^ j) {             if ((b[i] & b[j]) == b[i]) Q.emplace(i, j);           }       }       vector<pair<int, int>> E;       while (!Q.empty()) {         int u = Q.front().first, v = Q.front().second;         Q.pop();         if (vis[u] || vis[v]) continue;         vis[u] = 1, E.emplace_back(u, v);         for (int i = 1; i <= m; i++) {           if (b[u][i]) cnt[i]--;         }         for (int i = 1; i <= m; i++)           if (cnt[i] == 1) {             cnt[i] = 0;             for (int j = 1; j <= n; j++)               if (b[j][i] && !vis[j]) {                 b[j][i] = 0;                 for (int k = 1; k <= n; k++)                   if (j ^ k && !vis[k]) {                     if ((b[j] & b[k]) == b[j]) Q.emplace(j, k);                   }               }           }       }       if (E.size() < n - 1) {         puts( NO );         continue;       }       puts( YES );       for (auto p : E) {         printf( %d %d n , p.first, p.second);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = (int)1e9 + 9;   bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     return a.first < b.first;   }   int main() {     int n, m;     cin >> n >> m;     vector<pair<int, int> > k(n);     for (int(i) = (0); (i) < (n); ++(i)) {       cin >> k[i].first >> k[i].second;     }     sort(((k).begin()), ((k).end()), cmp);     double ans = oo;     for (int(i) = (0); (i) < (n); ++(i)) {       ans = min(ans, (double)k[i].first / (double)k[i].second);     }     ans *= (double)m;     cout << fixed << setprecision(8) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1000 * 1000 * 1000;   const long long inf64 = 1ll * inf * inf;   const long long mod = 1e9 + 7;   mt19937 rng(0);   long long binpow(long long a, long long p, long long mod = 998244353) {     if (p == 0) return 1;     if (p % 2 == 0) {       long long t = binpow(a, p / 2);       return t * t % mod;     } else {       return binpow(a, p - 1) * a % mod;     }   }   long long invert(long long x) { return binpow(x, 998244351); }   pair<long long, long long> add(pair<long long, long long> a,                                  pair<long long, long long> b) {     pair<long long, long long> ans;     ans.first = a.first * b.second + b.first * a.second;     ans.second = a.second * b.second;     ans.first %= 998244353;     ans.second %= 998244353;     return ans;   }   pair<long long, long long> multi(pair<long long, long long> a,                                    pair<long long, long long> b) {     return {a.first * b.first % 998244353, a.second * b.second % 998244353};   }   void run() {     long long n;     cin >> n;     vector<vector<long long>> a(n);     vector<vector<long long>> poses(1e6 + 10);     for (long long i = 0; i < n; i++) {       long long k;       cin >> k;       a[i].resize(k);       for (long long j = 0; j < k; j++) {         cin >> a[i][j];         poses[a[i][j]].push_back(i);       }     }     pair<long long, long long> ans = {0, 1};     for (long long i = 0; i < n; ++i) {       pair<long long, long long> t = {1, n};       for (long long k = 0; k < a[i].size(); ++k) {         pair<long long, long long> r = {1, a[i].size()};         pair<long long, long long> e = {poses[a[i][k]].size(), n};         ans = add(ans, multi(multi(t, r), e));       }     }     cout << ans.first * invert(ans.second) % 998244353;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     for (long long i = 0; i < t; i++) {       run();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> X;   map<long long, pair<int, long long> > M;   pair<int, long long> go(long long x) {     if (x <= 1) return {x, x};     if (M.find(x) != M.end()) return M[x];     int i = upper_bound(X.begin(), X.end(), x) - X.begin() - 1;     int p1, p2;     long long q1, q2;     pair<int, long long> P1;     P1 = go(x - X[i]);     p1 = P1.first;     q1 = P1.second;     P1 = go(X[i] - 1);     p2 = P1.first;     q2 = P1.second;     return M[x] = max(make_pair(p1 + 1, q1 + X[i]), {p2, q2});   }   int main() {     for (int i = 0; i <= 1e5 + 7; ++i) X.push_back(1LL * i * i * i);     long long x;     cin >> x;     pair<int, long long> Ans;     Ans = go(x);     cout << Ans.first <<     << Ans.second <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     if (n == 1) {       cout << 2;       return 0;     }     if (n == 2) {       cout << 3;       return 0;     }     long long int curr = 2, line = 3, to = 2;     while (to < n) {       line++;       to += curr;       if (to >= n) {         cout << line;         return 0;       }       line++;       to += (curr);       curr++;       if (to >= n) {         cout << line;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x1, y1, z, x, y;     cin >> x1 >> y1 >> z;     x = x1 - y1 + z;     y = x1 - y1 - z;     if (x > 0 && y > 0)       cout <<  +             <<   n ;     else if (x < 0 && y < 0)       cout <<  -             <<   n ;     else if (x == 0 && y == 0)       cout <<  0             <<   n ;     else       cout <<  ?             <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10, lgN = 29, INF = 2e9;   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   struct segTree {     struct node {       int l, r, w, flg;     } sgt[N * lgN];     int p = 0, rt[N] = {0};     void insert(int &i, int b, int e, int k, int x) {       sgt[++p] = sgt[i];       i = p;       if (b == e) {         sgt[i].w = sgt[i].w ? min(sgt[i].w, x) : x;         return;       }       int mid = (b + e) >> 1;       if (k <= mid)         insert(sgt[i].l, b, mid, k, x);       else         insert(sgt[i].r, mid + 1, e, k, x);       sgt[i].w = max(sgt[sgt[i].l].w, sgt[sgt[i].r].w);     }     int query(int i, int b, int e, int l, int r) {       if (e < l || r < b) return 0;       if (i == 0) return INF;       if (l <= b && e <= r) return sgt[i].w;       int mid = (b + e) >> 1, lc = sgt[i].l, rc = sgt[i].r;       return max(query(lc, b, mid, l, r), query(rc, mid + 1, e, l, r));     }     void walk(int i, int b, int e, int dep) {       if (!i) return;       int mid = (b + e) >> 1, lc = sgt[i].l, rc = sgt[i].r;       if (b != e) walk(lc, b, mid, dep + 1);       for (int i = 0; i < dep; i++) printf(    );       printf( %d[%d,%d]w=%d n , i, b, e, sgt[i].w);       if (b != e) walk(rc, mid + 1, e, dep + 1);     }   } t;   struct seg {     int l, r, p;   } a[N];   bool comp(seg a, seg b) { return a.l < b.l; }   int n, m, k, rt[N];   int bin(int x) {     int l = 1, r = k, ret = -1;     while (l <= r) {       int mid = (l + r) >> 1;       if (a[mid].l >= x)         ret = mid, r = mid - 1;       else         l = mid + 1;     }     return ret;   }   int main() {     n = read();     m = read();     k = read();     for (int i = 1; i <= k; i++) {       a[i].l = read();       a[i].r = read();       a[i].p = read();     }     sort(a + 1, a + k + 1, comp);     t.sgt[0].w = INF;     for (int i = k; i; i--) {       rt[i] = rt[i + 1];       t.insert(rt[i], 1, n, a[i].p, a[i].r);     }     for (int i = 1; i <= m; i++) {       int a = read(), b = read(), x = read(), y = read();       int lb = bin(x);       if (lb == -1 || t.query(rt[lb], 1, n, a, b) > y)         printf( no n );       else         printf( yes n );       fflush(stdout);     }   }


#include <bits/stdc++.h>   using namespace std;   long long N, K;   long long Top, Bot, H[200005], cnt[200005];   long long hcnt[200005];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> N >> K;     Top = 0, Bot = 2e5;     for (int i = 0; i <= N - 1; i++) {       cin >> H[i];       cnt[H[i]]++;       Top = max(Top, H[i]);       Bot = min(Bot, H[i]);     }     long long curc = 0;     for (int i = Top; i >= 1; i--) {       curc += cnt[i];       hcnt[i] += curc;     }     long long cutt = 0;     int ans = 0;     for (int i = Top; i >= Bot + 1; i--) {       if (cutt + hcnt[i] > K) {         ans++;         cutt = hcnt[i];       } else         cutt += hcnt[i];     }     if (cutt) ans++;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n, l, r;     cin >> n;     while (n-- && cin >> l >> r) {       if (l * 2 > r)         cout <<  YES ;       else         cout <<  NO ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n;   int main() {     ios::sync_with_stdio(false), cin.tie(0);     cin >> n;     long long a = (n + 2) * (n + 1) * n / 6,               b = (n + 4) * (n + 3) * (n + 2) * (n + 1) * n / 120;     cout << a * b;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long int n, i, m, k, x, pc = 0, c = 0;     cin >> n >> m >> k;     long long int p, f;     queue<long long int> q;     for (i = 0; i < m; i++) {       cin >> x;       q.push(x - 1);     }     x = 0;     while (!q.empty()) {       f = q.front();       p = (f - c) / k;       while (!q.empty() && ((q.front() - c) / k) == p) {         pc++;         q.pop();       }       c = pc;       x++;     }     cout << x;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, ed;   double p, M;   long long T;   vector<pair<double, double> > v;   pair<double, double> sta[100004];   double cros(pair<double, double> a, pair<double, double> b) {     return (b.second - a.second) / (a.first - b.first);   }   double r[100004], ret;   double mat[35][3][3], cur[3];   int main() {     scanf( %d%I64d , &n, &T);     for (int i = 0; i < n; i++) {       scanf( %d%d%lf , &a, &b, &p);       M = max(M, p * b);       v.push_back(make_pair(p, -p * a));     }     sort(v.begin(), v.end());     for (int i = 0; i < n; i++) {       if (i > 0 && v[i].first == v[i - 1].first) continue;       v[i].second *= -1;       while (ed > 1 && cros(sta[ed - 1], sta[ed]) > cros(sta[ed], v[i])) ed--;       sta[++ed] = v[i];     }     for (int i = 1; i < ed; i++) r[i] = cros(sta[i], sta[i + 1]);     r[ed] = 1e18 + 114;     double lst = 0;     long long t1 = 0;     for (int i = 1; i <= ed; i++) {       if (t1 == T || (t1 * M - lst) >= r[i]) continue;       mat[0][0][0] = 1 - sta[i].first, mat[0][0][1] = 0, mat[0][0][2] = 0;       mat[0][1][0] = sta[i].first * M, mat[0][1][1] = 1, mat[0][1][2] = 0;       mat[0][2][0] = sta[i].second, mat[0][2][1] = 1, mat[0][2][2] = 1;       for (int j = 1; j <= 33; j++) {         for (int i1 = 0; i1 < 3; i1++)           for (int j1 = 0; j1 < 3; j1++) {             mat[j][i1][j1] = 0;             for (int k = 0; k < 3; k++)               mat[j][i1][j1] += mat[j - 1][i1][k] * mat[j - 1][k][j1];           }       }       for (int j = 33; j >= 0; j--) {         if (t1 + (1ll << j) >= T) continue;         cur[0] = lst, cur[1] = t1, cur[2] = 1;         ret = 0;         for (int k = 0; k < 3; k++) ret += cur[k] * mat[j][k][0];         if (M * (t1 + (1ll << j)) - ret < r[i]) lst = ret, t1 += (1ll << j);       }       cur[0] = lst, cur[1] = t1, cur[2] = 1;       lst = 0;       for (int j = 0; j < 3; j++) lst += cur[j] * mat[0][j][0];       t1++;     }     printf( %.6lf , lst);   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string s;     cin >> s;     string ans;     ans += s[0];     for (int i = 1; i <= s.length() - 2; i = i + 2) {       ans += s[i];     }     ans += s[s.length() - 1];     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long test;     cin >> test;     while (test--) {       solve();       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a[101], t = 0;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       t += a[i];     }     int ans = 0;     for (int j = 1; j <= 5; j++) {       int k = t + j, i = 0;       while (k) {         k--;         if (!k) break;         i = ((i + 1) % (n + 1));       }       if (i != 0) ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     map<int, vector<int>> frec;     for (int i = 0; i < n; i++) {       int val;       cin >> val;       frec[val].push_back(i);     }     int x = 0;     vector<int> ans(n);     for (int i = 0; i < n; i++) {       while (frec[x].empty() && x >= 3) x -= 3;       if (frec[x].empty()) {         cout <<  Impossible n ;         return 0;       }       ans[i] = frec[x].back() + 1;       frec[x].pop_back();       x++;     }     cout <<  Possible n ;     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MM = 2e5 + 5;   int n, k, a, b, q, pref[MM], suff[MM], cnt[MM];   void upd(int bit[], int x, int v) {     for (; x < MM; x += x & -x) {       bit[x] += v;     }   }   int get(int bit[], int x) {     int res = 0;     for (; x > 0; x -= x & -x) res += bit[x];     return res;   }   int get(int bit[], int l, int r) { return get(bit, r) - get(bit, l - 1); }   int main() {     cin.tie(0)->sync_with_stdio(0);     cin >> n >> k >> a >> b >> q;     while (q--) {       int t;       cin >> t;       if (t == 1) {         int x, v;         cin >> x >> v;         int val = min(b, v + cnt[x]);         int diff = val - get(pref, x, x);         upd(pref, x, diff);         val = min(a, v + cnt[x]);         diff = val - get(suff, x, x);         upd(suff, x, diff);         cnt[x] += v;       } else {         int p;         cin >> p;         int ans = get(pref, p - 1);         ans += get(suff, p + k, MM - 1);         cout << ans <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e+5 + 10;   long long b[MAXN], l[MAXN], r[MAXN], o[MAXN], dp[MAXN];   int main() {     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       cin >> b[i];     }     for (int i = 1; i < n; i++) {       l[i] = b[i - 1] - (b[i - 1] % 2) + l[i - 1];       if (b[i - 1] == 1) l[i] = 0;     }     for (int i = n - 2; i > -1; i--) {       r[i] = b[i] - (b[i] % 2) + r[i + 1];       if (b[i] == 1) r[i] = 0;     }     for (int i = 1; i < n; i++) {       o[i] = o[i - 1] + b[i - 1] - (b[i - 1] % 2 == 0);     }     long long ans = 0, m = 0;     for (int i = 0; i < n; i++) {       m = max(m, l[i] - o[i]);       ans = max(ans, r[i] + o[i] + m);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve();   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int64_t t;     cin >> t;     while (t--) solve();     return 0;   }   vector<vector<int64_t>> to;   vector<bool> bobCan, parcouru;   vector<int64_t> pId;   int64_t nbPar;   int64_t da, db, goal;   void dfsBob(int64_t n, int64_t dist) {     if (bobCan[n]) return;     bobCan[n] = true;     goal++;     if (dist == 0) return;     for (int64_t &i : to[n]) dfsBob(i, dist - 1);   }   int64_t dfs2(int64_t n, int64_t dist, int64_t id) {     if (pId[n] == id) return 0;     pId[n] = id;     int64_t score = 0;     if (bobCan[n]) score = 1;     if (dist == 0) return score;     for (int64_t &i : to[n]) score += dfs2(i, dist - 1, id);     return score;   }   bool dfs1(int64_t n, int64_t dist) {     if (parcouru[n]) return false;     parcouru[n] = true;     if (dfs2(n, da, nbPar++) == goal) return true;     if (dist == 0) return false;     for (int64_t &i : to[n])       if (dfs1(i, dist - 1)) return true;     return false;   }   bool recherche(int64_t n, int64_t dist, int64_t m) {     if (parcouru[n]) return false;     parcouru[n] = true;     if (n == m) return true;     if (dist == 0) return false;     for (int64_t &i : to[n])       if (recherche(i, dist - 1, m)) return true;     return false;   }   void solve() {     int64_t n, a, b;     cin >> n >> a >> b >> da >> db;     to = vector<vector<int64_t>>(n + 1);     bobCan = parcouru = vector<bool>(n + 1);     for (int64_t i = 0; i < n - 1; i++) {       pair<int64_t, int64_t> input;       cin >> input.first >> input.second;       to[input.first].push_back(input.second);       to[input.second].push_back(input.first);     }     if (db <= 2 * da) {       cout <<  Alice n ;       return;     }     if (recherche(a, da, b)) {       cout <<  Alice n ;       return;     }     parcouru = vector<bool>(n + 1);     goal = 0;     dfsBob(b, db);     parcouru = vector<bool>(n + 1);     pId = vector<int64_t>(n + 1);     nbPar = 1;     if (dfs1(n, da)) {       cout <<  Alice n ;       return;     }     {       cout <<  Bob n ;       return;     }     return;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   long long int mul(long long int x, long long int y) {     return (x * 1ll * y) % mod;   }   long long int add(long long int x, long long int y) {     x += y;     while (x >= mod) x -= mod;     while (x < 0) x += mod;     return x;   }   long long int dp[200005][3];   long long int a, b, c;   long long int n, l, r;   long long int fun(int i, int j) {     if (i == n) {       if (j == 0)         return 1;       else         return 0;     }     if (dp[i][j] != -1) return dp[i][j];     dp[i][j] = add(         mul(a, fun(i + 1, (j) % 3)),         add(mul(b, fun(i + 1, (j + 1) % 3)), mul(c, fun(i + 1, (j + 2) % 3))));     return dp[i][j];   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n >> l >> r;     long long int x = (l - 1 + 2) / 3;     long long int y = (r - 1) / 3;     b = y - x + 1;     x = (l - 2 + 2) / 3;     y = (r - 2) / 3;     c = y - x + 1;     x = (l + 2) / 3;     y = (r) / 3;     a = y - x + 1;     if (l == 1 && r == 1) {       a = 0, b = 1, c = 0;     }     memset(dp, -1, sizeof(dp));     long long int ans = 0;     ans = add(ans, mul(a, fun(1, 0)));     ans = add(ans, mul(b, fun(1, 1)));     ans = add(ans, mul(c, fun(1, 2)));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     char c =  a ;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  z  + 1)         continue;       else if (s[i] != c) {         cout <<  NO n ;         return 0;       } else {         for (int j = i; j < s.size(); j++) {           if (s[j] == c) s[j] =  z  + 1;         }         c++;       }     }     cout <<  YES n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long max_n = 1e6 + 20;   long long n, m, k, ans, sum;   long long a[max_n];   long long mark[max_n];   vector<long long> v, adj[max_n], jda[max_n];   void dfs(long long v) {     mark[v] = 1;     if (adj[v].size() == 2) ans = -1;     for (auto i : adj[v]) {       if (!mark[i]) {         dfs(i);       }     }   }   int32_t main() {     cin >> n;     for (long long i = 1; i < n; i++) {       long long u, v;       cin >> u >> v;       u--, v--;       adj[v].push_back(u);       adj[u].push_back(v);     }     if (n == 2) {       cout <<  YES ;       return 0;     }     for (long long i = 0; i < n; i++) {       if (adj[i].size() == 1 && !mark[i]) dfs(i);     }     if (ans == -1) {       cout <<  NO ;     } else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[111111], ans[111111];   int n, kk;   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n); ++i) scanf( %I64d , &a[i]);     sort(a + 1, a + n + 1);     reverse(a + 1, a + n + 1);     for (int i = (1); i <= (n); ++i) a[i] += a[i - 1];     scanf( %d , &kk);     for (int t = (0); t < (kk); ++t) {       int k;       scanf( %d , &k);       k = (k > n - 1) ? n - 1 : k;       if (!ans[k]) {         long long l, r, st, tmp;         l = r = tmp = 1;         st = 0;         while (l <= n) {           ans[k] += (a[r] - a[l - 1]) * st;           tmp *= k;           ++st;           l = r + 1;           r += tmp;           r = (r > n) ? n : r;         }       }       printf( %I64d  , ans[k]);     }     return 0;   }


#include <bits/stdc++.h>   int main() {     unsigned long long no_of_paths;     unsigned long long no_of_arrangements;     scanf( %I64u , &no_of_paths);     no_of_arrangements = (no_of_paths) * (no_of_paths - 1) / 2 *                          (no_of_paths - 2) / 3 * (no_of_paths - 3) / 4 *                          (no_of_paths - 4) / 5;     no_of_arrangements *= (no_of_paths) * (no_of_paths - 1) * (no_of_paths - 2) *                           (no_of_paths - 3) * (no_of_paths - 4);     printf( %I64u n , no_of_arrangements);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, mod = 1, cnt = 0, i, min;     cin >> n;     vector<int> v{1, 5, 10, 20, 100};     while (n) {       min = INT_MAX;       for (i = 0; i < 5; ++i) {         if (n / v[i] >= 1) {           if (min > n / v[i]) {             min = n / v[i];             mod = n % v[i];           }         }       }       cnt += min;       n = mod;     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   int t = 1000001;   class myData {    public:     int t;     int x;     int y;     int d;     myData(int t, int x, int y, int d) : t(t), x(x), y(y), d(d) {}     bool operator<(const myData &a) const { return (this->t < a.t); }   };   long long exponentMod(long long A, long long B, long long C) {     if (A == 0) return 0;     if (B == 0) return 1;     long long y;     if (B % 2 == 0) {       y = exponentMod(A, B / 2, C);       y = (y * y) % C;     } else {       y = A % C;       y = (y * exponentMod(A, B - 1, C) % C) % C;     }     return (long long)((y + C) % C);   }   pair<int, int> factorize(long long n) {     int ans = 1;     int c = 0;     for (long long i = 2; i * i <= n; ++i) {       while (n % i == 0) {         if (i != 2) ans *= i, c++;         n /= i;       }     }     if (n != 1) {       if (n != 2) ans *= n, c++;     }     return {ans, c};   }   map<long long, long long> bit;   int N1 = 2e5;   void update(long long i) {     for (; i <= N1; i += i & (-i)) bit[i]++;   }   long long get(long long i) {     long long ans = 0;     for (; i > 0; i -= i & (-i)) ans += bit[i];     return ans;   }   int now;   bool sortByVal(const myData &a, const myData &b) { return (a.t < b.t); }   long long ans1 = 0;   int spf[1000003];   long long calc[1000003 + 1];   long long nCk(long long n, long long k, long long mod) {     return ((((calc[n] % mod) * exponentMod(calc[k], mod - 2, mod)) % mod) *             exponentMod(calc[n - k], mod - 2, mod)) %            mod;   }   void sieve() {     spf[1] = 1;     for (int i = 2; i < 1000003; i++) spf[i] = i;     for (int i = 4; i < 1000003; i += 2) spf[i] = 2;     for (int i = 3; i * i < 1000003; i++) {       if (spf[i] == i) {         for (int j = i * i; j < 1000003; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   set<int> getFactorization(int x) {     set<int> ret;     while (x != 1) {       ret.insert(spf[x]);       x = x / spf[x];     }     return ret;   }   bool prime[1000003 + 1];   void sieve2() {     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= 1000003; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= 1000003; i += p) prime[i] = false;       }     }   }   int MEX(int a[], int n) {     set<int> s;     for (int i = 0; i < n; i++) {       s.insert(a[i]);     }     int c = 0;     for (auto i = s.begin(); i != s.end(); i++) {       if (*i == c) {         c++;       } else         break;     }     return c;   }   int smallestDivisor(int n) {     if (n % 2 == 0) return 2;     for (int i = 3; i * i <= n; i += 2) {       if (n % i == 0) return i;     }     return n;   }   map<int, pair<int, int>> m;   const int maxn = 3e3 + 110;   const int mod = 1e9 + 7;   int dp[maxn][310];   int a[310], b[310];   int main() {     int l, n, x, y, ans = 0;     cin >> n >> l;     for (int i = 1; i <= n; i++) {       cin >> a[i] >> b[i];       dp[a[i]][i]++;       if (a[i] != b[i]) {         a[i + n] = b[i];         b[i + n] = a[i];         dp[a[i + n]][i + n]++;       }     }     for (int i = 1; i <= l; i++) {       for (int j = 1; j <= n * 2; j++) {         if (!dp[i][j]) continue;         for (int k = 1; k <= n * 2; k++) {           if ((j % n) != (k % n) && b[j] == a[k])             dp[i + a[k]][k] = (dp[i + a[k]][k] + dp[i][j]) % mod;         }       }     }     for (int i = 1; i <= 2 * n; i++) ans = (ans + dp[l][i]) % mod;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long T, m, n, q, w, mi, ma, cnt, s, t, fi, cc, e, r, z, x, an, anx, any,       cx, cy;   long long d[5][2];   int main() {     scanf( %I64d%I64d , &m, &n);     scanf( %I64d%I64d , &e, &r);     scanf( %I64d%I64d , &cx, &cy);     for (int i = 0; i < 4; i++) {       q = e;       w = r;       d[i][0] = e - m;       d[i][1] = r - n;       e = -w;       r = q;     }     fi = 0;     for (int i = 0; i < 4; i++) {       if (d[i][0] == 0 && d[i][1] == 0) {         fi = 1;         break;       }       if (cx == 0 && cy == 0) continue;       if ((d[i][1] * cx - d[i][0] * cy) % (cx * cx + cy * cy) == 0)         if ((d[i][0] * cx + d[i][1] * cy) % (cx * cx + cy * cy) == 0) {           fi = 1;           break;         }     }     if (fi == 1)       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class sparse_table {     vector<int> values;     vector<int> maxk;     vector<vector<int> > st;     void build(const vector<int>& a) {       values = a;       int n = a.size();       st.resize(maxk[n] + 1);       st[0].resize(n);       for (int i = 0; i < n; i++) st[0][i] = i;       for (int i = 1; i <= maxk[n]; i++) {         st[i].resize(n - (1 << i) + 1);         for (int j = 0; j < (int)st[i].size(); j++) {           if (values[st[i - 1][j]] < values[st[i - 1][j + (1 << (i - 1))]])             st[i][j] = st[i - 1][j];           else             st[i][j] = st[i - 1][j + (1 << (i - 1))];         }       }     }       public:     sparse_table(const vector<int>& vec) {       int n = vec.size();       maxk.resize(n + 1, 0);       for (int i = 2; i < n + 1; i++) maxk[i] = maxk[i / 2] + 1;       build(vec);     }     sparse_table() {}     int get(int l, int r) {       if (values[st[maxk[r - l]][l]] <           values[st[maxk[r - l]][r - (1 << maxk[r - l])]])         return st[maxk[r - l]][l];       else         return st[maxk[r - l]][r - (1 << maxk[r - l])];     }   };   int n;   vector<vector<int> > graph;   vector<int> vec;   vector<int> height;   vector<int> first;   vector<int> tin, tout, parent;   int cur_time = 0;   void dfs(int u, int par, int hg) {     height[u] = hg;     vec.push_back(u);     first[u] = vec.size() - 1;     tin[u] = cur_time++;     parent[u] = par;     for (int v : graph[u])       if (v != par) {         vec.push_back(u);         dfs(v, u, hg + 1);       }     tout[u] = cur_time++;   }   sparse_table st;   void precalc_lca() {     vec.clear();     height.clear();     first.clear();     height.resize(n);     first.resize(n);     tin.resize(n);     tout.resize(n);     parent.resize(n);     dfs(0, -1, 0);     vector<int> to_st(vec.size());     for (int i = 0; i < (int)vec.size(); i++) to_st[i] = height[vec[i]];     st = sparse_table(to_st);   }   int lca(int u, int v) {     int a = min(first[u], first[v]);     int b = max(first[u], first[v]);     return vec[st.get(a, b + 1)];   }   int dist(int u, int v) { return height[u] + height[v] - 2 * height[lca(u, v)]; }   bool is_child(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; }   int c1 = 0, c2 = 0, x = 0;   int add_vertex(int v) {     if (c1 == c2) {       if (dist(c1, v) == x + 1) {         x++;         if (is_child(c1, v)) {           auto it = upper_bound(               graph[c1].begin(), graph[c1].end(), v,               [](const int& i, const int& j) -> bool { return tin[i] < tin[j]; });           assert(it != graph[c1].begin());           it--;           c2 = *it;         } else {           c2 = parent[c1];         }       }     } else {       if (dist(c1, v) == x + 1) {         c1 = c2;       } else if (dist(c2, v) == x + 1) {         c2 = c1;       }     }     if (c1 == c2) {       return 2 * x;     } else {       return 2 * x - 1;     }   }   int main() {     cin >> n;     graph.resize(n);     for (int i = 1; i < n; i++) {       int a;       cin >> a;       a--;       graph[a].push_back(i);       graph[i].push_back(a);     }     precalc_lca();     for (int i = 1; i < n; i++) {       cout << add_vertex(i) <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long B = 360;   const long long N = 100005;   long long block(long long x) { return x / B; }   long long offset(long long x) { return x % B; }   deque<long long> a[B];   signed c[B][N];   long long n, q;   signed main() {     ios::sync_with_stdio(false);     cin >> n;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       a[block(i)].push_back(x);       c[block(i)][x]++;     }     cin >> q;     long long lastans = 0;     for (long long i = 0; i < q; i++) {       long long op, l, r, k;       cin >> op >> l >> r;       l = (l + lastans - 1) % n;       r = (r + lastans - 1) % n;       if (l > r) swap(l, r);       if (op == 2) cin >> k;       k = (k + lastans - 1) % n + 1;       long long L = block(l);       long long R = block(r);       l = offset(l);       r = offset(r);       if (op == 1) {         if (L == R) {           long long x = a[R][r];           a[R].erase(a[R].begin() + r);           a[L].insert(a[L].begin() + l, x);         } else {           for (long long i = L; i < R; i++) {             long long x = a[i].back();             a[i].pop_back();             c[i][x]--;             c[i + 1][x]++;             a[i + 1].push_front(x);           }           long long x = a[R][r + 1];           a[R].erase(a[R].begin() + r + 1);           a[L].insert(a[L].begin() + l, x);           c[R][x]--;           c[L][x]++;         }       } else {         long long ans = 0;         if (L == R) {           for (long long i = l; i <= r; i++) ans += a[L][i] == k;         } else {           for (long long i = l; i < a[L].size(); i++) ans += a[L][i] == k;           for (long long i = 0; i <= r; i++) ans += a[R][i] == k;           for (long long i = L + 1; i <= R - 1; i++) ans += c[i][k];         }         cout << (lastans = ans) << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     int x;     int y;   };   int main() {     int n;     cin >> n;     Point P[n];     for (int i = 0; i < n; i++) {       cin >> P[i].x >> P[i].y;     }     int val;     if (n < 2)       val = -1;     else if (n == 2) {       if (P[0].x != P[1].x && P[0].y != P[1].y)         val = abs(P[0].x - P[1].x) * abs(P[0].y - P[1].y);       else         val = -1;     } else {       int alt, larg;       if ((P[0].x - P[1].x) != 0)         alt = P[0].x - P[1].x;       else {         alt = P[0].x - P[2].x;       }       if ((P[0].y - P[1].y) != 0)         larg = P[0].y - P[1].y;       else {         larg = P[0].y - P[2].y;       }       val = abs(alt * larg);     }     cout << val << endl;   }


#include <bits/stdc++.h>   using namespace std;   int q, n;   int par[1 << 20][32], dep[1 << 20];   int da[500000];   void add(int v) {     par[n][0] = par[n + 1][0] = v;     dep[n] = dep[n + 1] = dep[v] + 1;     for (int i = (int)(0); i < (int)(20); i++)       par[n][i + 1] = par[n + 1][i + 1] = par[par[n][i]][i];   }   int lca(int u, int v) {     if (dep[u] > dep[v]) swap(u, v);     int dif = dep[v] - dep[u];     for (int i = (int)(0); i < (int)(21); i++)       if (dif & (1 << i)) v = par[v][i];     if (u == v) return u;     for (int i = 20; i >= 0; i--)       if (par[u][i] != par[v][i]) {         u = par[u][i];         v = par[v][i];       }     return par[u][0];   }   int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }   void solve() {     int u = 2, v = 3, d = 2, x, i = 0, jos = q;     ;     dep[2] = dep[3] = dep[4] = 1;     par[2][0] = par[3][0] = par[4][0] = 1;     n = 5;     while (q--) {       scanf( %d , &x);       add(x);       int du = dist(n, u), dv = dist(n, v);       if (du > d)         v = n, d = du;       else if (dv > d)         u = n, d = dv;       n += 2;       da[i] = d;       i++;     }     int j;     for (j = 0; j < jos; j++) {       printf( %d n , da[j]);     }   }   int main() {     scanf( %d , &q);     solve();     return 0;   }