malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, k;   vector<pair<long long, long long> > nums;   int main() {     cin >> n;     nums.resize(n);     for (int i = 0; i < n; i++) {       long long a, b;       cin >> a >> b;       nums[i] = make_pair(b, a);     }     sort(nums.begin(), nums.end());     long long res = 0;     long long left;     long long fac = 1;     long long last_left = 0;     cin >> k;     vector<long long> ks(k + 1);     for (int i = 0; i < k; i++) cin >> ks[i];     ks[k] = -1;     long long k_cnt = 0;     left = ks[0];     for (int i = 0; i < n; i++) {       if (left == -1) {         res += fac * nums[i].second * nums[i].first;         continue;       }       if (left <= nums[i].second) {         res += fac * left * nums[i].first;         nums[i].second -= left;         i--;         k_cnt++;         if (ks[k_cnt] == -1)           left = -1;         else           left = (ks[k_cnt] - ks[k_cnt - 1]);         fac++;       } else {         res += fac * nums[i].second * nums[i].first;         left -= nums[i].second;         nums[i].second = 0;       }     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 100;   int arr[maxn];   int cum[maxn], n, m, k;   int dp[2020];   map<int, int> offer;   int range(int l, int r) { return (cum[r] - cum[l]); }   int solve(int i) {     if (i == k) return 0;     int &ret = dp[i];     if (ret != -1) return ret;     ret = 1e9;     for (int j = i + 1; j <= k; j++) {       int buy = (j - i);       int of = offer[buy];       int taka = range(i, j);       int offtaka = range(i, i + of);       ret = min(ret, taka - offtaka + solve(j));     }     return ret;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     memset(dp, -1, sizeof dp);     for (int i = 1; i <= n; i++) scanf( %d , &arr[i]);     sort(arr + 1, arr + n + 1);     for (int i = 1; i <= m; i++) {       int xi, yi;       scanf( %d%d , &xi, &yi);       offer[xi] = max(offer[xi], yi);     }     for (int i = 1; i <= k; i++) cum[i] = cum[i - 1] + arr[i];     cout << solve(0) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   FILE *in = stdin;   class node {    public:     long long sum[5], hnode;     node() {       sum[0] = sum[1] = sum[2] = sum[3] = sum[4] = hnode = 0;       return;     }     node(int x) {       for (int q = 0; q < 5; q++) {         sum[q] = 0;       }       sum[0] = x;       hnode = 1;       return;     }   };   node merge(node a, node b) {     node c;     c.hnode = a.hnode + b.hnode;     int left = a.hnode % 5;     for (int q = 0; q < 5; q++) {       c.sum[q] = a.sum[q] + b.sum[(5 - left + q) % 5];     }     return c;   }   class segment_tree {    public:     node tree[4 * 100000];     segment_tree() {       for (int q = 0; q < 4 * 100000; q++) {         tree[q] = node();       }       return;     }     void add(int i, int l, int r, int x, int v, int typ) {       if (l == r) {         if (typ == 0) {           tree[i] = node(v);         } else {           tree[i] = node();         }         return;       }       int mid = (l + r) >> 1;       if (x <= mid)         add(i * 2, l, mid, x, v, typ);       else         add(i * 2 + 1, mid + 1, r, x, v, typ);       tree[i] = merge(tree[i * 2], tree[i * 2 + 1]);       return;     }     long long sum() { return tree[1].sum[2]; }   };   int N, a[100000][2];   set<int> heap;   segment_tree seg;   map<int, int> ind;   int main() {     fscanf(in,  %d , &N);     for (int q = 0; q < N; q++) {       char qq[22];       int x;       fscanf(in,  %s , qq);       if (qq[0] ==  s ) {         a[q][0] = -1;       } else {         fscanf(in,  %d , &x);         if (qq[0] ==  a )           a[q][0] = 0;         else           a[q][0] = 1;         a[q][1] = x;         heap.insert(x);       }     }     int cind = 0;     for (set<int>::iterator it = heap.begin(); it != heap.end(); it++) {       ind[*it] = cind;       cind++;     }     for (int q = 0; q < N; q++) {       if (a[q][0] == -1) {         printf( %I64d n , seg.sum());       } else {         seg.add(1, 0, cind - 1, ind[a[q][1]], a[q][1], a[q][0]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, nrh, nrt, nod1, nod2, d[100006], x, nr1, nr2, nra;   vector<int> G[100006];   vector<int> sters;   int uz[100006], uz2[100006];   int main() {     int i, j, k;     cin >> n >> m >> nrh >> nrt;     for (i = 1; i <= m; i++) {       cin >> nod1 >> nod2;       G[nod1].push_back(nod2);       G[nod2].push_back(nod1);       d[nod1]++;       d[nod2]++;     }     for (i = 1; i <= n; i++)       if (d[i] > nrh) uz[i] = 1;     for (i = 1; i <= n; i++) {       if (d[i] > nrt) {         for (j = 0; j < G[i].size(); j++) {           if (uz[G[i][j]]) {             nra = nr1 = nr2 = 0;             x = G[i][j];             for (k = 0; k < G[i].size(); k++) {               if (k != j) {                 uz2[G[i][k]]++;                 nr1++;                 sters.push_back(G[i][k]);               }             }             for (k = 0; k < G[x].size(); k++) {               if (G[x][k] != i) {                 uz2[G[x][k]]++;                 if (uz2[G[x][k]] == 2)                   nra++;                 else                   sters.push_back(G[x][k]);                 nr2++;               }             }             if (nr1 + nr2 - nra < nrt + nrh) {               for (k = 0; k < sters.size(); k++) uz2[sters[k]] = 0;               sters.clear();               continue;             }             cout <<  YES n ;             cout << G[i][j] <<     << i <<   n ;             for (k = 0; k < G[x].size() && nrh; k++) {               if (uz2[G[x][k]] == 1) {                 cout << G[x][k] <<    ;                 nrh--;               }             }             for (k = 0; nrh; k++) {               if (uz2[G[x][k]] == 2) {                 uz2[G[x][k]] = 0;                 cout << G[x][k] <<    ;                 nrh--;               }             }             cout <<   n ;             for (k = 0; k < G[i].size() && nrt; k++) {               if (uz2[G[i][k]]) {                 cout << G[i][k] <<    ;                 nrt--;               }             }             return 0;           }         }       }     }     cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     unordered_map<string, string> gr;     set<string> ending;     for (int i = 0; i < q; i++) {       string prev, next;       cin >> prev >> next;       gr[prev] = next;       ending.insert(next);     }     vector<pair<string, string>> ans;     for (auto it : gr) {       if (ending.count(it.first)) continue;       string cur = it.first;       while (gr.count(cur)) {         cur = gr[cur];       }       ans.push_back({it.first, cur});     }     cout << ans.size() <<   n ;     for (auto it : ans) cout << it.first <<     << it.second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 510;   const int MAXS = 110;   const int MAXM = 1E5 + 10;   const double EPS = 1E-8;   int siz, n, m, k, ban[MAXN], pos[MAXN], U[MAXM], V[MAXM], d[MAXN], id[MAXN];   vector<int> G[MAXN];   struct Mat {     double a[MAXS][MAXS];     Mat() { memset(a, 0, sizeof a); }     double* operator[](int x) { return a[x]; }     const double* operator[](int x) const { return a[x]; }     Mat operator*(const Mat& rhs) const {       Mat ret;       for (int i = 0; i <= siz; i++)         for (int j = 0; j <= siz; j++)           for (int k = 0; k <= siz; k++) ret[i][j] += a[i][k] * rhs[k][j];       return ret;     }   } trans;   Mat ksm(Mat a, long long b) {     Mat ret;     for (int i = 0; i <= siz; i++) ret[i][i] = 1;     for (; b; b >>= 1, a = a * a)       if (b & 1) ret = ret * a;     return ret;   }   double A[MAXN][MAXN << 1], C[MAXN], X[MAXN];   void Guass() {     for (int i = 1; i <= n; i++) {       int pos = i;       for (int j = i + 1; j <= n; j++)         if (abs(A[j][i]) > abs(A[pos][i])) pos = j;       if (abs(A[pos][i]) < EPS) throw  No Matrix Inverse ;       for (int j = 1; j <= 2 * n; j++) swap(A[pos][j], A[i][j]);       for (int j = i + 1; j <= n; j++) {         if (abs(A[j][i]) < EPS) continue;         double coef = A[j][i] / A[i][i];         for (int k = 1; k <= 2 * n; k++) A[j][k] -= A[i][k] * coef;       }     }     for (int i = n; i; i--) {       double inv = 1.0 / A[i][i];       for (int j = 1; j <= 2 * n; j++) A[i][j] = A[i][j] * inv;       for (int j = i - 1; j; j--) {         double coef = A[j][i] / A[i][i];         for (int k = 1; k <= 2 * n; k++) A[j][k] -= A[i][k] * coef;       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) swap(A[i][j], A[i][j + n]);   }   void solve() {     for (int i = 1; i <= n; i++) X[i] = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) X[j] += C[i] * A[j][i];   }   int main() {     scanf( %d%d%d , &n, &m, &k);     pos[++siz] = 1;     id[1] = 1;     for (int i = 1; i <= n; i++) {       scanf( %d , &ban[i]);       if (ban[i]) pos[++siz] = i, id[i] = siz;     }     for (int i = 1; i <= n; i++) A[i][i] = A[i][i + n] = 1;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &U[i], &V[i]);       G[U[i]].push_back(V[i]);       G[V[i]].push_back(U[i]);       ++d[U[i]];       ++d[V[i]];     }     for (int i = 1; i <= m; i++) {       int u = U[i], v = V[i];       if (!ban[u]) A[v][u] -= 1.0 / d[u];       if (!ban[v]) A[u][v] -= 1.0 / d[v];     }     Guass();     for (int i = 1; i <= siz; i++) {       memset(C, 0, sizeof C);       int x = pos[i];       if (x == 1)         C[1] = 1, solve();       else {         C[x] = 1;         for (int v : G[x]) C[v] += 1.0 / d[x];         solve();       }       double tot = 1;       for (int j = 1; j <= n; j++)         if (ban[j] && j != x) trans[i][id[j]] = X[j], tot -= X[j];       trans[i][i] = tot;     }     trans[siz][0] = 1;     Mat res;     res[0][1] = 1;     res = res * ksm(trans, k);     printf( %.10lf n , res[0][0]);   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, k;     cin >> n >> k;     long long a1[n], b1[n];     for (int i = 0; i < n; i++) cin >> a1[i] >> b1[i];     long long dp[n][k];     vector<long long> a, b;     vector<pair<pair<long long, long long>, long long> > e;     vector<long long> ans;     long long mx = 0;     for (int p = 0; p < n; p++) {       a.clear();       b.clear();       e.clear();       for (int i = 0; i < n; i++)         if (i == p)           continue;         else           e.push_back(make_pair(make_pair(b1[i], a1[i]), i));       sort(e.begin(), e.end());       a.push_back(0);       b.push_back(0);       for (int i = 0; i < e.size(); i++) {         a.push_back(e[i].first.second);         b.push_back(e[i].first.first);       }       for (int i = 0; i < n; i++)         for (int j = 0; j < k; j++) dp[i][j] = -1e9;       dp[0][0] = 0;       for (int i = 1; i < n; i++)         for (int j = 0; j < k; j++) {           dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i] * (k - 1));           if (j > 0)             dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + b[i] * (j - 1) + a[i]);         }       if (mx < a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]) {         mx = a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1];         vector<long long> x, bad;         int i = n - 1, j = k - 1;         while (dp[i][j] > 0) {           if (j > 0) {             if (dp[i - 1][j - 1] + b[i] * (j - 1) + a[i] == dp[i][j]) {               x.push_back(e[i - 1].second + 1);               i--;               j--;             } else {               bad.push_back(-e[i - 1].second - 1);               bad.push_back(e[i - 1].second + 1);               i--;             }           } else {             bad.push_back(-e[i - 1].second - 1);             bad.push_back(e[i - 1].second + 1);             i--;           }         }         reverse(x.begin(), x.end());         reverse(bad.begin(), bad.end());         for (int i = 0; i < bad.size(); i++) x.push_back(bad[i]);         x.push_back(p + 1);         ans = x;       }     }     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout <<   n ;   }   int main() {     ios::sync_with_stdio(0);     int q;     cin >> q;     while (q--) solve();   }
#include <bits/stdc++.h>   using namespace std;   vector<long long int> v;   long long int a[120100], n, dd;   map<int, int> mp;   int main() {     for (long long int i = 1; i <= 2000000000; i *= 2) {       v.push_back(i);     }     cin >> n;     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);       mp[a[i]]++;     }     sort(a, a + n);     int f, res = 0;     for (int i = 0; i < n; i++) {       f = 0;       for (int j = 0; j < v.size(); j++) {         if (v[j] <= a[i]) continue;         dd = v[j] - a[i];         if (dd == a[i]) {           if (mp[a[i]] >= 2) {             f = 1;             break;           }           continue;         }         if (mp[dd] >= 1) {           f = 1;           break;         }       }       if (f == 0) res++;     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     long long a, b, c;   } e[100005];   int cmp(node a, node b) { return a.c > b.c; }   int main() {     int n, f, i;     cin >> n >> f;     for (i = 1; i <= n; i++) {       scanf( %lld%lld , &e[i].a, &e[i].b);       if (e[i].a >= e[i].b) {         e[i].c = 0;       } else {         e[i].c = min(e[i].a * 2, e[i].b) - e[i].a;       }     }     sort(e + 1, e + 1 + n, cmp);     long long ans = 0;     for (i = 1; i <= f; i++) {       ans += min(2 * e[i].a, e[i].b);     }     for (; i <= n; i++) {       ans += min(e[i].a, e[i].b);     }     printf( %lld , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100010;   struct adjList {     int id, nxt;   } Lst[MaxN * 2];   int LCnt, Head[MaxN];   int N, w[MaxN], root;   void addEdge(int a, int b) {     Lst[LCnt].id = b;     Lst[LCnt].nxt = Head[a];     Head[a] = LCnt++;   }   pair<long long, long long> dfs(int id, int pre) {     vector<pair<long long, long long> > v;     for (int x = Head[id]; x != -1; x = Lst[x].nxt) {       if (Lst[x].id != pre) {         v.push_back(dfs(Lst[x].id, id));       }     }     sort(v.begin(), v.end());     long long sum = 1, rem = 0;     w[id]--;     for (int i = (int)(v.size()) - 1; i >= 0; i--) {       if (w[id] == 0) break;       w[id]--;       sum += v[i].first + 1;       rem += v[i].second;     }     if (w[id]) {       sum += 2 * min((long long)w[id], rem);       w[id] -= min((long long)w[id], rem);     }     return make_pair(sum, w[id]);   }   int main() {     while (cin >> N) {       memset(Head, -1, sizeof(Head));       LCnt = 0;       for (int i = 1; i <= N; i++) {         scanf( %d , w + i);       }       for (int i = 1; i < N; i++) {         int a, b;         scanf( %d %d , &a, &b);         addEdge(a, b);         addEdge(b, a);       }       cin >> root;       w[root]++;       cout << dfs(root, -1).first - 1 << endl;       ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000000000;   map<int, int> mp, mp2;   map<int, int>::iterator it;   int n, m;   int tmp[3] = {2, 3, 5}, flag, t;   void bfs(int val, int cnt) {     queue<int> q;     mp[val] = cnt;     q.push(val);     while (!q.empty()) {       int x = q.front();       q.pop();       for (int i = 0; i < 3; ++i) {         if (x % tmp[i] == 0 && x != 0) {           if (mp.count(x / tmp[i]) == 0) {             mp[x / tmp[i]] = mp[x] + 1;             q.push(x / tmp[i]);           }         }       }     }   }   void bfs1(int val, int cnt) {     if (mp.count(val) != 0) {       flag = mp[val];       return;     }     queue<int> q;     mp2[val] = cnt;     q.push(val);     while (!q.empty() && flag == -1) {       int x = q.front();       q.pop();       for (int i = 0; i < 3 && flag == -1; ++i) {         if (x % tmp[i] == 0 && x != 0 && flag == -1) {           if (mp.count(x / tmp[i]) != 0 && flag == -1) {             flag = mp[x / tmp[i]] + mp2[x] + 1;             return;           }           if (mp2.count(x / tmp[i]) == 0) {             mp2[x / tmp[i]] = mp2[x] + 1;             q.push(x / tmp[i]);           }         }       }     }   }   int main() {     while (cin >> n >> m) {       if (n == m) {         cout <<  0 n ;         continue;       }       mp.clear();       mp2.clear();       t = 0;       bfs(n, 0);       flag = -1;       bfs1(m, 0);       cout << flag << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, k;     cin >> n >> m >> k;     if (n > m) swap(n, m);     set<long long> vals;     for (long long i = 1; i <= n; ++i) {       if (vals.count(n / i)) {         for (long long j = n / i; j >= 1; --j) {           vals.insert(j);         }         break;       }       vals.insert(n / i);     }     long long res = -1;     int diff = 2;     vals.insert(n);     vals.insert(m);     for (long long x : vals) {       for (long long i = n / x - diff; i <= n / x + diff; ++i) {         if (i < 0 || i >= n) continue;         long long j = k - i;         if (j < 0 || j >= m) continue;         res = max(res, 1LL * (n / (i + 1)) * (m / (j + 1)));       }       for (long long i = x - diff; i <= x + diff; ++i) {         if (i < 0 || i >= n) continue;         long long j = k - i;         if (j < 0 || j >= m) continue;         res = max(res, 1LL * (n / (i + 1)) * (m / (j + 1)));       }     }     if (k < n) res = max(res, 1LL * m * (n / (k + 1)));     if (k < m) res = max(res, 1LL * n * (m / (k + 1)));     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int Mod = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double e = exp(1);   const double PI = acos(-1);   const double ERR = 1e-10;   int main() {     long long n;     scanf( %lld , &n);     long long ans = (1LL * (1 + n) * n) / 2;     if (ans % 2 == 0)       printf( 0 n );     else       printf( 1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 500;   const int M = 1e6 + 500;   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   const int BASE = 31337;   const int LOG = 18;   const int OFF = (1 << LOG);   const double EPS = 1e-9;   const double PI = 3.1415926535;   int blok[M], n, bio[M], pref[M], lft[M], q, k;   int get(int l, int r) {     if (l <= 0) return pref[r];     return pref[r] - pref[l - 1];   }   int check(int x) {     int sol = 0;     for (int i = 0; i < n; i++) {       if (blok[i]) {         if (lft[i] <= i - x) {           return -1;         } else {           i = lft[i] + x - 1;           sol++;         }       } else {         i += x - 1;         sol++;       }     }     return sol;   }   int main() {     scanf( %d%d%d , &n, &q, &k);     for (int i = 0; i < q; i++) {       int x;       scanf( %d , &x);       blok[x] = 1;     }     if (blok[0])       lft[0] = -INF;     else       lft[0] = 0;     for (int i = 1; i < n; i++) {       if (!blok[i])         lft[i] = i;       else         lft[i] = lft[i - 1];     }     long long fin = (long long)INF * INF;     for (int i = 1; i <= k; i++) {       int x;       scanf( %d , &x);       int y = check(i);       if (y == -1) continue;       fin = min(fin, (long long)x * y);     }     if (fin == (long long)INF * INF)       printf( -1 n );     else       printf( %I64d n , fin);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long o = 101;   const long long p = 6e4;   struct qwq {     long long x, y;   } a[o], b[o];   long long n, m, l, r, f[p + 1], ans = -1;   bool cmp1(qwq u, qwq v) { return u.x == v.x ? u.y > v.y : u.x < v.x; }   bool cmp2(qwq u, qwq v) { return u.x + u.y > v.x + v.y; }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (long long i = 1, x, y; i <= n; i++) {       cin >> x >> y;       if (y >= 0)         a[++l] = (qwq){x, y};       else         b[++r] = (qwq){x, y};     }     memset(f, -1, sizeof(f)), f[m] = 0;     sort(a + 1, a + l + 1, cmp1), sort(b + 1, b + r + 1, cmp2);     for (long long i = 1; i <= l; i++)       for (long long j = p; j >= a[i].x; j--)         if (f[j] != -1) f[j + a[i].y] = max(f[j] + 1, f[j + a[i].y]);     for (long long i = 1; i <= r; i++)       for (long long j = max(b[i].x, -b[i].y); j <= p; j++)         if (f[j] != -1) f[j + b[i].y] = max(f[j] + 1, f[j + b[i].y]);     for (long long i = 0; i <= p; i++) ans = max(f[i], ans);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> g[5000];   int n, m;   int c[100][100];   char t[100][100];   bool used[100][100];   char clr[5000];   bool valid(int i, int j) { return (0 <= i && i < n && 0 <= j && j < m); };   int di[4] = {-1, 1, 0, 0};   int dj[4] = {0, 0, -1, 1};   void dfs(int i, int j, int color) {     used[i][j] = true;     c[i][j] = color;     for (int k = 0; k < 4; k++) {       int i1 = i + di[k];       int j1 = j + dj[k];       if (valid(i1, j1) && t[i1][j1] == t[i][j] && !used[i1][j1])         dfs(i1, j1, color);     }   }   void add_edge(int i, int j) {     g[i].push_back(j);     g[j].push_back(i);   }   void dfs1(int i, int j) {     used[i][j] = true;     for (int k = 0; k < 4; k++) {       int i1 = i + di[k];       int j1 = j + dj[k];       if (valid(i1, j1)) {         if (t[i1][j1] == t[i][j] && !used[i1][j1]) dfs1(i1, j1);         if (t[i1][j1] != t[i][j]) add_edge(c[i1][j1], c[i][j]);       }     }   }   int l = 0;   bool _used[10000];   int h[3000];   int q[10000];   int bfs(int v) {     for (int i = 0; i < l; i++) {       _used[i] = false;       h[i] = 1000 * 1000 * 1000;     };     int rp = 0;     int wp = 1;     q[0] = v;     h[v] = 0;     while (rp != wp) {       v = q[rp];       rp++;       for (int i = 0; i < g[v].size(); i++) {         int u = g[v][i];         if (h[u] > h[v] + 1) {           h[u] = h[v] + 1;           q[wp] = u;           wp++;         }       }     }     int vmax = 0;     for (int i = 0; i < l; i++) vmax = max(vmax, h[i]);     return vmax;   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) scanf( %c , &t[i][j]);       scanf(  n );     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!used[i][j]) {           clr[l] = t[i][j];           dfs(i, j, l++);         }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) used[i][j] = false;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!used[i][j]) dfs1(i, j);     int best = 1000 * 1000 * 1000;     for (int i = 0; i < l; i++) {       int cur = bfs(i);       if (clr[i] ==  W  && (cur & 1)) cur++;       if (clr[i] ==  B  && (cur % 2 == 0)) cur++;       best = min(best, cur);     }     cout << best << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 22;   int h, g;   long long a[N];   vector<int> ans;   int path[25];   long long pull(int u) {     long long tp = a[u];     int cnt = 0;     path[cnt++] = u;     a[u] = 0;     while ((u << 1) < (1 << h) && max(a[u << 1], a[(u << 1) | 1]) > 0) {       if (a[u << 1] > a[(u << 1) | 1]) {         swap(a[u << 1], a[u]);         u <<= 1;       } else {         swap(a[(u << 1) | 1], a[u]);         u = (u << 1) | 1;       }       path[cnt++] = u;     }     if (u < (1 << g)) {       for (int i = cnt - 1; i >= 0; i--) {         a[path[i]] = a[path[i - 1]];       }       a[path[0]] = tp;       return 0;     }     return tp;   }   void solve() {     ans.clear();     cin >> h >> g;     long long sum = 0;     for (int i = 1; i <= (1 << h) - 1; i++) {       cin >> a[i];       sum += a[i];     }     for (int i = 1; i <= (1 << g) - 1; i++) {       while (true) {         long long v = pull(i);         if (v) {           sum -= v;           ans.push_back(i);         } else           break;       }     }     cout << sum << endl;     for (auto v : ans) cout << v <<    ;     cout << endl;   }   int main() {     int t;     ios::sync_with_stdio(0);     cin >> t;     while (t--) solve();     return 0;   }