fork download
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define PB push_back
  4. #define int long long
  5. #define st first
  6. #define nd second
  7. #define rd third
  8. #define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
  9. #define RE(i, n) FOR(i, 1, n)
  10. #define FORD(i, a, b) for(int i = (a); i >= (b); --i)
  11. #define REP(i, n) for(int i = 0;i <(n); ++i)
  12. #define VAR(v, i) __typeof(i) v=(i)
  13. #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
  14. #define ALL(x) (x).begin(), (x).end()
  15. #define SZ(x) ((int)(x).size())
  16. #define __builtin_ctz __builtin_ctzll
  17. #define __builtin_clz __builtin_clzll
  18. #define __builtin_popcount __builtin_popcountll
  19. using namespace std;
  20. template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
  21. template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  22. while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
  23. }
  24. #ifdef LOCAL
  25. #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
  26. #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
  27. #else
  28. #define debug(...) (__VA_ARGS__)
  29. #define debugv(x)
  30. #define cerr if(0)cout
  31. #endif
  32. #define next ____next
  33. #define prev ____prev
  34. #define left ____left
  35. #define hash ____hash
  36. typedef long long ll;
  37. typedef long double LD;
  38. typedef pair<int, int> PII;
  39. typedef pair<ll, ll> PLL;
  40. typedef vector<int> VI;
  41. typedef vector<VI> VVI;
  42. typedef vector<ll> VLL;
  43. typedef vector<pair<int, int> > VPII;
  44. typedef vector<pair<ll, ll> > VPLL;
  45.  
  46. template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
  47. template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
  48. template<class T1, class T2>
  49. ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
  50. template<class A, class B, class C> struct Triple { A first; B second; C third;
  51. bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
  52. template<class T> void ResizeVec(T&, vector<int>) {}
  53. template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  54. vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  55. for (T& v : vec) { ResizeVec(v, sz); }
  56. }
  57. typedef Triple<int, int, int> TIII;
  58. template<class A, class B, class C>
  59. ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
  60. template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  61. template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  62. template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  63.  
  64. struct Sol {
  65. void Test() {
  66. int h, g;
  67. cin>>h>>g;
  68. VI a(1 << h);
  69. VPII ps;
  70. int n = (1 << h) - 1;
  71. int imp = (1 << g) - 1;
  72. RE (i, n) {
  73. int x;
  74. cin>>x;
  75. ps.PB({x, i});
  76. a[i] = x;
  77. }
  78. VI taken(n + 1);
  79. sort(ALL(ps));
  80. VI gotten(n + 1);
  81. int sum = 0;
  82. for (auto p : ps) {
  83. int wh = p.nd;
  84. while (wh > imp || gotten[wh]) {
  85. wh /= 2;
  86. }
  87. if (wh == 0) { continue; }
  88. if (gotten[2 * wh] == gotten[2 * wh + 1]) {
  89. gotten[wh] = 1;
  90. sum += p.st;
  91. taken[p.nd] = 1;
  92. }
  93. }
  94. cout<<sum<<"\n";
  95. VI sum_gotten(n + 1);
  96. FORD (i, n, 1) {
  97. if (!taken[i]) {
  98. cout<<i<<" ";
  99. }
  100.  
  101. }
  102. cout<<"\n";
  103.  
  104. }
  105. };
  106.  
  107.  
  108.  
  109.  
  110. int32_t main() {
  111.  
  112. ios_base::sync_with_stdio(0);
  113. cout << fixed << setprecision(10);
  114. cerr << fixed << setprecision(10);
  115. cin.tie(0);
  116. //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  117.  
  118. int t;
  119. cin>>t;
  120. RE (i, t) {
  121. Sol sol;
  122. sol.Test();
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. return 0;
  150. }
  151.  
Success #stdin #stdout 0s 4380KB
stdin
2
3 2
7 6 3 5 4 2 1
3 2
7 6 5 4 3 2 1
stdout
10
6 3 2 1 
8
6 3 2 1