#include <bits/stdc++.h>   using namespace std;   struct Edge {     int u;     int v;     Edge() {}     Edge(int u, int v) : u(u), v(v) {}   };   struct WeightedEdge : Edge {     int w;   };   struct EdgeGroup {     int q;     vector<Edge> edges;     EdgeGroup() { q = -1; }     EdgeGroup(int q) : q(q) {}   };   const int MAX_M = 500005;   const int MAX_W = 500005;   const int MAX_N = 500005;   const int MAX_Q = 500005;   WeightedEdge raw_edge[MAX_M];   int vertex_set[MAX_N];   int cycle_set[MAX_N];   vector<Edge> edge[MAX_W];   vector<EdgeGroup> edge_group[MAX_W];   bool ans[MAX_Q];   int get_canonical(int disjoint_set[], int v) {     if (disjoint_set[v] == v) return v;     return disjoint_set[v] = get_canonical(disjoint_set, disjoint_set[v]);   }   bool join(int disjoint_set[], int u, int v) {     u = get_canonical(disjoint_set, u);     v = get_canonical(disjoint_set, v);     if (u == v) {       return false;     }     disjoint_set[v] = u;     return true;   }   bool check(const vector<Edge> &edges) {     vector<Edge> canonic_edges;     for (auto e : edges) {       int v = get_canonical(vertex_set, e.v);       int u = get_canonical(vertex_set, e.u);       cycle_set[v] = v;       cycle_set[u] = u;       canonic_edges.emplace_back(u, v);     }     for (auto e : canonic_edges) {       if (!join(cycle_set, e.v, e.u)) return false;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       vertex_set[i] = i;     }     for (int i = 1; i <= m; ++i) {       cin >> raw_edge[i].u >> raw_edge[i].v >> raw_edge[i].w;       edge[raw_edge[i].w].emplace_back(raw_edge[i].v, raw_edge[i].u);     }     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       ans[i] = true;       int k;       cin >> k;       for (int j = 0; j < k; ++j) {         int edge_id;         cin >> edge_id;         int w = raw_edge[edge_id].w;         if (edge_group[w].empty() || edge_group[w].back().q != i) {           edge_group[w].emplace_back(i);         }         edge_group[w].back().edges.push_back(raw_edge[edge_id]);       }     }     for (int w = 1; w < MAX_W; ++w) {       for (auto group : edge_group[w]) {         ans[group.q] &= check(group.edges);       }       for (auto e : edge[w]) {         join(vertex_set, e.v, e.u);       }     }     for (int i = 0; i < q; ++i) {       cout << (ans[i] ?  YES  :  NO ) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int s = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();     return s * w;   }   struct Edge {     int to, next, v, c;   } a[40001 << 7];   int h[40001], cnt = 1;   void add(int x, int y, int z, int w) {     a[++cnt] = (Edge){y, h[x], z, w}, h[x] = cnt,     a[++cnt] = (Edge){x, h[y], 0, -w}, h[y] = cnt;   }   int S, T, n, m, p[40001], d[40001];   int flow, val;   bool v[40001];   bool Spfa() {     std::queue<int> q;     memset(v, 0, sizeof(v));     memset(p, 0, sizeof(p));     memset(d, 0x3f, sizeof(d));     q.push(S), d[S] = 0, v[S] = 1;     while (!q.empty()) {       int x = q.front();       q.pop();       v[x] = 0;       for (int i = h[x]; i; i = a[i].next)         if (a[i].v && d[x] + a[i].c < d[a[i].to]) {           d[a[i].to] = d[x] + a[i].c, p[a[i].to] = i;           if (!v[a[i].to]) v[a[i].to] = 1, q.push(a[i].to);         }     }     if (d[T] == 1061109567) return 0;     return 1;   }   void Adjust() {     int minf = 0x7fffffff, x = T;     for (int i = T; i != S; i = a[p[i] ^ 1].to) minf = min(minf, a[p[i]].v);     for (int i = T; i != S; i = a[p[i] ^ 1].to)       a[p[i]].v -= minf, a[p[i] ^ 1].v += minf;     flow += minf, val += d[T] * minf;   }   int Edmonds_Karp() {     flow = val = 0;     while (Spfa()) Adjust();     return val;   }   int du[110];   signed main() {     int x, y, c, f, ans = 0;     n = read(), m = read(), add(n, 1, 0x7fffffff, 0), S = n + 1, T = n + 2;     for (int i = 1; i <= m; i++) {       x = read(), y = read(), c = read(), f = read();       if (c < f)         ans += f - c, add(y, x, f - c, 0), add(y, x, c, 1),             add(x, y, 0x7fffffff, 2);       else         add(x, y, c - f, 1), add(x, y, 0x7fffffff, 2), add(y, x, f, 1);       du[x] -= f, du[y] += f;     }     for (int i = 1; i <= n; i++)       if (du[i] > 0)         add(S, i, du[i], 0);       else         add(i, T, -du[i], 0);     cout << ans + Edmonds_Karp();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 100 * 1000 + 5;   const int mod = 1000 * 1000 * 1000;   int a[maxN];   int n;   string make(string s, int t) {     string res;     res.resize(n);     for (int i = t; i < n; i++) res[i] = s[i - t];     for (int i = 0; i < t; i++) res[i] = s[i + n - t];     return res;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     string s;     cin >> s;     vector<string> res;     for (int j = 0; j < 10; j++) {       for (int i = 0; i < n; i++)         if (s[i] ==  9 )           s[i] =  0 ;         else           s[i]++;       for (int i = 0; i < n; i++) res.push_back(make(s, i));     }     sort(res.begin(), res.end());     cout << res[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int M = 10005;   inline int read() {     int ret = 0, f = 0;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = 1;       c = getchar();     }     while (isdigit(c)) {       ret = ret * 10 + c - 48;       c = getchar();     }     if (f) return -ret;     return ret;   }   int T, mx, s, t, cnt, size, hh, n, m, x, y, sum, k, f[M], ha[M], v[M],       a[105][2005];   struct node {     int x, y;   } xu[N];   bool cmp(node a, node b) { return a.x < b.x; }   signed main() {     int xjh = 0;     T = read();     while (T--) {       cnt = 0;       n = read();       m = read();       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) {           xjh++;           a[i][j] = read();           xjh++;           xu[++cnt].x = a[i][j];           xu[cnt].y = j;           xjh++;           a[i + n][j] = a[i][j];         }       size = 0;       sort(xu + 1, xu + cnt + 1, cmp);       for (int i = cnt; i; i--) {         if (v[xu[i].y] == 0) {           size++;           xjh++;           v[xu[i].y] = 1;           xjh++;         }         if (size >= m) break;       }       mx = 1 << n;       for (int j = 1; j <= m; j++)         if (v[j] != 0) {           for (int s = 0; s < mx; s++) {             xjh++;             xjh++;             xjh--;             ha[s] = 0;             for (int i = 1; i <= n; i++) {               sum = 0;               xjh++;               for (int k = i; k < n + i; k++) {                 x = k - i;                 if (s & (1 << x)) sum = sum + a[k][j];               }               xjh++;               xjh++;               xjh--;               ha[s] = max(ha[s], sum);               xjh++;               xjh++;               xjh--;             }           }           for (int s = mx - 1; s >= 0; s--) {             for (int t = s; t; t = (t - 1) & s) {               xjh++;               f[s] = max(f[s], f[s ^ t] + ha[t]);               xjh++;               xjh++;               xjh--;             }           }         }       xjh++;       xjh++;       xjh--;       printf( %d n , f[mx - 1]);       xjh++;       xjh++;       xjh--;       for (int i = 0; i < mx; i++) f[i] = 0;       for (int j = 1; j <= m; j++) v[j] = 0;       xjh++;       xjh++;       xjh--;     }   }


#include <bits/stdc++.h>   using namespace std;   double n, m, k, r;   double f(int x, int y) {     x++;     y++;     return (min(n + 1, x + r) - max((double)x, r)) *            (min(m + 1, y + r) - max((double)y, r));   }   int main() {     cin >> n >> m >> r >> k;     double ans = 0;     double nbpos = (n - r + 1) * (m - r + 1);     int tot = 0;     priority_queue<pair<double, pair<int, int> > > q;     pair<double, pair<int, int> > cur;     q.push(make_pair(f(n / 2, m / 2), make_pair(n / 2, m / 2)));     set<pair<int, int> > done;     done.insert(make_pair(n / 2, m / 2));     pair<int, int> pos;     while (tot < k) {       cur = q.top();       pos = cur.second;       q.pop();       ans += cur.first;       if (pos.first - 1 >= 0 &&           done.find(make_pair(pos.first - 1, pos.second)) == done.end()) {         q.push(make_pair(f(pos.first - 1, pos.second),                          make_pair(pos.first - 1, pos.second)));         done.insert(make_pair(pos.first - 1, pos.second));       }       if (pos.first + 1 < n &&           done.find(make_pair(pos.first + 1, pos.second)) == done.end()) {         q.push(make_pair(f(pos.first + 1, pos.second),                          make_pair(pos.first + 1, pos.second)));         done.insert(make_pair(pos.first + 1, pos.second));       }       if (pos.second - 1 >= 0 &&           done.find(make_pair(pos.first, pos.second - 1)) == done.end()) {         q.push(make_pair(f(pos.first, pos.second - 1),                          make_pair(pos.first, pos.second - 1)));         done.insert(make_pair(pos.first, pos.second - 1));       }       if (pos.second + 1 < m &&           done.find(make_pair(pos.first, pos.second + 1)) == done.end()) {         q.push(make_pair(f(pos.first, pos.second + 1),                          make_pair(pos.first, pos.second + 1)));         done.insert(make_pair(pos.first, pos.second + 1));       }       tot++;     }     cout << setprecision(11) << ans / nbpos << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, c, e;     char s[100];     scanf( %d%d%*d.%d , &n, &m, &c);     map<string, int> u;     for (int i = 0; i < n; i++) {       scanf( %s%d , s, &e);       if (e * c / 100 >= 100) u[s] = e * c / 100;     }     for (int i = 0; i < m; i++) {       scanf( %s , s);       u[s];     }     printf( %d n , int(u.size()));     for (auto& x : u) printf( %s %d n , x.first.c_str(), x.second);   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a[1000100], b[1000100], ans;   stack<int> st;   int n, m;   bool del[1000100], used[1001000];   int main() {     scanf( %d%d , &n, &m);     int i;     for (i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       a[u].push_back(v);       b[v].push_back(u);     }     memset(del, 0, sizeof(del));     for (i = 1; i <= n; i++) {       if (del[i]) continue;       del[i] = 1;       st.push(i);       int len = a[i].size(), j;       for (j = 0; j < len; j++) del[a[i][j]] = 1;     }     memset(used, 0, sizeof(used));     while (!st.empty()) {       int u = st.top();       st.pop();       used[u] = 1;       int j, len = b[u].size();       for (j = 0; j < len; j++)         if (used[b[u][j]]) {           used[u] = 0;           break;         }       if (used[u]) ans.push_back(u);     }     int len = ans.size();     printf( %d n , ans.size());     for (i = 0; i < len; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   long long i, j, n;   int ar[N];   int main() {     for (int i = 2; i < N; i++) {       for (int j = i + i; j < N; j += i) {         ar[j] += j / i;       }     }     cin >> n;     long long sum = 0;     for (int i = 2; i <= n; i++) sum += ar[i];     cout << 4 * sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int trie[5000005][2];   int n, cnt;   inline int read() {     int t = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       t = (t << 3) + (t << 1) + (ch ^ 48);       ch = getchar();     }     return t;   }   inline void write(int t) {     if (t > 9) {       write(t / 10);     }     putchar(t % 10 +  0 );   }   inline void insert(int t) {     int pos = 0;     for (int i = 29; i >= 0; i--) {       int x = (t >> i) & 1;       if (!trie[pos][x]) {         trie[pos][x] = ++cnt;       }       pos = trie[pos][x];     }   }   inline int solve(int t, int cur) {     if (t == -1) {       return 0;     }     if (!trie[cur][0]) {       return solve(t - 1, trie[cur][1]);     } else if (!trie[cur][1]) {       return solve(t - 1, trie[cur][0]);     } else {       return (1 << t) +              min(solve(t - 1, trie[cur][0]), solve(t - 1, trie[cur][1]));     }   }   int main() {     n = read();     while (n--) {       insert(read());     }     write(solve(29, 0));     putchar(10);     return 0;   }


#include <bits/stdc++.h>   inline long long read() {     long long x = 0;     char ch = getchar(), w = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * w;   }   void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   inline void writeln(long long x) {     write(x);     puts(  );   }   using namespace std;   const int N = 3e5 + 666;   long long a[N], n;   const long long mo = 998244353;   long long ksm(long long x, long long y) {     long long res = 1;     for (; y; y >>= 1, x = x * x % mo)       if (y & 1) res = res * x % mo;     return res;   }   int main() {     n = read();     long long inv = ksm(100, mo - 2);     for (int i = 1; i <= n; ++i) a[i] = read() * inv % mo;     long long e1 = 0, s = 1;     for (int i = 1; i <= n; ++i) {       e1 += s;       s *= a[i] % mo;       s %= mo;       e1 %= mo;     }     e1 = e1 * ksm(s, mo - 2) % mo;     long long ans = 0;     for (int i = n; i >= 1; --i) {       ans = ans * a[i] % mo + (1 - a[i] + mo) * e1 % mo;       ans += 1;       ans %= mo;     }     writeln(ans);     return 0;   }


#include <bits/stdc++.h>   #define mid (tl+tr)/2   #define mod 1000000007   #define fain(arr) for(ll i=0;i<n;i++)cin>>arr[i];   #define all(x) x.begin(),x.end()   #define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);   #define bugv(n1) if(DEBUG)cout<<#n1<<  is  <<n1<<  n ;   #define buga(A) cout<<#A<<  is : <<endl ; for(auto x:A)cout<<x<<   ; cout<<endl;   #define FILE freopen( input.txt , r ,stdin);   #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))   #define endl   n    #define ll long long    #define pii pair<ll,ll>   #define pll pair<long long ,long long >   #define pi acos(-1)   #define sz(x) ((ll)x.size())   #define clr(x) memset(x, 0, sizeof(x))   #define init(x, a) memset(x, a, sizeof(x))   #define DEBUG true   using namespace std;      int main(){    // FILE;    SPEED;    ll t;    cin>>t;    while(t--){          int n,m;          cin>>n>>m;             char arr[n][m];             for(int i=0;i<n;i++){           for(int j=0;j<m;j++){            cin>>arr[i][j];           }          }                int left[n][m];          int right[n][m];          int up[n][m];             for(int i=0;i<n;i++){            for(int j=m-1;j>=0;j--){              if(j==m-1){                right[i][j]=arr[i][j]== * ?1:0;              }              else{               if(arr[i][j]== * )               right[i][j]=right[i][j+1]+1;               else               right[i][j]=0;              }            }          }              for(int i=0;i<n;i++){            for(int j=0;j<m;j++){              if(j==0){                left[i][j]=arr[i][j]== * ?1:0;              }              else{               if(arr[i][j]== * )               left[i][j]=left[i][j-1]+1;               else               left[i][j]=0;              }            }          }          int ans=0;          for(int i=0;i<n;i++){            for(int j=0;j<m;j++){              if(i==0){                up[i][j]=arr[i][j]== * ?1:0;              }              else{               up[i][j]=min({left[i][j],right[i][j],1+up[i-1][j]});              }              ans+=up[i][j];            }          }             cout<<ans<<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100;   int n;   int t[2][N];   vector<int> vals[N];   vector<pair<int, int>> G[N];   bool vis[N];   int color[N];   vector<int> C[2];   bool cycle;   void dfs(int v) {     vis[v] = true;     C[color[v]].push_back(v);     for (auto it : G[v]) {       if (vis[it.first] && (color[v] ^ it.second) != color[it.first])         cycle = true;       if (!vis[it.first]) {         color[it.first] = color[v] ^ it.second;         dfs(it.first);       }     }   }   void solve() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) G[i].clear(), vals[i].clear();     for (int j = 0; j <= 1; ++j)       for (int i = 1; i <= n; ++i) {         scanf( %d , &t[j][i]);         vals[t[j][i]].push_back(i);       }     for (int i = 1; i <= n; ++i) {       if ((int)vals[i].size() != 2) {         printf( -1 n );         return;       }       int u = vals[i][0], v = vals[i][1], w;       if (u == v) continue;       w = ((t[0][u] == t[0][v] && t[0][u] == i) ||            (t[1][u] == t[1][v] && t[1][u] == i));       G[u].push_back(make_pair(v, w));       G[v].push_back(make_pair(u, w));     }     cycle = false;     for (int i = 1; i <= n; ++i) vis[i] = false;     vector<int> ans;     for (int i = 1; i <= n; ++i)       if (!vis[i]) {         for (int j = 0; j <= 1; ++j) C[j].clear();         dfs(i);         if ((int)C[0].size() > (int)C[1].size()) swap(C[0], C[1]);         for (auto it : C[0]) ans.push_back(it);       }     if (cycle) printf( -1 n );     printf( %d n , (int)ans.size());     for (auto it : ans) printf( %d  , it);     printf(  n );   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const double PI = 2.0 * acos(0.0);   const int MAXN = 33;   int n, m;   string s;   bool d1[26];   bool d2[700];   int encode(char x, char y) { return (x -  a ) * 26 + (y -  a ); }   int main() {     memset(d1, false, sizeof(d1));     memset(d2, false, sizeof(d2));     cin >> n;     for (int i = 0, _n = (n); i < _n; i++) {       cin >> s;       m = s.length();       for (int j = 0, _n = (m); j < _n; j++) {         d1[s[j] -  a ] = true;         if (j + 1 < m) d2[encode(s[j], s[j + 1])] = true;       }     }     char r1, r2;     int x;     for (int i = 0, _n = (26); i < _n; i++) {       if (!d1[i]) {         r1 =  a  + i;         cout << r1 << endl;         return 0;       }     }     for (int i = 0, _n = (700); i < _n; i++) {       if (!d2[i]) {         r1 =  a  + (i / 26);         r2 =  a  + (i % 26);         cout << r1 << r2 << endl;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     if (k > n)       cout << k;     else       for (int i = 2;; i++) {         if (k * i > n) {           cout << k * i;           break;         }       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, m, t;     scanf( %d %d %d %d , &n, &k, &m, &t);     for (int i = 1; i <= t; ++i) {       int x, y;       scanf( %d %d , &x, &y);       if (x) {         if (y <= k) {           k++;         }         n++;         printf( %d %d n , n, k);       } else {         if (y < k) {           k -= y;           n -= y;         } else {           n -= (n - y);         }         printf( %d %d n , n, k);       }     }   }


#pragma GCC optimize(2)   #include <bits/stdc++.h>   #define all(n) (n).begin(), (n).end()   #define se second   #define fi first   #define pb push_back   #define mp make_pair   #define sqr(n) (n) * (n)   #define rep(i, a, b) for (int i = (a); i <= (b); ++i)   #define per(i, a, b) for (int i = (a); i >= (b); --i)   #define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef pair<int, int> PII;   typedef pair<ll, ll> PLL;   typedef vector<int> VI;   typedef double db;       template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }   template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }   template<class T> void clear(T& a) { T().swap(a); }       const int N = 2e5 + 5;       int n, m, _, k;   ll a[N];       int main() {    IOS;    for (cin >> _; _; --_) {     cin >> n; ll mx = -1, s = 0;           rep (i, 1, n) cin >> a[i], umax(mx, a[i]), s += a[i];           ll ans = mx * (n - 1ll) - s;           if (ans < 0) ans = (ans % (n - 1) + (n - 1)) % (n - 1);           cout << ans <<   n ;       }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   using namespace std;   int n, m;   string s;   int prime(int x) {     if (x == 1) return 0;     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return 0;     return 1;   }   int check(int x, int y) {     if (x > n || x < 1 || y >= m || y < 0) return 0;     return 1;   }   long long gcd(long long a, long long b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   int main() {     cin >> s;     if (s.size() <= 0) {       string t = s;       reverse(t.begin(), t.end());       string k = s + t;       cout << k;       return 0;     }     for (int i = 0; i < s.size(); i++) {       string t =   , k =   ;       for (int j = 0; j < i; j++) t += s[j];       for (int j = i; j < s.size(); j++) k += s[j];       string t1 =   ;       if (k.size() >= t.size()) {         reverse(k.begin(), k.end());         for (int j = k.size() - t.size(); j < k.size(); j++) t1 += k[j];         if (t1 == t) {           string ans = k;           reverse(k.begin(), k.end());           ans += k;           cout << ans;           return 0;         }       } else if (t.size() >= k.size()) {         reverse(t.begin(), t.end());         for (int j = 0; j < t.size() - k.size(); j++) t1 += t[j];         if (t1 == k) {           string ans = t;           reverse(t.begin(), t.end());           ans += t;           cout << ans;           return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     cout.tie(nullptr);     long long int n, m, i, j, x;     cin >> n >> m;     long long int a[n];     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (i = 0; i < m; i++) {       cin >> x;       cout << upper_bound(a, a + n, x) - a <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s = 86400;     long long t, a;     cin >> n >> t;     for (int i = 1; i <= n; i++) {       cin >> a;       if (s - a >= t) {         cout << i;         break;       } else         t -= s - a;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize  -O3    using ll = long long;   using ull = unsigned long long;   using ld = long double;   using namespace std;   const int MX = 7007;   ll f[MX];   bool better(ll x, ll y) { return ((x ^ y) & x) != 0; }   ll x[MX], y[MX];   bool ok[MX];   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n;     cin >> n;     vector<pair<ll, ll>> a;     map<ll, int> cnt;     map<ll, ll> sum;     for (int i = 0; i < n; i++) {       cin >> x[i];     }     for (int i = 0; i < n; i++) {       cin >> y[i];     }     for (int i = 0; i < n; i++) {       a.emplace_back(x[i], y[i]);       cnt[x[i]]++;       sum[x[i]] += y[i];     }     sort(a.begin(), a.end());     for (int i = n - 1; i >= 0; i--) {       if (cnt[a[i].first] >= 2) {         ok[i] = true;       }       for (int j = i + 1; j < n; j++) {         if (!better(a[i].first, a[j].first) && ok[j]) {           ok[i] = true;         }       }     }     ll ans = 0;     for (int i = 0; i < n; i++) {       if (ok[i]) {         ans += a[i].second;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int32_t> dp(1, 0);   void solve() {     int64_t n;     cin >> n;     int64_t x3 = 0, x2 = 0;     while (n % 3 == 0 && n != 1) {       n /= 3;       x3++;     }     while (n % 2 == 0 && n != 1) {       n /= 2;       x2++;     }     if (x3 >= x2 && n == 1) {       cout << x3 + (x3 - x2) <<   n ;     } else {       cout << -1 <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t_;     cin >> t_;     while (t_--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 20010;   int lf[max_n];   int rf[max_n];   int main() {     long long sum = 0;     int n;     int x, y;     memset(lf, 0, sizeof(lf));     memset(rf, 0, sizeof(rf));     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x, &y);       ++lf[y - x + 1000];       ++rf[x + y];     }     for (int i = 0; i <= 2050; ++i) {       sum += (lf[i] * (lf[i] - 1)) / 2;     }     for (int i = 0; i <= 2050; ++i) {       sum += (rf[i] * (rf[i] - 1)) / 2;     }     printf( %I64d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int r, j, l, x, y, m, c, n, s, f, q, i, z, p, k, d, t, u, e, g, w;   string s1, s2, s3, s4, str;   vector<long long int> v;   int main() {     cin >> n >> x;     long long int a[n];     for (long long int i = 0; i < n; i++) {       cin >> a[i];       v.push_back(a[i]);     }     k = 0;     double ans = 0.000;     for (long long int i = 0; i < n - 2; i++) {       p = a[i] + x;       l = lower_bound(v.begin(), v.end(), p) - v.begin();       if (v[l] != p) l--;       if (l <= i) continue;       double ans2 =           (((v[l] - a[i + 1]) * 1.000) / ((v[l] - a[i]) * 1.000) * 1.000);       if (ans2 - ans >= 1e-9) {         k = 1;         ans = ans2;       }     }     if (k == 0)       cout << -1;     else       printf( %.9f , ans);   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long maxn = 2001;   long long h[maxn], dp[maxn][2 * maxn];   signed main() {     long long n, k;     scanf( %lld%lld , &n, &k);     for (long long i = 1; i <= n; i++) scanf( %lld , &h[i]);     dp[0][2001] = 1;     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j < 2 * maxn; j++)         if (h[i] == h[i % n + 1])           dp[i][j] = k * dp[i - 1][j] % mod;         else           dp[i][j] = (dp[i - 1][j + 1] + dp[i - 1][j - 1] +                       (k - 2) * dp[i - 1][j] % mod) %                      mod;     }     long long ans = 0;     for (long long i = 1; i < maxn; i++) ans = (ans + dp[n][maxn + i]) % mod;     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 900;   struct edge {     int v, w, b;     double c;   };   vector<edge> adj[maxn];   int n, k;   int s, t;   double dis[maxn];   int bk[maxn], bki[maxn];   bool inq[maxn];   long long x[maxn], y[maxn];   inline void ae(int u, int v, int w, double c) {     edge nu, nv;     nu.v = v;     nu.w = w;     nu.c = c;     nu.b = adj[v].size();     nv.v = u;     nv.w = 0;     nv.c = -c;     nv.b = adj[u].size();     adj[u].push_back(nu);     adj[v].push_back(nv);   }   inline pair<int, double> spfa() {     for (int i = 0; i < maxn; i++) dis[i] = 1e30;     memset(inq, 0, sizeof(inq));     dis[s] = 0;     queue<int> q;     q.push(s);     while (!q.empty()) {       int u = q.front();       q.pop();       inq[u] = 0;       for (int i = adj[u].size() - 1; i >= 0; i--) {         int v = adj[u][i].v;         int w = adj[u][i].w;         double c = adj[u][i].c;         if (dis[u] + c < dis[v] && w != 0) {           dis[v] = dis[u] + c;           bk[v] = u;           bki[v] = i;           if (!inq[v]) {             q.push(v);             inq[v] = 1;           }         }       }     }     if (dis[t] > 1e20) return make_pair(-1, 0.0);     int cur = t;     int flw = 0x3f3f3f3f;     while (cur != s) {       flw = min(flw, adj[bk[cur]][bki[cur]].w);       cur = bk[cur];     }     cur = t;     double cst = 0;     while (cur != s) {       adj[bk[cur]][bki[cur]].w -= flw;       adj[cur][adj[bk[cur]][bki[cur]].b].w += flw;       cst += flw * adj[bk[cur]][bki[cur]].c;       cur = bk[cur];     }     return make_pair(flw, cst);   }   inline pair<int, double> ek() {     pair<int, double> ans = make_pair(0ll, 0.0);     while (1) {       pair<int, double> cr = spfa();       if (cr.first <= 0) return ans;       ans.first += cr.first;       ans.second += cr.second;     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x[i] >> y[i];     }     s = n + n + 1;     t = n + n + 3;     for (int i = 1; i <= n; i++) {       ae(s, i, 2, 0.0);       ae(i + n, t, 1, 0.0);     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         if (y[i] > y[j])           ae(i, j + n, 1,              sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));       }     }     pair<int, double> ans = ek();     if (ans.first != n - 1)       cout << -1;     else       printf( %.15lf , ans.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int st = 0;     cin >> s;     int cnt[4] = {0};     for (int i = 0; i < s.size(); i++) {       if ( 1  <= s[i] && s[i] <=  3 ) {         ++cnt[s[i] -  0 ];       }     }     for (int i = 1; i <= 3; i++) {       for (int j = 0; j < cnt[i]; j++) {         if (st) {           cout <<  + ;         } else {           st = 1;         }         cout << i;       }     }     return 0;   }


#include <iostream>   #include <cstdio>   #include <algorithm>   #include <cstring>   #include <cmath>   #include <vector>   #include <map>   #include <queue>   #define int long long   #define mod 998244353   using namespace std;   int n,c[505],a[505],f[505][505],pos,ans;   char s[15];   int dfs(int now,int nowd){    if (now>n)return 1;    if (now==pos)return dfs(now+1,nowd+1);    if (f[now][nowd]!=-1)return f[now][nowd];    int ans=0;    if (now<pos)ans=dfs(now+1,nowd)+dfs(now+1,max(0ll,nowd+a[now]));    else{     ans=dfs(now+1,nowd);     if (nowd+a[now]>0)ans+=dfs(now+1,nowd+a[now]);    }    ans%=mod;    return f[now][nowd]=ans;   }   signed main(){    cin>>n;    for (int i=1;i<=n;i++){     scanf( %s ,s+1);     if (s[1]== - )c[i]=-1;     else scanf( %lld ,&c[i]);    }    for (int i=1;i<=n;i++){     if (c[i]==-1)continue;     for (int j=1;j<=n;j++){      if (c[j]==-1)a[j]=-1;      else if (c[j]<c[i]||(c[j]==c[i]&&j<i))a[j]=1;      else a[j]=0;     }     memset(f,-1,sizeof(f));     pos=i;     ans=(ans+c[i]*dfs(1,0))%mod;    }    cout<<ans<<endl;    return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int N, arr[1000000];   set<int> st;   int main() {     cin >> N;     if (N % 2 == 0) {       cout <<  NO ;       return 0;     }     for (int i = 1, b = 1, e = N * 2; i <= N; b += 2, e -= 2)       arr[i] = b, i++, arr[i] = e, i++;     for (int i = N + 1; i <= N * 2;) {       arr[i] = arr[i - N] + 1;       i++;       arr[i] = arr[i - N] - 1;       i++;     }     cout <<  YES n ;     for (int i = 1; i <= 2 * N; i++) cout << arr[i] <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const double eps = 1e-8;   int N;   long long K;   double pi;   int X[maxn], Y[maxn];   struct Seg {     double radl, radr;     void correct() {       if (radl > 2 * pi) radl -= 2 * pi;       if (radr > 2 * pi) radr -= 2 * pi;       if (radl < 0) radl += 2 * pi;       if (radr < 0) radr += 2 * pi;       if (radl > radr) swap(radl, radr);     }     bool operator<(const Seg& rhs) const { return radl < rhs.radl; }   } ses[maxn];   double rads[maxn * 2];   unordered_map<double, int> mp;   int c[maxn * 2];   void add(int pos, int val) {     while (pos <= N * 2) {       c[pos] += val;       pos += (pos & (-pos));     }   }   int sum(int pos) {     int res = 0;     while (pos) {       res += c[pos];       pos -= (pos & (-pos));     }     return res;   }   int main() {     scanf( %d%lld , &N, &K);     long long ALL = 1ll * N * (N - 1) / 2;     pi = acos(-1);     for (int i = 1; i <= N; i++) scanf( %d%d , &X[i], &Y[i]);     double mxr = 1e5, mir = 0;     while (mxr - mir > eps) {       double mid = (mxr + mir) / 2.0;       int rcnt = 0, rscnt = 0;       mp.clear();       memset(c, 0, sizeof(c));       for (int i = 1; i <= N; i++) {         double dis = sqrt(X[i] * X[i] + Y[i] * Y[i]);         if (dis <= mid) continue;         double ang = atan2(Y[i], X[i]) + pi;         double dan = acos(mid / dis);         ses[++rcnt] = Seg{ang + dan, ang - dan};         ses[rcnt].correct();         rads[++rscnt] = ses[rcnt].radl;         rads[++rscnt] = ses[rcnt].radr;       }       sort(ses + 1, ses + rcnt + 1);       sort(rads + 1, rads + rscnt + 1);       for (int i = 1; i <= rscnt; i++) mp[rads[i]] = i;       long long cnt = 0;       for (int i = 1; i <= rcnt; i++) {         cnt += sum(mp[ses[i].radr] - 1) - sum(mp[ses[i].radl]);         add(mp[ses[i].radr], 1);       }       if (ALL - cnt >= K)         mxr = mid;       else         mir = mid;     }     printf( %.8lf n , mir);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   int a[maxn], b[maxn];   int n, k;   bool check(int x, int state) {     if (state == 1) {       int c = 0, right = 0;       for (int i = 1; i <= n; i++) {         if (a[i] <= x) {           right = i;           c++;           i++;         }       }       if (c == k - k / 2 && right == n && k % 2 == 0) return false;       return c >= k - k / 2;     } else {       int c = 0, right = 0;       for (int i = 2; i <= n; i++) {         if (a[i] <= x) {           right = i;           c++;           i++;         }       }       if (c == k / 2 && right == n && k % 2 != 0) return false;       return c >= k / 2;     }   }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b + 1, b + n + 1);     int l = 1, r = n;     while (l < r) {       int mid = r + l >> 1;       if (check(b[mid], 0) || check(b[mid], 1))         r = mid;       else         l = mid + 1;     }     cout << b[l];     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   template <class T>   inline void amin(T &x, const T &y) {     if (y < x) x = y;   }   template <class T>   inline void amax(T &x, const T &y) {     if (x < y) x = y;   }   template <class Iter>   void rprintf(const char *fmt, Iter begin, Iter end) {     for (bool sp = 0; begin != end; ++begin) {       if (sp)         putchar(   );       else         sp = true;       printf(fmt, *begin);     }     putchar(  n );   }   int N;   int dp[2011];   int prv[2011];   bool tar[1011];   bool cur[1011];   int calc(int a) {     memset(dp, 0xc0, sizeof dp);     dp[0] = 0;     for (int i = 0, i_len = (N); i < i_len; ++i) {       for (int j = 1; j < a; j++) {         if (dp[i + j + 1] < dp[i] + j) {           dp[i + j + 1] = dp[i] + j;           prv[i + j + 1] = i;         }       }     }     return dp[N] - a + 1;   }   void query(vector<int> v) {     printf( %d , (int)v.size());     for (int i = 0, i_len = (v.size()); i < i_len; ++i) printf(  %d , v[i] + 1);     printf(  n );     fflush(stdout);     for (__typeof((v).begin()) e = (v).begin(), e_end = (v).end(); e != e_end;          ++e)       cur[*e] = true;     int x;     scanf( %d , &x);     x--;     for (int i = 0, i_len = (v.size()); i < i_len; ++i) cur[(x + i) % N] = false;   }   void solve() {     int ma = 0, a = 0;     for (int b = 1; b < N; b++) {       int tmp = calc(b);       if (ma < tmp) {         ma = tmp;         a = b;       }     }     if (ma == 0) {       puts( 0 );       fflush(stdout);     } else {       calc(a);       int x = N;       memset(tar, 0, sizeof tar);       memset(cur, 0, sizeof cur);       while (x) {         for (int k = prv[x]; k < x - 1; k++) tar[k] = true;         x = prv[x];       }       while (1) {         vector<int> v;         for (int i = 0, i_len = (N); i < i_len; ++i)           if (tar[i] && !cur[i]) {             v.push_back(i);           }         if ((int)v.size() <= a) {           for (int i = 0, i_len = (N); i < i_len; ++i)             if (tar[i] && cur[i]) {               if ((int)v.size() == a) break;               v.push_back(i);             }           query(v);           break;         } else {           v.resize(a);           query(v);         }       }       puts( 0 );       fflush(stdout);     }   }   void MAIN() {     scanf( %d , &N);     solve();   }   int main() {     int TC = 1;     for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int N = 1e6 + 5;   long long dp[105], cnt[105], b[N], a[N];   void add(long long &a, long long b) {     a += b;     if (a >= mod) a -= mod;   }   struct Mat {     long long t[101][101];     Mat() { memset(t, 0, sizeof t); }   } I;   int m;   Mat operator*(Mat a, Mat b) {     Mat c;     for (int i = 0; i < m; i++)       for (int j = 0; j < m; j++) {         long long t = 0;         for (int k = 0; k < m; k++) {           add(c.t[i][j], a.t[i][k] * b.t[k][j] % mod);         }       }     return c;   }   Mat Pow(Mat a, int b) {     Mat c = I;     while (b) {       if (b & 1) c = c * a;       a = a * a;       b >>= 1;     }     return c;   }   int main() {     int n, l, x;     cin >> n >> l >> m;     for (int i = 0; i < m; i++) dp[i] = 0, cnt[i] = 0;     for (int i = 1; i <= n; i++) scanf( %d , &x), dp[x % m]++;     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), cnt[a[i] % m]++;     for (int i = 1; i <= n; i++) scanf( %lld , &b[i]);     for (int i = 0; i < m; i++) I.t[0][i] = dp[i];     Mat pi;     for (int i = 0; i < m; i++)       for (int j = 0; j < m; j++) pi.t[i][j] = cnt[(m - i + j) % m];     Mat ans = Pow(pi, l - 2);     for (int i = 0; i < m; i++) cnt[i] = 0, dp[i] = 0;     for (int i = 1; i <= n; i++) {       cnt[(a[i] + b[i]) % m]++;     }     for (int i = 0; i < m; i++)       for (int j = 0; j < m; j++)         add(dp[(j + i) % m], ans.t[0][j] * cnt[i] % mod);     printf( %lld n , dp[0]);   }


#include <bits/stdc++.h>   using namespace std;   long long int variables[3001][3001];   long long int cost[3001][3001];   int n;   long long int a[3001];   void inicjuj() {     for (int i = 0; i < n; ++i) {       variables[0][i] = a[i] - i;     }     sort(variables[0], variables[0] + n);     for (int i = 0; i < n; ++i) cost[0][i] = abs(variables[0][i] - a[0]);   }   void __built_column(int X) {     long long int minpref = 1e18;     for (int j = 0; j < n; ++j) {       minpref = min(minpref, cost[X - 1][j]);       variables[X][j] = variables[X - 1][j] + 1;       cost[X][j] = minpref + (long long int)abs(a[X] - variables[X][j]);     }   }   long long int oblicz() {     for (int i = 1; i < n; ++i) {       __built_column(i);     }     long long int wyn = 1e18;     for (int j = 0; j < n; ++j) {       wyn = min(wyn, cost[n - 1][j]);     }     return wyn;   }   void wczytaj() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) cin >> a[i];   }   int main() {     wczytaj();     inicjuj();     cout << oblicz() << endl;   }


#include <bits/stdc++.h>   using namespace std;   namespace IO {   template <typename T>   inline void read(T &x) {     x = 0;     long long f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -f;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     x = x * f;   }   }  // namespace IO   using namespace IO;   const long long maxn = 200000;   long long dp[maxn + 10][4];   long long a[maxn + 10];   void DP(long long &x, long long y) { x = max(x, y); }   signed main() {     long long _;     read(_);     while (_--) {       long long n;       read(n);       for (long long i = 0; i <= n; i++) {         dp[i][0] = dp[i][1] = dp[i][2] = 0;         a[i] = 0;       }       for (long long i = 0; i < n; i++) {         read(a[i]);       }       for (long long i = 0; i < n; i++) {         if (i % 2 == 1) {           DP(dp[i + 1][0], dp[i][0]);           if (i + 2 <= n) DP(dp[i + 2][1], max(dp[i][1] + a[i], dp[i][0] + a[i]));           DP(dp[i + 1][2], max(dp[i][1], max(dp[i][0], dp[i][2])));         } else {           DP(dp[i + 1][0], dp[i][0] + a[i]);           if (i + 2 <= n) DP(dp[i + 2][1], max(dp[i][1], dp[i][0]) + a[i + 1]);           DP(dp[i + 1][2], max(dp[i][1], max(dp[i][0], dp[i][2])) + a[i]);         }       }       cout << max(dp[n][1], max(dp[n][2], dp[n][0])) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   char a[11];   bool ok = true;   bool check(char a[]) {     for (int i = 0; i < 7; i++)       if (a[i] == a[i + 1]) return false;     return true;   }   int main() {     for (int i = 1; i <= 8; i++) {       scanf( %s , a);       ok &= check(a);     }     if (ok)       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100050;   const int M = 10050;   long long mn[M], h;   int ord[M], tmp[M], que[M], k, n, m;   bool was[M];   long long a[N];   int c[N];   stack<int> stk[M];   struct cmp {     bool operator()(int i, int j) {       return c[i] > c[j] || (c[i] == c[j] && i < j);     }   };   set<int, cmp> pts;   bool in[N];   void Cns(int ost) {     while (stk[ost].size() && a[stk[ost].top()] >= mn[ost]) {       int i = stk[ost].top();       stk[ost].pop();       pts.insert(i);       in[i] = 1;     }   }   void Upd(long long x) {     int i = 1, j = 1, xb = 0, xz = 0;     for (int i = 0; i < k; i++) was[i] = 0;     for (; i <= k || j <= xb;) {       int u;       if (i > k || (j <= xb && mn[que[j]] < mn[ord[i]]))         u = que[j++];       else         u = ord[i++];       if (was[u]) continue;       was[u] = 1;       tmp[++xz] = u;       int v = (u + x) % k;       if (mn[v] > mn[u] + x) {         mn[v] = mn[u] + x;         Cns(v);         que[++xb] = v;       }     }     assert(xz == k);     for (int i = 1; i <= k; i++) ord[i] = tmp[i];   }   int main() {     scanf( %lld %i %i %i , &h, &n, &m, &k);     for (int i = 1; i < k; i++) mn[i] = h;     for (int i = 1; i <= k; i++) ord[i] = i - 1;     vector<int> ord;     for (int i = 1; i <= n; i++)       scanf( %lld %i , &a[i], &c[i]), a[i]--, ord.push_back(i);     sort(ord.begin(), ord.end(), [&](int i, int j) { return a[i] < a[j]; });     for (int i : ord) stk[a[i] % k].push(i);     Cns(0);     while (m--) {       int t;       scanf( %i , &t);       if (t == 1) {         long long x;         scanf( %lld , &x);         Upd(x);       } else if (t == 2) {         int x, y;         scanf( %i %i , &x, &y);         if (in[x]) pts.erase(x);         c[x] -= y;         if (in[x]) pts.insert(x);       } else {         if (pts.empty())           printf( 0 n );         else {           int i = *pts.begin();           printf( %i n , c[i]);           pts.erase(i);           c[i] = 0;           in[i] = 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5;   long long n, k;   long long a[N];   priority_queue<tuple<long long, long long, long long> > pq;   inline long long sq(long long x) { return x * x; }   long long cost(long long len, long long pieces) {     long long each = len / pieces;     long long extra = len % pieces;     long long res = extra * sq(each + 1) + sq(each) * (pieces - extra);     return res;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> k;     long long ans = 0;     for (long long i = 1; i <= n; i++) {       cin >> a[i];       ans += sq(a[i]);       long long val = cost(a[i], 1) - cost(a[i], 2);       long long len = a[i];       long long pieces = 2;       pq.push(make_tuple(val, len, pieces));     }     for (long long i = 1; i <= k - n; i++) {       long long val, len, pieces;       tie(val, len, pieces) = pq.top();       pq.pop();       ans -= val;       long long new_val = cost(len, pieces) - cost(len, pieces + 1);       pieces++;       pq.push(make_tuple(new_val, len, pieces));     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, h, m, data[55];   int main() {     cin >> n >> h >> m;     for (int i = 1; i <= n; i++) {       data[i] = h;     }     for (int i = 1; i <= m; i++) {       long long l, r, x;       cin >> l >> r >> x;       for (int j = l; j <= r; j++) {         data[j] = min(data[j], x);       }     }     long long ans = 0;     for (int i = 1; i <= n; i++) ans += data[i] * data[i];     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long a, b, c, d;       cin >> a >> b >> c >> d;       cout << b <<     << c <<     << c << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long n, m, l, r, T, mid;   bool check(long long x) {     long long p = x * m, s = 1, d = n, t;     for (long i = 0; i <= x && p >= 0 && d; i++) {       d -= (t = min(s, d));       p -= t * i;       s = s * (x - i) / (i + 1);     }     return p >= 0 && !d;   }   int main() {     for (scanf( %ld , &T); T; T--) {       scanf( %ld%ld , &n, &m);       m = min(m, n / 2);       l = 0;       r = n;       while (l < r) {         mid = (l + r) / 2;         if (check(mid))           r = mid;         else           l = mid + 1;       }       printf( %ld n , r);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   struct Vec {     long long x, y;     Vec operator+(const Vec &o) const { return (Vec){x + o.x, y + o.y}; }     Vec operator-(const Vec &o) const { return (Vec){x - o.x, y - o.y}; }     long long operator*(const Vec &o) const {       return 1ll * x * o.y - 1ll * y * o.x;     }     long long operator|(const Vec &o) const {       return 1ll * x * o.x + 1ll * y * o.y;     }   } mon[1010], arr[11];   vector<int> ir[11][1010];   bool book[11];   int a[11];   int die[1010], clk;   int now;   bool chk(int p) {     if (die[p] == clk) return 1;     now++;     if (now > m) return 0;     int ap = a[now];     int ma = ir[ap][p].size();     if (!ma) {       die[p] = clk;       return 1;     }     for (int i = 0; i < ma; ++i)       if (!chk(ir[ap][p][i])) return 0;     die[p] = clk;     return 1;   }   bool check(int x) {     clk++;     now = 0;     return chk(x);   }   bool dfs(int x, int p) {     if (x > m) {       if (check(p)) return 1;       return 0;     }     for (int i = 1; i <= m; ++i)       if (!book[i]) {         a[x] = i;         book[i] = 1;         if (dfs(x + 1, p)) return 1;         book[i] = 0;       }     return 0;   }   int main() {     scanf( %d%d , &m, &n);     for (int i = 1; i <= m; ++i) scanf( %lld%lld , &arr[i].x, &arr[i].y);     for (int i = 1; i <= n; ++i) scanf( %lld%lld , &mon[i].x, &mon[i].y);     Vec vn, v1;     for (int i = 1; i <= m; ++i)       for (int j = 1; j <= n; ++j) {         vn = mon[j] - arr[i];         for (int k = 1; k <= n; ++k) {           if (k == j) continue;           v1 = mon[k] - arr[i];           if (v1 * vn != 0 || (v1 | vn) <= 0) continue;           if (abs(v1.x) > abs(vn.x) || abs(v1.y) > abs(vn.y)) continue;           ir[i][j].push_back(k);         }       }     int ans = 0;     for (int i = 1; i <= n; ++i) {       memset(book, 0, sizeof(book));       if (dfs(1, i)) ans++;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct frnd {     int w;     int h;     frnd(int w, int h) : w(w), h(h) {}   };   bool cmp(frnd a, frnd b) { return a.h < b.h; }   int main() {     int n;     cin >> n;     vector<frnd> friends;     int sum = 0;     int height = 0;     int prevH = 0;     for (int i = 0; i < n; i++) {       int w, h;       cin >> w >> h;       frnd a(w, h);       friends.push_back(a);       sum += w;       if (h > height) {         prevH = height;         height = h;       } else {         if (h > prevH) {           prevH = h;         }       }     }     for (int i = 0; i < n; i++) {       cout << (sum - friends[i].w) * ((height == friends[i].h) ? prevH : height)            <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct query {     int id, l, x, ans;   } qrs[100009];   bool idcomp(query a, query b) { return a.id < b.id; }   bool lcomp(query a, query b) { return a.l < b.l; }   int N, Q, a[100009], killer[20];   int modExp(int a, int b) {     if (b == 0) return 1;     int c = modExp(a, b >> 1);     c = (c * 1ll * c) % 1000000007;     if (b & 1) c = (c * 1ll * a) % 1000000007;     return c;   }   int main() {     scanf( %d %d , &N, &Q);     memset(&killer, -1, sizeof killer);     for (int i = 0; i < N; i++) {       scanf( %d , &a[i]);     }     for (int q = 0; q < Q; q++) {       scanf( %d %d , &qrs[q].l, &qrs[q].x);       qrs[q].id = q;       qrs[q].l--;     }     sort(qrs, qrs + Q, lcomp);     int q = 0, nz = 0;     for (int i = 0; i < N; i++) {       for (int j = 0; j < 20; j++) {         if (killer[j] != -1 && (a[i] & 1 << j)) {           a[i] ^= a[killer[j]];         }       }       for (int j = 0; j < 20; j++) {         if (killer[j] == -1 && (a[i] & 1 << j)) {           killer[j] = i;           for (int it = 0; it < i; it++) {             if ((a[it] & 1 << j)) a[it] ^= a[i];           }           break;         }       }       if (a[i] == 0) nz++;       while (q < Q && qrs[q].l < i) q++;       while (q < Q && qrs[q].l == i) {         int x = qrs[q].x;         for (int j = 0; j < 20; j++) {           if (killer[j] != -1 && (x & 1 << j)) {             x ^= a[killer[j]];           }         }         if (x == 0)           qrs[q].ans = modExp(2, nz);         else           qrs[q].ans = 0;         q++;       }     }     sort(qrs, qrs + Q, idcomp);     for (int q = 0; q < Q; q++) {       printf( %d n , qrs[q].ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   const int MOD = 1e9 + 7;   const int N = 1007;   int n;   vector<int> ans, l, r, p1, p2, p3, p4;   long long dis[N];   long long qry(int t, int i, int j, int k) {     printf( %d %d %d %d n , t, i, j, k);     fflush(stdout);     long long res;     scanf( %lld , &res);     return res;   }   void Init() {     ans.clear();     l.clear();     r.clear();   }   int Solve() {     for (int i = (3); i < (n + 1); ++i) {       long long res = qry(2, 1, 2, i);       if (res == 1)         l.push_back(i);       else         r.push_back(i);     }     ans.push_back(1);     if ((int)(r).size()) {       long long mx = -LINF, id;       for (auto o : r) {         long long res = qry(1, 1, 2, o);         dis[o] = res;         if (res > mx) {           mx = res;           id = o;         }       }       p4.clear();       p1.clear();       for (auto o : r)         if (o != id) {           long long res = qry(2, 1, id, o);           if (res == 1)             p1.push_back(o);           else             p4.push_back(o);         }       sort((p4).begin(), (p4).end(),            [](int a, int b) { return dis[a] < dis[b]; });       sort((p1).begin(), (p1).end(),            [](int a, int b) { return dis[a] > dis[b]; });       for (auto o : p4) ans.push_back(o);       ans.push_back(id);       for (auto o : p1) ans.push_back(o);     }     ans.push_back(2);     if ((int)(l).size()) {       long long mx = -LINF, id;       for (auto o : l) {         long long res = qry(1, 1, 2, o);         dis[o] = res;         if (res > mx) {           mx = res;           id = o;         }       }       p2.clear();       p3.clear();       for (auto o : l)         if (o != id) {           long long res = qry(2, 1, id, o);           if (res == 1)             p3.push_back(o);           else             p2.push_back(o);         }       sort((p2).begin(), (p2).end(),            [](int a, int b) { return dis[a] < dis[b]; });       sort((p3).begin(), (p3).end(),            [](int a, int b) { return dis[a] > dis[b]; });       for (auto o : p2) ans.push_back(o);       ans.push_back(id);       for (auto o : p3) ans.push_back(o);     }     printf( 0 );     for (auto o : ans) printf(  %d , o);     return puts(  );   }   int main() {     scanf( %d , &n);     Init();     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9;   const long long INF = 1e15;   template <class o>   void qr(o& x) {     char c = getchar();     x = 0;     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     x *= f;   }   template <class o>   void qw(o x) {     if (x / 10) qw(x / 10);     putchar(x % 10 +  0 );   }   template <class o>   void pr1(o x) {     if (x < 0) x = -x, putchar( - );     qw(x);     putchar(   );   }   template <class o>   void pr2(o x) {     if (x < 0) x = -x, putchar( - );     qw(x);     putchar(  n );   }   long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   long long power(long long a, long long b = mod - 2, long long p = mod) {     long long c = 1;     while (b) {       if (b & 1) c = c * a % p;       b /= 2;       a = a * a % p;     }     return c;   }   template <class o>   void cmax(o& x, o y) {     if (x < y) x = y;   }   template <class o>   void cmin(o& x, o y) {     if (x > y) x = y;   }   template <typename t1, typename t2>   void ad(t1& x, t2 y) {     x += y;     if (x >= mod) x -= mod;   }   template <typename t1, typename t2>   void dl(t1& x, t2 y) {     x -= y;     if (x < 0) x += mod;   }   template <typename T>   struct BIT {     T* c;     int n;     BIT(int _n) : n(_n) {       c = new T[n];       c--;       for (int i = 1; i <= n; i++) c[i] = T(0);     }     void add(int x, T y) {       for (; x <= n; x += x & -x) c[x] = c[x] + y;     }     T sum(int x) {       T y = T(0);       for (; x; x &= x - 1) y = y + c[x];       return y;     }   };   long long jc[N], inv[N];   void jc_init(int n) {     jc[0] = 1;     for (int i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % mod;     inv[n] = power(jc[n]);     for (int i = n; i; i--) inv[i - 1] = inv[i] * i % mod;   }   long long C(int x, int y) {     if (x < y || y < 0) return 0;     return jc[x] * inv[y] % mod * inv[x - y] % mod;   }   int n, m;   char s[N][N];   void solve() {     qr(n);     qr(m);     vector<vector<pair<int, int> > > v;     for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1);     for (int i = 1; i <= n - 2; i++)       for (int j = 1; j <= m; j++)         if (s[i][j] ==  1 ) {           s[i][j] ^= 1;           vector<pair<int, int> > o;           o.push_back(make_pair(i, j));           o.push_back(make_pair(i + 1, j));           s[i + 1][j] ^= 1;           if (j < m)             o.push_back(make_pair(i + 1, j + 1)), s[i + 1][j + 1] ^= 1;           else             o.push_back(make_pair(i + 1, j - 1)), s[i + 1][j - 1] ^= 1;           v.push_back(o);           assert(((int)o.size()) == 3);         }     for (int j = 1; j <= m - 2; j++) {       vector<pair<int, int> > o;       if (s[n][j] ==  1 ) o.push_back(make_pair(n, j)), s[n][j] =  0 ;       if (s[n - 1][j] ==  1 ) o.push_back(make_pair(n - 1, j)), s[n - 1][j] =  0 ;       if (((int)o.size())) {         o.push_back(make_pair(n, j + 1));         s[n][j + 1] ^= 1;         if (((int)o.size()) < 3)           o.push_back(make_pair(n - 1, j + 1)), s[n - 1][j + 1] ^= 1;         v.push_back(o);       }     }     vector<pair<int, int> > o;     char &x = s[n - 1][m - 1], &y = s[n - 1][m], &u = s[n][m - 1], &vv = s[n][m];     if (x ==  1 ) o.push_back(make_pair(n - 1, m - 1)), x =  0 ;     if (y ==  1 ) o.push_back(make_pair(n - 1, m)), y =  0 ;     if (((int)o.size())) {       for (int j = m - 1; j <= m; j++)         if (((int)o.size()) < 3) o.push_back(make_pair(n, j)), s[n][j] ^= 1;       v.push_back(o);     }     o.clear();     if (u ==  1  && vv ==  1 ) {       o.push_back(make_pair(n - 1, m - 1));       o.push_back(make_pair(n - 1, m));       o.push_back(make_pair(n, m));       v.push_back(o);       o[2] = make_pair(n, m - 1);       v.push_back(o);     } else if (u ==  1 ) {       o.push_back(make_pair(n, m - 1));       o.push_back(make_pair(n - 1, m));       o.push_back(make_pair(n, m));       v.push_back(o);       o[0] = make_pair(n - 1, m - 1);       o[1] = make_pair(n, m - 1);       o[2] = make_pair(n - 1, m);       v.push_back(o);       o[2] = make_pair(n, m);       v.push_back(o);     } else if (vv ==  1 ) {       o.push_back(make_pair(n, m));       o.push_back(make_pair(n - 1, m - 1));       o.push_back(make_pair(n, m - 1));       v.push_back(o);       o.clear();       o.push_back(make_pair(n - 1, m - 1));       o.push_back(make_pair(n - 1, m));       o.push_back(make_pair(n, m));       v.push_back(o);       o[0] = make_pair(n, m - 1);       v.push_back(o);     }     pr2(((int)v.size()));     for (auto i : v) {       for (auto j : i) pr1(j.first), pr1(j.second);       puts(  );     }   }   int main() {     int T = 1;     qr(T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct r {     int p, t;   } a;   vector<r> v;   bool sss(const r& i, const r& j) {     if (i.p > j.p)       return true;     else if (i.p == j.p)       return i.t < j.t;     else       return false;   }   int main() {     int n, k;     cin >> n >> k;     map<pair<int, int>, int> mp;     for (int cs = 1; cs <= n; cs++) {       cin >> a.p >> a.t;       mp[make_pair(a.p, a.t)]++;       v.push_back(a);     }     sort(v.begin(), v.end(), sss);     cout << mp[{v[k - 1].p, v[k - 1].t}] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 5e5 + 3, M = 2e5 + 3;   int MOD;   string s, t, ts;   int n, m, pw[N], hs[N], ht, Z[N + M];   bool check_prime(int x) {     for (int i = 2; i * i <= x; ++i) {       if (x % i == 0) return false;     }     return true;   }   int add(int x, int y) {     x += y;     while (x >= MOD) x -= MOD;     while (x < 0) x += MOD;     return x;   }   int mul(int x, int y) { return (x * 1LL * y) % MOD; }   int get_hash(int l, int r) {     return add(hs[r], -mul(hs[l - 1], pw[r - l + 1]));   }   void init() {     while (true) {       MOD = uniform_int_distribution<int>(1e8, 1e9)(rng);       if (check_prime(MOD)) break;     }     cin >> s >> t;     n = s.size(), m = t.size();     ts =     + t + s, s =     + s, t =     + t;     pw[0] = 1;     for (int i = 1; i <= n; ++i) pw[i] = mul(pw[i - 1], 10);     for (int i = 1; i <= n; ++i) hs[i] = add(s[i] -  0 , mul(hs[i - 1], 10));     for (int i = 1; i <= m; ++i) ht = add(t[i] -  0 , mul(ht, 10));     Z[1] = n + m;     int l = 1, r = 1;     for (int i = 2; i <= n + m; ++i) {       if (i > r) {         l = r = i;         while (r <= n + m && ts[r] == ts[r - l + 1]) ++r;         Z[i] = r - l;         r--;       } else {         int k = i - l + 1;         if (Z[k] < r - i + 1)           Z[i] = Z[k];         else {           l = i;           while (r <= n + m && ts[r] == ts[r - l + 1]) ++r;           Z[i] = r - l;           r--;         }       }     }   }   void solve() {     init();     for (int i = 1; i + 2 * (m - 1) - 1 <= n; ++i) {       int x = get_hash(i, i + m - 2);       int x2 = get_hash(i + m - 1, i + 2 * m - 3);       x = add(x, x2);       if (x == ht) {         cout << i <<     << i + m - 2 <<   n               << i + m - 1 <<     << i + 2 * m - 3;         return;       }     }     for (int i = 1; i + m - 1 <= n; ++i) {       int x = get_hash(i, i + m - 1);       if (m - Z[i + m] > 0) {         if (i - m + Z[i + m] > 0) {           int x2 = get_hash(i - m + Z[i + m], i - 1);           x = add(x, x2);           if (x == ht) {             cout << i - m + Z[i + m] <<     << i - 1 <<   n                   << i <<     << i + m - 1;             return;           }           x = add(x, -x2);         }         if (i + 2 * m - Z[i + m] - 1 <= n) {           int x2 = get_hash(i + m, i + 2 * m - Z[i + m] - 1);           x = add(x, x2);           if (x == ht) {             cout << i <<     << i + m - 1 <<   n                   << i + m <<     << i + 2 * m - Z[i + m] - 1;             return;           }           x = add(x, -x2);         }       }       if (m - Z[i + m] - 1 > 0) {         if (i - m + Z[i + m] + 1 > 0) {           int x2 = get_hash(i - m + Z[i + m] + 1, i - 1);           x = add(x, x2);           if (x == ht) {             cout << i - m + Z[i + m] + 1 <<     << i - 1 <<   n                   << i <<     << i + m - 1;             return;           }           x = add(x, -x2);         }         if (i + 2 * m - Z[i + m] - 2 <= n) {           int x2 = get_hash(i + m, i + 2 * m - Z[i + m] - 2);           x = add(x, x2);           if (x == ht) {             cout << i <<     << i + m - 1 <<   n                   << i + m <<     << i + 2 * m - Z[i + m] - 2;             return;           }           x = add(x, -x2);         }       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 500;   const int dr[] = {0, 1, 0, -1};   const int dc[] = {1, 0, -1, 0};   bool vis[MAXN + 5][MAXN + 5];   char grid[MAXN + 5][MAXN + 5];   int W, H;   vector<pair<char, pair<int, int> > > ans;   inline void put(char ch, int i, int j) {     ans.push_back(make_pair(ch, make_pair(i, j)));   }   void buildRed(int i, int j) {     if (i < 0 || j < 0 || i >= H || j >= W) return;     if (grid[i][j] ==  # ) return;     if (vis[i][j]) return;     vis[i][j] = true;     for (int dir = 0; dir < 4; dir++) buildRed(i + dr[dir], j + dc[dir]);     put( D , i + 1, j + 1);     put( R , i + 1, j + 1);     return;   }   int main() {     int c, c2;     scanf( %d%d , &H, &W);     for (c = 0; c < H; c++) scanf( %s , grid[c]);     for (c = 0; c < H; c++)       for (c2 = 0; c2 < W; c2++)         if (grid[c][c2] ==  . ) put( B , c + 1, c2 + 1);     for (c = 0; c < H; c++)       for (c2 = 0; c2 < W; c2++)         if (!vis[c][c2] && grid[c][c2] ==  . ) {           vis[c][c2] = true;           for (int dir = 0; dir < 4; dir++) buildRed(c + dr[dir], c2 + dc[dir]);         }     printf( %d n , ans.size());     for (c = 0; c < ans.size(); c++)       printf( %c %d %d n , ans[c].first, ans[c].second.first,              ans[c].second.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 19;   int n, k, m, e, a[N], b[N], g[N], c[N], d[N], f[N], t[N], w[N], p[N];   vector<int> v[N];   void upd(int k) {     if (t[k << 1] > t[k << 1 | 1])       t[k] = t[k << 1] + w[k], p[k] = p[k << 1];     else       t[k] = t[k << 1 | 1] + w[k], p[k] = p[k << 1 | 1];   }   void build(int k = 1, int l = 1, int r = n) {     if (l == r) {       t[k] = d[g[l]];       w[k] = 0;       p[k] = l;       return;     }     int h = l + r >> 1;     build(k << 1, l, h);     build(k << 1 | 1, h + 1, r);     upd(k);   }   void modify(int L, int R, int X, int k = 1, int l = 1, int r = n) {     if (L <= l && r <= R) {       w[k] += X;       t[k] += X;       return;     }     int h = l + r >> 1;     if (L <= h) modify(L, R, X, k << 1, l, h);     if (h < R) modify(L, R, X, k << 1 | 1, h + 1, r);     upd(k);   }   void dfs(int u, int fa) {     int i, x, o = 1;     a[u] = ++e;     g[e] = u;     d[u] = d[fa] + 1;     f[u] = fa;     for (i = 0; i < v[u].size(); i++) {       x = v[u][i];       if (x != fa) dfs(x, u), o = 0;     }     m += o;     b[u] = e;   }   void go(int x) {     while (x && !c[x]) {       modify(a[x], b[x], -1);       c[x] = 1;       e++;       x = f[x];     }   }   int main() {     int i, x, y;     long long s = 0;     scanf( %d%d , &n, &k);     for (i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       v[x].push_back(y);       v[y].push_back(x);     }     e = 0;     dfs(1, 0);     if (m <= k) {       for (i = m; i <= k; i++) s = max(s, (long long)i * (n - i));       cout << s;       return 0;     }     build();     e = 0;     for (i = 1; i <= k; i++) {       go(g[p[1]]);     }     s = (long long)k * (n - k);     for (i = 0; i <= n - e; i++) s = min(s, (long long)(k - i) * (n - k - i));     cout << s;     return 0;   }


#include <bits/stdc++.h>   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(nullptr);     int n;     std::cin >> n;     std::vector<std::vector<std::pair<int, int>>> g(n);     for (int i = 1; i < n; ++i) {       int u, v, w;       std::cin >> u >> v >> w;       --u;       --v;       g[u].push_back(std::make_pair(v, w));       g[v].push_back(std::make_pair(u, w));     }     std::vector<int> sz(n);     std::vector<long long> w(n);     long long ans = 0;     std::function<void(int, int)> dfs = [&](int u, int fa) {       sz[u] = 1;       std::vector<int> adj;       for (auto p : g[u]) {         int v = p.first, l = p.second;         if (v == fa) {           continue;         }         w[v] += l * 2;         dfs(v, u);         w[u] += w[v];         sz[u] += sz[v];         ans += l * sz[v];         adj.push_back(v);       }       std::sort(adj.begin(), adj.end(),                 [&](int u, int v) { return w[u] * sz[v] < w[v] * sz[u]; });       long long sum = 0;       for (auto v : adj) {         ans += sum * sz[v];         sum += w[v];       }     };     dfs(0, -1);     std::cout << std::setprecision(20) << 1.0 * ans / (n - 1) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   void sherlock(int t) {     int n;     cin >> n;     string s;     cin >> s;     string t1, t2, ans;     int mink = 1;     ans = s;     for (int k = 1; k <= n; k++) {       t1 = s.substr(k - 1, n - k + 1);       t2 = s.substr(0, k - 1);       if ((n - k + 1) % 2 == 1) reverse(t2.begin(), t2.end());       t1 += t2;       if (t1 < ans) {         mink = k;         ans = t1;       }     }     cout << ans << endl << mink << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int tt;     cin >> tt;     for (int t = 1; t <= tt; t++) sherlock(t);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct dato {     int i, j, val;   };   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<dato> input(k);     for (int t = 0; t < k; t++) {       dato &d = input[t];       cin >> d.i >> d.j >> d.val;       d.i--;       d.j--;     }     long long int p;     cin >> p;     if (n > m) {       for (int t = 0; t < k; t++) {         dato &d = input[t];         swap(d.i, d.j);       }       swap(n, m);     }     vector<int> numcol(m, 0);     vector<int> prodcol(m, 1);     for (int t = 0; t < k; t++) {       dato &d = input[t];       numcol[d.j]++;       prodcol[d.j] *= d.val;     }     bool error = false;     for (int j = 0; j < m and not error; j++)       error = numcol[j] == n and prodcol[j] == 1;     if (error or n % 2 != m % 2)       cout << 0 << endl;     else {       int numllenas = 0;       for (int j = 0; j < m; j++)         if (numcol[j] == n) numllenas++;       int e = (n - 1) * (m - 1) - k + numllenas;       long long int r = 2;       long long int a = 1;       while (e > 0) {         if (e % 2 == 0) {           r = (r * r) % p;           e /= 2;         } else {           a = (a * r) % p;           e--;         }       }       cout << a << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   const int INF = 0x3f3f3f3f;   const long long int LINF = 0x3f3f3f3f3f3f3f3f;   const int NINF = -INF - 1;   const long long int mod = 1e9 + 7;   int pre[MAXN];   int find(int x) { return pre[x] == x ? x : pre[x] = find(pre[x]); }   int T, n, k;   char s[MAXN];   int cnt[MAXN][28];   int main() {     cin >> T;     while (T--) {       scanf( %d%d , &n, &k);       scanf( %s , s);       vector<int> vec;       for (int i = 0; i <= n + 1; i++) pre[i] = i;       for (int i = 0; i < n; i++) {         if (i + k < n) {           int a = find(i), b = find(i + k);           if (a != b) pre[a] = b;         }       }       for (int i = 0; i < n / 2; i++) {         int a = find(i), b = find(n - i - 1);         if (a != b) pre[a] = b;       }       for (int i = 0; i < n; i++) {         cnt[find(i)][s[i] -  a ]++;       }       int ans = 0;       for (int i = 0; i < n; i++) {         if (pre[i] != i) continue;         int mm = 0, tot = 0;         for (int j = 0; j < 26; j++) {           mm = max(cnt[i][j], mm);           tot += cnt[i][j];         }         for (int j = 0; j < 26; j++) cnt[i][j] = 0;         ans += tot - mm;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 16;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     vector<int> ans(maxn, -1);     ans[0] = 0;     for (int i = 1; i < maxn; ++i) {       for (auto j : vector<int>{4, 6, 9}) {         if (i >= j && ans[i - j] != -1) {           ans[i] = max(ans[i], ans[i - j] + 1);         }       }     }     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       int n;       cin >> n;       if (n < maxn) {         cout << ans[n] <<   n ;       } else {         int t = (n - maxn) / 4 + 1;         cout << t + ans[n - 4 * t] <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000000 + 7;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n;     cin >> n;     n -= 10;     if (n >= 1 && n <= 11 && n != 10) {       cout << 4;     } else if (n == 10) {       cout << 15;     } else {       cout << 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n];       long long i, j, k, l;       for (i = 0; i < n; i++) {         cin >> a[i];       }       long long diff;       bool value = true;       for (i = 0; i < n - 1; i++) {         diff = abs(a[i] - a[i + 1]);         if (diff == 1) {           continue;         }         if (diff > 1) {           if (a[i] > a[i + 1]) {             continue;           }           if (a[i] < a[i + 1]) {             cout <<  NO                   <<   n ;             value = false;             break;           }         }       }       if (value) {         cout <<  YES               <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int f[55], s[55];   long double p[55];   long double dp[55][5005];   bool vis[55][5005];   int main() {     int n, r;     scanf( %d%d , &n, &r);     for (int i = 1; i <= n; i++) {       scanf( %d%d%Lf , &f[i], &s[i], &p[i]);       p[i] /= 100;     }     long double lo = 0, hi = 1e18, ans = 0;     for (int i = 0; i < 200; i++) {       long double mid = (lo + hi) / 2;       memset(vis, 0, sizeof(vis));       memset(dp, 0, sizeof(dp));       vis[n + 1][0] = 1;       for (int i = n; i >= 1; i--) {         for (int j = 0; j <= r; j++) {           long double res = 0;           if (j >= s[i]) {             res += (f[i] + dp[i + 1][j - f[i]]) * p[i];             res += (s[i] + dp[i + 1][j - s[i]]) * (1 - p[i]);             res = min(res, mid);             dp[i][j] = res;             vis[i][j] = 1;           } else if (j >= f[i]) {             res += (f[i] + dp[i + 1][j - f[i]]) * p[i];             res += (s[i] + mid) * (1 - p[i]);             res = min(res, mid);             dp[i][j] = res;             vis[i][j] = 1;           } else {             res += (f[i] + mid) * p[i];             res += (s[i] + mid) * (1 - p[i]);             res = min(res, mid);             dp[i][j] = mid;             vis[i][j] = mid;           }         }       }       bool ok = 0;       for (int j = 0; j <= r; j++) {         if (!vis[1][j]) continue;         if (dp[1][j] < mid) ok = 1;       }       if (ok)         ans = mid, hi = mid;       else         lo = mid;     }     printf( %.20Lf n , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long n, x, d;   int sad;   string s;   int main() {     cin >> n >> x;     while (n--) {       string s;       long long d;       cin >> s >> d;       if (s[0] ==  + ) {         x += d;       } else {         if (d <= x) {           x -= d;         } else           sad++;       }     }     cout << x <<     << sad <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, s;     scanf( %I64d%I64d%I64d , &n, &k, &s);     if (k * (n - 1) < s || s < k)       printf( NO n );     else {       long long sum = s, step = k;       long long dis = n - 1;       printf( YES n );       long long now = 1;       for (int i = 1; i <= k; i++) {         if ((step - 1) <= sum - dis) {           if (now == 1) {             if (i == 1)               printf( %d , n);             else               printf(  %d , n);             now = n;           } else {             if (i == 1)               printf( %d , 1);             else               printf(  %d , 1);             now = 1;           }           step -= 1;           sum -= dis;         } else {           long long d = sum - step + 1;           if (now - d <= 0) {             if (i == 1)               printf( %d , d + now);             else               printf(  %d , d + now);             now = d + now;           } else {             if (i == 1)               printf( %d , now - d);             else               printf(  %d , now - d);             now = now - d;           }           step -= 1;           sum -= d;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int n;     cin >> n;     if (n % 2 == 0) return cout <<  NO , 0;     long long int arr[2 * n + 1];     long long int a = 1, b = n + 1;     for (long long int i = 1; i < 2 * n + 1; i += 2) {       if (a % 2) {         arr[a++] = i;         arr[b++] = i + 1;       } else {         arr[b++] = i;         arr[a++] = i + 1;       }     }     cout <<  YES n ;     for (long long int i = 1; i < 2 * n + 1; i++) cout << arr[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005], b[100005];   int main() {     int i, j, n, p, k;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b[i] = a[i];     }     sort(b + 1, b + 1 + n);     for (i = 1; i <= n; i++) {       if (b[i] != a[i]) break;     }     p = i;     if (i == n + 1) {       printf( yes n );       printf( 1 1 );       return 0;     }     for (j = n; j >= 1; j--) {       if (b[j] != a[j]) break;     }     k = j;     int flag = 0;     for (i = p, j = k; i <= k && j >= p; i++, j--) {       if (b[i] != a[j]) {         flag = 1;         break;       }     }     if (flag) {       printf( no n );     } else {       printf( yes n );       printf( %d %d n , p, k);     }   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = numeric_limits<int>::max() / 2 - 1;   inline int run() {     int n, a, b, k;     cin >> n >> a >> b >> k;     string s;     cin >> s;     int cur = 0;     vector<int> ans;     for (int i = 0; i < n; ++i) {       if (s[i] ==  0 ) cur++;       if (s[i] ==  1 ) cur = 0;       if (cur == b) ans.push_back(i), cur = 0;     }     for (int i = 0; i < a - 1 && ans.size() > 1; ++i) {       ans.pop_back();     }     cout << ans.size() << endl;     for (auto i : ans) cout << i + 1 <<    ;     return 0;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       string s;       cin >> s;       bool ans = true;       for (long long i = 0; i < k; i++) {         char temp = s[i];         for (long long j = i; j < n; j += k) {           if (temp ==  ?  && s[j] != temp) {             temp = s[j];             s[i] = temp;           }           if (temp != s[j] && s[j] !=  ? ) {             ans = false;           }         }       }       long long cnt1 = 0, cnt0 = 0, cnt = 0;       for (long long i = 0; i < k; i++) {         if (s[i] ==  1 ) {           cnt1++;         } else if (s[i] ==  0 ) {           cnt0++;         } else {           cnt++;         }       }       long long x = cnt - abs(cnt0 - cnt1);       if (x < 0 || x % 2 == 1) {         ans = false;       }       if (ans) {         cout <<  YES               <<   n ;       } else {         cout <<  NO               <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   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 << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   long long die = 0;   const double eps = 1e-8;   const long long mod = 1e9 + 7;   const long long N = 1e6 + 5;   long long n;   long long a[N];   long long x, y;   bool mp[N];   long long fac[N];   long long inv[N];   long long C(long long y, long long i) {     return fac[y] * inv[i] % mod * inv[y - i] % mod;   }   void sol1() {     x = y = 0;     for (long long i = (1); i <= (n); i++)       if (a[i] == -1) {         if (mp[i])           x++;         else           y++;       }     n = x + y;     inv[0] = inv[1] = fac[0] = 1;     for (long long i = (1); i <= (n); i++) fac[i] = fac[i - 1] * i % mod;     for (long long i = (2); i <= (n); i++)       inv[i] = (mod - mod / i) * inv[mod % i] % mod;     for (long long i = (2); i <= (n); i++) (inv[i] *= inv[i - 1]) %= mod;     long long ans = fac[n];     for (long long i = (1); i <= (y); i++)       (ans += ((i & 1) ? -1 : 1) * C(y, i) * fac[n - i] % mod) %= mod;     (ans += mod) %= mod;     printf( %I64d n , ans);   }   void rd() {     n = read();     for (long long i = (1); i <= (n); i++) {       a[i] = read();       if (a[i] != -1) mp[a[i]] = 1;     }     sol1();   }   signed main() { rd(); }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v;     vector<int>::iterator j;     int n, i;     cin >> n;     n = 2 * n;     int cnt = 0;     int x[n];     int cnt2 = 0;     for (i = 0; i < n; i++) {       cin >> x[i];       if (i % 2 == 0) {         if (x[i] < 0) {           cnt++;         } else if (x[i] > 0) {           cnt2++;         }       }     }     if (cnt <= 1 || cnt2 == 1) {       cout <<  Yes ;       return 0;     } else if (cnt == n / 2 || cnt2 == n / 2) {       cout <<  Yes ;       return 0;     }     cout <<  No ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 5;   char s[MAXN];   int lst = 1, cnt = 1, len[MAXN];   int fa[MAXN], ch[MAXN][26];   int siz[MAXN], c[MAXN], ord[MAXN];   inline int read() {     int x = 0;     char ch = getchar();     while (!isdigit(ch)) ch = getchar();     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x;   }   inline void insert(int c) {     int p = lst, np = lst = ++cnt;     len[np] = len[p] + 1, siz[np] = 1;     while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];     if (!p) return fa[np] = 1, void();     int q = ch[p][c];     if (len[q] == len[p] + 1) return fa[np] = q, void();     int nq = ++cnt;     fa[nq] = fa[q], fa[q] = fa[np] = nq;     len[nq] = len[p] + 1, memcpy(ch[nq], ch[q], sizeof(ch[q]));     while (ch[p][c] == q) ch[p][c] = nq, p = fa[p];   }   inline void clear() {     lst = cnt = 1;     memset(c, 0, sizeof(c));     memset(fa, 0, sizeof(fa));     memset(ch, 0, sizeof(ch));     memset(siz, 0, sizeof(siz));   }   inline long long solve() {     long long ans = 0;     for (int i = 1; i <= cnt; i++) c[len[i]]++;     for (int i = 1; i <= cnt; i++) c[i] += c[i - 1];     for (int i = cnt; i; i--) ord[c[len[i]]--] = i;     for (int i = cnt; i; i--) siz[fa[ord[i]]] += siz[ord[i]];     for (int i = 1; i <= cnt; i++)       ans += 1ll * (len[i] - len[fa[i]]) * siz[i] * siz[i];     return ans;   }   int main() {     int t = read();     while (t--) {       clear();       scanf( %s , s + 1);       int n = strlen(s + 1);       for (int i = 1; i <= n; i++) insert(s[i] -  a );       printf( %lld n , solve());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<pair<int, int>>> g;   vector<int> used;   vector<int> tout;   int timer = 0;   bool dfs(int v, int cost) {     timer++;     used[v] = 1;     for (auto to : g[v]) {       if (to.second > cost) {         if (used[to.first] == 0) {           if (!dfs(to.first, cost)) {             return false;           }         } else {           if (used[to.first] == 1) {             return false;           }         }       }     }     used[v] = 2;     ++timer;     tout[v] = timer;     return true;   }   struct edge {     int a, b, c, index;   };   int main() {     int n, m;     int a, b, c;     cin >> n >> m;     g.resize(n);     tout.resize(n);     vector<pair<int, int>> index;     vector<edge> E;     for (int i = 0; i < m; i++) {       cin >> a >> b >> c;       --a;       --b;       g[a].push_back(make_pair(b, c));       index.push_back(make_pair(i + 1, c));       edge e;       e.a = a;       e.b = b;       e.c = c;       e.index = i + 1;       E.push_back(e);     }     int L = -1;     int R = 1e9;     while (R - L > 1) {       int M = (R + L) / 2;       used.clear();       used.resize(n, 0);       bool q = true;       for (int i = 0; i < n; i++) {         if (used[i] == 0) {           if (!dfs(i, M)) {             q = false;             break;           }         }       }       if (q) {         R = M;       } else {         L = M;       }     }     int k = 0;     timer = 0;     used.clear();     used.resize(n, 0);     bool q = true;     for (int i = 0; i < n; i++) {       if (used[i] == 0) {         if (!dfs(i, R)) {           q = false;           break;         }       }     }     vector<int> ans;     for (int i = 0; i < m; i++) {       if (E[i].c <= R && tout[E[i].a] <= tout[E[i].b]) {         k += 1;         ans.push_back(E[i].index);       }     }     cout << R <<     << k << endl;     for (int i = 0; i < k; i++) {       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 210, M = 50 * 1000 + 10;   int G, S, n, m, par[N];   int get_par(int v) { return par[v] == v ? v : par[v] = get_par(par[v]); }   struct edge {     int v = 0, u = 0, g = 0, s = 0;     bool operator<(edge b) const {       return make_pair(make_pair(s, v), u) < make_pair(make_pair(b.s, b.v), b.u);     }   } e[M];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> m >> G >> S;     for (int i = 0; i < m; i++) cin >> e[i].v >> e[i].u >> e[i].g >> e[i].s;     sort(e, e + m, [](edge x, edge y) { return x.g < y.g; });     long long ans = 1LL << 62;     set<edge> E;     for (int i = 0; i < m; i++) {       E.insert(e[i]);       iota(par, par + n + 1, 0);       edge goodbye;       for (auto e : E) {         int v = e.v, u = e.u;         v = get_par(v), u = get_par(u);         if (v == u)           goodbye = e;         else           par[u] = v;       }       E.erase(goodbye);       if ((int)(E.size()) == n - 1)         ans = min(1LL * G * e[i].g + 1LL * S * (*--E.end()).s, ans);     }     cout << (ans < 1LL << 62 ? ans : -1) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, a[100001], b[100001], flag = 1;       cin >> n;       vector<int> v(2, 0);       for (int i = 0; i < n; i++) cin >> a[i];       for (int j = 0; j < n; j++) cin >> b[j];       for (int i = 0; i < n; i++) {         if (a[i] < b[i] && v[1] != 1) {           flag = 0;           break;         } else if (a[i] > b[i] && v[0] != -1) {           flag = 0;           break;         }         if (a[i] == 1) v[1] = 1;         if (a[i] == -1) v[0] = -1;       }       if (flag == 0)         cout <<  NO n ;       else         cout <<  YES n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int arr[10005];   bool readl[10005];   int read_pages(int ini) {     int curr = ini;     readl[ini] = 1;     while (curr != arr[curr]) {       curr = arr[curr];       readl[curr] = true;     }     return curr;   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     int days = 0;     for (int i = 1; i <= n; i++) {       int curr = read_pages(i);       for (int j = i; j <= curr; j++) {         if (!readl[j]) {           curr = max(read_pages(j), curr);         }       }       i = curr;       days++;     }     cout << days;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     set<char> ST;     for (char x : s) ST.insert(x);     if (k > n) {       for (int i = 0; i < n; i++) cout << s[i];       for (int i = n; i < k; i++) cout << *ST.begin();       return;     }     for (int i = k - 1; i >= 0; i--) {       if (s[i] < *ST.rbegin()) {         string f =   ;         for (int j = 0; j < i; j++) f += s[j];         char mnThatWorkz =  z ;         for (char c : ST) {           if (c > s[i] && mnThatWorkz > c) mnThatWorkz = c;         }         f += mnThatWorkz;         while ((int)f.size() < k) f += *ST.begin();         cout << f <<   n ;         return;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void print(const T niz[], const int siz) {     for (int i = 0; i < siz; i++) cout << niz[i] <<    ;     cout << endl;   }   const long long p = 103;   long long add(long long x, long long y) { return (x + y) % 1000000007; }   long long mul(long long x, long long y) { return (x * y) % 1000000007; }   long long power(long long x, long long y) {     if (y == 0) return 1;     long long pola = power(x, y / 2);     pola = mul(pola, pola);     if (y % 2 == 1) pola = mul(pola, x);     return pola;   }   long long inverz[1000005];   int n;   string x;   long long hes[1000005][2];   long long get(long long l, long long r, long long idx) {     if (idx == 0) {       long long sta = hes[r][0] - hes[l - 1][0];       sta %= 1000000007;       sta += 1000000007;       sta %= 1000000007;       sta = mul(sta, inverz[l - 1]);       return sta;     } else {       long long sta = hes[l][1] - hes[r + 1][1];       sta %= 1000000007;       sta += 1000000007;       sta %= 1000000007;       sta = mul(sta, inverz[n - r]);       return sta;     }   }   bool checkpal(int l, int r) {     l = max(l, 0);     r = min(r, n);     if (l == r) return 1;     if (l > r) return 0;     long long sta1 = get(l, r, 0);     long long sta2 = get(l, r, 1);     return (sta1 == sta2);   }   int maksn = 0;   void solve() {     cin >> x;     n = x.length();     maksn = max(maksn, n);     for (int(i) = (0); (i) <= (maksn + 1); ++(i)) {       hes[i][0] = hes[i][1] = 0;     }     long long st = 1;     long long tr = 0;     for (int(i) = (0); (i) <= (n - 1); ++(i)) {       tr = add(tr, mul(st, x[i] -  a  + 1));       hes[i + 1][0] = tr;       st = mul(st, p);     }     tr = 0;     st = 1;     for (int(i) = (n - 1); (i) >= (0); --(i)) {       tr = add(tr, mul(st, x[i] -  a  + 1));       hes[i + 1][1] = tr;       st = mul(st, p);     }     int l = 1;     int r = n;     if (checkpal(1, n)) {       cout << x << endl;       return;     }     int necul = 0;     int necur = 0;     while (l < r) {       if (x[l - 1] != x[r - 1]) {         for (int(i) = (l); (i) <= (r); ++(i)) {           if (checkpal(l, i)) {             necul = i + 1;             necur = r;           }         }         for (int(i) = (r); (i) >= (l); --(i)) {           if (checkpal(i, r)) {             if ((i - 1 - l) < (necur - necul)) {               necur = i - 1;               necul = l;             }           }         }         break;       } else {         l++;         r--;       }     }     for (int(i) = (1); (i) <= (n); ++(i)) {       if (i >= necul && i <= necur) continue;       cout << x[i - 1];     }     cout << endl;   }   int main() {     ios_base::sync_with_stdio(false);     inverz[1000001] = power(power(p, 1000001), 1000000007 - 2);     for (int(i) = (1000000); (i) >= (0); --(i)) inverz[i] = mul(inverz[i + 1], p);     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, i;     cin >> n >> k;     long long int a[n + 1], b[n + 1];     pair<long long int, long long int> d[n + 1];     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < n; i++) cin >> b[i];     for (i = 0; i < n; i++) d[i] = make_pair(a[i] - b[i], i);     sort(d, d + n);     long long int s = 0;     for (i = 0; i < k; i++) s += a[d[i].second];     for (i = k; i < n; i++) {       s += min(a[d[i].second], b[d[i].second]);     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, w;     long long n;     cin >> k >> n >> w;     int l{};     for (int i = 1; i <= w; i++) {       l = l + i;     }     int s = (l * k) - n;     if (s > 0)       cout << s;     else       cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dev[5000005];   long long S[5000005];   long long pri[5000005];   void sieve() {     memset(dev, -1, sizeof dev);     int p = 0;     for (int i = 2; i <= 5000005; i++) {       if (dev[i] != -1) continue;       pri[p++] = i;       for (long long j = 1; j < 5000005; j++) {         if (i * j >= 5000005) break;         dev[i * j] = i;       }     }   }   int main() {     sieve();     for (int i = 1; i < 5000005; i++) {       int x = i;       while (x > 1) {         int d = dev[x];         S[i]++;         x /= d;       }     }     for (int i = 2; i <= 5000005; i++) {       S[i] += S[i - 1];     }     int t;     cin >> t;     while (t--) {       int a, b;       scanf( %d%d , &a, &b);       printf( %I64d n , S[a] - S[b]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 1e5 + 5;   ll f[N];   int n, ans, c;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lld , &f[i]);     for (int i = 2; i < n; i++) {       if (f[i] == f[i - 1] + f[i - 2]) {         c++;         ans = max(ans, c + 2);       } else {         c = 0;       }     }     printf( %d n , (ans == 0 || ans == 1) ? (n == 1 ? 1 : 2) : ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p[100001], w[100001];   vector<pair<int, int>> e[100001];   long long ans[100001], sum;   int root(int x) {     while (x != p[x]) x = p[x] = p[p[x]];     return x;   }   void pre(int x, int from) {     sum += w[x];     for (auto c : e[x])       if (c.first != from) pre(c.first, x);   }   int gao(int x, int from, int t) {     int cnt = w[x];     for (auto c : e[x]) {       if (c.first == from) continue;       cnt += gao(c.first, x, c.second);     }     e[x].clear();     ans[t] = cnt * (sum - cnt) * 2;     return cnt;   }   int main() {     int n;     scanf( %d , &n);     map<int, vector<pair<pair<int, int>, int>>> u;     for (int i = 1; i <= n; i++) w[p[i] = i] = 1;     for (int i = 1; i != n; i++) {       int x, y, c;       scanf( %d%d%d , &x, &y, &c);       u[c].push_back({{x, y}, i});     }     for (auto& r : u) {       vector<int> v;       for (auto c : r.second) {         int x = root(c.first.first);         int y = root(c.first.second);         int t = c.second;         e[x].push_back({y, t});         e[y].push_back({x, t});         v.push_back(x);         v.push_back(y);       }       sort(v.begin(), v.end());       v.resize(unique(v.begin(), v.end()) - v.begin());       for (auto x : v)         if (!e[x].empty()) {           pre(x, sum = 0);           gao(x, 0, 0);         }       for (auto c : r.second) {         int x = root(c.first.first);         int y = root(c.first.second);         if (w[x] < w[y])           w[p[x] = y] += w[x];         else           w[p[y] = x] += w[y];       }     }     long long tmp = *max_element(ans + 1, ans + n);     printf( %I64d %d n , tmp, count(ans + 1, ans + n, tmp));     for (int i = 1; i != n; i++)       if (ans[i] == tmp) printf( %d  , i);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   char c[maxn];   long long mod = 1e9 + 7;   long long ex(long long a, long long b) {     a = a % mod;     long long ans = 1;     while (b) {       if (b % 2 == 1) {         ans = (ans * a) % mod;       }       a = (a * a) % mod;       b = b / 2;     }     return ans;   }   int main() {     int k;     scanf( %s%d , c, &k);     long long sum = 0;     int len = strlen(c);     for (int i = 0; c[i]; i++) {       if (c[i] ==  0  || c[i] ==  5 ) {         long long a1 = ex(2, i);         long long q = ex(2, len);         long long ans = ((a1 % mod) * ((ex(q, k) - 1) % mod) % mod);         long long ni = ex(q - 1, mod - 2) % mod;         sum = (sum + (ans * ni) % mod) % mod;       }     }     printf( %I64d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   istream &operator>>(istream &is, vector<T> &arr);   template <typename T>   ostream &operator<<(ostream &os, vector<T> &arr);   int INF = 1e9 + 5;   long long C(int n, int m) {     long long ans = 1;     for (int i = 1; i <= m; i++) {       ans *= n;       ans /= i;       n--;     }     return ans;   }   int main() {     int n;     cin >> n;     vector<pair<int, string>> scores = {{0,  Kuro }, {0,  Shiro }, {0,  Katie }};     vector<string> tr(3);     cin >> tr;     for (int i = 0; i < 3; i++) {       map<char, int> charMap;       for (int j = 0; j < tr[i].length(); j++) {         charMap[tr[i][j]]++;       }       for (auto ca : charMap) {         int sc = ca.second;         int l = tr[i].length();         if (n == 1 && sc == l)           scores[i].first = max(scores[i].first, sc - 1);         else           scores[i].first = max(scores[i].first, min(sc + n, l));       }     }     sort(scores.begin(), scores.end());     if (scores[2].first == scores[1].first)       cout <<  Draw  << endl;     else       cout << scores[2].second << endl;   }   template <typename T>   istream &operator>>(istream &is, vector<T> &arr) {     typename vector<T>::iterator a;     for (a = arr.begin(); a != arr.end(); a++) {       is >> *a;     }     return is;   }   template <typename T>   ostream &operator<<(ostream &os, vector<T> &arr) {     typename vector<T>::iterator a;     for (a = arr.begin(); a != arr.end(); a++) {       os << *a <<    ;     }     os << endl;     return os;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       cout << n / 2 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, count = 0, max = 0;     char line;     cin >> n;     n++;     while (n--) {       char temp;       scanf( %c , &temp);       if (temp ==    ) {         if (count > max) max = count;         count = 0;       } else if (temp >=  A  && temp <=  Z )         count++;     }     if (count > max) max = count;     cout << max;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       if (y1 == y2)         cout << abs(x2 - x1) <<   n ;       else if (x1 == x2)         cout << abs(y2 - y1) <<   n ;       else {         int x, y;         x = abs(x2 - x1);         y = abs(y2 - y1);         cout << x + y + 2 <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   vector<int> graph[maxn];   vector<int> g[maxn];   bool mark[maxn];   int n, m;   int par[maxn];   int cm[maxn];   bool comp[maxn];   int mk[maxn];   int moalefe[maxn];   void dfs(int x, int y) {     mark[x] = 1;     cm[x] = y;     moalefe[y]++;     for (int v : graph[x])       if (!mark[v]) dfs(v, y);   }   void ddfs(int x) {     mk[x] = 1;     for (int v : g[x]) {       if (mk[v] == 0)         ddfs(v);       else if (mk[v] == 1)         comp[cm[v]] = 1;     }     mk[x] = 2;   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       int t1, t2;       cin >> t1 >> t2;       t1--;       t2--;       graph[t1].push_back(t2);       graph[t2].push_back(t1);       g[t1].push_back(t2);     }     int mo = 1;     for (int i = 0; i < n; i++) {       if (!mark[i]) {         dfs(i, mo);         mo++;       }     }     for (int i = 0; i < n; i++) {       if (mk[i] == 0) ddfs(i);     }     int ans = 0;     for (int i = 1; i < mo; i++) {       if (comp[i])         ans += moalefe[i];       else         ans += moalefe[i] - 1;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   const unsigned int M = 1000000007;   long long power(long long x, long long y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = x * res;       y = y >> 1;       x = x * x;     }     return res;   }   long long power(long long x, long long y, long long mod) {     long long res = 1;     while (y > 0) {       if (y & 1) res = x * res % mod;       y = y >> 1;       x = x * x % mod;     }     return res;   }   using namespace std;   void nik() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   signed main() {     nik();     long long test = 1;     cin >> test;     while (test--) {       long long one[100005] = {0};       string s;       cin >> s;       long long n = s.size();       s =     + s;       for (long long i = 1; i <= n; i++) {         if (s[i] ==  1 )           one[i] = one[i - 1] + 1;         else           one[i] = one[i - 1];       }       long long ans = min(one[n], n - one[n]);       if (ans == 0) {         cout << ans <<   n ;         continue;       }       for (long long i = 1; i <= n; i++) {         ans = min(ans, i - one[i] + one[n] - one[i]);       }       long long b = ans;       for (long long i = n; i >= 1; i--) {         b = min(b, (n - i + 1) - (one[n] - one[i - 1]) + one[i - 1]);       }       cout << min(ans, b) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<int> gru;     for (int i = 0; i < n; i++) {       gru.push_back(i + 1);     }     long long lead = 1;     for (int i = 0; i < k; i++) {       long long a;       cin >> a;       lead += a;       lead = lead % gru.size();       if (lead == 0) {         lead = gru.size();       }       cout << gru[lead - 1] <<    ;       gru.erase(gru.begin() + lead - 1);     }   }