malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count());   const long long maxn = 3e5 + 10;   struct E {     long long to, nxt;   } e[maxn * 2];   long long tot, head[maxn];   long long Add[maxn << 2], lz[maxn << 2];   void adde(long long u, long long v) {     tot++;     e[tot] = {v, head[u]};     head[u] = tot;   }   vector<pair<long long, long long> > v[maxn];   long long n;   long long ans[maxn];   void dfs(int u, int fa, int dep) {     int len = v[u].size();     for (int i = 0; i < len; i++) {       int d = min(n + 1, dep + v[u][i].first + 1);       Add[dep] += v[u][i].second;       Add[d] -= v[u][i].second;     }     Add[dep] += Add[dep - 1];     ans[u] = Add[dep];     for (int i = head[u]; i; i = e[i].nxt) {       int to = e[i].to;       if (to == fa) continue;       dfs(to, u, dep + 1);     }     for (int i = 0; i < len; i++) {       int d = min(n + 1, dep + v[u][i].first + 1);       Add[dep] -= v[u][i].second;       Add[d] += v[u][i].second;     }     Add[dep] -= Add[dep - 1];   }   int main() {     cin >> n;     long long t1, t2;     for (long long i = 1; i < n; i++) {       scanf( %lld%lld , &t1, &t2);       adde(t1, t2);       adde(t2, t1);     }     long long m, t3;     cin >> m;     for (long long i = 1; i <= m; i++) {       scanf( %lld%lld%lld , &t1, &t2, &t3);       v[t1].push_back(make_pair(t2, t3));     }     dfs(1, 1, 1);     for (int i = 1; i <= n; i++) printf( %lld  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   complex<double> a[524288], b[524288], r[524288], o[524289];   void fft(complex<double> *x, int n, int base) {     int size = 0, mask = (n >> 1) - 1;     for (int i = base - 1; i >= 0; --i, size = (size << 1) | 1, mask >>= 1) {       for (int j = 0; j < n; ++j) {         int front = j >> i, back = j & mask;         int a = (front & size) << 1, b = a | 1;         r[j] = x[(a << i) | back] +                o[(front << i) * (524288 / n)] * x[(b << i) | back];       }       for (int j = 0; j < n; ++j) x[j] = r[j];     }   }   void ifft(complex<double> *x, int n, int base) {     fft(x, n, base);     reverse(x + 1, x + n);     for (int i = 0; i < n; ++i) x[i] /= n;   }   void pin(vector<int> *v) {     cout << v->size() <<   : ;     for (int i : *v) cout << i <<    ;     cout << endl;   }   vector<int> *mg(vector<int> *pa, vector<int> *pb) {     int nn = pa->size() + pb->size() + 2, base = 0;     while ((1 << base) < nn) ++base;     nn = (1 << base);     for (int i = 0; i < pa->size(); ++i) a[i] = complex<double>((*pa)[i], 0);     for (int i = pa->size(); i <= nn; ++i) a[i] = complex<double>(0, 0);     for (int i = 0; i < pb->size(); ++i) b[i] = complex<double>((*pb)[i], 0);     for (int i = pb->size(); i <= nn; ++i) b[i] = complex<double>(0, 0);     fft(a, nn, base);     fft(b, nn, base);     for (int i = 0; i < nn; ++i) a[i] *= b[i];     ifft(a, nn, base);     vector<int> *rt = new vector<int>();     for (int i = 0; i < nn; ++i)       rt->push_back(((long long)(real(a[i]) + 0.5)) % 1009);     while (rt->size() && rt->back() == 0) rt->pop_back();     delete pa;     delete pb;     return rt;   }   int cnt[524288];   const double PI = acos(-1);   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (int i = 0; i <= 524288; ++i)       o[i] = exp(2. * i * PI / 524288 * complex<double>(0, 1));     int n, m, k;     cin >> n >> m >> k;     while (n--) {       int t;       cin >> t;       ++cnt[t];     }     auto cmp = [&](const vector<int> *a, const vector<int> *b) {       return a->size() > b->size();     };     priority_queue<vector<int> *, vector<vector<int> *>, decltype(cmp)> pq(cmp);     for (int i = 1; i <= m; ++i) {       if (cnt[i] == 0) continue;       vector<int> *v = new vector<int>();       for (int j = 0; j <= cnt[i]; ++j) v->push_back(1);       pq.push(v);     }     while (pq.size() > 1u) {       vector<int> *p1, *p2;       p1 = pq.top();       pq.pop();       p2 = pq.top();       pq.pop();       pq.push(mg(p1, p2));     }     vector<int> *ans = pq.top();     cout << (ans->size() > k ? (*ans)[k] : 0) << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, v;   map<int, int> mp;   vector<int> a;   bool can(int mid) {     int rem = 0;     for (int i = 0; i < a.size(); i++) {       if (mp[a[i]] >= mid) {         rem += mp[a[i]] / mid;       }     }     return rem >= k;   }   bool cmp(int s, int f) { return mp[s] > mp[f]; }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> v;       mp[v]++;     }     for (auto i : mp) {       a.push_back(i.first);     }     sort(a.begin(), a.end(), cmp);     int l, r, ans = 1;     l = 1, r = n;     while (l < r) {       int mid = (l + r) / 2;       if (can(mid)) {         l = mid + 1;         ans = mid;       } else {         r = mid;       }     }     n = a.size();     for (int i = 0; i < n; i++) {       if (k - mp[a[i]] / ans >= 0) {         for (int j = 0; j < mp[a[i]] / ans; j++) cout << a[i] <<    ;         k -= mp[a[i]] / ans;       } else {         for (int j = 0; j < k; j++) cout << a[i] <<    ;         break;       }     }   }
#include <bits/stdc++.h>   using namespace std;   void update(int *arr, int pos, int diff) {     while (pos <= 100001) {       arr[pos] = max(arr[pos], diff);       pos += pos & -pos;     }   }   int sum(int *arr, int pos) {     int ret = 0;     while (pos > 0) {       ret = max(ret, arr[pos]);       pos -= pos & -pos;     }     return ret;   }   int n, c, d;   int coin[100001], dia[100001];   int main() {     scanf( %d%d%d , &n, &c, &d);     int ans = 0;     for (int i = 0; i < n; i++) {       int b, p;       char k;       int coin_max, dia_max, m;       coin_max = 0;       dia_max = 0;       scanf( %d%d %c , &b, &p, &k);       if (k ==  C ) {         coin_max = sum(dia, d);         if (p > c) continue;         coin_max = max(coin_max, sum(coin, c - p));         update(coin, p, b);       } else {         dia_max = sum(coin, c);         if (p > d) continue;         dia_max = max(dia_max, sum(dia, d - p));         update(dia, p, b);       }       if (coin_max == 0 && dia_max == 0) continue;       m = max(b + coin_max, b + dia_max);       ans = max(ans, m);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   signed main() {     std::string s;     std::cin >> s;     long long n = s.size();     long long p = 0;     long long ttl = 0;     while (1) {       if (p == n) break;       while (p < n && std::isalpha(s[p])) ++p;       long long a = 0;       std::string tmp;       while (p < n && (s[p] ==  .  || std::isdigit(s[p]))) {         tmp.push_back(s[p]);         if (std::isdigit(s[p])) a = a * 10 + (s[p] -  0 );         ++p;       }       if (tmp.size() < 3 || tmp[tmp.size() - 3] !=  . ) a *= 100;       ttl += a;     }     long long integral = ttl / 100;     std::deque<char> output;     if (integral == 0) {       output.push_front( 0 );     } else {       long long dgt = 0;       while (integral) {         if (dgt == 3) {           dgt = 0;           output.push_front( . );         }         output.push_front(integral % 10 +  0 );         integral /= 10;         ++dgt;       }     }     long long fractional = ttl % 100;     if (fractional) {       output.push_back( . );       output.push_back( 0  + fractional / 10);       output.push_back( 0  + fractional % 10);     }     for (auto ch : output) std::cout << ch;     std::cout << std::endl;     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   vector<char> cc;   void solve() {     int n, m, k, d = 0;     cin >> n >> m >> k;     vector<string> arr(n), ans(n);     for (int i = 0; i < n; i++) {       cin >> arr[i];       ans[i].resize(m);       for (int j = 0; j < m; j++) d += (arr[i][j] ==  R );     }     vector<int> cnt;     for (int i = 0; i < k - d % k; i++) cnt.push_back(d / k);     for (int i = 0; i < d % k; i++) cnt.push_back(d / k + 1);     vector<int> have(k);     int ptr = 0;     for (int i = 0; i < n; i++) {       if (i % 2 == 0) {         for (int j = 0; j < m; j++) {           char cur = cc[ptr];           ans[i][j] = cur;           have[ptr] += (arr[i][j] ==  R );           if (have[ptr] == cnt[ptr] && ptr < k - 1) ptr++;         }       } else {         for (int j = m - 1; j >= 0; j--) {           char cur = cc[ptr];           ans[i][j] = cur;           have[ptr] += (arr[i][j] ==  R );           if (have[ptr] == cnt[ptr] && ptr < k - 1) ptr++;         }       }     }     for (int i = 0; i < n; i++) cout << ans[i] <<   n ;   }   int main() {     srand(1373737);     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     for (char c =  0 ; c <=  9 ; c++) cc.push_back(c);     for (char c =  a ; c <=  z ; c++) cc.push_back(c);     for (char c =  A ; c <=  Z ; c++) cc.push_back(c);     int t;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const double eps = (double)1e-8;   const int mod = (int)1000000007;   const int maxn = (int)1e6 + 5;   int n, l, r, z[2 * maxn];   string a, b, c, d;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     cin >> a;     cin >> b;     c =   ;     for (int i = (n - 1); i >= (0); i--) {       if (a[i] ==  W ) c +=  E ;       if (a[i] ==  E ) c +=  W ;       if (a[i] ==  N ) c +=  S ;       if (a[i] ==  S ) c +=  N ;     }     d = c +  #  + b;     l = 0;     r = 0;     z[0] = 0;     for (int i = (1); i < (d.size()); i++) {       z[i] = 0;       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < d.size() && d[i + z[i]] == d[z[i]]) {         z[i]++;       }       if (i + z[i] - 1 > r) {         l = i;         r = i + z[i] - 1;       }       if (i + z[i] - 1 == (int)d.size() - 1) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k, s, x, y, cur, pr, cans, tot, a[2005][2005], dp[4][2005][2005];   vector<pair<int, int> > all[10];   int ans[10][10];   int dist(int x2, int y2, int x3, int y3) { return abs(x2 - x3) + abs(y2 - y3); }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> k >> s;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> cur;         a[i][j] = cur;         all[cur].push_back({i, j});       }     }     for (int i = 1; i <= k; i++) {       for (int j = 0; j < 4; j++)         for (int z = 0; z <= n + 1; z++) {           for (int zr = 0; zr <= m + 1; zr++) {             dp[j][z][zr] = -1e9;           }         }       for (int j = 1; j <= n; j++) {         for (int z = 1; z <= m; z++) {           dp[0][j][z] = max(dp[0][j][z - 1] + 1, dp[0][j - 1][z] + 1);           if (a[j][z] == i) dp[0][j][z] = max(dp[0][j][z], 0);         }       }       for (int j = n; j >= 1; j--) {         for (int z = 1; z <= m; z++) {           dp[1][j][z] = max(dp[1][j][z - 1] + 1, dp[1][j + 1][z] + 1);           if (a[j][z] == i) dp[1][j][z] = max(dp[1][j][z], 0);         }       }       for (int j = 1; j <= n; j++) {         for (int z = m; z >= 1; z--) {           dp[2][j][z] = max(dp[2][j][z + 1] + 1, dp[2][j - 1][z] + 1);           if (a[j][z] == i) dp[2][j][z] = max(dp[2][j][z], 0);         }       }       for (int j = n; j >= 1; j--) {         for (int z = m; z >= 1; z--) {           dp[3][j][z] = max(dp[3][j][z + 1] + 1, dp[3][j + 1][z] + 1);           if (a[j][z] == i) dp[3][j][z] = max(dp[3][j][z], 0);         }       }       for (int j = i; j <= k; j++) {         cans = -1e9;         for (int z = 0; z < all[j].size(); z++) {           x = all[j][z].first;           y = all[j][z].second;           cans = max(cans, dp[0][x][y]);           cans = max(cans, dp[1][x][y]);           cans = max(cans, dp[2][x][y]);           cans = max(cans, dp[3][x][y]);         }         ans[i][j] = cans;       }     }     tot = -1e9;     for (int i = 0; i < s; i++) {       cin >> cur;       if (i) {         if (pr < cur)           tot = max(tot, ans[pr][cur]);         else           tot = max(tot, ans[cur][pr]);       }       pr = cur;     }     cout << tot;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const int K = 3;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int ans = -1;       for (int i = 1; i <= n; ++i) {         long long int x;         cin >> x;         ans = max(ans, x - i);       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     cin >> n;     long long ans = n;     for (int i = 2; i * i <= n; ++i)       if (n % i == 0) {         while (n % i == 0) n /= i, ans += n;       }     if (n > 1) ++ans;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[200200];   void solve() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     int pref = 0;     for (int i = 0; i < n; i++) {       if (a[i])         pref++;       else         break;     }     if (pref == n) {       printf( NO n );       return;     }     vector<int> rj;     if (pref % 2 == 1) {       for (int i = pref - 1; i < n - 2; i += 1) {         int xr = a[i] ^ a[i + 1] ^ a[i + 2];         int sm = a[i] + a[i + 1] + a[i + 2];         if (xr == 0) {           rj.push_back(i + 1);           a[i] = 0;           a[i + 1] = 0;           a[i + 2] = 0;           pref -= 1;           break;         } else {           if (sm == 1) {             rj.push_back(i + 1);             a[i + 1] = 1;             a[i + 2] = 1;             i += 1;             pref += 2;           } else if (sm == 3) {             i += 1;             pref += 2;           }         }       }     }     if (pref >= n) {       printf( NO n );       return;     }     if (pref % 2 == 1) {       printf( NO n );       return;     }     for (int i = pref - 1; i >= 1; i -= 2) {       rj.push_back(i);     }     for (int i = 0; i < pref; i++) a[i] = 0;     for (int i = pref; i < n - 2; i++) {       int xr = a[i] ^ a[i + 1] ^ a[i + 2];       int sm = a[i] + a[i + 1] + a[i + 2];       if (sm == 0) continue;       if (a[i]) {         if (xr == 1) {           rj.push_back(i + 1);           rj.push_back(i);           a[i] = 0;           i++;           a[i + 1] = 1;           a[i] = 0;           i--;         } else {           rj.push_back(i + 1);           a[i] = 0;           a[i + 1] = 0;           a[i + 2] = 0;         }       } else {         if (xr == 0) {           rj.push_back(i + 1);           a[i] = 0;           a[i + 1] = 0;           a[i + 2] = 0;         }       }     }     for (int i = 0; i < n; i++) {       if (a[i]) {         printf( NO n );         return;       }     }     printf( YES n%d n , rj.size());     for (int i : rj) {       printf( %d  , i);     }     printf(  n );   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 5;   struct Edge {     int to, nxt;   } e[N * 2];   int n, head[N], cntE, siz[N], f[N][2];   int ans[N], g[N], mxpt[N], pos[N];   inline void add(int u, int v) {     e[++cntE] = (Edge){v, head[u]};     head[u] = cntE;   }   void dfs1(int u, int fa) {     siz[u] = 1;     for (int i = head[u], v; i; i = e[i].nxt)       if ((v = e[i].to) != fa) {         dfs1(v, u);         siz[u] += siz[v];         if (siz[v] > siz[mxpt[u]]) mxpt[u] = v;         int w = siz[v] <= n / 2 ? siz[v] : f[v][0];         if (f[u][0] < w)           f[u][1] = f[u][0], f[u][0] = w, pos[u] = v;         else           f[u][1] = max(f[u][1], w);       }   }   void dfs2(int u, int fa) {     if (siz[mxpt[u]] > n / 2)       ans[u] = siz[mxpt[u]] - f[mxpt[u]][0] <= n / 2;     else       ans[u] = n - siz[u] - g[u] <= n / 2;     for (int i = head[u], v; i; i = e[i].nxt)       if ((v = e[i].to) != fa) {         int w = (n - siz[u] <= n / 2) ? n - siz[u] : g[u];         g[v] = max(w, f[u][pos[u] == v]);         dfs2(v, u);       }   }   int main() {     scanf( %d , &n);     for (int i = 1, a, b; i < n; ++i) {       scanf( %d%d , &a, &b);       add(a, b);       add(b, a);     }     dfs1(1, 0);     dfs2(1, 0);     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   pair<int, int> a[200];   pair<int, int> b[200];   int pp[200];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       int aa, bb;       scanf( %d%d , &aa, &bb);       a[i].first = min(aa, bb), a[i].second = max(aa, bb);     }     for (int i = 1; i <= m; i++) {       int aa, bb;       scanf( %d%d , &aa, &bb);       b[i].first = min(aa, bb), b[i].second = max(aa, bb);     }     for (int i = 1; i <= n; i++) {       bool flag1 = false, flag2 = false;       for (int j = 1; j <= m; j++) {         if (a[i].first != b[j].first && a[i].second == b[j].second)           pp[b[j].second] = 1, flag1 = true;         if (a[i].second != b[j].second && a[i].first == b[j].first)           pp[b[j].first] = 1, flag2 = true;         if (a[i].second != b[j].first && a[i].first == b[j].second)           pp[b[j].second] = 1, flag2 = true;         if (a[i].first != b[j].second && a[i].second == b[j].first)           pp[b[j].first] = 1, flag1 = true;         if (flag1 && flag2 && a[i].first != a[i].second) {           cout << -1 << endl;           return 0;         }       }     }     for (int i = 1; i <= m; i++) {       bool flag1 = false, flag2 = false;       for (int j = 1; j <= n; j++) {         if (a[j].first != b[i].first && a[j].second == b[i].second) flag2 = true;         if (a[j].second != b[i].second && a[j].first == b[i].first) flag1 = true;         if (a[j].second != b[i].first && a[j].first == b[i].second) flag2 = true;         if (a[j].first != b[i].second && a[j].second == b[i].first) flag1 = true;         if (flag1 && flag2 && b[i].first != b[i].second) {           cout << -1 << endl;           return 0;         }       }     }     int sum = 0;     int ans;     for (int i = 1; i <= 9; i++) {       if (pp[i]) ans = i;       sum += pp[i];     }     if (sum == 1) {       cout << ans << endl;       return 0;     } else {       cout << 0 << endl;       return 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)2e5 + 10;   vector<int> res[maxn];   int main() {     string s;     cin >> s;     stack<int> st, st2;     int ma = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  1 ) {         if (st.empty()) {           cout << -1 << endl;           return 0;         }         res[st.top()].push_back(i + 1);         st2.push(st.top());         st.pop();       } else {         if (!st2.empty()) {           res[st2.top()].push_back(i + 1);           st.push(st2.top());           st2.pop();         } else {           res[ma].push_back(i + 1);           st.push(ma);           ma++;         }       }     }     if (!st2.empty()) {       cout << -1 << endl;       return 0;     }     int cnt = 0;     for (int i = 0; i < maxn; i++)       if (!res[i].empty()) cnt++;     cout << cnt << endl;     for (int i = 0; i < maxn; i++)       if (!res[i].empty()) {         printf( %d , res[i].size());         for (int j = 0; j < res[i].size(); j++) {           printf(  %d , res[i][j]);         }         printf(  n );       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 255;   int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};   char s[MX][MX];   long long tot[MX][MX];   int was[MX][MX];   int cur[MX][MX];   int main() {     int n, m, p, w;     scanf( %d %d %d %d , &n, &m, &p, &w);     for (int i = 1; i <= n; i++) {       scanf( %s , s[i]);       for (int j = m; j >= 1; j--) {         s[i][j] = s[i][j - 1];       }       s[i][0] =   0 ;     }     int cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (s[i][j] >=  A  && s[i][j] <=  Z ) {           cnt++;           queue<pair<int, int> > q;           q.push(make_pair(i, j));           cur[i][j] = p * (s[i][j] -  A  + 1);           was[i][j] = cnt;           while (!q.empty()) {             int xx = q.front().first, yy = q.front().second;             q.pop();             tot[xx][yy] += cur[xx][yy];             for (int dir = 0; dir < 4; dir++) {               int nx = xx + dx[dir], ny = yy + dy[dir];               if (was[nx][ny] != cnt && cur[xx][yy] / 2 > 0 && s[nx][ny] !=  *  &&                   nx >= 1 && nx <= n && ny >= 1 && ny <= m) {                 was[nx][ny] = cnt;                 cur[nx][ny] = cur[xx][yy] / 2;                 q.push(make_pair(nx, ny));               }             }           }         }       }     }     int res = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (tot[i][j] > w) res++;       }     }     printf( %d n , res);   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ans;   int p[2000], damage[2000], u[2000];   int main() {     int n, Max, r, used = 0, TT;     cin >> n >> Max >> r;     for (int i = 1; i <= n; i++) cin >> p[i] >> damage[i];     int cap = Max;     memset(u, 0, sizeof(u));     for (int s = 0;; s++) {       int cur = -1;       for (int i = 1; i <= n; i++)         if (!u[i] && p[i] / 100.00 >= Max / double(cap))           if (cur == -1 || damage[i] > damage[cur]) cur = i;       if (cur == -1 && r >= 0) {         cout <<  NO  << endl;         return 0;       }       if (cur != -1) {         r -= damage[cur];         u[cur] = 1;         used++;         ans.push_back(make_pair(s, cur));       }       Max += r;       if (Max > cap) Max = cap;       if (Max <= 0) {         TT = s + 1;         break;       }     }     cout <<  YES  << endl;     cout << TT <<     << ans.size() << endl;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].first <<     << ans[i].second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 100001;   bool ok;   long long int n, q;   long long int trie[6 * N][5], done[6 * N];   long long int nxt = 1;   string s;   void insert(string s) {     long long int node = 0;     for (char c : s) {       if (trie[node][c -  a ] == 0) {         node = trie[node][c -  a ] = nxt++;       } else {         node = trie[node][c -  a ];       }     }     done[nxt - 1] = 1;   }   void dfs(long long int u, long long int idx, long long int f) {     if (f >= 2) return;     if (idx == s.length() - 1 && f == 1 && done[u]) {       ok = 1;       return;     }     if (idx == s.length() - 1 || done[u]) return;     for (long long int i = 0; i < 3; i++) {       if (trie[u][i] == 0) continue;       if (s[idx + 1] == (char)(i +  a ))         dfs(trie[u][i], idx + 1, f);       else         dfs(trie[u][i], idx + 1, f + 1);     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (long long int i = 0; i < n; i++) {       cin >> s;       insert(s);     }     for (long long int i = 0; i < q; i++) {       cin >> s;       ok = 0;       dfs(0, -1, 0);       cout << (ok ?  YES n  :  NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ar[310];   bool mp[310][310];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> ar[i];     string str;     for (int i = 1; i <= n; i++) {       cin >> str;       for (int j = 0; j < n; j++)         if (str[j] ==  1 ) mp[i][j + 1] = true;     }     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++)           if (mp[i][k] && mp[k][j]) mp[i][j] = true;     for (int i = 1; i < n; i++)       for (int j = i + 1; j <= n; j++)         if (ar[i] > ar[j] && mp[i][j]) swap(ar[i], ar[j]);     cout << ar[1];     for (int i = 2; i <= n; i++) cout <<     << ar[i];     cout <<   n ;   }