#include <bits/stdc++.h>   using namespace std;   const int maxn = 200500;   int mapp[maxn], num[maxn], ans[maxn];   int main() {     int n, a, b, k;     while (cin >> n >> a >> b >> k) {       memset(ans, 0, sizeof(ans));       char c;       for (int i = 0; i < n; i++) {         cin >> c;         mapp[i] = c -  0 ;       }       int len = 0, pos = 0, tk = a;       int sum, sum2 = 0;       for (int i = 0; i < n; i++) {         if (0 == mapp[i]) len++;         if (1 == mapp[i] || i == n - 1) {           num[pos++] = len;           int kk = len / b;           if (kk <= tk)             tk -= kk;           else {             if (i == n - 1 && !mapp[i]) i++;             for (int j = tk + 1; j <= kk; j++) {               ans[i - len - 1 + j * b] = 1;               sum2++;             }             tk = 0;           }           len = 0;         }       }       sum = 1;       len = 0;       for (int i = 0; i < n; i++) {         if (0 == mapp[i]) len++;         if ((1 == mapp[i] || i == n - 1) && sum > 0) {           if (i == n - 1 && !mapp[i]) i++;           int kk = len / b;           for (int j = 1; j <= kk; j++) {             sum--;             if (!ans[i - len - 1 + j * b]) sum2++;             ans[i - len - 1 + j * b] = 1;             if (sum <= 0) break;           }           len = 0;         }       }       cout << sum2 << endl;       int flag = 0;       for (int i = 0; i < n; i++) {         if (!ans[i]) continue;         if (!flag)           flag = 1;         else           cout <<    ;         cout << i + 1;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, n1, n2, a[111111], sum1 = 0, sum2 = 0;   int main() {     cin >> n >> n1 >> n2;     for (int i = 0; i < n; i++) cin >> a[i];     if (n1 > n2) swap(n1, n2);     sort(a, a + n);     reverse(a, a + n);     int i = 0;     int m1 = n1;     int m2 = n2;     while (m1 != 0) {       sum1 += a[i];       i++;       m1--;     }     while (m2 != 0) {       sum2 += a[i];       i++;       m2--;     }     cout << fixed << setprecision(7) << (sum1 * 1.0 / n1) + (sum2 * 1.0 / n2);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110000;   int s1, s2;   int n, a[N];   int mi[17][N * 2], ma[17][N * 2];   int BB[N * 2];   int getmi(int l, int r) {     return min(mi[BB[r - l + 1]][l],                mi[BB[r - l + 1]][r - (1 << BB[r - l + 1]) + 1]);   }   int getma(int l, int r) {     return max(ma[BB[r - l + 1]][l],                ma[BB[r - l + 1]][r - (1 << BB[r - l + 1]) + 1]);   }   void init() {     for (int j = (int)0; j <= (int)16; j++)       for (int i = (int)(1 << j); i <= (int)(1 << (j + 1)) - 1; i++) BB[i] = j;     for (int i = (int)1; i <= (int)n; i++) {       mi[0][i] = ma[0][i] = a[i];     }     for (int j = (int)1; j <= (int)16; j++)       for (int i = (int)1; i <= (int)n - (1 << j) + 1; i++) {         mi[j][i] = min(mi[j - 1][i], mi[j - 1][i + (1 << (j - 1))]);         ma[j][i] = max(ma[j - 1][i], ma[j - 1][i + (1 << (j - 1))]);       }   }   bool f[N], g[N];   bool check(int S) {     for (int i = (int)0; i <= (int)n; i++) f[i] = g[i] = 0;     f[0] = g[0] = 1;     int maf, mag;     maf = mag = 0;     for (int i = (int)0; i <= (int)n - 1; i++) {       bool f1 = (i <= maf);       bool f2 = (i <= mag);       if (f1) {         int now = a[i];         if (i == 0) now = s1;         int l = i + 1;         int r = n;         int rt = 0;         while (l < r) {           int mid = (l + r) >> 1;           if ((abs(getma(i + 1, mid) - now) > S) ||               (abs(getmi(i + 1, mid) - now) > S))             r = mid;           else             l = mid + 1, rt = mid;         }         if (!(abs((getma(i + 1, l) - now) > S) ||               (abs(getmi(i + 1, l) - now) > S)))           rt = l;         mag = max(mag, rt);       }       if (f2) {         int now = a[i];         if (i == 0) now = s2;         int l = i + 1;         int r = n;         int rt = 0;         while (l < r) {           int mid = (l + r) >> 1;           if ((abs(getma(i + 1, mid) - now) > S) ||               (abs(getmi(i + 1, mid) - now) > S))             r = mid;           else             l = mid + 1, rt = mid;         }         if (!((abs(getma(i + 1, l) - now) > S) ||               (abs(getmi(i + 1, l) - now) > S)))           rt = l;         maf = max(maf, rt);       }     }     return (maf == n) || (mag == n);   }   int main() {     scanf( %d%d%d , &n, &s1, &s2);     for (int i = (int)1; i <= (int)n; i++) scanf( %d , &a[i]);     init();     int L = abs(s1 - s2);     int R = 1000000000;     int ans = 0;     while (L < R) {       int mid = (L + R) >> 1;       if (check(mid))         ans = mid, R = mid;       else         L = mid + 1;     }     if (check(L)) ans = L;     cout << ans << endl;     return 0;   }


#pragma GCC diagnostic ignored  -Wunused-parameter    #define _USE_MATH_DEFINES   #include <bits/stdc++.h>      using namespace std;      #define INP  solve    #define OUT  solve       const long long INF_LL = 8e18;   const int INF = 1e9 + 100;   const int MOD = 1e9 + 7;   //const int Base = 311;9   const long double EPS = 1e-10;   const int BLOCK = 1000;   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());      void open_file() {       #ifdef THEMIS           freopen(INP  .inp , r ,stdin);           freopen(OUT  .out , w ,stdout);       #endif // THEMIS   }      const int maxN = 3e5 + 100;      void sol(){       int n;       cin >> n;       vector<vector<int> > s(n);       map<int, int> id;       int M = 0;       int N = 0;       for (int i = 0; i < n; i++) {           int k;           cin >> k;           M += k;           s[i].resize(k);           for (int j = 0; j < k; j++) {               int x;               cin >> x;               if (!id.count(x)) id[x] = N++;               s[i][j] = id[x];           }           sort(s[i].begin(), s[i].end());       }       int block = (int)sqrt(M) + 1;       vector<int> large, small;       for (int i = 0; i < n; i++) {           if ((int)s[i].size() >= block) large.push_back(i);           else small.push_back(i);       }          int time = 0;       vector<int> visited(N, 0);       for (int u : large) {           time++;           for (int x : s[u]) visited[x] = time;           for (int v = 0; v < n; v++) {               if (u == v) continue;               int d = 0;               for (int val : s[v]) {                   d += visited[val] == time;                   if (d == 2) {                       cout << u + 1 <<     << v + 1 <<   n ;                       return;                   }               }           }       }          vector<vector<int> > adj(N);       for (int i = 0; i < n; i++) {           for (int x : s[i]) adj[x].push_back(i);       }       vector<int> last(N, -1), pos(N, -1);       for (int u = 0; u < N; u++) {           for (int i : adj[u]) {               if ((int)s[i].size() < block) {                   for (int j = 0; j < (int)s[i].size() && s[i][j] < u; j++) {                       int v = s[i][j];                       if (last[v] == u) {                           cout << pos[v] + 1 <<     << i + 1 <<   n ;                           return;                       }                       last[v] = u;                       pos[v] = i;                   }               }           }       }          cout << -1 <<   n ;   }      void solve() {       clock_t begin = clock();           int T;       cin >> T;       //T = 1;       int TestCase = 0;       while (T--) {           TestCase += 1;           //cout <<  Case #  << TestCase <<  :  <<    ;           //cout <<  Test   << TestCase <<  :  ;           sol();       }       clock_t end = clock();       cerr <<  Time:   << fixed << setprecision(10) << (double)(end - begin) / (double)CLOCKS_PER_SEC <<   n ;   }      int main(int argc,char *argv[]) {       ///srand(time(nullptr));       ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);       open_file();       solve();   }


#include <bits/stdc++.h>   #define int long long   #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)      using namespace std;      #define MOD         1000000007   #define nline         n            #define pb          push_back   #define mp          make_pair   #define fi          first   #define se          second   #define yes         cout<< YES n    #define no          cout<< NO n    #define vi          vector<int>   #define pii         pair<int,int>   #define sz(v)       v.size()   #define lcm(a,b)    (a*b)/(__gcd(a,b))   #define deb(x)      cout<<#x<<  =  <<x<<endl;   #define deb2(x,y)   cout<<#x<<  =  <<x<<   <<#y<<  =  <<y<<endl;   #define deb3(x,y,z) cout<<#x<<  =  <<x<<   <<#y<<  =  <<y<<   <<#z<<  =  <<z<<endl;   #define PR(a,n)     cout<<#a<<  =  ;for(int _=0;_<n;_++)cout<<a[_]<<   ;cout<<endl;   #define all(v)      v.begin(),v.end()   #define sortv(v)    sort(all(v));   #define sortrev(v)  sort(all(v),greater<int>())   #define maxHeap     priority_queue<int>   #define minHeap     priority_queue<int, vector<int>, greater<int>>      int pow(int a, int b, int m){       int ans = 1;       while(b){           if (b&1) ans = (ans*a) % m;           b /= 2;           a = (a*a) % m;       }       return ans;   }      int SumOfDigits(int x)   {       if (x < 0)return -1;       int s = 0;       while(x)       {           s += x%10;           x /= 10;       }           return s;   }      //lower_bound = first element >= val, end otherwise   //upper_bound = first element > val, end otherwise   //remember 2 pointer , binary search , bits method   //always look for the edges i.e max value ad min value possible      inline void solve()   {       int k,n,m;       cin>>k>>n>>m;       int a[n],b[m];       int cnt1 = 0,cnt2=0;       vi x,y;       for(int i=0;i<n;i++)       {           cin>>a[i];       }       for(int i=0;i<m;i++)       {           cin>>b[i];       }       vi ans;       int i=0,j=0;       int total = k;       while(1)       {           if(sz(ans)==n+m)           break;           if(a[i]==0 && i<n)           {               ans.pb(0);               total++;               i++;               continue;           }           else if(b[j]==0 && j<m)           {               ans.pb(0);               total++;               j++;               continue;           }           if(( a[i]<b[j] && (i<n && j<m) ) || (i<n && j>=m))           {               if(total>=a[i])               {                   ans.pb(a[i]);                   i++;               }               else                {                   cout<<-1<<nline;                   return;               }           }           else if( (a[i]>=b[j] && (j<m && i<n)) || (j<m && i>=n) )           {               if(total>=b[j])               {                   ans.pb(b[j]);                   j++;               }               else                {                   cout<<-1<<nline;                   return;               }           }       }       if(sz(ans)==n+m)       {           for(auto x:ans)           cout<<x<<   ;           cout<<nline;       }       else        cout<<-1<<nline;          return;   }      signed main()   {       boost;       int t=1;       cin>>t;       for(int tt=1;tt<=t;tt++)       {           // cout<< Case # <<tt<< :  ;           solve();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 503;   int m, n, vis[N][N];   vector<int> adj[N * N];   void init() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int id(int x, int y) { return (x - 1) * n + y; }   struct ds {     char c;     int x, y;   };   vector<ds> V;   void print(int u, int cc, int type = -1) {     int x = (u - 1) / n + 1, y = u - (x - 1) * n;     if (adj[u].size() == 0 && !type) {       V.push_back((ds){ R , x, y});       return;     }     V.push_back((ds){ B , x, y});     for (int v : adj[u]) print(v, cc, 0);     if (!type) V.push_back((ds){ D , x, y}), V.push_back((ds){ R , x, y});   }   int cc = 0;   queue<pair<int, int> > Q;   int hang[4] = {0, 0, 1, -1};   int cot[4] = {1, -1, 0, 0};   void build(int orgx, int orgy, int cc) {     vis[orgx][orgy] = cc;     Q.push({orgx, orgy});     while (Q.size()) {       int x = Q.front().first, y = Q.front().second;       Q.pop();       for (int t = 0; t <= (int)3; ++t) {         int nx = x + hang[t], ny = y + cot[t];         if (nx < 1 || nx > m || ny < 1 || ny > n || vis[nx][ny]) continue;         adj[id(x, y)].push_back(id(nx, ny));         vis[nx][ny] = cc;         Q.push({nx, ny});       }     }     print(id(orgx, orgy), cc);   }   void solve() {     cin >> m >> n;     for (int i = 1; i <= (int)m; ++i)       for (int j = 1; j <= (int)n; ++j) {         char c;         cin >> c;         if (c ==  # ) vis[i][j] = -1;       }     for (int i = 1; i <= (int)m; ++i)       for (int j = 1; j <= (int)n; ++j)         if (!vis[i][j]) build(i, j, ++cc);     cout << V.size() <<   n ;     for (ds it : V) cout << it.c <<     << it.x <<     << it.y <<   n ;   }   int main() {     init();     solve();   }


#include <bits/stdc++.h>   using namespace std;   queue<int> q;   const int maxn = 40007;   const int maxm = 800007;   const int inf = 0x7f7f7f7f;   struct E {     int u, v, w, cf;   } e[maxm];   int first[maxn], nt[maxm], ES = 1;   bool acc[maxm];   inline void addE(int u, int v, int w, int cf) {     e[++ES] = (E){u, v, w, cf};     nt[ES] = first[u];     first[u] = ES;     return;   }   inline void add(int u, int v, int w) {     addE(u, v, w, 1);     addE(v, u, w, 0);     return;   }   int tot;   int N, M;   int cur[maxn];   int dep[maxn];   int S, T;   bool BFS() {     memset(dep, 0, sizeof(dep));     dep[S] = 1;     q.push(S);     int u, v;     while (!q.empty()) {       u = q.front();       q.pop();       for (int i = first[u]; i; i = nt[i]) {         v = e[i].v;         if (e[i].cf > 0 && acc[i] && !dep[v]) {           dep[v] = dep[u] + 1;           q.push(v);         }       }     }     return dep[T] != 0;   }   int dfs(int u, int f) {     if (u == T) return f;     int v, d, sum = 0;     for (int &i = cur[u]; i; i = nt[i]) {       v = e[i].v;       if (dep[v] == dep[u] + 1 && e[i].cf > 0 && acc[i]) {         d = dfs(v, min(f, e[i].cf));         if (d > 0) {           sum += d;           f -= d;           e[i].cf -= d;           e[i ^ 1].cf += d;           if (f <= 0) return sum;         }       }     }     return sum;   }   int solve() {     int re = 0;     while (BFS()) {       memcpy(cur, first, sizeof(first));       re += dfs(S, inf);     }     return re;   }   bool check(int p) {     for (int i = 1; i <= ES; i++) {       if (e[i].w <= p)         acc[i] = true;       else         acc[i] = false;       e[i].cf = (i & 1) ^ 1;     }     return solve() == N;   }   int main() {     scanf( %d%d , &N, &M);     int u, v, w;     int L = 0, R = 0, mid, ans = -1;     for (int i = 1; i <= M; i++) scanf( %d%d%d , &u, &v, &w), add(u, v + N, w);     S = 0;     T = 2 * N + 1;     for (int i = 1; i <= N; i++) add(S, i, -1), add(i + N, T, -1);     R = 1e9;     while (L <= R) {       mid = L + R >> 1;       if (check(mid))         ans = mid, R = mid - 1;       else         L = mid + 1;     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6;   int Left[N], Right[N];   void pre(int node) {     if (node == -1) return;     pre(Left[node]);     printf( %d n , node + 1);     pre(Right[node]);   }   int main() {     memset(Left, -1, sizeof Left);     memset(Right, -1, sizeof Right);     string s;     cin >> s;     for (int i = 0; i + 1 < (int)s.size(); ++i)       if (s[i] ==  l )         Left[i] = i + 1;       else         Right[i] = i + 1;     pre(0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Edge {     int v, next;   } edge[2050];   int e, ft[2050];   int a, n, m;   bool rain[2050];   int x[2050], p[2050];   long long dp[2050][2050];   long long opt[2050];   int main() {     int l, r;     while (cin >> a >> n >> m) {       e = 0;       for (int i = 0; i <= a; i++) {         rain[i] = false;         for (int j = 0; j <= m; j++) dp[i][j] = (1ll << 60);         ft[i] = -1;       }       for (int i = 0; i < n; i++) {         scanf( %d%d , &l, &r);         while (l < r) {           rain[l++] = true;         }       }       for (int i = 0; i < m; i++) {         scanf( %d%d , x + i, p + i);         edge[e].v = i;         edge[e].next = ft[x[i]];         ft[x[i]] = e++;         if (x[i] == 0) dp[0][i] = 0;       }       dp[0][m] = 0;       p[m] = 0;       opt[0] = 0;       for (int i = 0; i < a; i++) {         for (int j = 0; j <= m; j++) {           if (dp[i][j] >= (1ll << 60)) continue;           if (j < m || !rain[i])             dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + p[j]);         }         opt[i + 1] = (1ll << 60);         for (int j = 0; j <= m; j++) opt[i + 1] = min(opt[i + 1], dp[i + 1][j]);         for (int j = ft[i + 1]; j != -1; j = edge[j].next) {           int idx = edge[j].v;           dp[i + 1][idx] = min(dp[i + 1][idx], opt[i + 1]);         }         dp[i + 1][m] = min(dp[i + 1][m], opt[i + 1]);       }       long long ans = -1;       if (opt[a] < (1ll << 60)) ans = opt[a];       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fastIO(void);   void decimal(int);   long long int modmulti(long long int, long long int, long long int);   long long int modadd(long long int, long long int, long long int);   long long int modpower(long long int, long long int, long long int);   long long int gcd(long long int, long long int);   long long int modinv(long long int, long long int);   int main() {     fastIO();     long long int n;     cin >> n;     long long int t1, t2;     vector<long long int> a(n);     multiset<long long int> b;     for (long long int i = 0; i < n; i++) cin >> a[i];     for (long long int i = 0; i < n; i++) {       cin >> t1;       b.insert(t1);     }     long long int c[n];     for (long long int i = 0; i < n; i++) {       t2 = n - a[i];       auto itr = b.lower_bound(n - a[i]);       if (itr == b.end()) itr = b.begin();       c[i] = (a[i] + *itr) % n;       b.erase(itr);     }     for (long long int i = 0; i < n; i++) cout << c[i] <<    ;     cout <<   n ;   }   void fastIO(void) {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   void decimal(int n) {     cout << fixed;     cout << setprecision(n);   }   long long int modmulti(long long int a, long long int b, long long int m) {     return ((a % m) * (b % m)) % m;   }   long long int modadd(long long int a, long long int b, long long int m) {     return ((a % m) + (b % m) + m) % m;   }   long long int modpower(long long int a, long long int n, long long int m) {     if (n == 0) return 1;     long long int result = 1;     while (n > 0) {       if (n % 2 == 1) result = modmulti(result, a, m);       a = modmulti(a, a, m);       n = n / 2;     }     return result;   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int modinv(long long int a, long long int m) {     return modpower(a, m - 2, m);   }


#include <bits/stdc++.h>   using namespace std;   const static int maxN = 1000;   struct {     int r, c;   } tooth[maxN];   int toothrow[maxN + 1];   int main() {     int n, m, k;     int i;     int ans;     while (scanf( %d%d%d , &n, &m, &k) != EOF) {       for (i = 0; i < n; i++) {         scanf( %d%d , &tooth[i].r, &tooth[i].c);       }       memset(toothrow, -1, sizeof(int) * (m + 1));       ans = 0;       for (i = 0; i < n; i++) {         if (toothrow[tooth[i].r] != -1) {           if (tooth[i].c < toothrow[tooth[i].r]) {             ans -= toothrow[tooth[i].r];             ans += tooth[i].c;             toothrow[tooth[i].r] = tooth[i].c;           }         } else {           ans += tooth[i].c;           toothrow[tooth[i].r] = tooth[i].c;         }       }       printf( %d n , min(ans, k));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace {   using namespace std;   template <typename flow_t = long long>   struct PushRelabel {     struct Edge {       int to, rev;       flow_t f, c;     };     vector<vector<Edge>> g;     vector<flow_t> ec;     vector<Edge*> cur;     vector<vector<int>> hs;     vector<int> H;     PushRelabel(int n) : g(n), ec(n), cur(n), hs(2 * n), H(n) {}     void add_edge(int s, int t, flow_t cap, flow_t rcap = 0) {       if (s == t) return;       Edge a = {t, (int)g[t].size(), 0, cap};       Edge b = {s, (int)g[s].size(), 0, rcap};       g[s].push_back(a);       g[t].push_back(b);     }     void add_flow(Edge& e, flow_t f) {       Edge& back = g[e.to][e.rev];       if (!ec[e.to] && f) hs[H[e.to]].push_back(e.to);       e.f += f;       e.c -= f;       ec[e.to] += f;       back.f -= f;       back.c += f;       ec[back.to] -= f;     }     flow_t max_flow(int s, int t) {       const int n = g.size();       ec.assign(n, 0);       cur.assign(n, nullptr);       hs.assign(2 * n, {});       H.assign(n, 0);       int v = g.size();       H[s] = v;       ec[t] = 1;       vector<int> co(2 * v);       co[0] = v - 1;       for (int i = 0; i < v; ++i) cur[i] = g[i].data();       for (auto& e : g[s]) add_flow(e, e.c);       if (hs[0].size())         for (int hi = 0; hi >= 0;) {           int u = hs[hi].back();           hs[hi].pop_back();           while (ec[u] > 0)             if (cur[u] == g[u].data() + g[u].size()) {               H[u] = 1e9;               for (auto& e : g[u])                 if (e.c && H[u] > H[e.to] + 1) H[u] = H[e.to] + 1, cur[u] = &e;               if (++co[H[u]], !--co[hi] && hi < v)                 for (int i = 0; i < v; ++i)                   if (hi < H[i] && H[i] < v) {                     --co[H[i]];                     H[i] = v + 1;                   }               hi = H[u];             } else if (cur[u]->c && H[u] == H[cur[u]->to] + 1)               add_flow(*cur[u], min(ec[u], cur[u]->c));             else               ++cur[u];           while (hi >= 0 && hs[hi].empty()) --hi;         }       return -ec[s];     }   };   template <typename flow_t = long long>   struct MaxFlow {     static constexpr flow_t INF = numeric_limits<flow_t>::max();     PushRelabel<flow_t> flow;     MaxFlow(const int n) : flow(n) {}     inline void add_arc(const int u, const int v, const flow_t capacity) {       flow.add_edge(u, v, capacity);     }     inline void add_edge(const int u, const int v, const flow_t capacity) {       flow.add_edge(u, v, capacity, capacity);     }     inline void add_infinite_arc(const int u, const int v) {       flow.add_edge(u, v, INF);     }     inline void add_infinite_edge(const int u, const int v) {       flow.add_edge(u, v, INF, INF);     }     flow_t solve(const int s, const int t) { return flow.max_flow(s, t); }   };   template <typename flow_t>   class MaxFlowLR {    private:     static constexpr flow_t INF = numeric_limits<flow_t>::max();     int n;     vector<flow_t> from_source, to_sink, outgoing_capacity;     vector<tuple<int, int, flow_t>> demands;     flow_t total_demand;     bool invalid;     PushRelabel<flow_t> flow;       public:     MaxFlowLR(const int n)         : n(n),           from_source(n),           to_sink(n),           outgoing_capacity(n),           total_demand(0),           invalid(false),           flow(n + 2) {}     void add_arc(const int u, const int v, const flow_t demand,                  const flow_t capacity) {       if (demand > capacity) {         invalid = true;         return;       }       if (u != v) {         from_source[v] += demand;         to_sink[u] += demand;         flow.add_edge(u, v, capacity - demand);         demands.push_back({u, v, demand});         total_demand += demand;         outgoing_capacity[u] += capacity;       }     }     flow_t solve(const int s, const int t) {       if (invalid) {         return -1;       }       const int source = n;       const int sink = n + 1;       for (int v = 0; v < n; ++v) {         if (from_source[v] != 0) {           flow.add_edge(source, v, from_source[v]);         }         if (to_sink[v] != 0) {           flow.add_edge(v, sink, to_sink[v]);         }       }       flow.add_edge(t, s, INF);       const flow_t flow_aux = flow.max_flow(source, sink);       if (s != t) {         flow.g[s].pop_back();         flow.g[t].pop_back();       }       for (int v = n - 1; v >= 0; --v) {         if (to_sink[v] != 0) {           flow.g[sink].pop_back();           flow.g[v].pop_back();         }         if (from_source[v] != 0) {           flow.g[source].pop_back();           flow.g[v].pop_back();         }       }       if (flow_aux != total_demand) return -1;       const flow_t flow_new = flow.max_flow(s, t);       flow_t total_flow = outgoing_capacity[s];       for (const auto& e : flow.g[s]) total_flow -= e.c;       return total_flow;     }     vector<map<int, flow_t>> retrieve() {       vector<map<int, flow_t>> ans(n);       for (int u = 0; u < n; ++u)         for (const auto& e : flow.g[u])           if (e.f > 0) ans[u][e.to] += e.f, ans[e.to][u] -= e.f;       for (auto t : demands)         if (get<2>(t) > 0)           ans[get<0>(t)][get<1>(t)] += get<2>(t),               ans[get<1>(t)][get<0>(t)] -= get<2>(t);       return ans;     }   };   }  // namespace   inline int divceil(const int x, const int y) {     const int q = x / y;     return q * y == x ? q : q + 1;   }   inline long long divceil(const long long x, const long long y) {     const long long q = x / y;     return q * y == x ? q : q + 1;   }   map<int, int> coordinate_compression(vector<int> v) {     sort(v.begin(), v.end());     const int n = unique(v.begin(), v.end()) - v.begin();     map<int, int> compr;     for (int i = 0; i < n; ++i) compr[v[i]] = i;     return compr;   }   map<long long, int> coordinate_compression(vector<long long> v) {     sort(v.begin(), v.end());     const int n = unique(v.begin(), v.end()) - v.begin();     map<long long, int> compr;     for (int i = 0; i < n; ++i) compr[v[i]] = i;     return compr;   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     int r, b;     scanf( %d%d , &r, &b);     bool flip = false;     if (r >= b) {       flip = true;       swap(r, b);     }     vector<std::pair<int, int>> original_points;     vector<int> allx, ally;     for (int i = 0; i < n; ++i) {       int x, y;       scanf( %d%d , &x, &y);       original_points.emplace_back(x, y);       allx.push_back(x);       ally.push_back(y);     }     map<int, int> comprx = coordinate_compression(allx),                   compry = coordinate_compression(ally);     const int xlines = comprx.rbegin()->second + 1;     const int ylines = compry.rbegin()->second + 1;     vector<int> xdeg(xlines), ydeg(ylines);     vector<int> xcons(xlines), ycons(ylines);     vector<std::pair<int, int>> points;     for (const std::pair<int, int> p : original_points) {       const int xx = comprx[p.first];       const int yy = compry[p.second];       points.emplace_back(xx, yy);       ++xdeg[xx];       ++ydeg[yy];       ++xcons[xx];       ++ycons[yy];     }     while (m--) {       int t, l, d;       scanf( %d%d%d , &t, &l, &d);       if (t == 1) {         const auto it = comprx.find(l);         if (it != comprx.end()) xcons[it->second] = min(xcons[it->second], d);       } else {         const auto it = compry.find(l);         if (it != compry.end()) ycons[it->second] = min(ycons[it->second], d);       }     }     MaxFlowLR<long long> dinic(2 + xlines + ylines);     const int source = 0;     const int sink = 1;     const auto get_xvert = [&](const int i) { return 2 + i; };     const auto get_yvert = [&](const int i) { return 2 + xlines + i; };     for (int i = 0; i < xlines; ++i) {       const int L = divceil(xdeg[i] - xcons[i], 2);       const int R = (xdeg[i] + xcons[i]) / 2;       dinic.add_arc(source, get_xvert(i), L, R);     }     for (int i = 0; i < ylines; ++i) {       const int L = divceil(ydeg[i] - ycons[i], 2);       const int R = (ydeg[i] + ycons[i]) / 2;       dinic.add_arc(get_yvert(i), sink, L, R);     }     for (const std::pair<int, int> p : points)       dinic.add_arc(get_xvert(p.first), get_yvert(p.second), 0, 1);     long long flow_value = dinic.solve(source, sink);     if (flow_value == -1) {       puts( -1 );       return 0;     }     long long ans = flow_value * r + (n - flow_value) * b;     cout << ans << endl;     map<std::pair<int, int>, int> red;     auto flow = dinic.retrieve();     for (int i = 0; i < xlines; ++i) {       const int x = get_xvert(i);       for (const auto p : flow[x]) {         const int j = p.first - 2 - xlines;         const int f = p.second;         if (j >= 0) red[std::pair<int, int>(i, j)] += f;       }     }     for (const std::pair<int, int> p : points) {       int& v = red[p];       if (v) {         v--;         putchar(flip ?  b  :  r );       } else {         putchar(flip ?  r  :  b );       }     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 5;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void solve() {     vector<long long> v(14);     long long curr_score = 0;     long long o = 0;     for (int i = 0; i < 14; i++) {       cin >> v[i];     }     for (int i = 0; i < 14; i++) {       vector<long long> t(14);       for (int k = 0; k < 14; k++) {         t[k] = v[k];       }       long long p = v[i] / 14, q = v[i] % 14;       t[i] = 0;       for (int j = 0; j < 14; j++) {         t[j] += p;       }       long long j = i + 1;       while (q--) {         if (j == 14) {           j = 0;         }         t[j++]++;       }       long long cnt = 0;       for (int i = 0; i < 14; i++) {         if (t[i] % 2 == 0) {           cnt += t[i];         }       }       curr_score = max(curr_score, cnt);     }     cout << curr_score <<   n ;   }   int main() {     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     long long a, b, cost;     edge(long long x, long long y, long long z) : a(x), b(y), cost(z){};   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, k, p;     cin >> n >> k >> p;     vector<long long> v(n), a(k);     for (long long i = 0; i < (long long)(n); i++) cin >> v[i];     for (long long i = 0; i < (long long)(k); i++) cin >> a[i];     sort((v).begin(), (v).end());     sort((a).begin(), (a).end());     long long ans = INT_MAX;     for (long long i = (long long)(0); i <= (long long)(k - n); i++) {       long long res = INT_MIN;       for (long long j = (long long)(0); j <= (long long)(n - 1); j++) {         res = max(res, abs(v[j] - a[i + j]) + abs(a[i + j] - p));       }       ans = min(ans, res);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long ans;   int n, i, j, k, u, v;   int size[80011], fa[80011];   vector<int> e[80011];   void dfs(int now) {     unsigned long long o = 0;     size[now] = 1;     for (int i = 0; i < e[now].size(); i++)       if (e[now][i] != fa[now]) {         fa[e[now][i]] = now;         dfs(e[now][i]);         o += (unsigned long long)size[now] * size[e[now][i]];         size[now] += size[e[now][i]];       }     ans -= o * (o + 2LL * size[now] * (n - size[now]));   }   int main() {     scanf( %d , &n);     for (i = 1; i < n; i++) {       scanf( %d%d , &u, &v);       e[u].push_back(v);       e[v].push_back(u);     }     ans = (n - 1LL) * n / 2;     ans *= ans;     dfs(1);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ans = 0;   string str;   int n;   int main() {     ios::sync_with_stdio(false);     cin >> n;     cin >> str;     for (int i = 0; i < n; ++i) ans += (str[i] -  0 ) & 1 ? 0 : i + 1;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline void normal(long long int &a) {     a %= 1000000007;     (a < 0) && (a += 1000000007);   }   inline long long int modMul(long long int a, long long int b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a * b) % 1000000007;   }   inline long long int modAdd(long long int a, long long int b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a + b) % 1000000007;   }   inline long long int modSub(long long int a, long long int b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     a -= b;     normal(a);     return a;   }   inline long long int modPow(long long int b, long long int p) {     long long int r = 1;     while (p) {       if (p & 1) r = modMul(r, b);       b = modMul(b, b);       p >>= 1;     }     return r;   }   inline long long int modInverse(long long int a) {     return modPow(a, 1000000007 - 2);   }   inline long long int modDiv(long long int a, long long int b) {     return modMul(a, modInverse(b));   }   inline bool isInside(pair<int, int> p, long long int n, long long int m) {     return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < m);   }   inline bool isInside(pair<int, int> p, long long int n) {     return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < n);   }   inline bool isSquare(long long int x) {     long long int s = sqrt(x);     return (s * s == x);   }   inline bool isFib(long long int x) {     return isSquare(5 * x * x + 4) || isSquare(5 * x * x - 4);   }   inline bool isPowerOfTwo(long long int x) {     return ((1LL << (long long int)log2(x)) == x);   }   template <typename first, typename second>   ostream &operator<<(ostream &os, const pair<first, second> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  } ;   }   template <typename T>   ostream &operator<<(ostream &os, const set<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename first, typename second>   ostream &operator<<(ostream &os, const map<first, second> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << it->first <<   =   << it->second;     }     return os <<  ] ;   }   clock_t tStart = clock();   void faltu() { cerr << endl; }   template <typename T>   void faltu(T a[], int n) {     for (int i = 0; i < n; ++i) cerr << a[i] <<    ;     cerr << endl;   }   template <typename T, typename... hello>   void faltu(T arg, const hello &...rest) {     cerr << arg <<    ;     faltu(rest...);   }   const long long int INF = 0x3f3f3f3f3f3f3f3f;   const long double EPS = 1e-9;   const int inf = 0x3f3f3f3f;   const int mx = (int)2e5 + 9;   long long int n, m, a, b, u, t, i, j, d, cs = 0, counT = 0, k, ans = 0, l = 0,                                            sum1 = 0, sum = 0, Max, Min, num;   int _time, idx;   set<long long int> vc[mx];   int st[mx], en[mx], pos[mx], arr[mx];   void dfs(int node, int par = 0) {     st[node] = ++_time;     arr[node] = ++idx;     pos[idx] = node;     for (auto next : vc[node]) {       if (next == par) continue;       dfs(next, node);     }     en[node] = _time;   }   int main() {     cin >> n >> m;     for (i = 2; i <= n; i++) {       cin >> a;       vc[i].insert(a);       vc[a].insert(i);     }     dfs(1);     while (m--) {       cin >> u >> k;       if (arr[u] + k - 1 > n) {         cout << -1 <<   n ;         continue;       }       int v = pos[arr[u] + k - 1];       if (st[u] <= st[v] && en[u] >= en[v])         cout << v <<   n ;       else         cout << -1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long test(vector<long long>& h, int l, int r) {     long long ret = 0;     for (int i = l; i < r; i++) {       long long tmp = 0;       for (int j = i + 1; j < r; j++) {         tmp = max(tmp, abs(h[j] - h[j - 1]));         ret += tmp;       }     }     return ret;   }   int main() {     int n, q;     cin >> n >> q;     vector<long long> h(n);     for (int i = 0; i < n; i++) {       scanf( %I64d , &h[i]);     }     while (q--) {       int l, r;       scanf( %d%d , &l, &r);       l--;       long long ans = 0;       long long last = 0;       deque<pair<long long, int>> dq;       for (int i = l + 1; i < r; i++) {         long long tmp = abs(h[i] - h[i - 1]);         long long len = 1;         while (dq.size() && dq.back().first <= tmp) {           auto val = dq.back();           dq.pop_back();           len += val.second;           last -= val.first * val.second;         }         dq.push_back({tmp, len});         last += tmp * len;         ans += last;       }       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cont = 0;   template <typename T, typename F>   struct AHO_CORASICK {     const long long ALPHA = 26;     long long cv(F c) { return c -  0 ; }     vector<map<long long, long long>> tgo;     vector<long long> link;     vector<long long> ch;     vector<long long> p;     vector<bool> flag;     long long sz = 0;     long long size() { return sz; }     void new_node(long long tp, long long tch) {       sz++;       tgo.emplace_back();       link.push_back(0);       ch.push_back(tch);       p.push_back(tp);       flag.push_back(false);     }     AHO_CORASICK() {       new_node(0, 0);       new_node(1, 0);     }     void add_string(T cad) {       long long pt = 1;       for (long long i = 0; i < cad.size(); i++) {         long long tch = cv(cad[i]);         if (tgo[pt][tch] == 0) {           tgo[pt][tch] = sz;           new_node(pt, tch);         }         pt = tgo[pt][tch];       }       flag[pt] = true;     }     long long get_link(long long nodo) {       if (link[nodo] == 0) {         if (nodo == 1 || p[nodo] == 1)           link[nodo] = 1;         else           link[nodo] = go(get_link(p[nodo]), ch[nodo]);         flag[nodo] = flag[nodo] | flag[link[nodo]];       }       return link[nodo];     }     long long go(long long nodo, F tch) { return go(nodo, cv(tch)); }     long long go(long long nodo, long long tch) {       if (tgo[nodo][tch] == 0) {         tgo[nodo][tch] = (nodo == 1) ? 1 : go(get_link(nodo), tch);       }       return tgo[nodo][tch];     }   };   long long X;   vector<string> bf;   string temp;   void fff(long long sum) {     if (sum > X) return;     for (long long i = 0; i < temp.size(); i++) {       long long s = 0;       for (long long j = i; j < temp.size(); j++) {         s += temp[j] -  0 ;         if (s != X && X % s == 0) return;       }     }     if (sum == X) {       bf.push_back(temp);       return;     }     for (long long i = 1; i <= 9; i++) {       temp += (i +  0 );       fff(sum + i);       temp.pop_back();     }   }   const long long MAXN = 1010;   long long dp[MAXN][MAXN * 5];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (long long i = 0; i < MAXN; i++) {       for (long long j = 0; j < 5 * MAXN; j++) {         dp[i][j] = 1e9;       }     }     string cad;     cin >> cad;     cin >> X;     long long N = cad.size();     fff(0);     AHO_CORASICK<string, char> ac;     for (auto x : bf) {       ac.add_string(x);     }     dp[0][1] = 0;     for (long long i = 0; i < N; i++) {       for (long long j = 1; j < ac.size(); j++) {         long long pt = ac.go(j, cad[i]);         if (ac.flag[pt] == false) dp[i + 1][pt] = min(dp[i + 1][pt], dp[i][j]);         dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);       }     }     long long res = 1e9;     for (long long i = 0; i < ac.sz; i++) {       res = min(res, dp[N][i]);     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char name[5][10] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard };     int n, c = 5;     scanf( %d , &n);     while (n > c) {       n -= c;       c *= 2;     }     printf( %s n , name[(n - 1) / (c / 5)]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     map<string, pair<string, long long>> best;     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       string name;       long long score;       cin >> name >> score;       best[name].second = max(best[name].second, score);     }     for (auto itr = best.begin(); itr != best.end(); itr++) {       long long count = 0;       for (auto i = best.begin(); i != best.end(); i++) {         if (itr->second.second >= i->second.second) count++;       }       double percent = ((double)count / best.size() * 1.0) * 100.0;       if (percent < 50.0)         itr->second.first =  noob ;       else if (percent >= 50.0 && percent < 80.0)         itr->second.first =  random ;       else if (percent >= 80.0 && percent < 90.0)         itr->second.first =  average ;       else if (percent >= 90.0 && percent < 99.0)         itr->second.first =  hardcore ;       else         itr->second.first =  pro ;     }     cout << best.size() << endl;     for (auto x : best) {       cout << x.first <<     << x.second.first << endl;     }   }   int32_t main() {     long long testcases = 1;     while (testcases--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void run_case() {     string A, B, C, D;     cin >> A;     cin >> B;     C = (A + B);     sort(C.begin(), C.end());     cin >> D;     sort(D.begin(), D.end());     if (C == D)       cout <<  YES n ;     else       cout <<  NO n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long T = 1;     while (T--) run_case();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:200000000 )   const double EPS = 1E-9;   const int INF = 1000000000;   const long long INF64 = (long long)1E18;   const double PI = 3.1415926535897932384626433832795;   char s[2100][2100];   int ans[2100];   int main() {     int n, m, k;     scanf( %d%d%d n , &n, &m, &k);     for (int i = 0; i < (int)(n); i++) gets(s[i]);     for (int i = 0; i < (int)(n); i++)       for (int j = 0; j < (int)(m); j++) {         if (s[i][j] ==  .  || s[i][j] ==  D ) continue;         if (s[i][j] ==  U ) {           if (i % 2 == 1) continue;           ans[j]++;           continue;         }         int nj;         if (s[i][j] ==  L )           nj = j - i;         else           nj = j + i;         if (0 <= nj && nj < m) ans[nj]++;       }     for (int i = 0; i < (int)(m); i++) printf(i ?   %d  :  %d , ans[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int32_t x;     scanf( %d , &x);     return x;   }   inline long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   inline string get() {     char ch[2000010];     scanf( %s , ch);     return ch;   }   const int MAX_LG = 21;   const int maxn = 2e3 + 10;   const int maxm = 1e5 + 10;   const int base = 29;   const int mod = 1e9 + 7;   const int INF = 1e9 + 100;   const int SQ = 317;   int Cmp;   int mark[maxn];   int v[maxm], u[maxm], deg[maxn], has[maxn][maxn], e[maxm];   int vc[5];   inline void dfs(int node, int id) {     int edgeid = has[node][vc[id]] - 1;     if (edgeid < 0) return;     int nxt = v[edgeid] == node ? u[edgeid] : v[edgeid];     has[node][vc[id]] = 0;     has[nxt][vc[id]] = 0;     dfs(nxt, id ^ 1);     has[node][vc[id ^ 1]] = edgeid + 1;     has[nxt][vc[id ^ 1]] = edgeid + 1;     e[edgeid] = vc[id ^ 1];   }   int res;   int32_t main() {     int a = in(), b = in(), m = in();     for (int i = 0; i < m; i++) {       v[i] = in(), u[i] = in() + a;       deg[v[i]]++;       deg[u[i]]++;       res = max(res, max(deg[v[i]], deg[u[i]]));     }     for (int i = 0; i < m; i++) {       bool fl = false;       for (int col = 1; col <= res; col++) {         if (has[v[i]][col] == 0 && has[u[i]][col] == 0 && col <= res) {           has[v[i]][col] = has[u[i]][col] = i + 1;           e[i] = col;           fl = true;           break;         }       }       if (!fl) {         Cmp++;         for (int col = 1; col <= res; col++) {           if (has[v[i]][col] && !has[u[i]][col]) vc[0] = col;           if (has[v[i]][col] == 0 && has[u[i]][col]) vc[1] = col;         }         dfs(v[i], 0);         has[v[i]][vc[0]] = has[u[i]][vc[0]] = i + 1;         e[i] = vc[0];       }     }     cout << res <<   n ;     for (int i = 0; i < m; i++) cout << e[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int f[100005], b[100005], fn[100005], bn[100005], fm, bm;   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       scanf( %d , f + i);       ++fn[f[i]];       fm = max(fm, f[i]);     }     for (int i = 0; i < m; ++i) {       scanf( %d , b + i);       ++bn[b[i]];       bm = max(bm, b[i]);     }     if (fm < bm) {       printf( Impossible n );     } else {       bool ans = true;       bool chong = false;       for (int i = 1; i <= bm; ++i) {         if (bn[i] > 0) {           if (fn[i] == 0) {             ans = false;           } else if (fn[i] > 1) {             chong = true;           }         }       }       if (!ans) {         printf( Impossible n );       } else {         if (chong) {           printf( Ambiguity n );         } else {           printf( Possible n );           for (int i = 0; i < n; ++i) {             fn[f[i]] = i + 1;           }           for (int i = 0; i < m; ++i) {             printf( %d  , fn[b[i]]);           }           printf(  n );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     x = 0;     char ch = getchar(), flag = 0;     while ((ch >  9  || ch <  0 ) && ch !=  - ) ch = getchar();     if (ch ==  - ) flag = 1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     if (flag) x = -x;   }   inline void read(int &x, int &y) { read(x), read(y); }   inline void read(int &x, int &y, int &z) { read(x), read(y), read(z); }   const int N = 20005, inf = 1e9 + 7, mod = 20000;   int n, m, up[N], cup[N], cnt, si[N], num[N], dep[N], use[N];   vector<int> g[N], f[N], s[N];   bool vis[N];   struct node {     int to, next, v;   } p[N << 4];   int S, T, head[N], st[N], tot, ans;   inline void add(int x, int y, int z) {     p[tot].to = y;     p[tot].next = head[x];     p[tot].v = z;     head[x] = tot++;     p[tot].to = x;     p[tot].next = head[y];     p[tot].v = 0;     head[y] = tot++;   }   queue<int> q;   inline bool bfs(int x) {     memset(st, 0, sizeof(st));     st[x] = 1;     q.push(x);     while (!q.empty()) {       int x = q.front();       q.pop();       for (int i = head[x]; ~i; i = p[i].next) {         int y = p[i].to, v = p[i].v;         if (!v || st[y]) continue;         st[y] = st[x] + 1;         q.push(y);       }     }     return st[T] > 0;   }   inline int dfs(int x, int f) {     int r = 0;     if (x == T) return f;     for (int i = head[x]; ~i; i = p[i].next) {       int y = p[i].to, v = p[i].v, tmp;       if (!v || st[y] != st[x] + 1) continue;       tmp = dfs(y, min(f - r, v));       r += tmp;       p[i].v -= tmp;       p[i ^ 1].v += tmp;       if (r == f) break;     }     if (!r) st[x] = -1;     return r;   }   inline void dinic() {     int t;     while (bfs(S))       while (t = dfs(S, inf)) ans += t;     printf( %d n , ans);   }   inline void Add(int x, int y, int z) {     g[x].push_back(y);     f[x].push_back(z);     g[y].push_back(x);     f[y].push_back(z);     vis[x] = vis[y] = 1;   }   inline void Dfs(int x, int fa) {     vis[x] = 1;     for (int i = 0; i < g[x].size(); i++) {       int y = g[x][i], z = f[x][i];       if (y == fa) continue;       if (vis[y] && dep[y] < dep[x]) {         cnt++;         s[cnt].push_back(z);         si[z]--;         int k = x, o = 0;         while (k != y) {           si[cup[k]]--;           s[cnt].push_back(cup[k]);           k = up[k];         }         continue;       }       if (vis[y]) continue;       cup[y] = z;       up[y] = x;       dep[y] = dep[x] + 1;       Dfs(y, x);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int x, y, z;       scanf( %d%d%d , &x, &y, &z);       Add(x, y, z);       si[z]++;       use[z]++;     }     for (int i = 1; i <= n; i++)       if (vis[i]) {         memset(vis, 0, sizeof(vis));         Dfs(i, -1);         break;       }     memset(head, -1, sizeof(head));     S = 0;     T = cnt + 1;     for (int i = 1; i <= m; i++)       if (!si[i] && use[i])         T++;       else if (use[i])         ans++;     int CNT = cnt;     for (int i = 1; i <= m; i++)       if (!si[i] && use[i]) add(num[i] = ++CNT, T, 1);     for (int i = 1; i <= cnt; i++) {       bool isok = 1;       for (int j = 0; j < s[i].size(); j++)         if (si[s[i][j]]) isok = 0;       add(S, i, s[i].size() - 1);       for (int j = 0; j < s[i].size(); j++)         if (num[s[i][j]]) add(i, num[s[i][j]], 1);     }     dinic();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[100100], sir[100100];   int viz[100100], mi, ma, t, n, freq[100100], fre[100100];   int prim(int x) {     for (int i = 2; i <= x; ++i) {       if (i * i > x) return 1;       if (x % i == 0) return 0;     }     return 1;   }   int main() {     cin >> (s + 1);     n = strlen(s + 1);     for (int i = 2; i <= n; ++i) {       if (prim(i)) {         mi = ++t;         for (int j = 1; j <= n; ++j) {           if (i * j > n) break;           if (viz[i * j]) mi = min(mi, viz[i * j]);         }         if (mi != t) --t;         for (int j = 1; j <= n; ++j) {           if (i * j > n) break;           viz[i * j] = mi;         }       }     }     for (int i = 1; i <= n; ++i) freq[viz[i]]++;     for (int i = 1; i <= n; ++i) fre[s[i]]++;     for (int i = 1; i <= t; ++i) {       ma = 0;       for (int j =  a ; j <=  z ; ++j)         if (fre[j] > fre[ma]) {           ma = j;         }       if (fre[ma] >= freq[i]) {         fre[ma] -= freq[i];         for (int j = 1; j <= n; ++j)           if (viz[j] == i) s[j] = ma;       } else {         cout <<  NO ;         return 0;       }     }     t = 0;     for (int j =  a ; j <=  z ; ++j)       while (fre[j]) {         --fre[j];         sir[++t] = j;       }     for (int i = 1; i <= n; ++i)       if (!viz[i]) {         s[i] = sir[t--];       }     cout <<  YES n ;     cout << (s + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n;     cin >> n;     long long int a[n];     long long int q = 0, p = 0, z = 0;     for (int i = 0; i < n; ++i) {       cin >> a[i];       if (a[i] < 0)         q++;       else if (a[i] > 0)         p++;       else         z++;     }     long long int m1, m2, m3, m4, m5;     sort(a, a + n);     m1 = a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4] * a[n - 5];     m2 = a[n - 1] * a[n - 2] * a[n - 3] * a[0] * a[1];     m3 = a[n - 1] * a[0] * a[1] * a[2] * a[3];     long long int ans = max(m1, max(m2, m3));     cout << ans <<   n ;   }   int main() {     ;     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int t;     cin >> t;     for (int i = 1; i <= t; ++i) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 10, inf = 1e9;   int r[N], c[N], dist[N];   bool seen[N] = {0};   int main() {     int row, col, k;     cin >> row >> col >> k;     for (int i = 1; i <= k; i++) {       cin >> r[i] >> c[i];       dist[i] = inf;       if (r[i] == 1 and c[i] == 1) dist[i] = 0;     }     dist[0] = inf;     while (1) {       int cur = 0;       for (int i = 1; i <= k; i++) {         if (!seen[i] and dist[cur] > dist[i]) {           cur = i;         }       }       if (!cur) break;       seen[cur] = 1;       for (int i = 1; i <= k; i++) {         if (seen[i]) continue;         if (abs(r[i] - r[cur]) > 2 and abs(c[i] - c[cur]) > 2) continue;         int temp = 1;         if ((abs(r[i] - r[cur]) + abs(c[i] - c[cur])) == 1) temp = 0;         if (dist[cur] + temp < dist[i]) dist[i] = temp + dist[cur];       }     }     int ans = inf;     for (int i = 1; i <= k; i++) {       if (r[i] == row and c[i] == col)         ans = min(ans, dist[i]);       else if (r[i] >= row - 1 or c[i] >= col - 1)         ans = min(ans, dist[i] + 1);     }     if (ans == inf)       cout << -1;     else       cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long int _sieve_size;   bitset<10000010> bs;   vector<int> primes;   void sieve(long long int upperbound) {     _sieve_size = upperbound + 1;     bs.set();     bs[0] = bs[1] = 0;     for (long long int i = 2; i <= _sieve_size; i++)       if (bs[i]) {         for (long long int j = i * i; j <= _sieve_size; j += i) bs[j] = 0;         primes.push_back((int)i);       }   }   bool isPrime(long long int N) {     if (N <= _sieve_size) return bs[N];     for (int i = 0; i < (int)primes.size(); i++)       if (N % primes[i] == 0) return false;     return true;   }   int main() {     int h1, m1, h2, m2, oh, om;     char tmp;     cin >> h1 >> tmp >> m1;     cin >> h2 >> tmp >> m2;     om = m1 - m2;     if (om < 0) {       om += 60;       h1 -= 1;     }     oh = h1 - h2;     if (oh < 0) {       oh += 24;     }     if (oh < 10) {       cout << 0 << oh;     } else       cout << oh;     cout << tmp;     if (om < 10) {       cout << 0 << om;     } else       cout << om;     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4786)   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   char str[1000005];   int Z[1000005], n;   void Zfunction() {     int L, R, r, i, j, k;     L = 0;     R = 0;     Z[0] = 0;     for (i = 1; i < n; i++) {       if (i > R) {         L = i;         R = i;         while (R < n && str[R - L] == str[R]) R++;         Z[i] = R - L;         R--;       } else {         r = i - L;         if (Z[r] < R - i + 1)           Z[i] = Z[r];         else {           L = i;           while (R < n && str[R - L] == str[R]) R++;           Z[i] = R - L;           R--;         }       }     }   }   int max(int a, int b) {     if (a > b) return a;     return b;   }   int main() {     int i, j, k, maxr, res;     while (scanf( %s , str) != EOF) {       n = strlen(str);       maxr = 0;       res = 0;       Zfunction();       for (i = 0; i < n; i++) {         if (Z[i] == n - i) {           if (maxr >= n - i) {             res = n - i;             break;           }         }         maxr = max(maxr, Z[i]);       }       if (res == 0 || n <= 2)         printf( Just a legend n );       else {         for (i = 0; i < res; i++) printf( %c , str[i]);         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 1, M = 100;   int n, q;   bitset<N> sq[M];   vector<int> on[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (int x = 0, qwerty = M; x < qwerty; x++) sq[x][0] = 1;     for (int x = 0, qwerty = q; x < qwerty; x++) {       int l, r, v;       cin >> l >> r >> v;       l--;       r--;       if (l / M == r / M) {         for (int i = l; i <= r; i++) on[i].push_back(v);         continue;       }       for (int i = (l / M) + 1; i < r / M; i++) {         sq[i] |= (sq[i] << v);       }       for (int i = l; i < M * ((l / M) + 1); i++) on[i].push_back(v);       for (int i = M * (r / M); i <= r; i++) on[i].push_back(v);     }     bitset<N> ans;     for (int x = 0, qwerty = n; x < qwerty; x++) {       int pos = x / M;       bitset<N> tmp = sq[pos];       for (int v : on[x]) tmp |= tmp << v;       ans |= tmp;     }     vector<int> aa;     for (int x = 1, qwerty = n + 1; x < qwerty; x++)       if (ans[x]) aa.push_back(x);     cout << ((int)(aa).size()) <<   n ;     for (int x = 0, qwerty = ((int)(aa).size()); x < qwerty; x++)       cout << aa[x] <<    n [x + 1 == ((int)(aa).size())];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<string> v;     string a;     while (n--) {       cin >> a;       v.push_back(a);     }     int cot = 0;     bool p = 1;     int ans = 1000000;     string c;     for (int j = 0; j < v.size(); j++) {       c = v[j];       cot = 0;       for (int i = 0; i < v.size(); i++) {         if (c == v[i] || i == j)           continue;         else {           int ncot = 0;           string b = v[i];           while (c != b) {             ncot++;             char d = b[0];             b.erase(b.begin());             b.push_back(d);             if (ncot > b.length()) {               p = 0;               break;             }           }           if (p == 0)             break;           else             cot += ncot;         }       }       if (p == 0) break;       if (cot < ans) {         ans = cot;       }     }     if (p)       cout << ans << endl;     else       cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline bool equal(double _x, double _y) {     return _x > _y - 1e-9 && _x < _y + 1e-9;   }   char s[300];   int mx, cnt, sum, ans;   bool OK(char c) {     if (c >=  a  && c <=  z  || c >=  A  && c <=  Z ) return 1;     return 0;   }   int main() {     int n;     scanf( %d , &n);     scanf( %s , s);     for (int l = 0, r = 0; l < n; l = r) {       if (s[l] ==  ( ) {         while (s[r] !=  ) ) r++;         for (int i = l + 1; i < r; i++) {           if (OK(s[i]) && !OK(s[i - 1])) {             cnt++;           }         }         for (int i = l + 1; i < r; i++) s[i] =  _ ;       }       r++;     }     for (int i = 0; i < n; i++) {       if (OK(s[i]))         sum++;       else         sum = 0;       ans = max(ans, sum);     }     printf( %d %d n , ans, cnt);   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     if (b == 0) return 1;     if (b % 2 == 0) return power(a * a % 1000000007, b / 2);     return a * power(a, b - 1) % 1000000007;   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long a[100000];   int main() {     long long n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     long long ans = 0;     for (int i = 0; i < n / 2; i++) {       ans += a[i] * a[n - i - 1] * 2;       ans %= 10007;     }     if (n % 2) ans += a[n / 2] * a[n / 2];     ans %= 10007;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 101;   short dp[N][N][N * N], a[N], ans;   int n;   short calc(int u, int k, int sum) {     if (u == N) return !sum && !k;     short &ref = dp[u][k][sum];     if (ref != -1) return ref;     ref = 0;     for (short i = 0; i <= a[u] && sum - u * i >= 0 && i <= k; i++)       ref += calc(u + 1, k - i, sum - u * i);     return ref = min(ref, (short)2);   }   int main() {     scanf( %d , &n);     memset(dp, -1, sizeof dp);     int freq = 0;     for (int i = 0, a; i < n; i++) {       scanf( %d , &a);       if (!::a[a]) freq++;       ::a[a]++;     }     for (short i = 1; i < N; i++)       for (short j = 1; j <= a[i]; j++)         if (calc(1, j, i * j) == 1) {           ans = max(ans, j);           if (j == a[i] && freq == 2) ans = n;         }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300 * 1000 + 5, SQ = 373;   int a[N];   long long dp[2 * N];   pair<pair<int, int>, int> t[N];   long long finans[N];   int main() {     ios ::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int q;     cin >> q;     for (int i = 1; i <= q; i++) {       cin >> t[i].first.second >> t[i].first.first;       t[i].second = i;     }     sort(t + 1, t + q + 1);     for (int i = 1; i <= q; i++) {       int x = t[i].first.first, y = t[i].first.second;       if (x < SQ) {         if (x != t[i - 1].first.first) {           for (int j = n; j > 0; j--) {             dp[j] = dp[j + x] + a[j];           }         }         finans[t[i].second] = dp[y];       } else {         long long ans = 0;         for (int j = y; j <= n; j += x) {           ans += a[j];         }         finans[t[i].second] = ans;       }     }     for (int i = 1; i <= q; i++) {       cout << finans[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   inline int add(int a, int b) {     if ((a += b) >= mod) a -= mod;     return a;   }   inline int dec(int a, int b) {     if ((a -= b) < 0) a += mod;     return a;   }   inline void _add(int& a, int b) {     if ((a += b) >= mod) a -= mod;   }   inline void _dec(int& a, int b) {     if ((a -= b) < 0) a += mod;   }   inline int mult(int a, int b) {     long long t = 1ll * a * b;     if (t >= mod) t %= mod;     return t;   }   const int L = 16;   char s[510];   int n, nodecnt = 0, f[170][65536], f2[170][65536];   void fwt_or(int* A, int flag) {     if (flag == 1)       for (int i = 1; i < 65536; i <<= 1)         for (int j = 0; j < 65536; j += (i << 1))           for (int k = 0; k < i; k++) _add(A[i + j + k], A[j + k]);     else       for (int i = 1; i < 65536; i <<= 1)         for (int j = 0; j < 65536; j += (i << 1))           for (int k = 0; k < i; k++) _dec(A[i + j + k], A[j + k]);   }   void fwt_and(int* A, int flag) {     if (flag == 1)       for (int i = 1; i < 65536; i <<= 1)         for (int j = 0; j < 65536; j += (i << 1))           for (int k = 0; k < i; k++) _add(A[j + k], A[i + j + k]);     else       for (int i = 1; i < 65536; i <<= 1)         for (int j = 0; j < 65536; j += (i << 1))           for (int k = 0; k < i; k++) _dec(A[j + k], A[i + j + k]);   }   int dfs(int l, int r) {     int p = ++nodecnt;     if (l == r) {       if (s[l] ==  ? ) {         for (int t = 0; t < 4; t++) {           int S = 0;           for (int i = 0; i < 16; i++)             if ((i >> t) & 1) S |= (1 << i);           f[p][S]++;           f[p][65535 ^ S]++;         }       } else {         if (s[l] >=  A  && s[l] <=  D ) {           int t = s[l] -  A , S = 0;           for (int i = 0; i < 16; i++)             if ((i >> t) & 1) S |= (1 << i);           f[p][S] = 1;         } else {           int t = s[l] -  a , S = 0;           for (int i = 0; i < 16; i++)             if ((i >> t) & 1) S |= (1 << i);           f[p][65535 ^ S] = 1;         }       }       memcpy(f2[p], f[p], sizeof(f[p]));       return p;     }     int cnt = 0, pos = 0;     for (int i = l; i <= r; i++) {       if (s[i] ==  ( )         cnt++;       else if (s[i] ==  ) )         cnt--;       if (!cnt) {         pos = i + 1;         break;       }     }     int lc = dfs(l + 1, pos - 2), rc = dfs(pos + 2, r - 1);     if (s[pos] !=  | ) {       fwt_and(f[lc], 1);       fwt_and(f[rc], 1);       for (int i = 0; i < 65536; i++) f[p][i] = mult(f[lc][i], f[rc][i]);       fwt_and(f[p], -1);     }     if (s[pos] !=  & ) {       fwt_or(f2[lc], 1);       fwt_or(f2[rc], 1);       for (int i = 0; i < 65536; i++) f2[p][i] = mult(f2[lc][i], f2[rc][i]);       fwt_or(f2[p], -1);     }     for (int i = 0; i < 65536; i++) _add(f[p][i], f2[p][i]);     memcpy(f2[p], f[p], sizeof(f[p]));     return p;   }   int m, fs[16];   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     dfs(1, n);     scanf( %d , &m);     memset(fs, 255, sizeof(fs));     for (int i = 1, a, b, c, d, e; i <= m; i++) {       scanf( %d%d%d%d%d , &a, &b, &c, &d, &e);       fs[a | (b << 1) | (c << 2) | (d << 3)] = e;     }     int ans = 0;     for (int i = 0; i < 65536; i++) {       bool s = 1;       for (int j = 0; j < 16; j++) s &= (fs[j] == -1 || fs[j] == ((i >> j) & 1));       if (s) ans = add(ans, f[1][i]);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = (long long)(1e9 + 7);   const long long inv = (long long)(5e8 + 4);   int main() {     long long n, m;     cin >> n >> m;     long long ans = (n % mod) * (m % mod) % mod;     long long l = 1, r = 1;     while (l <= min(n, m)) {       long long a = n / l;       r = min(m, n / a);       long long x1, x2, x3, x4;       x1 = (n / r) % mod;       x2 = (l + r) % mod;       x3 = (x1 * x2) % mod;       x4 = (r - l + 1) % mod;       long long temp = ((x3 * x4) % mod) * inv % mod;       ans = ((ans + mod) - temp) % mod;       l = r + 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long Mx = 512;   long long D[Mx][Mx];   int32_t main() {     long long n, m, mod, a = 0, b = 0;     cin >> n >> m >> mod;     vector<long long> C(n);     for (long long i = 0; i < m; i++)       for (long long j = 0; j < n; j++) {         char c;         cin >> c;         if (c ==  1 ) C[j]++;       }     for (long long i = 0; i < n; i++)       if (C[i] == 0)         a++;       else if (C[i] == 1)         b++;     D[0][0] = 1;     for (long long d = 1; d <= a + b; d++) {       for (long long i = 0; i <= d; i++) {         long long j = d - i;         D[i][j] =             (((i >= 2) ? ((i * (i - 1) / 2 % mod) * D[i - 2][j + 2] % mod) : 0) +              ((i >= 1) ? ((i * j % mod) * D[i - 1][j] % mod) : 0) +              ((j >= 2) ? ((j * (j - 1) / 2 % mod) * D[i][j - 2] % mod) : 0)) %             mod;       }     }     cout << D[a][b];   }


#include <bits/stdc++.h>   int n, a[200005];   bool check1(int x) {     for (int i = x + 1; i <= n; i++)       if (a[i] >= a[i - 1]) return 0;     return 1;   }   bool check2(int x) {     for (int i = x + 1; i <= n; i++)       if (a[i] <= a[i - 1]) return 0;     return 1;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     if (check1(1) || check2(1)) {       printf( YES );       return 0;     }     for (int i = 2; i <= n; i++) {       if (a[i] <= a[i - 1]) {         if (check1(i))           printf( YES );         else           printf( NO );         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[105], p[105];   long long g[105];   long long d[105][105][105];   bool ok[105][105];   char c[105];   void read(int& x) {     char c = getchar();     x = 0;     while (!isdigit(c)) c = getchar();     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }   }   long long dp(int l, int r, int k) {     if (ok[l][r]) return d[l][r][k] = g[r - l + k + 1];     if (d[l][r][k]) return d[l][r][k];     for (int i = l; i < r; i++) {       d[l][r][k] = max(d[l][r][k], dp(l, i, 0) + dp(i + 1, r, k));     }     for (int i = l; i < p[r]; i++) {       if (c[i] == c[r] && c[i + 1] != c[r]) {         d[l][r][k] =             max(d[l][r][k], dp(l, i, k + r - p[r] + 1) + dp(i + 1, p[r] - 1, 0));       }     }     return d[l][r][k];   }   int main() {     read(n);     scanf( %s , c + 1);     for (int i = 1; i <= n; i++) {       read(a[i]);       g[i] = a[i];     }     for (int i = 1; i <= n; i++) {       for (int j = i; j >= 1; j--) {         if (c[j] == c[i]) {           p[i] = j;           ok[j][i] = 1;         } else           break;       }     }     for (int i = 2; i <= n; i++)       for (int j = 1; j < i; j++) g[i] = max(g[i], g[j] + a[i - j]);     printf( %lld n , dp(1, n, 0));   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 7;   const long long INF = LLONG_MAX / 2;   const long long N = 2e5 + 1;   struct event {     long long sa;     long long ea;     long long sb;     long long eb;   };   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     {       long long n;       cin >> n;       vector<event> a(n);       for (long long i = 0; i < n; i++)         cin >> a[i].sa >> a[i].ea >> a[i].sb >> a[i].eb;       vector<pair<double, double> > p;       multiset<pair<double, double> > s1;       multiset<pair<double, double> > s2;       bool ans = 1;       for (long long i = 0; i < n; i++) {         p.push_back({a[i].sa, i});         p.push_back({a[i].ea + 0.5, i});       }       sort(p.begin(), p.end());       for (auto i : p) {         pair<double, double> c = {a[i.second].sb, a[i.second].eb};         if (s1.count(c)) {           s1.erase(s1.find(c));           s2.erase(s2.find({c.second, c.first}));         } else {           if (s1.empty() || (*s1.rbegin()).first <= c.second &&                                 (*s2.begin()).first >= c.first) {             s1.insert(c);             s2.insert({c.second, c.first});           } else {             ans = 0;             break;           }         }       }       s1.clear();       s2.clear();       p = vector<pair<double, double> >(0);       for (long long i = 0; i < n; i++) {         p.push_back({a[i].sb, i});         p.push_back({a[i].eb + 0.5, i});       }       sort(p.begin(), p.end());       for (auto i : p) {         pair<double, double> c = {a[i.second].sa, a[i.second].ea};         if (s1.count(c)) {           s1.erase(s1.find(c));           s2.erase(s2.find({c.second, c.first}));         } else {           if (s1.empty() || (*s1.rbegin()).first <= c.second &&                                 (*s2.begin()).first >= c.first) {             s1.insert(c);             s2.insert({c.second, c.first});           } else {             ans = 0;             break;           }         }       }       if (ans)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, arr[300010], lft[300010], rgt[300010], mx = 0, cnt = 0;   stack<long long> s1, s2;   set<long long> st;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = 0; i < n; i++) lft[i] = -1, rgt[i] = n;     for (int i = 0; i < n; i++) {       while (!s1.empty() && (arr[i] % arr[s1.top()])) rgt[s1.top()] = i, s1.pop();       s1.push(i);     }     for (int i = n - 1; i >= 0; i--) {       while (!s2.empty() && (arr[i] % arr[s2.top()])) lft[s2.top()] = i, s2.pop();       s2.push(i);     }     for (int i = 0; i < n; i++) mx = max(mx, rgt[i] - lft[i] - 2);     for (int i = 0; i < n; i++)       if (rgt[i] - lft[i] - 2 == mx) st.insert(lft[i] + 2);     cout << st.size() <<     << mx <<   n ;     for (auto u : st) cout << u <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int l, r;     scanf( %d%d , &l, &r);     if (l == r && l % 2)       printf( %d , l);     else       printf( %d , 2);   }


#include <bits/stdc++.h>   using namespace std;   void init_code() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   int main() {     init_code();     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> arr(n + 1, 0);       map<int, int> mp;       for (int i = 1; i <= n; ++i) {         cin >> arr[i];         mp[arr[i]] += 1;       }       int res = 0, shifts = 0;       for (int i = 1; i <= n; ++i) {         if (arr[i] > i + shifts) {           int diff = arr[i] - i - shifts;           res += diff;           shifts += diff;         }       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000007;   void __print(long long int x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     long long int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   void solve() {     long long int n;     cin >> n;     vector<long long int> arr(n);     for (long long int i = 0; i < n; i++) cin >> arr[i];     long long int l = n, r = -1;     for (long long int i = 0; i < n; i++) {       if (arr[i] == 1) {         l = i;         break;       }     }     for (long long int i = n - 1; n - 1 < -1 ? i < -1 : i > -1;          n - 1 < -1 ? i += 1 : i -= 1) {       if (arr[i] == 1) {         r = i;         break;       }     }     long long int ans = 0;     for (long long int i = l; l < r + 1 ? i < r + 1 : i > r + 1;          l < r + 1 ? i += 1 : i -= 1) {       if (arr[i] == 0) {         ans++;       }     }     cout << ans << endl;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T sqr(T a) {     return (a) * (a);   }   template <class T>   T abs(T a) {     return (a < 0) ? -(a) : (a);   }   const double Pi = acos(-1.0);   const double eps = 1e-10;   const int INF = 1000 * 1000 * 1000;   const double phi = 0.5 + sqrt(1.25);   int main() {     string Less =  z , More =    , tmp =   ;     int n;     scanf( %d n , &n);     for (int i = 0, _n = (n)-1; i <= _n; ++i) {       getline(cin, tmp);       Less = min(Less, tmp);       More = max(More, tmp);     }     int i = 0, m = ((int)((Less).size()));     bool coincide = 1;     while (i < m && coincide)       if (Less.substr(0, i + 1) == More.substr(0, i + 1))         i++;       else         coincide = 0;     printf( %d n , i);     return 0;   }


#include <bits/stdc++.h>   const long long mod = 1e9 + 7;   const long long LL_MAX = 9223372036854775807;   using namespace std;   int a[100005];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < 2 * n; i++) scanf( %d , &a[i]);     int ans = 0;     for (int i = 0; i < 2 * n; i++) {       if (a[i] == 0) {         continue;       }       if (a[i] != a[i + 1]) {         int t = 0, j;         for (j = i + 1; j < 2 * n && a[j] != a[i]; j++) {           if (a[j] != 0) t++;         }         a[j] = 0;         ans += t;       } else         i++;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int record[300000];   int main() {     map<pair<int, int>, int> m;     memset(record, 0, sizeof(record));     int n, p;     scanf( %d%d , &n, &p);     map<pair<int, int>, int>::iterator iter;     for (int i = 0; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       record[a]++;       record[b]++;       if (a > b)         m[make_pair(b, a)]++;       else         m[make_pair(a, b)]++;     }     long long ans = 0;     for (iter = m.begin(); iter != m.end(); iter++) {       int x = (iter->first).first;       int y = (iter->first).second;       if (record[x] + record[y] - iter->second < p && record[x] + record[y] >= p)         ans--;     }     sort(record + 1, record + n + 1);     int ed = n;     for (int st = 1; st <= n; st++) {       while (record[st] + record[ed] >= p && ed > st) ed--;       ans += min(n - ed, n - st);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fastpow(int a, int b, int MOD) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y) % MOD;       }       y = (y * y) % MOD;       b /= 2;     }     return x;   }   long long InverseEuler(int n, int MOD) { return fastpow(n, MOD - 2, MOD); }   long long f[3];   bool init;   long long C(int n, int r, int MOD) {     if (!init) {       init = 1;       f[0] = 1;       for (int i = 1; i < 300000; i++) f[i] = (f[i - 1] * i) % MOD;     }     return (f[n] *             ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %            MOD;   }   int N;   string s[77000];   unordered_map<int, int> ct[9];   unordered_map<int, int> loc[9][77000];   int main() {     std::ios::sync_with_stdio(false);     cin >> N;     for (int i = 0; i < N; i++) {       cin >> s[i];       int M = s[i].length();       for (int j = 0; j < M; j++) {         int tmp = 0;         for (int k = j; k < M; k++) {           tmp = tmp * 10 + (s[i][k] -  0 );           ct[k - j][tmp]++;           loc[k - j][i][tmp]++;         }       }     }     for (int i = 0; i < N; i++) {       int M = s[i].length();       int ln = 10;       int res;       for (int j = 0; j < M; j++) {         int tmp = 0;         for (int k = j; k < M; k++) {           if (k - j >= ln) break;           tmp = tmp * 10 + (s[i][k] -  0 );           if (ct[k - j][tmp] == loc[k - j][i][tmp]) {             res = tmp;             ln = k - j;           }         }       }       string res2 =   ;       while (res) {         res2 +=  0  + (res % 10);         res /= 10;       }       reverse(res2.begin(), res2.end());       while (res2.size() != ln + 1) res2 =  0  + res2;       cout << res2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using namespace chrono;   const int maxn = 1e6;   void solve() {     int n, a[21][21], val;     vector<int> data;     map<int, int> m;     cin >> n;     for (int i = 0; i < n * n; i++) {       cin >> val;       data.push_back(val);       m[val]++;     }     if (n == 1) {       cout <<  YES             <<   n ;       cout << data[0] <<   n ;       return;     }     for (int ii = 0; ii < n; ii++) {       for (int jj = 0; jj < n; jj++) {         a[ii][jj] = 0;       }     }     for (int ii = 0; ii < n / 2; ii++) {       for (int jj = 0; jj < n / 2; jj++) {         int v_f = 0, v_s = 0;         for (auto it = m.begin(); it != m.end(); it++) {           if (it->second >= 4) {             v_f = it->first;             break;           }         }         if (v_f == 0) {           cout <<  NO                 <<   n ;           return;         }         a[ii][jj] = v_f;         a[n - ii - 1][jj] = v_f;         a[ii][n - jj - 1] = v_f;         a[n - ii - 1][n - jj - 1] = v_f;         m[v_f] -= 4;       }     }     if ((n & 1) == 0) {       cout <<  YES             <<   n ;       for (int ii = 0; ii < n; ii++) {         for (int jj = 0; jj < n; jj++) {           cout << a[ii][jj] <<    ;         }         cout <<   n ;       }       cout <<   n ;       return;     }     for (int ii = 0; ii < n / 2; ii++) {       int f = 0;       for (auto it = m.begin(); it != m.end(); it++) {         if (it->second >= 2) {           f = it->first;         }       }       if (f == 0) {         cout <<  NO               <<   n ;         return;       }       a[ii][n / 2] = f;       a[n - ii - 1][n / 2] = f;       m[f] -= 2;     }     for (int ii = 0; ii < n / 2; ii++) {       int f = 0;       for (auto it = m.begin(); it != m.end(); it++) {         if (it->second >= 2) f = it->first;       }       if (f == 0) {         cout <<  NO               <<   n ;         return;       }       a[n / 2][ii] = f;       a[n / 2][n - ii - 1] = f;       m[f] -= 2;     }     int f = 0;     for (auto it = m.begin(); it != m.end(); it++) {       if (it->second == 1) {         f = it->first;       }     }     if ((n & 1) == 1) {       a[n / 2][n / 2] = f;     }     cout <<  YES           <<   n ;     for (int ii = 0; ii < n; ii++) {       for (int jj = 0; jj < n; jj++) {         cout << a[ii][jj] <<    ;       }       cout <<   n ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)(2e6 + 123);   const int R = (int)(2e6);   const long long inf = (long long)(1e12 + 7);   long long pn, p[maxn], lp[maxn], b[maxn];   bool u[maxn];   void resheto(long long n) {     for (long long i = 3; i * i <= n; i += 2)       if (!u[i])         for (long long j = i * i; j <= n; j += 2 * i) u[j] = 1;     p[pn++] = 2;     for (long long i = 3; i <= n; i += 2)       if (!u[i]) p[pn++] = i;     for (long long i = 0; i < pn; ++i) {       for (long long j = p[i]; j <= n; j += p[i]) lp[j] = p[i];     }   }   long long n, val, x, k, t, ans1;   long long res(long long x) {     long long ans = x;     for (long long mask = 1; mask < (1 << n); ++mask) {       long long cnt = 0, a = 1;       for (long long i = 0; i < n; ++i) {         if ((mask >> i) & 1) {           cnt++;           a *= b[i];         }       }       if (cnt & 1)         ans -= x / a;       else         ans += x / a;     }     return ans - ans1;   }   long long bin(long long l, long long r, long long k) {     while (r - l > 1) {       long long m = (l + r) / 2;       if (res(m) >= k)         r = m;       else         l = m;     }     if (res(l) == k) return l;     return r;   }   int main() {     ios_base ::sync_with_stdio(NULL);     resheto(R);     cin >> t;     for (long long cs = 1; cs <= t; ++cs) {       cin >> x >> val >> k;       n = ans1 = 0;       while (val != 1) {         b[n++] = lp[val];         long long y = lp[val];         while (val % y == 0) val /= y;       }       ans1 = res(x);       cout << bin(x + 1, inf, k) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e6 + 3;   const int INFI = 1e9 * 2;   const long long LINFI = 1e17;   const double pi = acos(-1.0);   const int N = 111;   const int M = 11;   const int move[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};   double mabs(double x) { return x > 0 ? x : -x; }   int main() {     int n;     while (scanf( %d , &n) != EOF) {       if (n == 1)         printf( a na nb nb n );       else {         if (n & 1) {           n--;           for (int i = 0; i < n; i++) putchar((i & 2) ?  a  :  b );           puts( c );           for (int i = 0; i < n; i++) putchar((i & 2) ?  d  :  e );           puts( c );           putchar( f );           for (int i = 0; i < n; i++) putchar((i & 2) ?  g  :  h );           printf(  n );           putchar( f );           for (int i = 0; i < n; i++) putchar((i & 2) ?  i  :  j );           printf(  n );         } else {           for (int i = 0; i < n; i++) putchar((i & 2) ?  a  :  b );           printf(  n );           for (int i = 0; i < n; i++) putchar((i & 2) ?  c  :  d );           printf(  n );           n -= 2;           putchar( e );           for (int i = 0; i < n; i++) putchar((i & 2) ?  f  :  g );           puts( h );           putchar( e );           for (int i = 0; i < n; i++) putchar((i & 2) ?  i  :  j );           puts( h );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, h;     cin >> n >> h;     vector<int> a(n, 0);     copy(istream_iterator<int>(cin), istream_iterator<int>(), a.begin());     vector<int> a_orig = a;     sort(a.begin(), a.end());     int max1 = a[n - 1] + a[n - 2];     int min1 = a[0] + a[1];     int best_sofar = max1 - min1;     vector<int> res(n, 2);     if (n >= 3) {       min1 = min(min1 + h, a[1] + a[2]);       max1 = max(max1, a[0] + a[n - 1] + h);       if (abs(max1 - min1) < best_sofar) {         best_sofar = abs(max1 - min1);         res[distance(a_orig.begin(), min_element(a_orig.begin(), a_orig.end()))] =             1;       }     }     cout << best_sofar << endl;     copy(res.begin(), res.end(), ostream_iterator<int>(cout,    ));     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)1e18;   const long long mod = (long long)1e9 + 7;   const double eps = (double)1e-9;   const double pi = acos(-1.0);   const int dx[] = {0, 0, 1, 0, -1};   const int dy[] = {0, 1, 0, -1, 0};   const int N = 100500;   void solve() {     int a, b, cnt = 0;     string second;     cin >> a >> b >> second;     vector<int> v;     cnt = 0;     for (int i = 0; i < (int)second.size(); ++i) {       if (second[i] ==  X ) continue;       int j = i;       while (j + 1 < (int)second.size() && second[j + 1] == second[j]) j++;       int len = j - i + 1;       if (len < b) continue;       if (b <= len && len < a) {         cout <<  NO  << endl;         return;       }       if (a <= len && len < 2 * b)         cnt++;       else         v.push_back(len);       i = j;     }     if (v.size() == 0) {       if (cnt % 2 == 0)         cout <<  NO  << endl;       else         cout <<  YES  << endl;       return;     }     if (v.size() > 1) {       cout <<  NO  << endl;       return;     }     int x = v[0];     for (int i = 0; i + a <= x; ++i) {       if ((b <= i && i < a) || (b <= x - a - i && x - a - i < a)) continue;       if (2 * b <= max(i, x - a - i)) continue;       int ncnt = cnt;       if (a <= i && i < 2 * b) ncnt++;       if (a <= x - a - i && x - a - i < 2 * b) ncnt++;       if (ncnt % 2 == 0) {         cout <<  YES  << endl;         return;       }     }     cout <<  NO  << endl;   }   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     int T;     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int p = 1000000007;   int n, x, y, g[100005], vg[100005], C, P = 1, a1, a2, a3, b, ans;   vector<int> l[100005], r[100005];   inline int ksm(int a, int b) {     int res = 1;     while (b) {       if (b & 1) res = (long long)res * a % p;       a = (long long)a * a % p;       b >>= 1;     }     return res;   }   int head[100005], to[200005], nex[200005], cnt;   inline void adde(int f, int t) {     to[++cnt] = t;     nex[cnt] = head[f];     head[f] = cnt;   }   int fa[100005], dep[100005], sz[100005], son[100005], seg[100005], top[100005];   void dfs1(int u, int pre) {     fa[u] = pre;     dep[u] = dep[pre] + 1;     sz[u] = 1;     for (int i = head[u], v; i; i = nex[i]) {       v = to[i];       if (v == pre) continue;       dfs1(v, u);       sz[u] += sz[v];       if (sz[v] > sz[son[u]]) son[u] = v;     }   }   void dfs2(int u) {     if (son[u]) {       seg[son[u]] = ++cnt;       top[son[u]] = top[u];       dfs2(son[u]);     }     for (int i = head[u], v; i; i = nex[i]) {       v = to[i];       if (top[v]) continue;       seg[v] = ++cnt;       top[v] = v;       dfs2(v);     }   }   int sum[100005], sum1[100005];   inline int query(int *a, int x) {     int res = 0;     for (; x; x -= x & -x) res = (res + a[x]) % p;     return res;   }   inline void modify(int *a, int x, int y) {     for (; x <= n + 1; x += x & -x) a[x] = (a[x] + y) % p;   }   inline int Query(int x) {     int res = ((query(sum1, 1) - query(sum, 1) * 2) % p + p) % p;     while (x) {       res = ((res + (long long)query(sum, seg[x]) * (seg[x] + 1) -               query(sum1, seg[x])) %                  p +              p) %             p;       res = ((res - (long long)query(sum, seg[top[x]] - 1) * seg[top[x]] +               query(sum1, seg[top[x]] - 1)) %                  p +              p) %             p;       x = fa[top[x]];     }     return res;   }   inline void Modify(int x, int y) {     while (x) {       modify(sum, seg[top[x]], y);       modify(sum, seg[x] + 1, (p - y) % p);       modify(sum1, seg[top[x]], (long long)y * seg[top[x]] % p);       modify(sum1, seg[x] + 1, (p - (long long)y * (seg[x] + 1) % p) % p);       x = fa[top[x]];     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &x, &y);       g[i] = y - x + 1;       vg[i] = ksm(g[i], p - 2);       l[x].push_back(i);       r[y].push_back(i);       P = (long long)P * g[i] % p;       C = max(C, y);     }     for (int i = 1; i < n; ++i) {       scanf( %d%d , &x, &y);       adde(x, y);       adde(y, x);     }     dep[0] = -1;     dfs1(1, 0);     seg[1] = top[1] = cnt = 1;     dfs2(1);     for (int c = 1; c <= C; ++c) {       for (unsigned int i = 0; i < l[c].size(); ++i) {         int x = l[c][i];         a1 = (a1 + (long long)dep[x] * vg[x]) % p;         a2 = (a2 + vg[x]) % p;         a3 = (a3 + (long long)dep[x] * vg[x] % p * vg[x]) % p;         b = (b + (long long)Query(x) * vg[x]) % p;         Modify(x, vg[x]);       }       ans = ((ans + (long long)a1 * a2 - a3 - 2 * b) % p + p) % p;       for (unsigned int i = 0; i < r[c].size(); ++i) {         int x = r[c][i];         a1 = ((a1 - (long long)dep[x] * vg[x]) % p + p) % p;         a2 = (a2 - vg[x] + p) % p;         a3 = ((a3 - (long long)dep[x] * vg[x] % p * vg[x]) % p + p) % p;         Modify(x, p - vg[x]);         b = ((b - (long long)Query(x) * vg[x]) % p + p) % p;       }     }     ans = (long long)ans * P % p;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void bringToTen(int &k, int &rating, vector<int> &bucket) {     for (int i = 9; i > 0; i--) {       int x = 0;       while (x < 100) {         int h = min(k / (10 - i), bucket[x + i]);         k -= h * (10 - i);         bucket[x + 10] += h;         rating += h;         bucket[x + i] -= h;         x += 10;       }     }   }   void addToTen(int &k, int &rating, vector<int> &bucket) {     int x = 0;     while (x < 100) {       int h = min(k / 10, bucket[x]);       rating += h;       bucket[x + 10] += h;       bucket[x] -= h;       k -= 10 * h;       x += 10;     }   }   int main(void) {     int n, k, rating = 0, h;     scanf(  %d %d , &n, &k);     vector<int> a(n);     for (int(i) = (0); i < (n); i++) scanf(  %d , &a[i]);     vector<int> num(101, 0);     for (int(i) = (0); i < (n); i++) {       num[a[i]]++;       rating += a[i] / 10;     }     bringToTen(k, rating, num);     addToTen(k, rating, num);     printf( %d n , rating);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[200000], arr2[200000];   int main() {     int n, i;     long long sum = 0;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &arr[i]);       arr2[i] = arr[i] + arr2[i - 1];     }     for (i = 0; i < n; i++) {       if (arr[i] == 0) {         sum += arr2[i];       }     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 6, M = 3, nBits = 2e8 + 5, MM = (1 << 16), MAX = 1111,             OO = 0x3f3f3f3f, MOD = 1000000007, inf = 1 << 30;   const double PI = acos(-1);   const long long INF = (long long)1e18, MOOD = 1000000007;   long long GCD(long long a, long long b) { return !b ? a : GCD(b, a % b); }   long long LCM(long long x, long long y) { return (x * y / GCD(x, y)); }   long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); }   void fast() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int t, a, b, color[N], n, m, k, x, y, r[5001], c[5001];   int main() {     fast();     cin >> n >> m >> k;     for (int i = 1; i <= k; ++i) {       cin >> a >> b >> color[i];       if (a == 1)         r[b] = i;       else         c[b] = i;     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) cout << color[max(r[i], c[j])] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long int, long long int> > P;   int n;   bool compare_y(const pair<long long int, long long int>& p1,                  const pair<long long int, long long int>& p2) {     if (p1.second != p2.second) return p1.second < p2.second;     return p1.first < p2.first;   }   long long int d(pair<long long int, long long int> p1,                   pair<long long int, long long int> p2) {     return pow(p1.first - p2.first, 2) + pow(p1.second - p2.second, 2);   }   long long int func(int l, int h,                      vector<pair<long long int, long long int> > Y) {     if (h - l == 1) return d(P[l], P[h]);     if (h - l == 2)       return min(min(d(P[l], P[l + 1]), d(P[l + 1], P[h])), d(P[l], P[h]));     int mid = (l + h) / 2, sz = Y.size();     vector<pair<long long int, long long int> > left, right;     for (int i = 0; i < sz; ++i) {       if (Y[i].first <= P[mid].first)         left.push_back(Y[i]);       else         right.push_back(Y[i]);     }     long long int m = min(func(l, mid, left), func(mid + 1, h, right));     for (int i = 0; i < sz; ++i)       for (int j = i + 1; j < i + 10 && j < sz; ++j)         if (d(Y[i], Y[j]) < m) m = d(Y[i], Y[j]);     return m;   }   int main() {     cin >> n;     vector<pair<long long int, long long int> > Q;     for (int i = 0; i < n; ++i) {       long long int u;       cin >> u;       P.push_back(make_pair(i + 1, u));       Q.push_back(make_pair(i + 1, u));       if (i) P[i].second += P[i - 1].second;       if (i) Q[i].second += Q[i - 1].second;     }     sort(Q.begin(), Q.end(), compare_y);     cout << func(0, n - 1, Q) << endl;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       long long n, x;       cin >> n >> x;       long long deg[1005] = {};       for (long long i = 1; i < n; i++) {         long long u, v;         cin >> u >> v;         deg[u]++, deg[v]++;       }       if (n == 1 || deg[x] == 1)         cout <<  Ayush n ;       else {         if (n % 2)           cout <<  Ashish n ;         else           cout <<  Ayush n ;       }     }     cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int n, g[21][21], a, t[maxn];   bool ans = true;   int find(int times) {     int i = 1;     bool finded = false;     for (; i <= 1000; ++i)       if (t[i] >= times) {         finded = true;         break;       }     if (!finded) ans = false;     t[i] -= times;     return i;   }   int main() {     cin >> n;     for (int i = 0; i < n * n; i++) {       cin >> a;       t[a]++;     }     for (int i = 1; i <= n / 2; i++)       for (int j = 1; j <= n / 2; j++)         g[i][j] = g[n - i + 1][j] = g[i][n - j + 1] = g[n - i + 1][n - j + 1] =             find(4);     if (ans) {       if (n & 1) {         for (int i = 1; i <= n / 2; i++)           if (i != (n + 1) / 2) {             g[i][(n + 1) / 2] = g[n - i + 1][(n + 1) / 2] = find(2);             g[(n + 1) / 2][i] = g[(n + 1) / 2][n - i + 1] = find(2);           }         g[(n + 1) / 2][(n + 1) / 2] = find(1);       }       if (ans) {         cout <<  YES  << endl;         for (int i = 1; i <= n; i++)           for (int j = 1; j <= n; j++) cout << g[i][j] <<    n [j == n];       }     }     if (!ans) cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d;   int a[200];   int x[200], y[200];   int dis[200];   vector<pair<int, int> > edge[200];   int bellman_ford() {     for (int i = 0; i < n; ++i) dis[i] = 1000000000;     dis[0] = 0;     for (int i = 1; i < n; ++i) {       for (int j = 0; j < n; ++j) {         for (int k = 0, len = edge[j].size(); k < len; ++k)           dis[edge[j][k].first] =               min(dis[edge[j][k].first], dis[j] + edge[j][k].second);       }     }     return max(0, dis[n - 1]);   }   int D(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); }   int main() {     while (cin >> n >> d) {       a[0] = 0;       a[n - 1] = 0;       for (int i = 1; i < n - 1; ++i) cin >> a[i];       for (int i = 0; i < n; ++i) {         cin >> x[i] >> y[i];       }       for (int i = 0; i < n; ++i)         for (int j = 0; j < n; ++j)           if (i != j) {             edge[i].push_back(make_pair(j, D(i, j) * d - a[i]));           }       cout << bellman_ford() << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 50;   const long long Mod = 1e9 + 7;   struct node {     long long to, next, w;   } edge[maxn * 2];   struct Edge {     long long u, v;   } t[maxn];   long long k = 0, head[maxn];   void add(long long u, long long v) {     edge[++k].to = v;     edge[k].next = head[u];     head[u] = k;   }   long long T, p[maxn], m, n;   long long ans = 0, sz[maxn], cnt[maxn];   void dfs(long long x, long long f, long long opt) {     sz[x] = 1;     for (long long i = head[x]; i; i = edge[i].next) {       if (edge[i].to == f) continue;       dfs(edge[i].to, x, i);       sz[x] += sz[edge[i].to];     }     if (opt == 0) return;     cnt[opt] = sz[x] * (n - sz[x]);     if (cnt[opt] == 0 && opt != 0) cnt[opt] = n - 1;   }   int main() {     cin >> T;     while (T--) {       k = 0;       memset(head, 0, sizeof(head));       memset(cnt, 0, sizeof(cnt));       cin >> n;       for (long long i = 1; i <= n - 1; i++) {         long long u, v;         cin >> u >> v;         add(u, v);         add(v, u);       }       cin >> m;       for (long long i = 1; i <= m; i++) cin >> p[i];       sort(p + 1, p + 1 + m);       long long cnt1 = n - 1 - m;       if (cnt1 <= 0) cnt1 = 0;       dfs(1, 1, 0);       sort(cnt + 1, cnt + 1 + k);       while (m > n - 1) {         p[m - 1] = (p[m - 1] * p[m]) % Mod;         m--;       }       long long ans = 0, opt = 0;       for (long long i = k / 2 + 1; i <= k; i++) {         if (cnt1) {           ans = (ans + cnt[i] * 1) % Mod;           cnt1--;         } else {           ans = (ans + cnt[i] * p[++opt] % Mod) % Mod;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int>> L[1000005];   long long fac[1000005];   const long long MOD = 1000000007;   int main() {     int N, M;     scanf( %d%d , &N, &M);     for (int i = 0; i < N; ++i) {       int g;       scanf( %d , &g);       map<int, int> cnt;       for (int j = 0; j < g; ++j) {         int a;         scanf( %d , &a);         ++cnt[a];       }       for (auto x : cnt) {         L[x.first].push_back({i, x.second});       }     }     map<vector<pair<int, int>>, int> mp;     for (int i = 1; i <= M; ++i) ++mp[L[i]];     fac[0] = 1;     for (int i = 1; i <= M; ++i) fac[i] = (fac[i - 1] * i) % MOD;     long long ans = 1;     for (auto &x : mp) ans = (ans * fac[x.second]) % MOD;     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long get_sum(vector<long long> &prefix, int left, int right) {     if (left != 0)       return prefix[right] - prefix[left - 1];     else       return prefix[right];   }   void To_Add_or_Not_To_Add(vector<long long> &a, long long k) {     int n = a.size();     sort(a.begin(), a.end());     vector<long long> prefix = a;     for (int i = 1; i < n; i++) prefix[i] += prefix[i - 1];     long ans = 0;     int maxFreq = INT_MIN;     for (auto key : a) {       int freq = upper_bound(a.begin(), a.end(), key) -                  lower_bound(a.begin(), a.end(), key);       int low = 0;       int high = lower_bound(a.begin(), a.end(), key) - a.begin();       high--;       long long stock = k;       while (low <= high) {         int mid = (low + high) / 2;         int len = high - mid + 1;         auto req = len * key - get_sum(prefix, mid, high);         if (stock >= req) {           stock -= req;           high = mid - 1;           freq += len;         } else {           low = mid + 1;         }       }       if (freq > maxFreq) {         maxFreq = freq;         ans = key;       }     }     cout << maxFreq <<     << ans << endl;   }   int main() {     int n;     cin >> n;     long long k;     cin >> k;     vector<long long> a(n);     for (auto &ele : a) cin >> ele;     To_Add_or_Not_To_Add(a, k);     return 0;   }