Lang:Python2
Edit12345678910111213141516171819202122232425262728293031import mathimport itertoolswhile True:try:M, N = (int(i) for i in raw_input().split())WHs = []for n in xrange(N):w, h = (int(i) for i in raw_input().split())WHs.append((w, h))ans = float('inf')for itr in itertools.combinations(range(N), N-1):H = 0heights = []widthLeft = Mfor i in itr:if widthLeft >= WHs[i][0]:heights.append(WHs[i][1])widthLeft -= WHs[i][0]else:h_tmp = int(math.ceil(float(widthLeft)/WHs[i][0]*WHs[i][1]))heights.append(h_tmp)widthLeft = 0if widthLeft <= 0:H += max(heights)heights = []widthLeft = Mif heights:H += max(heights)ans = min(ans, H)print ans