#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000001;   const double EPS = 10e-9;   int const S = 1 << 19;   int n, a[S], leftlink[S], rightlink[S], decrease;   pair<int, int> tree[2 * S];   pair<int, int> Min() { return tree[1]; }   void Init() {     tree[S] = make_pair(INF, 0);     for (int i = 1; i <= (n); ++i) tree[i + S] = make_pair(a[i], i);     for (int i = n + 1; i <= (S - 1); ++i) tree[S + i] = make_pair(INF, i);     for (int i = S - 1; i >= (1); --i)       tree[i] = min(tree[2 * i], tree[2 * i + 1]);   }   void Delete(int p) {     for (tree[p + S] = make_pair(INF, p), p += S, p >>= 1; p; p >>= 1)       tree[p] = min(tree[p * 2], tree[p * 2 + 1]);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= (n); ++i) {       cin >> a[i];       leftlink[i] = i - 1;       rightlink[i] = i + 1;     }     rightlink[n] = 0;     Init();     pair<int, int> greedy;     long long ans = 0;     while (n) {       if (n <= 2) break;       greedy = Min();       Delete(greedy.second);       if (!leftlink[greedy.second]) {         ans += ((long long)(n - 2)) * (greedy.first - decrease);         leftlink[rightlink[greedy.second]] = 0;         decrease = greedy.first;         --n;         continue;       }       if (!rightlink[greedy.second]) {         ans += ((long long)(n - 2)) * (greedy.first - decrease);         decrease = greedy.first;         rightlink[leftlink[greedy.second]] = 0;         --n;         continue;       }       leftlink[rightlink[greedy.second]] = leftlink[greedy.second];       rightlink[leftlink[greedy.second]] = rightlink[greedy.second];       ans +=           min(a[leftlink[greedy.second]], a[rightlink[greedy.second]]) - decrease;       --n;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0, m = 0, temp = 0;     cin >> n >> m;     int arr[n + 1];     fill(arr, arr + n + 1, -1);     for (int i = 1; i <= m; i++) {       cin >> temp;       int k = temp;       while (k <= n && arr[k] == -1) {         arr[k] = temp;         k++;       }     }     for (int j = 1; j <= n; j++) cout << arr[j] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, cost;   int di[4] = {0, -1, 0, 1};   int dj[4] = {-1, 0, 1, 0};   char grid[51][51];   int vis[51][51];   pair<int, int> pi;   pair<int, int> pf;   vector<pair<int, int>> ili;   vector<pair<int, int>> ilf;   void dfs(int r, int c, vector<pair<int, int>>* island) {     vis[r][c] = 1;     (*island).push_back({r, c});     for (int v = 0; v < 4; v++) {       int i = r + di[v];       int j = c + dj[v];       if (i > 0 && i <= n && j > 0 && j <= n) {         if (grid[i][j] ==  0  && !vis[i][j]) {           dfs(i, j, island);         }       }     }   }   int main() {     cin >> n;     cin >> pi.first >> pi.second;     cin >> pf.first >> pf.second;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> grid[i][j];       }     }     dfs(pi.first, pi.second, &ili);     if (vis[pf.first][pf.second]) {       cout <<  0  << endl;     } else {       dfs(pf.first, pf.second, &ilf);       int less = (int)INFINITY;       for (int i = 0; i < (int)ili.size(); i++) {         for (int j = 0; j < (int)ilf.size(); j++) {           cost = pow((ili[i].first - ilf[j].first), 2) +                  pow((ili[i].second - ilf[j].second), 2);           if (cost < less && cost) {             less = cost;           }         }       }       cout << less << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000007;   const long long LLINF = 1000000000000000007LL;   const double EPS = 1e-9;   const int sz = 200100;   int ar[600][600];   int p[600][600];   int suf_sum_2d(int i1, int j1, int i2, int j2) {     return p[i2 + 1][j2 + 1] - p[i2 + 1][j1] - p[i1][j2 + 1] + p[i1][j1];   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         cin >> ar[i][j];         p[i + 1][j + 1] = p[i][j + 1] + p[i + 1][j] - p[i][j] + ar[i][j];       }     }     int mx = -INF;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         int r = min(min(i + 1, j + 1), min(n - i, m - j));         int res = ar[i][j];         for (int k = 1; k < r; ++k) {           res =               suf_sum_2d(i - k, j - k, i + k, j + k) - res - ar[i - k + 1][j - k];           mx = max(mx, res);         }       }     }     cout << mx;   }


#include <bits/stdc++.h>   using namespace std;   unordered_map<int, int> mp;   bool f(int mid) {     if (mp.size() < mid) return false;     int a = 0, b = 0;     bool fl = 0;     for (auto it = mp.begin(); it != mp.end(); it++) {       if (it->second > mid) {         a = mid;         if (fl == 0) {           b++;         } else {           b += 2;           fl = 0;         }       } else if (it->second == mid) {         if (a == 0) {           a = mid;           fl = 1;         } else           b++;       } else if (it->second < mid)         b++;     }     return a >= mid && b >= mid;   }   int main() {     int test;     cin >> test;     for (int t = 1; t <= test; t++) {       int n, ans = 0, imax = 0;       cin >> n;       mp.clear();       for (int i = 0; i < n; i++) {         int temp;         cin >> temp;         mp[temp]++;         imax = max(imax, mp[temp]);       }       int low = 1, high = imax;       while (low <= high) {         int mid = low + (high - low) / 2;         if (f(mid)) {           ans = mid;           low = mid + 1;         } else           high = mid - 1;       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int INT_MAX_VAL = (int)0x3F3F3F3F;   int INT_MIN_VAL = (int)-0x3F3F3F3F;   long long LONG_MAX_VAL = (long long)0x3F3F3F3F3F3F3F3F;   long long LONG_MIN_VAL = (long long)-0x3F3F3F3F3F3F3F3F;   vector<long long> G[5005];   long long n;   int W;   int cnt[5005];   int dp[2][5005][5005];   int dfs2(int v, int p) {     if (G[v].size() == 1) {       cnt[v] = 1;       return 1;     }     int res = 0;     for (auto &w : G[v])       if (w != p) res += dfs2(w, v);     cnt[v] = res;     return res;   }   void dfs(int v, int p) {     dp[1][v][0] = 0;     if (G[v].size() == 1) {       dp[0][v][1] = 0;       return;     }     dp[0][v][0] = 0;     dp[1][v][0] = 0;     for (auto &w : G[v]) {       if (w == p) continue;       dfs(w, v);       for (int i = W; i >= 0; --i) {         int d0 = INT_MAX_VAL;         int d1 = INT_MAX_VAL;         for (int j = min(i, cnt[w]); j >= 0; --j) {           d0 = min(d0, dp[0][w][j] + dp[0][v][i - j]);           d0 = min(d0, dp[1][w][j] + dp[0][v][i - j] + 1);           d1 = min(d1, dp[1][w][j] + dp[1][v][i - j]);           d1 = min(d1, dp[0][w][j] + dp[1][v][i - j] + 1);         }         dp[0][v][i] = d0;         dp[1][v][i] = d1;       }     }   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     memset(dp, 0x3F, 2 * 5005 * 5005 * sizeof(int));     cin >> n;     for (long long(i) = 0LL; (i) < (n - 1); (i)++) {       long long a, b;       cin >> a >> b;       --a, --b;       G[a].push_back(b);       G[b].push_back(a);     }     if (n == 2) {       cout << 1 <<   n ;       return 0;     }     int i = 0;     while (G[i].size() == 1) ++i;     W = dfs2(i, -1) / 2;     dfs(i, -1);     cout << dp[0][i][W] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   struct ban {     long long s, a, b;   };   bool operator<(const ban& a, const ban& b) { return (a.a - a.b) > (b.a - b.b); }   int n;   long long s;   ban a[N];   long long sum;   long long ans;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> s;     for (int i = 0; i < n; ++i) {       cin >> a[i].s >> a[i].a >> a[i].b;       sum += a[i].s;     }     sort(a, a + n);     long long t = 0;     for (int i = 0; i < n; ++i) {       if (a[i].a - a[i].b > 0) t += a[i].s;     }     long long yans = 0;     for (int i = 0; i < n; ++i) {       if (a[i].a - a[i].b > 0) yans += (a[i].a * a[i].s);     }     long long tt = (((sum / s) + !!(sum % s)) - ((t / s) + !!(t % s))) * s;     for (int i = n - 1; i >= 0; --i) {       if (a[i].a - a[i].b <= 0) {         if (tt > a[i].s) {           tt -= a[i].s;           yans += (a[i].b * a[i].s);         } else {           yans += (a[i].b * tt);           yans += (a[i].a * (a[i].s - tt));           tt = 0;         }       }     }     ans = yans;     t = 0;     for (int i = n - 1; i >= 0; --i) {       if (a[i].b - a[i].a > 0) t += a[i].s;     }     yans = 0;     for (int i = n - 1; i >= 0; --i) {       if (a[i].b - a[i].a > 0) yans += (a[i].b * a[i].s);     }     tt = (((sum / s) + !!(sum % s)) - ((t / s) + !!(t % s))) * s;     for (int i = 0; i < n; ++i) {       if (a[i].b - a[i].a <= 0) {         if (tt > a[i].s) {           tt -= a[i].s;           yans += (a[i].a * a[i].s);         } else {           yans += (a[i].a * tt);           yans += (a[i].b * (a[i].s - tt));           tt = 0;         }       }     }     ans = max(ans, yans);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace ::std;   int NEGINF = -1e9;   int main() {     ifstream fin( input.txt );     int n, l, r;     cin >> n >> l >> r;     vector<int> a(n), b(n), p(n), c(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<int> ppos(n);     for (int i = 0; i < n; i++) {       cin >> p[i];       ppos[p[i] - 1] = i;     }     int loc, csmall = NEGINF;     for (int i = 0; i < n; i++) {       loc = ppos[i];       if (csmall + 1 + a[loc] < l) {         b[loc] = l;         csmall = b[loc] - a[loc];       } else if (csmall + 1 + a[loc] > r) {         cout <<  -1 n ;         return 0;       } else {         csmall++;         b[loc] = a[loc] + csmall;       }     }     for (int i = 0; i < n; i++) {       cout << b[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T abs(T x) {     return x > 0 ? x : -x;   }   char a[2005][2005];   bool used[2005][2005];   queue<pair<int, int> > q;   int nu() {     puts( Not unique );     return 0;   }   bool impasse(int ic, int jc) {     int cnt = 0;     cnt += (a[ic][jc - 1] ==  . );     cnt += (a[ic][jc + 1] ==  . );     cnt += (a[ic - 1][jc] ==  . );     cnt += (a[ic + 1][jc] ==  . );     return (cnt < 2);   }   void add(int ic, int jc) {     if (impasse(ic - 1, jc) && !used[ic - 1][jc]) {       q.push(make_pair(ic - 1, jc));       used[ic - 1][jc] = true;     }     if (impasse(ic + 1, jc) && !used[ic + 1][jc]) {       q.push(make_pair(ic + 1, jc));       used[ic + 1][jc] = true;     }     if (impasse(ic, jc - 1) && !used[ic][jc - 1]) {       q.push(make_pair(ic, jc - 1));       used[ic][jc - 1] = true;     }     if (impasse(ic, jc + 1) && !used[ic][jc + 1]) {       q.push(make_pair(ic, jc + 1));       used[ic][jc + 1] = true;     }   }   int main() {     int n, m;     scanf( %d %d n , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %c , &a[i][j]);       }       scanf( %c , &a[0][0]);     }     for (int i = 0; i <= n + 1; i++) {       a[i][0] =  * ;       a[i][m + 1] =  * ;     }     for (int i = 0; i <= m + 1; i++) {       a[0][i] =  * ;       a[n + 1][i] =  * ;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if (a[i][j] ==  * ) continue;         int cnt = 0;         cnt += (a[i][j - 1] ==  . );         cnt += (a[i][j + 1] ==  . );         cnt += (a[i - 1][j] ==  . );         cnt += (a[i + 1][j] ==  . );         if (cnt == 0) return nu();         if (cnt == 1) {           used[i][j] = true;           q.push(make_pair(i, j));         }       }     while (!q.empty()) {       int ic = q.front().first;       int jc = q.front().second;       q.pop();       if (a[ic][jc] !=  . ) continue;       if (a[ic + 1][jc] ==  . ) {         a[ic][jc] =  ^ ;         a[ic + 1][jc] =  v ;         add(ic + 1, jc);       }       if (a[ic - 1][jc] ==  . ) {         a[ic][jc] =  v ;         a[ic - 1][jc] =  ^ ;         add(ic - 1, jc);       }       if (a[ic][jc + 1] ==  . ) {         a[ic][jc] =  < ;         a[ic][jc + 1] =  > ;         add(ic, jc + 1);       }       if (a[ic][jc - 1] ==  . ) {         a[ic][jc] =  > ;         a[ic][jc - 1] =  < ;         add(ic, jc - 1);       }       if (a[ic][jc] ==  . ) return nu();       add(ic, jc);     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] ==  . ) return nu();     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         putchar(a[i][j]);       }       putchar(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long v[100100];   long long h[100100];   int main() {     long long n, m;     cin >> n >> m;     long long vidx = 0;     for (; vidx < n; ++vidx) {       long long x;       cin >> x;       v[vidx] = x;     }     long long hidx = 0;     for (long long loop = 0; loop < m; ++loop) {       long long x1, x2, y;       cin >> x1 >> x2 >> y;       if (x1 > 1) continue;       h[hidx] = x2;       ++hidx;     }     sort(v, v + vidx);     sort(h, h + hidx);     v[vidx] = 1e9;     ++vidx;     long long res = n + m;     for (long long idx = 0; idx < vidx; ++idx) {       long long lim = v[idx];       auto it = lower_bound(h, h + hidx, lim) - h;       res = min(res, hidx - it + idx);     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long przodek(long long n, long long m) {     if (n == m) return n;     if (n > m) return przodek(n / 2, m);     return przodek(n, m / 2);   }   int main() {     long long q;     cin >> q;     set<pair<long long, long long> > path_fee;     for (int i = 0; i < q; i++) {       long long op, v, u, w;       cin >> op;       if (op == 1) {         cin >> u >> v >> w;         long long prz = przodek(u, v);         while (u != prz) {           pair<long long, long long> act_fee = *path_fee.lower_bound({u, 0});           if (act_fee.first == u)             path_fee.erase(act_fee);           else             act_fee.second = 0;           path_fee.insert({u, act_fee.second + w});           u /= 2;         }         while (v != prz) {           pair<long long, long long> act_fee = *path_fee.lower_bound({v, 0});           if (act_fee.first == v)             path_fee.erase(act_fee);           else             act_fee.second = 0;           path_fee.insert({v, act_fee.second + w});           v /= 2;         }         continue;       }       cin >> u >> v;       long long cost = 0;       long long prz = przodek(u, v);       while (u != prz) {         pair<long long, long long> act_fee = *path_fee.lower_bound({u, 0});         if (act_fee.first == u) {           cost += act_fee.second;         }         u /= 2;       }       while (v != prz) {         pair<long long, long long> act_fee = *path_fee.lower_bound({v, 0});         if (act_fee.first == v) {           cost += act_fee.second;         }         v /= 2;       }       cout << cost << endl;     }   }


#include <bits/stdc++.h>   int arr[1100000];   using namespace std;   int main() {     memset(arr, 0, sizeof arr);     int n;     cin >> n;     int x = 1, pre = 1;     for (int i = 1; i <= n; i++) {       int c;       cin >> c;       arr[x] = i;       x += c;     }     x = arr[1];     for (int i = 1; i <= 1000000; i++) {       if (!arr[i])         arr[i] = x;       else         x = arr[i];     }     int q;     cin >> q;     while (q--) {       cin >> x;       cout << arr[x] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, x, l, r, m;   int main() {     cin >> n;     int endtime = 0;     while (n--) {       cin >> x;       endtime += x;     }     cin >> m;     while (m--) {       cin >> l >> r;       if (endtime <= l) {         cout << l << endl;         return 0;       } else if (endtime > r) {         continue;       } else {         cout << endtime << endl;         return 0;       }     }     cout <<  -1  << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   const int N6 = 1e6 + 6;   const int N9 = 1e9 + 7;   const long long INFLL = (long long)1e18 + 100;   const int INF = 1e9 + 100;   const long long mod = 1e9 + 9;   const long long P = 31;   int n, k, cur, ans;   std::vector<int> g[1111];   map<pair<int, int>, bool> cnt;   void solve() {     cin >> n >> k;     int cur;     for (int i = 1; i <= n; ++i) {       cur = 0;       for (int j = 1; j <= n; ++j) {         if (!cnt.count(make_pair(i, j)) && i != j) {           ++cur;           cnt[make_pair(i, j)];           cnt[make_pair(j, i)];           g[i].push_back(j);           ++ans;         }         if (cur == k) {           break;         }       }       if (cur != k) {         cout << -1;         exit(0);       }     }     cout << ans <<   n ;     for (int i = 1; i <= n; ++i) {       for (int j = 0; j < g[i].size(); ++j) {         cout << i <<     << g[i][j] <<   n ;       }     }   }   int main() {     int tt = 1;     while (tt--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int a = 0;     char c = getchar();     bool f = 0;     while (!isdigit(c)) {       if (c ==  - ) f = 1;       c = getchar();     }     while (isdigit(c)) {       a = (a << 3) + (a << 1) + (c ^  0 );       c = getchar();     }     return f ? -a : a;   }   int point[51][3], N;   void solve(int l, int r, int dir) {     if (l == r)       printf( (%01d*abs(((1-abs((t-%d)))+abs((abs((t-%d))-1))))) ,              point[l][dir] / 2, l, l);     else {       putchar( ( );       solve(l, l + r >> 1, dir);       putchar( + );       solve((l + r >> 1) + 1, r, dir);       putchar( ) );     }   }   int main() {     N = read();     for (int i = 1; i <= N; ++i) {       point[i][0] = read();       point[i][1] = read();       point[i][2] = read();     }     solve(1, N, 0);     putchar(  n );     solve(1, N, 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 100111, inf = 1000111222;   const int max_lev = 18;   int n, m, q, parent[max_lev][max_n], best_h[max_n], used[max_n], h[max_n];   int real_p[max_lev][max_n];   int tin[max_n], tout[max_n], cur;   vector<int> g[max_n];   void dfs(int v, int p) {     tin[v] = ++cur;     parent[0][v] = p;     real_p[0][v] = p;     for (int to : g[v]) {       if (to == p) {         continue;       }       if (tin[to]) {         if (tin[to] < tin[parent[0][v]]) {           parent[0][v] = to;         }       } else {         dfs(to, v);         if (tin[parent[0][to]] < tin[parent[0][v]]) {           parent[0][v] = parent[0][to];         }       }     }     tout[v] = cur;   }   void get_all_p(int parent[max_lev][max_n]) {     for (int lev = 1; lev < max_lev; ++lev) {       for (int i = 0; i < n; ++i) {         parent[lev][i] = parent[lev - 1][parent[lev - 1][i]];       }     }   }   bool is_ancestor(int v1, int v2) {     return tin[v1] <= tin[v2] && tout[v1] >= tout[v2];   }   int lca(int v1, int v2) {     if (is_ancestor(v1, v2)) {       return v1;     }     for (int i = max_lev - 1; i >= 0; --i) {       if (!is_ancestor(real_p[i][v1], v2)) {         v1 = real_p[i][v1];       }     }     return real_p[0][v1];   }   int get_d(int v, int up) {     if (v == up) {       return 0;     }     int res = 1;     for (int i = max_lev - 1; i >= 0; --i) {       if (!is_ancestor(parent[i][v], up)) {         res += 1 << i;         v = parent[i][v];       }     }     return res;   }   int main() {     scanf( %d%d%d , &n, &m, &q);     while (m--) {       int u, v;       scanf( %d%d , &u, &v);       --u;       --v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(0, 0);     get_all_p(parent);     get_all_p(real_p);     while (q--) {       int u, v;       scanf( %d%d , &u, &v);       --u;       --v;       int lc = lca(u, v);       int ans = get_d(u, lc) + get_d(v, lc);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 6e5 + 8;   const int T = 2e5 + 2;   int n, b[N], t[N];   template <typename _Tp>   inline void IN(_Tp& x) {     char ch;     bool flag = 0;     x = 0;     while (ch = getchar(), !isdigit(ch))       if (ch ==  - ) flag = 1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     if (flag) x = -x;   }   struct Segment_Tree {     long long sum[N << 2];     void update(int x, int l, int r, int pos, int val) {       if (l == r) {         sum[x] += val;         return void();       }       (pos <= ((l + r) >> 1))           ? update(((x) << 1), l, ((l + r) >> 1), pos, val)           : update(((x) << 1 | 1), ((l + r) >> 1) + 1, r, pos, val);       sum[x] = max(sum[((x) << 1)], sum[((x) << 1 | 1)]);     }   } Tree;   int main() {     IN(n);     for (int i = 1; i <= n; ++i) IN(b[i]), t[i] = b[i] - i + T;     for (int i = 1; i <= n; ++i) Tree.update(1, 1, N - 1, t[i], b[i]);     printf( %lld n , Tree.sum[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> mp;   vector<string> vr;   int n, m;   int a[105][105], b[105][105];   int xx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};   int yy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};   bool check(int i, int j) {     if (i >= 0 && i < n && j >= 0 && j < m && a[i][j] != -2 && a[i][j] != -6)       return true;     return false;   }   bool Check(int i, int j) {     if (i >= 0 && i < n && j >= 0 && j < m && b[i][j] == -6) return true;     return false;   }   void dfs(int x, int y) {     int X, Y, i;     for (i = 0; i < 8; i++) {       X = x + xx[i];       Y = y + yy[i];       if (check(X, Y)) b[X][Y]++;     }   }   int main() {     char ma[105][105];     while (~scanf( %d%d , &n, &m)) {       int f = 0;       memset(a, 0, sizeof(a));       memset(b, 0, sizeof(b));       for (int i = 0; i < n; i++) {         cin >> ma[i];       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           a[i][j] = ma[i][j] -  0 ;         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (a[i][j] == -6) {             b[i][j] = -6;             dfs(i, j);           }           if (a[i][j] == -2) b[i][j] = -2;         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (b[i][j] == -2) {             for (int k = 0; k < 8; k++) {               int X = xx[k] + i;               int Y = yy[k] + j;               if (Check(X, Y)) {                 f = 1;                 break;               }             }           }           if (a[i][j] != b[i][j]) {             f = 1;             break;           }           if (f == 1) break;         }         if (f == 1) break;       }       if (f)         printf( NO n );       else         printf( YES n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int a, b, c, d;       cin >> a >> b >> c >> d;       string w, x, y, z, k =  Ya , l =  Tidak ;       if (a == 0 && d == 0) {         if (b % 2 == 1) {           cout <<  Tidak Ya Tidak Tidak n ;         } else {           cout <<  Tidak Tidak Ya Tidak n ;         }       } else if (b == 0 && c == 0) {         if (a % 2 == 1) {           cout <<  Ya Tidak Tidak Tidak n ;         } else {           cout <<  Tidak Tidak Tidak Ya n ;         }       } else {         if ((a + b) % 2 == 1) {           cout <<  Ya Ya Tidak Tidak n ;         } else {           cout <<  Tidak Tidak Ya Ya n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   pair<long long, int> arr[N];   long long t[4 * N] = {};   inline void update(int v, int vl, int vr, int pos, long long val) {     if (vl == vr) {       t[v] = val;       return;     }     int vm = vl + (vr - vl) / 2;     if (pos <= vm)       update(2 * v + 1, vl, vm, pos, val);     else       update(2 * v + 2, vm + 1, vr, pos, val);     t[v] = max(t[2 * v + 1], t[2 * v + 2]);   }   inline long long get(int v, int vl, int vr, int l, int r) {     if (r < vl || vr < l) return 0;     if (l <= vl && vr <= r) return t[v];     int vm = vl + (vr - vl) / 2;     long long ql = get(2 * v + 1, vl, vm, l, r);     long long qr = get(2 * v + 2, vm + 1, vr, l, r);     return max(ql, qr);   }   inline bool c(pair<long long, int> x, pair<long long, int> y) {     if (x.first != y.first)       return x.first < y.first;     else       return x.second > y.second;   }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       long long r, h;       cin >> r >> h;       arr[i] = {1ll * r * r * h, i};     }     sort(arr, arr + n, c);     for (int i = 0; i < n; i++) {       long long res = get(0, 0, n - 1, 0, max(0, arr[i].second - 1));       update(0, 0, n - 1, arr[i].second, res + arr[i].first);     }     cout << fixed << setprecision(6) << 3.14159265 * get(0, 0, n - 1, 0, n - 1)          <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   int n;   char s[20];   int ans[300100];   vector<int> dep[300100];   int G[300100][26], siz[300100], son[300100];   vector<int *> a;   void dfs(int o, int depth) {     dep[depth].push_back(o);     siz[o] = 1;     for (int i = 0; i < 26; ++i) {       if (G[o][i] == -1) continue;       dfs(G[o][i], depth + 1);       if (son[o] == -1 || siz[son[o]] < siz[G[o][i]]) {         son[o] = G[o][i];       }       siz[o] += siz[G[o][i]];     }   }   int DFS(int u, int v) {     int ret = 0;     for (int i = 0; i < 26; ++i) {       if (G[u][i] == -1) continue;       if (G[v][i] == -1) {         G[v][i] = G[u][i];         ret += siz[G[u][i]];         a.push_back(&G[v][i]);       } else {         ret += DFS(G[u][i], G[v][i]);       }     }     return ret;   }   int main() {     memset(G, -1, sizeof(G));     memset(son, -1, sizeof(son));     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d%s , &u, &v, s);       G[u][s[0] -  a ] = v;     }     dfs(1, 0);     int tmp = 0;     for (int i = 0; i <= n; ++i) {       a.clear();       ans[i] += tmp;       for (auto &j : dep[i]) {         if (son[j] != -1) ans[i + 1] += siz[son[j]] - 1;         for (int k = 0; k < 26; ++k) {           if (G[j][k] == -1 || son[j] == G[j][k]) continue;           ans[i + 1] += DFS(G[j][k], son[j]);         }       }       for (auto &i : a) {         *i = -1;       }       tmp += (int)dep[i].size();     }     tmp = 1;     for (int i = 2; i <= n; ++i) {       if (ans[tmp] > ans[i]) {         tmp = i;       }     }     printf( %d n%d n , ans[tmp], tmp);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string cad;     cin >> n >> cad;     int res = 0;     for (int i = 0; i < n; ++i) {       if (cad[i] ==  0 ) {         res++;         break;       } else {         res++;       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s, ss;   int m, l, r, k, i, j, n, p;   int main() {     cin >> s;     cin >> m;     ss = s;     for (i = 1; i <= m; i++) {       cin >> l >> r >> k;       r--;       l--;       for (j = l; j <= r; j++) {         p = r - l + 1;         ss[j] = s[l + ((j - l - k) % p + p) % p];       }       s = ss;     }     cout << ss << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2000000000;   const int MAX = 10000000;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     while (cin >> n) {       map<string, int> m;       for (int i = 0; i < n; i++) {         string s;         cin >> s;         m[s]++;       }       int mx = 0;       for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)         mx = max(mx, it->second);       cout << mx <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, i = 0, last = 0, current = 0;   int main() {     int x, y, z = 0, a = 0, c = 1;     long long b;     cin >> x;     for (int i = 0; i < x; i++) {       cin >> y >> z;       a = z * 2;       c = y * 2;       if (a > c) {         if (z > c)           cout << z * z << endl;         else           cout << c * c << endl;       } else {         if (y > a)           cout << y * y << endl;         else           cout << a * a << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int testnum;   struct Point3 {     double x, y, z;     Point3(double _x = 0, double _y = 0, double _z = 0) {       x = _x;       y = _y;       z = _z;     }     bool operator==(Point3 other) {       return abs(x - other.x) < 1e-9 && abs(y - other.y) < 1e-9 &&              abs(z - other.z) < 1e-9;     }     bool operator!=(Point3 other) {       return abs(x - other.x) > 1e-9 || abs(y - other.y) > 1e-9 ||              abs(z - other.z) > 1e-9;     }     double length() { return sqrt(x * x + y * y + z * z); }     Point3 operator*(double k) { return Point3(k * x, k * y, k * z); }     Point3 normalize() { return (*this) * (1 / length()); }     Point3 operator+(Point3 other) {       return Point3(x + other.x, y + other.y, z + other.z);     }     Point3 operator-(Point3 other) {       return Point3(x - other.x, y - other.y, z - other.z);     }     double dist(Point3 other) { return (*this - other).length(); }     double operator^(Point3 other) {       return x * other.x + y * other.y + z * other.z;     }     Point3 operator*(Point3 other) {       return Point3(y * other.z - z * other.y, z * other.x - x * other.z,                     x * other.y - y * other.x);     }     void display() { printf( Point3 (%lf , %lf , %lf) n , x, y, z); }   };   Point3 operator*(double k, Point3 p) {     return Point3(k * p.x, k * p.y, k * p.z);   }   struct Point2 {     double x, y;     Point2(double _x = 0, double _y = 0) {       x = _x;       y = _y;     }     double length() { return sqrt(x * x + y * y); }     Point2 operator*(double k) { return Point2(k * x, k * y); }     Point2 normalize() { return (*this) * (1 / length()); }     Point2 operator+(Point2 other) { return Point2(x + other.x, y + other.y); }     Point2 operator-(Point2 other) { return Point2(x - other.x, y - other.y); }     double dist(Point2 other) { return (*this - other).length(); }     double operator^(Point2 other) { return x * other.x + y * other.y; }     double operator*(Point2 other) { return x * other.y - y * other.x; }     void display() { printf( Point2 (%lf , %lf) n , x, y); }   };   Point2 operator*(double k, Point2 p) { return Point2(k * p.x, k * p.y); }   struct Plane {     double a, b, c, d;     Point3 A, B, C;     Point3 N, O, X, Y;     Plane() {}     Plane(Point3 _AA, Point3 _BB, Point3 _CC) {       A = _AA;       B = _BB;       C = _CC;       Point3 normal = (B - A) * (C - A);       N = normal.normalize();       a = N.x;       b = N.y;       c = N.z;       d = -(A ^ N);       O = Point3(-d * a, -d * b, -d * c);       if (A != O)         X = (A - O).normalize();       else         X = (B - O).normalize();       Y = normal * X;     }     Plane(double _a, double _b, double _c, double _d) {       double len = sqrt(_a * _a + _b * _b + _c * _c);       a = _a / len;       b = _b / len;       c = _c / len;       d = _d / len;       N = Point3(a, b, c);       if (a != 0) {         A = Point3(-(d + 0 * b + 0 * c) / a, 0, 0);         B = Point3(-(d + 1 * b + 0 * c) / a, 1, 0);         C = Point3(-(d + 0 * b + 1 * c) / a, 0, 1);       } else if (b != 0) {         A = Point3(0, -(d + 0 * a + 0 * c) / b, 0);         B = Point3(1, -(d + 1 * a + 0 * c) / b, 0);         C = Point3(0, -(d + 0 * a + 1 * c) / b, 1);       } else {         A = Point3(0, 0, -(d + 0 * a + 0 * b) / c);         B = Point3(1, 0, -(d + 1 * a + 0 * b) / c);         C = Point3(0, 1, -(d + 0 * a + 1 * b) / c);       }       O = Point3(-d * a, -d * b, -d * c);       if (A != O)         X = (A - O).normalize();       else         X = (B - O).normalize();       Y = N * X;     }     Point3 project(Point3 P) { return (P - (P ^ N) * N) + O; }     Point2 project2D(Point3 P) {       P = project(P);       return Point2(P ^ X, P ^ Y);     }     double distance(Point3 p) { return abs(a * p.x + b * p.y + c * p.z + d); }     void display() {       printf( Plane (%lf , %lf , %lf , %lf) n , a, b, c, d);       printf( A :  );       A.display();       printf( B :  );       B.display();       printf( C :  );       C.display();       printf( N :  );       N.display();       printf( O :  );       O.display();       printf( X :  );       X.display();       printf( Y :  );       Y.display();     }   };   void preprocess() {}   int n, m;   vector<Point3> p3;   vector<Point2> p2;   vector<Plane> plns;   double eval(double x, double y) {     double ans = 0;     double dx, dy;     for (int i = 0; i < n; i++) {       dx = x - p2[i].x;       dy = y - p2[i].y;       ans = max(ans, dx * dx + dy * dy);     }     return ans;   }   pair<double, double> doTernaryY(double x) {     double lo = 1e4;     double hi = 0;     for (int i = 0; i < n; i++) {       lo = min(lo, p2[i].y);       hi = max(hi, p2[i].y);     }     for (int iter = 0; iter < 35; iter++) {       double y1 = (lo + lo + hi) / 3;       double y2 = (lo + hi + hi) / 3;       double val1 = eval(x, y1);       double val2 = eval(x, y2);       if (val1 < val2)         hi = y2;       else         lo = y1;     }     double ans = (lo + hi) / 2;     return make_pair(ans, eval(x, ans));   }   pair<double, double> doTernaryX() {     double lo = 1e4;     double hi = 0;     for (int i = 0; i < n; i++) {       lo = min(lo, p2[i].x);       hi = max(hi, p2[i].x);     }     for (int iter = 0; iter < 35; iter++) {       double x1 = (lo + lo + hi) / 3;       double x2 = (lo + hi + hi) / 3;       double val1 = doTernaryY(x1).second;       double val2 = doTernaryY(x2).second;       if (val1 < val2)         hi = x2;       else         lo = x1;     }     double ans = (lo + hi) / 2;     return make_pair(ans, doTernaryY(ans).first);   }   void solve() {     p2.resize(n);     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) p2[j] = plns[i].project2D(p3[j]);       pair<double, double> centre = doTernaryX();       double rad = sqrt(eval(centre.first, centre.second));       printf( %.9lf n , rad);     }   }   bool input() {     scanf( %d , &n);     scanf( %d , &m);     p3.resize(n);     plns.resize(m);     int a, b, c;     for (int i = 0; i < n; i++) {       scanf( %d , &a);       scanf( %d , &b);       scanf( %d , &c);       p3[i] = Point3(a, b, c);     }     for (int i = 0; i < m; i++) {       scanf( %d , &a);       scanf( %d , &b);       scanf( %d , &c);       plns[i] = Plane(a, b, c, 0);     }     return true;   }   int main() {     preprocess();     int T = 1;     for (testnum = 1; testnum <= T; testnum++) {       if (!input()) break;       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, a[1000005];   multiset<long long int> st;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (long long int i = 1; i <= n; i++) cin >> a[i];     long long int ans = 0;     for (long long int i = 1; i <= n; i++) {       st.insert(a[i]);       auto it = st.begin();       if (*it < a[i]) {         ans += a[i] - *it;         st.erase(it);         st.insert(a[i]);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10;   int main() {     ios_base ::sync_with_stdio(false), cout.tie(0), cin.tie(0);     int n, cnt = 0;     cin >> n;     int sv = n;     while (n--) {       int x, y;       cin >> x >> y;       if (x < 0) ++cnt;     }     cout << (min(cnt, sv - cnt) <= 1 ?  Yes n  :  No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200010];   int main() {     long long n, k;     cin >> n >> k;     n -= k * (k + 1) / 2;     if (n < 0) {       cout <<  NO  << endl;       return 0;     }     for (int i = 0; i < k; i++) {       a[i] = i + 1 + n / k + (i >= (k - n % k));     }     bool ok = 1;     for (int i = 1; i < k; i++) {       if (a[i] > a[i - 1] * 2) {         if (i == k - 1) ok = 0;         a[k - 1]++;         a[i]--;       }     }     if (ok) {       cout <<  YES  << endl;       for (int i = 0; i < k; i++) cout << a[i] << endl;     } else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m);     int arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     int s1 = count(arr, arr + n, 1);     int s2 = count(arr, arr + n, -1);     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d %d , &a, &b);       if ((b - a + 1) % 2 != 0) {         printf( %d n , 0);       } else {         int c = (b - a + 1) / 2;         if (s1 >= c && s2 >= c) {           printf( %d n , 1);         } else {           printf( %d n , 0);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   map<long long, long long> mp;   int main() {     long long k, i;     cin >> k;     string s;     cin >> s;     long long ans = 0, cnt = 0;     long long n = s.length();     mp[0] = 1;     for (i = 0; i < n; i++) {       if (s[i] ==  1 ) {         cnt++;       }       if (cnt >= k) {         ans += mp[cnt - k];       }       mp[cnt]++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = (long long)1e5 + 5;   long long a[N];   inline void solve() {     long long n;     cin >> n;     queue<pair<long long, long long> > negative, positive;     long long pos_sum = 0;     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       if (a[i] == 0) {         continue;       }       if (a[i] > 0) {         pos_sum += a[i];         positive.push({i, a[i]});       } else {         negative.push({i, a[i]});       }     }     while (!positive.empty()) {       long long p = positive.front().first;       long long x = positive.front().second;       while (!negative.empty() && negative.front().first < p) {         negative.pop();       }       if (negative.empty()) {         break;       }       long long relax = min(x, -negative.front().second);       positive.front().second -= relax;       pos_sum -= relax;       if (positive.front().second == 0) {         positive.pop();       }       if (relax >= -negative.front().second) {         negative.pop();       } else {         negative.front().second += relax;       }     }     cout << pos_sum << endl;   }   signed main() {     long long Q;     cin >> Q;     while (Q--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int lst[200005], nxt[400005], to[400005], tol;   int sz[200005], f[200005];   long long dp[200005];   int vis[200005];   int qu[200005];   int N, K;   void adde(int u, int v) {     nxt[tol] = lst[u];     to[tol] = v;     lst[u] = tol;     ++tol;   }   void solve() {     cin >> N >> K;     memset(lst, 0xff, sizeof(lst));     tol = 0;     memset(sz, 0, sizeof(sz));     for (int i = 0; i < (K * 2); ++i) {       int u;       scanf( %d , &u);       sz[u] = 1;     }     for (int i = 0; i < (N - 1); ++i) {       int u, v;       scanf( %d%d , &u, &v);       adde(u, v);       adde(v, u);     }     memset(vis, 0, sizeof(vis));     memset(dp, 0, sizeof(dp));     int tail = 0;     f[1] = 0;     vis[1] = 1;     qu[tail++] = 1;     for (int i = 0; i < (tail); ++i) {       int u = qu[i];       for (int p = lst[u]; p != -1; p = nxt[p]) {         int v = to[p];         if (!vis[v]) {           vis[v] = 1;           f[v] = u;           qu[tail++] = v;         }       }     }     for (int i = tail - 1; i >= 0; --i) {       int u = qu[i];       dp[u] += min(sz[u], 2 * K - sz[u]);       sz[f[u]] += sz[u];       dp[f[u]] += dp[u];     }     cout << dp[1] << endl;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, ans, now;   int v[505][505];   int main() {     scanf( %d%d , &n, &k);     now = n * n;     for (int i = n; i >= 1; i--)       for (int h = n; h >= k; h--) v[i][h] = now--;     now = 1;     for (int i = 1; i <= n; i++)       for (int h = 1; h < k; h++) v[i][h] = now++;     for (int i = 1; i <= n; i++) ans += v[i][k];     printf( %d n , ans);     for (int i = 1; i <= n; i++) {       for (int h = 1; h <= n; h++) cout << v[i][h] <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[31][31], used[31][31];   long long n, m;   vector<pair<int, int> > zz = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   int check(long long x, long long y) {     if (x >= 1 && x <= n && y >= 1 && y <= m && used[x][y] == 0) return 1;     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     for (; t > 0; t--) {       long long ans = 0;       cin >> n >> m;       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) {           cin >> a[i][j];           used[i][j] = 0;         }       if (n == 1 && m == 1) {         cout << 0 <<   n ;         continue;       }       queue<int> x, y, dep;       int k = 0;       x.push(1);       y.push(1);       dep.push(1);       x.push(n);       y.push(m);       dep.push(1);       used[1][1] = 1;       used[n][m] = 1;       long long z = n + m - 1;       z /= 2;       while (k + 1 <= z) {         k++;         long long ed = 0;         long long nul = 0;         while (!dep.empty() && dep.front() == k) {           long long xx = x.front();           x.pop();           long long yy = y.front();           y.pop();           long long d = dep.front();           dep.pop();           if (a[xx][yy] == 1)             ed++;           else             nul++;           for (int i = 0; i < 4; i++) {             xx += zz[i].first;             yy += zz[i].second;             if (check(xx, yy)) {               x.push(xx);               y.push(yy);               dep.push(d + 1);               used[xx][yy] = 1;             }             xx -= zz[i].first;             yy -= zz[i].second;           }         }         ans += min(ed, nul);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[100010];   inline int read() {     char c = getchar();     int x = 0, flag = 1;     while (!isdigit(c)) {       if (c ==  - ) flag = -1;       c = getchar();     }     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     return x * flag;   }   int main() {     int n = read(), k = read(), lst = 1, ans = 0;     for (int i = 1; i <= n; i++) {       a[i] = read();       if (a[i] == a[i - 1]) lst = i;       ans = max(ans, i - lst + 1);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   struct lazy_seg {    public:     vector<T> st, lazy;     lazy_seg(int n) { st.resize(4 * n + 1, 0), lazy.resize(4 * n + 1, 0); }     lazy_seg() = default;     void push(int l, int r, int curr) {       if (lazy[curr] != 0) {         st[curr] += lazy[curr] * (r - l + 1);         if (l != r)           lazy[2 * curr] += lazy[curr], lazy[2 * curr + 1] += lazy[curr];         lazy[curr] = 0;       }     }     void pull(int l, int r, int curr) {       push(l, r, curr);       int mid = l + (r - l) / 2;       if (l != r) push(l, mid, 2 * curr), push(mid + 1, r, 2 * curr + 1);       st[curr] = (st[2 * curr] + st[2 * curr + 1]);     }     void upd(int x, int y, T v, int l, int r, int curr) {       int mid = (l + r) / 2;       push(l, r, curr);       if (l == x && r == y) {         lazy[curr] += v;         push(l, r, curr);         return;       }       if (y <= mid)         upd(x, y, v, l, mid, 2 * curr);       else if (x > mid)         upd(x, y, v, mid + 1, r, 2 * curr + 1);       else         upd(x, mid, v, l, mid, 2 * curr),             upd(mid + 1, y, v, mid + 1, r, 2 * curr + 1);       pull(l, r, curr);     }     T qry(int x, int y, int l, int r, int curr) {       int mid = (l + r) / 2;       push(l, r, curr);       if (l == x && r == y) return st[curr];       if (y <= mid)         return qry(x, y, l, mid, 2 * curr);       else if (x > mid)         return qry(x, y, mid + 1, r, 2 * curr + 1);       return (qry(x, mid, l, mid, 2 * curr) +               qry(mid + 1, y, mid + 1, r, 2 * curr + 1));     }     void build(int l, int r, int curr) {       int mid = (l + r) / 2;       if (l == r) {         st[curr] = 0;         return;       }       build(l, mid, 2 * curr);       build(mid + 1, r, 2 * curr + 1);       st[curr] = (st[2 * curr] + st[2 * curr + 1]);     }   };   const int N = 500005;   int n;   string s;   lazy_seg<long long> f(N);   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n >> s;     long long ans = 0;     int curl = 0;     bool has = false;     stack<pair<int, int> > opt_range;     opt_range.push({-1000000000, -1});     for (int i = 0; i < n; i++) {       long long fa = ans;       if (s[i] ==  0 ) {         if (has) {           opt_range.push({curl, i - 1});         }         ans += f.qry(0, i, 0, n - 1, 1);         has = false;       } else {         if (!has) {           has = 1;           curl = i;         }         if (opt_range.top().second != -1) {           auto u = opt_range.top();           f.upd(u.second + 1, i, +1, 0, n - 1, 1);           u.second--;           if (u.first > u.second) {             opt_range.pop();           } else {             opt_range.pop();             opt_range.push(u);           }         } else {           f.upd(0, i, 1, 0, n - 1, 1);         }         ans += f.qry(0, i, 0, n - 1, 1);       }     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e3 + 5;   const int INF = (int)1e9 + 7;   int x[N], y[N];   int n;   map<pair<int, int>, int> id;   vector<pair<int, int> > g[2][2];   int dist(int i, int j) {     return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);   }   void calc(vector<pair<int, int> > g[2][2]) {     bool flag = 0;     int sz00 = (int)g[0][0].size();     int sz11 = (int)g[1][1].size();     int sz01 = (int)g[0][1].size();     int sz10 = (int)g[1][0].size();     int fx, fy;     if (sz00 == n) {       flag = 1, fx = 0, fy = 0;     }     if (sz11 == n) {       flag = 1, fx = 1, fy = 1;     }     if (sz01 == n) {       flag = 1, fx = 0, fy = 1;     }     if (sz10 == n) {       flag = 1, fx = 1, fy = 0;     }     if (flag) {       vector<pair<int, int> > nw[2][2];       for (auto x : g[fx][fy]) {         int a = x.first, b = x.second;         a /= 2, b /= 2;         id[{a, b}] = id[{x.first, x.second}];         nw[abs(a) % 2][abs(b) % 2].push_back({a, b});       }       calc(nw);     }     if (sz00 + sz11 && sz01 + sz10) {       cout << sz00 + sz11 << endl;       for (pair<int, int> c : g[0][0]) {         cout << id[c] <<    ;       }       for (pair<int, int> c : g[1][1]) {         cout << id[c] <<    ;       }       exit(0);     }     if (sz00 && sz11) {       cout << sz00 << endl;       for (pair<int, int> c : g[0][0]) {         cout << id[c] <<    ;       }       exit(0);     }     if (sz01 && sz10) {       cout << sz01 << endl;       for (pair<int, int> c : g[0][1]) {         cout << id[c] <<    ;       }       exit(0);     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x[i], &y[i]);       x[i] += 1e6, y[i] += 1e6;       g[abs(x[i]) % 2][abs(y[i]) % 2].push_back({x[i], y[i]});       id[{x[i], y[i]}] = i;     }     calc(g);   }


#pragma comment(linker,  /STACK:256777216 )   #define _USE_MATH_DEFINES   #define _CRT_SECURE_NO_WARNINGS   #include <iostream>   #include <string>   #include <cstring>   #include <vector>   #include <queue>   #include <bitset>   #include <stack>   #include <deque>   #include <set>   #include <unordered_set>   #include <map>   #include <unordered_map>   #include <algorithm>   #include <math.h>   #include <chrono>   #include <cmath>   #include <climits>   #include <ctime>   #include <random>   #include <iostream>   #include <cstdio>   #include <iomanip>   using namespace std;      #define forx(_name,_from, to, value) for (int name = from; name < to; name += value)   #define rforx(_name, from, to, value) for (int name = from; name > to; name -= _value)   #define all(_STL_NAME) _STL_NAME.begin(), _STL_NAME.end()   #define rall(_STL_NAME) _STL_NAME.rbegin(), _STL_NAME.rend()   #define mp(_FIRST,_SECOND) make_pair(_FIRST,_SECOND)      typedef long long ll;   typedef unsigned long long llu;   typedef long double ld;   const ld eps = 1e-9;   mt19937 rndm;      void start() {    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);    cout << setprecision(5);    cout.setf(ios::fixed);    string FILENAME =  dice ;    rndm.seed(time(0));   #ifdef _DEBUG    freopen( input.txt ,  rt , stdin);    freopen( output.txt ,  wt , stdout);   #else    /*freopen((FILENAME +  .in ).c_str(),  rt , stdin);    freopen((FILENAME +  .out ).c_str(),  wt , stdout);*/   #endif   }      const int MAX_N = 5e3 + 5;   const int INF = 1e9 + 7;   const int MOD = 1e9 + 7;      int a[MAX_N];   ll cnt[MAX_N];   ll dp1[MAX_N][MAX_N];   ll dp2[MAX_N][MAX_N];      void CalcCnt(int n, int k) {    for (int i = 0; i < n; ++i) {     dp1[i][0] = 1;    }       for (int j = 1; j <= k; ++j) {     for (int i = 0; i < n; ++i) {      if (i > 0) {       dp1[i][j] = (dp1[i][j] + dp1[i - 1][j - 1]) % MOD;      }      if (i < n - 1) {       dp1[i][j] = (dp1[i][j] + dp1[i + 1][j - 1]) % MOD;      }     }    }       for (int i = 0; i < n; ++i) {     for (int j = 0; j <= k; ++j) {      dp2[i][j] = (dp1[i][j] * dp1[i][k - j]) % MOD;     }    }       for (int i = 0; i < n; ++i) {     for (int j = 0; j <= k; ++j) {      cnt[i] = (cnt[i] + dp2[i][j]) % MOD;     }    }   }      void solve() {    int n, k, q;    cin >> n >> k >> q;    CalcCnt(n, k);    for (int i = 0; i < n; ++i) {     cin >> a[i];    }       ll ans = 0;    for (int i = 0; i < n; ++i) {     ans = (ans + (a[i] * cnt[i]) % MOD) % MOD;    }       for (int i = 0; i < q; ++i) {     int ii, x;     cin >> ii >> x;     --ii;     ans = (ans - (a[ii] * cnt[ii]) % MOD + MOD) % MOD;     a[ii] = x;     ans = (ans + (a[ii] * cnt[ii]) % MOD) % MOD;     cout << ans <<   n ;    }   }      void multitest() {    int t;    cin >> t;    for (int i = 0; i < t; ++i) {     solve();    }   }      int main() {    start();    //multitest();    solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 2;   vector<int> g[MAXN];   int d[MAXN], low[MAXN], scc[MAXN];   bool stacked[MAXN];   stack<int> s;   int ticks, current_scc;   void tarjan(int u) {     d[u] = low[u] = ticks++;     s.push(u);     stacked[u] = true;     const vector<int> &out = g[u];     for (int k = 0, m = out.size(); k < m; ++k) {       const int &v = out[k];       if (d[v] == -1) {         tarjan(v);         low[u] = min(low[u], low[v]);       } else if (stacked[v]) {         low[u] = min(low[u], low[v]);       }     }     if (d[u] == low[u]) {       int v;       do {         v = s.top();         s.pop();         stacked[v] = false;         scc[v] = current_scc;       } while (u != v);       current_scc++;     }   }   vector<pair<long long, long long> > adj[2 * MAXN];   vector<pair<long long, pair<long long, long long> > > reg;   long long peso[2 * MAXN];   long long dp[2 * MAXN];   long long getval(long long x) {     long long r = sqrt(2 * x);     if (r * r + r > 2 * x) r--;     return (r + 1) * x - (r * (r + 1) * (r + 2) / 6);   }   long long solve(long long u) {     long long high = 0;     if (dp[u] != -1) return dp[u];     for (auto d : adj[u]) {       long long v = d.first, w = d.second;       high = max(high, solve(v) + w);     }     return dp[u] = high;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     memset(d, -1, sizeof(d));     memset(scc, -1, sizeof(scc));     long long n, m;     cin >> n >> m;     for (long long(i) = (0); (i) < (m); (i)++) {       long long u, v, w;       cin >> u >> v >> w;       g[u].push_back(v);       reg.push_back(make_pair((u), make_pair((v), (w))));     }     long long x;     cin >> x;     tarjan(x);     long long tot = current_scc;     for (auto t : reg) {       long long u = t.first, v = t.second.first, w = t.second.second;       if (scc[u] != -1 && scc[u] == scc[v]) {         peso[scc[u]] += getval(w);       } else if (scc[u] == -1 || scc[v] == -1)         continue;       else if (scc[u] != scc[v]) {         adj[scc[u] + tot].push_back(make_pair(scc[v], w));       }     }     for (long long(i) = (0); (i) < (tot); (i)++) {       adj[i].push_back(make_pair(i + tot, peso[i]));     }     memset(dp, -1, sizeof(dp));     cout << solve(scc[x]) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cap[200005];   int empty[200005];   int main() {     int n, i, m, z, p, k, x;     cin >> n;     set<int> unfilled;     for (i = 1; i <= n; i++) {       cin >> cap[i];       unfilled.insert(i);       empty[i] = cap[i];     }     cin >> m;     for (i = 1; i <= m; i++) {       cin >> z;       if (z == 1) {         cin >> p >> x;         set<int>::iterator it = unfilled.upper_bound(p - 1);         int flag = 0;         while (it != unfilled.end() && flag == 0) {           int l = *it;           if (x >= empty[l]) {             x = x - empty[l];             empty[l] = 0;             it++;             unfilled.erase(l);           } else {             empty[l] = empty[l] - x;             it++;             flag = 1;           }         }       } else {         cin >> k;         int l = cap[k] - empty[k];         cout << l <<   n ;       }     }   }


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


#include <bits/stdc++.h>   using namespace std;   long long dp[1001][1001];   long long mem(int sz, int rem) {     if (rem == 0) return 1;     if (dp[sz][rem] != -1) return dp[sz][rem];     long long& ref = dp[sz][rem];     ref = 0;     for (int i = 1; i <= sz - 2; i++) {       long long t = (sz - 1 - i) * mem(i, rem - 1);       ref = (ref + t) % 1000000007LL;     }     return ref;   }   int n, m, k;   int main() {     cin >> n >> m >> k;     memset(dp, -1, sizeof dp);     long long t1 = mem(n, k);     long long t2 = mem(m, k);     long long res = (t1 * t2) % 1000000007LL;     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     pair<int, int> u, d, r, l, c;     u.first = 0;     u.second = 1;     d.first = 0;     d.second = -1;     r.first = 1;     r.second = 0;     l.first = -1;     l.second = 0;     int ans = 0, n;     string s;     cin >> n >> s;     for (int i = 0; i < n - 1; i++) {       if (s[i] ==  U ) {         c.first = u.first;         c.second = u.second;       } else if (s[i] ==  D ) {         c.first = d.first;         c.second = d.second;       } else if (s[i] ==  L ) {         c.first = l.first;         c.second = l.second;       } else {         c.first = r.first;         c.second = r.second;       }       for (int j = i + 1; j < n; j++) {         if (s[j] ==  U ) {           c.first += u.first;           c.second += u.second;         } else if (s[j] ==  D ) {           c.first += d.first;           c.second += d.second;         } else if (s[j] ==  L ) {           c.first += l.first;           c.second += l.second;         } else {           c.first += r.first;           c.second += r.second;         }         if (c.first == 0 && c.second == 0) ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     int a[k];     int count = 0;     for (int i = 0; i < k; i++) {       cin >> a[i];     }     while (n--) {       int b;       for (int i = 0; i < m; i++) {         cin >> b;         int *p = find(a, a + k, b);         count = count + p - a + 1;         for (int j = p - a; j > 0; j--) {           int t = a[j];           a[j] = a[j - 1];           a[j - 1] = t;         }       }     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long H;     int n;     cin >> H >> n;     vector<long long> a(n);     long long sum = 0;     long long gap = 0;     long long h = H;     int f = 0;     int pos;     for (int i = 0; i < n; i++) {       cin >> a[i];       sum -= a[i];       h += a[i];       if (h <= 0 && !f) {         f = 1;         pos = i + 1;       }       gap = max(gap, sum);     }     if (f) {       cout << pos << endl;       return 0;     }     if (sum <= 0) {       cout << -1 << endl;       return 0;     }     long long whole = (H - gap) / sum;     H -= whole * sum;     long long result = whole * n;     for (int i = 0;; i++) {       H += a[i % n];       result++;       if (H <= 0) {         cout << result << endl;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   const int len = (int)2e5;   int a[len];   int main() {     int k, n;     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     string s;     cin >> s;     long long ans = 0;     for (int i = 0; i < n; i++) {       int j = i;       vector<int> v;       while (j < n && s[j] == s[i]) {         v.push_back(a[j]);         j++;       }       sort(v.begin(), v.end(), greater<int>());       for (int l = 0; l < min(k, (int)v.size()); l++) {         ans += v[l];       }       i = j - 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O3 )   using namespace std;   void _main();   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     _main();   }   const int inf = INT_MAX / 2;   const long long infl = 1LL << 60;   template <class T>   bool chmax(T &a, const T &b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool chmin(T &a, const T &b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   string E;   int P, M;   struct Node {     Node *left, *right;     int value, id, cnt;   };   int i = 0, idx = 0;   Node *parse() {     Node *n = new Node;     n->id = idx;     idx++;     if (E[i] ==  ( ) {       i++;       n->left = parse();       i++;       n->right = parse();       i++;       n->value = -1;       n->cnt = n->left->cnt + n->right->cnt + 1;     } else {       n->value = E[i] -  0 ;       i++;       n->cnt = 0;     }     return n;   }   int vis[4101][105];   pair<int, int> memo[4101][105];   pair<int, int> f(Node *n, int p) {     int m = n->cnt - p;     if (m < 0) return {inf, inf};     int id = n->id;     if (vis[id][p]) return memo[id][p];     if (n->cnt == 0) return memo[id][p] = {n->value, n->value};     int mi = inf, ma = -inf;     for (int pls = 0; pls < p; pls++) {       auto le = f(n->left, pls);       if (le.first == inf) continue;       auto ri = f(n->right, p - pls - 1);       if (ri.first == inf) continue;       vector<int> a = {le.first, le.second};       vector<int> b = {ri.first, ri.second};       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) {           int res = a[i] + b[j];           chmin(mi, res);           chmax(ma, res);         }     }     for (int pls = 0; pls < p + 1; pls++) {       auto le = f(n->left, pls);       if (le.first == inf) continue;       auto ri = f(n->right, p - pls);       if (ri.first == inf) continue;       vector<int> a = {le.first, le.second};       vector<int> b = {ri.first, ri.second};       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) {           int res = a[i] - b[j];           chmin(mi, res);           chmax(ma, res);         }     }     assert(mi != inf);     vis[id][p] = 1;     return memo[id][p] = {mi, ma};   }   pair<int, int> g(Node *n, int p) {     int m = n->cnt - p;     if (m < 0) return {inf, inf};     int id = n->id;     if (vis[id][p]) return memo[id][p];     if (n->cnt == 0) return memo[id][p] = {n->value, n->value};     int mi = inf, ma = -inf;     for (int pls = 0; pls < p; pls++) {       auto le = g(n->left, pls);       if (le.first == inf) continue;       auto ri = g(n->right, p - pls - 1);       if (ri.first == inf) continue;       vector<int> a = {le.first, le.second};       vector<int> b = {ri.first, ri.second};       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) {           int res = a[i] - b[j];           chmin(mi, res);           chmax(ma, res);         }     }     for (int pls = 0; pls < p + 1; pls++) {       auto le = g(n->left, pls);       if (le.first == inf) continue;       auto ri = g(n->right, p - pls);       if (ri.first == inf) continue;       vector<int> a = {le.first, le.second};       vector<int> b = {ri.first, ri.second};       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) {           int res = a[i] + b[j];           chmin(mi, res);           chmax(ma, res);         }     }     assert(mi != inf);     vis[id][p] = 1;     return memo[id][p] = {mi, ma};   }   void _main() {     cin >> E >> P >> M;     E +=  = ;     Node *root = parse();     if (P <= M) {       auto res = f(root, P);       int ans = res.second;       cout << ans << endl;     } else {       auto res = g(root, M);       int ans = res.second;       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   struct info_t {     int x, y, i;   };   int main() {     int n, m;     scanf( %d %d , &n, &m);     vector<info_t> from(m), to(m);     vector<vector<int>> answer_from, answer_to;     auto solve = [&](vector<info_t> &info, vector<vector<int>> &answer) {       for (int i = 0; i < m; ++i) {         scanf( %d %d , &info[i].x, &info[i].y);         --info[i].x;         --info[i].y;         info[i].i = i;       }       sort(info.begin(), info.end(), [&](const info_t &a, const info_t &b) {         return a.x < b.x || (a.x == b.x && a.y < b.y);       });       while (true) {         bool need = false;         for (int i = 0; i < m; ++i) {           if (info[i].x > i) {             answer.push_back(                 vector<int>{info[i].x, info[i].y, info[i].x - 1, info[i].y});             --info[i].x;             need = true;           }         }         for (int i = m - 1; ~i; --i) {           if (info[i].x < i) {             answer.push_back(                 vector<int>{info[i].x, info[i].y, info[i].x + 1, info[i].y});             ++info[i].x;             need = true;           }         }         if (!need) {           break;         }       }       for (int i = 0; i < m; ++i) {         while (info[i].y < info[i].i) {           answer.push_back(               vector<int>{info[i].x, info[i].y, info[i].x, info[i].y + 1});           ++info[i].y;         }         while (info[i].y > info[i].i) {           answer.push_back(               vector<int>{info[i].x, info[i].y, info[i].x, info[i].y - 1});           --info[i].y;         }       }     };     solve(from, answer_from);     solve(to, answer_to);     sort(from.begin(), from.end(),          [&](const info_t &a, const info_t &b) { return a.i < b.i; });     sort(to.begin(), to.end(),          [&](const info_t &a, const info_t &b) { return a.i < b.i; });     for (int i = 0; i < m; ++i) {       while (from[i].x < to[i].x) {         answer_from.push_back(             vector<int>{from[i].x, from[i].y, from[i].x + 1, from[i].y});         ++from[i].x;       }       while (from[i].x > to[i].x) {         answer_from.push_back(             vector<int>{from[i].x, from[i].y, from[i].x - 1, from[i].y});         --from[i].x;       }     }     printf( %d n , answer_from.size() + answer_to.size());     for (auto v : answer_from) {       printf( %d %d %d %d n , v[0] + 1, v[1] + 1, v[2] + 1, v[3] + 1);     }     reverse(answer_to.begin(), answer_to.end());     for (auto v : answer_to) {       printf( %d %d %d %d n , v[2] + 1, v[3] + 1, v[0] + 1, v[1] + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int size1 = 8e5;   const long long INF = 1e18;   const int INF1 = 1e9;   const double eps = 1e-5;   const long long mod = 1e9 + 7;   vector<vector<int> > graph(size1);   vector<vector<long long> > graph1(size1);   static int maxder[size1], minder[size1], d[size1], v[size1], u[size1], w[size1],       tin[size1], tout[size1];   static bool used[size1];   static long long ras[size1];   static long long tree[size1], tree1[size1], tree2[size1], tree3[size1],       w1[size1];   int num = 1;   int time1 = 0;   void dfs(int v) {     used[v] = true;     minder[v] = INF1;     for (int i = 0; i < graph[v].size(); i++) {       if (!used[graph[v][i]]) {         dfs(graph[v][i]);         minder[v] = min(minder[v], minder[graph[v][i]]);       }     }     maxder[v] = num;     num++;     minder[v] = min(minder[v], maxder[v]);   }   void dfs1(int v, long long ras1, int depth) {     time1++;     used[v] = true;     ras[maxder[v]] = ras1;     d[v] = depth;     tin[v] = time1;     for (int i = 0; i < graph[v].size(); i++) {       if (!used[graph[v][i]]) {         dfs1(graph[v][i], ras1 + graph1[v][i], depth + 1);       }     }     time1++;     tout[v] = time1;   }   void build_tree(int tl, int tr, int pos) {     if (tl == tr) {       tree[pos] = ras[tl];     } else {       int tm = (tl + tr) / 2;       build_tree(tl, tm, pos * 2);       build_tree(tm + 1, tr, pos * 2 + 1);       tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];     }   }   void prib(int tl, int tr, int pos, int l, int r, long long x) {     if (l <= tl && tr <= r) {       tree1[pos] += x;     } else {       int tm = (tl + tr) / 2;       if (max(tl, l) <= min(tm, r)) {         prib(tl, tm, pos * 2, l, r, x);       }       if (max(tm + 1, l) <= min(tr, r)) {         prib(tm + 1, tr, pos * 2 + 1, l, r, x);       }       tree[pos] = tree[pos * 2] + tree[pos * 2 + 1] +                   tree1[pos * 2] * (tm - tl + 1) + tree1[pos * 2 + 1] * (tr - tm);     }   }   long long zn(int tl, int tr, int pos, int pos1, long long add) {     if (tl == pos1 && tr == pos1) {       return add + tree[pos] + tree1[pos];     } else {       int tm = (tl + tr) / 2;       if (tl <= pos1 && pos1 <= tm) {         return zn(tl, tm, pos * 2, pos1, add + tree1[pos]);       } else {         return zn(tm + 1, tr, pos * 2 + 1, pos1, add + tree1[pos]);       }     }   }   void build_tree1(int tl, int tr, int pos) {     if (tl == tr) {       tree2[pos] = ras[tl] + w1[tl];     } else {       int tm = (tl + tr) / 2;       build_tree1(tl, tm, pos * 2);       build_tree1(tm + 1, tr, pos * 2 + 1);       tree2[pos] = min(tree2[pos * 2], tree2[pos * 2 + 1]);     }   }   void upd2(int tl, int tr, int pos, int l, int r, long long x) {     if (l <= tl && tr <= r) {       tree3[pos] += x;     } else {       int tm = (tl + tr) / 2;       if (max(tl, l) <= min(tm, r)) {         upd2(tl, tm, pos * 2, l, r, x);       }       if (max(tm + 1, l) <= min(tr, r)) {         upd2(tm + 1, tr, pos * 2 + 1, l, r, x);       }       tree2[pos] = min(tree2[pos * 2] + tree3[pos * 2],                        tree2[pos * 2 + 1] + tree3[pos * 2 + 1]);     }   }   long long zn1(int tl, int tr, int pos, int l, int r, long long add) {     if (l <= tl && tr <= r) {       return add + tree3[pos] + tree2[pos];     } else {       int tm = (tl + tr) / 2;       long long ans = INF;       if (max(l, tl) <= min(r, tm)) {         ans = min(ans, zn1(tl, tm, pos * 2, l, r, add + tree3[pos]));       }       if (max(tm + 1, l) <= min(tr, r)) {         ans = min(ans, zn1(tm + 1, tr, pos * 2 + 1, l, r, add + tree3[pos]));       }       return ans;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, q, i;     cin >> n >> q;     for (i = 1; i <= n - 1; i++) {       cin >> v[i] >> u[i] >> w[i];       graph[v[i]].push_back(u[i]);       graph[u[i]].push_back(v[i]);       graph1[v[i]].push_back(w[i]);       graph1[u[i]].push_back(w[i]);     }     dfs(1);     for (i = n; i <= 2 * n - 2; i++) {       cin >> v[i] >> u[i] >> w[i];       w1[maxder[v[i]]] = w[i];     }     memset(used, false, sizeof used);     dfs1(1, 0, 0);     build_tree(1, n, 1);     build_tree1(1, n, 1);     for (i = 1; i <= q; i++) {       int t, a, b;       cin >> t >> a >> b;       if (t == 1) {         if (a >= n) {           upd2(1, n, 1, maxder[v[a]], maxder[v[a]], b - w1[maxder[v[a]]]);           w1[maxder[v[a]]] = b;         } else {           int v1 = 0;           if (d[v[a]] > d[u[a]]) {             v1 = v[a];           } else {             v1 = u[a];           }           prib(1, n, 1, minder[v1], maxder[v1], b - w[a]);           upd2(1, n, 1, minder[v1], maxder[v1], b - w[a]);           w[a] = b;         }       } else {         long long res = INF;         if (max(tin[a], tin[b]) <= min(tout[a], tout[b]) && d[a] <= d[b]) {           res = min(res, zn(1, n, 1, maxder[b], 0) - zn(1, n, 1, maxder[a], 0));         }         res = min(res, zn1(1, n, 1, minder[a], maxder[a], 0) -                            zn(1, n, 1, maxder[a], 0) + zn(1, n, 1, maxder[b], 0));         cout << res << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   long long MOD = 1e9 + 7;   using namespace std;   int in() {     int x;     scanf( %d , &x);     return x;   }   long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   template <typename A, size_t NNN, typename T>   void Fill(A (&array)[NNN], const T &val) {     fill((T *)array, (T *)(array + NNN), val);   }   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   struct pair_hash {     template <class T1, class T2>     size_t operator()(const pair<T1, T2> &p) const {       return hash<T1>()(p.first) ^ hash<T2>()(p.second);     }   };   vector<pair<long long, long long> > G[400001];   long long ans[400001], w[400001];   long long s;   long long dfs(int t, int p) {     long long temp1 = w[t], temp2 = 0;     for (int i = 0; i < G[t].size(); i++) {       if (G[t][i].first == p) continue;       temp2 = max(temp2, dfs(G[t][i].first, t) + w[t] - G[t][i].second);       if (temp2 > temp1) swap(temp1, temp2);     }     s = max(s, temp1 + temp2 - w[t]);     return temp1;   }   int main() {     long long n = lin();     for (long long i = 1; i <= n; ++i) w[i] = lin();     for (long long i = 1; i <= n; ++i) {       ans[i] = w[i];     }     for (int i = 0; i < n - 1; i++) {       long long u = lin(), v = lin(), c = lin();       G[u].push_back({v, c});       G[v].push_back({u, c});     }     s = max(s, dfs(1, 0));     for (long long i = 1; i <= n; ++i) s = max(s, w[i]);     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<int, int> tab;     std::map<int, int>::iterator p;     int n;     int x;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       tab[x]++;     }     x = 0;     int nb = 0;     for (p = tab.begin(); p != tab.end(); ++p) {       nb++;       if (p->second > x) {         x = p->second;       }     }     cout << x <<     << nb;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c1 = 0, c2 = 0;     cin >> n;     char x[n];     for (int i = 0; i < n; i++) {       cin >> x[i];       if (x[i] ==  A )         c1++;       else if (x[i] ==  D )         c2++;     }     if (c1 > c2)       cout <<  Anton ;     else if (c1 < c2)       cout <<  Danik ;     else       cout <<  Friendship ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   inline int add(int x, int y) {     x += y;     return x >= mod ? x - mod : x;   }   inline int sub(int x, int y) {     x -= y;     return x < 0 ? x + mod : x;   }   inline int mul(int x, int y) { return 1ll * x * y % mod; }   int quick_pow(int x, int y) {     int res = 1;     while (y) {       if (y & 1) res = mul(res, x);       x = mul(x, x), y >>= 1;     }     return res;   }   inline int getinv(int x) { return quick_pow(x, mod - 2); }   const int maxk = 17;   const int maxn = 1 << 16 | 5;   int fac[maxk], ifac[maxk], inv[maxk];   void init(int N) {     fac[0] = 1;     for (int i = 1; i <= N; i++) fac[i] = mul(fac[i - 1], i);     ifac[N] = getinv(fac[N]);     for (int i = N; i >= 1; i--) ifac[i - 1] = mul(ifac[i], i);     inv[0] = inv[1] = 1;     for (int i = 2; i <= N; i++) inv[i] = mul(inv[mod % i], mod - mod / i);   }   int n, K, c;   map<vector<int>, int> Map;   void getln(int *A) {     static int B[maxk];     B[0] = 0;     for (int i = 1; i <= K; i++) {       int tmp = 0;       for (int j = 1; j < i; j++) tmp = add(tmp, mul(j, mul(B[j], A[i - j])));       B[i] = sub(A[i], mul(tmp, inv[i]));     }     for (int i = 0; i <= K; i++) A[i] = B[i];   }   void getexp(int *A) {     static int B[maxk];     B[0] = 1;     for (int i = 1; i <= K; i++) {       int tmp = 0;       for (int j = 0; j < i; j++) tmp = add(tmp, mul(mul(A[i - j], i - j), B[j]));       B[i] = mul(tmp, inv[i]);     }     for (int i = 0; i <= K; i++) A[i] = B[i];   }   int popcnt[maxn], A[maxn];   vector<int> num[205];   int cnt[205], sum[maxn << 1], tot;   int st[205], top;   int Ln[maxn][maxk];   int main() {     scanf( %d%d%d , &n, &K, &c), init(K);     int all = 0;     for (int i = 1, x; i <= n; i++) {       scanf( %d , &x), all ^= x;       vector<int> A;       for (int j = 1; j <= K; j++) A.push_back(x ^ (x - j));       Map[A]++;     }     for (auto u : Map) num[++tot] = u.first, cnt[tot] = u.second;     int S = (1 << K) - 1;     for (int i = 0; i <= S; i++) {       static int f[maxk];       f[0] = 1;       for (int j = 0; j < K; j++)         f[j + 1] = ((i >> j) & 1) ? mod - ifac[j + 1] : ifac[j + 1];       getln(f);       for (int j = 0; j <= K; j++) Ln[i][j] = f[j];     }     S = (1 << c) - 1;     for (int i = 1; i <= S; i++) popcnt[i] = popcnt[i >> 1] + (i & 1);     for (int i = 0; i <= S; i++) {       for (int j = 1; j <= tot; j++) {         int sta = 0;         for (int k = 0; k < K; k++)           if (popcnt[i & num[j][k]] & 1) sta |= (1 << k);         if (!sum[sta]) st[++top] = sta;         sum[sta] += cnt[j];       }       static int f[maxk], g[maxk], h[maxk];       for (int j = 0; j <= K; j++) f[j] = h[j] = 0;       f[0] = 1;       for (int j = 1; j <= top; j++) {         int sta = st[j];         for (int k = 0; k <= K; k++) g[k] = mul(Ln[sta][k], sum[sta]);         getexp(g);         for (int k = 0; k <= K; k++)           for (int l = 0; k + l <= K; l++)             h[k + l] = add(h[k + l], mul(f[k], g[l]));         for (int k = 0; k <= K; k++) f[k] = h[k], h[k] = 0;         sum[sta] = 0;       }       top = 0;       A[i] = mul(f[K], fac[K]);     }     int N = 1 << c;     for (int l = 2; l <= N; l <<= 1) {       int m = (l >> 1);       for (int j = 0; j < N; j += l)         for (int i = j; i < j + m; i++) {           int u = A[i], v = A[i + m];           A[i] = add(u, v);           A[i + m] = sub(u, v);         }     }     int tmp = getinv(mul(N, quick_pow(n, K)));     for (int i = 0; i < N; i++) printf( %d  , mul(A[i ^ all], tmp));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       int flag = 0;       int n;       int now;       cin >> n >> now;       int l = now, r = now;       int past = 0;       for (int i = 0; i < n; i++) {         int len, lb, rb;         cin >> len >> lb >> rb;         l -= len - past;         r += len - past;         l = max(l, lb);         r = min(r, rb);         if (l > r) flag = 1;         past = len;       }       if (flag)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};   const int dy[8] = {-1, 0, 1, -1, 1, 0, 1, -1};   const int maxn = 1000;   const int mod = 998244353;   int res, sum, r, n;   string s;   int main() {     ios::sync_with_stdio(false);     cin >> s;     for (char c : s) {       r = (c -  0 ) % 3;       sum += r;       ++n;       if (n == 3 || r == 0 || sum % 3 == 0) res++, sum = n = 0;     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, s, r, m, ans;     string x;     cin >> n >> m;     long long a[n + 1][m + 1];     for (i = 1; i <= n; i++) {       if (i % 2 == 1) {         for (j = 0; j < m; j++) a[i][j] = 0;       } else {         for (j = 0; j < m; j++) a[i][j] = 1;       }     }     for (i = 2; i <= n; i += 2) {       if (i % 4 == 2) {         a[i][m - 1] = 0;       } else         a[i][0] = 0;     }     for (i = 1; i <= n; i++) {       for (j = 0; j < m; j++) {         if (a[i][j] == 0)           cout <<  # ;         else           cout <<  . ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(NULL);     ios_base::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int k;       cin >> k;       string s;       cin >> s;       int ps = 0;       int maxps = 0;       for (int i = k - 1; i >= 0; --i) {         if (s[i] ==  P ) {           ++ps;         } else {           maxps = max(ps, maxps);           ps = 0;         }       }       cout << maxps <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   int main(void) {     int matrix[12][12];     memset(matrix, 0, sizeof(matrix));     int i;     char s[3];     int col_r, row_r, col_k, row_k;     scanf( %s , s);     col_r = s[0] -  a  + 2;     row_r = s[1] -  1  + 2;     scanf( %s , s);     col_k = s[0] -  a  + 2;     row_k = s[1] -  1  + 2;     for (i = 2; i < 10; i++) {       matrix[row_r][i] = 1;       matrix[i][col_r] = 1;     }     matrix[row_k][col_k] = 1;     matrix[row_k - 1][col_k + 2] = 1;     matrix[row_k - 1][col_k - 2] = 1;     matrix[row_k + 1][col_k - 2] = 1;     matrix[row_k + 1][col_k + 2] = 1;     matrix[row_k - 2][col_k + 1] = 1;     matrix[row_k - 2][col_k - 1] = 1;     matrix[row_k + 2][col_k + 1] = 1;     matrix[row_k + 2][col_k - 1] = 1;     matrix[row_r - 1][col_r + 2] = 1;     matrix[row_r - 1][col_r - 2] = 1;     matrix[row_r + 1][col_r + 2] = 1;     matrix[row_r + 1][col_r - 2] = 1;     matrix[row_r - 2][col_r + 1] = 1;     matrix[row_r - 2][col_r - 1] = 1;     matrix[row_r + 2][col_r + 1] = 1;     matrix[row_r + 2][col_r - 1] = 1;     int j, sum = 0;     for (i = 2; i < 10; i++)       for (j = 2; j < 10; j++)         if (matrix[i][j] == 0) sum++;     printf( %d n , sum);     return 0;   }


#include <bits/stdc++.h>   const int mod = 1e9 + 7;   const double eps = 1e-6;   const double pi = acos(-1);   const int inf = 0x7fffffff;   using namespace std;   int a[105];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     if (a[1] == 3) return 0 * printf( NO n );     int x = 1, y = 2;     for (int i = 1; i <= n; i++) {       if (a[i] == x) {         if (x == 1) {           if (y == 2)             y = 3;           else             y = 2;         } else if (x == 2) {           if (y == 1)             y = 3;           else             y = 1;         } else if (x == 3) {           if (y == 1)             y = 2;           else             y = 1;         }       } else if (a[i] == y) {         if (y == 1) {           if (x == 2)             x = 3;           else             x = 2;         } else if (y == 2) {           if (x == 1)             x = 3;           else             x = 1;         } else if (y == 3) {           if (x == 1)             x = 2;           else             x = 1;         }       } else         return 0 * printf( NO n );     }     printf( YES n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000001;   const long double EPS = 1e-9;   const int MOD = 1000000007;   const long long LLINF = 1000000000000000001;   const int maxn = 350000;   int n, k;   int result[maxn];   int cost[maxn];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> cost[i];     set<pair<int, int>, greater<pair<int, int> > > s;     for (int i = 1; i <= k; i++) s.insert(make_pair(cost[i], i));     for (int i = k + 1; i <= n; i++) {       s.insert(make_pair(cost[i], i));       result[s.begin()->second] = i;       s.erase(s.begin());     }     for (int i = n + 1; i <= n + k; i++) {       result[s.begin()->second] = i;       s.erase(s.begin());     }     long long sumCost = 0;     for (int i = 1; i <= n; i++) sumCost += (long long)(result[i] - i) * cost[i];     cout << sumCost <<   n ;     for (int i = 1; i <= n; i++) cout << result[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long MOD = 1e9 + 6;   const long long inf = 2e18;   const int maxn = 200005;   const int maxa = 300005;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     if (b < 0) return -1;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long powmod2(long long a, long long b) {     long long res = 1;     a %= MOD;     if (b < 0) return -1;     for (; b; b >>= 1) {       if (b & 1) res = res * a % MOD;       a = a * a % MOD;     }     return res;   }   const int INF = 0x3f3f3f3f;   int a[maxn];   vector<pair<int, int> > ans;   vector<int> val[maxn];   int dis[maxn];   int main() {     ios::sync_with_stdio(0);     int n, m, k;     cin >> n >> m >> k;     for (int i = 1; i <= m; i++) cin >> a[i];     int now = 2;     int cnt = 1;     val[0].push_back(1);     for (int i = 1; i <= m; i++) {       for (int j = 1; j <= a[i]; j++) {         dis[now] = i;         val[i].push_back(now++);       }       cnt += a[i] - 1;     }     if (k > cnt) {       return 0 * puts( -1 );     }     int lstpos = 0;     int nowval = 0;     for (int i = 2; i <= n; i++) {       if (dis[i] > nowval) {         nowval = dis[i];         lstpos = 0;       }       if (lstpos != val[dis[i] - 1].size() && cnt > k) {         ans.push_back({i, val[dis[i] - 1][lstpos]});         if (lstpos != 0) {           cnt--;         }         lstpos++;       } else {         ans.push_back({i, val[dis[i] - 1][0]});       }     }     if (k != cnt) {       return 0 * puts( -1 );     }     cout << n << endl;     for (auto k : ans) {       cout << k.first <<     << k.second << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 999999929, MAXN = 300005, P2 = (1 << 19), MTS = (1 << 20) + 5;   int pot2[MAXN];   int TREE[MTS][2];   int n;   long long suma(int p, int k, int num) {     p += P2;     k += P2;     long long res = 0LL;     while (p < k) {       if ((p & 1)) {         res += TREE[p++][num];         if (res >= MOD) res -= MOD;       }       if (!(k & 1)) {         res += TREE[k--][num];         if (res >= MOD) res -= MOD;       }       p >>= 1;       k >>= 1;     }     if (p == k) {       res += TREE[p][num];       if (res >= MOD) res -= MOD;     }     return res;   }   void aT(int pos, int val, int num) {     pos += P2;     TREE[pos][num] = val;     while (pos > 1) {       pos >>= 1;       TREE[pos][num] = TREE[(pos << 1)][num] + TREE[((pos << 1) + 1)][num];       if (TREE[pos][num] >= MOD) TREE[pos][num] -= MOD;     }   }   void add(int pos) {     aT(pos, pot2[pos], 0);     aT(n - pos + 1, pot2[n - pos + 1], 1);   }   bool palindrom(int l) {     int k = min(l - 1, n - l);     long long a = suma(l - k, l + k, 0);     long long b = suma(n - l - k + 1, n - l + k + 1, 1);     if (l - k > n - l - k + 1) {       int g = l - k - (n - l - k + 1);       b *= pot2[g];       b %= MOD;     } else {       int g = n - l + 1 - l;       a *= pot2[g];       a %= MOD;     }     return (a == b);   }   int main() {     scanf( %d , &n);     pot2[0] = 1;     for (int i = 1; i <= n; ++i) {       pot2[i] = (pot2[i - 1] << 1);       if (pot2[i] >= MOD) pot2[i] %= MOD;     }     for (int i = 0; i < n; ++i) {       int a;       scanf( %d , &a);       if (!palindrom(a)) {         printf( YES );         return 0;       }       add(a);     }     printf( NO );   }


#include <bits/stdc++.h>   using namespace std;   const int inf = ~0u >> 1, mod = (int)(1e9) + 7;   int n, m;   long long pow2[555 * 555], f[555][555];   int main() {     scanf( %d%d , &n, &m);     pow2[0] = 1ll;     for (int i = 0; i < (int)500 * 500; ++i) pow2[i + 1] = (pow2[i] * 2ll) % mod;     f[0][0] = 1ll;     for (int k = 0; k < (int)m; ++k) {       for (int i = 1; i <= n; ++i)         for (int j = 0; j < i; ++j) {           long long tmp = f[j][k];           tmp = tmp * (pow2[i - j] - 1) % mod;           tmp = tmp * (pow2[j * (i - j)]) % mod;           f[i][k + 1] = (f[i][k + 1] + tmp) % mod;         }     }     long long ans = 0;     for (int i = 1; i <= n; ++i)       ans = (ans + f[i][m] * pow2[i * (n - i)] % mod) % mod;     if (m)       cout << ans << endl;     else       cout << 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int MOD = 998244353;   long long int exp(long long int n, long long int e) {     if (e == 0) return 1;     long long int temp = exp((n * n) % MOD, e / 2);     return e % 2 == 0 ? temp : (temp * n) % MOD;   }   long long int inverse(long long int n) { return exp(n, MOD - 2); }   int LIM = 3e5 + 10;   vector<long long int> factorial(LIM, 1);   vector<long long int> inv(LIM, 1);   long long int choose(long long int n, long long int r) {     long long int ans = ((factorial[n] * inv[r]) % MOD * inv[n - r]) % MOD;     return ans;   }   void solve() {     int n, k;     cin >> n >> k;     vector<long long int> arr(n + 1);     int a = 0;     for (int i = 1; i <= n; i++) {       cin >> arr[i];       if (i > 1) a += arr[i] == arr[i - 1];     }     a += (arr[1] == arr[n]);     long long int ans = 0;     long long int m = n - a;     for (int i = 0; i < m; i++) {       long long int temp = (exp(k - 2, i) * choose(m, i)) % MOD;       long long int prod = -1;       long long int c = m - i;       if (c % 2 == 1)         prod = exp(2, c - 1);       else         prod = (((exp(2, c) - choose(c, c / 2) + MOD) % MOD) * inv[2]) % MOD;       ;       temp = (temp * prod) % MOD;       ans = (ans + temp) % MOD;     }     ans = (ans * exp(k, a)) % MOD;     cout << ans << endl;   }   int main() {     int t;     t = 1;     for (int i = 1; i < LIM; i++) {       factorial[i] = (factorial[i - 1] * i) % MOD;       inv[i] = inverse(factorial[i]);     }     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, x, y, p;   int v[109][109];   int main() {     cin >> n >> k;     p = n;     while (p) {       v[x++][y++] = k;       p--;     }     for (int i = 0; i < n; i++) {       for (int h = 0; h < n; h++) cout << v[i][h] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (1 << 30) - 1;   const long double EPS = 1e-9;   const long double PI = fabs(atan2(0.0, -1.0));   const int MAXV = 80010;   int n;   vector<int> g[MAXV];   void load() {     scanf( %d , &n);     for (int i = 0, u, v; i < n - 1; i++) {       scanf( %d%d , &u, &v);       --u, --v;       g[u].push_back(v);       g[v].push_back(u);     }   }   int sz[MAXV];   unsigned long long pths[MAXV];   unsigned long long dp[MAXV];   unsigned long long bad = 0;   void dfs(int v, int p) {     sz[v] = 1;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       dfs(to, v);       sz[v] += sz[to];     }     pths[v] = 0;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       pths[v] += sz[to] * 1ULL * (sz[v] - sz[to] - 1);     }     assert(pths[v] % 2 == 0);     pths[v] /= 2;     pths[v] += sz[v] - 1;     dp[v] = 0;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       dp[v] += dp[to];     }     bad += dp[v];     dp[v] += pths[v] * 1ULL * sz[v];     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       bad += dp[to] * 1ULL * (sz[v] - sz[to] - 1);     }   }   void solve() {     dfs(0, -1);     unsigned long long res = 0;     unsigned long long all = 0;     for (int i = 0; i < n; i++) all += pths[i];     for (int i = 0; i < n; i++) res += pths[i] * (all - pths[i]);     cout << res - 2 * bad <<   n ;   }   int main() {     load();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long inf = 1e18;   const long double eps = 1e-10;   struct Pnt {     long long x, y;     Pnt(long long x, long long y) : x(x), y(y) {}   };   Pnt operator+(Pnt a, Pnt b) { return {a.x + b.x, a.y + b.y}; }   bool operator==(Pnt a, Pnt b) { return (a.x == b.x) && (a.y == b.y); }   struct Solver {     vector<Pnt> a;     long long n;     Solver(vector<Pnt> a) : a(a) { n = a.size(); }     long long solve() {       if (n % 2) return 0;       long long d = n / 2;       Pnt mid = a[0] + a[d];       for (long long i = 0; i < n; ++i) {         long long j = (i + d) % n;         if (!(a[i] + a[j] == mid)) return 0;       }       return 1;     }   };   const long long debug = 0;   int32_t main() {     if (debug) freopen( input.txt ,  r , stdin);     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long tests = 1;     if (debug) cin >> tests;     while (tests--) {       long long stress = 0;       if (!stress) {         long long n;         cin >> n;         vector<Pnt> a;         a.reserve(n);         for (long long i = 0; i < n; ++i) {           long long x, y;           cin >> x >> y;           a.push_back({x, y});         }         Solver solver(a);         long long res = solver.solve();         if (res)           cout <<  YES n ;         else           cout <<  NO n ;       } else {       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void debug(T v, string delimiter =   n ) {     for (__typeof(v.begin()) it = v.begin(), it_end = v.end(); it != it_end; it++)       cout << *it << delimiter;     cout << flush;   }   int dx[] = {0, 1, 0, -1};   int dy[] = {1, 0, -1, 0};   string ds =  SENW ;   const double PI = 4.0 * atan(1.0);   string solve(string s) {     if (s[0] ==  0 ) return 0;     long long ans = 1;     if (s[0] ==  ?  || isalpha(s[0])) ans = 9;     string zero =   ;     vector<int> used(30);     int rem = 10;     if (isalpha(s[0])) {       used[s[0] -  A ] = 1;       rem--;     }     for (int i = 1; i < (int)(s.size()); i++) {       if (s[i] ==  ? ) {         zero +=  0 ;       } else if (isalpha(s[i])) {         if (used[s[i] -  A ] == 0) {           used[s[i] -  A ] = 1;           ans *= rem;           rem--;         }       }     }     ostringstream out;     out << ans;     string ret = out.str();     return ret + zero;   }   int main() {     string s;     while (cin >> s) {       cout << solve(s) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 300000;   int sub[2 * MAXN + 5][2];   int par[2 * MAXN + 5];   int getRoot(int a) {     while (par[a] > 0) {       a = par[a];     }     return a;   }   long long int ans = 0;   void dsu(int a, int b) {     int x0 = sub[a][0] + sub[b][0];     int x1 = sub[a][1] + sub[b][1];     ans -= sub[a][0] * 1ll * sub[a][1] + sub[b][0] * 1ll * sub[b][1];     ans += x0 * 1ll * x1;     if (sub[a][0] + sub[a][1] < sub[b][0] + sub[b][1]) {       sub[b][0] = x0;       sub[b][1] = x1;       par[a] = b;     } else {       sub[a][0] = x0;       sub[a][1] = x1;       par[b] = a;     }   }   void removeDSU(int a, int b) {     if (sub[a][0] + sub[a][1] < sub[b][0] + sub[b][1]) {       sub[b][0] -= sub[a][0];       sub[b][1] -= sub[a][1];       par[a] = -1;     } else {       sub[a][0] -= sub[b][0];       sub[a][1] -= sub[b][1];       par[b] = -1;     }     ans -= (sub[a][0] + sub[b][0]) * 1ll * (sub[a][1] + sub[b][1]);     ans += sub[a][0] * 1ll * sub[a][1] + sub[b][0] * 1ll * sub[b][1];   }   vector<pair<int, int> > st[8 * MAXN];   void addEdge(int id, int l, int r, int u, int v, int x, int y) {     if (v < l || r < u) return;     if (u <= l && r <= v) {       st[id].push_back(pair<int, int>(x, y));       return;     }     int mid = (l + r) / 2;     addEdge(id * 2, l, mid, u, v, x, y);     addEdge(id * 2 + 1, mid + 1, r, u, v, x, y);   }   unordered_map<int, int> prevApp[MAXN + 4];   long long int res[MAXN + 5];   void dfs(int id, int l, int r) {     vector<pair<int, int> > operations;     for (auto i : st[id]) {       int m = getRoot(i.first);       int n = getRoot(i.second);       if (m == n) {         continue;       }       operations.push_back(pair<int, int>(m, n));       dsu(m, n);     }     int mid = (l + r) / 2;     if (l == r) {       res[l] = ans;     } else {       dfs(id * 2, l, mid);       dfs(id * 2 + 1, mid + 1, r);     }     reverse(operations.begin(), operations.end());     for (auto i : operations) {       removeDSU(i.first, i.second);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     int q;     cin >> q;     for (int i = 1; i <= q; i++) {       int x, y;       cin >> x >> y;       if (prevApp[x][y] == 0) {         prevApp[x][y] = i;       } else {         addEdge(1, 1, q, prevApp[x][y], i - 1, x, y + MAXN);         prevApp[x][y] = 0;       }     }     for (int i = 1; i <= MAXN; i++) {       for (auto j : prevApp[i]) {         if (j.second > 0) {           addEdge(1, 1, q, j.second, q, i, j.first + MAXN);         }       }     }     memset(par, -1, sizeof(par));     for (int i = 1; i <= MAXN; i++) {       sub[i][0] = 1;       sub[i + MAXN][1] = 1;     }     dfs(1, 1, q);     for (int i = 1; i <= q; i++) cout << res[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool flag = 0;   void sss(int &n, int &k) {     if (flag == 0)       n = n + k;     else if (flag == 1)       n = n - k;   }   void print(int &now, int k) {     k += 48;     if (k > now) {       while (k != now) {         printf( + );         now++;       }     } else if (k < now) {       while (k != now) {         printf( - );         now--;       }     }     printf( . );   }   int main() {     int i, k = 0, n = 0;     char c;     while ((c = getchar()) != EOF) {       if (c >=  0  && c <=  9 )         n = n * 10 + c -  0 ;       else if (c ==  + ) {         sss(k, n);         n = 0;         flag = 0;       } else if (c ==  - ) {         sss(k, n);         n = 0;         flag = 1;       }     }     sss(k, n);     n = 0;     int p = k, j;     while (p) {       p /= 10;       n++;     }     if (k == 0) n = 1;     int now = 0;     for (i = n; i >= 1; i--) {       p = k;       for (j = 1; j <= i - 1; j++) p /= 10;       p %= 10;       print(now, p);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     long long mod = 1000000007;     long long sols = 0, ti = 0;     for (int i = s.size() - 1; i >= 0; i--) {       if (s[i] ==  b )         ti++;       else {         sols += ti;         ti *= 2;         sols %= mod;         ti %= mod;       }     }     cout << sols << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   string str1, str2;   int main() {     scanf( %d %d , &n, &t);     cin >> str1 >> str2;     int l1 = n - t, l2 = n - t;     string ans;     ans.resize(n);     for (int i = 0; i < n; i++) {       if (l1 == 0 && l2 == 0) break;       if (str1[i] == str2[i]) {         ans[i] = str1[i];         l1--;         l2--;       }     }     if (l1 == 0 && l2 == 0) {       for (int i = 0; i < n; i++) {         if (ans[i] >=  a  && ans[i] <=  z ) continue;         for (int j =  a ; j <=  z ; j++) {           if (j != str1[i] && j != str2[i]) {             ans[i] = j;             break;           }         }       }     } else {       int state = 0;       for (int i = 0; i < n; i++) {         if (l1 == 0 && l2 == 0) {           if (ans[i] >=  a  && ans[i] <=  z ) continue;           for (int j =  a ; j <=  z ; j++) {             if (j != str1[i] && j != str2[i]) {               ans[i] = j;               break;             }           }         } else {           if (ans[i] >=  a  && ans[i] <=  z ) continue;           if (state == 0) {             l1--;             ans[i] = str1[i];             state = 1;           } else {             l2--;             ans[i] = str2[i];             state = 0;           }         }       }     }     if (l1 > 0 || l2 > 0)       printf( -1 n );     else       cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<long long int, long long int> &a,                  const pair<long long int, long long int> &b) {     if (a.first > b.first)       return (a.first < b.first);     else if (a.first == b.first)       return (a.second > b.second);     else       return true;   }   void solve() {     long long int n, c;     cin >> n >> c;     long long int a[n];     for (long long int i = 0; i < n; i++) cin >> a[i];     long long int cnt = 1;     for (long long int i = 1; i < n; i++) {       if (a[i] - a[i - 1] <= c)         cnt++;       else         cnt = 1;     }     if (n == 1)       cout << 1;     else       cout << cnt;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   int arr[N][N], n;   vector<int> adj[N], revAdj[N];   bool visited[N];   vector<int> vec;   void dfs(int v) {     visited[v] = 1;     for (auto i : revAdj[v])       if (!visited[i]) dfs(i);     vec.push_back(v);   }   void dfs2(int v) {     visited[v] = 1;     for (auto i : adj[v])       if (!visited[i]) dfs2(i);   }   void func() {     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> arr[i][j];         if (arr[i][j]) arr[i][j] = 1;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (j != i && arr[i][j]) {           revAdj[j].push_back(i);           adj[i].push_back(j);         }       }     }     for (int i = 1; i <= n; i++)       if (!visited[i]) dfs(i);     reverse(vec.begin(), vec.end());     for (int i = 1; i <= n; i++) visited[i] = 0;     int cnt = 0;     for (int i = 0; i < n; i++) {       if (!visited[vec[i]]) {         cnt++;         if (cnt > 1) {           cout <<  NO n ;           return;         }         dfs2(vec[i]);       }     }     cout <<  YES n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int ntc = 1;     for (int i = 1; i <= ntc; i++) {       func();     }     return 0;   }