1. 
/**
* FWP, Ausgew¤hlte Probleme aus dem ACM Programming Contest, SS10
* Problem: 11782 - Optimal Cut
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2882
*
* @author Evgeni Pavlidis
* @version 1.0, 06/02/2010
*
* Method : DP - Memoization
* Status : Accepted
* Runtime: 0.988
*/

import java.io.*;
import java.util.*;

public class Main {

private static int h,size;
private static int[] a;


private static int[] topDown(int n, int k)
{
int[] result = new int[k+1];
if(n*2+1 >= size || k == 1)
{
for(int i = 1; i <= k; i++)
result[i] = a[n];
return result;
}

int[] left = topDown(n*2+1, k-1);
int[] right = topDown(n*2+2, k-1);
int max;

result[1] = a[n];

for(int i = 2; i <= k; i++)
{
max = result[i-1];

for(int l = 1; l < i; l++)
if(left[l] + right[i-l] > max)
max = left[l]+right[i-l];

result[i] = max;
}

return result;
}


public static void main(String...args)
{
Scanner scanner = new Scanner(System.in);
int k;

while(true)
{
h = scanner.nextInt()+1;
if(h == 0)
break;
k = scanner.nextInt();

size = (1 << h) - 1;
a = new int[size];


for(int i = 0; i < size; i++)
a[i] = Integer.MAX_VALUE;


// build heap tree
int pos = 0;
for(int i = 0; i < size; i++)
{
a[pos] = scanner.nextShort();

if(pos*2+1 < size)
pos = pos * 2 + 1;
else
if(pos % 2 == 0)
{
pos = pos + 1;
pos = (pos - 1 )/ 2;

while(a[(pos-1)/2] == Integer.MAX_VALUE)
pos = (pos-1) / 2;
}
else
pos++;
}

int[] result = topDown(0,k);

System.out.println(result[k]);
}
}
}