#include <bits/stdc++.h>   using namespace std;   long long sqr(long long a) { return a * a; }   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<long long> sumx(n, 0ll);     vector<long long> sumy(m, 0ll);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         long long c;         cin >> c;         sumx[i] += c;         sumy[j] += c;       }     }     vector<long long> ansx(n + 1, 0ll);     vector<long long> ansy(m + 1, 0ll);     for (int i = 0; i <= n; i++) {       for (int j = 0; j < n; j++) {         ansx[i] += sumx[j] * sqr((2 * j + 1) - 2 * i);       }     }     for (int i = 0; i <= m; i++) {       for (int j = 0; j < m; j++) {         ansy[i] += sumy[j] * sqr((2 * j + 1) - 2 * i);       }     }     pair<long long, pair<int, int> > res =         make_pair((long long)1e18, make_pair(-1, -1));     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         res = min(res, make_pair(ansx[i] + ansy[j], make_pair(i, j)));       }     }     cout << 4 * res.first << endl;     cout << res.second.first <<     << res.second.second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, ans;   bool pd(int n) {     for (int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return false;     return true;   }   int main() {     scanf( %d , &n);     if (pd(n))       puts( 1 );     else if (n % 2 == 0 || pd(n - 2))       puts( 2 );     else       puts( 3 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> ans, adj[200007];   bool vis[200007];   int inputorder[200007], relorder[200007];   bool cmp(int a, int b) { return relorder[a] < relorder[b]; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, x, a, b, i, j;     cin >> n;     for (i = 0; i < n - 1; i++) {       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     for (i = 0; i < n; i++) {       cin >> inputorder[i];       relorder[inputorder[i]] = i;     }     for (i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end(), cmp);     queue<int> q;     q.push(1);     memset(vis, false, sizeof(vis));     while (!(q.empty())) {       queue<int> temp;       while (!(q.empty())) {         int x = q.front();         q.pop();         ans.push_back(x);         vis[x] = true;         for (j = 0; j < adj[x].size(); j++)           if (vis[adj[x][j]] == false) temp.push(adj[x][j]);       }       q = temp;     }     for (i = 0; i < n; i++)       if (inputorder[i] != ans[i]) {         cout <<  No ;         return 0;       }     cout <<  Yes ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, sz[N], sum[N];   int64_t r[N][3], ans;   vector<pair<int, int>> g[N];   void dfs(int x, int f) {     sz[x] = 1;     for (auto &e : g[x]) {       int y = e.first, w = e.second;       if (y == f) continue;       dfs(y, x), sz[x] += sz[y];       if (w) sum[y] = sz[y], r[y][2] += r[y][0] + r[y][1], r[y][0] = r[y][1] = 0;       r[x][0] += r[y][0];       r[x][1] += r[y][1];       r[x][2] += r[y][2];       sum[x] += sum[y];     }     for (auto &e : g[x]) {       int y = e.first, w = e.second;       if (y == f) continue;       ans += sum[y] * (int64_t)(sz[x] - sz[y]) * (sz[x] - sz[y] - 1);       ans +=           (sz[y] - sum[y]) * (int64_t)(sum[x] - sum[y]) * (sum[x] - sum[y] - 1);       ans += 2 * r[y][0] * (sum[x] - sum[y]);       ans += 2 * (r[y][1] + r[y][2]) * (sz[x] - sz[y]);       r[x][0] += (sz[y] - sum[y]) * (int64_t)(sum[x] - sum[y]);       r[x][1] += sum[y] * (int64_t)(sz[x] - sz[y]);     }     r[x][0] += sum[x];     ans += sum[x] * (sum[x] - 1ll);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       int ok = 1;       while (w) {         if (w % 10 != 7 && w % 10 != 4) ok = 0;         w /= 10;       }       g[u].push_back({v, ok});       g[v].push_back({u, ok});     }     dfs(1, 0);     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, num = 0, dem = 0;     cin >> n;     if (n % 2 == 0) {       num = (n / 2) - 1;       dem = (n / 2) + 1;     } else {       num = n / 2;       dem = n - num;     }     set<int> x, y;     while (1) {       int flag1 = -1;       x.clear();       y.clear();       if (num == 2) x.insert(num);       for (int i = 2; i * i <= num; i++) {         if (num % i == 0) {           x.insert(i);           x.insert(num / i);         }       }       for (int i = 2; i * i <= dem; i++) {         if (dem % i == 0) {           y.insert(i);           y.insert(dem / i);         }       }       for (auto i : x) {         if (y.count(i) == 1) {           flag1 = 1;           break;         }       }       if (flag1 == -1) break;       num--;       dem++;     }     return cout << num <<     << dem <<   n , 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> g[200010];   int vis[200010], d[200010];   stack<int> his;   void dfs(int u, int dd) {     his.push(u);     d[u] = dd;     for (int v : g[u]) {       if (!d[v])         dfs(v, dd + 1);       else if (d[u] - d[v] + 1 >= ceil(sqrt(n))) {         puts( 2 );         printf( %d n , d[u] - d[v] + 1);         for (int i = d[v] - 1; i < d[u]; ++i) {           printf( %d  , his.top());           his.pop();         }         exit(0);       }     }     if (!vis[u])       for (int v : g[u]) vis[v] = 1;     his.pop();   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0, u, v; i < m; ++i) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 1);     puts( 1 );     int visT = 0;     for (int i = 1; visT < ceil(sqrt(n)); ++i) {       if (!vis[i]) printf( %d  , i), ++visT;     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const double EPS = 1e-7;   const int INF = 1000000000;   char buf[100];   int maxl[2000000];   int maxr[2000000];   int minl[2000000];   int minr[2000000];   pair<int, int> g[2000000];   int n;   int X = 0;   void dfs(int, int);   void add_dfs(int x, int mm) {     if (X > mm) return;     if (g[x].second == -1) {       g[x].second = X;       X++;       dfs(g[x].second, mm);     }     add_dfs(g[x].second, mm);   }   bool OK = true;   void dfs(int x, int mm) {     if (X > minl[x]) OK = false;     if (X <= maxl[x]) {       g[x].first = X;       X++;       dfs(g[x].first, maxl[x]);     }     if (X > minr[x]) OK = false;     if (X <= maxr[x]) {       g[x].second = X;       X++;       dfs(g[x].second, maxr[x]);     }     add_dfs(x, mm);   }   vector<int> res;   void dfs2(int x) {     if (g[x].first != -1) dfs2(g[x].first);     res.push_back(x);     if (g[x].second != -1) dfs2(g[x].second);   }   int main() {     int m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       maxl[i] = 0;       maxr[i] = 0;       minl[i] = n;       minr[i] = n;       g[i] = make_pair(-1, -1);     }     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       scanf( %s , buf);       if (buf[0] ==  L ) {         maxl[x] = max(maxl[x], y);         minl[x] = min(minl[x], y);       } else {         maxr[x] = max(maxr[x], y);         minr[x] = min(minr[x], y);       }     }     X = 1;     dfs(0, n - 1);     dfs2(0);     if (!OK)       printf( IMPOSSIBLE n );     else {       for (int i = 0; i < res.size(); i++) {         printf( %d  , res[i] + 1);       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long max(long long a, long long b) { return ((a > b) ? a : b); }   inline long long min(long long a, long long b) { return ((a > b) ? b : a); }   inline long long gcd(long long a, long long b) {     if (b == 0) return a;     a %= b;     return gcd(b, a);   }   long long power(long long x, long long y) {     if (y == 0) return 1;     long long a = power(x, y / 2);     if (y % 2 == 0)       return a * a;     else       return x * a * a;   }   long long powermod(long long x, long long ex, long long md) {     long long ans = 1ll;     while (ex > 0) {       if (ex & 1ll) ans = (ans * x) % md;       ex >>= 1ll;       x = (x * x) % md;     }     return ans;   }   const long long inf = 1e18 + 9;   const long long mod = 1e9 + 7;   const long double PI = acos(-1);   const long double eps = 1e-9;   const long long N = 2e5 + 11;   long long n, t, easy, hard;   long long a[N], b[N], pfx[N], cnteasy[N], cnthard[N];   vector<pair<long long, long long> > v;   void solve() {     cin >> n >> t >> easy >> hard;     for (int i = 0; i < n; i++) cin >> b[i + 1];     for (int i = 0; i < n; i++) cin >> a[i + 1];     for (int i = 0; i < n; i++) v.push_back(make_pair(a[i + 1], b[i + 1]));     sort((v).begin(), (v).end());     for (int i = 0; i < n; i++) {       if (v[i].second)         pfx[i + 1] = pfx[i] + hard;       else         pfx[i + 1] = pfx[i] + easy;       cnthard[i + 1] = cnthard[i] + (v[i].second);       cnteasy[i + 1] = cnteasy[i] + (!v[i].second);     }     if (pfx[n] <= t) {       cout << n <<   n ;     } else {       long long ans = 0;       for (long long i = 0; i < n; i++) {         long long t = v[i].first - 1;         if (t < 0) continue;         long long val = i;         long long tym = pfx[i];         long long rem = t - tym;         if (rem >= 0) {           long long x = min(rem / easy, cnteasy[n] - cnteasy[i]);           rem -= (x * easy);           val += x;           val += min(rem / hard, cnthard[n] - cnthard[i]);           ans = max(ans, val);         }       }       cout << ans <<   n ;     }     for (int i = 0; i < n + 1; i++)       a[i] = b[i] = pfx[i] = cnteasy[i] = cnthard[i] = 0;     v.clear();     n = t = easy = hard = 0;   }   int main() {     {       ios::sync_with_stdio(false);       cin.tie(0);       cout.tie(0);     };     cout << fixed << setprecision(20);     long long NTC = 1;     cin >> NTC;     long long PTC = 0;     while (PTC++ < NTC) {       solve();       cout <<   n ;     }     cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int x = 0, y = 0, dp = 0, tmp;     int sum = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  U ) {         y++;         if (x < y) {           if (dp == 2) sum++;           dp = 1;         }       }       if (s[i] ==  R ) {         x++;         if (x > y) {           if (dp == 1) sum++;           dp = 2;         }       }     }     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.1415926536;   const long long INF = 1e18;   const int32_t M = 1e6 + 7;   const int32_t MM = 998244353;   long long a, b, c, d;   void solve() {     cin >> a >> b >> c >> d;     double p, q, x;     p = (double)a / b;     q = (double)c / d;     x = (1 - p) * (1 - q);     x = 1 - x;     cout << fixed << setprecision(12);     cout << p / x;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> ans;   int n, r, fr[101][101];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       scanf( %d , &r);       for (int x, j = 0; j < r; j++) {         scanf( %d , &x);         fr[i][x]++;       }     }     for (int i = 1; i <= 100; i++) {       int c = 0;       for (int j = 0; j <= 100; j++) {         c += fr[j][i];       }       if (c == n) {         ans.push_back(i);       }     }     for (int i = 0; i < ans.size(); i++) {       printf( %d , ans[i]);       if (i != ans.size() - 1) putchar(   );     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 5;   const int mod = 1e9 + 7;   int a[N], f[N];   int main() {     int n;     int p = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (a[i] == 0) f[p++] = i;     }     int v = 0;     for (int i = 0; i < n; i++) {       if (a[i] != 0) {         a[i] = abs(f[v] - i);         if (v + 1 < p) a[i] = min(a[i], abs(f[v + 1] - i));         if (v - 1 >= 0) a[i] = min(a[i], abs(f[v - 1] - i));       } else         v++;     }     printf( %d , a[0]);     for (int i = 1; i < n; i++) {       printf(  %d , a[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dist[605][605], u, v, a[205], n, Vv, E, m, fix[605], match[605], w, ans, l,       r, k;   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       q;   vector<pair<int, int> > V[605];   vector<pair<pair<int, int>, int> > ed;   void dijkstra(int cur) {     for (int i = 1; i <= Vv; i++) dist[cur][i] = 1e7;     q.push({0, cur});     dist[cur][cur] = 0;     while (q.size()) {       u = q.top().second;       int d = q.top().first;       q.pop();       if (d > dist[cur][u]) continue;       for (int i = 0; i < V[u].size(); i++) {         v = V[u][i].first;         if (dist[cur][v] > dist[cur][u] + V[u][i].second) {           dist[cur][v] = dist[cur][u] + V[u][i].second;           q.push({dist[cur][v], v});         }       }     }   }   bool dfs(int u, int T) {     for (int v = 1; v <= Vv; v++) {       if (!fix[v] && dist[u][v] <= T) {         fix[v] = 1;         if (match[v] == -1 || dfs(match[v], T)) {           match[v] = u;           return true;         }       }     }     return false;   }   bool check(int T) {     int pas = 0;     for (k = 1; k <= Vv; k++) {       match[k] = -1;     }     for (k = 1; k <= n; k++) {       for (int i = 1; i <= Vv; i++) fix[i] = 0;       if (dfs(a[k], T)) pas++;     }     if (pas >= m) return true;     return false;   }   int main() {     cin >> Vv >> E >> n >> m;     for (k = 1; k <= n; k++) {       cin >> a[k];     }     for (k = 1; k <= E; k++) {       cin >> u >> v >> w;       ed.push_back({{u, v}, w});       V[u].push_back({v, w});       V[v].push_back({u, w});     }     for (k = 1; k <= n; k++) {       dijkstra(a[k]);     }     l = 0;     r = 1731311;     ans = -1;     while (l <= r) {       int mid = (l + r) / 2;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else         l = mid + 1;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[200005], pos[200005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     ;     long long n;     cin >> n;     cout << n <<    ;     if (n == 1) {       cout <<  1 ;       return 0;     }     if (n % 2) {       long long tmp = 1;       while (tmp < n) {         cout << tmp <<    ;         tmp += 2;       }       cout << n <<    ;       while (tmp > 1) {         cout << tmp - 2 <<    ;         tmp -= 2;       }       tmp = 2;       while (tmp < n - 1) {         cout << tmp <<    ;         tmp += 2;       }       cout << n - 1 <<     << n - 1 <<    ;       while (tmp > 2) {         cout << tmp - 2 <<    ;         tmp -= 2;       }     } else {       long long tmp = 2;       while (tmp < n) {         cout << tmp <<    ;         tmp += 2;       }       cout << n <<    ;       while (tmp > 2) {         cout << tmp - 2 <<    ;         tmp -= 2;       }       tmp = 1;       while (tmp < n - 1) {         cout << tmp <<    ;         tmp += 2;       }       cout << n - 1 <<     << n - 1 <<    ;       while (tmp > 1) {         cout << tmp - 2 <<    ;         tmp -= 2;       }     }     return 0;   }


#include <bits/stdc++.h>   int main(void) {     int n;     int a[1100];     while (scanf( %d , &n) == 1) {       for (int i = 0; i < n; i++) scanf( %d , a + i);       int cnt = 1, ans = 0;       int j = 0;       if (n <= 2) {         printf( %d n , ans);         break;       }       if (a[0] > a[1])         j = -1;       else if (a[0] == a[1])         j = 0;       else         j = 1;       while (cnt < n - 1) {         if (j == -1) {           for (; cnt < n - 1 && a[cnt] > a[cnt + 1]; cnt++)             ;           if (cnt == n - 1) break;           if (a[cnt] > a[cnt + 1])             j = -1;           else if (a[cnt] == a[cnt + 1])             j = 0;           else {             ans++;             j = 1;           }         }         if (j == 0) {           for (; cnt < n - 1 && a[cnt] == a[cnt + 1]; cnt++)             ;           if (cnt == n - 1) break;           if (a[cnt] > a[cnt + 1])             j = -1;           else if (a[cnt] == a[cnt + 1])             j = 0;           else             j = 1;         }         if (j == 1) {           for (; cnt < n - 1 && a[cnt] < a[cnt + 1]; cnt++)             ;           if (cnt == n - 1) break;           if (a[cnt] > a[cnt + 1]) {             ans++;             j = -1;           } else if (a[cnt] == a[cnt + 1])             j = 0;           else             j = 1;         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<set<int> > g(n + 1);     map<int, int> co;     for (int i = 0; i < m; i++) {       int n1, n2;       cin >> n1 >> n2;       g[n1].insert(n2);       g[n2].insert(n1);       co[n1]++;       co[n2]++;     }     int minn = 1000000000;     for (int i = 1; i <= n; i++) {       set<int>::iterator iter;       for (iter = g[i].begin(); iter != g[i].end(); iter++) {         set<int>::iterator iter2;         for (iter2 = g[*iter].begin(); iter2 != g[*iter].end(); iter2++) {           if (*iter2 == i) continue;           if (g[*iter2].count(i)) {             minn = min(minn, co[*iter] + co[*iter2] + co[i] - 6);           }         }       }     }     if (minn == 1000000000)       cout << -1 << endl;     else       cout << minn << endl;     return 0;   }


// REMEMBER USING MOD INVERSE WHILE DOING COMBINATORICS   // REMEMBER using int32_t while typecasting size into int      #include<bits/stdc++.h>   using namespace std;   #define pb push_back   #define int long long   #define all(x) x.begin(),x.end()   #define nl    n    #define ok  cout<< OK n    #define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)   #define free freopen( input.txt , r ,stdin);freopen( output.txt , w ,stdout)   #define ff first   #define ss second   typedef pair<int,int> pii;   const long long INF = 1ll << 32;   const long long MAX_N = 3e6 + 5;   const long long MOD = 1e9 + 7;      const long long N = 5001;      int I;      void solve()   {       int n;       cin>>n;       int even = 0 , odd = 0;       for(int i = 0 ; i<2 * n ; i++) {           int p;           cin>>p;           if(p & 1) odd++;           else even++;       }       cout<<(even == odd ?  Yes  :  No )<<nl;      }      int32_t main()   {       ios;       int Test = 1;       cin>>Test;       for(I = 1 ; I<=Test ;  I++)       {           solve();       }               }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const double PI = acos(-1.);   const double eps = 1e-10;   const int INF = 0x3f3f3f3f;   const long long INFll = 0x3f3f3f3f3f3f3f3f;   const int MAXN = 1e6 + 5;   const int MX = 123456;   const int inf = 0x3f3f3f3f;   const int MXE = 1e7;   struct MinCost_MaxFlow {     struct Edge {       int v, w, nxt;       int cost;     } E[MXE];     int head[MX], tot, level[MX], pre[MX], d[MX];     bool vis[MX];     void init() {       memset(head, -1, sizeof(head));       tot = 0;     }     void add(int u, int v, int w, int cost) {       E[tot].v = v;       E[tot].w = w;       E[tot].cost = cost;       E[tot].nxt = head[u];       head[u] = tot++;       E[tot].v = u;       E[tot].w = 0;       E[tot].cost = -cost;       E[tot].nxt = head[v];       head[v] = tot++;     }     bool spfa(int s, int t) {       memset(vis, 0, sizeof(vis));       memset(d, 0x3f, sizeof(d));       memset(pre, -1, sizeof(pre));       queue<int> q;       q.push(s);       d[s] = 0;       vis[s] = 1;       while (!q.empty()) {         int u = q.front();         q.pop();         vis[u] = 0;         for (int i = head[u]; ~i; i = E[i].nxt) {           int w = E[i].w, v = E[i].v, cost = E[i].cost;           if (w > 0 && d[v] > d[u] + cost) {             d[v] = d[u] + cost;             pre[v] = i;             if (!vis[v]) {               q.push(v);               vis[v] = 1;             }           }         }       }       return pre[t] != -1;     }     int solve(int s, int t, int &cost) {       int flow = 0;       cost = 0;       while (spfa(s, t)) {         int minFlow = inf;         for (int i = pre[t]; ~i; i = pre[E[i ^ 1].v])           minFlow = min(minFlow, E[i].w);         for (int i = pre[t]; ~i; i = pre[E[i ^ 1].v]) {           cost += minFlow * E[i].cost;           E[i].w -= minFlow;           E[i ^ 1].w += minFlow;         }         flow += minFlow;       }       return flow;     }   } F;   int a[MAXN];   int main() {     int n;     scanf( %d , &n);     F.init();     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int st = 0, en = 2 * n + 5;     int add = en - 1;     F.add(st, add, 4, 0);     int magic = 4;     for (int i = 1; i <= n; i++) {       F.add(add, i, 1, 0);       F.add(i + n, en, 1, 0);       F.add(i, i + n, 1, -1);       F.add(i, i + n, INF, 0);       int cnt1 = 0, cnt2 = 0;       for (int j = i + 1; j <= n; j++) {         if (abs(a[i] - a[j]) == 1 && cnt1 <= magic) {           F.add(i + n, j, 1, 0);           cnt1++;         }         if (a[i] % 7 == a[j] % 7 && cnt2 <= magic) {           F.add(i + n, j, 1, 0);           cnt2++;         }       }     }     int flow, cost;     flow = F.solve(st, en, cost);     printf( %d n , -cost);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( avx,avx2,tune=native )   template <typename T>   using Prior = std::priority_queue<T>;   template <typename T>   using prior = std::priority_queue<T, std::vector<T>, std::greater<T>>;   using namespace std;   void pre() {}   void solve() {     int n, k;     cin >> n >> k;     vector<long long> arr(n);     for (auto& x : arr) cin >> x;     vector<vector<long long>> dp(n, vector<long long>(n + 1));     for (int j = 1; j <= n; ++j)       dp[0][j] = dp[0][j - 1] + (bool)(arr[j - 1] == j);     if (dp[0].back() >= k) return cout << 0 <<   n , void();     for (int i = 1; i < n; ++i) {       for (int j = i + 1; j <= n; ++j)         dp[i][j] =             max(dp[i - 1][j - 1], dp[i][j - 1] + (bool)(arr[j - 1] == j - i));       if (dp[i].back() >= k) return cout << i <<   n , void();     }     cout << -1 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     pre();     int _ = 1;     cin >> _;     vector<long long> __(_);     iota(begin(__), end(__), 1);     for (auto i : __) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long maxi(long long &a, long long b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   inline long long mini(long long &a, long long b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   inline long long maxi(long double &a, long double b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   inline long long mini(long double &a, long double b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long t;     cin >> t;     while (t--) {       long long n;       string a, b;       cin >> n >> a >> b;       vector<long long> ans1, ans2;       a +=  0 ;       b +=  0 ;       for (long long i = 1; i <= n; i++) {         if (a[i] != a[i - 1]) {           ans1.push_back(i);         }         if (b[i] != b[i - 1]) {           ans2.push_back(i);         }       }       for (long long i = (long long)ans2.size() - 1; i >= 0; i--) {         ans1.push_back(ans2[i]);       }       cout << (long long)ans1.size() <<   n ;       for (auto now : ans1) {         cout << now <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, k, m, t, x, y, sum;     cin >> t;     while (t > 0) {       t = t - 1;       cin >> n >> k;       long long a[n], c[k];       for (i = 0; i < n; i++) {         cin >> a[i];       }       for (i = 0; i < k; i++) {         cin >> c[i];       }       sort(a, a + n);       sort(c, c + k);       sum = 0;       m = -1;       j = n - 1;       for (i = 0; i < k; i++) {         if (c[i] == 1) {           sum = sum + 2 * a[j];           m = i;         } else           sum = sum + a[j];         c[i] = c[i] - 1;         j = j - 1;       }       j = m + 1;       i = n - k - 1;       for (i = i; i >= 0; i = i - 1) {         while (c[j] > 1) {           c[j] = c[j] - 1;           i = i - 1;         }         sum = sum + a[i];         j = j + 1;       }       cout << sum <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double eps = 1e-9;   const int INF = 1e9 + 7;   const int MAXN = int(2e5 + 7);   int n, a[3], sum, g[10][10], d[10], ans = INF * 2;   string second[7] = { Anka ,    Chapay ,  Cleo ,        Troll ,                        Dracul ,  Snowy ,   Hexadecimal };   int get(string a) {     for (int i = 0; i < 7; i++)       if (second[i] == a) return i;   }   void check() {     vector<int> v[3];     for (int i = 0; i < 7; i++) v[d[i]].push_back(i);     int maxi = 0, mini = INF * 2;     for (int i = 0; i < 3; i++) {       if (v[i].size() == 0) return;       maxi = max(maxi, a[i] / (int)v[i].size());       mini = min(mini, a[i] / (int)v[i].size());     }     int res = 0;     for (int i = 0; i < 3; i++)       for (int j = 0; j < v[i].size(); j++)         for (int k = 0; k < v[i].size(); k++) res += g[v[i][j]][v[i][k]];     if (ans > maxi - mini)       ans = maxi - mini, sum = res;     else if (ans == maxi - mini && res > sum)       sum = res;   }   void go(int x) {     if (x == 7) {       check();       return;     }     for (int i = 0; i < 3; i++) {       d[x] = i;       go(x + 1);     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       string b[3];       cin >> b[0] >> b[1] >> b[2];       g[get(b[0])][get(b[2])] = 1;     }     cin >> a[0] >> a[1] >> a[2];     go(0);     cout << ans <<     << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const unsigned long long inf = 1e18;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<unsigned long long> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     unsigned long long ans = 0;     for (int i = 0; i < n; i++) {       for (int j = i; j < n; j++) {         for (int k = j; k < n; k++) {           ans = max(ans, (a[i] | a[j] | a[k]));         }       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const int MOD = 1e9 + 7;   const int N = 2e6 + 10;   int n, m, x, a[N], fi[N];   long long pos[N];   void prepare() {     scanf( %d%d , &n, &m);     for (auto i = 0; i < n; i++) scanf( %d , a + i), a[n + i] = a[i];     for (auto i = 1; i <= m; i++) scanf( %d , &x), fi[x] = i;   }   int solve() {     int ans = 0;     for (int r = 0, l = 0; r < 2 * n; r++) {       if (!fi[a[r]]) {         l = r + 1;         continue;       }       if (l < r)         pos[r] = fi[a[r]] + (pos[r - 1] - fi[a[r]] + m - 1) / m * m;       else         pos[r] = fi[a[r]];       while (r - l >= n || pos[r] - pos[l] >= m) l++;       ans = max(ans, r - l + 1);     }     return ans;   }   int main() {     prepare();     cout << solve();   }


#include <bits/stdc++.h>   using namespace std;   int n, m, d;   int a[(1 << 20) + 5];   int sum[(1 << 20) + 5];   int b[22][100005];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> d;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x;       for (int j = 1; j <= x; j++) {         cin >> y;         b[i][max(1, y - d + 1)]++;         b[i][y + 1]--;       }     }     for (int j = 0; j < m; j++) {       int c = 0;       for (int i = 1; i <= n; i++) {         c += b[j][i];         if (c) a[i] += (1 << j);       }     }     int ans = 22;     for (int i = 1; i <= n - d + 1; i++) sum[a[i]]++;     for (int j = 0; j < m; j++)       for (int i = 0; i < (1 << m); i++)         if ((i & (1 << j))) sum[i] += sum[(i ^ (1 << j))];     for (int i = 0; i < (1 << m); i++)       if (sum[i] == 0) ans = min(ans, m - __builtin_popcount(i));     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &x) {     x = 0;     long long f = 0;     char ch = getchar();     while (!isdigit(ch)) f = ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     x = f ? -x : x;   }   const long long N = 15, M = 105;   long long mp[N][N], f[N][1 << N], n, m, q;   struct lca {     long long x, y, z;   } L[M];   struct xy {     long long x, y;   } B[M];   bool in(long long x, long long y) { return x & (1 << y); }   long long dfs(long long u, long long mask) {     if (~f[u][mask]) return f[u][mask];     long long &now = f[u][mask];     mask ^= (1 << u), now = 0;     long long pos;     for (long long i = 0; i < n; i++)       if (mask & (1 << i)) {         pos = i;         break;       }     for (long long sub = mask; sub; sub = (sub - 1) & mask) {       if (!(sub & (1 << pos))) continue;       long long flag = 0, v = -1;       for (long long i = 1; i <= q; i++)         if (in(sub, L[i].z)) {           if (!in(sub, L[i].x) || !in(sub, L[i].y)) {             flag = 1;             break;           }         } else if (u == L[i].z) {           if (in(sub, L[i].x) && in(sub, L[i].y)) {             flag = 1;             break;           }         }       if (flag) continue;       for (long long i = 1; i <= m; i++)         if (B[i].x != u && B[i].y != u && (in(sub, B[i].x) ^ in(sub, B[i].y))) {           flag = 1;           break;         }       if (flag) continue;       for (long long i = 0; i < n; i++)         if (mp[u][i] && in(sub, i))           if (!~v)             v = i;           else {             flag = 1;             break;           }       if (flag) continue;       if (~v)         now = (now + dfs(u, mask ^ (1 << u) ^ sub) * dfs(v, sub));       else         for (long long i = 0; i < n; i++)           if (in(sub, i))             now = (now + dfs(u, mask ^ (1 << u) ^ sub) * dfs(i, sub));     }     return now;   }   signed main() {     read(n), read(m), read(q);     for (long long i = 1, x, y; i <= m; i++)       read(B[i].x), read(B[i].y), --B[i].x, --B[i].y,           mp[B[i].x][B[i].y] = mp[B[i].y][B[i].x] = 1;     for (long long x, y, z, i = 1; i <= q; i++)       read(L[i].x), read(L[i].y), read(L[i].z), L[i].x--, L[i].y--, L[i].z--;     memset(f, -1, sizeof(f));     for (long long i = 0; i < n; i++) f[i][1 << i] = 1;     printf( %lld , dfs(0, (1 << n) - 1));   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100;   long long n, k;   int a[15];   long long rc[15], p[15], ans;   void solve() {     scanf( %d%d , &n, &k);     k++;     scanf( %d , &a[1]);     rc[1] = 1;     long long now = 1, tmp;     int ss = 0;     for (int i = 2; i <= n; i++) {       scanf( %d , &a[i]);       tmp = 1;       while (ss < a[i]) now *= 10, tmp *= 10, ss++;       p[i - 1] = tmp - 1;       rc[i] = now;     }     ans = 0;     for (int i = 1; i < n; i++) {       if (k >= p[i])         ans += p[i] * rc[i], k -= p[i];       else {         ans += rc[i] * k;         k = 0;       }     }     if (k) ans += rc[n] * k;     printf( %lld n , ans);   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e5;   int n;   string ans;   int kmp(string s) {     vector<int> pf(s.size());     for (int j = 1; j < s.size(); ++j) {       int k = pf[j - 1];       while (s[k] != s[j]) {         if (!k) {           k = -1;           break;         }         k = pf[k - 1];       }       pf[j] = k + 1;     }     return pf.back();   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) {       if (!i) {         cin >> ans;         continue;       }       string s;       cin >> s;       string t = s;       t +=  # ;       int a = min((int)ans.size(), (int)s.size());       t.insert(t.end(), ans.end() - a, ans.end());       int x = kmp(t);       while (x--) ans.pop_back();       ans.insert(ans.end(), s.begin(), s.end());     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const long long int MOD = 1e18 + 7;   long long int modPow(long long int a, long long int b) {     long long int res = 1;     a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long int MODPow(long long int a, long long int b) {     long long int res = 1;     a %= MOD;     for (; b; b >>= 1) {       if (b & 1) res = res * a % MOD;       a = a * a % MOD;     }     return res;   }   long long int nCr(long long int n, long long int k) {     long long int res = 1;     if (k > n - k) k = n - k;     for (long long int i = 0; i < k; ++i) {       res = (res * (n - i)) % mod;       res = (res * modPow(i + 1, mod - 2)) % mod;     }     return res;   }   void Sieve(long long int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long int p = 2; p * p <= n; p++)       if (prime[p] == true)         for (long long int i = p * 2; i <= n; i += p) prime[i] = false;   }   int main() {     int n, m, k;     cin >> n >> m >> k;     int a[n], b[n];     for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i];     sort(b, b + n, greater<int>());     map<int, int> mp;     for (int i = 0; i < m * k; i++) mp[b[i]]++;     long long int total = 0, sum = 0;     for (int i = 0; i < m * k; i++) total += b[i];     cout << total <<   n ;     int c = 0;     std::vector<int> v;     for (int i = 0; i < n; i++) {       if (mp[a[i]] > 0) c++, mp[a[i]]--;       if (c == m) v.push_back(i + 1), c = 0;     }     for (int i = 0; i < k - 1; i++) cout << v[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename tp>   inline void read(tp& x) {     x = 0;     char tmp;     bool key = 0;     for (tmp = getchar(); !isdigit(tmp); tmp = getchar()) key = (tmp ==  - );     for (; isdigit(tmp); tmp = getchar()) x = (x << 3) + (x << 1) + (tmp ^  0 );     if (key) x = -x;   }   template <typename tp>   inline void ckmn(tp& x, tp y) {     x = x < y ? x : y;   }   template <typename tp>   inline void ckmx(tp& x, tp y) {     x = x < y ? y : x;   }   const int N = 100010;   struct edge {     int la, b;   } con[N << 1];   int tot, first[N];   void add_edge(int from, int to) {     con[++tot] = (edge){first[from], to};     first[from] = tot;   }   int n, m;   vector<int> ans;   void clear() {     tot = 0;     for (int i = (1); i <= (n); ++i) first[i] = 0;   }   int key = 0;   int dep[N], vis[N], inp[N], num[N], mnv[N], dp[N];   void dfs(int pos) {     vis[pos] = 1;     inp[pos] = 1;     mnv[pos] = pos;     for (int i = first[pos]; i; i = con[i].la) {       if (vis[con[i].b]) {         if (inp[con[i].b]) {           if (dep[con[i].b] < dep[mnv[pos]]) mnv[pos] = con[i].b;           ++num[pos];           --num[con[i].b];         } else           key = 1;       } else {         dep[con[i].b] = dep[pos] + 1;         dfs(con[i].b);         num[pos] += num[con[i].b];         if (dep[mnv[con[i].b]] < dep[mnv[pos]]) mnv[pos] = mnv[con[i].b];       }     }     inp[pos] = 0;   }   void fsd(int pos) {     vis[pos] = 1;     if (dep[pos] > 0) {       if (num[pos] == 1) {         dp[pos] = dp[mnv[pos]];       }     }     for (int i = first[pos]; i; i = con[i].la)       if (!vis[con[i].b]) fsd(con[i].b);   }   bool doit(int rt) {     key = 0;     for (int i = (1); i <= (n); ++i) vis[i] = 0;     for (int i = (1); i <= (n); ++i) num[i] = 0;     dfs(rt);     if (key) return false;     for (int i = (1); i <= (n); ++i) dp[i] = 0;     dp[rt] = 1;     for (int i = (1); i <= (n); ++i) vis[i] = 0;     fsd(rt);     for (int i = (1); i <= (n); ++i)       if (dp[i]) ans.push_back(i);     return true;   }   void solve() {     int x, y;     int sed = 0;     read(n), read(m);     for (int i = (1); i <= (m); ++i) {       read(x), read(y);       add_edge(x, y);       sed = (sed + (long long)233 * x) % 998244353;     }     srand(sed);     ans.clear();     for (int _ = (1); _ <= (100); ++_) {       int rt = ((rand() << 16ll) | rand()) % n + 1;       if (doit(rt)) break;     }     if ((int)ans.size() * 5 < n) {       puts( -1 );     } else {       for (int i = (0); i <= ((int)ans.size() - 1); ++i) printf( %d  , ans[i]);       puts(  );     }     clear();   }   int main() {     int T;     read(T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int a, b, c, d, i, j, n, flag = 0;     cin >> a >> b >> c >> d;     n = max(max(a, b), max(c, d));     flag = 1;     for (i = 0; i <= n; i++) {       for (j = 0; j <= n; j++) {         if (b + i * a == d + j * c) {           cout << b + i * a << endl;           flag = 0;           break;         }       }       if (!flag) break;     }     if (flag) cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     int m = 0, n = 0, c = 0;     m = a.length();     n = b.length();     int x = n - 1;     if (m != n) {       cout <<  NO ;       return 0;     }     for (int i = 0; i < m; i++) {       if (a[i] == b[x]) {         c++;       }       x--;     }     if (c == m) {       cout <<  YES ;     } else {       cout <<  NO ;     }   }


#include <bits/stdc++.h>   const long long int mod = 1e9 + 7;   using namespace std;   bool prime(long long int n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   bool isPowerOfTwo(long long int n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   void SieveOfEratosthenes(long long int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (long long int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (long long int p = 2; p <= n; p++)       if (prime[p]) cout << p <<    ;   }   long long int modularinv(long long int x, long long int n) {     if (n == 0)       return 1;     else if (n % 2 == 0)       return modularinv((x * x) % mod, n / 2);     else       return (x * modularinv((x * x) % mod, (n - 1) / 2)) % mod;   }   class node {    public:     long long int first;     bool operator<(const node& rhs) const { return first > rhs.first; }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int n, i;     cin >> n;     vector<long long int> v(n);     long long int sum = 0;     for (i = 0; i < n; i++) {       cin >> v[i];       sum += v[i];     }     long long int count = 0;     long long int d;     if (sum % 2 != 0)       sum = sum / 2 + 1;     else       sum = sum / 2;     for (i = 0; i < n; i++) {       count += v[i];       if (count >= sum) {         d = i + 1;         break;       }     }     cout << d;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     cin.get();     vector<string> v(n);     for (int i = 0; i < n; i++) getline(cin, v[i]);     vector<int> p(k);     for (int i = 0; i < k; i++) p[i] = i;     int minDiff = INT_MAX;     do {       int minNum, maxNum;       minNum = INT_MAX;       maxNum = INT_MIN;       for (int i = 0; i < n; i++) {         int num = 0;         for (int j = 0; j < k; j++) num = num * 10 + v[i][p[j]] -  0 ;         if (num < minNum) minNum = num;         if (num > maxNum) maxNum = num;       }       minDiff = min(minDiff, maxNum - minNum);     } while (next_permutation(p.begin(), p.end()));     cout << minDiff;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 200009;   vector<int> a;   vector<int> b;   vector<int> bb;   int main() {     long long s, x;     cin >> s >> x;     int s1 = s, x1 = x;     int ans = 0;     for (int i = 0; i < 45; i++) {       if ((s & ((long long)1 << i)) && (x & ((long long)1 << i))) ans++;       if (!(s & ((long long)1 << i)) && (x & ((long long)1 << i))) {         cout << 0;         return 0;       }       if ((s & ((long long)1 << i)) && !(x & ((long long)1 << i))) {         cout << 0;         return 0;       }       if (!(s & ((long long)1 << i)) && !(x & ((long long)1 << i))) {         if ((s & ((long long)1 << (i + 1))) && !(x & ((long long)1 << (i + 1)))) {           s -= ((long long)1 << (i + 1));         }         if (!(s & ((long long)1 << (i + 1))) && (x & ((long long)1 << (i + 1)))) {           s -= ((long long)1 << (i + 1));         }       }     }     if (s1 != x1)       cout << ((long long)1 << ans);     else       cout << ((long long)1 << ans) - 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, p, q, d[110];   void nhap() {     memset(d, 0, sizeof(d));     int x;     cin >> n >> p;     for (int i = 1; i <= p; i++) {       cin >> x;       d[x] = 1;     }     cin >> q;     for (int i = 1; i <= q; i++) {       cin >> x;       d[x] = 1;     }   }   void cbi() {     for (int i = 1; i <= n; i++)       if (!d[i]) {         cout <<  Oh, my keyboard! ;         exit(0);       }     cout <<  I become the guy. ;   }   void xuli() {}   void ghikq() {}   int main() {     nhap();     cbi();     xuli();     ghikq();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 600010;   struct wl {     int k, l, pos;   };   wl w[MAXN];   wl temp[MAXN];   int n;   int p[MAXN];   int cnt, tcnt;   int *t;   int x[MAXN], y[MAXN];   int find(int p) {     int l = 0, r = tcnt;     while (l < r) {       int mid = (l + r) / 2;       if (p > t[mid])         l = mid + 1;       else         r = mid;     }     return l;   }   void init() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &x[i], &y[i]);       p[cnt++] = x[i];       p[cnt++] = y[i];     }     sort(p, p + cnt);     int new_cnt = 0;     new_cnt = unique(p, p + cnt) - p;     t = p;     cnt = new_cnt;     tcnt = cnt;     new_cnt = 0;     w[new_cnt].k = 0;     w[new_cnt++].l = t[0] - 1;     for (int i = 0; i < cnt - 1; i++) {       w[new_cnt].k = t[i];       w[new_cnt++].l = 1;       w[new_cnt].k = t[i] + 1;       w[new_cnt++].l = t[i + 1] - t[i] - 1;     }     w[new_cnt].k = t[cnt - 1];     w[new_cnt++].l = 1;     cnt = new_cnt;     for (int i = 0; i < n; i++) {       int xx = find(x[i]) + 1, yy = find(y[i]) + 1;       swap(w[(xx - 1) * 2 + 1], w[(yy - 1) * 2 + 1]);     }   }   long long ans = 0;   long long sum[MAXN];   void solve(int l, int r) {     if (l == r) return;     int p1 = l, p2, p = l;     int mid = (l + r) / 2;     p2 = mid + 1;     solve(l, mid);     solve(mid + 1, r);     sum[l] = w[l].l;     for (int i = l + 1; i <= mid; i++) sum[i] = sum[i - 1] + w[i].l;     while (p1 <= mid && p2 <= r) {       if (w[p1].k <= w[p2].k) {         temp[p++] = w[p1++];         continue;       }       if (w[p1].k > w[p2].k) {         temp[p] = w[p2];         ans += (sum[mid] - sum[p1] + (long long)w[p1].l) * (long long)(w[p2].l);         p2++;         p++;       }     }     for (int i = p2; i <= r; i++) temp[p++] = w[i];     for (int i = p1; i <= mid; i++) temp[p++] = w[i];     for (int i = l; i <= r; i++) w[i] = temp[i];     return;   }   int main() {     init();     solve(0, cnt - 1);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> g[200001], rg[200001];   bool vis[200001];   long long int par[200001];   long long int sz[200001], dnom[200001];   long long int ans = 0, no = 0;   long long int n, m, S;   deque<long long int> top;   void dfs(long long int x) {     vis[x] = 1;     for (auto v : g[x]) {       if (!vis[v]) dfs(v);     }     top.push_front(x);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m >> S;     S--;     vector<pair<long long int, long long int> > g1[n];     long long int dis[n];     for (int i = 0; i < n; i++) {       dis[i] = LLONG_MAX / 2;     }     for (int i = 0; i < m; i++) {       long long int u, v, w;       cin >> u >> v >> w;       u--;       v--;       g1[u].push_back({v, w});       g1[v].push_back({u, w});     }     set<pair<long long int, long long int> > second;     second.insert({0, S});     dis[S] = 0;     while (!second.empty()) {       pair<long long int, long long int> p = *second.begin();       second.erase(second.begin());       if (vis[p.second]) continue;       vis[p.second] = 1;       long long int cd = p.first, u = p.second;       for (auto v : g1[u]) {         if (cd == dis[v.first] + v.second) {           g[v.first].push_back(u);           rg[u].push_back(v.first);         }         if (dis[v.first] > cd + v.second) {           dis[v.first] = cd + v.second;           second.insert({dis[v.first], v.first});         }       }     }     memset(vis, 0, sizeof(vis));     dfs(S);     long long int ans = 0;     dnom[top[0]] = -1;     for (int i = 1; i < top.size(); i++) {       long long int u = top[i];       long long int x = dnom[rg[u][0]];       bool flag = 1;       for (auto par : rg[u]) {         if (dnom[par] != x) {           flag = 0;           break;         }       }       if (!flag || x == -1)         dnom[u] = u;       else         dnom[u] = x;       sz[dnom[u]]++;       ans = max(ans, sz[dnom[u]]);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e4 + 7;   const long long M = 1e3 + 7;   const long long INF = 1e18;   long long n, m, k, a[N], g, r, ans;   bool h[N][M], ok;   queue<pair<long long, long long> > q;   vector<long long> v;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m;     for (long long i = 1; i <= m; i++) cin >> a[i];     cin >> g >> r;     sort(a + 1, a + m + 1);     v.push_back(1);     while (!v.empty()) {       for (long long i : v) q.push({i, g});       v.clear();       ans += g;       while (!q.empty()) {         long long node = q.front().first, t = q.front().second;         q.pop();         if (h[node][t]) continue;         h[node][t] = 1;         if (node == m) {           ans -= t;           ok = 1;           break;         }         if (t == 0) v.push_back(node);         if (node > 1 && a[node] - a[node - 1] <= t)           q.push({node - 1, t - a[node] + a[node - 1]});         if (a[node + 1] - a[node] <= t)           q.push({node + 1, t - a[node + 1] + a[node]});       }       if (ok == 1) break;       ans += r;     }     if (ok)       cout << ans << endl;     else       cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int tobin(string s) {     int ans = 0;     for (int i = 0; i < s.size(); i++) {       ans |= 1 << (s[i] -  a );     }     return ans;   }   int main() {     int n, mask, save = 0;     cin >> n;     mask = (1 << 26) - 1;     while (n--) {       char c, c2;       string s;       cin >> c;       if (c ==  . ) {         cin >> s;         mask &= ~tobin(s);       }       if (c ==  ! ) {         if (__builtin_popcount(mask) == 1) save++;         cin >> s;         mask &= tobin(s);       }       if (c ==  ? ) {         if (__builtin_popcount(mask) == 1 && n != 0) save++;         cin >> c2;         mask &= ~(1 << (int)(c2 -  a ));       }     }     cout << save <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long max_n = 1000006;   inline long long read() {     long long x = 0;     bool w = 0;     char c = getchar();     while (c <  0  || c >  9 ) w |= c ==  - , c = getchar();     while (c >=  0  && c <=  9 )       x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();     return w ? -x : x;   }   inline void write(long long x) {     if (x < 0) x = -x, putchar( - );     if (x > 9) write(x / 10);     putchar(x % 10 ^ 48);   }   long long n, a[max_n], t[max_n], pr[max_n];   struct unionfind {     long long fa[max_n], sz[max_n];     inline void clear() {       for (register long long i = 0; i <= n; ++i) fa[i] = i;     }     inline long long find(long long x) {       if (fa[x] == x) return x;       return fa[x] = find(fa[x]);     }     inline void merge(long long x, long long y) {       x = find(x), y = find(y);       if (x == y) return;       fa[x] = y;     }   } uf;   long long c[max_n], d[max_n];   inline void getpr() {     for (register long long i = 2; i < max_n; ++i)       if (!pr[i])         for (register long long j = i, k = 0; j < max_n; j += i) {           if (t[j]) {             if (!k)               c[i] = uf.find(t[j]);             else               uf.merge(k, t[j]);             k = t[j];           }           pr[j] = i;         }   }   map<pair<long long, long long>, bool> mp;   signed main() {     n = read();     long long T = read();     uf.clear();     for (register long long i = 1; i <= n; ++i) {       a[i] = read();       t[a[i]] = i;     }     getpr();     for (register long long i = 2; i < max_n; ++i)       if (pr[i] == i) c[i] = uf.find(c[i]);     for (register long long i = 1; i <= n; ++i) {       long long j = a[i] + 1, cnt = 0;       d[++cnt] = uf.find(i);       while (j != 1) {         d[++cnt] = c[pr[j]];         j /= pr[j];       }       sort(d + 1, d + 1 + cnt), cnt = unique(d + 1, d + 1 + cnt) - d - 1;       for (register long long j = 1; j < cnt; ++j)         for (register long long k = j + 1; k <= cnt; ++k)           mp[make_pair(d[j], d[k])] = 1;     }     while (T--) {       long long x = read(), y = read();       x = uf.find(x), y = uf.find(y);       if (x == y) {         puts( 0 );         continue;       }       if (x > y) x ^= y ^= x ^= y;       if (mp[make_pair(x, y)])         puts( 1 );       else         puts( 2 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v;     bool g;     int n, m[5001] = {0}, x;     cin >> n;     if (n % 2 == 1) {       v.push_back(1);       m[1] = 1;     } else {       v.push_back(2);       m[2] = 1;     }     while (true) {       g = false;       for (int i = 1; i <= n; i++) {         if (!m[i] && abs(v.back() - i) != 1) {           v.push_back(i);           m[i] = 1;           g = true;         }       }       if (!g) break;     }     cout << v.size() <<   n ;     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp_pos[1000010], dp_neg[1000010];   int mark_pos[1000010], mark_neg[1000010];   int pos[1000010], neg[1000010], cnt_pos = 0, cnt_neg = 0;   int max_pos, max_neg;   int a[1000010];   void calc(int* dp, int* mark) {     int cnt = 0, i, j;     for (i = 0; i <= max_pos * max_neg; i++) dp[i] = 10000000;     for (i = 0; i <= 1000; i++)       if (mark[i]) a[cnt++] = i;     for (i = 0; i < cnt; i++) dp[a[i]] = 1;     for (i = 1; i <= max_pos * max_neg; i++)       for (j = 0; j < cnt && a[j] <= i; j++) dp[i] = min(dp[i], 1 + dp[i - a[j]]);   }   int main() {     int n, k, i;     scanf( %d , &n);     scanf( %d , &k);     memset(mark_pos, 0, sizeof(mark_pos));     memset(mark_neg, 0, sizeof(mark_neg));     for (i = 0; i < k; i++) {       int x;       scanf( %d , &x);       if (x >= n)         mark_pos[x - n] = 1;       else         mark_neg[n - x] = 1;     }     if (mark_pos[0]) {       printf( 1 n );       return 0;     }     max_pos = max_neg = -10000000;     for (i = 0; i <= 1000; i++)       if (mark_pos[i]) max_pos = i;     for (i = 0; i <= 1000; i++)       if (mark_neg[i]) max_neg = i;     if (max_neg == -10000000 || max_pos == -10000000) {       printf( -1 n );       return 0;     }     calc(dp_pos, mark_pos);     calc(dp_neg, mark_neg);     int ans = 2 * 10000000;     for (i = 1; i <= max_neg * max_pos; i++)       ans = min(ans, dp_pos[i] + dp_neg[i]);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, check = 0;     string x;     cin >> n >> x;     sort(x.begin(), x.end());     if (n == 1)       cout <<  YES ;     else {       for (int i = 0; i < x.size() - 1; i++) {         if (x[i] == x[i + 1]) {           check = 1;           break;         }       }       if (check == 1)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1e5 + 10;   using namespace std;   map<int, int> mp;   struct a {     int x, y, z;   };   vector<a> res;   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       int t;       cin >> t;       mp[t]++;     }     priority_queue<pair<int, int>> pq;     for (auto it = mp.begin(); it != mp.end(); ++it) {       int fi = it->second;       int se = it->first;       pq.push({fi, se});     }     while (pq.size() >= 3) {       auto xx = pq.top();       pq.pop();       auto yy = pq.top();       pq.pop();       auto zz = pq.top();       pq.pop();       a temp;       temp.x = xx.second;       temp.y = yy.second;       temp.z = zz.second;       res.push_back(temp);       if (xx.first - 1) {         pq.push({xx.first - 1, xx.second});       }       if (yy.first - 1) {         pq.push({yy.first - 1, yy.second});       }       if (zz.first - 1) {         pq.push({zz.first - 1, zz.second});       }     }     cout << res.size() <<   n ;     for (int i = 0; i < res.size(); ++i) {       int b[3];       b[0] = res[i].x;       b[1] = res[i].y;       b[2] = res[i].z;       sort(b, b + 3);       cout << b[2] <<     << b[1] <<     << b[0] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(int64_t &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const int64_t &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.16f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.first);     putchar(   );     _W(x.second);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   int MOD = 1e9 + 7;   void ADD(long long &x, long long v) {     x = (x + v) % MOD;     if (x < 0) x += MOD;   }   const int SIZE = 1 << 20;   vector<int> pos[SIZE];   int new_id[SIZE];   int a[SIZE];   int dp[SIZE];   void solve() {     int n;     R(n);     vector<int> d;     for (int i = 0; i < (n); ++i) {       R(a[i]);       d.emplace_back(a[i]);     }     for (int i = 0; i < (n); ++i) pos[i].clear();     (sort(d.begin(), d.end()),      d.resize(distance(d.begin(), unique(d.begin(), d.end()))));     for (int i = 0; i < (n); ++i)       a[i] = (lower_bound(d.begin(), d.end(), a[i]) - d.begin());     int an = 0;     for (int i = 0; i < (n); ++i) {       pos[a[i]].emplace_back(i);     }     int it = 0;     for (int i = 0; i < (((int)(d).size())); ++i) {       an = max(an, ((int)(pos[i]).size()));       for (int v : pos[i]) new_id[it++] = v;     }     for (int i = n - 1; i >= 0; i--) {       dp[i] = 1;       if (i + 1 < n && new_id[i] < new_id[i + 1]) dp[i] += dp[i + 1];     }     for (int i = 0; i < (n); ++i) {       int base = dp[i];       int added = 0;       if (i && a[new_id[i]] != a[new_id[i - 1]]) {         added += lower_bound((pos[a[new_id[i - 1]]]).begin(),                              (pos[a[new_id[i - 1]]]).end(), new_id[i]) -                  pos[a[new_id[i - 1]]].begin();       }       if (i + base < n && a[new_id[i + base]] != a[new_id[i + base - 1]]) {         added +=             pos[a[new_id[i + base]]].end() -             upper_bound((pos[a[new_id[i + base]]]).begin(),                         (pos[a[new_id[i + base]]]).end(), new_id[i + base - 1]);       }       an = max(an, base + added);     }     for (int i = (1); i < (((int)(d).size())); ++i) {       for (int j = 0; j < (((int)(pos[i]).size())); ++j) {         an = max(an, ((int)(pos[i]).size()) - j +                          (int)(lower_bound((pos[i - 1]).begin(),                                            (pos[i - 1]).end(), pos[i][j]) -                                pos[i - 1].begin()));       }     }     W(n - an);   }   int main() {     int ___T;     scanf( %d , &___T);     for (int cs = 1; cs <= ___T; cs++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[10000], b[10000];   long long res[10000];   int val[10000];   map<long long, int> mp;   int main() {     int n;     long long ans = 0;     int len = 0;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       mp[a[i]]++;       if (mp[a[i]] == 2) res[++len] = a[i];     }     for (int i = 1; i <= n; i++) {       cin >> b[i];     }     for (int i = 1; i <= len; i++) {       for (int j = 1; j <= n; j++) {         if ((res[i] & a[j]) == a[j]) val[j] = 1;       }     }     for (int i = 1; i <= n; i++) {       ans += val[i] * b[i];     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void deba(T *a, int n) {     for (int i = 0; i < n; i++) cout << a[i] <<    ;     cout <<   n ;   }   const int N = 5e5 + 10, M = 5e5 + 10;   long long n, m, c[N], t[N], num, ans[N];   vector<pair<long long, long long> > e3[N];   vector<long long> e2[M];   bool mark[M];   vector<long long> e[M], er[M], topSort;   long long topSort_num[M], comp[M], cnt_comp;   void add(long long x, long long y) {     e2[x ^ 1].push_back(y);     e2[y ^ 1].push_back(x);   }   void topol(long long x) {     mark[x] = 1;     for (auto u : e[x])       if (!mark[u]) topol(u);     topSort.push_back(x);     topSort_num[x] = topSort.size();   }   void dfs(long long x) {     comp[x] = cnt_comp;     for (auto u : er[x])       if (!comp[u]) dfs(u);   }   bool check(long long x, bool exx = 0) {     topSort.clear();     memset(mark, 0, sizeof(mark));     memset(comp, 0, sizeof(comp));     cnt_comp = 1;     for (int i = 0; i < 2 * num; i++) e[i] = e2[i];     for (int i = 0; i < m; i++)       if (t[i] > x) e[2 * i + 1].push_back(2 * i);     for (int i = 0; i < 2 * num; i++)       if (!mark[i]) topol(i);     reverse(topSort.begin(), topSort.end());     for (int i = 0; i < 2 * num; i++) er[i].clear();     for (int i = 0; i < 2 * num; i++)       for (auto u : e[i]) er[u].push_back(i);     for (auto u : topSort)       if (!comp[u]) {         dfs(u);         cnt_comp++;       }     for (int i = 0; i < m; i++) {       if (comp[2 * i] == comp[2 * i + 1]) return 0;       if (comp[2 * i + 1] > comp[2 * i])         ans[i] = 1;       else         ans[i] = 0;     }     return 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     num = m;     for (int i = 0; i < m; i++) {       long long v, u;       cin >> v >> u >> c[i] >> t[i];       e3[v].push_back({c[i], i});       e3[u].push_back({c[i], i});     }     for (int i = 1; i < n + 1; i++) {       if (!e3[i].size()) continue;       bool flg = 0;       long long id;       sort(e3[i].begin(), e3[i].end());       for (int j = 1; j < e3[i].size(); j++) {         if (e3[i][j].first == e3[i][j - 1].first && flg) {           cout <<  No ;           return 0;         }         if (e3[i][j].first == e3[i][j - 1].first) flg = 1, id = j;       }       if (flg) add(2 * e3[i][id].second + 1, 2 * e3[i][id - 1].second + 1);       long long lst = e3[i][0].second;       for (int j = 1; j < e3[i].size(); j++) {         long long ex = e3[i][j].second;         add(2 * lst, 2 * ex);         add(2 * ex, 2 * num + 1);         add(2 * lst, 2 * num + 1);         lst = num++;       }     }     long long l = -1, r = (long long)N * 10000;     while (l < r - 1) {       long long mid = (l + r) / 2;       if (check(mid))         r = mid;       else         l = mid;     }     if (r == N * 10000) {       cout <<  No ;       return 0;     }     check(r, 1);     long long res = 0;     for (int i = 0; i < m; i++) res += ans[i];     cout <<  Yes n  << r <<     << res <<   n ;     for (int i = 0; i < m; i++)       if (ans[i]) cout << i + 1 <<    ;     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   const int maxm = 305;   long long a[maxn];   long long c[11][maxn];   long long num[maxn];   long long n, m, v, k;   int main() {     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     long long ans = 0;     ans += (a[n - 1] * k + a[n - 2]) * (m / (k + 1));     long long mod = m % (k + 1);     ans += (a[n - 1]) * mod;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, i, j, k, l, T, u, v, q, t = 0, t1 = 0, te, c = 0;   long long int a[1010], b[1010];   long long int gcd(long long int p, long long int q) {     if (q == 0)       return p;     else       return gcd(q, p % q);   }   int main() {     std::ios::sync_with_stdio(false);     long long int t, w, b;     cin >> t >> w >> b;     long long int g = gcd(max(w, b), min(w, b)), lcm, ans = 0;     double l = w;     l = l * b;     l = l / g;     if (l <= 6e18) {       lcm = w / g;       lcm = lcm * b;       w = min(w, b);       ans = t / lcm;       ans = ans * w;       ans += min(t % lcm, w - 1);     } else {       w = min(w, b);       lcm = t + 1;       ans += min(t % lcm, w - 1);     }     g = gcd(t, ans);     t = t / g;     ans = ans / g;     cout << ans <<  /  << t;   }


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


#include <bits/stdc++.h>   using namespace std;   long long n;   long long head[1100000], a[1100000];   struct node {     long long v, nex;   } bian[2100000];   long long cnt;   void add(long long x, long long y) {     bian[++cnt].v = y;     bian[cnt].nex = head[x];     head[x] = cnt;   }   long long tot[1100000], d[1100000], f[1100000], zong;   void dfs(long long x, long long fa) {     tot[x] = 1;     for (long long i = head[x]; i; i = bian[i].nex) {       long long v = bian[i].v;       if (v == fa) continue;       dfs(v, x);       tot[x] += tot[v];     }     f[1] += tot[x];   }   long long ans = 0;   void dp(long long x, long long fa) {     ans = max(ans, f[x]);     for (long long i = head[x]; i; i = bian[i].nex) {       long long v = bian[i].v;       if (v == fa) continue;       f[v] = f[x] - tot[v] + n - tot[v];       dp(v, x);     }   }   signed main() {     cin >> n;     for (long long i = 1; i < n; i++) {       long long x, y;       scanf( %lld%lld , &x, &y);       add(x, y), add(y, x);     }     dfs(1, 0);     dp(1, 0);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long N, K, L, R;   long long G, A;   long long ans;   void run(int use, long long ntot) {     if (N <= 500000) {       for (int nplus = 0; nplus <= N - 1; nplus++) {         long long rr = (N + nplus + use);         long long rv = G + (1 + use);         long long nbefore = ((ntot - rv) % rr + rr) % rr;         long long nafter = nplus - nbefore;         if (nbefore >= 0 && nbefore <= G && nafter >= 0 && nafter <= A)           ans = max(ans, (long long)nplus + use);       }     } else {       if (G + 1 + use <= ntot) {         long long rv = ntot - G - 1 - use;         if (rv <= G) ans = max(ans, rv + use + A);       }       for (int nrun = 1; nrun * N <= ntot; nrun++) {         long long nstart = (nrun * (N + use)) + (G + 1 + use);         long long nleft = ntot - nstart;         if (nleft >= 0) {           long long nbefore = nleft % nrun;           long long nafter = (nleft - ((nrun + 1) * (nbefore))) / nrun;           if (nafter > A) {             long long ndrop = (nafter - A + nrun) / (nrun + 1);             nafter -= ndrop * (nrun + 1);             nbefore += ndrop * nrun;           }           if (nafter >= 0 && nafter <= A && nbefore >= 0 && nbefore <= G)             ans = max(ans, nbefore + nafter + use);         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cin >> N >> L >> R >> K;     G = (R - L + N) % N;     A = (N - 1 - G);     ans = -1;     for (int i = 0; i <= 1; i++) {       for (int j = 0; j <= i; j++) {         run(i, K + j);       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T abs(T t) {     return t < 0 ? -t : t;   }   const long long modn = 1000000007;   inline long long mod(long long x) { return x % modn; }   const int MAXN = 312345;   int n, m;   vector<long long> s[4];   struct pd {     long long x;     int i[3];     pd(long long xx, int ii, int iii) {       x = xx;       i[1] = ii;       i[2] = iii;     }     pd() {}     bool operator<(const struct pd &o) const {       return (x < o.x || (x == o.x && i[1] < o.i[1]));     }   } dp[MAXN];   bool cmp(int a, int b) { return a > b; }   int main() {     scanf( %d %d , &n, &m);     long long res = 0;     for (int a = 0; a < n; a++) {       long long w, val;       scanf( %lld %lld , &w, &val);       s[w].push_back(val);       if (w == 3) res += val;     }     for (int i = 1; i <= 3; i++) sort(s[i].begin(), s[i].end(), cmp);     dp[0] = (pd){0, 0, 0};     for (int i = 0; i < m; i++) {       dp[i + 1] = max(dp[i], dp[i + 1]);       if (dp[i].i[1] != s[1].size())         dp[i + 1] = max(dp[i + 1], pd(dp[i].x + s[1][dp[i].i[1]], dp[i].i[1] + 1,                                       dp[i].i[2]));       if (dp[i].i[2] != s[2].size())         dp[i + 2] = max(dp[i + 2], pd(dp[i].x + s[2][dp[i].i[2]], dp[i].i[1],                                       dp[i].i[2] + 1));       ;     }     int i = 0, j = 0;     long long ans = 0;     for (int k = s[3].size(); k >= 0; k--) {       if (m < 3 * k) {         if (k > 0) {           res -= s[3][k - 1];         }         continue;       }       int sob = m - 3 * k;       ans = max(ans, res + dp[sob].x);       if (k > 0) res -= s[3][k - 1];     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005, MOD = 1e9 + 7;   int d, n, a[N], dp[N], rt;   vector<int> g[N];   void dfs(int v, int pr = -1) {     dp[v] = 1;     for (int to : g[v]) {       if (to == pr) {         continue;       }       if (a[to] < a[rt] || a[to] > a[rt] + d) {         continue;       }       if (a[to] == a[rt] && to > rt) {         continue;       }       dfs(to, v);       dp[v] = (1ll * dp[v] * (dp[to] + 1)) % MOD;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> d >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       rt = i;       dfs(rt, -1);       ans = (ans + dp[i]) % MOD;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, d, i, j;     cin >> k >> d;     if (d != 0) {       for (i = 1; i <= k; i++) {         if (i == 1)           cout << d;         else           cout << 0;       }     }     if (d == 0) {       if (k == 1)         cout << 0;       else         cout <<  No solution ;     }   }


#include<bits/stdc++.h>      #define ll long long   using namespace std;   const int N = 2e5 + 10;      struct Seg_Tree {       struct {           int l, r, num;           ll sum, sL, sR;       } t[N << 3];      #define update(p) {                                                                              t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum;                                            t[p].num = t[p << 1].num + t[p << 1 | 1].num;                                            t[p].sL = t[p << 1].sL + t[p << 1].sum * t[p << 1 | 1].num + t[p << 1 | 1].sL;           t[p].sR = t[p << 1].sR + t[p << 1 | 1].sum * t[p << 1].num + t[p << 1 | 1].sR;       }          void build(int p, int l, int r) {           t[p].l = l, t[p].r = r;           if (l == r)               return t[p].sum = t[p].sL = t[p].sR = t[p].num = 0, void();           int mid = (l + r) >> 1;           build(p << 1, l, mid);           build(p << 1 | 1, mid + 1, r);           update(p);       }          void change(int p, int x, ll v) {           if (t[p].l == t[p].r) {               t[p].sum += v, t[p].sL += v, t[p].sR += v;               t[p].num += v < 0 ? -1 : 1;               return;           }           int mid = (t[p].l + t[p].r) >> 1;           change(x <= mid ? p << 1 : p << 1 | 1, x, v);           update(p);       }          ll ask_L(int p, int x) {           if (t[p].num <= x)return t[p].sum;           ll val = ask_L(p << 1, x);           if (x > t[p << 1].num)               val += ask_L(p << 1 | 1, x - t[p << 1].num);           return val;       }          ll ask_R(int p, int x) {           if (t[p].num <= x)return t[p].sum;           ll val = ask_R(p << 1 | 1, x);           if (x > t[p << 1 | 1].num)               val += ask_R(p << 1, x - t[p << 1 | 1].num);           return val;       }          ll ask_sL(int p, int x) {           if (t[p].num <= x)return t[p].sL;           ll val = ask_sL(p << 1, x);           if (x > t[p << 1].num)               val += t[p << 1].sum * (x - t[p << 1].num) + ask_sL(p << 1 | 1, x - t[p << 1].num);           return val;       }          ll ask_sR(int p, int x) {           if (t[p].num <= x)return t[p].sR;           ll val = ask_sR(p << 1 | 1, x);           if (x > t[p << 1 | 1].num)               val += t[p << 1 | 1].sum * (x - t[p << 1 | 1].num) + ask_sR(p << 1, x - t[p << 1 | 1].num);           return val;       }   } S;      int n, q;   ll a[N];   vector<ll> v;   pair<int, ll> p[N];   #define f(x) (S.ask_R(1, x)-S.ask_L(1,x+1))      inline ll solve() {       if (n <= 1)return 0ll;       int l, r, pmx = (n) >> 1;       if (f(pmx) <= 0)return S.t[1].sR - S.t[1].sL;       int pl = 1, pr = n - 1;       if (f(pl) < 0) {           l = 1, r = pmx;           while (l < r) {               int mid = (l + r) >> 1;               f(mid) >= 0 ? (r = mid) : (l = mid + 1);           }           pl = l;       }       if (f(pr - 1) < 0) {           l = pmx, r = n - 2;           while (l < r) {               int mid = (l + r + 1) >> 1;               f(mid) >= 0 ? (l = mid) : (r = mid - 1);           }           pr = l + 1;       }       ll ans = S.ask_R(1, pr) - S.ask_L(1, pl);       if (pl > 1)ans += S.ask_sR(1, pl - 1) - S.ask_sL(1, pl - 1);       if (pr < n - 1)ans += (S.t[1].sR - S.ask_sR(1, pr)) - (S.t[1].sL - S.ask_sL(1, pr));       return ans;   }      int main() {       scanf( %d%d , &n, &q);       for (int i = 1; i <= n; i++)           scanf( %lld , &a[i]), v.push_back(a[i]);       for (int i = 1; i <= q; i++) {           scanf( %d%lld , &p[i].first, &p[i].second);           v.push_back(p[i].second);       }       v.push_back(0);       sort(v.begin(), v.end());       v.erase(unique(v.begin(), v.end()), v.end());       for (int i = 1; i <= n; i++)           a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();       for (int i = 1; i <= q; i++)           p[i].second = lower_bound(v.begin(), v.end(), p[i].second) - v.begin();       S.build(1, 1, (int) v.size() - 1);       for (int i = 1; i <= n; i++)S.change(1, a[i], v[a[i]]);       printf( %lld n , solve());       for (int i = 1; i <= q; i++) {           if (p[i].first == 1) {               n++;               S.change(1, p[i].second, v[p[i].second]);           } else {               n--;               S.change(1, p[i].second, -v[p[i].second]);           }           printf( %lld n , solve());       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long tot, n, w, b, x;   deque<pair<long long, long long> > maxq;   void add(long long val, long long pos) {     while (maxq.size() > 0 && maxq.back().first <= val) maxq.pop_back();     maxq.push_back({val, pos});   }   long long query() { return maxq.front().first; }   void erase(long long pos) {     while (maxq.size() > 0 && maxq.front().second < pos) maxq.pop_front();   }   int main() {     cin >> n >> w >> b >> x;     long long c[n], cost[n];     for (int i = 0; i < n; i++) cin >> c[i];     for (int j = 0; j < n; j++) cin >> cost[j];     long long dp[n][10005], temp;     for (int i = 0; i < n; i++)       for (int j = 0; j < 10005; j++) dp[i][j] = -1000000000000ll * n;     for (long long j = 0; j < 10001; j++) {       if (j <= c[0]) {         if (w - cost[0] * j < 0)           dp[0][j] = -1000000000000ll * n;         else           dp[0][j] = min(w + b * j, w - cost[0] * j + x);       } else         dp[0][j] = -1000000000000ll * n;     }     for (int i = 1; i < n; i++) {       maxq.clear();       tot = 0;       for (long long j = 0; j < 10001; j++) {         add(dp[i - 1][j] - tot, j);         temp = query() + tot;         if (temp < 0)           dp[i][j] = -1000000000000ll * n;         else           dp[i][j] = min(w + b * j, temp + x);         tot -= cost[i];         erase(j - c[i] + 1);       }     }     for (int i = 10000; i >= 0; i--) {       if (dp[n - 1][i] >= 0) {         cout << i << endl;         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string s;     cin >> s;     long long int n = s.size();     map<char, long long int> r;     for (long long int i = 0; i < n; i++) {       r[s[i]]++;     }     long long int k = 0, cnt = 0;     for (auto it = r.begin(); it != r.end(); it++) {       if (it->second >= 2)         k++;       else if (it->second == 1)         cnt++;     }     cout << k + cnt / 2 <<   n ;     return;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long int t = 1;     cin >> t;     for (long long int i = 1; i < t + 1; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isVowel(char c) {     return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u );   }   int main() {     vector<string> v;     int cnt[] = {5, 7, 5};     for (int i = 0; i < 3; i++) {       string s;       char c;       cin >> c;       getline(cin, s);       s = c + s;       v.push_back(s);     }     for (int i = 0; i < 3; i++) {       int c = 0;       for (auto u : v[i]) {         if (isVowel(u)) c++;       }       if (c != cnt[i]) return cout <<  NO n , 0;     }     cout <<  YES n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000;   int n, k, p[N], gk[256], gl[256];   int main() {     cin >> n >> k;     memset(gk, -1, sizeof(gk));     for (int i = 0; i < n; i++) {       cin >> p[i];     }     for (int i = 0; i < n; i++) {       int cc = p[i];       if (gk[cc] == -1) {         int leftmost, len = 0;         for (int j = cc; j >= 0 && gk[j] == -1 && len < k; j--) {           leftmost = j;           len++;         }         if (leftmost > 0 && len < k && gl[leftmost - 1] + len <= k) {           len += gl[leftmost - 1];           leftmost = gk[leftmost - 1];         }         for (int j = cc; j >= leftmost; j--) {           gk[j] = leftmost;           gl[j] = len;         }       }       p[i] = gk[cc];     }     for (int i = 0; i < n; i++) {       cout << p[i] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(228);   const int M = 1791791791;   const int B = 239;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<string> a(n), b(m);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < m; i++) {       cin >> b[i];     }     map<int, int> ans;     for (int i = 0; i + m - 1 < n; i++) {       int kek = 0;       for (int x = i; x <= i + m - 1; x++) {         for (int y = 0; y < m; y++) {           kek = (kek * (long long)B + a[x][y]) % M;         }       }       ans[kek] = i;     }     for (int j = 0; j + m - 1 < n; j++) {       int kek = 0;       for (int x = 0; x < m; x++) {         for (int y = j; y <= j + m - 1; y++) {           kek = (kek * (long long)B + b[x][y]) % M;         }       }       if (ans.count(kek)) {         cout << ans[kek] + 1 <<     << j + 1 <<   n ;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 1e1;   long long n;   long long r, avg;   long long must, ans = 0, sum = 0;   pair<long long, long long> d[MAXN];   int main() {     cin >> n >> r >> avg;     long long x, y;     for (long long i = 1; i <= n; i++) {       scanf( %I64d%I64d , &x, &y);       sum += x;       d[i] = make_pair(y, x);     }     must = max(n * avg - sum, 1LL * 0);     sort(d + 1, d + n + 1);     long long i = 1;     while (must) {       ans += min(must, r - d[i].second) * d[i].first;       must -= min(must, r - d[i].second);       i++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int a[100001];   stack<int> st;   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     long long int sum = 0;     for (int i = 0; i < n; i++) cin >> a[i];     sum += a[0];     for (int i = 1; i < n; i++) sum += max(a[i] - a[i - 1], 0);     cout << sum <<   n ;     for (int i = 0; i < a[0]; i++) st.push(0);     for (int i = 1; i < n; i++) {       if (a[i] - a[i - 1] > 0) {         for (int j = 0; j < a[i] - a[i - 1]; j++) st.push(i);       } else if (st.size() != 0) {         int num = abs(a[i] - a[i - 1]);         for (int j = 0; j < num; j++) {           int inde = st.top();           inde++;           st.pop();           cout << inde <<     << i <<   n ;         }       }     }     for (int i = 0; i < a[n - 1]; i++) {       if (st.size() != 0) {         cout << st.top() + 1 <<     << n <<   n ;         st.pop();       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long xx, yy, n, m, i, g, j, k, x, y;     std::vector<long long> v;     vector<pair<long long, long long>> vp;     cin >> n >> m;     map<pair<long long, long long>, long long> mp;     for (i = 0; i < m; i++) {       cin >> x >> y;       vp.push_back({x, y});       mp[{x, y}]++;     }     for (i = 1; i * i <= n; i++) {       if (n % i == 0) {         v.push_back(i);         v.push_back(n / i);       }       if ((n % i == 0 && i == n / i) || i == 1) {         v.pop_back();       }     }     for (auto itr : v) {       g = 1;       k = itr;       for (i = 0; i < m; i++) {         xx = vp[i].first + k;         yy = vp[i].second + k;         if (xx >= n + 1) {           xx = xx % (n + 1) + 1;         }         if (yy >= n + 1) {           yy = yy % (n + 1) + 1;         }         if (mp.find({xx, yy}) == mp.end() && mp.find({yy, xx}) == mp.end()) {           g = 0;           break;         }       }       if (g) {         cout <<  Yes               <<   n ;         return 0;       }     }     cout <<  No           <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long sum = 0, n, m;   const long long mod = 1e9 + 7;   long long f[25];   long long qpow(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;   }   long long getc(long long a, long long b) {     if (a < b) return 0;     if (b > a - b) b = a - b;     long long s1 = 1, s2 = 1;     for (long long i = 0; i < b; i++) {       s1 = s1 * (a - i) % mod;       s2 = s2 * (i + 1) % mod;     }     return s1 * qpow(s2, mod - 2) % mod;   }   long long lucas(long long n, long long k) {     if (k == 0) return 1;     return getc(n % mod, k % mod) * lucas(n / mod, k / mod) % mod;   }   long long solve() {     long long ans = 0;     for (int i = 0; i < (1 << n); i++) {       long long cnt = 0, sum = m;       for (int j = 0; j < n; j++) {         if (i & (1 << j)) {           sum -= f[j] + 1;           cnt++;         }       }       if (sum < 0) continue;       if (cnt & 1)         ans -= lucas(sum + n - 1, n - 1);       else         ans += lucas(sum + n - 1, n - 1);       ans %= mod;     }     return (ans + mod) % mod;   }   int main() {     scanf( %lld%lld , &n, &m);     for (int i = 0; i < n; i++) scanf( %lld , &f[i]);     printf( %lld n , solve());     return 0;   }


#include <bits/stdc++.h>   const int inf = 1e5 + 10;   const long long mod = 998244353;   const int LARGEST = 0x3f3f3f3f;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};   int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};   int bx[] = {0, 1, 0, -1};   int by[] = {-1, 0, 1, 0};   using namespace std;   int a[inf], stmin[inf][20], stmax[inf][20];   pair<int, int> q[inf];   int dp[inf];   int n, s, len, head, tail;   int ask(int x, int y) {     int tlen = log(y - x + 1) / log(2);     int p = max(stmax[x][tlen], stmax[y - (1 << tlen) + 1][tlen]);     int q = min(stmin[x][tlen], stmin[y - (1 << tlen) + 1][tlen]);     return p - q;   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> s >> len;     for (int i = 1; i <= n; i++) {       cin >> a[i];       stmax[i][0] = stmin[i][0] = a[i];     }     for (int j = 1; j < 20; j++)       for (int i = 1; i <= n; i++)         if (i + (1 << (j - 1)) <= n) {           stmax[i][j] = max(stmax[i][j - 1], stmax[i + (1 << (j - 1))][j - 1]);           stmin[i][j] = min(stmin[i][j - 1], stmin[i + (1 << (j - 1))][j - 1]);         }     if (len > n || ask(1, len) > s) {       cout << -1 << endl;       return 0;     }     for (int i = 1; i <= n; i++) dp[i] = LARGEST;     dp[0] = 0;     dp[len] = 1;     head = tail = 1;     q[tail++] = make_pair(dp[0], 0);     for (int i = len + 1; i <= n; i++) {       int l = 1, r = i - len + 1, ans = -1;       while (l < r) {         int mid = (l + r) >> 1;         if (ask(mid, i) <= s)           r = ans = mid;         else           l = mid + 1;       }       if ((ask(l, i) <= s) && ans == -1)         ans = l;       else if (ans != -1 && (ask(l, i) <= s))         ans = min(ans, l);       while (head < tail && q[tail - 1].first >= dp[i - len]) tail--;       q[tail++] = make_pair(dp[i - len], i - len);       if (ans != -1) {         while (head < tail && q[head].second < ans - 1) head++;         if (head < tail) dp[i] = min(dp[i], q[head].first + 1);       }     }     if (dp[n] != LARGEST)       cout << dp[n] << endl;     else       cout << -1 << endl;   }


#include <bits/stdc++.h>   const double PI = acos(-1.0);   using namespace std;   const int MAXN = 200117, MAXM = 1200117, C = 5, MAXX = 1000000120;   int n, k, a[MAXN], b, c, m, p[MAXM];   long long ans, s[C];   priority_queue<long long> q[C];   inline void add(int x) {     int rx = ((x % C) + C) % C;     for (int i = 0; i < C; i++) {       int d = c * ((i < rx) ? (C + i - rx) : (i - rx));       long long xx =           d + 1LL * b *                   (((MAXX + i) - (x + ((i < rx) ? (C + i - rx) : (i - rx)))) / C);       if (((int)(q[i]).size()) < k)         s[i] += xx, q[i].push(xx);       else if (q[i].top() > xx)         s[i] -= q[i].top() - xx, q[i].pop(), q[i].push(xx);     }   }   int main() {     scanf( %d %d %d %d , &n, &k, &b, &c);     b = min(b, 5 * c);     for (int i = 0; i < n; scanf( %d , &a[i++]))       ;     for (int i = 0; i < n; i++)       for (int j = 0; j <= 5; j++) p[m++] = a[i] + j;     sort(p, p + m);     m = unique(p, p + m) - p;     sort(a, a + n);     ans = (2117117117117117117LL);     for (int i = 0, j = 0; i < m; i++) {       for (; j < n && p[i] >= a[j]; add(a[j++]))         ;       int t = ((p[i] % C) + C) % C;       if (((int)(q[t]).size()) == k)         ans = min(ans, s[t] - 1LL * k * b * (((MAXX + t) - p[i]) / C));     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int po(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y >>= 1;       x = (x * x) % p;     }     return res % p;   }   long long int modInverse(long long int n, long long int p) {     return po(n, p - 2, p);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     long long int cnt = 0;     for (long long int i = 1; i < n + 1; i++) {       long long int c = 0;       long long int x = i;       for (long long int j = 2; j * j <= i; j++) {         if (x % j == 0) {           c++;           while (x % j == 0) x /= j;         }       }       if (x != 1) c++;       if (c == 2) cnt++;     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   struct listNode {     long long int n;     listNode *next;     listNode(long long int n = 0LL) : n(n), next(NULL) {}   };   long long int num;   listNode *edge[100100], *tail[100100];   long long int color[100100];   void insertEdge(long long int a, long long int b) {     if (!edge[a]) {       edge[a] = new listNode(b);       tail[a] = edge[a];     } else {       tail[a]->next = new listNode(b);       tail[a] = tail[a]->next;     }   }   void dfs(long long int node, long long int parent, long long int &res0,            long long int &res1) {     long long int a, b, temp;     if (color[node]) {       res0 = 0LL;       res1 = 1LL;     } else {       res0 = 1LL;       res1 = 0LL;     }     for (listNode *ptr = edge[node]; ptr; ptr = ptr->next) {       if (ptr->n == parent) continue;       dfs(ptr->n, node, a, b);       if (color[node]) {         res1 *= (a + b);         if (res1 >= 1000000007) res1 %= 1000000007;       } else {         res1 *= (a + b);         if (res1 >= 1000000007) res1 %= 1000000007;         temp = res0 * b;         if (temp >= 1000000007) temp %= 1000000007;         res1 += temp;         if (res1 >= 1000000007) res1 %= 1000000007;         res0 *= (a + b);         if (res0 >= 1000000007) res0 %= 1000000007;       }     }   }   int main() {     long long int i, a, b;     scanf( %I64d , &num);     for (i = 1; i <= num - 1; i++) {       scanf( %I64d , &a);       insertEdge(i, a);       insertEdge(a, i);     }     for (i = 0; i < num; i++) scanf( %I64d , color + i);     dfs(0, 0, a, b);     printf( %I64d n , b);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         (i + j) % 2 ? cout <<  B  : cout <<  W ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s1;     int tc;     cin >> tc;     getchar();     while (tc--) {       cin >> s1;       for (int i = 0; i < s1.length(); i += 2) {         cout << s1[i];       }       cout << s1[s1.size() - 1] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long row1[n], row2[n];     for (int i = 0; i < n; i++) cin >> row1[i];     for (int i = 0; i < n; i++) cin >> row2[i];     long long first, second, pref = 0, pres = 0;     for (int i = 0; i < n; i++) {       first = max(pref, pres + row1[i]);       second = max(pres, pref + row2[i]);       pref = first;       pres = second;     }     cout << max(pref, pres);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int min(int a, int b) {     if (a > b)       return b;     else       return a;   }   int main() {     int tc;     cin >> tc;     while (tc--) {       long long int n;       cin >> n;       vector<int> a(n + 1, 0);       vector<int> ans(n + 1, 0);       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       vector<int> ones;       vector<int> zeros;       ;       string s;       cin >> s;       int one = 0;       for (int i = 0; i < n; i++) {         if (s[i] ==  1 ) {           ones.push_back(a[i + 1]);         } else           zeros.push_back(a[i + 1]);       }       int k = ones.size();       int max = n - k;       sort(ones.begin(), ones.end());       sort(zeros.begin(), zeros.end());       map<int, int> m;       map<int, int> p;       int j = n;       for (int i = k - 1; i >= 0; i--) {         m[ones[i]] = j;         j--;       }       for (int i = 0; i < max; i++) {         p[zeros[i]] = i + 1;       }       for (int i = 1; i <= n; i++) {         if (s[i - 1] ==  0 ) {           ans[i] = p[a[i]];         } else {           ans[i] = m[a[i]];         }       }       for (int i = 1; i <= n; i++) cout << ans[i] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 111;   const int INF = (int)1e9;   const int mod = (int)1e9 + 7;   int n, m;   vector<int> v[N];   int get(int i, int j) {     if (i > j) swap(i, j);     int s = j - i;     s = min(s, m - s);     return s;   }   int get_res(int x, int y) {     auto left = v[y].begin(), right = v[y].end();     --right;     int s = get(x, *left);     s = min(s, get(x, *right));     auto it = upper_bound(v[y].begin(), v[y].end(), x);     if (it != v[y].end()) {       s = min(s, get(x, *it));     }     if (it != v[y].begin()) {       --it;       s = min(s, get(x, *it));     }     return s;   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         char x;         x = getchar();         if (x ==  1 ) v[i].push_back(j);       }       scanf(  n );     }     int ans = (int)1e9;     for (int j = 0; j < m; ++j) {       int res = 0;       for (int i = 0; i < n; ++i) {         if (!v[i].size()) {           puts( -1 );           return 0;         }         res += get_res(j, i);       }       ans = min(ans, res);     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   int read() {     register int x = 0;     register char f = 1, ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = !f;     for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^  0 );     return f ? x : -x;   }   int n, fac[200005], dp[200005], sz[200005];   int edge, to[200005 << 1], pr[200005 << 1], hd[200005];   void addedge(int u, int v) { to[++edge] = v, pr[edge] = hd[u], hd[u] = edge; }   void dfs(int u, int fa = 0) {     sz[u] = 1, dp[u] = 1;     int d = 0;     for (register int i = hd[u], v; i; i = pr[i])       if ((v = to[i]) != fa)         dfs(v, u), sz[u] += sz[v], ++d, dp[u] = 1ll * dp[u] * dp[v] % 998244353;     if (!fa)       dp[u] = 1ll * n * dp[u] % 998244353 * fac[d] % 998244353;     else       dp[u] = 1ll * (d + 1) * dp[u] % 998244353 * fac[d] % 998244353;   }   int main() {     n = read();     for (register int i = 1, u, v; i < n; ++i)       u = read(), v = read(), addedge(u, v), addedge(v, u);     fac[0] = 1;     for (register int i = 1; i <= n; ++i)       fac[i] = 1ll * fac[i - 1] * i % 998244353;     dfs(1);     printf( %d n , dp[1]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 4;   int n, k, a[N];   bool used[N], inStack[N];   set<int> s;   set<int> q;   void add(int& it) {     if (s.size() && *s.begin() < a[it]) {       cout << -1;       exit(0);     }     inStack[a[it]] = 1;     used[a[it]] = 1;     s.insert(a[it]);     ++it;   }   void sovle() {     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       q.insert(i);     }     for (int i = 0; i < k; ++i) {       cin >> a[i];       used[a[i]] = 1;       q.erase(a[i]);     }     int it = 0;     for (int i = 1; i <= n;) {       if (used[i]) {         if (inStack[i]) {           int top = *s.begin();           if (top != i) {             cout << -1;             exit(0);           } else {             s.erase(top);             ++i;           }         } else {           add(it);         }       } else {         if (it < k) {           add(it);         } else {           if (s.empty()) {             a[it] = *q.rbegin();             add(it);             q.erase(*q.rbegin());           } else {             auto findIt = q.lower_bound(*s.begin());             if (findIt == q.begin()) {               cout << -1;               exit(0);             } else {               --findIt;               a[it] = *findIt;               add(it);               q.erase(findIt);             }           }         }       }     }     for (int i = 0; i < n; ++i) {       cout << a[i] <<    ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     sovle();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MAXN = 100;   int cnt[MAXN];   int pre[MAXN];   ll fact[MAXN];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     fact[0] = 1;     for (int i = 1; i < MAXN; i++) {       fact[i] = fact[i - 1] * i;     }     int kase;     cin >> kase;     while (kase--) {       memset(cnt, 0, sizeof(cnt));       memset(pre, 0, sizeof(pre));       ll n;       cin >> n;       int idx = 2;       while (n) {         cnt[n % idx]++;         n /= idx;         idx++;       }       pre[0] = cnt[0];       for (int i = 1; i <= idx; i++) {         pre[i] = pre[i - 1] + cnt[i];       }       ll same = 1;       for (int i = 0; i < idx; i++) {         same *= fact[cnt[i]];       }       ll ans = 1;       idx--;       for (int i = 1; i < idx; i++) {         ans *= (pre[i] - i + 1);       }       ll cost = pre[0];       for (int i = 0; i < idx; i++) {         pre[i]--;       }       for (int i = 1; i < idx - 1; i++) {         cost *= (pre[i] - i + 1);       }       cout << (ans - cost) / same - 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int d[5001];   int t[5001];   int l[5001];   int r[5001];   int x[5001];   vector<int> cl, cr, num, cx;   int eksi[5001];   int ek[5001][5001];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m;     for (int i = 1; i <= n; i++) d[i] = 1000000000;     for (int ad = 1; ad <= m; ad++) {       cin >> t[ad] >> l[ad] >> r[ad] >> x[ad];       if (t[ad] == 1)         for (int i = l[ad]; i <= r[ad]; i++) eksi[i] += x[ad];       else {         int ma = -1000000000;         for (int i = l[ad]; i <= r[ad]; i++) ma = max(ma, d[i] + eksi[i]);         if (ma < x[ad]) {           cout <<  NO  << endl;           return 0;         } else           for (int i = l[ad]; i <= r[ad]; i++)             if (d[i] > x[ad] - eksi[i]) d[i] = x[ad] - eksi[i];       }     }     for (int i = 1; i <= n; i++) eksi[i] = d[i];     for (int i = 1; i <= m; i++) {       if (t[i] == 1) {         for (int j = l[i]; j <= r[i]; j++) eksi[j] += x[i];       } else if (t[i] == 2) {         int ma = -1000000000;         for (int j = l[i]; j <= r[i]; j++) ma = max(ma, eksi[j]);         if (x[i] != ma) {           cout <<  NO  << endl;           return 0;         }       }     }     cout <<  YES  << endl;     for (int i = 1; i <= n; i++) cout << d[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   inline void wrote(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) wrote(x / 10);     putchar(x % 10 +  0 );   }   inline void writeln(long long x) {     wrote(x);     puts(  );   }   const long long maxn = 111;   double p[maxn], win, tim;   long long n, a[maxn];   int main() {     n = read();     win = 1;     for (long long i = 1; i <= n; ++i) {       p[i] = (a[i] = read()) * 0.01;       win *= p[i];     }     tim = n;     for (long long it = 0; ((1 - win) > 1e-10) && (it <= 500000); it++) {       tim += 1 - win;       long long cho = 0;       double cho_v = 0;       for (long long i = 1; i <= n; ++i) {         double now_v = (1 - p[i]) * a[i] * 0.01 / p[i];         if (now_v > cho_v) cho_v = now_v, cho = i;       }       win = 1;       p[cho] += (1 - p[cho]) * a[cho] * 0.01;       for (long long i = 1; i <= n; ++i) win = win * p[i];     }     printf( %lf n , tim);   }


#include <bits/stdc++.h>   using namespace std;   char g[8][8];   int calc(int x, int y, int a, int d) {     int res = 0;     while (x != d) {       x += a;       ++res;       if (g[x][y] !=  . ) return 2e9;     }     return res;   }   int main() {     for (int i = 0; i < 8; ++i) scanf( %s , g[i]);     int a = 2e9, b = 2e9;     for (int i = 0; i < 8; ++i)       for (int j = 0; j < 8; ++j) {         if (g[i][j] ==  W ) a = min(a, calc(i, j, -1, 0));         if (g[i][j] ==  B ) b = min(b, calc(i, j, 1, 7));       }     if (a <= b)       puts( A );     else       puts( B );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     long long n, d;     cin >> n >> d;     long long a[105] = {0};     long long pref[105] = {0};     for (long long i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] % 2 != 0) pref[i]++;       if (i != 1) pref[i] += pref[i - 1];     }     long long z = 0;     long long b[105] = {0};     for (long long i = 2; i < n; i++) {       if (i - pref[i] == pref[i]) {         z++;         b[z] = abs(a[i] - a[i + 1]);       }     }     sort(b + 1, b + 1 + z);     long long v = 1;     while (d >= 0) {       if (v > z) break;       if (d - b[v] >= 0) {         d -= b[v];         v++;       } else         break;     }     cout << v - 1;   }


#include<bits/stdc++.h>   #define ll long long int   #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);      using namespace std;   int main(){               ll t;               cin>>t;               while(t--){                           ll n,k1,k2,w,b;                           cin>>n>>k1>>k2;                           cin>>w>>b;                                                      int m=(k1+k2)/2;                           int c=((n*2)-(k1+k2))/2;                                                     if(m>=w && c>=b){                                       cout<< YES <<  n ;                           }else{                                       cout<< NO <<  n ;                           }               }   }