-
Notifications
You must be signed in to change notification settings - Fork 13
/
level_order.py
executable file
·337 lines (322 loc) · 8.08 KB
/
level_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# /*
# *
# ********************************************************************************************
# * AUTHOR : AKASH KANDPAL *
# * Language : Python2 *
# * Motto : The master has failed more times than the beginner has even tried. *
# * IDE used: Atom *
# * My Domain : http://harrypotter.tech/ *
# ********************************************************************************************
# *
# */
from collections import Counter
from math import ceil
from fractions import gcd
import math
import itertools
from itertools import permutations
from itertools import combinations
import calendar
from itertools import product
from datetime import date
from string import ascii_uppercase
def printdec(ans):
print '{0:.6f}'.format(ans)
def countchars(stra):
s=Counter(stra)
return s
def readInts():
return list(map(int, raw_input().strip().split()))
def readInt():
return int(raw_input())
def readStrs():
return raw_input().split()
def readStr():
return raw_input().strip()
def readarr(n):
return [map(int,list(readStr())) for i in xrange(n)]
def readnumbertolist():
a=[int(i) for i in list(raw_input())]
return a
def strlistTostr(list1):
return ''.join(list1)
def numlistTostr(list1):
return ''.join(str(e) for e in list1)
def strTolist(str):
return str.split()
def strlistTointlist(str):
return map(int, str)
def slicenum(number,x):
return int(str(number)[:x])
def precise(num):
return "{0:.10f}".format(num)
def rsorted(a):
return sorted(a,reverse=True)
def binar(x):
return '{0:063b}'.format(x)
def findpermute(word):
perms = [''.join(p) for p in permutations(word)]
perms = list(set(perms))
return perms
def findsubsets(S,m):
return list(set(itertools.combinations(S, m)))
def sort1(yy,index):
return yy.sort(key = lambda x:x[index])
def reversepair(yy):
return yy[::-1]
def checkint(x):
return (x).is_integer()
def sum_digits(n):
s = 0
while n:
s += n % 10
n //= 10
return s
def vowel_count(str):
count = 0
vowel = set("aeiouAEIOU")
for alphabet in str:
if alphabet in vowel:
count = count + 1
return count
def leapyear(year):
return calendar.isleap(year)
def factorial(n):
p=1
for i in range(2,n+1):
p=p*i
return p
def primes_sieve(limit):
limitn = limit+1
not_prime = set()
primes = []
for i in range(2, limitn):
if i in not_prime:
continue
for f in range(i*2, limitn, i):
not_prime.add(f)
primes.append(i)
return primes
def distinctstr(s):
t =''.join(set(s))
return t
def countdict(s):
d ={}
for i in range(len(s)):
if s[i] not in d.keys():
d[s[i]]=1
else:
d[s[i]]+=1
return d
import operator as op
def nck(n, k):
k = min(n-k,k)
result = 1
for i in range(1, k+1):
result = result* (n-i+1) / i
return result
def gcd(a,b):
if (a == 0):
return b;
return gcd(b%a, a);
def powerm(x,y,m=1000000007):
if (y == 0):
return 1;
p = powerm(x, y/2, m) % m;
p = (p * p) % m;
if(y%2 == 0):
return p
else:
return (x * p) % m;
def power(x,y):
if (y == 0):
return 1;
p = power(x, y/2) ;
p = (p * p) ;
if(y%2 == 0):
return p
else:
return (x * p) ;
def modInverse(a,m):
g = gcd(a, m);
if(g!=1):
return -1
else:
return powerm(a, m-2, m);
def lcm(a, b):
return a * b / gcd(a, b)
def matrixcheck(x,y):
faadu = []
directions = zip((0,0,1,-1),(1,-1,0,0))
for dx,dy in directions:
if R>x+dx>=0<=y+dy<C and A[x+dx][y+dy]==0:
faadu.append((x+dx,y+dy))
return faadu
def stringcount(s):
return [s.count(i) for i in "abcdefghijklmnopqrstuvwxyz"]
def bubbleSort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
def isSubsetSum(st, n, sm) :
# arr, n, k
subset=[[True] * (sm+1)] * (n+1)
for i in range(0, n+1) :
subset[i][0] = True
for i in range(1, sm + 1) :
subset[0][i] = False
for i in range(1, n+1) :
for j in range(1, sm+1) :
if(j < st[i-1]) :
subset[i][j] = subset[i-1][j]
if (j >= st[i-1]) :
subset[i][j] = subset[i-1][j] or subset[i - 1][j-st[i-1]]
return subset[n][sm];
def decimal_to_octal(dec):
decimal = int(dec)
return oct(decimal)
def decimal_to_binary(dec):
decimal = int(dec)
return bin(decimal)
def decimal_to_hexadecimal(dec):
decimal = int(dec)
return hex(decimal)
def find_duplicate(expr):
stack=[]
char_in_between = 0
f =1
for i in range(0, len(expr)):
if expr[i] == '}' or expr[i] == ')':
pair = '{' if expr[i] == '}' else '('
pop=''
while(len(stack) > 0 and pop != pair):
pop = stack.pop()
if (pop != '{' and pop != '('): char_in_between +=1
if char_in_between == 0:
print "Duplicate"
f =0
break
char_in_between = 0
else:
stack.append(expr[i])
return f
def dictlist(keys,values):
{d.setdefault(key,[]).append(value) for key, value in zip(keys,values)}
return d
def mullistbyconst(my_list,r):
my_new_list = []
for i in my_list:
my_new_list.append(i * r)
return my_new_list
def coinchange(S, m, n):
# (arr,length,sum)
table = [0 for k in range(n+1)]
table[0] = 1
for i in range(0,m):
for j in range(S[i],n+1):
table[j] += table[j-S[i]]
return table[n]
import itertools
def permutation_of_list(lista):
return list(itertools.permutations(lista))
def palincheck(i):
return str(i) == str(i)[::-1]
def bigMod(a, b, c):
if (a == 0 or b == 0) :
return 0
if (a == 1) :
return b
if (b == 1) :
return a
a2 = bigMod(a, b / 2, c)
if ((b & 1) == 0) :
return (a2 + a2) % c
else :
return ((a % c) + (a2 + a2)) % c
def days(year1,year2):
begin = date(year1, 1, 1)
end = date(year2, 1, 1)
return (end-begin).days
from functools import reduce
def factors(n):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(pow(n, 0.5) + 1)) if n % i == 0)))
def prelongfact(factt):
for i in reversed(range(1,int(factt**0.5))):
if factt%i==0:
break
return factt/i
def factmul(n,lim,m):
mul=1
ans=1
if(n>=lim):
print 0
else:
for j in range(1,n+1):
mul=(mul*j)%m
ans=(ans*mul)%m
print ans
def knapSack(W , wt , val , n):
if n == 0 or W == 0 :
return 0
if (wt[n-1] > W):
return knapSack(W , wt , val , n-1)
else:
return max(val[n-1] + knapSack(W-wt[n-1] , wt , val , n-1),
knapSack(W , wt , val , n-1))
def findOccurrences(s, ch):
return [i for i, letter in enumerate(s) if letter == ch]
# m = 329885391853
# lim = prelongfact(m)
mod = 10 ** 9 + 7
# fact=[1]
# for i in xrange(1,100001):
# fact.append(((arr[i-1]%mod)*(i%mod))%mod)
# for i,j in product(xrange(R),xrange(C)):
# print "Case #{}: {}".format(i+1,ans)
# file1 = open("abc.txt","w")
# for i in range(1000):
# file1.write("HITESH SIR ")
# file1.close()
# l = [ [] for i in range(k) ]
# arr = [[0 for x in range(100)] for y in range(100)]
m,n=readInts()
l1 = readInts()
l=[]
a=maxx=0
for i in range(m):
l.append(i)
sumx=l1[i]
for j in range(1,2**n):
k=l.pop(0)
if k>=m:break
sumx+=l1[k]
l.append(2*k+1)
l.append(2 * k + 2)
del l[:]
if maxx<sumx:
maxx=sumx
a=i
l.append(a)
for j in range(1,2**n):
k=l.pop(0)
if k>=m:break
print(l1[k]),
l.append(2*k+1)
l.append(2 * k + 2)
'''
Case 1:
Input:
7 2
1 2 3 4 5 6 7
Output:
3 6 7
Case 2:
Input:
9 2
2 4 8 1 3 5 6 7 8
Output:
8 5 6
'''