(n, m, k) = map(int, input().split())
p = [0] + list(map(int, input().split()))
pref = [0]
for i in range(1, n + 1):
    pref += [pref[i - 1] + p[i]]
dp = [[0] * (k + 1) for i in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, k + 1):
        if j * m > i:
            dp[i][j] = 0
        if i - m >= 0:
            dp[i][j] = max(dp[i - 1][j], dp[i - m][j - 1] + pref[i] - pref[i - m])
        else:
            dp[i][j] = dp[i - 1][j];
print(dp[n][k])


(n, m, k) = map(int, input().split())
p = [0] + list(map(int, input().split()))
pref = [0] + [sum(p[ : i + 1]) for i in range(1, n + 1)]
dp = [[0] * (k + 1) for i in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, k + 1):
        if j * m > i:
            dp[i][j] = 0
        if i - m >= 0:
            dp[i][j] = max(dp[i - 1][j], dp[i - m][j - 1] + pref[i] - pref[i - m])
        else:
            dp[i][j] = dp[i - 1][j];
print(dp[n][k])


(n, m, k) = map(int, input().split())
p = [0] + list(map(int, input().split()))
dp = [[0] * (k + 1) for i in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, k + 1):
        if j * m > i:
            dp[i][j] = 0
        if i - m >= 0:
            dp[i][j] = max(dp[i - 1][j], dp[i - m][j - 1] + sum(p[ : i + 1]) - sum(p[ : i - m + 1]))
        else:
            dp[i][j] = dp[i - 1][j];
print(dp[n][k])


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource

# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")

def main():
    t = 1

    rr = []
    for _ in range(t):
        n,m,k = LI()
        a = LI()

        dp = [[-_] * (n+1) for _ in range(k+1)]
        t = sum(a[:m])
        dp[1][m-1] = t
        for i in range(m,n):
            t -= a[i-m]
            t += a[i]
            x = 0
            for j in range(k):
                x = dp[j][i-m]
                if x >= 0 and dp[j+1][i] < x + t:
                    dp[j+1][i] = x + t

                if dp[j+1][i] < dp[j+1][i-1]:
                    dp[j+1][i] = dp[j+1][i-1]

        # print(JAA(dp, "\n", " "))
        rr.append(max(dp[-1]))

    return JA(rr, "\n")

print(main())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource

# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")

def main():
    t = 1

    rr = []
    for _ in range(t):
        n,m,k = LI()
        a = LI()

        dp = [[-_] * (n+1) for _ in range(k+1)]
        t = sum(a[:m])
        dp[1][m-1] = t
        for i in range(m,n):
            t -= a[i-m]
            t += a[i]
            x = 0
            for j in range(k):
                dp[j+1][i] = max(dp[j+1][i-1], dp[j][i-m]+t)

        # print(JAA(dp, "\n", " "))
        rr.append(max(dp[-1]))

    return JA(rr, "\n")

print(main())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource

# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")

def main():
    t = 1

    rr = []
    for _ in range(t):
        n,m,k = LI()
        a = LI()

        if k == 1:
            rr.append(sum(a.sort()[-k:]))
            continue

        dp = [[0] * (n+1) for _ in range(k+1)]
        t = sum(a[:m])
        dp[1][m-1] = t
        for i in range(m,n):
            t -= a[i-m]
            t += a[i]
            x = 0
            for j in range(k):
                dp[j+1][i] = max(dp[j+1][i-1], dp[j][i-m]+t)

        # print(JAA(dp, "\n", " "))
        rr.append(max(dp[-1]))

    return JA(rr, "\n")

print(main())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource

# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")

def main():
    t = 1

    rr = []
    for _ in range(t):
        n,m,k = LI()
        a = LI()

        dp = [[-_] * (n+1) for _ in range(k+1)]
        t = sum(a[:m])
        dp[1][m-1] = t
        for i in range(m,n):
            t -= a[i-m]
            t += a[i]
            x = 0
            for j in range(k):
                x = dp[j][i-m]
                dj = dp[j+1]
                dj[i] = dj[i-1]
                if x >= 0 and dj[i] < x + t:
                    dj[i] = x + t

        # print(JAA(dp, "\n", " "))
        rr.append(max(dp[-1]))

    return JA(rr, "\n")

print(main())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource

# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")

def main():
    t = 1

    rr = []
    for _ in range(t):
        n,m,k = LI()
        a = LI()

        if m == 1:
            rr.append(sum(sorted(a)[-k:]))
            continue

        dp = [[0] * (n+1) for _ in range(k+1)]
        t = sum(a[:m])
        dp[1][m-1] = t
        for i in range(m,n):
            t -= a[i-m]
            t += a[i]
            x = 0
            for j in range(k):
                dp[j+1][i] = max(dp[j+1][i-1], dp[j][i-m]+t)

        # print(JAA(dp, "\n", " "))
        rr.append(max(dp[-1]))

    return JA(rr, "\n")

print(main())


#include<bits/stdc++.h>

using namespace std;

const int MAXN = (1<<21)+100;

int a[MAXN];
vector<int> op;

int get_id(int i){
	int left = 2 * i;
	int right = 2 * i + 1;
	if(!a[left] && !a[right]) return i;
	if(a[left] > a[right]) return get_id(left);
	else get_id(right);
}

void f(int i){
	int left = 2 * i;
	int right = 2 * i + 1;
	if(!a[left] && !a[right]) a[i] = 0;
	else if(a[left] > a[right]){
		a[i] = a[left];
		f(left);
	}else{
		a[i] = a[right];
		f(right);
	}
}

int main(){
	ios::sync_with_stdio(false);
//	freopen("in.txt", "r", stdin);
	int t;
	cin >> t;
	while(t--){
		int h, g;
		op.clear();
		cin >> h >> g;
		for(int i=1; i<1<<(h+1); i++) a[i] = 0;
		for(int i=1; i<1<<h; i++) cin >> a[i];
		int limit = (1<<g) - 1;
		for(int i=1; i<1<<g; i++){
			while(get_id(i) > limit){
				op.push_back(i);
				f(i);
			}
		}
		long long ans = 0;
		for(int i=1; i<1<<g; i++) ans += a[i];
		cout << ans << endl;
		for(int i=0; i<op.size(); i++) cout << op[i] << " ";
		cout << '\n';
	}
}


#include<bits/stdc++.h>

using namespace std;

const int MAXN = (2<<21) + 50;

int a[MAXN];
vector<int> op;

int get_id(int i){
  int l = 2 * i;
  int r = 2 * i + 1;
  if(!a[l] && !a[r]) return i;
  if(a[l] > a[r]) return get_id(l);
  else return get_id(r);
}

void f(int i){
  int l = 2 * i;
  int r = 2 * i + 1;
  if(!a[l] && !a[r]){
    a[i] = 0;
  }else if(a[l] > a[r]){
    a[i] = a[l];
    f(l);
  }
  else{
    a[i] = a[r];
    f(r);
  }
}

int main(){
  ios::sync_with_stdio(false);
  // freopen("in.txt", "r", stdin);
  int t;
  cin >> t;
  while(t--){
    int h, g;
    op.clear();
    cin >> h >> g;
    for(int i=1; i<1<<(h+1); i++) a[i] = 0;
    for(int i=1; i<1<<h; i++) cin >> a[i];
    int limit = (1<<g) - 1;
    long long ans = 0;
    for(int i=1; i<1<<g; i++){
      while(get_id(i) > limit){
        op.push_back(i);
        f(i);
      }
      ans += a[i];
    }
    cout << ans << endl;
    for(auto x : op) cout << x << " ";
    cout << '\n';
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int MAXN = (1<<21) + 50;

bool good[MAXN];
bool taken[MAXN];

int main(){
	ios::sync_with_stdio(false);
//	freopen("in.txt", "r", stdin);
	int t;
	cin >> t;
	while(t--){
		int h, g;
		vector< pair<int, int> > vp;
		cin >> h >> g;
		for(int i=1; i<1<<h; i++){
			int num;
			cin >> num;
			vp.push_back({num, i});
			taken[i] = false;
		}
		for(int i=0; i<1<<h; i++) good[i] = false;
		sort(vp.begin(), vp.end());
		int limit = (1<<g) - 1;
		long long ans = 0;
		vector<int> op;
		for(int i=0; i<vp.size(); i++){
			int pos = vp[i].second;
			while(pos > limit || good[pos]) pos /= 2;
			if(pos == 0) continue;
			if(good[2 * pos] == good[2 * pos + 1]){
				good[pos] = true;
				ans += vp[i].first;
				taken[vp[i].second] = true;
			}
		}
		cout << ans << endl;
		for(int i=1; i<1<<h; i++){
			if(!taken[i]) cout << i << " ";
		}
		cout << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
const int N = 1<<20;
int t, n, h, g, r, sum, a[N], d[N];
std::vector<int> ans;
void f(int i) {
	int le = 2*i<N ? a[2*i] : 0;
	int ri = 2*i+1<N ? a[2*i+1] : 0;
	if (le==0 && ri==0) {
		a[i]=0;
		if(a[i&1?i-1:i+1]==0)
			d[i/2]=32-__builtin_clz(i/2);
	}
	else if (le>ri) a[i]=le, f(2*i);
	else a[i]=ri, f(2*i+1);
}
void fr(int i) {
	int le = 2*i<N ? a[2*i] : 0;
	int ri = 2*i+1<N ? a[2*i+1] : 0;
	if (le || ri) {
		if (le>ri && d[2*i]<=g) r=2*i+1, fr(r);
		else if (ri>le && d[2*i+1]<g) r=2*i, fr(r);
		else if (le>ri) fr(2*i);
		else fr(2*i+1);
	}
}
int main() {
	for(scanf("%d", &t); t--; ans.clear(), sum=0) {
		scanf("%d%d", &h, &g);
		for(int i=1; i<1<<h; i++)
			scanf("%d", a+i), d[i]=h;
		for(r=1; ans.size()<(1<<h)-(1<<g); fr(r=1))
			f(r), ans.push_back(r);
		for(int i=1; i<1<<g; i++) sum += a[i];
		printf("\n%d\n", sum);
		for(int j : ans) printf("%d ", j);
	}
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
typedef long long lld;

const int SIZE = (1 << 21) + 1;

int level[SIZE];
int where[SIZE];

int N, G, cur_size;
int heap[SIZE];

int signature[SIZE], sigTFO[SIZE], token;

int ans[SIZE], asize;

bool TFO[SIZE];

int get(int idx)
{
  if (idx >= SIZE)
  {
    return 0;
  }

  if (signature[idx] != token)
  {
    signature[idx] = token;
    heap[idx] = 0;
  }

  return heap[idx];
}

void set(int idx, int value)
{
  signature[idx] = token;
  heap[idx] = value;
  where[value] = idx;
}

bool is_bad(int idx)
{
  if (idx >= SIZE)
  {
    return true;
  }

  if (sigTFO[idx] != token)
  {
    sigTFO[idx] = token;
    TFO[idx] = false;
  }

  return TFO[idx];
}

void mark(int idx, bool value)
{
  sigTFO[idx] = token;
  TFO[idx] = value;
}

int better_child(int idx)
{
  int child1 = idx * 2, child2 = idx * 2 + 1;

  if (get(child1) > get(child2))
  {
    return child1;
  }
  else
  {
    return child2;
  }
}

int take_and_update(int idx)
{
  int child1 = idx * 2, child2 = idx * 2 + 1;
  int cur_val = get(idx);

  if (get(child1) == 0 && get(child2) == 0)
  {
    set(idx, 0);
    return 0;
  }
  int better_then = better_child(idx);
  set(idx, get(better_then));
  take_and_update(better_then);
  where[get(idx)] = idx;
  // cout << get(idx) << " is now on " << idx << endl;

  // It's bad if we already depleted levels below G, or if our best child did
  int better_now = better_child(idx);
  bool bad_value = (level[idx] == G && get(child1) == 0 && get(child2) == 0) || (is_bad(better_now));
  mark(idx, bad_value);

  return cur_val;
}

int original[SIZE];

void process_test()
{
  token++;
  asize = 0;

  scanf("%d %d", &N, &G);
  cur_size = (1 << N) - 1;
  int elements_to_take = (1 << N) - (1 << G);
  lld current_sum = 0;

  for (int i = 1; i <= cur_size; i++)
  {
    int numb;
    scanf("%d", &numb);

    set(i, numb);
    current_sum += numb;

    original[i] = numb;
    where[numb] = i;
  }

  sort(original + 1, original + cur_size + 1);

  int taken = 0;

  for (int idx = cur_size; idx >= 1; idx--)
  {
    if (asize == elements_to_take)
    {
      break;
    }

    int value = original[idx];
    int cur_vert = where[value];
    // cout << "The best to try is " << value << " on " << cur_vert << " with bad = " << is_bad(cur_vert) << endl;
    if (is_bad(cur_vert)) // If this is untouchable, go with the next ones
    {
      continue;
    }

    // cout << "Taking it - " << value << " on " << cur_vert << endl;

    ans[++asize] = cur_vert;
    int took = take_and_update(cur_vert);

    current_sum -= took;
  }

  printf("%lld\n", current_sum);
  for (int i = 1; i <= asize; i++)
  {
    printf("%d", ans[i]);
    (i == asize) ? printf("\n") : printf(" ");
  }
}

int main()
{
  // freopen("input.txt", "r", stdin);

  int cur_level = 1;
  for (int i = 1; i < SIZE; i++)
  {
    if ((1 << cur_level) == i)
    {
      cur_level += 1;
    }

    level[i] = cur_level;
  }

  int tests;
  scanf("%d", &tests);
  for (int t = 1; t <= tests; t++)
  {
    process_test();
  }
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
typedef long long lld;

const int SIZE = (1 << 21) + 1;

int level[SIZE];
int where[SIZE];

int N, G, cur_size;
int heap[SIZE];

int signature[SIZE], sigTFO[SIZE], token;

int ans[SIZE], asize;

bool TFO[SIZE];

int get(int idx)
{
  if (idx >= SIZE)
  {
    return 0;
  }

  if (signature[idx] != token)
  {
    signature[idx] = token;
    heap[idx] = 0;
  }

  return heap[idx];
}

void set(int idx, int value)
{
  signature[idx] = token;
  heap[idx] = value;
  where[value] = idx;
}

bool is_bad(int idx)
{
  if (idx >= SIZE)
  {
    return true;
  }

  if (sigTFO[idx] != token)
  {
    sigTFO[idx] = token;
    TFO[idx] = false;
  }

  return TFO[idx];
}

void mark(int idx, bool value)
{
  sigTFO[idx] = token;
  TFO[idx] = value;
}

int better_child(int idx)
{
  int child1 = idx * 2, child2 = idx * 2 + 1;

  if (get(child1) > get(child2))
  {
    return child1;
  }
  else
  {
    return child2;
  }
}

int take_and_update(int idx)
{
  int child1 = idx * 2, child2 = idx * 2 + 1;
  int cur_val = get(idx);

  if (get(child1) == 0 && get(child2) == 0)
  {
    where[idx] = -1; // Taking it
    set(idx, 0);
    return 0;
  }
  int better_then = better_child(idx);
  set(idx, get(better_then));
  take_and_update(better_then);
  where[get(idx)] = idx;
  // cout << get(idx) << " is now on " << idx << endl;

  // It's bad if we already depleted levels below G, or if our best child did
  int better_now = better_child(idx);
  bool bad_value = (level[idx] == G && get(child1) == 0 && get(child2) == 0) || (is_bad(better_now));
  mark(idx, bad_value);

  return cur_val;
}

int original[SIZE];

void process_test()
{
  token++;
  asize = 0;

  scanf("%d %d", &N, &G);
  cur_size = (1 << N) - 1;
  int elements_to_take = (1 << N) - (1 << G);
  lld current_sum = 0;

  for (int i = 1; i <= cur_size; i++)
  {
    int numb;
    scanf("%d", &numb);

    set(i, numb);
    current_sum += numb;

    original[i] = numb;
    where[numb] = i;
  }

  sort(original + 1, original + cur_size + 1);

  int taken = 0;

  for (int idx = cur_size; idx >= 1; idx--)
  {
    if (asize == elements_to_take)
    {
      break;
    }

    int value = original[idx];
    int cur_vert = where[value];
    // cout << "The best to try is " << value << " on " << cur_vert << " with bad = " << is_bad(cur_vert) << endl;
    if (is_bad(cur_vert)) // If this is untouchable, go with the next ones
    {
      continue;
    }

    // cout << "Taking it - " << value << " on " << cur_vert << endl;

    ans[++asize] = cur_vert;
    int took = take_and_update(cur_vert);

    current_sum -= took;
  }

  printf("%lld\n", current_sum);
  for (int i = 1; i <= asize; i++)
  {
    printf("%d", ans[i]);
    (i == asize) ? printf("\n") : printf(" ");
  }
}

int main()
{
  // freopen("input.txt", "r", stdin);

  int cur_level = 1;
  for (int i = 1; i < SIZE; i++)
  {
    if ((1 << cur_level) == i)
    {
      cur_level += 1;
    }

    level[i] = cur_level;
  }

  int tests;
  scanf("%d", &tests);
  for (int t = 1; t <= tests; t++)
  {
    process_test();
  }
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <string.h>
#include <iostream>
using namespace std;
typedef long long lld;

const int SIZE = (1 << 21) + 1;

int level[SIZE];

int N, G, cur_size;
int heap[SIZE];

int signature[SIZE], sigTFO[SIZE], token;

int ans[SIZE], asize;

bool TFO[SIZE];

int get(int idx)
{
  if (idx >= SIZE)
  {
    return 0;
  }

  if (signature[idx] != token)
  {
    signature[idx] = token;
    heap[idx] = 0;
  }

  return heap[idx];
}

void set(int idx, int value)
{
  signature[idx] = token;
  heap[idx] = value;
}

bool is_bad(int idx)
{
  if (idx >= SIZE)
  {
    return true;
  }

  if (sigTFO[idx] != token)
  {
    sigTFO[idx] = token;
    TFO[idx] = false;
  }

  return TFO[idx];
}

void mark(int idx, bool value)
{
  sigTFO[idx] = token;
  TFO[idx] = value;
}

int better_child(int idx)
{
  int child1 = idx * 2, child2 = idx * 2 + 1;

  if (get(child1) > get(child2))
  {
    return child1;
  }
  else
  {
    return child2;
  }
}

int take_and_update(int idx)
{
  int child1 = idx * 2, child2 = idx * 2 + 1;
  int cur_val = get(idx);

  if (get(child1) == 0 && get(child2) == 0)
  {
    set(idx, 0);
    return 0;
  }
  int better_then = better_child(idx);
  set(idx, get(better_then));
  take_and_update(better_then);

  // It's bad if we already depleted levels below G, or if our best child did
  int better_now = better_child(idx);
  bool bad_value = (level[idx] == G && get(child1) == 0 && get(child2) == 0) || (is_bad(better_now));
  mark(idx, bad_value);

  return cur_val;
}

void process_test()
{
  token++;
  asize = 0;

  scanf("%d %d", &N, &G);
  cur_size = (1 << N) - 1;
  int elements_to_take = (1 << N) - (1 << G);
  lld current_sum = 0;

  for (int i = 1; i <= cur_size; i++)
  {
    int numb;
    scanf("%d", &numb);

    set(i, numb);
    current_sum += numb;
  }

  int cur_vert = 1;
  for (int i = 1; i <= elements_to_take; i++)
  {
    ans[++asize] = cur_vert;
    int took = take_and_update(cur_vert);

    current_sum -= took;

    while (is_bad(cur_vert))
    {
      int child1 = cur_vert * 2, child2 = cur_vert * 2 + 1;
      cur_vert = is_bad(child1) ? child2 : child1;
    }
  }

  printf("%lld\n", current_sum);
  for (int i = 1; i <= asize; i++)
  {
    printf("%d", ans[i]);
    (i == asize) ? printf("\n") : printf(" ");
  }
}

int main()
{
  // freopen("input.txt", "r", stdin);

  int cur_level = 1;
  for (int i = 1; i < SIZE; i++)
  {
    if ((1 << cur_level) == i)
    {
      cur_level += 1;
    }

    level[i] = cur_level;
  }

  int tests;
  scanf("%d", &tests);
  for (int t = 1; t <= tests; t++)
  {
    process_test();
  }
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
vector <int> a((2<<20)+5);
int leg(int j,int g){
    int z=1;
    int start=(int)(pow(2,g-1));
    int end=(int)(pow(2,g)-1);
    int m;
    while(z==1){
        if(j>=start && j<=end){
            m=max(a[2*j],a[2*j+1]);
            z=0;
            break;
        }
        else{
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    if(m==0){
        return 0;
    }
    else{
        return 1;
    }
}
vector <int> change(int j,int h){
    int z=1;
    int start=(int)(pow(2,h-1));
    int end=(int)(pow(2,h)-1);
    while(z==1){
        if(j>=start && j<=end){
            z=0;
            a[j]=0;
            break;
        }
        int m=max(a[2*j],a[2*j+1]);
        if(m==0){
            z=0;
            a[j]=0;
            break;
        }
        else{
            a[j]=m;
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    return a;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
        int n=(int)(pow(2,h)-1);
        int m=(int)(pow(2,g)-1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <int> ans;
        for(int i=0;i<g;i+=0){
            int start=(int)(pow(2,i));
            int end=(int)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                if(leg(j,g)==1){
                    ans.pb(j);
                    change(j,h);
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        int sum=0;
        for(int i=1;i<=(int)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
int leg(int j,int g,vector <int> &a){
    int z=1;
    int start=(int)(pow(2,g-1));
    int end=(int)(pow(2,g)-1);
    int m;
    while(z==1){
        if(j>=start && j<=end){
            m=max(a[2*j],a[2*j+1]);
            z=0;
            break;
        }
        else{
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    if(m==0){
        return 0;
    }
    else{
        return 1;
    }
}
vector <int> change(int j,int h,vector <int> &a){
    int z=1;
    int start=(int)(pow(2,h-1));
    int end=(int)(pow(2,h)-1);
    while(z==1){
        if(j>=start && j<=end){
            z=0;
            a[j]=0;
            break;
        }
        int m=max(a[2*j],a[2*j+1]);
        if(m==0){
            z=0;
            a[j]=0;
            break;
        }
        else{
            a[j]=m;
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    return a;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
        int n=(int)(pow(2,h)-1);
        int m=(int)(pow(2,g)-1);
        vector <int> a(n+1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <int> ans;
        for(int i=0;i<g;i+=0){
            int start=(int)(pow(2,i));
            int end=(int)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                if(leg(j,g,a)==1){
                    ans.pb(j);
                    change(j,h,a);
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        int sum=0;
        for(int i=1;i<=(int)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#define ll long long int
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
        int n=(int)(pow(2,h)-1);
        int m=(int)(pow(2,g)-1);
        vector <int> a(n+1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <int> ans;
        for(int i=0;i<g;i+=0){
            int start=(int)(pow(2,i));
            int end=(int)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                ll flag=0;
                int z=1;
                int start1=(int)(pow(2,g-1));
                int end1=(int)(pow(2,g)-1);
                int m;
                int j1=j;
                 while(z==1){
                    if(j1>=start1 && j1<=end1){
                        m=max(a[2*j1],a[2*j1+1]);
                        z=0;
                        break;
                    }
                    else{
                    if(a[2*j1]>a[2*j1+1]){
                        j1=2*j1;
                    }
                    else{
                        j1=2*j1+1;
                    }
                    }}
                    if(m==0){
                        flag=0;
                    }
                    else{
                        flag=1;
                    }
                if(flag==1){
                    ans.pb(j);
                    int z1=1;
                    int start2=(int)(pow(2,h-1));
                    int end2=(int)(pow(2,h)-1);
                    int j2=j;
                    while(z1==1){
                        if(j2>=start2 && j2<=end2){
                            z1=0;
                            a[j2]=0;
                            break;
                        }
                        int mm=max(a[2*j2],a[2*j2+1]);
                        if(mm==0){
                            z1=0;
                            a[j2]=0;
                            break;
                        }
                        else{
                            a[j2]=mm;
                            if(a[2*j2]>a[2*j2+1]){
                                j2=2*j2;
                            }
                            else{
                                j2=2*j2+1;
                            }
                        }
                    }
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        int sum=0;
        for(int i=1;i<=(int)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#define ll long long int
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
int leg(int j,int g,vector <int> &a){
    int z=1;
    int start=(int)(pow(2,g-1));
    int end=(int)(pow(2,g)-1);
    int m;
    while(z==1){
        if(j>=start && j<=end){
            m=max(a[2*j],a[2*j+1]);
            z=0;
            break;
        }
        else{
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    if(m==0){
        return 0;
    }
    else{
        return 1;
    }
}
vector <int> change(int j,int h,vector <int> &a){
    int z=1;
    int start=(int)(pow(2,h-1));
    int end=(int)(pow(2,h)-1);
    while(z==1){
        if(j>=start && j<=end){
            z=0;
            a[j]=0;
            break;
        }
        int m=max(a[2*j],a[2*j+1]);
        if(m==0){
            z=0;
            a[j]=0;
            break;
        }
        else{
            a[j]=m;
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    return a;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
        int n=(int)(pow(2,h)-1);
        int m=(int)(pow(2,g)-1);
        vector <int> a(n+1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <int> ans;
        for(int i=0;i<g;i+=0){
            int start=(int)(pow(2,i));
            int end=(int)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                if(leg(j,g,a)==1){
                    ans.pb(j);
                    change(j,h,a);
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        int sum=0;
        for(int i=1;i<=(int)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define M 998244353
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
ll leg(ll j,ll g,vector <ll> &a){
    ll z=1;
    ll start=(ll)(pow(2,g-1));
    ll end=(ll)(pow(2,g)-1);
    ll m;
    while(z==1){
        if(j>=start && j<=end){
            m=max(a[2*j],a[2*j+1]);
            z=0;
            break;
        }
        else{
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    if(m==0){
        return 0;
    }
    else{
        return 1;
    }
}
vector <ll> change(ll j,ll h,vector <ll> &a){
    ll z=1;
    ll start=(ll)(pow(2,h-1));
    ll end=(ll)(pow(2,h)-1);
    while(z==1){
        if(j>=start && j<=end){
            z=0;
            a[j]=0;
            break;
        }
        ll m=max(a[2*j],a[2*j+1]);
        if(m==0){
            z=0;
            a[j]=0;
            break;
        }
        else{
            a[j]=m;
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    return a;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll h,g;
        cin>>h>>g;
        ll n=(ll)(pow(2,h)-1);
        ll m=(ll)(pow(2,g)-1);
        vector <ll> a(n+1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <ll> ans;
        for(int i=0;i<g;i+=0){
            ll start=(ll)(pow(2,i));
            ll end=(ll)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                if(leg(j,g,a)==1){
                    ans.pb(j);
                    change(j,h,a);
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        ll sum=0;
        for(int i=1;i<=(ll)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define M 998244353
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
ll leg(ll j,ll g,vector <ll> &a){
    ll z=1;
    ll start=(ll)(pow(2,g-1));
    ll end=(ll)(pow(2,g)-1);
    ll m;
    while(z==1){
        if(j>=start && j<=end){
            m=max(a[2*j],a[2*j+1]);
            z=0;
            break;
        }
        else{
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    if(m==0){
        return 0;
    }
    else{
        return 1;
    }
}
vector <ll> change(ll j,ll h,vector <ll> &a){
    ll z=1;
    ll start=(ll)(pow(2,h-1));
    ll end=(ll)(pow(2,h)-1);
    while(z==1){
        if(j>=start && j<=end){
            z=0;
            a[j]=0;
            break;
        }
        ll m=max(a[2*j],a[2*j+1]);
        if(m==0){
            z=0;
            a[j]=0;
            break;
        }
        else{
            a[j]=m;
            if(a[2*j]>a[2*j+1]){
                j=2*j;
            }
            else{
                j=2*j+1;
            }
        }
    }
    return a;
}
int main(){
    ll t;
    cin>>t;
    while(t--){
        ll h,g;
        cin>>h>>g;
        ll n=(ll)(pow(2,h)-1);
        ll m=(ll)(pow(2,g)-1);
        vector <ll> a(n+1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <ll> ans;
        for(int i=0;i<g;i+=0){
            ll start=(ll)(pow(2,i));
            ll end=(ll)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                if(leg(j,g,a)==1){
                    ans.pb(j);
                    change(j,h,a);
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        ll sum=0;
        for(int i=1;i<=(ll)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#define ll long long int
#define pb push_back
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
        int n=(int)(pow(2,h)-1);
        int m=(int)(pow(2,g)-1);
        vector <int> a(n+1);
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        vector <int> ans;
        for(int i=0;i<g;i+=0){
            int start=(int)(pow(2,i));
            int end=(int)(pow(2,i+1)-1);
            for(int j=start;j<=end;j+=0){
                ll flag=0;
                int z=1;
                int start1=(int)(pow(2,g-1));
                int end1=(int)(pow(2,g)-1);
                int m;
                int j1=j;
                 while(z==1){
                    if(j1>=start1 && j1<=end1){
                        m=max(a[2*j1],a[2*j1+1]);
                        z=0;
                        break;
                    }
                    else{
                    if(a[2*j1]>a[2*j1+1]){
                        j1=2*j1;
                    }
                    else{
                        j1=2*j1+1;
                    }
                    }}
                    if(m==0){
                        flag=0;
                    }
                    else{
                        flag=1;
                    }
                if(flag==1){
                    ans.pb(j);
                    int z1=1;
                    int start2=(int)(pow(2,h-1));
                    int end2=(int)(pow(2,h)-1);
                    int j2=j;
                    while(z1==1){
                        if(j2>=start2 && j2<=end2){
                            z1=0;
                            a[j2]=0;
                            break;
                        }
                        int mm=max(a[2*j2],a[2*j2+1]);
                        if(mm==0){
                            z1=0;
                            a[j2]=0;
                            break;
                        }
                        else{
                            a[j2]=mm;
                            if(a[2*j2]>a[2*j2+1]){
                                j2=2*j2;
                            }
                            else{
                                j2=2*j2+1;
                            }
                        }
                    }
                }
                else{
                    j+=1;
                }
            }
            if(ans.size()==n-m){
                break;
            }
            i+=1;
        }
        ll sum=0;
        for(int i=1;i<=(int)(pow(2,g)-1);i++){
            sum+=a[i];
        }
        cout<<sum<<endl;
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl;
#define Get(x,y) scanf("%I64d%I64d",&x,&y);
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define Gett(x,y) scanf("%d%d",&x,&y);
#define f(i,a,b) for(int i=a;i<b;i++)
#define get(x) scanf("%I64d",&x);
#define gett(x) scanf("%d",&x);
#define ops(x) cout<<x<<" !"<<endl;
#define maxm(a,b) a=max(a,b);
#define minm(a,b) a=min(a,b);
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=(1<<21)+9;

int n,m,t,e,a[N],mx[N],mark[N];
ll ans;
vector<int> tree[N];

void merge(int x,int u,int v){
	int p1=0,p2=0;
	while(p1<tree[u].size() || p2<tree[v].size()){
		if(p2==tree[v].size() || (p1!=tree[u].size() && tree[u][p1]<tree[v][p2]))
			tree[x].pb(tree[u][p1++]);
		else
			tree[x].pb(tree[v][p2++]);
	}
	tree[x].pb(a[x]);
}
void make_tree(){
	f(i,1,n*2+1) mx[i]=0; ans=0;
	f(i,n/2,n)
		tree[i].clear(),tree[i].pb(a[i]);
	f_(i,n/2-1,1)
		tree[i].clear(),merge(i,i*2,i*2+1);
}
int binary_search(int x,int k){
	int l=-1,r=tree[x].size()-1,mid;
	while(l+1<r){
		mid=(l+r)/2;
		if(tree[x][mid]<=k) l=mid;
		else r=mid;
	}
	return tree[x][r];
}
void solve(int x){
	int p=binary_search(x,max(mx[x*2],mx[x*2+1]));
	mark[p]=t;
	ans+=p,mx[x]=p;
}

int main(){
	cin>>t;t++;e=t;
	while(--t){
		cin>>n>>m;
		n=(1<<n),m=(1<<m);
		f(i,1,n) gett(a[i]);
		make_tree();
		f_(i,m-1,1)
			solve(i);
		cout<<ans<<'\n';
		f_(i,n-1,1)
			if(mark[a[i]]!=t)
				cout<<i<<" ";
		cout<<'\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
bool can_be_called[(1<<20)];
int input[(1<<21)],h,g;
stack<int> st;

bool tryy(int pos){
	if(input[2*pos] == 0 && input[2*pos+1] == 0){
		if(pos >= (1<<g)){
			return true;
		}
		st.push(pos);
		return false;
	}else{
		bool ret = true;
		if(input[2*pos] > input[2*pos+1]){
			if(!tryy(2*pos)){
				ret = false;
			}
		}else{
			if(!tryy(2*pos+1)){
				ret = false;
			}
		}
		if(ret){
			return true;
		}
		st.push(pos);
		return false;
	}
}

void call(int pos){
	if(input[2*pos] == 0 && input[2*pos+1] == 0){
		input[pos] = 0;
	}else{
		if(input[2*pos] > input[2*pos+1]){
			input[pos] = input[2*pos];
			call(2*pos);
		}else{
			input[pos] = input[2*pos+1];
			call(2*pos+1);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);
	int t;
	cin >> t;
	while(t--){
		long long ret = 0;
	    cin >> h >> g;
	    for(int i = 1 ; i < (1<<h) ; i += 1){
	    	cin >> input[i];
	    }
	    for(int i = (1<<h) ; i < (1<<(h+1)) ; i += 1){
	    	input[i] = 0;
	    }
	    queue<int> q;
	    q.push(1);
	    vector<int> ans;
	    while(q.size()){
	    	int a = q.front();
	    	while(tryy(a)){
	    		ans.push_back({a});
	    		call(a);
	    	}
	    	if(st.size()){
		    	st.pop();
		    }
		    while(st.size()){
		    	int b = st.top();
		    	b ^= 1;
		    	q.push(b);
		    	st.pop();
		    }
		    q.pop();
	    }
	    for(int i = 1 ; i < (1<<h) ; i += 1){
	    	ret += input[i];
	    }
	    cout << ret << endl;
	    for(int i : ans){
	    	cout << i << " ";
	    }cout << endl;
	}
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <set>
#include <functional>
#include <deque>
#include <map>
#include <queue>
#include <complex>
#include <ctime>
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
const int MOD = 1000000007;
const double eps=1e-8;
const double PI=acos(-1.0);
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n,ll p=MOD) {ll r=1%p;for (a%=p;n;a=a*a%p,n>>=1)if(n&1)r=r*a%p;return r;}
ll inv(ll x,ll p=MOD){return x<=1?1:inv(p%x,p)*(p-p/x)%p;}

int h,g;
int a[(1<<21)+10];
vector<int> ans;

int height(int id){
    if(a[id]==0) return 0;
    if(a[id<<1]>a[(id<<1)|1]) return 1+height(id<<1);
    else return 1+height((id<<1)|1);
}

void pop(int id){
    if(a[id]==0) return ;
    if(a[id<<1]>a[(id<<1)|1]){
        a[id]=a[id<<1];
        pop(id<<1);
    }
    else{
        a[id]=a[(id<<1)|1];
        pop((id<<1)|1);
    }
}

void dfs(int id,int dep){
    if(a[id]==0) return ;
    while(dep+height(id)>g){
        ans.emplace_back(id);
        pop(id);
    }
    dfs(id<<1,dep+1);
    dfs((id<<1)|1,dep+1);
}

void solve(){
    ans.clear();
    scanf("%d%d",&h,&g);
    for(int i=1;i<(1<<h);i++){
        scanf("%d",&a[i]);
    }
    dfs(1,0);
    ll sum=0;
    for(int i=1;i<(1<<g);i++){
        sum+=a[i];
        a[i]=0;
    }
    printf("%lld\n",sum);
    for(int i=0;i<ans.size();i++){
        printf("%d ",ans[i]);
    }
    printf("\n");
}

int main(int argc, char* argv[]) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 200001
#define MOD ll(998244353)

using namespace std;

typedef long long ll;

typedef long double ld;

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int q;

    cin >> q;

    for (; q > 0; q--)
    {
        int h, g;

        cin >> h >> g;

        int a[(1 << h)];

        vector <int> gr; gr.clear();

        for (int i = 1; i < (1 << h); i++) cin >> a[i];

        for (int i = 1; i < (1 << g); i++)
        {
            bool f = 1;

            int v = i;

            vector <pair <int, int> > pt; pt.clear();

            while (f)
            {
                f = 0;

                if (v * 2 < (1 << h) && 0 != max(a[v + v], a[v + v + 1]))
                {
                    f = 1;

                    if (a[v + v] > a[v + v + 1])
                    {
                        pt.pb({v, a[v + v]});

                        v = v + v;
                    }
                    else
                    {
                        pt.pb({v, a[v + v + 1]});

                        v = v + v + 1;
                    }
                }
            }

            pt.pb({v, 0});

            if (v >= (1 << g))
            {
                gr.pb(i);

                i--;

                for (auto it : pt) a[it.F] = it.S;

                continue;
            }

        }

        ll sm = 0;

        for (int i = 1; i < (1 << g); i++) sm += ll(a[i]);

        cout << sm << endl;

        for (auto it : gr) cout << it << " ";

        cout << endl;
    }
}


#include <bits/stdc++.h>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 200001
#define MOD ll(998244353)

using namespace std;

typedef long long ll;

typedef long double ld;

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int q;

    cin >> q;

    for (; q > 0; q--)
    {
        int h, g;

        cin >> h >> g;

        ll a[(1 << h)];

        vector <int> gr; gr.clear();

        for (int i = 1; i < (1 << h); i++) cin >> a[i];

        for (int i = 1; i < (1 << g); i++)
        {
            bool f = 1;

            int v = i;

            vector <pair <int, ll> > pt; pt.clear();

            while (f)
            {
                f = 0;

                if (v * 2 < (1 << h) && 0 != max(a[v + v], a[v + v + 1]))
                {
                    f = 1;

                    if (a[v + v] > a[v + v + 1])
                    {
                        pt.pb({v, a[v + v]});

                        v = v + v;
                    }
                    else
                    {
                        pt.pb({v, a[v + v + 1]});

                        v = v + v + 1;
                    }
                }
            }

            pt.pb({v, 0});

            if (v >= (1 << g))
            {
                gr.pb(i);

                i--;

                for (auto it : pt) a[it.F] = it.S;

                continue;
            }

        }

        ll sm = 0;

        for (int i = 1; i < (1 << g); i++) sm += a[i];

        cout << sm << endl;

        for (auto it : gr) cout << it << " ";

        cout << endl;
    }
}


#include <bits/stdc++.h>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 200001
#define MOD ll(998244353)

using namespace std;

typedef long long ll;

typedef long double ld;

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int q;

    cin >> q;

    for (; q > 0; q--)
    {
        int h, g;

        cin >> h >> g;

        int a[(1 << h)];

        vector <int> gr; gr.clear();

        for (int i = 1; i < (1 << h); i++) cin >> a[i];

        for (int i = 1; i < (1 << g); i++)
        {
            bool f = 1;

            int v = i;

            vector <pair <int, int> > pt; pt.clear();

            while (f)
            {
                f = 0;

                if (v * 2 < (1 << h) && 0 != max(a[v + v], a[v + v + 1]))
                {
                    f = 1;

                    if (a[v + v] > a[v + v + 1])
                    {
                        pt.pb({v, a[v + v]});

                        v = v + v;
                    }
                    else
                    {
                        pt.pb({v, a[v + v + 1]});

                        v = v + v + 1;
                    }
                }
            }

            pt.pb({v, 0});

            if (v >= (1 << g))
            {
                gr.pb(i);

                i--;

                for (auto it : pt) a[it.F] = it.S;

                continue;
            }

        }

        int sm = 0;

        for (int i = 1; i < (1 << g); i++) sm += a[i];

        cout << sm << endl;

        for (auto it : gr) cout << it << " ";

        cout << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=(1<<21)+100;
ll a[maxx];
int n,m;

inline int fcs(int i)
{
	if(a[i<<1]==0&&a[i<<1|1]==0) return i;
	if(a[i<<1]>a[i<<1|1]) return fcs(i<<1);
	else return fcs(i<<1|1);
}
inline void dfs(int i)
{
	if(a[i<<1]==0&&a[i<<1|1]==0)
	{
		a[i]=0;
		return ;
	}
	if(a[i<<1]>a[i<<1|1])
	{
		a[i]=a[i<<1];
		dfs(i<<1);
	}
	else a[i]=a[i<<1|1],dfs(i<<1|1);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<(1<<(n+1));i++) a[i]=0;
		for(int i=1;i<(1<<n);i++) scanf("%lld",&a[i]);
		int top=(1<<m)-1;
		vector<int> ans;
		ans.clear();
		for(int i=1;i<=top;i++)
		{
			while(fcs(i)>top)
			{
				ans.push_back(i);
				dfs(i);
			}
		}
		ll sum=0;
		for(int i=1;i<=top;i++) sum+=a[i];
		cout<<sum<<endl;
		for(int i=0;i<ans.size();i++) cout<<ans[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=(1<<21)+100;
ll a[maxx];
int n,m;

inline int fcs(int i)
{
	if(a[i<<1]==0&&a[i<<1|1]==0) return i;
	if(a[i<<1]>a[i<<1|1]) return fcs(i<<1);
	else return fcs(i<<1|1);
}
inline void dfs(int i)
{
	if(a[i<<1]==0&&a[i<<1|1]==0)
	{
		a[i]=0;
		return ;
	}
	if(a[i<<1]>a[i<<1|1])
	{
		a[i]=a[i<<1];
		dfs(i<<1);
	}
	else a[i]=a[i<<1|1],dfs(i<<1|1);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<(1<<n);i++) scanf("%lld",&a[i]);
		a[(1<<n)]=a[(1<<n)+1]=0;
		int top=(1<<m)-1;
		vector<int> ans;
		ans.clear();
		for(int i=1;i<=top;i++)
		{
			while(fcs(i)>top)
			{
				ans.push_back(i);
				dfs(i);
			}
		}
		ll sum=0;
		for(int i=1;i<=top;i++) sum+=a[i];
		cout<<sum<<endl;
		for(int i=0;i<ans.size();i++) cout<<ans[i]<<" ";
		cout<<endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <utility>
#include <cmath>
#include <tuple>
using namespace std;
#define fio ios::sync_with_stdio(0), cin.tie(0);
#define md ((s + e) >> 1)
#define lch n * 2, s, md
#define rch n * 2 + 1, md + 1, e
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, M, K, T;
int H, G;
int v[1 << 21];
int cnt[1 << 21];

void build(int cur) {
	if (cur < H) {
		build(cur * 2);
		build(cur * 2 + 1);
		cnt[cur] = v[cur * 2] > v[cur * 2 + 1] ? cnt[cur * 2] : cnt[cur * 2 + 1];
		cnt[cur] +=  G <= cur;
	}
	else {
		v[cur] = cnt[cur] = 0;
	}
}

void pop (int cur) {
	if (!v[cur * 2] && !v[cur * 2 + 1]) {
		v[cur] = 0;
		cnt[cur] = 0;
	}
	else {
		int child = cur * 2;
		if (v[cur * 2] < v[cur * 2 + 1]) child++;
		v[cur] = v[child];
		pop(child);
		cnt[cur] = v[cur * 2] > v[cur * 2 + 1] ? cnt[cur * 2] : cnt[cur * 2 + 1];
		cnt[cur] +=  G <= cur;
	}
}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d", &H, &G);
		H = 1 << H;
		G = 1 << G;
		vector<int> ans;
		ll tot = 0;
		for (int i = 1; i < H; i++) {
			scanf("%d", v + i);
			tot += v[i];
		}
		build(1);
		for (int cur = 1; cur < H; cur++) while (cnt[cur]) {
			tot -= v[cur];
			pop(cur);
			ans.push_back(cur);
		}
		printf("%lld\n", tot);
		for (int x : ans) {
			printf("%d ", x);
		}
		puts("");
	}

}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <utility>
#include <cmath>
#include <tuple>
using namespace std;
#define fio ios::sync_with_stdio(0), cin.tie(0);
#define md ((s + e) >> 1)
#define lch n * 2, s, md
#define rch n * 2 + 1, md + 1, e
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, M, K, T;
int H, G;
int v[1 << 21];
int cnt[1 << 21];

void build(int cur) {
	if (cur < H) {
		build(cur * 2);
		build(cur * 2 + 1);
		cnt[cur] = v[cur * 2] > v[cur * 2 + 1] ? cnt[cur * 2] : cnt[cur * 2 + 1];
		cnt[cur] +=  G <= cur;
	}
}

void pop (int cur) {
	if (!v[cur * 2] && !v[cur * 2 + 1]) {
		v[cur] = 0;
		cnt[cur] = 0;
	}
	else {
		int child = cur * 2;
		if (v[cur * 2] < v[cur * 2 + 1]) child++;
		v[cur] = v[child];
		pop(child);
		cnt[cur] = v[cur * 2] > v[cur * 2 + 1] ? cnt[cur * 2] : cnt[cur * 2 + 1];
		cnt[cur] +=  G <= cur;
	}
}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d", &H, &G);
		H = 1 << H;
		G = 1 << G;
		vector<int> ans;
		ll tot = 0;
		for (int i = 1; i < H; i++) {
			scanf("%d", v + i);
			tot += v[i];
		}
		build(1);
		for (int cur = 1; cur < H; cur++) while (cnt[cur]) {
			tot -= v[cur];
			pop(cur);
			ans.push_back(cur);
		}
		printf("%lld\n", tot);
		for (int x : ans) {
			printf("%d ", x);
		}
		puts("");
	}

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e6+7;
int a[N],b[N],g,h,tot,n,m;
inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
	}return x*f;
}
ll sum;
void dlt(int x) {
	if(a[x<<1]==0&&a[x<<1|1]==0) a[x]=0;
	else {
		if(a[x<<1]>a[x<<1|1]) a[x]=a[x<<1],dlt(x<<1);
		else a[x]=a[x<<1|1],dlt(x<<1|1);
	}
}
int get(int x,int dep) {
	if(a[x<<1]>a[x<<1|1]) return get(x<<1,dep+1);
	else if(a[x<<1]<a[x<<1|1]) return get(x<<1|1,dep+1);
	else return dep;
}
void dfs(int x,int dep) {
	if(a[x]==0) return ;
	while(get(x,dep)>g) sum-=a[x],b[++tot]=x,dlt(x);
	dfs(x<<1,dep+1);dfs(x<<1|1,dep+1);
}
int main() {
    int q;cin>>q;
    while(q--) {sum=0;tot=0;
    	h=read(),g=read();
    	n=(1<<h)-1,m=(1<<g)-1;
    	for(int i=1;i<=m;i++) b[i]=0;
    	for(int i=n+1;i<=2*n+1;i++) a[i]=0;
    	for(int i=1;i<=n;i++) a[i]=read(),sum+=a[i];
		dfs(1,1);printf("%I64d\n",sum);
		for(int i=1;i<=n-m;i++) printf("%d ",b[i]);
		puts(" ");
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e6+7;
int a[N],b[N],g,h,tot,n,m;
ll sum;
void dlt(int x) {
	if(a[x<<1]==0&&a[x<<1|1]==0) a[x]=0;
	else {
		if(a[x<<1]>a[x<<1|1]) a[x]=a[x<<1],dlt(x<<1);
		else a[x]=a[x<<1|1],dlt(x<<1|1);
	}
}
int get(int x,int dep) {
	if(a[x<<1]>a[x<<1|1]) return get(x<<1,dep+1);
	else if(a[x<<1]<a[x<<1|1]) return get(x<<1|1,dep+1);
	else return dep;
}
void dfs(int x,int dep) {
	if(a[x]==0) return ;
	while(get(x,dep)>g) sum-=a[x],b[++tot]=x,dlt(x);
	dfs(x<<1,dep+1);dfs(x<<1|1,dep+1);
}
int main() {
    int q;cin>>q;
    while(q--) {sum=0;tot=0;
    	scanf("%d%d",&h,&g);
    	n=(1<<h)-1,m=(1<<g)-1;
    	for(int i=1;i<=m;i++) b[i]=0;
    	for(int i=n+1;i<=2*n+1;i++) a[i]=0;
    	for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
		dfs(1,1);printf("%I64d\n",sum);
		for(int i=1;i<=n-m;i++) printf("%d ",b[i]);
		puts(" ");
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e6+7;
int a[N],b[N],g,h,sum,tot,n,m;
void dlt(int x) {
	if(a[x<<1]==0&&a[x<<1|1]==0) a[x]=0;
	else {
		if(a[x<<1]>a[x<<1|1]) a[x]=a[x<<1],dlt(x<<1);
		else a[x]=a[x<<1|1],dlt(x<<1|1);
	}
}
int get(int x,int dep) {
	if(a[x<<1]>a[x<<1|1]) return get(x<<1,dep+1);
	else if(a[x<<1]<a[x<<1|1]) return get(x<<1|1,dep+1);
	else return dep;
}
void dfs(int x,int dep) {
	if(a[x]==0) return ;
	while(get(x,dep)>g) sum-=a[x],b[++tot]=x,dlt(x);
	dfs(x<<1,dep+1);dfs(x<<1|1,dep+1);
}
int main() {
    int q;cin>>q;
    while(q--) {sum=0;tot=0;
    	scanf("%d%d",&h,&g);
    	n=(1<<h)-1,m=(1<<g)-1;
    	for(int i=1;i<=m;i++) b[i]=0;
    	for(int i=n+1;i<=2*n+1;i++) a[i]=0;
    	for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
		dfs(1,1);printf("%lld\n",sum);
		for(int i=1;i<=n-m;i++) printf("%d ",b[i]);
		puts(" ");
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e6+7;
int a[N],b[N],g,h,sum,tot,n,m;
void dlt(int x) {
	if(a[x<<1]==0&&a[x<<1|1]==0) a[x]=0;
	else {
		if(a[x<<1]>a[x<<1|1]) a[x]=a[x<<1],dlt(x<<1);
		else a[x]=a[x<<1|1],dlt(x<<1|1);
	}
}
int get(int x,int dep) {
	if(a[x<<1]>a[x<<1|1]) return get(x<<1,dep+1);
	else if(a[x<<1]<a[x<<1|1]) return get(x<<1|1,dep+1);
	else return dep;
}
void dfs(int x,int dep) {
	if(a[x]==0) return ;
	while(get(x,dep)>g) sum-=a[x],b[++tot]=x,dlt(x);
	dfs(x<<1,dep+1);dfs(x<<1|1,dep+1);
}
int main() {
    int q;cin>>q;
    while(q--) {sum=0;tot=0;
    	scanf("%d%d",&h,&g);
    	n=(1<<h)-1,m=(1<<g)-1;
    	for(int i=1;i<=m;i++) b[i]=0;
    	for(int i=n+1;i<=2*n+1;i++) a[i]=0;
    	for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
		dfs(1,1);printf("%I64d\n",sum);
		for(int i=1;i<=n-m;i++) printf("%d ",b[i]);
		puts(" ");
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e6+7;
int a[N],b[N],g,h,tot,n,m;
ll sum;
void dlt(int x) {
	if(a[x<<1]==0&&a[x<<1|1]==0) a[x]=0;
	else {
		if(a[x<<1]>a[x<<1|1]) a[x]=a[x<<1],dlt(x<<1);
		else a[x]=a[x<<1|1],dlt(x<<1|1);
	}
}
int get(int x,int dep) {
	if(a[x<<1]>a[x<<1|1]) return get(x<<1,dep+1);
	else if(a[x<<1]<a[x<<1|1]) return get(x<<1|1,dep+1);
	else return dep;
}
void dfs(int x,int dep) {
	if(a[x]==0) return ;
	while(get(x,dep)>g) sum-=a[x],b[++tot]=x,dlt(x);
	dfs(x<<1,dep+1);dfs(x<<1|1,dep+1);
}
int main() {
    int q;cin>>q;
    while(q--) {sum=0;tot=0;
    	scanf("%d%d",&h,&g);
    	n=(1<<h)-1,m=(1<<g)-1;
    	for(int i=1;i<=m;i++) b[i]=0;
    	for(int i=n+1;i<=2*n+1;i++) a[i]=0;
    	for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
		dfs(1,1);printf("%I64d\n",sum);
		for(int i=1;i<=n-m;i++) printf("%d ",b[i]);
		puts(" ");
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e6+7;
int a[N],ans[N],tot,h,g;
ll sum;
inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';ch=getchar();
	}return x*f;
}
void del(int x) {
	if(a[x<<1]==0&&a[x<<1|1]==0) a[x]=0;
	else {
		if(a[x<<1]>a[x<<1|1]) a[x]=a[x<<1],del(x<<1);
		else a[x]=a[x<<1|1],del(x<<1|1);
	}
}
int get(int x,int dep) {
	if(a[x<<1]>a[x<<1|1]) return get(x<<1,dep+1);
	else if(a[x<<1]<a[x<<1|1]) return get(x<<1|1,dep+1);
	else return dep;
}
void dfs(int x,int dep) {
	if(a[x]==0) return ;
	while(get(x,dep)>g) {
		sum-=a[x];ans[++tot]=x;del(x);
	}
	dfs(x<<1,dep+1);dfs(x<<1|1,dep+1);
}
int main() {int q;q=read();
    while(q--) {sum=0,tot=0;
    	h=read(),g=read();
    	int n=(1<<h)-1,m=(1<<g)-1;
    	for(int i=1;i<=n;i++) a[i]=read(),sum+=a[i];
    	for(int i=n+1;i<=2*n+1;i++) a[i]=0;
    	for(int i=1;i<=n-m;i++) ans[i]=0;
    	dfs(1,1);printf("%I64d\n",sum);
    	for(int i=1;i<=n-m;i++) printf("%d ",ans[i]);
    	puts(" ");
	}
	return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define testcase(t) int t;cin>>t;while(t--)
#define mp(x,y) make_pair(x,y)
#define in(x) insert(x)
#define rep(i,a,b)  for (__typeof((b)) i=(a);i<=(b);i++)
#define nrep(i,a,b)  for (__typeof((b)) i=(b);i>=(a);i--)
#define PI 3.14159265358979323846
#define SP(x) setprecision(x)
#define reflex ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define M  998244353
#define sz(a) (int)a.size()
#define endl "\n"
#define all(v) v.begin(), v.end()
#define UNIQUE(x) x.resize(distance(x.begin(), unique(all(x))))
#define mem(arr,val) memset(arr,val,sizeof(arr)) //For "0" and "-1"
#define inp0(a,n) rep(i,0,n-1) cin>>a[i];
#define inp1(a,n) rep(i,1,n) cin>>a[i];
#define prll(x) printf("%lld",x)
#define nth_ele(v,n) nth_element(v.begin(), v.begin()+n ,v.end())//O(N)
#define debug(x) cerr << (#x) << ": " << (x) << endl
#define debugv(x) for(auto IT:x) cout<<IT<<" "; cout<<endl;
#define minheap priority_queue <int, vector<int>, greater<int>>
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll gcd(ll n1, ll n2){if(n2!=0) return gcd(n2, n1%n2);else return n1;}
ll powerM(ll a,ll b){if(b==0) return 1LL;  if(b==1) return a%M; ll x=powerM(a,b/2)%M; if(b&1){return ((((a%M)*(x%M))%M)*(x%M))%M;}return ((x%M)*(x%M))%M;}
ll power(ll a,ll b){if(b==0) return 1;  if(b==1) return a; ll x=power(a,b/2); if(b&1){return ((((a)*(x)))*(x));}return ((x)*(x));}

const ll INF=1e10;
const int SIZE = 300005;

pii a[SIZE];
int vis[SIZE];
int ans[SIZE];
int h,g,sum=0;

void pull(int id){
    while(a[id]>min(a[id<<1LL], a[(id<<1LL)|1LL])){
        if(a[id<<1LL] < a[(id<<1LL)|1LL]){
            swap(a[id<<1LL], a[id]);
            id<<=1LL;
        }
        else{
            swap(a[(id<<1LL)|1LL], a[id]);
            id = (id<<1LL)|1LL;
        }
        if(id >= (1LL<<h)) return;
    }
}

signed main()
{
    testcase(T){
        sum=0;
        cin>>h>>g;
        rep(i,1LL,(1LL<<h)-1LL){
            vis[i]=0;
            cin>>a[i].ff;
            a[i].ss=i;
            ans[i]=0;
        }
        h--;
        nrep(lvl, 0, h-1LL){
            int l = 1LL<<lvl;
            int r = 1LL<<(lvl+1LL);
            rep(i,l,r-1){
                pii tmp = a[i];
                int bot = i<<(h-lvl);
                a[i]=a[bot];
                a[bot]=mp(INF,0);
                pull(i);
                if(lvl<g){
                    int need = max(ans[i*2], ans[i*2+1LL]);
                    while(a[i].ff<need){
                        a[i]=mp(INF,0);
                        pull(i);
                    }
                    // cout<<sum<<endl;
                    sum+=a[i].ff;
                    ans[i] = a[i].ff;
                    vis[a[i].ss]=1LL;
                    a[i]=tmp;
                    pull(i);
                }
                else{
                    a[bot]=tmp;
                }
            }
        }
        cout<<sum<<endl;
        nrep(i,1, (1LL<<(h+1))-1){
            if(!vis[i]) cout<<i<<" ";
        }
        cout<<endl;
    }
}
// To be practiced again!!


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define testcase(t) int t;cin>>t;while(t--)
#define mp(x,y) make_pair(x,y)
#define in(x) insert(x)
#define rep(i,a,b)  for (__typeof((b)) i=(a);i<=(b);i++)
#define nrep(i,a,b)  for (__typeof((b)) i=(b);i>=(a);i--)
#define PI 3.14159265358979323846
#define SP(x) setprecision(x)
#define reflex ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define M  998244353
#define sz(a) (int)a.size()
#define endl "\n"
#define all(v) v.begin(), v.end()
#define UNIQUE(x) x.resize(distance(x.begin(), unique(all(x))))
#define mem(arr,val) memset(arr,val,sizeof(arr)) //For "0" and "-1"
#define inp0(a,n) rep(i,0,n-1) cin>>a[i];
#define inp1(a,n) rep(i,1,n) cin>>a[i];
#define prll(x) printf("%lld",x)
#define nth_ele(v,n) nth_element(v.begin(), v.begin()+n ,v.end())//O(N)
#define debug(x) cerr << (#x) << ": " << (x) << endl
#define debugv(x) for(auto IT:x) cout<<IT<<" "; cout<<endl;
#define minheap priority_queue <int, vector<int>, greater<int>>
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll gcd(ll n1, ll n2){if(n2!=0) return gcd(n2, n1%n2);else return n1;}
ll powerM(ll a,ll b){if(b==0) return 1LL;  if(b==1) return a%M; ll x=powerM(a,b/2)%M; if(b&1){return ((((a%M)*(x%M))%M)*(x%M))%M;}return ((x%M)*(x%M))%M;}
ll power(ll a,ll b){if(b==0) return 1;  if(b==1) return a; ll x=power(a,b/2); if(b&1){return ((((a)*(x)))*(x));}return ((x)*(x));}

const ll INF=1e9+1;
const int SIZE = 1LL<<20;

pii a[SIZE];
int vis[SIZE];
int ans[SIZE];
int h,g,sum=0;

void pull(int id){
    while(a[id]>min(a[id<<1LL], a[(id<<1LL)|1LL])){
        if(a[id<<1LL] < a[(id<<1LL)|1LL]){
            swap(a[id<<1LL], a[id]);
            id<<=1LL;
        }
        else{
            swap(a[(id<<1LL)|1LL], a[id]);
            id = (id<<1LL)|1LL;
        }
        if(id >= (1LL<<h)) return;
    }
}

signed main()
{
    reflex;
    testcase(T){
        sum=0;
        cin>>h>>g;
        rep(i,1LL,(1LL<<h)-1LL){
            vis[i]=0;
            cin>>a[i].ff;
            a[i].ss=i;
            ans[i]=0;
        }
        h--;
        nrep(lvl, 0, h-1LL){
            int l = 1LL<<lvl;
            int r = 1LL<<(lvl+1LL);
            rep(i,l,r-1){
                pii tmp = a[i];
                int bot = i<<(h-lvl);
                a[i]=a[bot];
                a[bot]=mp(INF,0);
                pull(i);
                if(lvl<g){
                    int need = max(ans[i*2], ans[i*2+1LL]);
                    while(a[i].ff<need){
                        a[i]=mp(INF,0);
                        pull(i);
                    }
                    // cout<<sum<<endl;
                    sum+=a[i].ff;
                    ans[i] = a[i].ff;
                    vis[a[i].ss]=1LL;
                    a[i]=tmp;
                    pull(i);
                }
                else{
                    a[bot]=tmp;
                }
            }
        }
        cout<<sum<<endl;
        nrep(i,1, (1LL<<(h+1))-1){
            if(!vis[i]) cout<<i<<" ";
        }
        cout<<endl;
    }
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define testcase(t) int t;cin>>t;while(t--)
#define mp(x,y) make_pair(x,y)
#define in(x) insert(x)
#define rep(i,a,b)  for (__typeof((b)) i=(a);i<=(b);i++)
#define nrep(i,a,b)  for (__typeof((b)) i=(b);i>=(a);i--)
#define PI 3.14159265358979323846
#define SP(x) setprecision(x)
#define reflex ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define M  998244353
#define sz(a) (int)a.size()
#define endl "\n"
#define all(v) v.begin(), v.end()
#define UNIQUE(x) x.resize(distance(x.begin(), unique(all(x))))
#define mem(arr,val) memset(arr,val,sizeof(arr)) //For "0" and "-1"
#define inp0(a,n) rep(i,0,n-1) cin>>a[i];
#define inp1(a,n) rep(i,1,n) cin>>a[i];
#define prll(x) printf("%lld",x)
#define nth_ele(v,n) nth_element(v.begin(), v.begin()+n ,v.end())//O(N)
#define debug(x) cerr << (#x) << ": " << (x) << endl
#define debugv(x) for(auto IT:x) cout<<IT<<" "; cout<<endl;
#define minheap priority_queue <int, vector<int>, greater<int>>
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll gcd(ll n1, ll n2){if(n2!=0) return gcd(n2, n1%n2);else return n1;}
ll powerM(ll a,ll b){if(b==0) return 1LL;  if(b==1) return a%M; ll x=powerM(a,b/2)%M; if(b&1){return ((((a%M)*(x%M))%M)*(x%M))%M;}return ((x%M)*(x%M))%M;}
ll power(ll a,ll b){if(b==0) return 1;  if(b==1) return a; ll x=power(a,b/2); if(b&1){return ((((a)*(x)))*(x));}return ((x)*(x));}

const ll INF=1e9+1;
const int SIZE = 1LL<<20;

pii a[SIZE];
int vis[SIZE];
int ans[SIZE];
int h,g,sum=0;

void pull(int id){
    while(a[id]>min(a[id<<1LL], a[(id<<1LL)|1LL])){
        if(a[id<<1LL] < a[(id<<1LL)|1LL]){
            swap(a[id<<1LL], a[id]);
            id<<=1LL;
        }
        else{
            swap(a[(id<<1LL)|1LL], a[id]);
            id = (id<<1LL)|1LL;
        }
        if(id >= (1LL<<h)) return;
    }
}

signed main()
{
    testcase(T){
        sum=0;
        cin>>h>>g;
        rep(i,1LL,(1LL<<h)-1LL){
            vis[i]=0;
            cin>>a[i].ff;
            a[i].ss=i;
            ans[i]=0;
        }
        h--;
        nrep(lvl, 0, h-1LL){
            int l = 1LL<<lvl;
            int r = 1LL<<(lvl+1LL);
            rep(i,l,r-1){
                pii tmp = a[i];
                int bot = i<<(h-lvl);
                a[i]=a[bot];
                a[bot]=mp(INF,0);
                pull(i);
                if(lvl<g){
                    int need = max(ans[i*2], ans[i*2+1LL]);
                    while(a[i].ff<need){
                        a[i]=mp(INF,0);
                        pull(i);
                    }
                    // cout<<sum<<endl;
                    sum+=a[i].ff;
                    ans[i] = a[i].ff;
                    vis[a[i].ss]=1LL;
                    a[i]=tmp;
                    pull(i);
                }
                else{
                    a[bot]=tmp;
                }
            }
        }
        cout<<sum<<endl;
        nrep(i,1, (1LL<<(h+1))-1){
            if(!vis[i]) cout<<i<<" ";
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRVI(N, X) VI X(N); for (int ___I=0; ___I<n; ___I++) scanf("%d", &(X[___I]))
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second

using namespace std;

VI st;
int g,h,mg,mh;

int leaffrom(int i){
        int l=2*i;
        int r=2*i+1;
        if (r<mh){
            if (st[l]>st[r]) return leaffrom(l);
            if (st[l]<st[r]) return leaffrom(r);
        }
        return i;
}

int remove(int i){
        int l=2*i;
        int r=2*i+1;
        if (r<mh){
            if (st[l]>st[r]) {st[i]=st[l]; return remove(l);}
            if (st[l]<st[r]) {st[i]=st[r]; return remove(r);}
        }
        st[i]=0;
        return 0;
}

int main(){
    CASET{
        RII(h,g);
        mg=1<<g;
        mh=1<<h;
        st=VI(mh,0);
        LL r(0);
        REP(i,mh-1){
            RI(st[i+1]);
            r+=st[i+1];
        }
        int nbe=mh-1;
        VI res(0);
        for (int curr=1; curr<mg; curr++){
            while (nbe>=mg){
                int lf=leaffrom(curr);
               // cout<<curr<<" "<<lf<<" "<<mg<<endl;
                if (lf>=mg){
                  //  REP(j,mh) {cout<<st[j+1]<<" ";} cout<<endl;
                    r-=st[curr];
                    remove(curr);
                   // REP(j,mh) {cout<<st[j+1]  <<" ";} cout<<endl;
                    res.PB(curr);          //printf("%d%c", curr,nbe==mg?'\n':' ');
                    nbe--;
                }
                else{
                    break;
                }
            }
        }
        printf("%I64d\n",r);
        REP(i,SZ(res)){
            printf("%d%c", res[i],(i==SZ(res)-1)?'\n':' ');
        }
    }
}


#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRVI(N, X) VI X(N); for (int ___I=0; ___I<n; ___I++) scanf("%d", &(X[___I]))
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second

using namespace std;

VI st;
int g,h,mg,mh;

int leaffrom(int i){
        int l=2*i;
        int r=2*i+1;
        if (r<mh){
            if (st[l]>st[r]) return leaffrom(l);
            if (st[l]<st[r]) return leaffrom(r);
        }
        return i;
}

int remove(int i){
        int l=2*i;
        int r=2*i+1;
        if (r<mh){
            if (st[l]>st[r]) {st[i]=st[l]; return remove(l);}
            if (st[l]<st[r]) {st[i]=st[r]; return remove(r);}
        }
        st[i]=0;
        return 0;
}

int main(){
    CASET{
        RII(h,g);
        mg=1<<g;
        mh=1<<h;
        st=VI(mh,0);
        LL r(0);
        REP(i,mh-1){
            RI(st[i+1]);
            r+=st[i+1];
        }
        int nbe=mh-1;
        VI res(0);
        for (int curr=1; curr<=mg; curr++){
            while (nbe>=mg){
                int lf=leaffrom(curr);
               // cout<<curr<<" "<<lf<<" "<<mg<<endl;
                if (lf>=mg){
                  //  REP(j,mh) {cout<<st[j+1]<<" ";} cout<<endl;
                    r-=st[curr];
                    remove(curr);
                   // REP(j,mh) {cout<<st[j+1]  <<" ";} cout<<endl;
                    res.PB(curr);          //printf("%d%c", curr,nbe==mg?'\n':' ');
                    nbe--;
                }
                else{
                    break;
                }
            }
        }
        printf("%d\n",r);
        REP(i,SZ(res)){
            printf("%d%c", res[i],(i==SZ(res)-1)?'\n':' ');
        }
    }
}


#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRVI(N, X) VI X(N); for (int ___I=0; ___I<n; ___I++) scanf("%d", &(X[___I]))
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second

using namespace std;

VI st;
int g,h,mg,mh;

int leaffrom(int i){
        int l=2*i;
        int r=2*i+1;
        if (r<mh){
            if (st[l]>st[r]) return leaffrom(l);
            if (st[l]<st[r]) return leaffrom(r);
        }
        return i;
}

int remove(int i){
        int l=2*i;
        int r=2*i+1;
        if (r<mh){
            if (st[l]>st[r]) {st[i]=st[l]; return remove(l);}
            if (st[l]<st[r]) {st[i]=st[r]; return remove(r);}
        }
        st[i]=0;
        return 0;
}

int main(){
    CASET{
        RII(h,g);
        mg=1<<g;
        mh=1<<h;
        st=VI(mh,0);
        LL r(0);
        REP(i,mh-1){
            RI(st[i+1]);
            r+=st[i+1];
        }
        int nbe=mh-1;
        VI res(0);
        for (int curr=1; curr<mg; curr++){
            while (nbe>=mg){
                int lf=leaffrom(curr);
               // cout<<curr<<" "<<lf<<" "<<mg<<endl;
                if (lf>=mg){
                  //  REP(j,mh) {cout<<st[j+1]<<" ";} cout<<endl;
                    r-=st[curr];
                    remove(curr);
                   // REP(j,mh) {cout<<st[j+1]<<" ";} cout<<endl;
                    res.PB(curr);          //printf("%d%c", curr,nbe==mg?'\n':' ');
                    nbe--;
                }
                else{
                    break;
                }
            }
        }
        printf("%d\n",r);
        REP(i,SZ(res)){
            printf("%d%c", res[i],(i==SZ(res)-1)?'\n':' ');
        }
    }
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void solve (vector <int> &A,vector <int> &G, int a, int m)
{
    int x=2*a;
    int y=2*a+1;
    if (y>A.size())
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(A,G,x,m);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(A,G,y,m);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{
    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        vector <int> A(n,0);
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B(n-m);
        int ind=0;
        vector <int> G(n,1);
        for (int i=1; i<m; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(A,G,i,m);
                /*print(A);
                print(G);*/
                B[ind]=i;
                ind++;
            }
        }
        cout<<suma<<endl;
        for (int i=0; i<B.size(); i++)
        {
            cout<<B[i]<<" ";
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void solve (vector <int> &A,vector <int> &G, int a, int m)
{
    int x=2*a;
    int y=2*a+1;
    if (y>A.size())
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(A,G,x,m);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(A,G,y,m);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{
    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        vector <int> A(n,0);
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B(n-m);
        int ind=0;
        vector <int> G(n);
        for (int i=1;i<n;i++) {G[i]=1;}
        for (int i=1; i<m; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(A,G,i,m);
                /*print(A);
                print(G);*/
                B[ind]=i;
                ind++;
            }
        }
        cout<<suma<<endl;
        for (int i=0; i<B.size(); i++)
        {
            cout<<B[i]<<" ";
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void solve (vector <int> &A,vector <int> &G, int a, int m)
{
    int x=2*a;
    int y=2*a+1;
    if (y>A.size())
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(A,G,x,m);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(A,G,y,m);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{
    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        vector <int> A(n,0);
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B(n-m,0);
        int ind=0;
        vector <int> G(n,1);
        for (int i=1; i<m; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(A,G,i,m);
                /*print(A);
                print(G);*/
                B[ind]=i;
                ind++;
            }
        }
        cout<<suma<<endl;
        for (int i=0; i<B.size(); i++)
        {
            cout<<B[i]<<" ";
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MAXn=1024*1024+5;
int A[MAXn];

void solve (vector <int> &G, int a, int m, int n)
{
    int x=2*a;
    int y=2*a+1;
    if (y>n)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(G,x,m,n);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(G,y,m,n);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{
    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B;
        B.push_back(0);
        int ind=0;
        vector <int> G(n);
        for (int i=1;i<n;i++) {G[i]=1;}
        for (int i=1; i<m; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(G,i,m,n);
                /*print(A);
                print(G);*/
                ind++;
            }
            B.push_back(ind);
        }
        cout<<suma<<endl;
        int counter=0;
        for (int i=0; i<B.size()-1; i++)
        {
            for (int j=B[i];j<B[i+1];j++) {cout<<i+1<<" ";}
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void solve (vector <int> &A,vector <int> &G, int a, int m)
{
    int x=2*a;
    int y=2*a+1;
    if (y>A.size())
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(A,G,x,m);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(A,G,y,m);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{
    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        vector <int> A(n,0);
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B(n-m,0);
        int ind=0;
        vector <int> G(n,1);
        for (int i=1; i<n; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(A,G,i,m);
                /*print(A);
                print(G);*/
                if (B.size()>750000) {cout<<1/0<<endl;}
                B[ind]=i;
                ind++;
            }
        }
        cout<<suma<<endl;
        for (int i=0; i<B.size(); i++)
        {
            cout<<B[i]<<" ";
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MAXn=1024*1024+5;
int A[MAXn];

void solve (vector <int> &G, int a, int m, int n)
{
    int x=2*a;
    int y=2*a+1;
    if (y>n)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(G,x,m,n);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(G,y,m,n);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{
    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B(n-m);
        int ind=0;
        vector <int> G(n);
        for (int i=1;i<n;i++) {G[i]=1;}
        for (int i=1; i<m; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(G,i,m,n);
                /*print(A);
                print(G);*/
                B[ind]=i;
                ind++;
            }
        }
        cout<<suma<<endl;
        for (int i=0; i<B.size(); i++)
        {
            cout<<B[i]<<" ";
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MAXn=1024*1024+5;
int A[MAXn];

void solve (vector <int> &G, int a, int m, int n)
{
    int x=2*a;
    int y=2*a+1;
    if (y>n)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]==0 && A[y]==0)
    {
        G[a]=0;
        A[a]=0;
        return;
    }
    if (A[x]>A[y])
    {
        A[a]=A[x];
        solve(G,x,m,n);
        if (a<m)
        {
            if (A[2*a]>A[2*a+1])
            {
                G[a]=G[2*a];
            }
            else
            {
                G[a]=G[2*a+1];
            }
        }
        return;
    }
    A[a]=A[y];
    solve(G,y,m,n);
    if (a<m)
    {
        if (A[2*a]>A[2*a+1])
        {
            G[a]=G[2*a];
        }
        else
        {
            G[a]=G[2*a+1];
        }
    }
    return;

}

void print (vector <int> X)
{
    for (int j=0; j<X.size(); j++)
    {
        cout<<X[j]<<" ";
    }
    cout<<endl;
    return;

}

int main()
{ios_base::sync_with_stdio(false); cin.tie(0);

    int T;
    cin>>T;
    for (int t=0; t<T; t++)
    {
        int h,g;
        cin>>h>>g;
        int n=1;
        for (int i=0; i<h; i++)
        {
            n=2*n;
        }
        long long int suma=0;
        for (int i=1; i<n; i++)
        {
            cin>>A[i];
            suma=suma+A[i];
        }
        int m=1;
        for (int i=0; i<g; i++)
        {
            m=2*m;
        }
        int veik=n-m;
        vector <int> B;
        B.push_back(0);
        int ind=0;
        vector <int> G(n);
        for (int i=1;i<n;i++) {G[i]=1;}
        for (int i=1; i<m; i++)
        {
            while (G[i]==1)
            {
                suma=suma-A[i];
                solve(G,i,m,n);
                /*print(A);
                print(G);*/
                ind++;
            }
            B.push_back(ind);
        }
        cout<<suma<<endl;
        int counter=0;
        for (int i=0; i<B.size()-1; i++)
        {
            for (int j=B[i];j<B[i+1];j++) {cout<<i+1<<" ";}
        }
        cout<<endl;

    }

    return 0;
}


///#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;

#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define openfile  if (fopen(task ".inp","r")){freopen(task ".inp","r",stdin);freopen(task ".out","w",stdout);}
#define ll long long
#define ii pair <int, int>
#define pll pair <ll, ll>
#define all(c) c.begin(), c.end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

const int INF = 1E9;
const int MOD = 1E9 + 7;
const int N = 4E6 + 5;

int t = 1;

int n, h[N], g, height, avail[N], ans[N];
ll a[N];

void Find_Level(int id, int p)
{
    if (id == p) h[id] = 1;
    else h[id] = h[p] + 1;
    if (h[id] >= 20) return ;
    Find_Level(id * 2, id);
    Find_Level(id * 2 + 1, id);
}

void F(int id)
{
    while ((avail[id * 2] == 1 && avail[id * 2 + 1] == 1) == 0){
        if(a[id * 2] > a[id * 2 + 1]) {
            a[id] = a[id * 2];
            id = id * 2;
        }
        else {
            a[id] = a[id * 2 + 1];
            id = id * 2 + 1;
        }
    }
    a[id] = 0;
    avail[id] = 1;
}

bool check(int id)
{
    if (avail[id] == 1) return false;

    while ((avail[id * 2] == 1 && avail[id * 2 + 1] == 1) == 0){
        if (a[id * 2] > a[id * 2 + 1]) id = id * 2;
        else id = id * 2 + 1;
    }
    return h[id] > g;
}

void Input()
{
    cin >> height >> g;
    n = (1 << height) - 1;
    for (int i = 1; i <= n; i++) cin >> a[i];
}

void Prepare()
{

}

void Solve()
{
    ll Sum = 0;
    for (int i = 1; i <= 2 * n + 1; i++) avail[i] = 1;

    for (int i = 1; i <= n; i++) {
        Sum += a[i];
        avail[i] = 0;
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)if (avail[i] == 0) {
        while(check(i)) {
                    cnt++;
                    ans[cnt] = i;
                    Sum -= a[i];
                    F(i);
        }
    }

    cout << Sum << "\n";
    for (int i = 1; i <= cnt; i++) cout << ans[i] << " ";
    cout << "\n";

}

#define task "test"
int main()
{
    openfile;
    Find_Level(1, 1);
     cin >> t;
    while (t--) {
        Input();
        Prepare();
        Solve();
    }
}


///#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;

#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define openfile  if (fopen(task ".inp","r")){freopen(task ".inp","r",stdin);freopen(task ".out","w",stdout);}
#define ll long long
#define ii pair <int, int>
#define pll pair <ll, ll>
#define all(c) c.begin(), c.end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

const int INF = 1E9;
const int MOD = 1E9 + 7;
const int N = 1E7 + 5;

int t = 1;

int n, h[N], g, height, avail[N], ans[N];
ll a[N];

void Level(int u, int d)
{
    h[u] = d;
    if (d == 20) return;
    Level(u * 2, d + 1);
    Level(u * 2 + 1, d + 1);
}

void F(int id)
{
    while ((avail[id * 2] == 1 && avail[id * 2 + 1] == 1) == 0){
        if(a[id * 2] > a[id * 2 + 1]) {
            a[id] = a[id * 2];
            id = id * 2;
        }
        else {
            a[id] = a[id * 2 + 1];
            id = id * 2 + 1;
        }
    }
    a[id] = 0;
    avail[id] = 1;
}

bool check(int id)
{
    if (avail[id] == 1) return false;

    while ((avail[id * 2] == 1 && avail[id * 2 + 1] == 1) == 0){
        if (a[id * 2] > a[id * 2 + 1]) id = id * 2;
        else id = id * 2 + 1;
    }
    return h[id] > g;
}

void Input()
{
    cin >> height >> g;
    n = (1 << height) - 1;
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
}

void Prepare()
{

}

void Solve()
{
    ll Sum = 0;
    for (int i = 1; i <= 2 * n + 1; i++) avail[i] = 1;

    for (int i = 1; i <= n; i++) {
        Sum += a[i];
        avail[i] = 0;
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)if (avail[i] == 0) {
        while(check(i)) {
                    cnt++;
                    ans[cnt] = i;
                    Sum -= a[i];
                    F(i);
        }
    }

    printf("%lld\n", Sum);
    for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
    cout << "\n";

}

#define task "test"
int main()
{
    openfile;
    Level(1, 1);
     cin >> t;
    while (t--) {
        Input();
        Prepare();
        Solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;

void solve() {
    int h, g; cin>>h>>g;
    vector<ll> t(1<<(h+1), 0);
    for (int i=1; i<(1<<h); i++) {
	cin>>t[i];
    }

    function<bool(int)> pop = [&](int i) {
	if (t[i*2]==0 && t[i*2+1]==0) {
	    if (i < (1<<g)) {
		return false;
	    }
	    t[i] = 0;
	    return true;
	}

	int to = t[i*2] > t[i*2+1] ? i*2: i*2+1;
	int val = t[to];
	if (pop(to)) {
	    t[i] = val;
	    return true;
	} else {
	    return false;
	}
    };

    vector<int> res;
    for (int i=1; i<(1<<h); i++) {
	while (t[i] > 0) {
	    if (pop(i)) {
		res.push_back(i);
	    } else {
		break;
	    }
	}
    }

    ll tot = std::accumulate(t.begin(), t.end(), 0LL);

    cout<<tot<<"\n";
    for (int i: res) cout<<i<<" ";
    cout<<"\n";
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    int t; cin>>t;
    while (t--) {
	solve();
    }

    return 0;
}