#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using ii = pair<int, int>;   template <typename A, typename B>   ostream& operator<<(ostream& os, pair<A, B> p) {     return os <<  {  << p.first <<  ,   << p.second <<  } ;   }   const int oo = 0x3f3f3f3f;   const ll OO = ll(oo) * oo;   const ll M = 1e9 + 9;   ll p2[100005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int m;     cin >> m;     ll k = 1;     for (int i = 0; i < m + 1; i++) {       p2[i] = k;       k <<= 1ll;       k %= M;     }     ll ans = 1;     for (int i = 0; i < n; i++) {       if (p2[m] - i - 1 <= 0) {         cout << 0 << endl;         return 0;       }       ans *= (p2[m] - i - 1) % M;       ans %= M;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   int a[5005];   int solve(int i, int j) {     int k, vans, hans, mini, s, t;     vans = j - i + 1;     mini = a[i];     for (k = i + 1; k <= j; k++)       if (mini > a[k]) mini = a[k];     hans = mini;     for (k = i; k <= j; k++) a[k] -= mini;     hans = mini;     for (k = i; k <= j;) {       if (a[k] == 0) {         k++;       } else {         s = k;         t = s;         while (t <= j && a[t] != 0) t++;         hans += solve(s, t - 1);         k = t;       }     }     if (vans < hans)       return vans;     else       return hans;   }   int main() {     int n, i;     int vans, hans;     int ans;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     ans = solve(1, n);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( O3 )   const long long inf = 1e17;   void bfs(int a, vector<vector<int> > &g, vector<int> &da) {     int n = g.size();     queue<int> q;     q.push(a);     vector<bool> u(n, false);     u[a] = true;     da[a] = 0;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int i = 0; i < g[v].size(); i++) {         int x = g[v][i];         if (!u[x]) {           u[x] = true;           da[x] = da[v] + 1;           q.push(x);         }       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int q;     cin >> q;     while (q--) {       int n, m, a, b, c;       cin >> n >> m >> a >> b >> c;       a--;       b--;       c--;       vector<vector<int> > g(n, vector<int>(0));       vector<long long> cost(m);       for (int i = 0; i < m; i++) cin >> cost[i];       sort(cost.begin(), cost.end());       for (int i = 1; i < m; i++) cost[i] += cost[i - 1];       vector<int> da(n, 0), db(n, 0), dc(n, 0);       for (int i = 0; i < m; i++) {         int fr, to;         cin >> fr >> to;         fr--;         to--;         g[fr].push_back(to);         g[to].push_back(fr);       }       bfs(a, g, da);       bfs(b, g, db);       bfs(c, g, dc);       long long ans = inf;       for (int i = 0; i < n; i++) {         if (da[i] + db[i] + dc[i] - 1 > m - 1) continue;         long long cur = 0;         if (da[i] + db[i] + dc[i] != 0) cur += cost[da[i] + db[i] + dc[i] - 1];         if (db[i] > 0) cur += cost[db[i] - 1];         ans = min(ans, cur);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cache[1005][20][2];   int ret[1005];   int n;   vector<int> ans(1005, INT_MAX);   void query(vector<int> &arr) {     if (!arr.empty()) {       printf( %lu n , arr.size());       for (auto &x : arr) printf( %i  , x);       printf(  n );       fflush(stdout);       for (int x = 1; x <= n; x++) scanf( %i , &ret[x]);     } else       memset(ret, 0, sizeof ret);   }   int main() {     scanf( %i , &n);     for (int x = 2, y = 1; x < 2 * n; x *= 2, y++) {       vector<int> w;       for (int z = 1; z <= n; z += x)         for (int a = 0; a < x / 2 && z + a <= n; a++) w.push_back(z + a);       query(w);       for (int z = 1; z <= n; z++) cache[z][y][0] = ret[z];       w.clear();       for (int z = 1 + x / 2; z <= n; z += x)         for (int a = 0; a < x / 2 && z + a <= n; a++) w.push_back(z + a);       query(w);       for (int z = 1; z <= n; z++) cache[z][y][1] = ret[z];     }     printf( -1 n );     for (int z = 1; z <= n; z++) {       for (int x = 2, y = 1; x < 2 * n; x *= 2, y++) {         for (int a = 0; a <= 1; a++)           if (((z - 1) >> (y - 1) & 1) != a) ans[z] = min(ans[z], cache[z][y][a]);       }       printf( %i  , ans[z]);     }     printf(  n );     fflush(stdout);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110000;   int a[N], wei[N], r1, r2;   bool can[N], ans[N];   int n, m;   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d%d , &n, &m);       n--;       for (int i = (1); i <= (int)m; i++) {         scanf( %d , &a[i]);         wei[i] = a[i];         ans[i] = 0;         can[i] = 1;       }       r1 = r2 = 0;       int shiwang = 0;       for (int i = (1); i <= (int)n; i++) {         int x, y;         scanf( %d%d , &x, &y);         if (y == 1) {           if (!shiwang) shiwang = i;         }         if (!shiwang && x == 0) r1++;         if (x == 0) r2++;         if (shiwang) can[x] = 0;         a[x]--;         if (!shiwang) wei[x]--;       }       if (!shiwang) {         for (int i = (1); i <= (int)m; i++)           if (a[i] <= r2)             printf( Y );           else             printf( N );         printf(  n );       } else {         int lu = 0;         for (int i = (1); i <= (int)m; i++)           if (can[i] && wei[i] <= r1) {             ans[i] = 1;             if (r2 - wei[i] > lu) lu = r2 - wei[i];           }         for (int i = (1); i <= (int)m; i++) {           if (a[i] <= lu) ans[i] = 1;           if (ans[i])             printf( Y );           else             printf( N );         }         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > A, B;   void getDivisors(long long x, vector<pair<long long, long long> > &X) {     for (long long i = 1; i * i <= x; i++) {       if (x % i) continue;       X.push_back({i, x / i});     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL), cout.tie(NULL);     long long a, b;     cin >> a >> b;     long long area = a + b;     getDivisors(a, A);     getDivisors(b, B);     long long lim = 0;     while (lim * lim <= area) lim++;     for (long long i = lim - 1; i >= 1; i--) {       if (area % i) continue;       int f = 0;       long long x = i;       long long y = area / i;       int ptr =           lower_bound(A.begin(), A.end(), make_pair(x + 1, 0LL)) - A.begin();       if (ptr > 0 && A[ptr - 1].second <= y) f = 1;       ptr = lower_bound(B.begin(), B.end(), make_pair(x + 1, 0LL)) - B.begin();       if (ptr > 0 && B[ptr - 1].second <= y) f = 1;       if (f == 1) {         cout << (x + y) * 2 <<   n ;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 60;   vector<int> adj[maxn];   int n, m;   void read() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--, b--;       adj[a].push_back(b);       adj[b].push_back(a);     }   }   bool vis[maxn];   vector<int> ch;   void dfs(int v) {     vis[v] = 1;     ch.push_back(v);     for (int i = 0; i < int(adj[v].size()); i++) {       int nxt = adj[v][i];       if (!vis[nxt]) dfs(nxt);     }   }   void solve() {     memset(vis, 0, sizeof(vis));     if (m > n) {       puts( NO );       return;     } else if (m == n) {       dfs(0);       for (int i = 0; i < n; i++) {         if (!vis[i] || int(adj[i].size()) != 2) {           puts( NO );           return;         }       }       puts( YES n0 );       return;     } else {       for (int i = 0; i < n; i++) {         if (int(adj[i].size()) > 2) {           puts( NO );           return;         }       }       set<pair<int, int> > line;       for (int i = 0; i < n; i++) {         if (vis[i]) continue;         if (int(adj[i].size()) == 0) {           line.insert(make_pair(i, i));           vis[i] = 1;         }         if (int(adj[i].size()) == 1) {           ch.clear();           dfs(i);           line.insert(make_pair(ch.front(), ch.back()));         }       }       for (int i = 0; i < n; i++) {         if (!vis[i]) {           puts( NO );           return;         }       }       vector<pair<int, int> > ans;       while (int(line.size())) {         if (int(line.size()) == 1) {           ans.push_back(*line.begin());           line.erase(line.begin());         } else {           pair<int, int> a = *line.begin();           line.erase(line.begin());           pair<int, int> b = *line.begin();           line.erase(line.begin());           ans.push_back(make_pair(a.first, b.first));           line.insert(               make_pair(min(a.second, b.second), max(a.second, b.second)));         }       }       puts( YES );       printf( %d n , int(ans.size()));       for (int i = 0; i < int(ans.size()); i++) {         printf( %d %d n , ans[i].first + 1, ans[i].second + 1);       }     }   }   int main() {     read();     solve();   }


#include <bits/stdc++.h>   using namespace std;   vector<int> ed[2000];   int sho[1200][1200];   int col[1200];   int n, m;   int makk[1200];   int sp[1200];   void dfs(int v, int ccc) {     sp[v] = ccc;     for (int a = 0; a < ed[v].size(); a++) {       if (sp[ed[v][a]] == -1) dfs(ed[v][a], ccc);     }   }   void getsho(int v) {     int q[3000];     int dp[3000];     int c[3000];     for (int a = 0; a < n; a++) c[a] = -1;     q[0] = v;     dp[0] = 0;     c[v] = 1;     int bb = 0;     int ee = 0;     while (bb <= ee) {       int vx = q[bb];       sho[v][vx] = dp[bb];       for (int a = 0; a < ed[vx].size(); a++) {         if (c[ed[vx][a]] == -1) {           int vu = ed[vx][a];           c[vu] = 1;           ee++;           q[ee] = vu;           dp[ee] = dp[bb] + 1;         }       }       bb++;     }   }   int isok;   void kolo(int v, int c) {     col[v] = c;     for (int a = 0; a < ed[v].size(); a++) {       if (col[ed[v][a]] == c)         isok = 0;       else if (col[ed[v][a]] == -1)         kolo(ed[v][a], 1 - c);     }   }   int main() {     cin >> n >> m;     for (int a = 0; a < 1200; a++) makk[a] = 0;     isok = 1;     int x, y;     for (int a = 0; a < n; a++)       for (int b = 0; b < n; b++) sho[a][b] = 1000000;     for (int a = 0; a < m; a++) {       cin >> x >> y;       ed[x - 1].push_back(y - 1);       ed[y - 1].push_back(x - 1);     }     for (int a = 0; a < 1100; a++) col[a] = -1;     kolo(0, 0);     if (!isok) {       cout << -1;       return 0;     }     for (int a = 0; a < n; a++) sp[a] = -1;     int ccc = -1;     for (int a = 0; a < n; a++) {       if (sp[a] == -1) {         ccc++;         dfs(a, ccc);       }     }     for (int a = 0; a < n; a++) getsho(a);     int mini = 0;     for (int a = 0; a < n; a++) {       for (int b = a + 1; b < n; b++) {         if (sp[a] == sp[b] && sho[a][b] > makk[sp[a]] && sho[a][b] < 100000)           makk[sp[a]] = sho[a][b];       }     }     int ss = 0;     for (int a = 0; a < 1200; a++) ss += makk[a];     cout << ss;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300 * 1000 + 20;   int n, a[N];   multiset<int> e;   long long ans;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       if (e.size())         if (*e.begin() < a[i]) {           ans += a[i] - *e.begin();           e.erase(e.begin());           e.insert(a[i]);         }       e.insert(a[i]);     }     return cout << ans << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long max2(long long a, long long int b) {     return (a) > (b) ? (a) : (b);   }   inline long long max3(long long a, long long b, long long c) {     return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));   }   inline long long min2(long long a, long long b) {     return (a) < (b) ? (a) : (b);   }   inline long long min3(long long a, long long b, long long c) {     return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));   }   using namespace std;   const long long modu = 998244353;   long long dr8[] = {0, 0, 1, -1, 1, 1, -1, -1};   long long dc8[] = {1, -1, 0, 0, -1, 1, -1, 1};   long long dr4[] = {0, 1, -1, 0};   long long dc4[] = {1, 0, 0, -1};   inline void deb_vi(vector<long long> a) {     for (long long i = 0; i < a.size(); i++) cout << a[i] <<    ;   }   inline void deb_vll(vector<long long> a) {     for (long long i = 0; i < a.size(); i++) cout << a[i] <<    ;   }   inline bool is_palindrome(const string& s) {     return std::equal(s.begin(), s.end(), s.rbegin());   }   using namespace std::chrono;   long long power(long long a, long long b) {     long long result = 1;     while (b) {       if (b & 1) result = result * a % modu;       a = (a * a) % modu;       b /= 2;       result %= modu;     }     return result;   }   long long fun1(string s) {     long long ans = 0;     long long l = s.length() - 1;     long long exp = 0;     for (long long i = l; i >= 0; i--) {       ans = (s[i] -  0 ) % modu * power(10, exp) + ans;       ans %= modu;       exp += 2;     }     return ans % modu;   }   long long fun2(string s) {     long long ans = 0;     long long l = s.length() - 1;     long long exp = 1;     for (long long i = l; i >= 0; i--) {       ans = (s[i] -  0 ) % modu * power(10, exp) + ans;       ans %= modu;       exp += 2;     }     return ans % modu;   }   int32_t main() {     auto start = high_resolution_clock::now();     {       long long n;       cin >> n;       string a[n];       long long ans = 0;       for (long long i = 0; i < n; i++) {         cin >> a[i];         long long t1 = fun1(a[i]) * n;         t1 %= modu;         long long t2 = fun2(a[i]) * n;         t2 %= modu;         ans += (t1 + t2);         ans %= modu;       }       cout << (ans + modu) % modu;     }     auto stop = high_resolution_clock::now();     auto duration = duration_cast<microseconds>(stop - start);     return 0;   }


#include<iostream>   #include<algorithm>   #include<vector>   #include<bitset>   #include<time.h>   using namespace std;   #define R register int   bitset<200000>S[60];   bool c[60];   int main(){    srand(time(0));    int n,m,p,q;    cin>>n>>m>>p;    q=n-1>>1;    string s;    for(R i=0;i!=n;i++){     cin>>s;     for(R j=0;j!=m;j++){      if(s[j]== 1 ){       S[j][i]=1;      }     }    }    vector<int>A,E;    for(int i=0;i!=m;i++){     c[i]=false;     if(S[i].count()>q){      A.push_back(i);     }    }    if(A.empty()==true){     for(R i=0;i!=m;i++){      cout<<0;     }     return 0;    }    p=0;    while(clock()<2950){     bitset<200000>B=S[A[p]];     vector<int>C,D;     D.push_back(A[p]);     for(R i=A.size()-1;i!=-1;i--){      if(i!=p){       C.push_back(A[i]);      }     }     random_shuffle(C.begin(),C.end());     for(vector<int>::iterator T=C.begin();T!=C.end();T++){      if((B&S[*T]).count()>q){       B&=S[*T];       D.push_back(*T);      }     }     if(D.size()>E.size()){      E=D;     }     p++;     if(p==A.size()){      p=0;     }    }    for(vector<int>::iterator T=E.begin();T!=E.end();T++){     c[*T]=true;    }    for(R i=0;i!=m;i++){     if(c[i]==true){      cout<<1;     }else{      cout<<0;     }    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 998244353;   const long long INF = 1000000000;   void solve() {     long long n;     string s;     cin >> n;     vector<long long> p(n);     vector<long long> ans(n);     for (int i = 0; i < n; i++) {       long long x;       cin >> x;       p[--x] = i;     }     cin >> s;     long long ones = 1;     long long nuls = 1;     for (long long i = 0; i < n; i++)       if (s[i] ==  0 ) ++nuls;     for (long long i = 0; i < n; i++) {       if (s[p[i]] ==  0 ) {         ans[p[i]] = ones;         ++ones;       } else {         ans[p[i]] = nuls;         ++nuls;       }     }     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;       #define send {ios_base::sync_with_stdio(false);}   #define help {cin.tie(NULL);}   #define f first   #define s second   #define pb push_back   #define vi vector<long long>   #define vii vector<pair<long long,long long>>   #define dict unordered_map<long long, long long>   #define contains(d,x) (d.find(x)!=d.end())      typedef long long ll;   typedef long double lld;   typedef unsigned long long ull;      const lld pi = 3.14159265358979323846;   const ll mod = 1000000007;   // const ll mod = 998244353;       ll n, m, k, l, p, q, r, x, y, z, sz;   const ll template_array_size = 1e6 + 5;   ll a[template_array_size];   ll b[template_array_size];   ll c[template_array_size];   string s, t;   ll ans = 0;   ll ts[5000000];      ll power(int x, int y){    if(!y) return 1;    ll temp = power(x, y/2);    temp = (temp*temp)%mod;    if(y&1){     return (temp*x)%mod;    }    return temp;   }      int find(int i, ll rep[]) {    int t = i;    while(rep[i]!=i){     i = rep[i];    }    while(rep[t]!=t){     t = rep[t];     rep[t] = i;    }    return i;   }      int onion(int i, int j, ll rep[]){    i = find(i,rep);    j = find(j,rep);    rep[j] = i;    return i!=j;   }      void solve(int tc = 0) {    cin >> n >> m;    vector<vii> adj(m+1);    vi chosen;    for(int i=0; i<m+1; i++) a[i] = i;    for(int i=0; i<n; i++) {     cin >> x;     if (x==1) {      cin >> y;      x = 0;     } else {      cin >> x >> y;     }     if(onion(x,y,a)){      chosen.pb(i+1);     }    }    p = chosen.size();    ans = power(2,p);    cout << ans <<     << p << endl;    for(int i : chosen) cout << i <<   ;    cout << endl;   }       int main() {           send help       // int tc = 1;    // cin >> tc;    // for (int t = 0; t < tc; t++) solve(t);    solve();   }


#include <bits/stdc++.h>   using namespace std;   int a, b, m[1000];   int s = 0;   int main() {     int n;     cin >> n;     int z = n;     while (n > 0) {       s = 0;       cin >> a >> b;       while (a > 0 && b > 0) {         if (a >= b) {           int t = a / b;           a = a % b;           s += t;         } else {           int t = b / a;           b = b % a;           s += t;         }       }       m[z - n + 1] = s;       --n;     }     int j;     for (j = 1; j <= z; ++j) cout << m[j] << endl;   }


#include <bits/stdc++.h>   using namespace std;   void doRoutine() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     srand(322179);   }   const int C = 5010;   int tr[C * 8];   bool cross(pair<pair<int, int>, int> hor, pair<int, pair<int, int> > vert) {     return (vert.second.first <= hor.second &&             hor.second <= vert.second.second) &&            (hor.first.first <= vert.first && vert.first <= hor.first.second);   }   void init() {     for (int i = 0; i < C * 8; ++i) {       tr[i] = 0;     }   }   int get(int l, int r, int lb = -C, int rb = C, int v = 0) {     if (l == lb && r == rb) {       return tr[v];     } else {       int mb = (lb + rb) / 2;       if (r <= mb) {         return get(l, r, lb, mb, v * 2 + 1);       } else if (l >= mb) {         return get(l, r, mb, rb, v * 2 + 2);       } else {         return get(l, mb, lb, mb, v * 2 + 1) + get(mb, r, mb, rb, v * 2 + 2);       }     }   }   void add(int i, int x, int lb = -C, int rb = C, int v = 0) {     if (lb + 1 == rb) {       tr[v] += x;     } else {       int mb = (lb + rb) / 2;       if (i < mb) {         add(i, x, lb, mb, v * 2 + 1);       } else if (i >= mb) {         add(i, x, mb, rb, v * 2 + 2);       }       tr[v] = tr[v * 2 + 1] + tr[v * 2 + 2];     }   }   signed main() {     doRoutine();     int n;     cin >> n;     vector<pair<int, pair<int, int> > > verts;     vector<pair<pair<int, int>, int> > hors;     for (int i = 0; i < n; ++i) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       if (x1 > x2) {         swap(x1, x2);       }       if (y1 > y2) {         swap(y1, y2);       }       if (x1 == x2) {         verts.push_back({x1, {y1, y2}});       } else {         hors.push_back({{x1, x2}, y1});       }     }     sort(verts.begin(), verts.end());     long long ans = 0;     for (int i = 0; i < verts.size(); ++i) {       vector<pair<int, int> > events;       for (auto seg : hors) {         if (cross(seg, verts[i])) {           events.push_back({seg.first.second, seg.second});         }       }       if (!events.empty()) {         sort(events.begin(), events.end());         reverse(events.begin(), events.end());         int ptr = 0;         init();         for (int j = (int)verts.size() - 1; j > i; --j) {           while ((ptr < events.size() && events[ptr].first >= verts[j].first)) {             add(events[ptr].second, 1);             ++ptr;           }           int cnt = get(verts[j].second.first, verts[j].second.second + 1);           ans += cnt * (cnt - 1) / 2;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( O3 )   #pragma GCC target( avx )   const long long INF = numeric_limits<long long>::max();   const long double PI = 3.1415926535898;   const long long MOD = 1000000007;   const long long LIM = 100005;   long long fpow(long long x, long long y) {     long long temp;     if (y == 0) return 1;     temp = fpow(x, y / 2);     if (y % 2 == 0)       return temp * temp;     else       return x * temp * temp;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   long long modInverse(long long n, long long p) { return fpow(n, p - 2); }   map<long long, long long> primeFactors(long long n) {     map<long long, long long> mp;     while (n % 2 == 0) {       if (mp.count(2) == 1)         mp[2]++;       else         mp.insert({2, 1});       n = n / 2;     }     for (long long i = 3; i <= sqrt(n); i = i + 2) {       while (n % i == 0) {         if (mp.count(i) == 1)           mp[i]++;         else           mp.insert({i, 1});         n = n / i;       }     }     if (n > 2) {       if (mp.count(n) == 1)         mp[n]++;       else         mp.insert({n, 1});     }     return mp;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     for (long long tt = 1; tt <= t; ++tt) {       long long n, ans = 0;       cin >> n;       vector<long long> t(n + 1);       for (long long i = 1; i <= n; i++) cin >> t[i];       t[0] = 0;       sort(t.begin(), t.end());       ;       long long dp[n + 1][2 * n + 1];       for (long long i = 0; i <= n; i++)         for (long long j = 0; j <= 2 * n; j++) dp[i][j] = INF;       for (long long i = 0; i <= 2 * n; i++) dp[0][i] = 0;       for (long long i = 1; i <= n; i++) {         for (long long j = i; j <= 2 * n; j++) {           dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(t[i] - j));         }       }       ans = dp[n][2 * n];       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, l, r, mid, a;   int main() {     cin >> n >> k;     a = 240 - k;     l = 0;     r = n + 1;     while (r - l > 1) {       mid = (r + l) / 2;       if (5 * (mid * (mid + 1) / 2) > a) {         r = mid;       } else {         l = mid;       }     }     cout << l << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long n, k, l, r, mid, ans;   long long a[200100];   bool check(long long x) {     long long p, q;     double sum = 0, s;     for (long long i = 1; i <= n; i++) {       if (a[i] == 0) continue;       s = a[i];       if (a[i] >= k) return 1;       p = x - 1;       q = n - i + x - 1;       p = min(p, q - p);       while (p) {         s = s * q / p;         p--;         q--;         if (s >= k) return 1;       }       sum += s;       if (sum >= k) return 1;     }     return 0;   }   int main() {     scanf( %I64d%I64d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %I64d , &a[i]);       if (a[i] >= k) {         puts( 0 );         return 0;       }     }     l = 1;     r = k;     while (l <= r) {       mid = (l + r) >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else         l = mid + 1;     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const string yno[2] = { NO n ,  YES n };   int dem(vector<int> &a, int k) {     return upper_bound(a.begin(), a.end(), k) -            lower_bound(a.begin(), a.end(), k);   }   int dem(long long k) {     int ans = 0;     while (k) {       ans += k & 1;       k >>= 1;     }     return ans;   }   void process() {     int n, m;     cin >> n >> m;     vector<int> a(n), b(m);     for (int &i : a) cin >> i;     for (int &i : b) cin >> i;     vector<pair<long long, long long> > c;     map<int, pair<long long, long long> > M;     for (int i = 0; i <= n - 1; i++) {       for (int j = 0; j <= m - 1; j++) {         M[a[i] + b[j]].first |= (1ll << i);         M[a[i] + b[j]].second |= (1ll << j);       }     }     for (auto it = M.begin(); it != M.end(); it++) {       c.push_back(it->second);     }     int ans = 0;     for (int i = 0; i < c.size(); i++) {       for (int j = i; j < c.size(); j++) {         ans = max(ans, __builtin_popcountll(c[i].first | c[j].first) +                            __builtin_popcountll(c[i].second | c[j].second));       }     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     process();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 252;   int dis(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }   bool Gao(int x1, int y1, int x2, int y2) {     if (dis(0, 0, x1, y1) <= max(x2, y2)) return true;     for (int i = 1; i <= min(x2, y2); ++i) {       if (dis(x2 - i, y2 - i, x1, y1) <= i) return true;     }     return false;   }   int main() {     int x1, x2, y1, y2;     while (cin >> x1 >> y1 >> x2 >> y2) {       if (Gao(x1, y1, x2, y2))         cout <<  Polycarp  << endl;       else         cout <<  Vasiliy  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, p[1005];   vector<int> T;   bool ok;   inline void enter() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> p[i];   }   void dfs(int u) {     while (T[u] != 1) {       T[u]++;       u = p[u];     }     cout << u <<    ;   }   inline void solve() {     for (int i = 1; i <= n; i++) {       T.assign(n + 4, 0);       dfs(i);     }   }   int main() {     enter();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   string s;   int puc(int k, int t) {     vector<int> v(n + 1, 0);     v[n - 1] = (s[n - 1] ==  H );     for (int i = n - 2; i >= 0; --i) v[i] = v[i + 1] + (s[i] ==  H );     int nh = 0, ns = 0;     for (int i = 0; i < int(n); ++i) {       if (s[i] ==  H )         ++nh;       else if (s[i] ==  S )         ++ns;     }     if (k + ns < nh) return 0;     int zk = k;     int ret = 0;     int qui = -1, q = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  S ) {         ++k;         --ns;       } else if (s[i] ==  H ) {         --nh;         if (k > 0)           --k;         else if (qui == -1) {           qui = i;           q = 1;         } else           ++q;       }       if (nh == 0 and k >= q) {         if (qui == -1 and ret < t) return true;         if (ret + (i - qui) < t) return true;         break;       }       ++ret;     }     k = zk;     int ult = -1;     int r = 0;     int ans = 0;     for (int i = 0; i < int(n); ++i) {       if (s[i] ==  S ) ++k;       if (k >= r and ult != -1) {         ans += i - ult;         if (v[i] > 0) ans += i - ult;         k -= r;         ult = -1;         r = 0;       }       if (s[i] ==  H ) {         if (k > 0)           --k;         else if (ult == -1) {           ult = i;           r = 1;         } else           ++r;       }       if (ult == -1 and v[i + 1] == 0) break;       ++ans;     }     return (ult == -1 and ans < t);   }   int main() {     cin >> n >> t;     cin >> s;     int lo = 0, hi = n, m;     while (hi - lo > 5) {       int m = (lo + hi) / 2;       if (puc(m, t))         hi = m;       else         lo = m;     }     bool trobat = 0;     if (puc(0, t)) {       cout << 0 << endl;       trobat = true;     }     for (int k = lo; !trobat and k <= hi; ++k) {       if (puc(k, t)) {         cout << k << endl;         trobat = true;       }     }     if (!trobat) cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const string YES =  Yes ;   const string NO =  No ;   string solve() {     int N;     cin >> N;     vector<int> p(N);     for (int i = 0; i < (N); ++i) {       cin >> p[i];     }     for (int i = 0; i < (N - 1); ++i) {       if (p[i] + 1 == p[i + 1]) {         continue;       }       if (p[i] > p[i + 1]) {         continue;       }       return NO;     }     return YES;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int T;     cin >> T;     for (int t = 0; t < (T); ++t) {       cout << solve() << endl;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a, b, c, ans, boby, apu;     cin >> n;     while (n--) {       cin >> a >> b >> c >> ans;       boby = b - a;       apu = c + ans;       if (boby % apu == 0) {         cout << boby / apu << endl;       } else         cout <<  -1  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9;   const int N = 5001;   const int maxxor = (1 << 13);   int f[2][maxxor];   vector<int> a[N];   int cnp(int l, int r, int pos, int id) {     if (l > r) return oo;     int mid = (l + r) / 2;     if (a[id][mid] > pos) return min(a[id][mid], cnp(l, mid - 1, pos, id));     return cnp(mid + 1, r, pos, id);   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       a[x].push_back(i);     }     for (int i = 0; i <= 1; i++)       for (int j = 0; j < maxxor; j++) f[i][j] = oo;     f[0][0] = 0;     int id;     vector<int> res;     for (int i = 1; i < N; i++) {       int now = i & 1;       int back = now ^ 1;       for (int j = 0; j < maxxor; j++) {         f[now][j] = f[back][j];         if (a[i].size() == 0) {           if (i == N - 1 && f[now][j] != oo) res.push_back(j);           continue;         }         int pos = f[back][j ^ i];         id = cnp(0, a[i].size() - 1, pos, i);         if (id != oo) f[now][j] = min(f[now][j], id);         if (i == N - 1 && f[now][j] != oo) res.push_back(j);       }     }     cout << res.size() << endl;     for (int xnew : res) cout << xnew <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int add(int a, int b) {     return (a + b >= 1000000007) ? a + b - 1000000007 : a + b;   }   inline void inc(int &a, int b) { a = add(a, b); }   inline int sub(int a, int b) {     return (a - b < 0) ? a - b + 1000000007 : a - b;   }   inline void dec(int &a, int b) { a = sub(a, b); }   inline int mul(int a, int b) { return 1ll * a * b % 1000000007; }   inline void grow(int &a, int b) { a = mul(a, b); }   const int INV = 205555557;   int phi[1000005];   int pre[1000005];   int f[1000005];   int onepre[1000005];   int twopre[1000005];   void phiCalc() {     phi[0] = 0;     phi[1] = 1;     for (int i = 2; i <= 1000005 - 1; i++) phi[i] = i;     for (int i = 2; i <= 1000005 - 1; i++) {       if (phi[i] == i) {         for (int j = i; j <= 1000005 - 1; j += i) phi[j] -= phi[j] / i;       }     }   }   int func(int n, int m) {     int res = sub(0, INV);     grow(res, n);     grow(res, n + 1);     grow(res, n + 2);     int tres = 15;     inc(tres, mul(4, mul(n, mul(n, n))));     dec(tres, mul(12, mul(n, n)));     dec(tres, mul(7, n));     int ttres = mul(n, n);     dec(ttres, mul(3, n));     dec(ttres, 3);     grow(ttres, sub(0, 6));     grow(ttres, m);     return mul(res, add(tres, ttres));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     phiCalc();     for (int i = 1; i < 1000005; i++) {       for (int j = i; j < 1000005; j += i) inc(f[j], mul(i, phi[j / i]));     }     pre[0] = f[0];     onepre[0] = twopre[0] = 0;     for (int i = 1; i < 1000005; i++) {       pre[i] = add(pre[i - 1], f[i]);       onepre[i] = add(onepre[i - 1], mul(i, f[i]));       twopre[i] = add(twopre[i - 1], mul(mul(i, i), f[i]));     }     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       if (n > m) swap(n, m);       int ans = 0;       inc(ans, pre[n]);       inc(ans, twopre[n]);       dec(ans, mul(2, onepre[n]));       inc(ans, mul(m, mul(n, pre[n])));       dec(ans, mul(m, onepre[n]));       inc(ans, mul(m, pre[n]));       dec(ans, mul(n, onepre[n]));       inc(ans, mul(n, pre[n]));       grow(ans, 2);       inc(ans, func(n, m));       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int f = 1, x = 0;     char s = getchar();     while (s <  0  || s >  9 ) {       if (s ==  - ) f = -1;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = x * 10 + s -  0 ;       s = getchar();     }     return x * f;   }   int len[300 + 5];   string str[300 + 5];   bool vis[300 + 5][300 + 5];   int main() {     int n = read(), Ans;     for (int i = 1; i <= n; i++) {       cin >> str[i];       len[i] = int(str[i].length()) + len[i - 1];     }     Ans = len[n] + n - 1;     for (int i = 1; i <= n; i++) {       for (int j = i; j <= n; j++) {         int tmp = 0;         if (vis[i][j]) continue;         vis[i][j] = 1;         for (int t = j + 1; t <= n - j + i; t++) {           bool flag = 0;           for (int s = 0; s <= j - i; s++)             if (str[i + s] != str[t + s]) {               flag++;               break;             }           if (!flag) tmp++, t += j - i, vis[t][t + j - i] = 1;         }         if (tmp != 0) {           int tot = len[n] - (tmp + 1) * (len[j] - len[i - 1]) + tmp + n;           Ans = min(Ans, tot);         }       }     }     printf( %d n , Ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18, MAXN = 1e5 + 128, MOD = 998244353;   long long binpow(long long a, long long n) {     long long res = 1;     while (n) {       if (n & 1) res *= a;       a *= a;       n >>= 1;     }     return res;   }   long long NOD(long long NOD1, long long NOD2) {     while (NOD2) {       NOD1 %= NOD2;       swap(NOD1, NOD2);     }     return NOD1;   }   long long NOK(long long NOK1, long long NOK2) {     return NOK1 * NOK2 / NOD(NOK1, NOK2);   }   long long n, i1, i2;   string s;   char a, b;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> s;     a = s[0];     for (int i = 0; i < n; i++)       if (a == s[i])         i1 = i;       else         break;     b = s[n - 1];     for (int i = n - 1; i >= 0; i--)       if (b == s[i])         i2 = i;       else         break;     i2 = n - i2;     if (b == a)       cout << ((i1 + 2) * (i2 + 1)) % MOD;     else       cout << (i2 + i1 + 2) % MOD;   }


#include <bits/stdc++.h>   using namespace std;   const int maxint = 0x7fffffff;   const int minint = 0x80000000;   const double eps = 1e-6;   int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int max(int a, int b) { return a > b ? a : b; }   int main() {     int sum, d, n, a;     scanf( %d , &d);     scanf( %d , &n);     sum = 0;     while (--n) {       scanf( %d , &a);       sum += d - a;     }     scanf( %d , &a);     printf( %d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t, n;     cin >> t;     while (t--) {       string A, B;       cin >> n;       cin >> A >> B;       int f = 1;       for (int i = 0; i < n; ++i) {         if (A[i] > B[i]) f = 0;       }       if (!f) {         cout << -1 <<   n ;         continue;       }       int ans = 0;       for (int i = 0; i < 26; ++i) {         bitset<27> good;         for (int j = 0; j < n; ++j) {           if (A[j] == B[j]) continue;           if (B[j] == char( a  + i)) {             good[A[j] -  a ] = 1;           }         }         ans += good.count();         for (int j = 0; j < n; ++j) {           if (A[j] == B[j]) continue;           if (good[A[j] -  a ] == 1) A[j] = char( a  + i);         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e5 + 5;   int n, k;   vector<pair<int, int> > g[M];   int ans;   int dp[M][2];   void dfs(int u, int Pa) {     vector<pair<int, int> > ch;     for (auto p : g[u]) {       int v = p.first, w = p.second;       if (v == Pa) continue;       dfs(v, u);       ch.push_back(make_pair(dp[v][0] + w, dp[v][1] + w));     }     bool flag = ch.size() > 0;     if (flag) {       sort(ch.begin(), ch.end(), [&](pair<int, int> a, pair<int, int> b) {         return a.second > b.second;       });     }     for (int i = 0; i < k - 1 && i < ch.size(); i++) {       dp[u][1] += ch[i].second;     }     if (ch.size() <= k) {       for (auto p : ch) {         dp[u][0] += p.second;       }       if (flag) {         sort(ch.begin(), ch.end(), [&](pair<int, int> a, pair<int, int> b) {           return a.first - a.second > b.first - b.second;         });         dp[u][0] += ch[0].first - ch[0].second;       }     } else {       int max_val = -1e9;       for (int i = k; i < ch.size(); i++) {         max_val = max(max_val, ch[i].first);       }       int sum = 0;       for (int i = 0; i < k; i++) {         sum += ch[i].second;       }       for (int i = 0; i < k; i++) {         dp[u][0] = max(dp[u][0], sum - ch[i].second + max(max_val, ch[i].first));       }     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i < n; i++) {       int u, v, c;       scanf( %d%d%d , &u, &v, &c);       u++, v++;       g[u].push_back(make_pair(v, c));       g[v].push_back(make_pair(u, c));     }     dfs(1, 0);     printf( %d n , dp[1][0]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100002;   int arr[N], n, inc[N], decr[N];   int tree[4 * N];   int update(int pos, int l, int r, int x, int v) {     if (l > x || r < x) return tree[pos];     if (l == r) return tree[pos] = max(tree[pos], v);     int mid = (l + r) / 2;     return tree[pos] = max(update(pos * 2, l, mid, x, v),                            update(pos * 2 + 1, mid + 1, r, x, v));   }   int get(int pos, int l, int r, int qLow, int qHigh) {     if (l > qHigh || r < qLow) return 0;     if (l >= qLow && r <= qHigh) return tree[pos];     int mid = (l + r) / 2;     return max(get(pos * 2, l, mid, qLow, qHigh),                get(pos * 2 + 1, mid + 1, r, qLow, qHigh));   }   int bitonic() {     int i, max;     inc[1] = 1;     update(1, 0, n, arr[1], 1);     for (i = 2; i <= n; i++) {       int x = get(1, 0, n, 0, arr[i]);       inc[i] = x + 1;       update(1, 0, n, arr[i], inc[i]);     }     memset(tree, 0, sizeof tree);     decr[n] = 1;     update(1, 0, n, arr[n], 1);     for (i = n - 1; i > 0; i--) {       int x = get(1, 0, n, 0, arr[i]);       decr[i] = x + 1;       update(1, 0, n, arr[i], decr[i]);     }     max = 0;     for (i = 0; i <= n; i++)       if (inc[i] + decr[i] - 1 > max) max = inc[i] + decr[i] - 1;     return max;   }   int main() {     int a, b;     scanf( %d%d , &a, &n);     for (int i = 0; i < a; i++) {       int x, y;       scanf( %d%d , &x, &y);       arr[x]++, arr[y + 1]--;     }     for (int i = 1; i <= n; i++) arr[i] += arr[i - 1];     printf( %d , bitonic());     return 0;   }


#include <bits/stdc++.h>   int main() {     char s[50];     scanf( %s , s);     int i, l = strlen(s), flag = 0;     for (i = 0; i < l; i++) {       if (s[i] != s[l - i - 1]) flag++;     }     if (flag == 2)       printf( YES );     else if (flag == 0 && strlen(s) % 2 == 1)       printf( YES );     else       printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long l, r, f[100];   bool check(string s, int x, int y) {     if (s.size() % x != 0) return false;     for (int i = 0; i + x < s.size() and i + x < y + 1; i++)       if (s[i] != s[i + x]) return false;     return true;   }   long long p(int x) {     long long ans = 0;     for (int i = 1; i <= x; i++)       if (x % i == 0) {         f[i] = pow(2, i - 1);       }     for (int j = 1; j < x; j++) {       if (x % j != 0) continue;       ans += f[j];       for (int u = j * 2; u < x; u += j) f[u] -= f[j];     }     return ans;   }   long long solve(long long a) {     long long ans = 0;     string s =   ;     while (a) {       s += (a % (long long)2) +  0 ;       a /= (long long)2;     }     reverse(s.begin(), s.end());     bool no = 0;     for (int i = 1; i < s.size(); i++) {       if (check(s, i, 100000)) {         no = 1;       }     }     for (int i = 1; i < s.size(); i++) {       if (s[i] ==  0 ) continue;       s[i] =  0 ;       for (int j = 1; j <= i; j++) {         f[j] = check(s, j, i);       }       for (int j = i + 1; j < s.size(); j++) f[j] = pow(2, j - i - 1);       s[i] =  1 ;       for (int j = 1; j < s.size(); j++) {         if (s.size() % j != 0) continue;         ans += f[j];         for (int u = j * 2; u < s.size(); u += j) f[u] -= f[j];       }     }     for (int i = s.size() - 1; i >= 1; i--) ans += p(i);     ans += no;     return ans;   }   int main() {     scanf( %lld%lld , &l, &r);     printf( %lld , solve(r) - solve(l - 1));   }


#include <bits/stdc++.h>   long long z;   int l, r, i, j, k, ans;   short p;   std::vector<short> prm;   std::vector<int> to;   short dp[3000111];   bool done[3000111];   void dfs(long long now, char cnt) {     to.push_back(now);     for (; cnt < prm.size() && now * prm[cnt] <= r; ++cnt)       dfs(now * prm[cnt], cnt);   }   int main() {     prm.reserve(29);     to.reserve(3000111);     scanf( %d %d %hd , &l, &r, &p);     for (i = 2; i < p; ++i) {       for (j = 2; j * j <= i; ++j)         if (i % j == 0) break;       if (j * j > i) prm.push_back(i);     }     dfs(1, 0);     std::sort(to.begin(), to.end());     memset(dp, 0x3f, sizeof(dp));     dp[0] = 0;     for (i = 2; i < p; ++i)       for (j = 0, k = 1; j < to.size(); ++j) {         for (z = to[j] * i; k < to.size() && to[k] < z; ++k)           ;         if (to[k] == z && dp[k] > dp[j] + 1) {           dp[k] = dp[j] + 1;           if (i + dp[k] <= p) done[k] = true;         }       }     r = std::min(r, to.back());     for (i = 0; i < to.size() && to[i] < l; ++i)       ;     for (; i < to.size() && to[i] <= r; ++i)       if (done[i]) ++ans;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   long long N;   long long fac(long long x) {     long long res = 1;     for (int i = 1; i <= x; ++i) res = res * i % mod;     return res;   }   long long qpow(long long x, long long y) {     long long res = 1;     for (; y; x = x * x % mod, y >>= 1)       if (y & 1) res = res * x % mod;     return res;   }   int main() {     scanf( %lld , &N);     long long fz = fac(2 * N - 1), fm = fac(N - 1) * fac(N) % mod;     printf( %lld n , (2 * fz % mod * qpow(fm, mod - 2) % mod - N + mod) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<long long int> > dp;   vector<long long int> a;   long long int check(long long int l, long long int r) {     if (l >= r) return 0;     if (dp[l][r]) return dp[l][r];     if (l + 1 == r) {       dp[l][r] = a[r];       return a[r];     }     dp[l][r] = -1;     for (int m = l + 1; m < r; ++m) {       long long int lf = check(l, m), rt = check(m, r);       if (lf > 0 && lf == rt) {         dp[l][r] = lf + 1;         return lf + 1;       }     }     return dp[l][r];   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     a.resize(n + 1);     for (int i = 1; i <= n; ++i) cin >> a[i];     dp.assign(n + 1, vector<long long int>(n + 1, 0));     vector<long long int> ans(n + 1, 1e9);     ans[0] = 0;     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j <= n; ++j) {         if (check(i, j) > 0) ans[j] = min(ans[j], ans[i] + 1);       }     }     cout << ans[n];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, B, c;     cin >> n >> k >> B >> c;     long long a[n];     for (int i = 0; i < n; i++) cin >> a[i];     long long x = *min_element(a, a + n);     for (int i = 0; i < n; i++) a[i] = a[i] - x;     B = min(B, 5 * c);     long long ans = 1e18;     for (int t = 0; t < 5; t++) {       pair<long long, long long> b[n];       for (int i = 0; i < n; i++) {         b[i] = {a[i], 0};         while (b[i].first % 5 != t) {           b[i].first++;           b[i].second += c;         }         b[i].first /= 5;       }       sort(b, b + n);       priority_queue<long long> G;       long long suma = 0;       for (int i = 0; i < n; i++) {         if (G.size() >= k - 1) {           ans = min(ans, b[i].second + b[i].first * B * (k - 1) + suma);         }         G.push(b[i].second - b[i].first * B);         suma += b[i].second - b[i].first * B;         while (G.size() >= k) {           suma -= G.top();           G.pop();         }       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( Ofast,no-stack-protector )   using namespace std;   function<void(void)> ____ = []() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   };   template <typename T>   vector<T> &operator<<(vector<T> &__container, T x) {     __container.push_back(x);     return __container;   }   template <typename T>   ostream &operator<<(ostream &out, vector<T> &__container) {     for (T _ : __container) out << _ <<    ;     return out;   }   const int MAXN = 1e5 + 7;   struct EDGE {     int to, cap, rev;     EDGE(){};     EDGE(int to, int cap, int rev) : to(to), cap(cap), rev(rev){};   };   vector<EDGE> G[MAXN];   int iter[MAXN], rk[MAXN];   void ADDEDGE(int u, int v, int cap) {     G[u].push_back(EDGE(v, cap, (int)G[v].size()));     G[v].push_back(EDGE(u, 0, (int)G[u].size() - 1));   }   bool bfs() {     memset(rk, 0, sizeof(rk));     memset(iter, 0, sizeof(iter));     rk[0] = 1;     queue<int> que;     que.push(0);     while (!que.empty()) {       int u = que.front();       que.pop();       for (auto e : G[u]) {         if (!e.cap or rk[e.to]) continue;         rk[e.to] = rk[u] + 1;         que.push(e.to);       }     }     return rk[MAXN - 1] != 0;   }   int dfs(int u, int flow) {     if (u == MAXN - 1) return flow;     for (int &i = iter[u]; i < (int)G[u].size(); i++) {       auto &e = G[u][i];       if (!e.cap or rk[e.to] != rk[u] + 1) continue;       int d = dfs(e.to, min(e.cap, flow));       if (d) {         e.cap -= d;         G[e.to][e.rev].cap += d;         return d;       }     }     return 0;   }   int Dinic() {     int flow = 0;     while (bfs()) {       int d = dfs(0, 0x3f3f3f3f);       while (d) {         flow += d;         d = dfs(0, 0x3f3f3f3f);       }     }     return flow;   }   int n, m, idud[222][222], idlr[MAXN][222];   char g[222][222];   void solve() {     scanf( %d , &n);     scanf( %d , &m);     int cur = 1, cnt = 0, tot = 0;     for (int i = 1; i <= n; i++) scanf( %s , g[i] + 1);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (g[i][j] ==  # ) tot++;     for (int i = 1; i < n; i++)       for (int j = 1; j <= m; j++) idud[i][j] = cur++;     for (int i = 1; i <= n; i++)       for (int j = 1; j < m; j++) idlr[i][j] = cur++;     for (int i = 1; i < n; i++)       for (int j = 1; j <= m; j++)         if (g[i][j] ==  #  and g[i + 1][j] ==  # )           ADDEDGE(0, idud[i][j], 1), cnt++;     for (int i = 1; i <= n; i++)       for (int j = 1; j < m; j++)         if (g[i][j] ==  #  and g[i][j + 1] ==  # )           ADDEDGE(idlr[i][j], MAXN - 1, 1), cnt++;     for (int i = 1; i < n; i++)       for (int j = 1; j <= m; j++) {         if (g[i][j] ==  #  and g[i + 1][j] ==  # ) {           if (g[i][j - 1] ==  # ) ADDEDGE(idud[i][j], idlr[i][j - 1], 0x3f3f3f3f);           if (g[i + 1][j - 1] ==  # )             ADDEDGE(idud[i][j], idlr[i + 1][j - 1], 0x3f3f3f3f);           if (g[i][j + 1] ==  # ) ADDEDGE(idud[i][j], idlr[i][j], 0x3f3f3f3f);           if (g[i + 1][j + 1] ==  # )             ADDEDGE(idud[i][j], idlr[i + 1][j], 0x3f3f3f3f);         }       }     cout << tot - (cnt - Dinic()) <<   n ;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int x, y;     y = scanf( %d , &x);     return x;   }   const int N = -1;   int main() {     int X = in(), d = in();     vector<long long> v;     v.push_back(0);     int b = 0;     while (X) {       if (X % 2) {         v.push_back(v.back() + d);         long long back = v.back();         for (int j = 0; j < b; j++) v.push_back(back + d);       }       X /= 2;       b++;     }     cout << (int)v.size() - 1 << endl;     for (int i = 1; i < v.size(); i++) cout << v[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   const int maxN = 2000;   const unsigned int mod = 998244353;   constexpr int inf = INT_MAX;   class Array {    public:     inline const int *begin() const { return data; }     inline const int *end() const { return endPtr; }     inline void push_back(const int x) { *(endPtr++) = x; }     inline size_t size() const { return endPtr - data; }     template <class T>     inline void foreach (T fun) const {       std::for_each<const int *>(data, endPtr, fun);     }       private:     int data[maxN + 10], *endPtr = data;   };   Array edgeIn[maxN + 10], edgeOut[maxN + 10];   int originDegree[maxN + 10], degree[maxN + 10];   inline void reset(const int n) {     std::memcpy(degree, originDegree, sizeof(degree[0]) * n);   }   void readGraph(const int n) {     for (int i = 0; i < n; ++i) {       static char str[maxN + 10];       std::cin >> str;       for (int j = 0; j < n; ++j)         if (str[j] ==  1 ) {           edgeIn[j].push_back(i);           edgeOut[i].push_back(j);           ++originDegree[i];         }     }   }   bool isStrongConnect(const int n) {     std::sort(degree, degree + n);     int sum = 0;     for (int i = 0; i + 1 < n; ++i) {       sum += degree[i];       if (i * (i + 1) / 2 == sum) return false;     }     return true;   }   void reverseSet(const int n, const unsigned int set) {     for (int i = 0; i < n; ++i)       for (auto to : edgeOut[i])         if ((((set) >> (i)) & 0x01u) ^ (((set) >> (to)) & 0x01u)) {           --degree[i];           ++degree[to];         }   }   inline bool checkSet(const int n, const unsigned int set) {     reset(n);     reverseSet(n, set);     return isStrongConnect(n);   }   void reverseSingle(const int vertex) {     edgeOut[vertex].foreach ([](int p) { ++degree[p]; });     edgeIn[vertex].foreach ([](int p) { --degree[p]; });     degree[vertex] = edgeIn[vertex].size();   }   inline bool checkSingle(const int n, const int vertex) {     reset(n);     reverseSingle(vertex);     return isStrongConnect(n);   }   unsigned int factor(const unsigned int n) {     unsigned int ret = 1;     for (unsigned int i = 2; i <= n; ++i) ret = (ret * i) % mod;     return ret;   }   std::pair<int, int> enumSet(const int n) {     int count = inf, solution = 0;     const unsigned int msk = 1u << n;     for (unsigned int i = 1; i < msk; ++i) {       const int cnt = __builtin_popcount(i);       if (cnt <= count && checkSet(n, i)) {         if (cnt == count)           ++solution;         else {           solution = 1;           count = cnt;         }       }     }     return std::make_pair(count,                           count != inf ? (solution * factor(count)) % mod : 0);   }   std::pair<int, int> enumVertex(const int n) {     int solution = 0;     for (int i = 0; i < n; ++i) solution += checkSingle(n, i);     return std::make_pair(1, solution);   }   int main() {     std::ios::sync_with_stdio(false);     int n;     std::cin >> n;     readGraph(n);     reset(n);     if (isStrongConnect(n)) {       std::cout <<  0 1 n ;       return 0;     }     const auto [count, solution] = n <= 6 ? enumSet(n) : enumVertex(n);     if (count == inf)       std::cout <<  -1 n ;     else       std::cout << count <<     << solution <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, branch[10001], total;   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       branch[a]++, branch[b]++;     }     for (int i = 1; i <= n; i++) total += branch[i] * (branch[i] - 1) / 2;     cout << total << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, ans = -1;     cin >> n;     int a[n], l[n], r[n];     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0, des = 0; i < n; ++i) {       ++des;       l[i] = des = min(des, a[i]);     }     for (int i = n - 1, des = 0; i >= 0; --i) {       ++des;       r[i] = des = min(des, a[i]);     }     for (int i = 0; i < n; ++i) ans = max(ans, min(l[i], r[i]));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long add(long long a, long long b) {     return ((a % 1000000007LL) + (b % 1000000007LL)) % 1000000007LL;   }   inline long long mul(long long a, long long b) {     return ((a % 1000000007LL) * (b % 1000000007LL)) % 1000000007LL;   }   inline long long sub(long long a, long long b) {     return ((a % 1000000007LL) - (b % 1000000007LL) + 1000000007LL) %            1000000007LL;   }   void prefixInversionAndSwap(string &ss, long long x) {     for (long long i = 0; i <= x; i++) {       if (ss[i] ==  0 ) {         ss[i] =  1 ;       } else {         ss[i] =  0 ;       }     }     for (long long i = 0; i <= (x / 2); i++) {       swap(ss[i], ss[x - i]);     }     return;   }   void Flash() {     long long n;     cin >> n;     string a, b;     cin >> a >> b;     vector<long long> ans;     long long swapped = 0;     long long ind = 0, nai, bbit;     long long firstBit = a[0] -  0 ;     for (long long i = (n - 1); i > 0; i--) {       if (swapped) {         nai = (a[ind - i] -  0 );         nai ^= 1;       } else {         nai = (a[ind + i] -  0 );       }       bbit = b[i] -  0 ;       if (nai != bbit) {         if (nai == 1) {           if (firstBit == 0) {             ans.push_back(1);           }           ans.push_back(i + 1);           if (swapped) {             ind -= i;           } else {             ind += i;           }           swapped ^= 1;           firstBit = (nai ^ 1);         } else {           if (firstBit == 1) {             ans.push_back(1);           }           ans.push_back(i + 1);           if (swapped) {             ind -= i;           } else {             ind += i;           }           swapped ^= 1;           firstBit = (nai ^ 1);         }       }     }     if (firstBit != (b[0] -  0 )) {       ans.push_back(1);     }     cout << ans.size() <<    ;     for (auto it : ans) {       cout << it <<    ;     }     cout <<   n ;   }   int main(int argc, char const *argv[]) {     ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);     ;     long long t = 1;     cin >> t;     for (long long i = 1; i <= t; i++) {       Flash();     }     return 0;   }


#include <bits/stdc++.h>   #define int long long   using namespace std;      pair<int, int> extgcd(int a, int b) {       if(!b) return {1, 0};       pair<int, int> val = extgcd(b, a % b);       return {val.second, val.first - (a / b) * val.second};   }      signed main(){       ios_base::sync_with_stdio(false);       cin.tie(0);       int t; cin >> t;       while(t--){           int n; cin >> n;           string s; cin >> s;           int cnt = 0, cnt2 = 0;           for(int i = 0; i < n; ++i){               if(s[i] ==  1  && s[i + 1] ==  1 ){                   ++cnt; ++i;               }               else if(s[i] ==  1 ){                   ++cnt2;               }           }           int ans = 1, mod = 998244353, can = n - 1 - cnt2;           for(int i = n - cnt - cnt2; i > cnt; --i){               ans *= i; ans %= mod;           }           for(int i = n - cnt - cnt - cnt2; i >= 1; --i){               ans *= (extgcd(i, mod).first + mod) % mod; ans %= mod;           }           cout << ans <<   n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int countbit(int n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); }   int lowbit(int n) { return (n ^ (n - 1)) & n; }   const double pi = acos(-1.0);   const double eps = 1e-11;   template <class T>   T sqr(T x) {     return x * x;   }   int n, m, k;   int c[101], clen;   bool Nim(int k) {     for (int i = 0; i < 8; ++i) {       int sum = 0;       for (int j = 0; j < clen; ++j) sum += (c[j] >> i) & 1;       if (sum % (k + 1)) return true;     }     return false;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     int aw = 0, bw = 0;     for (int i = 0; i < n; ++i) {       char str[101];       char *pos, *pos1, *pos2;       scanf( %s , str);       pos = strchr(str,  - );       pos2 = strchr(str,  R );       pos1 = strchr(str,  G );       if (pos && !pos1 && pos2) bw = 1;       if (pos && !pos2 && pos1) aw = 1;       if (pos1 && pos2) c[clen++] = abs(pos1 - pos2) - 1;     }     if (aw && bw) {       printf( Draw n );       return 0;     } else if (aw) {       printf( First n );       return 0;     } else if (bw) {       printf( Second n );       return 0;     }     if (Nim(k))       printf( First n );     else       printf( Second n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class C>   inline void mini(C& a4, C b4) {     a4 = min(a4, b4);   }   template <class C>   inline void maxi(C& a4, C b4) {     a4 = max(a4, b4);   }   const int INF = 1e9 + 7;   const int base = 1e9;   const int SIGMA = 26;   const int N = 1e5 + 7;   int n;   string s;   vector<int> ans;   void rec(int v) {     if (v == n) return;     if (s[v] ==  l ) {       ans.push_back(v);       rec(v + 1);     } else {       rec(v + 1);       ans.push_back(v);     }   }   int main() {     std::ios_base::sync_with_stdio(0);     cout << setprecision(15) << fixed;     cin >> s;     n = ((int)(s).size());     rec(0);     reverse((ans).begin(), (ans).end());     for (__typeof((ans).begin()) i = (ans).begin(); i != (ans).end(); ++i)       printf( %d n , *i + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   bool cmax(int &a, int b) { return (a < b) ? a = b, 1 : 0; }   bool cmin(int &a, int b) { return (a > b) ? a = b, 1 : 0; }   template <typename T>   T read() {     T ans = 0, f = 1;     char ch = getchar();     while (!isdigit(ch) && ch !=  - ) ch = getchar();     if (ch ==  - ) f = -1, ch = getchar();     while (isdigit(ch))       ans = (ans << 3) + (ans << 1) + (ch -  0 ), ch = getchar();     return ans * f;   }   void file() {}   int n, dep[N];   int same;   void input() {     n = read<int>();     for (register int i = (int)1; i <= (int)n + 1; ++i) {       dep[i] = read<int>();       if (dep[i] > 1 && dep[i - 1] > 1) same = i;     }   }   void work() {     int old = 0, now = 0;     if (!same) {       puts( perfect );       return;     }     puts( ambiguous );     for (register int i = (int)1; i <= (int)n + 1; ++i) {       for (register int j = (int)1; j <= (int)dep[i]; ++j) {         now++;         printf( %d  , old);       }       old = now;     }     printf(  n );     old = now = 0;     for (register int i = (int)1; i <= (int)n + 1; ++i) {       for (register int j = (int)1; j <= (int)dep[i]; ++j) {         now++;         if (i == same) {           printf( %d  , old - 1);           same = 0;         } else           printf( %d  , old);       }       old = now;     }   }   int main() {     file();     input();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using pii = pair<int, int>;   constexpr int N = 2e5 + 5;   int t, a, b, p;   string s;   void Solve() {     cin >> t;     while (t--) {       cin >> a >> b >> p >> s;       s.pop_back();       vector<ll> suf;       suf.resize(s.size());       char last =  0 ;       for (int i = s.size() - 1; i >= 0; i--) {         if (s[i] != last) suf[i] = (s[i] ==  A  ? a : b);         last = s[i];       }       for (int i = suf.size() - 2; i >= 0; i--) suf[i] += suf[i + 1];       int id = 0;       while (id < suf.size() && suf[id] > p) id++;       cout << id + 1 <<   n ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     Solve();     return 0;   }


#include <bits/stdc++.h>   const int N = 1e5 + 1e4;   struct edg {     int now, to, val;   } ed[N];   struct edge {     int to, next;   } e[N];   int ehead[N], ecnt;   inline void add_edge(int now, int to) {     ecnt++;     e[ecnt].to = to;     e[ecnt].next = ehead[now];     ehead[now] = ecnt;   }   int n, m, left, rig, mid;   int pos[N], ans[N], acnt, pcnt;   void dfs(int now) {     pos[now] = 1;     for (int i = ehead[now]; i; i = e[i].next)       if (pos[e[i].to] == 0) dfs(e[i].to);     pos[now] = ++pcnt;   }   inline bool check(int lim) {     ecnt = acnt = 0;     memset(ehead, 0, sizeof(ehead));     memset(pos, 0, sizeof(pos));     for (int i = 1; i <= m; i++)       if (ed[i].val > lim) add_edge(ed[i].now, ed[i].to);     for (int i = 1; i <= n; i++)       if (!pos[i]) dfs(i);     for (int i = 1; i <= m; i++)       if (pos[ed[i].now] < pos[ed[i].to])         if (ed[i].val > lim)           return false;         else           ans[++acnt] = i;     return true;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++)       scanf( %d%d%d , &ed[i].now, &ed[i].to, &ed[i].val);     rig = 1e9;     while (left != rig) {       mid = (left + rig) >> 1;       if (check(mid))         rig = mid;       else         left = mid + 1;     }     check(left);     printf( %d %d n , left, acnt);     for (int i = 1; i <= acnt; i++) printf( %d  , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 2e5 + 10;   int beg[N];   int nxt[N];   int to[N];   int tot;   int a[N];   void add(int x, int y) {     ++tot;     nxt[tot] = beg[x];     to[tot] = y;     beg[x] = tot;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, m, k;     cin >> n >> m >> k;     for (int i = 1; i <= n; ++i) {       int x;       cin >> x;       add(x, i);     }     int ans = 0;     for (int i = 1; i <= m; ++i) {       int siz = 0;       for (int j = beg[i]; j; j = nxt[j]) a[++siz] = to[j];       if (siz == 0) continue;       reverse(a + 1, a + 1 + siz);       for (int r = 1, l = 1; r <= siz; ++r) {         for (; l < r && a[r] - a[l] + 1 > k + r - l + 1; ++l)           ;         ans = max(ans, r - l + 1);       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, k, p, x;     cin >> n >> k >> p;     std::vector<int> even, odd;     for (int i = 0; i < n; ++i) {       cin >> x;       if (x % 2)         odd.emplace_back(x);       else         even.emplace_back(x);     }     if (odd.size() < k - p || (odd.size() - k - p) % 2 != 0) {       cout <<  NO ;       return 0;     }     if ((even.size() + (odd.size() - (k - p)) / 2) < p) {       cout <<  NO  <<   n ;       return 0;     }     cout <<  YES  <<   n ;     long long cnt = 0;     if (k - p) {       while (even.size() > 0 && cnt != p) {         cout << 1 <<     << even.back() <<   n ;         even.pop_back();         cnt++;       }       while (odd.size() > 0 && cnt != p) {         cout << 2 <<     << odd.back() <<    ;         odd.pop_back();         cout << odd.back() <<   n ;         odd.pop_back();         cnt++;       }       for (int i = 0; i < k - p - 1; i++) {         vector<int> ans;         while (even.size() > 0) {           ans.emplace_back(even.back());           even.pop_back();         }         ans.emplace_back(odd.back());         odd.pop_back();         cout << ans.size() <<    ;         for (auto i : ans) {           cout << i <<    ;         }         cout <<   n ;       }       vector<int> ans;       while (odd.size() > 0) {         ans.emplace_back(odd.back());         odd.pop_back();       }       while (even.size() > 0) {         ans.emplace_back(even.back());         even.pop_back();       }       cout << ans.size() <<    ;       for (auto i : ans) {         cout << i <<    ;       }       cout <<   n ;     } else {       vector<int> ans;       for (int i = 0; i < p - 1; i++) {         if (even.size() > 0) {           cout << 1 <<     << even.back() <<   n ;           even.pop_back();         } else {           cout << 2 <<     << odd.back() <<    ;           odd.pop_back();           cout << odd.back() <<    ;           odd.pop_back();         }       }       while (even.size() > 0) {         ans.emplace_back(even.back());         even.pop_back();       }       while (odd.size()) {         ans.emplace_back(odd.back());         odd.pop_back();       }       cout << ans.size() <<    ;       for (auto I : ans) {         cout << I <<    ;       }     }   }


#include <bits/stdc++.h>   template <class T>   int gmax(T &a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int gmin(T &a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(const char *s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   const int MAXN = 2e5 + 5;   const int MOD = 998244353;   int a[MAXN];   int b[MAXN];   int id[MAXN];   long long cnt[MAXN];   int cmp(int x, int y) { return cnt[x] > cnt[y]; }   int main() {     int n;     while (~scanf( %d , &n)) {       for (int i = 0; i < n; ++i) scanf( %d , &a[i]);       for (int i = 0; i < n; ++i) scanf( %d , &b[i]);       sort(b, b + n);       for (int i = 0; i < n; ++i) {         id[i] = i;         cnt[i] = 1ll * (i + 1) * (n - i) * a[i];       }       sort(id, id + n, cmp);       long long result = 0;       for (int i = 0; i < n; ++i) {         result = (result + cnt[id[i]] % MOD * b[i] % MOD) % MOD;       }       printf( %lld n , result);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   const int MOD = 998244353;   int n, q;   long long seg[4 * MAXN], mul[4 * MAXN], plu[4 * MAXN];   set<pair<int, int>> number[MAXN];   void shift(int l, int r, int id) {     int mid = l + r >> 1;     seg[id << 1] = (seg[id << 1] * mul[id]) % MOD;     seg[id << 1] = (seg[id << 1] + plu[id] * (mid - l)) % MOD;     seg[id << 1 | 1] = (seg[id << 1 | 1] * mul[id]) % MOD;     seg[id << 1 | 1] = (seg[id << 1 | 1] + plu[id] * (r - mid)) % MOD;     mul[id << 1] = (mul[id << 1] * mul[id]) % MOD;     plu[id << 1] = (plu[id << 1] * mul[id]) % MOD;     mul[id << 1 | 1] = (mul[id << 1 | 1] * mul[id]) % MOD;     plu[id << 1 | 1] = (plu[id << 1 | 1] * mul[id]) % MOD;     plu[id << 1] = (plu[id << 1] + plu[id]) % MOD;     plu[id << 1 | 1] = (plu[id << 1 | 1] + plu[id]) % MOD;     mul[id] = 1;     plu[id] = 0;   }   long long seg_get(int s, int e, int l = 0, int r = n, int id = 1) {     if (l >= s && r <= e) return seg[id];     if (l >= e || r <= s) return 0;     shift(l, r, id);     int mid = l + r >> 1;     return (seg_get(s, e, l, mid, id << 1) + seg_get(s, e, mid, r, id << 1 | 1)) %            MOD;   }   void seg_do(int s, int e, int l = 0, int r = n, int id = 1) {     if (l >= s && r <= e) {       seg[id] = (seg[id] + (r - l)) % MOD;       plu[id] = (plu[id] + 1) % MOD;       return;     }     if (l >= e || r <= s) return;     int mid = l + r >> 1;     shift(l, r, id);     seg_do(s, e, l, mid, id << 1);     seg_do(s, e, mid, r, id << 1 | 1);     seg[id] = (seg[id << 1] + seg[id << 1 | 1]) % MOD;     return;   }   void seg_mul(int s, int e, int l = 0, int r = n, int id = 1) {     if (s == e) return;     if (l >= s && r <= e) {       seg[id] = (seg[id] * 2) % MOD;       plu[id] = (plu[id] * 2) % MOD;       mul[id] = (mul[id] * 2) % MOD;       return;     }     if (l >= e || r <= s) return;     int mid = l + r >> 1;     shift(l, r, id);     seg_mul(s, e, l, mid, id << 1);     seg_mul(s, e, mid, r, id << 1 | 1);     seg[id] = (seg[id << 1] + seg[id << 1 | 1]) % MOD;     return;   }   void handle(int l, int r, int x) {     auto itl = number[x].upper_bound({l, -2});     itl--;     if ((*itl).second <= l) itl++;     auto itr = number[x].lower_bound({r, -2});     if ((*itl).first > l) {       seg_do(l, min(r, (*itl).first));     }     for (auto it = itl; it != itr; it++) {       int L = max(l, (*it).first);       int R = min(r, (*it).second);       seg_mul(L, R);       if ((*it).second >= r) continue;       auto it2 = it;       it2++;       L = (*it).second;       R = r;       R = min(R, (*it2).first);       seg_do(L, R);     }     l = min(l, (*itl).first);     itr--;     r = max(r, (*itr).second);     itr++;     number[x].erase(itl, itr);     number[x].insert({l, r});   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (int i = 0; i < 4 * MAXN; i++) mul[i] = 1;     for (int i = 0; i <= n; i++) {       number[i].insert({-1, -1});       number[i].insert({n, n});     }     while (q--) {       int tp;       cin >> tp;       if (tp == 1) {         int l, r, x;         cin >> l >> r >> x;         handle(l - 1, r, x);       } else {         int l, r;         cin >> l >> r;         cout << seg_get(l - 1, r) <<   n ;       }     }     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[2210], b[2210];   long long inf = 1e13;   int main() {     memset(a, 0, sizeof(a));     memset(b, 0, sizeof(b));     int n = 0, k = 0;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) {       scanf( %I64d , &a[i]);     }     for (int i = 1; i <= n; ++i) {       scanf( %I64d , &b[i]);     }     long long l = 0, r = inf, res = 0;     while (l <= r) {       long long mid = (l + r) >> 1;       priority_queue<long long, vector<long long>, greater<long long>> qa;       priority_queue<long long> qb;       long long sz = 0, cur = 0;       for (int i = 1; i <= n; ++i) {         qa.push(a[i]);         long long tmp1 = qa.top() + b[i] - mid;         long long tmp2 = qb.empty() ? inf : (b[i] - qb.top());         if (tmp1 <= tmp2 && tmp1 <= 0) {           cur += tmp1;           ++sz;           qb.push(b[i]);           qa.pop();         } else if (tmp2 < tmp1 && tmp2 < 0) {           cur += tmp2;           qb.pop();           qb.push(b[i]);         }       }       if (sz >= k) {         res = cur + (long long)k * mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     printf( %I64d , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const int iinf = INT_MAX;   const long long linf = 2e18;   const double dinf = 1e30;   const int MOD = 1000000007;   inline int read() {     int X = 0, w = 0;     char ch = 0;     while (!isdigit(ch)) {       w |= ch ==  - ;       ch = getchar();     }     while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();     return w ? -X : X;   }   void write(int x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   void print(int x) {     cout << x <<   n ;     exit(0);   }   void PRINT(string x) {     cout << x <<   n ;     exit(0);   }   void douout(double x) { printf( %lf n , x + 0.0000000001); }   long long ans = 1e18, a, b, mx = 1e18;   int main() {     cin >> a >> b;     for (long long i = 1; i * i <= (a + b); i++) {       if (a % i == 0) mx = min(mx, a / i);       if (b % i == 0) mx = min(mx, b / i);       if ((a + b) % i == 0 && (a + b) / i >= mx)         ans = min(ans, 2 * i + 2 * ((a + b) / i));     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, p;       cin >> n >> p;       int cnt = 0;       for (int i = 1; i < n; ++i) {         for (int j = i + 1; j <= n; ++j) {           if (cnt == 2 * n + p) break;           cout << i <<     << j <<   n ;           cnt++;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double getangle(pair<int, int> a, pair<int, int> b) {}   int main() {     int n;     cin >> n;     double arr[n + 1];     for (int i = 0; i < n; ++i) {       int x, y;       cin >> x >> y;       arr[i] = atan2(x, y);       if (arr[i] < 0) arr[i] += 2.0 * M_PI;       arr[i] *= 180.0 / M_PI;     }     sort(arr, arr + n);     double a = 0.0;     if (n <= 2)       a = min(360.0 - (arr[0] - arr[n - 1] + 360.0),               360.0 - (arr[n - 1] - arr[0]));     else {       a = 360.0 - (arr[0] - arr[n - 1] + 360.0);       for (int i = 0; i < n - 1; ++i) a = min(a, 360.0 - (arr[i + 1] - arr[i]));     }     printf( %.8lf n , a);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string aux;     cin >> aux;     int flag_Invalid_Input = 0;     int memo[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};     int counter = 0;     int len = aux.size();     int i, j;     for (i = 0; i < len; i++) {       if (aux[i] >  1 ) {         for (j = 0; j < len - i; j++) {           counter += memo[j];         }         break;       }       if (aux[i] ==  1 ) {         counter += memo[len - i - 1];       }     }     cout << counter;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 210010;   const int mod = 1e9 + 7;   int dp[110][maxn], sum[maxn];   int a, b, k, t;   int main() {     scanf( %d%d%d%d , &a, &b, &k, &t);     dp[0][0] = 1;     for (int i = 1; i <= t; i++) {       for (int j = 0; j < maxn; j++)         sum[j] = ((j > 0 ? sum[j - 1] : 0) + dp[i - 1][j]) % mod;       for (int j = 0; j < maxn; j++)         dp[i][j] =             (sum[j] - (j - 2 * k - 1 >= 0 ? sum[j - 2 * k - 1] : 0) + mod) % mod;     }     for (int i = 0; i < maxn; i++) {       sum[i] = ((i > 0 ? sum[i - 1] : 0) + dp[t][i]) % mod;     }     int ans = 0;     for (int i = 0; i <= 2 * k * t; i++) {       (ans += (1LL) * dp[t][i] * (i + a - b - 1 < 0 ? 0 : sum[i + a - b - 1]) %               mod) %= mod;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void dfs(long long n, long long i,            unordered_map<long long, vector<long long>> &adj, vector<bool> &vis,            vector<long long> &a1, vector<long long> &a2) {     if (vis[n]) return;     vis[n] = true;     if (i == 0)       a1.push_back(n);     else       a2.push_back(n);     for (auto &it : adj[n]) {       if (!vis[it]) dfs(it, i ^ 1, adj, vis, a1, a2);     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       unordered_map<long long, vector<long long>> adj;       vector<bool> vis(n + 1, false);       vector<long long> a1, a2;       for (long long i = 0; i < m; i++) {         long long x, y;         cin >> x >> y;         adj[x].push_back(y);         adj[y].push_back(x);       }       queue<pair<long long, long long>> q;       q.push({1, 0});       while (q.size() > 0) {         auto it = q.front();         q.pop();         long long x, y;         tie(x, y) = it;         if (vis[x]) continue;         vis[x] = true;         if (y == 0)           a1.push_back(x);         else           a2.push_back(x);         for (auto &it : adj[x]) {           if (!vis[it]) q.push({it, y ^ 1});         }       }       if (a1.size() <= a2.size()) {         cout << a1.size() <<   n ;         for (auto x : a1) cout << x <<    ;         cout << endl;         ;       } else {         cout << a2.size() <<   n ;         for (auto x : a2) cout << x <<    ;         cout << endl;         ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1);   struct Point {     long long x, y, r;     Point() {}     Point(long long a, long long b, long long c) {       x = a;       y = b;       r = c;     }   };   long double rad;   long double cor(long double angle) {     if (angle < 0) {       return angle + 2 * PI;     }     return angle;   }   pair<long double, long double> fnd(Point& p) {     long double kor = sqrt(p.x * p.x + p.y * p.y);     if (kor > rad + p.r) {       return {20, 20};     }     if (rad - p.r > kor) {       return {20, 20};     } else {       long double an1 =           acos((rad * rad + p.x * p.x + p.y * p.y - p.r * p.r) / (2 * rad * kor));       long double tmp = sqrt(p.x * p.x + p.y * p.y - p.r * p.r);       long double l = cor(atan2(p.y, p.x));       return {l - an1, l + an1};     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long double d;     long long n;     cin >> n;     cin >> d;     rad = 0;     vector<Point> sp(n);     long long x, y, r;     for (long long i = 0; i < n; ++i) {       cin >> x >> y >> r;       sp[i].x = x;       sp[i].y = y;       sp[i].r = r;     }     vector<pair<long double, long long> > al(0);     for (long long kol = 0; kol < 2850; ++kol) {       rad += d;       for (long long i = 0; i < n; ++i) {         pair<long double, long double> now = fnd(sp[i]);         if (now.first == 20) {           continue;         } else if (now.first == 30) {           continue;         } else if (now.first < 0 && now.second > 0) {           al.push_back({cor(now.first), 0});           al.push_back({2 * PI, 1});           al.push_back({0, 0});           al.push_back({now.second, 1});         } else if (now.second > 2 * PI) {           al.push_back({now.first, 0});           al.push_back({2 * PI, 1});           al.push_back({0, 0});           al.push_back({now.second - 2 * PI, 1});         } else {           al.push_back({now.first, 0});           al.push_back({now.second, 1});         }       }     }     sort(al.begin(), al.end());     long long now = 0;     long long mx = 0;     for (auto p : al) {       if (p.second == 0) {         ++now;       } else {         --now;       }       mx = max(mx, now);     }     cout << mx << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long n, m, k, x, i, sum, j, r = 1, B[20][20];   char A[20][20];   int main() {     cin >> n >> m;     for (i = 0; i < n; i++) {       cin >> x;       sum += x;     }     if (sum == 0)       cout << 0;     else {       if (abs(sum) <= m)         cout <<  1 ;       else         cout << (abs(sum) / m) + (abs(sum) % m != 0);     }     return 0;   }


#include <bits/stdc++.h>   template <class T>   int gmax(T &a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int gmin(T &a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(const char *s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   const int MAXN = 2e5 + 5;   int X[MAXN];   int solve(int x, int y) {     if (x > y)       return y;     else if (x == y)       return 0;     else       return y - 1;   }   int has[MAXN];   int main() {     int n, m;     while (~scanf( %d %d , &n, &m)) {       memset(has, 0, sizeof(has));       map<int, long long> m1;       map<int, long long> m2;       map<int, int> m3;       long long tmp = 0;       for (int i = 0; i < m; ++i) {         scanf( %d , &X[i]);       }       for (int i = 1; i < m; ++i) {         if (X[i] == X[i - 1]) continue;         tmp += abs(X[i] - X[i - 1]);         m1[X[i]] += abs(X[i] - X[i - 1]);         m1[X[i - 1]] += abs(X[i] - X[i - 1]);         m2[X[i]] += solve(X[i], X[i - 1]);         m2[X[i - 1]] += solve(X[i - 1], X[i]);         m3[X[i]]++;         m3[X[i - 1]]++;         int t1 = X[i];         int t2 = X[i - 1];         if (t1 > t2) swap(t1, t2);         has[t1]++;         has[t2 + 1]--;       }       for (int i = 1; i <= n; ++i) has[i] += has[i - 1];       vector<long long> result;       result.push_back(tmp);       for (int i = 2; i <= n; ++i) {         long long tmp2 = tmp - m1[i];         result.push_back(tmp2 + m2[i] - (has[i] - m3[i]));       }       for (int i = 0; i < n; ++i) {         if (i) printf(   );         printf( %lld , result[i]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2005;   struct Point {     long long x, y;     Point() {}     Point(long long x, long long y) : x(x), y(y) {}     bool operator<(const Point& rhs) const {       if (y != rhs.y)         return y < rhs.y;       else         return x < rhs.x;     }   };   Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }   Point operator*(Point v, long long mul) { return Point(v.x * mul, v.y * mul); }   long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }   int n;   Point p[MAXN];   inline long long C(long long x) { return x * (x - 1) / 2; }   long long calc(Point o, vector<pair<Point, int> >& v) {     sort(v.begin(), v.end(),          [&o](const pair<Point, int>& a, const pair<Point, int>& b) -> bool {            return cross(a.first - o, b.first - o) < 0;          });     long long cnt[2] = {0}, ret = 0;     for (auto& i : v) cnt[i.second]++;     for (auto& i : v) {       cnt[i.second]--;       ret += C(cnt[0]) * C(cnt[1]);       cnt[i.second ^ 1]++;     }     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lld%lld , &p[i].x, &p[i].y);     long long ans = 0;     for (int i = 0; i < n; i++) {       vector<pair<Point, int> > v;       for (int j = 0; j < n; j++)         if (j != i) {           if (p[i] < p[j])             v.emplace_back(p[j], 1);           else             v.emplace_back(p[i] * 2 - p[j], 0);         }       ans += calc(p[i], v);     }     ans /= 2;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using VI = vector<int>;   const int NN = 200011;   int a[NN], b[NN], arr[NN];   VI vec[NN], cyc[NN];   int nc;   int dp[NN], vst[NN];   int n, s;   void dfs(int u) {     while (not vec[u].empty()) {       int v = vec[u].back();       vec[u].pop_back();       dfs(a[v]);       cyc[nc].push_back(v);     }   }   int solve() {     cin >> n >> s;     for (int i = 1; i <= n; i++) scanf( %d , a + i), arr[i] = a[i], dp[i] = i;     sort(arr + 1, arr + n + 1);     int m = unique(arr + 1, arr + n + 1) - arr - 1;     for (int i = 1; i <= n; i++) {       b[i] = a[i] = lower_bound(arr + 1, arr + m + 1, a[i]) - arr;     }     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; i++)       if (a[i] ^ b[i]) {         vec[b[i]].push_back(i);       }     for (int i = 1; i <= m; i++) {       if (not vec[i].empty()) {         dfs(i);         int r = ((int)cyc[nc].size());         for (int j = 0; j < r; j++) {           dp[cyc[nc][(j + 1) % r]] = cyc[nc][j];           s--;         }         nc++;       }       if (s < 0) return puts( -1 );     }     s = min(s, nc);     if (s >= 2) {       cout << nc - s + 2 << endl;       cout << s << endl;       for (int i = 0; i < s; i++) printf( %d  , cyc[i][0]);       puts(  );       int tmp = dp[cyc[s - 1][0]];       for (int i = s; --i;) {         dp[cyc[i][0]] = dp[cyc[i - 1][0]];       }       dp[cyc[0][0]] = tmp;     } else       cout << nc << endl;     for (int i = 1; i <= n; i++) {       if (dp[i] == i or vst[i]) continue;       VI ans;       for (int u = i; not vst[u];) {         ans.push_back(u);         vst[u] = 1;         u = dp[u];       }       printf( %d n , ans.size());       for (int u : ans) printf( %d  , u);       puts(  );     }   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   long long int n, i, j, t, ans, arr[3009], crr[3009][3009];   int main() {     scanf( %lld , &t);     while (t--) {       scanf( %lld , &n);       ans = 0;       for (i = 1; i <= n; i++) scanf( %lld , &arr[i]);       for (i = n; i >= 1; i--) {         for (j = i - 1; j >= 1; j--) {           ans += crr[arr[i]][arr[j]];         }         for (j = i + 1; j <= n; j++) {           crr[arr[j]][arr[i]]++;         }       }       printf( %lld n , ans);       for (i = 1; i <= n; i++) {         for (j = 1; j <= n; j++) crr[i][j] = 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, b, one, two;     string a;     one = 0;     two = 0;     cin >> n >> a;     for (b = 0; b < n; b++) {       if (a[b] ==  1 ) {         one++;       } else {         two++;       }     }     if (one > two) {       cout << one - two;     } else {       cout << two - one;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int a[2000000];   int dem;   int maxa;   int dp[10000001];   void mmax(int &a, int b) { a = max(a, b); }   bool ok(int len) {     for (int i = 0; i <= maxa; i++) dp[i] = 0;     dp[len] = 1;     for (int i = len + 1; i <= maxa; i++) {       mmax(dp[i], dp[i - 1]);       mmax(dp[i], dp[i >> 1] + dp[(i + 1) >> 1]);     }     long long sum = 0;     for (int i = 1; i <= n; i++) sum += dp[a[i]];     return sum >= k;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i], maxa = max(maxa, a[i]);     int d = 1, c = 10000000, g, fin = -1;     while (d <= c) {       g = (d + c) / 2;       if (ok(g)) {         fin = g;         d = g + 1;       } else         c = g - 1;     }     cout << fin;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, long long> mp1, mp2;   long long n, x, yy, c;   vector<pair<int, int>> v;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x >> yy;       v.push_back({x, yy});       mp1[x + yy]++;       mp2[x - yy]++;     }     for (int i = 0; i < n; i++) {       x = v[i].first;       yy = v[i].second;       c += mp1[x + yy];       c += mp2[x - yy];       c -= 2;     }     cout << c / 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200010;   int nowarn;   int n, m;   namespace Tree {   int head[MAXN], nxt[MAXN], to[MAXN], eidx;   void init() {     eidx = 0;     memset(head, -1, sizeof(head));   }   void adde(int u, int v) { to[eidx] = v, nxt[eidx] = head[u], head[u] = eidx++; }   }  // namespace Tree   namespace DFN {   int dfn[MAXN], dfn_cnt, son[MAXN], dep[MAXN], top[MAXN], size[MAXN], pa[MAXN];   void dfs1(int u, int fa, int d) {     using namespace Tree;     pa[u] = fa, dep[u] = d, son[u] = 0, size[u] = 1;     for (int i = head[u]; ~i; i = nxt[i]) {       int v = to[i];       dfs1(v, u, d + 1);       size[u] += size[v];       if (size[v] > size[son[u]]) son[u] = v;     }   }   void dfs2(int u, int tp) {     using namespace Tree;     top[u] = tp, dfn[u] = ++dfn_cnt;     if (son[u]) dfs2(son[u], tp);     for (int i = head[u]; ~i; i = nxt[i]) {       int v = to[i];       if (v == son[u]) continue;       dfs2(v, v);     }   }   void prelude() {     size[0] = dfn_cnt = 0;     dfs1(1, 0, 1);     dfs2(1, 1);   }   int lca(int u, int v) {     while (top[u] != top[v]) {       if (dep[top[u]] < dep[top[v]]) swap(u, v);       u = pa[top[u]];     }     return min(u, v);   }   }  // namespace DFN   int lowbit(int x) { return x & (-x); }   struct BIT {     long long c[MAXN];     void init() { memset(c, 0, sizeof(c)); }     void add(int p, long long v) {       for (; p <= n; p += lowbit(p)) c[p] += v;     }     long long sum(int p) const {       long long s = 0;       for (; p; p -= lowbit(p)) s += c[p];       return s;     }     long long sum(int l, int r) const { return sum(r) - sum(l - 1); }   };   struct Triple {     int first, second, val;     Triple() {}     Triple(int a, int b, int c) : first(a), second(b), val(c) {}   };   vector<Triple> chain[MAXN];   BIT sf, sg;   long long query(int u, int v, const BIT &bit) {     using namespace DFN;     long long s = 0;     while (top[u] != top[v]) {       s += bit.sum(dfn[top[v]], dfn[v]);       v = pa[top[v]];     }     s += bit.sum(dfn[u], dfn[v]);     return s;   }   long long f[MAXN], g[MAXN];   void dp(int u) {     using namespace Tree;     using namespace DFN;     g[u] = 0;     for (int i = head[u]; ~i; i = nxt[i]) dp(to[i]), g[u] += f[to[i]];     sg.add(dfn[u], g[u]);     f[u] = g[u];     for (int i = 0; i < (int)chain[u].size(); ++i) {       Triple tp = chain[u][i];       int s = tp.first, t = tp.second, w = tp.val;       if (t == u) swap(s, t);       if (s == u) {         f[u] = max(f[u], query(u, t, sg) - query(u, t, sf) + w);       } else {         f[u] = max(f[u], query(u, s, sg) + query(u, t, sg) - g[u] -                              query(u, s, sf) - query(u, t, sf) + w);       }     }     sf.add(dfn[u], f[u]);   }   int main() {     nowarn = scanf( %d%d , &n, &m);     Tree::init();     for (int i = 2; i <= n; ++i) {       int p;       nowarn = scanf( %d , &p);       Tree::adde(p, i);     }     DFN::prelude();     while (m--) {       int u, v, w;       nowarn = scanf( %d%d%d , &u, &v, &w);       int lca = DFN::lca(u, v);       chain[lca].push_back(Triple(u, v, w));     }     sf.init(), sg.init();     dp(1);     printf( %lld n , f[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s = 1, i;     cin >> n;     int a[n];     for (i = 0; i < n; i++) {       cin >> a[i];       if (a[i] == s) {         s++;       }     }     cout << s - 1 << endl;     s = 1;     for (i = 0; i < n; i++) {       if (a[i] == s) {         cout << i + 2001 <<    ;         s++;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     long long broj = 3;     int i;     for (i = 0; i < 3 * n - 1; i++) {       broj *= 3;       broj %= 1000000007;     }     long long broj1 = 7;     for (i = 0; i < n - 1; i++) {       broj1 *= 7;       broj1 %= 1000000007;     }     cout << (broj - broj1 + 1000000007) % 1000000007 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int a[N], cnt[N];   int f[N], bj[N];   vector<int> d[N];   int n, mx;   long long ans;   void init(int n) {     for (int i = 1; i <= n; i++) f[i] = 1;     for (int i = 2; i <= n; i++) {       if (bj[i]) continue;       f[i] = -1;       for (int j = i * 2; j <= n; j += i) {         bj[j] = 1;         f[j] = (j % (i * i)) ? f[j] * -1 : 0;       }     }   }   int count(int x) {     int res = 0;     for (int i = 0; i < d[x].size(); i++) {       res += f[d[x][i]] * cnt[d[x][i]];     }     return res;   }   void add(int x, int v) {     for (int i = 0; i < d[x].size(); i++) {       cnt[d[x][i]] += v;     }   }   int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       a[x]++;       mx = max(mx, x);     }     ans = mx;     for (int i = 1; i <= mx; i++) {       for (int j = i; j <= mx; j += i) {         d[j].push_back(i);       }     }     init(mx);     for (int g = 1; g <= mx; g++) {       stack<int> st;       for (int i = mx / g; i >= 1; i--) {         if (!a[i * g]) continue;         int c = count(i);         while (c) {           if (gcd(i, st.top()) == 1) {             ans = max(ans, (long long)i * g * st.top());             c--;           }           add(st.top(), -1);           st.pop();         }         st.push(i);         add(i, 1);       }       while (!st.empty()) {         add(st.top(), -1);         st.pop();       }     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005, mod = 1e9 + 7;   int yh[N][N], a[N];   long long kpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % mod;       a = a * a % mod;       b >>= 1;     }     return ans;   }   int main() {     for (int i = (0); i < (N); ++i) yh[i][0] = 1;     for (int i = (1); i < (N); ++i)       for (int j = (1); j < (N); ++j) {         yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j];         if (yh[i][j] >= mod) yh[i][j] -= mod;       }     int n, m;     cin >> n >> m;     a[0] = 0;     a[m + 1] = n + 1;     for (int i = (1); i < (m + 1); ++i) cin >> a[i];     sort(a + 1, a + m + 1);     long long ans = 1, k = n - m;     for (int i = (1); i < (m + 2); ++i) {       int c = a[i] - a[i - 1] - 1;       ans = ans * yh[k][c] % mod;       if (i != 1 && i != m + 1 && c) ans = ans * kpow(2, c - 1) % mod;       k -= c;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[81][81][81], n, m, i, j, k, ans, inf = 1000007, x, y, l, len;   pair<int, int> p;   vector<pair<int, int> > g[82];   int main() {     cin >> n >> m;     if (m > n) {       cout << -1 << endl;       return 0;     }     cin >> k;     for (i = 0; i < k; i++) {       cin >> x >> y >> l;       if (x == y) continue;       p.first = y;       p.second = l;       g[x].push_back(p);     }     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++)         for (k = 1; k <= n; k++) dp[i][j][k] = inf;     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++) dp[i][j][1] = 0;     for (k = 2; k <= m; k++)       for (j = 1; j < n; j++)         for (i = 1; i <= n - j; i++) {           len = g[i].size();           for (l = 0; l < len; l++) {             if (g[i][l].first < i) continue;             if (g[i][l].first > i + j) continue;             dp[i][i + j][k] =                 min(dp[i][i + j][k], min(dp[g[i][l].first][i + 1][k - 1],                                          dp[g[i][l].first][i + j][k - 1]) +                                          g[i][l].second);           }           len = g[i + j].size();           for (l = 0; l < len; l++) {             if (g[i + j][l].first > i + j) continue;             if (g[i + j][l].first < i) continue;             dp[i + j][i][k] =                 min(dp[i + j][i][k], min(dp[g[i + j][l].first][i + j - 1][k - 1],                                          dp[g[i + j][l].first][i][k - 1]) +                                          g[i + j][l].second);           }         }     ans = inf;     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++) ans = min(ans, dp[i][j][m]);     if (ans == inf)       cout << -1 << endl;     else       cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   int n;   long long arr[MAXN][3];   map<pair<long long, long long>, pair<long long, int> > height;   void update(long long x, long long y, long long z, int id) {     if (height.find(make_pair((x), (y))) == height.end())       height[make_pair((x), (y))] = make_pair((z), (id));     else       height[make_pair((x), (y))] =           max(height[make_pair((x), (y))], make_pair((z), (id)));   }   int main() {     cin >> n;     int idx1 = 0, idx2[2] = {0, 0};     long long best1 = 0, best2 = 0;     for (int i = 1; i <= n; i++) {       cin >> arr[i][0] >> arr[i][1] >> arr[i][2];       sort(arr[i], arr[i] + 3);       if (arr[i][0] > best1) {         best1 = arr[i][0];         idx1 = i;       }       if (height.find(make_pair((arr[i][1]), (arr[i][2]))) != height.end()) {         long long t =             min(arr[i][0] + height[make_pair((arr[i][1]), (arr[i][2]))].first,                 min(arr[i][1], arr[i][2]));         if (t > best2) {           idx2[0] = i;           idx2[1] = height[make_pair((arr[i][1]), (arr[i][2]))].second;           best2 = t;         }       }       update(arr[i][0], arr[i][1], arr[i][2], i);       update(arr[i][0], arr[i][2], arr[i][1], i);       update(arr[i][1], arr[i][2], arr[i][0], i);     }     if (best1 > best2)       cout << 1 << endl << idx1 << endl;     else       cout << 2 << endl << idx2[0] <<     << idx2[1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int len;   int main() {     cin >> len;     cin >> s;     int sum = 0;     cout << s[0];     for (int i = 1;; i++) {       sum += i;       if (sum >= len) break;       cout << s[sum];     }   }


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