#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   struct node {     int x, y, id;     void scan() {       scanf( %d%d , &x, &y);       if (x > y) swap(x, y);     }   } e[N * 5], q[N], tmp[N];   struct opt {     int x, y, sz, f;     long long ans;   } op[N * 5];   int T;   int ext;   int cnt, now, tot, n, m, qu, bs, l, r, top, pos;   int ed[N], in[N], sz[N], father[N];   vector<node> v[N], g[N];   long long ret[N], ans;   bool cmpx(const node &a, const node &b) { return a.x < b.x; }   bool cmpy(const node &a, const node &b) { return a.y < b.y; }   int calc(int x) {     int l = 1, r = m;     if (e[1].x >= x) return 1;     if (e[m].x < x) return m + 1;     while (l + 1 < r) {       int mid = (l + r) >> 1;       if (e[mid].x >= x)         r = mid;       else         l = mid + 1;     }     if (e[l].x >= x)       return l;     else       return r;   }   int getfather(int x) { return father[x] == x ? x : getfather(father[x]); }   void solve(int x, int y) {     int fx = getfather(x), fy = getfather(y);     if (fx == fy) return;     if (sz[fx] < sz[fy]) swap(fx, fy);     ++top;     op[top].x = fx;     op[top].y = fy;     op[top].sz = sz[fx];     op[top].f = father[fy];     op[top].ans = ans;     father[fy] = fx;     ans++;     sz[fx] += sz[fy];   }   void undo() {     for (int i(top); i >= (1); --i) {       int fx = op[i].x, fy = op[i].y;       father[fy] = op[i].f;       ans = op[i].ans;       sz[fx] = op[i].sz;     }   }   int main() {     scanf( %d%d , &n, &ext);     scanf( %d , &m);     for (int i(1); i <= (m); ++i) e[i].scan();     scanf( %d , &qu);     for (int i(1); i <= (qu); ++i) q[i].scan(), q[i].id = i;     sort(e + 1, e + m + 1, cmpx);     memset(in, 0, sizeof in);     for (int i(1); i <= (m); ++i) ++in[e[i].x];     bs = (int)floor(sqrt((double)(2 * m) * (double)(log(n) / 0.4)));     tot = 0;     now = 0;     memset(ed, 0, sizeof ed);     for (int i(1); i <= (n); ++i) {       now += in[i];       if (now >= bs) {         ++tot;         ed[tot] = i;         now = 0;       }     }     if (ed[tot] != n) {       ++tot;       ed[tot] = n;     }     for (int i(1); i <= (tot); ++i) {       l = ed[i - 1] + 1, r = ed[i];       v[i].clear();       g[i].clear();       for (int j(1); j <= (m); ++j)         if (e[j].x >= l && e[j].x <= r) v[i].push_back(e[j]);       for (int j(1); j <= (qu); ++j)         if (q[j].x >= l && q[j].y <= r) g[i].push_back(q[j]);     }     for (int et(1); et <= (tot); ++et) {       l = ed[et - 1] + 1, r = ed[et];       cnt = 0;       for (int i(1); i <= (qu); ++i)         if (q[i].x >= l && q[i].x <= r && q[i].y > r) {           tmp[++cnt] = q[i];         }       sort(tmp + 1, tmp + cnt + 1, cmpy);       sort(e + 1, e + m + 1, cmpx);       pos = calc(r + 1);       sort(e + pos, e + m + 1, cmpy);       for (int i(1); i <= (n); ++i) father[i] = i, sz[i] = 1;       ans = 0;       for (int j = pos, k = 1; k <= cnt; ++k) {         while (e[j].y <= tmp[k].y && j <= m) {           solve(e[j].x, e[j].y);           ++j;         }         top = 0;         for (auto edge : v[et]) {           if (tmp[k].x <= edge.x && edge.y <= tmp[k].y) {             solve(edge.x, edge.y);           }         }         ret[tmp[k].id] = tmp[k].y - tmp[k].x + 1 - ans;         undo();       }       for (int i(1); i <= (n); ++i) father[i] = i, sz[i] = 1;       ans = 0;       for (auto query : g[et]) {         top = 0;         for (auto edge : v[et]) {           if (query.x <= edge.x && edge.y <= query.y) {             solve(edge.x, edge.y);           }         }         ret[query.id] = query.y - query.x + 1 - ans;         undo();       }     }     for (int i(1); i <= (qu); ++i) printf( %lld n , ret[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef signed long long ll;      #define _P(...) (void)printf(__VA_ARGS__)   #define FOR(x,to) for(x=0;x<(to);x++)   #define FORR(x,arr) for(auto& x:arr)   #define FORR2(x,y,arr) for(auto& [x,y]:arr)   #define ALL(a) (a.begin()),(a.end())   #define ZERO(a) memset(a,0,sizeof(a))   #define MINUS(a) memset(a,0xff,sizeof(a))   template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}   template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}   //-------------------------------------------------------      int T,N,A[5050],S[5050];      ll dp[5050];      void solve() {    int i,j,k,l,r,x,y; string s;        cin>>T;    while(T--) {     cin>>N;     FOR(i,N) cin>>A[i];     FOR(i,N) cin>>S[i];     FOR(i,N+1) dp[i]=-1LL<<60;          ll ret=0;     FOR(i,N) {      ll ma=0;      for(j=i-1;j>=0;j--) if(A[i]!=A[j]) {       ll nma=ma+abs(S[i]-S[j]);       ma=max(ma,dp[j]+abs(S[i]-S[j]));       dp[j]=max(dp[j],nma);      }      dp[i]=ma;     }     FOR(i,N) ret=max(ret,dp[i]);     cout<<ret<<endl;    }   }         int main(int argc,char** argv){    string s;int i;    if(argc==1) ios::sync_with_stdio(false), cin.tie(0);    FOR(i,argc-1) s+=argv[i+1],s+=  n ; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);    cout.tie(0); solve(); return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename type>   type input() {     type var;     cin >> var;     return var;   }   template <typename type>   void input(type &var) {     cin >> var;     return;   }   template <typename type>   void output(type &var, const char *ch) {     cout << var << ch;   }   int main() {     long long a, b, N, p, r, c, d, e, f;     cin >> N >> p;     vector<pair<long long, long long> > v(N);     vector<double> w(N);     for (int i = 0; i < N; i++) {       cin >> a >> b;       v[i].first = b - a + 1;       v[i].second = b / p - (a - 1) / p;     }     double s = 0;     for (int i = 0; i < N; i++) {       c = v[i].first, e = v[(i + 1) % N].first, d = v[i].second,       f = v[(i + 1) % N].second;       w[i] = 2000 * ((c * e - (c - d) * (e - f)) / (c * e * 1.0));       s += w[i];     }     cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)          << setprecision(9) << s << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     long long int n, b, c, i;     float a, d;     scanf( %I64d , &n);     a = sqrt(n);     b = floor(a);     for (i = b; i > 0; i--) {       d = n % i;       if (d == 0)         break;       else         continue;     }     c = n / i;     printf( %I64d %I64d , i, c);   }


#include <bits/stdc++.h>   using namespace std;   struct Edge {     int v, u;     long long cap, cost;     Edge() : v(), u(), cap(), cost() {}     Edge(int _v, int _u, long long _cap, long long _cost)         : v(_v), u(_u), cap(_cap), cost(_cost) {}   };   const long long INF1 = (long long)1e9;   const long long INF2 = (long long)1e15;   const int E = (int)4e4;   const int V = 200;   int S, T;   int n, k;   int a[V];   long long c[V];   Edge ed[E];   int edSz;   long long dist[V];   int par[V];   void addEdge(int v, int u, long long cap, long long cost) {     ed[edSz++] = Edge(v, u, cap, cost);     ed[edSz++] = Edge(u, v, 0, -cost);   }   long long flow() {     for (int i = 0; i <= S; i++) dist[i] = INF2;     dist[S] = 0;     bool ch = true;     while (ch) {       ch = false;       for (int i = 0; i < edSz; i++) {         Edge e = ed[i];         if (e.cap <= 0) continue;         if (dist[e.v] == INF2) continue;         long long w = dist[e.v] + e.cost;         if (w >= dist[e.u]) continue;         dist[e.u] = w;         par[e.u] = i;         ch = true;       }     }     long long res = 0;     int v = T;     while (v != S) {       int id = par[v];       ed[id].cap--;       ed[id ^ 1].cap++;       res += ed[id].cost;       v = ed[id].v;     }     return res;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       a[i]--;     }     for (int i = 0; i < n; i++) scanf( %lld , &c[i]);     S = 2 * n;     T = 2 * n - 1;     for (int i = 0; i < n; i++) addEdge(2 * i, 2 * i + 1, 1, -INF1);     for (int i = 0; i < 2 * n - 1; i++) addEdge(i, T, INF1, 0);     addEdge(S, T, INF1, 0);     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         long long w = (a[i] == a[j] ? 0 : c[a[j]]);         addEdge(2 * i + 1, 2 * j, INF1, w);       }       addEdge(S, 2 * i, INF1, c[a[i]]);     }     long long ans = INF1 * n;     for (int i = 0; i < k; i++) ans += flow();     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long s = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar();     return s * w;   }   void put1() { puts( YES ); }   void put2() { puts( NO ); }   const int manx = 3e5 + 5;   ;   long long a[manx], dp[manx];   int main() {     long long n, m, ans = -0x3f3f3f3f;     n = read(), m = read();     for (int i = 1; i <= n; i++) {       a[i] = read();       dp[0] = max((long long)0, dp[0] + a[i]);       dp[1] = max(dp[0], dp[1] + a[i] * m);       dp[2] = max(dp[1], dp[2] + a[i]);       ans = max(ans, dp[2]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int inf = (long long int)1e18 + 10;   long long int dp[102][102] = {{0}};   map<char, long long int> m;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, k;     string s;     cin >> n >> k;     cin >> s;     m[s[0]] = 1;     dp[0][0] = 1;     dp[1][0] = 1;     dp[1][1] = 1;     for (long long int i = 2; i <= n; i++)       for (long long int j = 0; j <= i; j++) {         if (j <= i - 1) dp[i][j] = dp[i - 1][j];         if (j == 1) {           dp[i][1] += (m.find(s[i - 1]) == m.end());           m[s[i - 1]] = 1;         } else {           dp[i][j] += dp[i - 1][j - 1];           long long int r = -1;           for (long long int k = i - 2; k >= 0; k--) {             if (s[k] == s[i - 1]) {               r = k;               break;             }           }           if (r != -1) dp[i][j] -= dp[r][j - 1];         }       }     long long int ans = 0;     for (long long int i = n; i >= 0; i--) {       if (dp[n][i] >= k) {         ans += k * (n - i);         k = 0;         break;       } else {         ans += dp[n][i] * (n - i);         k -= dp[n][i];       }     }     if (k == 0) {       cout << ans <<   n ;     } else {       cout <<  -1 n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, tem;     cin >> s;     int m = s.size();     for (int i = 0; i < m; i++) {       if (s[i] !=  a ) tem += s[i];     }     int n = tem.size() / 2;     string sub1 = tem.substr(0, n);     string sub2 = s.substr(m - n, m);     if (sub1 == sub2 && tem.size() % 2 == 0) {       for (int i = 0; i < (m - n); i++) {         cout << s[i];       }     } else {       cout <<  :(  << endl;     }     return 0;   }


#include <bits/stdc++.h>   bool m1;   FILE *Input = stdin, *Output = stdout;   const int maxn = 1e5 + 5;   struct Edge {     int to, next;     Edge(){};     Edge(int ar1, int ar2) {       to = ar1;       next = ar2;     }   } Tree_List[maxn << 1], Tree2_List[maxn << 2];   int Numt[maxn], tott, deg[maxn], totn;   int Numt2[maxn << 1], tott2, size[maxn << 1], Max_Len2;   int Start_Mark, End_Mark, All_Mark[maxn << 1];   int Start, End, All[maxn << 1], Max_Len, next[maxn];   bool Found, onLine[maxn << 1], Mark[maxn << 1];   bool m2;   void Add_Tree(int ar1, int ar2) {     Tree_List[++tott] = Edge(ar2, Numt[ar1]);     Numt[ar1] = tott;   }   void Add_Tree2(int ar1, int ar2) {     Tree2_List[++tott2] = Edge(ar2, Numt2[ar1]);     Numt2[ar1] = tott2;   }   void Get_next(int ar1, int ar2) {     next[ar1] = ar1;     for (int i = Numt[ar1]; i; i = Tree_List[i].next) {       int hh = Tree_List[i].to;       if (hh == ar2) continue;       Get_next(hh, ar1);       if (deg[ar1] == 2) next[ar1] = next[hh];     }   }   void Link(int ar1, int ar2) {     for (int i = Numt[ar1]; i; i = Tree_List[i].next) {       int hh = Tree_List[i].to;       if (hh == ar2 || next[hh] == ar1) continue;       if (deg[next[hh]] == 1 || deg[ar1] == 1) {         Add_Tree2(ar1, next[hh]);         Add_Tree2(next[hh], ar1);         Link(next[hh], ar1);       } else {         Add_Tree2(ar1, hh);         Add_Tree2(hh, ar1);         Link(hh, ar1);       }     }   }   void Get_One_Mark(int ar1, int ar2, int ar3) {     if (Mark[ar1] && ar3 > Max_Len) {       Start_Mark = ar1;       Max_Len = ar3;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_One_Mark(hh, ar1, ar3 + 1);     }   }   void Get_the_Other_Mark(int ar1, int ar2, int ar3) {     if (Mark[ar1] && ar3 > Max_Len) {       End_Mark = ar1;       Max_Len = ar3;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_the_Other_Mark(hh, ar1, ar3 + 1);     }   }   void Get_One(int ar1, int ar2, int ar3) {     if (ar3 > Max_Len) {       Start = ar1;       Max_Len = ar3;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_One(hh, ar1, ar3 + 1);     }   }   void Get_the_Other(int ar1, int ar2, int ar3) {     if (ar3 > Max_Len) {       End = ar1;       Max_Len = ar3;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_the_Other(hh, ar1, ar3 + 1);     }   }   void Get_All(int ar1, int ar2, int ar3) {     All[++All[0]] = ar1;     if (ar1 == ar3) {       Found = 1;       return;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_All(hh, ar1, ar3);       if (Found) return;     }     All[0]--;   }   void Get_All_Mark(int ar1, int ar2, int ar3) {     if (Mark[ar1]) All[++All[0]] = ar1;     if (ar1 == ar3) {       Found = 1;       return;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_All_Mark(hh, ar1, ar3);       if (Found) return;     }     if (Mark[ar1]) All[0]--;   }   void Get_Two(int ar1, int ar2, int ar3) {     if (ar3 > Max_Len) {       Max_Len2 = Max_Len;       End = Start;       Max_Len = ar3;       Start = ar1;     } else if (ar3 > Max_Len2) {       Max_Len2 = ar3;       End = ar1;     }     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_Two(hh, ar1, ar3 + 1);     }   }   void Get_size(int ar1, int ar2) {     size[ar1] = 1;     for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) {       int hh = Tree2_List[i].to;       if (hh == ar2) continue;       Get_size(hh, ar1);       size[ar1] += size[hh];     }   }   int main() {     int n;     while (fscanf(Input,  %d , &n) == 1) {       memset(Numt, 0, sizeof(Numt));       memset(Numt2, 0, sizeof(Numt2));       memset(deg, 0, sizeof(deg));       memset(Mark, 0, sizeof(Mark));       memset(onLine, 0, sizeof(onLine));       tott = 0;       totn = n;       tott2 = 0;       for (int i = 1; i < n; i++) {         int h1, h2;         fscanf(Input,  %d %d , &h1, &h2);         Add_Tree(h1, h2);         Add_Tree(h2, h1);         deg[h1]++;         deg[h2]++;       }       int Root = 1;       while (Root <= n && deg[Root] == 2) Root++;       if (Root > n) abort();       Get_next(Root, 0);       Link(Root, 0);       bool Could = 1;       Max_Len = 0;       Get_One(Root, 0, 0);       Max_Len = 0;       Get_the_Other(Start, 0, 0);       Found = All[0] = 0;       Get_All(Start, 0, End);       for (int i = 1; i <= All[0]; i++) onLine[All[i]] = 1;       for (int i = 1; i <= All[0]; i++) {         bool flag = 0;         for (int j = Numt2[All[i]]; j; j = Tree2_List[j].next) {           int hh = Tree2_List[j].to;           if (onLine[hh]) continue;           Get_size(hh, All[i]);           if (size[hh] > 3) {             flag = 1;             break;           }           if (size[hh] == 2) abort();         }         if (flag) {           Could = 0;           break;         }       }       if (Could)         fprintf(Output,  Yes n );       else         fprintf(Output,  No n );     }     fclose(Input);     fclose(Output);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool mmax(T &m, const T q) {     if (m < q) {       m = q;       return true;     } else       return false;   }   template <typename T>   bool mmin(T &m, const T q) {     if (m > q) {       m = q;       return true;     } else       return false;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long l, r, k;     cin >> l >> r >> k;     long long p = 1, f = 0;     long long ans = 0;     while (1) {       if (p >= l) {         if (p <= r) {           f = 1;           cout << p <<    ;         } else           break;       }       if ((p) > 1e18 / k) break;       p *= k;     }     if (f == 0) cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool mustnot[100005];   char opr[100005];   int sum[100005];   int apr[100005];   int sat[100005];   int res[100005];   int a[100005];   int n, m, rc;   void work() {     int i;     memset(apr, -1, sizeof(apr));     for (i = 1; i <= m; ++i)       if (opr[i] ==  + ) {         if (sat[a[i]] != i - 1) mustnot[a[i]] = 1;         apr[a[i]] = 0;         sat[a[i]] = i;       } else {         ++sum[sat[a[i]] + 1];         --sum[i];         apr[a[i]] = 1;         sat[a[i]] = i;       }     for (i = 1; i <= n; ++i) {       if (apr[i] == 1) {         if (sat[i] != m) mustnot[i] = 1;       }       if (apr[i] == 0) ++sum[sat[i] + 1], --sum[m + 1];     }     for (i = 1; i <= m; ++i) sum[i] += sum[i - 1];     for (i = 1; i <= m; ++i)       if (sum[i]) mustnot[a[i]] = 1;     for (i = 1; i <= n; ++i)       if (!mustnot[i]) res[++rc] = i;   }   int main() {     int i;     scanf( %d%d n , &n, &m);     for (i = 1; i <= m; ++i) scanf( %c %d n , &opr[i], &a[i]);     work();     printf( %d n , rc);     for (i = 1; i <= rc; ++i) printf( %d  , res[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int Q;     cin >> Q;     while (Q--) {       string s;       cin >> s;       vector<int> num, vec;       int n = s.size();       for (int i = 4; i < n; i++) num.push_back(s[i] -  0 );       n = num.size();       int x = num.back();       unsigned long long ans = 1989;       while (ans % 10 != x) ans++;       num.pop_back();       vec.push_back(x);       ans /= 10;       while (num.size()) {         x = num.back();         ans++;         while (ans % 10 != x) ans++;         ans /= 10;         vec.push_back(x);         num.pop_back();       }       if (ans) cout << ans;       for (int i = n - 1; i >= 0; i--) cout << vec[i];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void chmax(T& a, const T& b) {     if (a < b) a = b;   }   template <typename T>   void chmin(T& a, const T& b) {     if (a > b) a = b;   }   int N, M;   long long dp[3001][3001];   const long long INF = 1e17;   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     cin >> N;     vector<int> A(N), B(N);     for (int i = (0); i < (N); i++) {       cin >> A[i];       A[i] -= i;       B[i] = A[i];     }     sort((B).begin(), (B).end());     B.erase(unique((B).begin(), (B).end()), B.end());     M = B.size();     fill(dp[0], dp[N + 1], INF);     dp[0][0] = 0;     for (int i = (0); i < (N); i++) {       for (int j = (0); j < (M); j++) {         chmin(dp[i + 1][j], dp[i][j] + abs(A[i] - B[j]));         chmin(dp[i][j + 1], dp[i][j]);       }     }     long long ans = INF;     for (int i = (0); i < (M); i++) {       chmin(ans, dp[N][i]);     }     cout << ans << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2000005;   const long long mod = 998244353;   long long n, ans, cn[maxn], pow3[maxn], pow1m[maxn];   void exgcd(long long a, long long b, long long &x, long long &y) {     if (!b) {       x = 1, y = 0;     } else {       exgcd(b, a % b, y, x);       y -= x * (a / b);     }   }   long long inv(long long a) {     long long x, y;     exgcd(a, mod, x, y);     return (x + mod) % mod;   }   long long quickpow(long long a, long long b) {     a %= mod;     (a += mod) %= mod;     long long ans = 1;     while (b) {       if (b & 1) (ans *= a) %= mod;       (a *= a) %= mod;       b >>= 1;     }     return ans;   }   int main() {     cin >> n;     cn[0] = pow3[0] = pow1m[0] = 1;     for (long long i = 1; i <= n; ++i) {       cn[i] = (cn[i - 1] * (n - i + 1) % mod) * inv(i) % mod;     }     for (long long i = 1; i < maxn; ++i) {       pow3[i] = pow3[i - 1] * 3 % mod;       pow1m[i] = mod - pow1m[i - 1];     }     for (long long i = 0; i < n; ++i) {       (ans += cn[i] * pow1m[i + 1] % mod *               ((quickpow(1 - pow3[i], n) - quickpow(-pow3[i], n) + mod) % mod) %               mod) %= mod;     }     (ans *= 3) %= mod;     for (int i = 1; i <= n; ++i) {       (ans += cn[i] * pow1m[i + 1] % mod * pow3[i] % mod *               quickpow(pow3[n], n - i) % mod * 2 % mod) %= mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   int main() {     long long int n, m, i, j, x, y, l = INF, u = 0;     cin >> n >> m;     if (m == 0)       cout << n - 1 << endl;     else {       for (i = 0; i < m; i++) {         cin >> x >> y;         if (y < x) swap(x, y);         u = max(u, x);         l = min(l, y);       }       if (l - u > 0)         cout << l - u << endl;       else         cout << 0 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   ostream& operator<<(ostream& a, const vector<int>& b) {     for (auto k : b) cout << k <<    ;     return a;   }   istream& operator>>(istream& a, pair<int, int>& b) {     cin >> b.first >> b.second;     return a;   }   ostream& operator<<(ostream& a, const pair<int, int>& b) {     cout <<  {  << b.first <<  ,   << b.second <<  } ;     return a;   }   template <typename T1, typename T2>   inline void chkmin(T1& x, const T2& y) {     if (x > y) x = y;   }   template <typename T1, typename T2>   inline void chkmax(T1& x, const T2& y) {     if (x < y) x = y;   }   const int INF = 1e9;   mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());   const int MAXN = 2001;   int n;   int dp[MAXN][MAXN];   vector<vector<int>> prefs, preft;   string s, t;   bool have(int i, int j, char k) {     if (j == 0) return preft[i - 1][k -  a ];     return preft[i - 1][k -  a ] - prefs[j - 1][k -  a ];   }   void solve() {     cin >> n;     cin >> s >> t;     prefs.assign(n, vector<int>(26, 0));     preft.assign(n, vector<int>(26, 0));     prefs[0][s[0] -  a ] = 1;     for (int i = 1; i < n; i++) {       prefs[i] = prefs[i - 1];       prefs[i][s[i] -  a ]++;     }     preft[0][t[0] -  a ] = 1;     for (int i = 1; i < n; i++) {       preft[i] = preft[i - 1];       preft[i][t[i] -  a ]++;     }     if (prefs[n - 1] != preft[n - 1]) {       cout <<  -1 n ;       return;     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= n; j++) {         dp[i][j] = INF;       }     }     dp[0][0] = 0;     for (int i = 0; i < n; i++) {       dp[i + 1][0] = i + 1;       for (int j = 0; j <= i; j++) {         if (have(i + 1, j, s[j]) && dp[i + 1][j + 1] > dp[i + 1][j]) {           dp[i + 1][j + 1] = dp[i + 1][j];         }         if (s[j] == t[i] && dp[i + 1][j + 1] > dp[i][j]) {           dp[i + 1][j + 1] = dp[i][j];         }         if (dp[i + 1][j + 1] > dp[i][j + 1] + 1) {           dp[i + 1][j + 1] = dp[i][j + 1] + 1;         }       }     }     cout << dp[n][n] <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     while (q--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x7f7f7f7f;   const int maxn = 1111;   const int mod = 1000000007;   const double eps = 1e-8;   bool vis[maxn][maxn];   int n, k;   vector<pair<int, int> > ans;   bool solve() {     for (int i = 1; i <= n; i++) {       int cnt = 0;       for (int j = 1; j <= n; j++) {         if (vis[i][j]) continue;         if (i == j) continue;         vis[i][j] = vis[j][i] = true;         cnt++;         ans.push_back(make_pair(i, j));         if (cnt == k) break;       }       if (cnt != k) return false;     }     return true;   }   int main() {     while (cin >> n >> k) {       if (!solve())         puts( -1 );       else {         cout << ((int)ans.size()) << endl;         for (int i = 0; i < ((int)ans.size()); i++)           printf( %d %d n , ans[i].first, ans[i].second);       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int i, ar[3];     cin >> ar[0] >> ar[1] >> ar[2];     int person = 0;     for (i = 0; i < 3; i++)       if (ar[i] > 0) {         ar[i]--;         person++;       }     sort(ar, ar + 3);     if (ar[0] > 0 && ar[2] > 0) {       person++;       ar[0]--;       ar[2]--;     }     if (ar[1] > 0 && ar[2] > 0) {       person++;       ar[2]--;       ar[1]--;     }     if (ar[0] > 0 && ar[1] > 0) {       person++;       ar[0]--;       ar[1]--;     }     if (ar[0] && ar[1] && ar[2]) {       person++;     }     cout << person << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int T = 1;     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int maxm = 1e5 + 5;   const int INF = 0x3f3f3f3f;   int main() {     int l, p, q;     scanf( %d%d%d , &l, &p, &q);     printf( %.10lf n , l * 1.0 / (p + q) * 1.0 * p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int maxn = 1e6;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, m, k;     cin >> n >> m >> k;     vector<int> a(maxn, 0);     for (int i = 0; i < n; i++) {       int x;       cin >> x;       x--;       a[x] = 1;     }     int ans = 0;     int cur = 0;     for (int i = 0; i < maxn; i++) {       cur += a[i];       if (i >= m) cur -= a[i - m];       if (cur == k) ans++, cur--, a[i] = 0;     }     cout << ans;   }


//manish kumar patel   #include<bits/stdc++.h>   using namespace std;            #define ll long long   #define ld long double   #define rep(i,a,b) for(ll i=a;i<b;i++)   #define repb(i,a,b) for(ll i=a;i>=b;i--)   #define err() cout<< -------------------------- <<endl;    #define errA(A) for(auto i:A)   cout<<i<<   ;cout<<endl;   #define err1(a) cout<<#a<<   <<a<<endl            #define err2(a,b) cout<<#a<<   <<a<<   <<#b<<   <<b<<endl   #define err3(a,b,c) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<endl   #define err4(a,b,c,d) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<   <<#d<<   <<d<<endl   #define pb push_back   #define all(A)  A.begin(),A.end()   #define allr(A)    A.rbegin(),A.rend()   #define ft first   #define sd second      #define pii pair<int,int>   #define pll pair<ll,ll>   #define V vector<ll>   #define S set<ll>   #define VV vector<V>   #define Vpll vector<pll>    #define yupp cout<< YES <<endl;   #define nope cout<< NO <<endl;   #define endl   n    // bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)    // {    //     return (a.second < b.second);    // }    // int parent(int i,V &a)   // {   //     if(a[i]==i)   //         return i;   //     return parent(a[i],a);   // }   // void make_par(V &a,int x,int y)   // {   //     int para=parent(x,a);   //     int parb=parent(y,a);   //     a[parb]=para;   // }   // // int comb[1001][1001];   // // rep(i,0,1001)   // // {   // //  rep(j,0,1001)   // //  {   // //   if(i==0 || j==0)   // //   {   // //    comb[i][j]=1;   // //   }   // //   else   // //   {   // //    comb[i][j]=comb[i-1][j-1]+comb[i-1][j];   // //   }   // //  }   // // }   int divi(int n)   {    int kk=-1;    repb(i,sqrt(n)+1,2)    {     if(n%i==0)      return i;    }    return -1;   }   int main()       {              #ifndef ONLINE_JUDGE            clock_t tStart = clock();           freopen( input.txt , r ,stdin);           freopen( output.txt , w ,stdout);           #endif           int t;           cin>>t;           // t=1;                while(t--)           {            int n;            cin>>n;            ll *a=new ll [n];            ll s=0;            rep(i,0,n)            {             cin>>a[i];             s+=a[i];            }            sort(a,a+n);            ll x=0;            rep(i,0,n)            {             x=max(x,a[i]*(n-1)-s);            }            cout<<max(x,((s+n-2)/(n-1))*(n-1)-s)<<endl;           }           #ifndef ONLINE_JUDGE               printf(  nRun Time -> %.10fs n , (double)(clock()-tStart) / CLOCKS_PER_SEC);               #endif   }


#include <bits/stdc++.h>   using namespace std;   int n, k, t[300010], u, v, a[300010], conf[300010], tc[300010], check[300010];   long long dp[300010][2];   vector<int> edge[300010];   const long long MOD = 998244353;   long long add(long long a, long long b) {     a += b;     if (a >= MOD) a -= MOD;     return a;   }   long long times(long long a, long long b) {     a *= b;     a %= MOD;     return a;   }   long long fexp(long long m) {     long long h = MOD - 2ll;     long long r = 1ll;     while (h > 0) {       m %= MOD;       if (h & 1ll) r *= m;       m *= m;       h >>= 1ll;       r %= MOD;     }     return r;   }   void conect(int vert, int dis) {     for (int i = 0; i < t[dis]; i++)       if (edge[dis][i] != vert) {         edge[vert].push_back(edge[dis][i]);         t[vert]++;         for (int j = 0; j < t[edge[dis][i]]; j++)           if (edge[edge[dis][i]][j] == dis) {             edge[edge[dis][i]][j] = vert;             break;           }       }     t[dis] = 0;   }   void super_node(int vert, int color) {     conf[vert] = -1;     check[vert] = a[vert];     int h;     if (a[vert] > 0) color = a[vert];     for (int i = 0; i < t[vert]; i++)       if (conf[edge[vert][i]] >= 0) {         if (conf[edge[vert][i]] == 0) super_node(edge[vert][i], color);         if (t[edge[vert][i]] == 1 && a[edge[vert][i]] == 0) {           t[edge[vert][i]] = 0;           swap(edge[vert][i], edge[vert][t[vert] - 1]);           edge[vert].pop_back();           t[vert]--;           i--;         } else if (check[edge[vert][i]] == color) {           tc[a[edge[vert][i]]]--;           conect(vert, edge[vert][i]);           swap(edge[vert][i], edge[vert][t[vert] - 1]);           edge[vert].pop_back();           t[vert]--;           i--;           if (a[vert] == 0) {             check[vert] = color;             a[vert] = color;             tc[color]++;           }         } else if (a[vert] == 0) {           if (check[vert] == check[edge[vert][i]]) {             color = check[vert];             tc[a[edge[vert][i]]]--;             conect(vert, edge[vert][i]);             swap(edge[vert][i], edge[vert][t[vert] - 1]);             edge[vert].pop_back();             i--;             t[vert]--;             swap(edge[vert][h], edge[vert][i]);             tc[a[edge[vert][i]]]--;             conect(vert, edge[vert][i]);             swap(edge[vert][i], edge[vert][t[vert] - 1]);             edge[vert].pop_back();             i--;             t[vert]--;             a[vert] = color;             tc[color]++;           } else if (check[vert] == 0) {             check[vert] = check[edge[vert][i]];             h = i;           } else if (tc[check[edge[vert][i]]] > 1 && check[vert] != color) {             check[vert] = check[edge[vert][i]];             h = i;           }         }       }     conf[vert] = 1;   }   void resp(int vert) {     conf[vert] = -1;     if (a[vert] == 0)       dp[vert][0] = 1;     else       dp[vert][1] = 1;     for (int i = 0; i < t[vert]; i++)       if (conf[edge[vert][i]] == 0) {         resp(edge[vert][i]);         dp[vert][0] =             times(dp[vert][0], add(dp[edge[vert][i]][0], dp[edge[vert][i]][1]));         dp[vert][1] =             times(dp[vert][1], add(dp[edge[vert][i]][0], dp[edge[vert][i]][1]));       }     if (a[vert] == 0) {       long long hf = 1;       for (int i = 0; i < t[vert]; i++)         if (conf[edge[vert][i]] == 1)           hf = times(hf, add(dp[edge[vert][i]][0], dp[edge[vert][i]][1]));       for (int i = 0; i < t[vert]; i++)         if (conf[edge[vert][i]] == 1) {           long long h = dp[edge[vert][i]][1];           h = times(h, hf);           h = times(h, fexp(add(dp[edge[vert][i]][0], dp[edge[vert][i]][1])));           dp[vert][1] = add(dp[vert][1], h);         }     }     conf[vert] = 1;   }   int main() {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       tc[a[i]]++;     }     tc[0] = 0;     for (int i = 1; i < n; i++) {       scanf( %d %d , &u, &v);       edge[u].push_back(v);       edge[v].push_back(u);       t[u]++;       t[v]++;     }     for (int i = 1; i <= n; i++)       if (a[i] > 0) {         super_node(i, a[i]);         break;       }     for (int i = 1; i <= k; i++)       if (tc[i] != 1) {         printf( 0 n );         return 0;       }     for (int i = 1; i <= n; i++) conf[i] = 0;     for (int i = 1; i <= n; i++)       if (t[i] > 0) {         resp(i);         printf( %lld n , dp[i][1]);         return 0;       }   }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long 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) {     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...);   }   template <typename T>   void printVec(const T &v, char sep =    ) {     for (auto &i : v) cout << i << sep;   }   template <typename T>   void printVecPair(const T &v, char sep =    ) {     for (auto &i : v) cout << i.first <<     << i.second << sep;   }   int n, k;   vector<string> dig;   string number[10];   int convert(string s1, string s2) {     int ans = 0;     for (int i = 0; i < 7; i++) {       if (s1[i] != s2[i]) {         if (s1[i] ==  0 )           ans++;         else           return -1;       }     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     dig.resize(n);     number[0] =  1110111 ;     number[1] =  0010010 ;     number[2] =  1011101 ;     number[3] =  1011011 ;     number[4] =  0111010 ;     number[5] =  1101011 ;     number[6] =  1101111 ;     number[7] =  1010010 ;     number[8] =  1111111 ;     number[9] =  1111011 ;     for (int i = 0; i < n; cin >> dig[i++])       ;     vector<vector<bool>> dp(n, vector<bool>(k + 1));     for (int i = 0; i <= 9; i++) {       int cost = convert(dig[n - 1], number[i]);       if (cost != -1 && cost <= k) dp[n - 1][cost] = true;     }     for (int i = n - 2; i >= 0; i--) {       for (int j = 0; j <= k; j++) {         for (int d = 0; d <= 9; d++) {           int cost = convert(dig[i], number[d]);           if (cost != -1 && j - cost >= 0 && dp[i + 1][j - cost]) {             dp[i][j] = true;           }         }       }     }     vector<int> ans(n);     for (int i = 0; i < n; i++) {       bool flag = 0;       for (int d = 9; d >= 0; d--) {         int cost = convert(dig[i], number[d]);         if (cost != -1 && k >= cost && i != n - 1 && dp[i + 1][k - cost]) {           flag = 1;           ans[i] = d;           k -= cost;           break;         }         if (cost == k && i == n - 1) {           ans[i] = d;           flag = 1;           k -= cost;           break;         }       }       if (!flag) {         cout <<  -1 n ;         return 0;       }     }     for (int i = 0; i < n; i++) cout << ans[i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool comp(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first && a.second < b.second);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int a[4], i;     for (i = 0; i < 4; i++) {       cin >> a[i];     }     sort(a, a + 4);     if (a[1] + a[2] > a[3] || a[2] < a[1] + a[0]) {       cout <<  TRIANGLE ;     } else if (a[1] + a[2] == a[3] || a[2] == a[1] + a[0]) {       cout <<  SEGMENT ;     } else {       cout <<  IMPOSSIBLE ;     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     vector<pair<int, int> > dp(n);     dp[n - 1].second = 0;     dp[n - 1].first = 1;     int mx = 0;     for (int i = n - 2; i > -1; i--) {       if (a[i + 1] > a[i])         dp[i].second = dp[i + 1].second + 1;       else         dp[i].second = 0;       if (a[i] < a[i + 1]) {         dp[i].first = dp[i + 1].first + 1;       } else         dp[i].first = 1;       if (i < n - 2 && a[i + 2] > a[i])         dp[i].second = max(dp[i].second, dp[i + 2].first + 1);       mx = max(mx, dp[i].first);       mx = max(mx, dp[i].second);     }     cout << mx;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   const long long MOD = 1e9 + 7;   long long s, x;   int a[50], n, ans0[1 << 21], ans1[1 << 21];   void work(int* ans, int s, int t) {     int len = (t - s + 1);     for (int i = 0; i < (1 << len); i++) {       int tem = 0;       for (int j = 1; j <= len; j++)         if ((i >> (j - 1)) & 1) {           if (a[s + j - 1] == 0) tem += 1 << j;           if (a[s + j - 1] == 1) tem += 1 << (j - 1);         } else {           if (a[s + j - 1] == 0) tem += 0;           if (a[s + j - 1] == 1) tem += 1 << (j - 1);         }       ans[++ans[0]] = tem;     }   }   int main() {     cin >> s >> x;     n = 0;     long long pp = x;     while (pp > 0) {       a[++n] = pp & 1;       pp = pp / 2;     }     int qq = 0, t = 0;     pp = s;     while (pp > 0) {       qq++;       pp /= 2;     }     n = max(n, qq);     t = n / 2;     work(ans0, 1, t);     work(ans1, t + 1, n);     sort(ans0 + 1, ans0 + ans0[0] + 1);     long long ans = 0;     for (int i = 1; i <= ans1[0]; i++) {       long long tem = s - (((long long)ans1[i]) << t);       ans += upper_bound(ans0 + 1, ans0 + ans0[0] + 1, tem) -              lower_bound(ans0 + 1, ans0 + ans0[0] + 1, tem);     }     if ((s xor 0) == x) ans -= 2;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, x, y;     cin >> n >> x >> y;     if (y < n) {       cout <<  -1  << endl;       return 0;     }     long long a = y - n + 1;     unsigned long long ans = 0;     ans += n - 1 + a * a;     if (ans < x) {       cout <<  -1  << endl;       return 0;     }     a = (y - n) + 1;     cout << a << endl;     for (int i = 0; i < n - 1; i++) cout << 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int size = 50 + 10;   int n, k, aim[size], num[size], ans = -1;   int main() {     cin >> n >> k;     if (k > n) {       cout << -1 << endl;       return 0;     }     for (int i = 1; i <= n; i++) cin >> aim[i];     sort(aim + 1, aim + n + 1);     num[1] = 0;     for (int i = 2; i <= n; i++)       if (aim[i] > aim[i - 1])         num[i] = i - 1;       else         num[i] = num[i - 1];     for (int i = 1; i <= n; i++)       if (aim[i] != aim[i + 1] && n - num[i] == k) ans = aim[i];     if (ans < 0) {       cout << ans << endl;       return 0;     }     cout << ans <<     << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long n, m;     long long mn, mx;     cin >> n >> m;     mn = n;     mn = max(0ll, n - 2 * m);     cout << mn <<    ;     long long cnt = 0;     while (m > 0) {       m -= cnt;       cnt++;     }     cout << max(0ll, n - cnt) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   void solve() {     string s;     cin >> s;     vector<vector<ll>> cnt(2, vector<ll>(2, 0));     ll e = 0, o = 0;     for (int i = 0; i < s.size(); i++) {       o++;       if (i % 2 == 0) {         o += cnt[s[i] -  a ][0];         e += cnt[s[i] -  a ][1];         cnt[s[i] -  a ][0]++;       } else {         e += cnt[s[i] -  a ][0];         o += cnt[s[i] -  a ][1];         cnt[s[i] -  a ][1]++;       }     }     cout << e <<     << o <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int tt = 0; tt < int(t); tt++) {       int a, b, c, r;       cin >> a >> b >> c >> r;       int L = max(min(a, b), c - r);       int R = min(max(a, b), c + r);       cout << max(a, b) - min(a, b) - max(0, R - L) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e3 + 5;   int a[maxn][maxn];   int col[maxn];   int deg[maxn];   bool vis[maxn];   int n;   void recurse() {     int piv = -1;     for (int i = 1; i <= n; i++) {       if (vis[i]) continue;       if (deg[i] % 2 == 1) {         piv = i;         break;       }     }     if (piv == -1) {       for (int i = 1; i <= n; i++) {         if (vis[i]) continue;         col[i] = 0;       }       return;     }     vis[piv] = true;     vector<int> adj;     for (int i = 1; i <= n; i++) {       if (vis[i]) continue;       if (a[piv][i] == 1) {         a[piv][i] = a[i][piv] = 0;         adj.push_back(i);         deg[i] -= 1;       }     }     for (auto i : adj) {       for (auto j : adj) {         if (i == j) continue;         if (a[i][j] == 1) {           a[i][j] = 0;           deg[i] -= 1;         } else {           a[i][j] = 1;           deg[i] += 1;         }       }     }     recurse();     int cnt[] = {0, 0};     for (int i : adj) {       cnt[col[i]] += 1;     }     col[piv] = cnt[0] % 2 == 0 ? 0 : 1;   }   void solve() {     int m;     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) a[i][j] = 0;     memset(deg, 0, sizeof deg);     memset(vis, false, sizeof vis);     for (int i = 1; i <= m; i++) {       int p, q;       cin >> p >> q;       a[p][q] = a[q][p] = 1;       deg[p] += 1;       deg[q] += 1;     }     recurse();     cout << *max_element(col + 1, col + n + 1) + 1 << endl;     for (int i = 1; i <= n; i++) cout << 1 + col[i] <<    ;     cout << endl;   }   int main() {     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     while (q--) {       int n, h;       cin >> h >> n;       int a[n];       for (int i = 0; i < n; ++i) cin >> a[i];       int cnt = 1;       vector<int> v;       for (int i = 1; i < n; ++i) {         if (a[i] == a[i - 1] - 1) {           ++cnt;         } else {           v.push_back(cnt);           cnt = 1;         }       }       v.push_back(cnt);       int sm = 0;       if (v.size() == 1) {         if (a[n - 1] < 2)           sm = 0;         else {           if (v[0] % 2 == 0)             sm = 1;           else             sm = 0;         }       } else {         for (int i = 0; i < v.size(); ++i) {           if (i == 0) {             if (v[i] % 2 == 0) ++sm;           } else if (i == v.size() - 1) {             ++v[i];             if (a[n - 1] >= 2 && v[i] % 2 == 0) ++sm;           } else {             ++v[i];             if (v[i] % 2 == 0) ++sm;           }         }       }       cout << sm << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   string a, ans;   int b;   string c;   int main() {     while (cin >> a >> b) {       for (int i = 0; i < a.size(); i++)         if (a[i] >=  A  && a[i] <=  Z ) a[i] = a[i] -  A  +  a ;       for (int i = 0; i < a.size(); i++) {         if ((int)a[i] < b + 97) {           a[i] = a[i] -  a  +  A ;         }       }       cout << a << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = (1 << 30) - 1;   const long long inf64 = ((long long)1 << 62) - 1;   const long double pi = 3.1415926535897932384626433832795;   template <class T>   T sqr(T x) {     return x * x;   }   template <class T>   T abs(T x) {     return x < 0 ? -x : x;   }   const int MAXN = 310000;   int a[MAXN], ind[MAXN], ind_cut[MAXN];   long long sum[MAXN];   bool comp(int ind1, int ind2) {     return ((a[ind1] < a[ind2]) || (a[ind1] == a[ind2] && ind1 < ind2));   }   int main() {     int n;     cin >> n;     sum[0] = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);       sum[i + 1] = (a[i] > 0 ? sum[i] + a[i] : sum[i]);       ind[i] = i;     }     sort(ind, ind + n, comp);     int i = 0;     int ind_l, ind_r;     long long max_sum = -2 * inf;     ind[n] = n;     a[n] = inf;     while (i < n) {       int j = i;       while (a[ind[j + 1]] == a[ind[i]]) j++;       long long temp = sum[ind[j]] - sum[ind[i] + 1] + 2 * a[ind[i]];       if (j > i && temp > max_sum) {         max_sum = temp;         ind_l = ind[i];         ind_r = ind[j];       }       i = j + 1;     }     int num = 0;     for (int i = 0; i < ind_l; ++i) {       ind_cut[num] = i + 1;       num++;     }     for (int i = ind_l + 1; i < ind_r; ++i) {       if (a[i] < 0) {         ind_cut[num] = i + 1;         num++;       }     }     for (int i = ind_r + 1; i < n; ++i) {       ind_cut[num] = i + 1;       num++;     }     cout << max_sum <<     << num << endl;     for (int i = 0; i < num; ++i) {       cout << ind_cut[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int c, hr, hb, wr, wb;     cin >> c >> hr >> hb >> wr >> wb;     long long int ret = 0, ret1 = 0;     int cnt = 0;     for (long long int i = 0; i <= (c / wr); i++) {       long long int r = i * wr;       long long int l = c - r;       long long int b = l / wb;       ret1 = i * hr + b * hb;       ret = max(ret, ret1);       cnt++;       if (cnt > 1e7) break;     }     cnt = 0;     for (long long int i = 0; i <= (c / wb); i++) {       long long int r = i * wb;       long long int l = c - r;       long long int b = l / wr;       ret1 = i * hb + b * hr;       ret = max(ret, ret1);       cnt++;       if (cnt > 1e7) break;     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     while (scanf( %d , &n) != EOF) {       printf( %d n , n * 3 / 2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> st;   int n;   long long w[4] = {1, 5, 10, 50};   bool vis[1280][105000];   void doit(int i, int s) {     if (i == n) {       st.insert(s);       return;     }     if (vis[i][s]) return;     vis[i][s] = 1;     for (int j = 0; j < 4; j++) doit(i + 1, w[j] + s);   }   long long ans[12] = {0, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292};   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     if (n <= 11)       cout << ans[n] << endl;     else       cout << ans[11] + (n - 11) * 49LL << endl;     ;   }


#include <bits/stdc++.h>   using namespace std;   long long mi, ma, n, d, k, l, r, mid, a[500007], b[500007], cnt[500007];   inline long long read() {     long long s = 0, w = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) w = -1;     for (; isdigit(c); c = getchar()) s = (s << 3) + (s << 1) + (c ^ 48);     return (s * w);   }   inline bool check() {     long long num = 0, sum = 0;     for (int i = 1; i <= n; i++) {       long long delta = mid - cnt[i] - b[i] - sum;       if (delta <= 0) {         sum = sum + b[i];         continue;       }       num = num + delta;       if (num > k) {         for (int j = min(i + 2 * d, n); j; j--) b[j] = 0;         return false;       }       b[i] = b[i] + delta;       if (i + 2 * d < n) b[i + 2 * d + 1] = b[i + 2 * d + 1] - delta;       sum = sum + b[i];     }     for (int i = 1; i <= n; i++) b[i] = 0;     return true;   }   int main() {     n = read(), d = read(), k = read();     mi = LLONG_MAX;     for (int i = 1; i <= n; i++) {       long long x = read();       a[max(1LL, i - d)] = a[max(1LL, i - d)] + x;       if (i + d < n) a[i + d + 1] = a[i + d + 1] - x;     }     for (int i = 1; i <= n; i++)       cnt[i] = cnt[i - 1] + a[i], ma = max(ma, cnt[i]), mi = min(mi, cnt[i]);     l = mi, r = ma + k;     while (l < r) {       mid = (l + r + 1) >> 1;       if (check())         l = mid;       else         r = mid - 1;     }     printf( %lld n , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005];   long long dp[100005];   int main() {     int n;     memset(a, 0, sizeof(a));     memset(dp, 0, sizeof(dp));     cin >> n;     int x;     for (int i = 0; i < n; i++) {       cin >> x;       a[x]++;     }     dp[1] = a[1];     dp[0] = 0LL;     for (int i = 2; i <= 100002; i++) {       dp[i] = max(dp[i - 1], dp[i - 2] + i * a[i]);     }     cout << dp[100002] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve(bool can_be_equal, const string &sa, vector<int> num_digits) {     int received = 0;     int given = 0;     int offset = can_be_equal ? 0 : 1;     for (char c : sa) {       int d = c -  0 ;       bool found = false;       for (int search = d + offset; search < 10; ++search) {         if (num_digits[search] != 0) {           num_digits[search]--;           found = true;           given++;           break;         }       }       if (!found) {         received++;         for (int search = 0; search < d + offset; ++search) {           if (num_digits[search] != 0) {             num_digits[search]--;             found = true;             break;           }         }       }     }     return can_be_equal ? received : given;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     string sa, sb;     cin >> sa;     cin >> sb;     vector<int> num_digits(10);     for (char c : sb) {       num_digits[c -  0 ]++;     }     cout << solve(true, sa, num_digits) << endl;     cout << solve(false, sa, num_digits) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long bigmod(long long a, int p, long long int m) {     if (p == 0) return 1;     if (p & 1) {       return ((a % m) * (bigmod(a, p - 1, m))) % m;     } else {       long long tmp = bigmod(a, p / 2, m);       return (tmp * tmp) % m;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int chu;     cin >> chu;     while (chu--) {       long long int n;       cin >> n;       long long int x;       cin >> x;       long long int a[n];       for (long long int i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       reverse(a, a + n);       long long int ans = 0;       long long int i = 0;       long long int y = 1;       for (i = 0; i < n; i++) {         if (a[i] * y >= x)           ans++, y = 1;         else           y++;       }       cout << ans;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int main() {     int k;     string s, st;     cin >> k >> s;     map<char, int> m;     set<char> s1;     if (s.length() % k) {       cout << -1;       return 0;     }     for (auto i = 0; i < s.length(); i++) {       if (m.find(s[i]) == m.end())         m.insert({s[i], 1});       else         m[s[i]]++;       s1.insert(s[i]);     }     for (auto i = m.begin(); i != m.end(); ++i) {       if (i->second % k) {         cout << -1;         return 0;       }     }     for (auto i = s1.begin(); i != s1.end(); ++i) {       while (m[*i] != 0) {         st += *i;         m[*i] -= k;       }     }     for (auto i = 0; i < k; i++) cout << st;   }


#include <bits/stdc++.h>   using namespace std;   template <typename _T>   inline void read(_T &f) {     f = 0;     _T fu = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) {         fu = -1;       }       c = getchar();     }     while (c >=  0  && c <=  9 ) {       f = (f << 3) + (f << 1) + (c & 15);       c = getchar();     }     f *= fu;   }   template <typename T>   void print(T x) {     if (x < 0) putchar( - ), x = -x;     if (x < 10)       putchar(x + 48);     else       print(x / 10), putchar(x % 10 + 48);   }   template <typename T>   void print(T x, char t) {     print(x);     putchar(t);   }   const int N = 105, md = 1e9 + 7;   inline int add(int x, int y) {     x += y;     if (x >= md) x -= md;     return x;   }   inline void addx(int &x, int y) {     x += y;     if (x >= md) x -= md;   }   inline int sub(int x, int y) {     x -= y;     if (x < 0) x += md;     return x;   }   inline void subx(int &x, int y) {     x -= y;     if (x < 0) x += md;   }   inline int mul(int x, int y) { return 1ll * x * y % md; }   inline int fpow(int x, int y) {     int ans = 1;     while (y) {       if (y & 1) ans = mul(ans, x);       y >>= 1;       x = mul(x, x);     }     return ans;   }   vector<int> adj[N];   int f[N][N][N], ans[N], siz[N], c[N][N], tmp[N][N];   int n;   void conv(int a[N][N], int b[N][N], int &lena, int lenb) {     for (int i = 0; i <= lena + lenb; i++) {       for (int j = 0; j <= lena + lenb; j++) tmp[i][j] = 0;     }     for (int i = 0; i <= lena; i++) {       for (int j = 0; j <= lena; j++) {         if (a[i][j] == 0) continue;         for (int x = 0; x <= lenb; x++) {           for (int y = 0; y <= lenb; y++) {             if (b[x][y] == 0) continue;             addx(tmp[i + x + 1][j], mul(mul(a[i][j], b[x][y]), y));             addx(tmp[i + x][j + y], mul(a[i][j], b[x][y]));           }         }       }     }     lena += lenb;     for (int i = 0; i <= lena; i++) {       for (int j = 0; j <= lena; j++) a[i][j] = tmp[i][j];     }   }   void dfs1(int u, int fa) {     siz[u] = 1;     f[u][0][1] = 1;     for (auto v : adj[u]) {       if (v == fa) continue;       dfs1(v, u);       conv(f[u], f[v], siz[u], siz[v]);     }   }   int main() {     read(n);     c[0][0] = 1;     for (int i = 1; i <= n; i++) {       c[i][0] = 1;       for (int j = 1; j <= i; j++) c[i][j] = add(c[i - 1][j - 1], c[i - 1][j]);     }     for (int i = 1; i < n; i++) {       int u, v;       read(u);       read(v);       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs1(1, 0);     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= n; j++) {         if (f[1][i][j] == 0) continue;         addx(ans[n - i - 1], mul(f[1][i][j], j));       }     }     for (int i = n - 1; i >= 0; i--) {       if (i != n - 1)         ans[i] = mul(ans[i], fpow(n, n - i - 2));       else         ans[i] = 1;       for (int j = i + 1; j <= n - 1; j++) subx(ans[i], mul(ans[j], c[j][i]));     }     for (int i = 0; i <= n - 1; i++) print(ans[i],    );     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve(int test_number);   int main() {     cout.setf(ios::fixed);     cout.precision(9);     cerr.setf(ios::fixed);     cerr.precision(3);     int n = 1;     for (int i = 0; i < n; i++) {       solve(i + 1);     }     return 0;   }   const int MAXN = 200100;   const int BUBEN = 800;   int n;   int a[MAXN];   int d[MAXN];   int nd;   bool used[BUBEN][MAXN];   int sum[BUBEN];   vector<int> q[MAXN];   int st[MAXN];   inline int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   void solve(int test_number) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i * i <= n; i++) {       if (n % i == 0) {         d[nd++] = i;         int j = n / i;         if (i != j && j != n) d[nd++] = j;       }     }     sort(d, d + nd);     for (int j = 0; j < nd; j++) {       int step = d[j];       for (int from = 0; from < step; from++) {         int mx = a[from];         for (int i = from; i < n; i += step) {           mx = max(mx, a[i]);         }         for (int i = from; i < n; i += step) {           if (mx == a[i]) {             used[j][i] = true;           }         }       }       int last = -1;       int pref = 0;       for (int i = 0; i < n; i++)         if (used[j][i]) {           if (last == -1) last = i;         } else if (last != -1) {           if (last == 0) {             pref = i - last;           } else {             q[j].push_back(i - last);             sum[j] += i - last;           }           last = -1;         }       if (last != -1) {         pref += n - last;       }       q[j].push_back(pref);       sum[j] += pref;       sort(q[j].begin(), q[j].end());     }     long long res = 0;     for (int len = 1; len < n; len++) {       int step = gcd(len, n);       long long j = lower_bound(d, d + nd, step) - d;       while ((int)q[j].size() != st[j] && q[j][st[j]] < len) {         sum[j] -= q[j][st[j]];         st[j]++;       }       if (sum[j] == n) {         res += (long long)n;       } else {         res += (long long)sum[j] -                (long long)(len - 1) * (long long)(q[j].size() - st[j]);       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int a, b, c;       cin >> a >> b >> c;       int ans = -1;       for (int x = 0; x < a + 1; x++) {         for (int y = 0; y <= c; y += 2) {           if (y / 2 + 2 * x <= b) {             ans = max(ans, 3 * x + y + y / 2);           }         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using ll = long long;   using ld = long double;   using namespace std;   void debugi(vector<int>& vec) {     int n = (int)vec.size();     for (int i = 0; i < n; i++) {       cout << vec[i] <<    ;     }     cout << endl;   }   void debugd(vector<long double>& vec) {     int n = (int)vec.size();     for (int i = 0; i < n; i++) {       cout << vec[i] <<    ;     }     cout << endl;   }   void debugl(vector<ll>& vec) {     int n = (int)vec.size();     for (int i = 0; i < n; i++) {       cout << vec[i] <<    ;     }     cout << endl;   }   void debugmap(map<int, int>& mp) {     for (auto x : mp) {       cout << x.first <<     << x.second <<   ,  ;     }     cout << endl;   }   void debugset(set<int>& st) {     for (auto x : st) {       cout << x <<    ;     }     cout << endl;   }   void debugparri(vector<pair<int, int>>& arr) {     for (int i = 0; i < (int)arr.size(); i++) {       cout <<  [  << arr[i].first <<   :   << arr[i].second <<  ]             <<    ;     }     cout << endl;   }   void debugpi(pair<int, int>& pi) {     cout << pi.first <<     << pi.second << endl;   }   void debugq(queue<int> q) {     while (!q.empty()) {       cout << q.front() <<    ;       q.pop();     }     cout << endl;   }   bool check(int n, int i, int j) {     if ((0 <= i & i < n) && (0 <= j & j < n)) {       return true;     }     return false;   }   ll MOD = 1e9 + 7;   ll powab(ll a, ll b, ll mod) {     if (b == 0) {       return 1;     }     ll z = powab(a, b / 2, mod);     ll res = (z * z * 1LL) % mod;     if (b & 1) {       res = (res * 1LL * a) % mod;     }     return res;   }   ll modinv(ll a, ll mod) {     ll ans = powab(a, mod - 2, mod);     return ans;   }   ll gcd(ll a, ll b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   void upd(int k, int x, vector<ll>& tree, int n) {     if (k == 0) {       return;     }     while (k <= n) {       tree[k] += x;       k += (k & -k);     }   }   ll sum(int k, vector<ll>& tree, int n) {     ll s = 0;     while (k >= 1) {       s += tree[k];       k -= (k & -k);     }     return s;   }   void solve(int tt, int t) {     int n, m, k, rn;     cin >> n >> m >> k;     rn = n;     int lim = 5050;     vector<int> dp(lim), lst(lim, -1);     vector<int> arr;     if (m == 1) {       if (k != 0) {         cout <<  NO  << endl;         return;       }       if (n % 2 != 0) {         cout <<  NO   << endl;         return;       }       vector<vector<char>> grid(n, vector<char>(m));       int ct = 1;       for (int i = 0; i < n; i++) {         if (i % 2 == 0 && ct) {           grid[i][0] =  a ;           grid[i + 1][0] =  a ;           ct ^= 1;         } else if (i % 2 == 0 && !ct) {           grid[i][0] =  b ;           grid[i + 1][0] =  b ;           ct ^= 1;         }       }       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cout << grid[i][j];         }         cout << endl;       }       return;     }     vector<vector<int>> grid(n, vector<int>(m, -1));     dp[0] = 1;     if (n % 2 != 0) {       if (k < m / 2) {         cout <<  NO  << endl;         return;       }       k -= m / 2;       int ct = 1;       for (int i = 0; i < m; i += 2) {         if (ct) {           grid[n - 1][i] = 1;           grid[n - 1][i + 1] = 1;           ct ^= 1;         } else {           grid[n - 1][i] = 0;           grid[n - 1][i + 1] = 0;           ct ^= 1;         }       }       n -= 1;     }     if (n % 2 == 0) {       for (int i = 2; i <= n; i += 2) {         arr.push_back(i);       }     } else {       for (int i = 1; i <= n; i += 2) {         arr.push_back(i);       }     }     reverse(arr.begin(), arr.end());     queue<pair<int, int>> q;     q.push({0, 0});     vector<int> vis(lim);     vis[0] = 1;     while (!q.empty()) {       int cur = q.front().first, dis = q.front().second;       q.pop();       for (auto x : arr) {         if (x + cur < lim) {           if (vis[x + cur] == 0) {             vis[x + cur] = 1;             q.push({x + cur, dis + 1});             lst[x + cur] = cur;             dp[x + cur] = dis + 1;           }         }       }     }     n = rn;     if (vis[k] == 1 && dp[k] <= m / 2) {       vector<int> nums;       int cur = k;       while (cur != 0) {         int prev = lst[cur];         nums.push_back(cur - prev);         cur = prev;       }       int z = nums.size(), idx = 0;       vector<int> curcol(m);       int cl = 2;       for (int i = 0; i < m; i++) {         if (i % 2 == 0) {           curcol[i] = 2;         } else           curcol[i] = 3;       }       for (int i = 0; i < m; i += 2) {         int use = 0;         if (idx < z) {           use = nums[idx];           idx += 1;         }         int col = i, col1 = i + 1;         int color = 0, st = n - 1, curcol = 2;         if (grid[n - 1][i] != -1) {           color = grid[n - 1][0] ^ 1;           st = n - 2;         }         if (i != 0 && st >= 0) {           if (grid[st][i - 1] == 0)             color = 1;           else             color = 0;         }         while (use > 0 && st >= 0) {           grid[st][col] = color;           grid[st][col1] = color;           use -= 1;           color ^= 1;           st -= 1;         }       }       for (int i = 0; i < m; i++) {         int now = curcol[i];         for (int j = 0; j < n; j += 2) {           if (grid[j][i] != -1) break;           grid[j][i] = now;           grid[j + 1][i] = now;           if (now == 2)             now = 3;           else             now = 2;         }       }       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cout << char(97 + grid[i][j]);         }         cout << endl;       }       return;     }     cout <<  NO  << endl;     return;   }   int main(int argc, const char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     for (int tt = 0; tt < t; tt++) {       solve(tt, t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, K;   int A[300005];   int DP[300005];   bool chk(int d) {     int mxp = 0;     for (int i = K; i <= N; i++) {       if (A[i] - A[DP[i - K] + 1] <= d) mxp = i;       DP[i] = mxp;     }     return mxp == N;   }   int main() {     cin >> N >> K;     for (int i = 1; i <= N; i++) cin >> A[i];     sort(A + 1, A + 1 + N);     int l = -1, r = 1e9;     while (l + 1 < r) {       int md = (l + r) / 2;       if (chk(md))         r = md;       else         l = md;     }     cout << r <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 119;   int n, F[N], T[N];   long double D[N];   vector<pair<int, int> > Adj[N];   void DFS(int v, int p) {     long double d = 2.0 / Adj[v].size(), t = 1.0;     for (auto X : Adj[v])       if (X.first == p) t += D[X.second], t -= (t > 2.0) ? 2.0 : 0;     for (auto X : Adj[v])       if (X.first != p) {         F[X.second] = v;         T[X.second] = X.first;         t += d;         t -= (t > 2.0) ? 2 : 0;         D[X.second] = t;         DFS(X.first, v);       }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++)       scanf( %d%d , &F[i], &T[i]), Adj[F[i]].push_back({T[i], i}),           Adj[T[i]].push_back({F[i], i});     DFS(1, 0);     printf( %d n , n - 1);     for (int i = 1; i < n; i++) {       if (D[i] > 1) swap(F[i], T[i]), D[i] -= 1;       printf( 1 %d %d %d %.12f n , i, F[i], T[i], (double)D[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[200013];   int type[200013], len[200013];   int high[200013];   int res[200013];   multiset<int> has;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= m; i++) scanf( %d%d , &type[i], &len[i]);     for (int i = m; i > 0; i--) high[i] = max(high[i + 1], len[i]);     for (int i = high[1] + 1; i <= n; i++) res[i] = a[i];     for (int i = 1; i <= high[1]; i++) has.insert(a[i]);     int dir = 1;     int last = high[1];     for (int i = 1; i <= m + 1; i++) {       if (len[i] == high[i]) {         while (last > len[i]) {           if (dir == 1) {             res[last--] = *has.rbegin();             has.erase(--has.end());           } else {             res[last--] = *has.begin();             has.erase(has.begin());           }         }         dir = type[i];       }     }     for (int i = 1; i <= n; i++) printf( %d  , res[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   int const maxl = 22;   int const maxm = 2520;   long long f[maxl][maxm + 4][50][2];   int a[maxl];   std::vector<int> alcm;   long long plcm[maxm + 4][10];   long long kth[maxm + 4];   long long l, r;   int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }   int lcm(int x, int y) {     if (!x) return y;     if (!y) return x;     return x * y / gcd(x, y);   }   void init() {     for (int i2 = 0, t2 = 1; i2 < 4; i2++, t2 *= 2)       for (int i3 = 0, t3 = 1; i3 < 3; i3++, t3 *= 3)         for (int i5 = 0, t5 = 1; i5 < 2; i5++, t5 *= 5)           for (int i7 = 0, t7 = 1; i7 < 2; i7++, t7 *= 7)             alcm.push_back(t2 * t3 * t5 * t7);     std::sort(alcm.begin(), alcm.end());     for (int i = 0; i <= maxm; i++)       for (int j = 0; j < 10; j++) plcm[i][j] = lcm(i, j);     for (int i = 0; i < (int)alcm.size(); i++) kth[alcm[i]] = i;   }   int get_len(long long x) {     int ret = 0;     for (; x; x /= 10) a[ret++] = x % 10;     return ret;   }   long long calc(long long x) {     int n = get_len(x);     for (int i = 0; i <= n; i++)       for (int j = 0; j < maxm; j++)         for (int k = 0; k < (int)alcm.size(); k++)           for (int l = 0; l < 2; l++) f[i][j][k][l] = 0;     f[0][0][0][0] = 1;     for (int i = 0; i < n; i++)       for (int j = 0; j < maxm; j++)         for (int k = 0; k < (int)alcm.size(); k++)           for (int l = 0; l < 2; l++) {             if (!f[i][j][k][l]) continue;             int up = (l ? 9 : a[n - i - 1]);             for (int ti = 0; ti <= up; ti++) {               int last = l || (ti < a[n - i - 1]);               f[i + 1][(j * 10 + ti) % maxm][kth[plcm[alcm[k]][ti]]][last] +=                   f[i][j][k][l];             }           }     long long ans = 0;     for (int i = 0; i < maxm; i++)       for (int k = 0; k < (int)alcm.size(); k++)         for (int l = 0; l < 2; l++)           if (!(i % alcm[k])) ans += f[n][i][k][l];     return ans;   }   int main() {     std::ios::sync_with_stdio(false);     init();     int T;     std::cin >> T;     while (T--) {       std::cin >> l >> r;       std::cout << calc(r) - calc(l - 1) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const double PI =       3.14159265358979323846264338327950288419716939937510582097494459230;   const long long MOD = 1e9 + 7;   const long long INF = 1e14;   long long mpow(long long a, long long b, long long p = MOD) {     a = a % p;     long long res = 1;     while (b > 0) {       if (b & 1) res = (res * a) % p;       a = (a * a) % p;       b = b >> 1;     }     return res % p;   }   const long long N = 4 * 100000;   long long n, m, b[N], a[N], g[N], h[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = 1; i <= (int)(n); ++i)       cin >> a[i], a[i] += a[i - 1], g[i] = 1e14;     for (int i = 1; i <= (int)(m); ++i)       cin >> b[i], b[i] += b[i - 1], h[i] = 1e14;     for (int i = 1; i <= (int)(n); ++i)       for (long long j = i; j <= n; j++)         g[j - i + 1] = min(g[j - i + 1], a[j] - a[i - 1]);     for (int i = 1; i <= (int)(m); ++i)       for (long long j = i; j <= m; j++)         h[j - i + 1] = min(h[j - i + 1], b[j] - b[i - 1]);     long long x;     cin >> x;     long long ans = 0;     for (int i = 1; i <= (int)(n); ++i)       for (int j = 1; j <= (int)(m); ++j) {         if (g[i] * h[j] <= x) {           ans = max(ans, (long long)(i * j));         }       }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int gcd(int a, int b) {     while (b > 0) {       int t = a;       a = b;       b = t % b;     }     return a;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     {       int i;       for (i = 1; i < n; ++i)         if (a[i] != a[i - 1]) break;       if (i == n) {         cout << (int64_t)n * (n - 1) << endl;         return 0;       }     }     int64_t ans = 0;     for (int divisor = 1; divisor < n; ++divisor) {       if (n % divisor > 0) continue;       vector<int> max_of_rem(a.begin(), a.begin() + divisor);       for (int i = divisor; i < n; ++i)         if (a[i] > max_of_rem[i % divisor]) max_of_rem[i % divisor] = a[i];       int lim = n;       for (int left = 0; left < lim;) {         int offs;         for (offs = 0; offs < n - 1 && a[(left + offs) % n] ==                                            max_of_rem[(left + offs) % divisor];              ++offs)           ;         if (offs == 0) {           ++left;           continue;         }         int goffs = offs;         if (left == 0) {           while (left + offs < lim &&                  a[lim - 1] == max_of_rem[(lim - 1) % divisor])             --lim;           offs += n - lim;         }         for (int i = divisor; i <= offs; i += divisor) {           if (gcd(n, i) == divisor) {             ans += offs == n ? n : offs - i + 1;           }         }         left += goffs;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int ansa, ansb;   int mid[110], cnt;   int yy;   int cmp(int a, int b) { return a > b; }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       int s;       scanf( %d , &s);       int m = s / 2;       int x;       if (s & 1) {         for (int i = 1; i <= m; ++i) {           scanf( %d , &x);           ansa += x;         }         scanf( %d , &x);         mid[cnt++] = x;         for (int i = 1; i <= m; ++i) {           scanf( %d , &x);           ansb += x;         }       } else {         for (int i = 1; i <= m; ++i) {           scanf( %d , &x);           ansa += x;         }         for (int i = 1; i <= m; ++i) {           scanf( %d , &x);           ansb += x;         }       }     }     sort(mid, mid + cnt, cmp);     for (int i = 0; i < cnt; ++i) {       if (i & 1)         ansb += mid[i];       else         ansa += mid[i];     }     printf( %d %d , ansa, ansb);   }


#include <bits/stdc++.h>   using namespace std;   const int N_MAX = 1005, M_MAX = 100005;   struct union_find {     int rank[N_MAX], parent[N_MAX];     union_find() {       for (int i = 0; i < N_MAX; i++) {         rank[i] = 0;         parent[i] = i;       }     }     int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }     bool join(int x, int y) {       x = find(x);       y = find(y);       if (x == y) return false;       if (rank[x] >= rank[y]) {         parent[y] = x;         if (rank[x] == rank[y]) rank[x]++;       } else         parent[x] = y;       return true;     }   };   struct edge {     int a, b;     char who;   };   int N, M, H;   edge edges[M_MAX];   union_find S_UF, M_UF;   int impossible() {     puts( -1 );     return 0;   }   int main() {     scanf( %d %d , &N, &M);     if (N % 2 == 0) return impossible();     H = (N - 1) / 2;     for (int i = 0; i < M; i++) {       scanf( %d %d %c , &edges[i].a, &edges[i].b, &edges[i].who);       edges[i].a--;       edges[i].b--;     }     int S = 0;     for (int i = 0; i < M; i++)       if (edges[i].who ==  S )         if (S_UF.join(edges[i].a, edges[i].b)) S++;     if (S < H) return impossible();     int components = N - S;     vector<int> answer;     for (int i = 0; i < M && (int)answer.size() < H - (components - 1); i++)       if (edges[i].who ==  M )         if (S_UF.find(edges[i].a) == S_UF.find(edges[i].b))           if (M_UF.join(edges[i].a, edges[i].b)) answer.push_back(i);     assert((int)answer.size() <= H - (components - 1));     if ((int)answer.size() != H - (components - 1)) return impossible();     for (int i = 0; i < M; i++)       if (edges[i].who ==  M )         if (S_UF.join(edges[i].a, edges[i].b)) {           assert(M_UF.join(edges[i].a, edges[i].b));           answer.push_back(i);         }     assert((int)answer.size() <= H);     if ((int)answer.size() != H) return impossible();     for (int i = 0; i < M; i++)       if (edges[i].who ==  S )         if (M_UF.join(edges[i].a, edges[i].b)) answer.push_back(i);     assert((int)answer.size() == N - 1);     printf( %d n , (int)answer.size());     for (int i = 0; i < (int)answer.size(); i++)       printf( %d%c , answer[i] + 1, i < (int)answer.size() - 1 ?     :   n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool reach(int from[3], int to[3], vector<vector<string>> &works) {     if (works[from[0]][from[1]][from[2]] ==  0 ) return false;     if (from[0] == to[0] && from[1] == to[1] && from[2] == to[2]) return true;     for (int d = 0; d < 3; ++d)       if (from[d] < to[d]) {         ++from[d];         bool res = reach(from, to, works);         --from[d];         if (res) return true;       }     return false;   }   int main() {     int lim[3];     for (int i = 0; i < 3; ++i) scanf( %d , &lim[i]);     vector<vector<string>> works(lim[0],                                  vector<string>(lim[1], string(lim[2],    )));     for (int i = 0; i < lim[0]; ++i)       for (int j = 0; j < lim[1]; ++j) scanf( %s , &works[i][j][0]);     int result = 0;     for (int i = 0; i < lim[0]; ++i)       for (int j = 0; j < lim[1]; ++j)         for (int k = 0; k < lim[2]; ++k)           if (works[i][j][k] ==  1 ) {             int at[3] = {i, j, k};             bool critical = false;             for (int fromd = 0; fromd < 3; ++fromd)               if (at[fromd] >= 1) {                 int from[3] = {i, j, k};                 --from[fromd];                 for (int tod = 0; tod < 3; ++tod)                   if (at[tod] + 1 < lim[tod]) {                     int to[3] = {i, j, k};                     ++to[tod];                     bool before = reach(from, to, works);                     works[i][j][k] =  0 ;                     bool after = reach(from, to, works);                     works[i][j][k] =  1 ;                     if (before && !after) critical = true;                   }               }             if (critical) ++result;           }     printf( %d n , result);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long suml[100100], sumr[100100], l[100100], r[100100], A[100100],       B[100100], total, ans[100100];   int main() {     long long N;     cin >> N;     for (long long i = 1; i <= N; i++) {       scanf( %lld , A + i);     }     for (long long i = N; i > 0; i--) {       scanf( %lld , B + i);     }     for (long long i = 1; i <= N; i++) {       l[i] = i - 1;       r[i] = i + 1;     }     for (long long i = 1; i <= N; i++) {       long long x = B[i];       long long sum = suml[r[x]] + sumr[l[x]] + A[x];       sumr[l[x]] = suml[r[x]] = sum;       l[r[x]] = l[x];       r[l[x]] = r[x];       total = max(total, sum);       ans[i] = total;     }     for (long long i = N - 1; i >= 0; i--) {       printf( %lld n , ans[i]);     }   }


#include <bits/stdc++.h>   const int MAXN = 1e5 + 10;   int n, m, k, p;   int h[MAXN], a[MAXN];   namespace solver1 {   long long fin[MAXN];   bool check(long long x) {     long long need = 0;     static long long t[MAXN];     for (int i = 1; i <= n; i++) {       need += (t[i] = std::max(0ll, (fin[i] - x + p - 1) / p));     }     if (need > k * m) return 0;     static int cnt[MAXN];     memset(cnt, 0, sizeof cnt);     for (int i = 1; i <= n; i++) {       for (int j = t[i] - 1; j >= 0; j--) {         long long delta = fin[i] - x - (long long)j * p;         if (delta <= h[i]) {           cnt[1]++;         } else if (h[i] + (long long)a[i] * (m - 1) < delta) {           return 0;         } else {           cnt[(delta - h[i] + a[i] - 1) / a[i] + 1]++;         }       }     }     int tot = 0;     for (int i = 1; i <= m; i++) {       tot += cnt[i];       tot = std::max(0, tot - k);     }     return tot == 0;   }   void main() {     for (int i = 1; i <= n; i++) {       fin[i] = (long long)a[i] * m + h[i];     }     long long l = 1, r = 1e15, ans = 1e15;     while (l <= r) {       long long mid = (l + r) >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     printf( %lld n , ans);   }   }  // namespace solver1   int main() {     scanf( %d%d%d%d , &n, &m, &k, &p);     for (int i = 1; i <= n; i++) {       scanf( %d%d , h + i, a + i);     }     solver1::main();   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     int x;     int y;     int z;     int id;   };   bool compY(Point a, Point b) {     if (a.y != b.y) {       return a.y < b.y;     }     return a.z < b.z;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n;     cin >> n;     map<int, vector<Point>> m;     for (int i = 0; i < n; i++) {       int x, y, z;       cin >> x >> y >> z;       m[x].push_back({x, y, z, i});     }     vector<pair<int, int>> ans;     vector<bool> deleted(n, false);     for (auto &e : m) {       if (e.second.size() < 2) {         continue;       }       vector<Point> a = e.second;       int k = a.size();       if (k < 2) {         continue;       }       sort(a.begin(), a.end(), compY);       for (int i = 0; i < k - 1; i++) {         if (a[i].y == a[i + 1].y) {           ans.emplace_back(a[i].id, a[i + 1].id);           deleted[a[i].id] = true;           deleted[a[i + 1].id] = true;           i++;         }       }       int l = 0;       int r = 0;       while (l < k) {         if (deleted[a[l].id]) {           l++;           continue;         }         r = max(r, l + 1);         while (r < k && deleted[a[r].id]) {           r++;         }         if (r == k) {           break;         }         ans.emplace_back(a[l].id, a[r].id);         deleted[a[l].id] = true;         deleted[a[r].id] = true;         l++;       }     }     bool havePrev = false;     Point prev;     for (auto &e : m) {       bool here = false;       Point cur;       for (auto &x : e.second) {         if (!deleted[x.id]) {           here = true;           cur = x;           break;         }       }       if (!here) {         continue;       }       if (havePrev) {         ans.emplace_back(prev.id, cur.id);         havePrev = false;       } else {         havePrev = true;         prev = cur;       }     }     for (auto &e : ans) {       cout << e.first + 1 <<     << e.second + 1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000;   int a[MAXN];   int n;   int main() {     scanf( %d , &n);     n *= 2;     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     int ans = 1e9;     for (int j = 1; j <= n; ++j)       for (int k = j + 1; k <= n; ++k) {         int t = 0;         for (int i = 1; i < n;) {           while (i == j || i == k) ++i;           int p = i + 1;           while (p == j || p == k) ++p;           t += a[p] - a[i];           i = p + 1;         }         ans = min(t, ans);       }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[10][10000];   long long cnt[10];   long long w;   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> w;     for (long long i = 0; i < 8; i++) {       cin >> cnt[i];     }     for (long long i = 0; i < 10; i++)       for (long long j = 0; j < 10000; j++) dp[i][j] = -1;     dp[0][0] = 0;     for (long long i = 0; i < 8; i++) {       for (long long j = 0; j < 10000; j++) {         if (dp[i][j] == -1) continue;         long long b = 840 / (i + 1);         b = min(b, cnt[i]);         for (long long k = 0; k <= b; k++)           if (j + k * (i + 1) < 10000) {             dp[i + 1][j + k * (i + 1)] =                 max(dp[i + 1][j + k * (i + 1)],                     dp[i][j] + (cnt[i] - k) / (840 / (i + 1)));           }       }     }     long long ans = 0;     for (long long j = 0; j < 10000; j++) {       if (dp[8][j] != -1 && j <= w) {         ans = max(ans, j + 840ll * (min(dp[8][j], (w - j) / 840)));       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     while (scanf( %d , &n) != EOF) {       if (n % 2 != 0)         printf( black n );       else {         printf( white n1 2 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int w, m;     cin >> w >> m;     if (m == 1 || w == 2) {       cout <<  YES  << endl;       return 0;     }     while (m) {       if (m % w == 1) {         --m;       } else if (m % w == w - 1) {         ++m;       }       if (m % w == 0) {         m /= w;       } else {         cout <<  NO  << endl;         return 0;       }     }     cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1.1e6;   int ans;   int n, m;   vector<int> F;   int S[N][7][7];   int s(int i, int a, int b) {     if (i == m) return (F[i] - a) / 3;     if (i + 1 == m) return (F[i] - a) / 3 + (F[i + 1] - b) / 3;     int loc = S[i + 1][b][0];     for (int j = 0; j < 3; j++)       if (a + j <= min(6, F[i]) && b + j <= min(6, F[i + 1]) &&           0 + j <= min(6, F[i + 2]))         loc = max(loc, j + ((F[i] - (a + j)) / 3) + S[i + 1][b + j][j]);     return loc;   }   int main() {     scanf(  %d%d , &n, &m);     F = vector<int>(m + 1);     for (int i = 0; i < n; i++) {       int a;       scanf(  %d , &a);       F[a]++;     }     for (int i = m; i + 1; i--)       for (int a = 0; a < 7; a++)         for (int b = 0; b < 7; b++) S[i][a][b] = s(i, a, b);     printf( %d n , ans + S[0][0][0]);   }


#include <bits/stdc++.h>   using namespace std;   int T, n, k, l, a[300010], lst;   bool flag = 0;   int main() {     scanf( %d , &T);     while (T--) {       flag = 0;       lst = 0;       scanf( %d%d%d , &n, &k, &l);       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i]);         if (a[i] > l)           flag = 1;         else if (a[i] + k <= l)           lst = 0;         else if (lst < k)           lst = max(lst + 1, k + a[i] - l);         else {           if (lst + 1 + a[i] <= l + k)             lst++;           else             flag = 1;         }       }       if (flag)         printf( No n );       else         printf( Yes n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s >> t;     int flag = 0;     vector<char> arr(26,  . );     for (int i = 0; i < s.length(); i++) {       int a = int(s[i]);       int b = int(t[i]);       if (arr[a - 97] == t[i] || arr[a - 97] ==  . ) {         arr[a - 97] = t[i];         if (arr[b - 97] == s[i] || arr[b - 97] ==  . )           arr[b - 97] = char(a);         else {           cout << -1;           flag = -1;           break;         }       } else {         cout << -1;         flag = -1;         break;       }     }     if (flag != -1) {       for (int i = 0; i < 26; i++) {         if (arr[i] !=  . ) {           int x = (int)arr[i];           if (arr[x - 97] == char(i + 97) || arr[x - 97] ==  . ) {             arr[x - 97] =  . ;             continue;           } else {             cout << -1;             flag = -1;             break;           }         }       }     }     if (flag != -1) {       int sum = 0;       for (int i = 0; i < 26; i++) {         if (arr[i] !=  . ) sum++;       }       cout << sum << endl;       if (sum != 0) {         for (int i = 0; i < 26; i++) {           if (arr[i] !=  . ) cout << arr[i] <<     << char(i + 97) << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   const int MOD = 1000000007;   int t, k, dp[N][N][2];   char l[N], r[N], s[N];   void add(int &a, int b) {     a += b;     if (a >= MOD) a -= MOD;   }   int dfs(int l, int pos, int f, int limit) {     if (l == -1) return f;     if (!limit && dp[l][pos][f] != -1) return dp[l][pos][f];     int up = limit ? (s[l] -  0 ) : 9, ret = 0;     for (int i = 0; i <= up; i++) {       if (i == 4 || i == 7) {         int ff = f;         if (pos != 1001) ff |= ((pos - l) <= k);         add(ret, dfs(l - 1, l, ff, limit && (i == up)));       } else {         add(ret, dfs(l - 1, pos, f, limit && (i == up)));       }     }     if (!limit) dp[l][pos][f] = ret;     return ret;   }   int gao(char *a) {     strcpy(s, a);     reverse(s, s + strlen(s));     return dfs(strlen(a) - 1, 1001, 0, 1);   }   void sub(char *s) {     int len = strlen(s), l = strlen(s) - 1;     while (s[l] ==  0 ) s[l] =  9 , l--;     s[l]--;     if (s[0] ==  0  && len > 1) {       for (int i = 1; i <= len; i++) s[i - 1] = s[i];     }   }   int main() {     memset(dp, -1, sizeof(dp));     cin >> t >> k;     while (t--) {       cin >> l >> r;       sub(l);       cout << ((gao(r) - gao(l)) % MOD + MOD) % MOD << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[101][101];   int main() {     int n;     cin >> n;     int sum = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> a[i][j];         if ((i == j || i == (n - j + 1)) ||             (i == (n + 1) / 2 || j == (n + 1) / 2)) {           sum += a[i][j];         }       }     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   const int N = 2e5 + 1;   const int oo = 1e9;   const double pi = acos(-1);   int main() {     int t;     cin >> t;     while (t--) {       int a, b, n;       cin >> a >> b >> n;       if (b < a) swap(a, b);       int ans = 0;       while (a <= n && b <= n) {         ++ans;         if (ans % 2)           a += b;         else           b += a;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const int N = 300010;   int n, m, ans, ansx, ansy, s1, s2, max1;   struct aa {     int x, y, w;   } a[N], b[N << 1];   multiset<int> q1, q2;   void Init(int &x) {     char y;     while ((y = getchar()) <  0  || y >  9 )       ;     x = y -  0 ;     while ((y = getchar()) >=  0  && y <=  9 ) x = (x << 3) + (x << 1) + y -  0 ;   }   bool cmp(aa x, aa y) { return x.x < y.x || x.x == y.x && x.y < y.y; }   int main() {     scanf( %d%d , &n, &m);     memset(a, 0, sizeof(a));     for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].y);     for (int i = 1; i <= n; ++i)       b[(i << 1) - 1].x = a[i].x, b[(i << 1) - 1].y = 0, b[i << 1].x = a[i].y,                    b[i << 1].y = 1, b[i << 1].w = i;     n <<= 1;     sort(b + 1, b + n + 1, cmp);     ans = 0;     ansx = 0;     ansy = 0;     s1 = 0;     s2 = 0;     q1.clear();     q2.clear();     for (int i = 1; i <= n; ++i)       if (b[i].y) {         if (s1 == m && b[i].x - max1 + 1 > ans) {           ansx = max1;           ansy = b[i].x;           ans = ansy - ansx + 1;         }         if (a[b[i].w].x > max1)           q2.erase(q2.find(a[b[i].w].x)), --s2;         else {           q1.erase(q1.find(a[b[i].w].x));           --s1;           if (s2)             q1.insert(max1 = *q2.begin()), q2.erase(q2.begin()), ++s1, --s2;           else if (s1)             max1 = *q1.rbegin();         }       } else {         if (s1 < m)           q1.insert(b[i].x), ++s1, max1 = b[i].x;         else           q2.insert(b[i].x), ++s2;       }     printf( %d n , ans);     bool flag = 0;     if (ans == 0) {       for (int i = 1; i < m; ++i) printf( %d  , i);       printf( %d n , m);       return 0;     }     for (int i = 1; i <= n && m; ++i)       if (a[i].x <= ansx && a[i].y >= ansy) {         if (flag)           putchar(   );         else           flag = 1;         printf( %d , i);         --m;       }     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0;     int nTemp = 0;     cin >> n;     nTemp = n;     int sum = 0;     int temp = 0;     while (nTemp--) {       cin >> temp;       sum = sum + temp;     }     int ways = 0;     for (int i = 1; i <= 5; i++) {       if ((sum + i) % (n + 1) != 1) {         ways++;       }     }     cout << ways << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)5e18;   const long long N = 120000;   long long n, a[N], m, b[N];   long long pre[N], nxt[N];   long long q[N], top = 0;   long long _abs(long long x) { return x < 0 ? -x : x; }   void solve() {     scanf( %lld%lld , &n, &m);     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       if (i > 1) b[i - 1] = _abs(a[i] - a[i - 1]);     }     b[0] = b[n] = inf;     q[++top] = 0;     for (long long i = 1; i <= n; i++) {       while (b[q[top]] < b[i]) nxt[q[top]] = i, top--;       pre[i] = q[top];       q[++top] = i;     }     while (m--) {       long long ans = 0;       long long l, r;       scanf( %lld%lld , &l, &r);       r--;       for (long long i = l; i <= r; i++) {         long long L = max(l, pre[i] + 1);         long long R = min(r, nxt[i] - 1);         ans += (R - i + 1) * (i - L + 1) * b[i];       }       printf( %lld n , ans);     }   }   signed main() {     long long TTT = 1;     while (TTT--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   const int MAXN = 1000006;   char s[MAXN];   int qp(long long a, int b) {     long long ans = 1;     while (b) {       if (b % 2) ans = ans * a % mod;       b /= 2;       a = a * a % mod;     }     return ans;   }   void solve() {     int n, k;     scanf( %d%d , &n, &k);     scanf( %s , s);     string P(s);     P =  #  + P;     int pi[MAXN], m = P.size() - 1, q = 0;     pi[0] = pi[1] = 0;     for (int i = 2; i <= m; ++i) {       while (q && P[i] != P[q + 1]) q = pi[q];       if (P[i] == P[q + 1]) ++q;       pi[i] = q;     }     int ile = 0;     int prev = -m + 1;     bool ok = true;     set<int> S;     int i = m;     while (i) {       S.insert(pi[i]);       i = pi[i];     }     for (int i = 0; i < (k); ++i) {       int a;       scanf( %d , &a);       ile += max(0, a - (prev + m));       if (a - prev < m && prev > 0 && S.find(m - a + prev) == S.end()) ok = false;       prev = a;     }     ile += max(0, n - (prev + m) + 1);     if (!ok)       printf( 0 n );     else       printf( %d n , qp(26, ile));   }   int main() {     int t = 1;     for (int i = 0; i < (t); ++i) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long bigmod(long long n, long long pow, long long m) {     long long ret = 1;     while (pow) {       if (pow % 2) {         ret *= n;         ret %= m;       }       n *= n;       n %= m;       pow /= 2;     }     return ret;   }   long long arr[3 * 100009];   int main() {     long long n, a, sum = 0, sum1 = 0;     scanf( %I64d , &n);     for (long long i = 1; i <= n; i++) scanf( %I64d , &arr[i]);     sort(arr + 1, arr + n + 1);     for (long long i = 1; i <= n; i++) {       sum += (arr[i] * (bigmod((long long)2, i - 1, 1000000000 + 7)));       sum -= (arr[i] * (bigmod((long long)2, n - i, 1000000000 + 7)));       sum %= 1000000000 + 7;     }     printf( %I64d n , sum);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1050;   long double pr[N][N];   int n, m, P[N];   inline long double gp(int i, int j) {     if (i < j)       return pr[i][j];     else       return 1 - pr[j][i];   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> P[i];     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) pr[i][j] = P[i] > P[j];     for (int i = 1, a, b; i <= m; ++i) {       cin >> a >> b;       if (a > b) swap(a, b);       for (int i = 1; i <= n; ++i)         if (i != a && i != b) {           pr[i][b] = pr[i][a] = (gp(i, a) + gp(i, b)) / 2;           pr[b][i] = pr[a][i] = 1 - pr[i][a];         }       pr[a][b] = pr[b][a] = 0.5;     }     long double res = 0;     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) res += pr[i][j];     cout << fixed << setprecision(10) << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int m;   vector<vector<char> > grid;   vector<vector<int> > mindist;   vector<int> first;   vector<int> last;   bool notpossible = false;   int main() {     scanf( %d %d n , &n, &m);     grid.assign(n, vector<char>(m));     first.assign(n, -1);     last.assign(n, -1);     mindist.assign(n, vector<int>(m));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         scanf( %c , &grid[i][j]);         if (grid[i][j] ==  1 ) {           if (first[i] == -1) {             first[i] = j;           }           last[i] = j;         }       }       if (last[i] == -1) {         notpossible = true;       }       scanf(  n );     }     if (notpossible) {       printf( -1 n );       return 0;     }     for (int i = 0; i < n; i++) {       int current = m - last[i];       for (int j = 0; j < m; j++) {         if (grid[i][j] ==  1 ) {           current = 0;         }         mindist[i][j] = current;         current++;       }       current = first[i] + 1;       for (int j = m - 1; j >= 0; j--) {         if (grid[i][j] ==  1 ) {           current = 0;         }         mindist[i][j] = min(mindist[i][j], current);         current++;       }     }     int mincolumn = 0;     for (int i = 0; i < n; i++) {       mincolumn += mindist[i][0];     }     for (int i = 1; i < m; i++) {       int currentcolumn = 0;       for (int j = 0; j < n; j++) {         currentcolumn += mindist[j][i];       }       mincolumn = min(mincolumn, currentcolumn);     }     printf( %d n , mincolumn);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   struct point {     double x, y;     point(double _x = 0, double _y = 0) : x(_x), y(_y) {}     void read() { scanf( %lf%lf , &x, &y); }   };   point polygon[30];   int sign(double x) {     if (fabs(x) < eps) return 0;     return (x > 0) ? 1 : -1;   }   double cross(const point &a, const point &b, const point &c, const point &d) {     return (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x);   }   double dist(const point &a, const point &b) {     return hypot(a.x - b.x, a.y - b.y);   }   pair<point, bool> getIntersection(const point &a, const point &b,                                     const point &c, const point &d) {     if (sign(cross(a, b, c, d)) == 0) return make_pair(point(0, 0), false);     double a1 = b.x - a.x, b1 = c.x - d.x, c1 = c.x - a.x;     double a2 = b.y - a.y, b2 = c.y - d.y, c2 = c.y - a.y;     double tmp = a1 * b2 - a2 * b1;     double x = (c1 * b2 - c2 * b1) / tmp;     double y = (c2 * a1 - c1 * a2) / tmp;     if (sign(x) >= 0 && sign(x - 1) < 0 && sign(y) >= 0 && sign(y - 1) < 0) {       return make_pair(point(a.x + x * (b.x - a.x), a.y + x * (b.y - a.y)), true);     } else {       return make_pair(point(0, 0), false);     }   }   int main() {     point start, end;     start.read();     end.read();     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) polygon[i].read();     int flag = 0;     point a, b;     int la, lb;     for (int i = 0; i < n; i++) {       pair<point, bool> tmp =           getIntersection(start, end, polygon[i], polygon[(i + 1) % n]);       if (tmp.second) {         if (flag == 0) {           a = tmp.first;           la = i;         } else {           b = tmp.first;           lb = i;         }         flag++;       }     }     if (flag < 2) {       printf( %.10lf n , dist(start, end));       return 0;     }     if (sign(dist(start, a) - dist(start, b)) > 0) {       swap(a, b);       swap(la, lb);     }     double ans = dist(start, a) + dist(a, b) * 2 + dist(b, end);     double s1 = dist(start, a) + dist(a, polygon[(la + 1) % n]);     for (int i = (la + 1) % n; i != lb; i = (i + 1) % n)       s1 += dist(polygon[i], polygon[(i + 1) % n]);     s1 += dist(polygon[lb], b) + dist(b, end);     ans = min(ans, s1);     double s2 = dist(start, a) + dist(a, polygon[la]);     for (int i = la; i != (lb + 1) % n; i = (i + n - 1) % n)       s2 += dist(polygon[i], polygon[(i + n - 1) % n]);     s2 += dist(polygon[(lb + 1) % n], b) + dist(b, end);     ans = min(ans, s2);     printf( %.10lf n , ans);     return 0;   }