-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperket.py
32 lines (22 loc) · 901 Bytes
/
perket.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
SOURNESS_INDEX = 0
BITTERNESS_INDEX = 1
def decimal_to_binary_array(i, length_limit):
binary = bin(i).replace("0b", "").zfill(length_limit)
binary_array = list(map(lambda x: True if x == "1" else False, binary))
return binary_array
def get_difference(ingredients, combination):
sourness_product = 1
bitterness_sum = 0
for i in range(len(ingredients)):
if combination[i]:
sourness_product *= ingredients[i][SOURNESS_INDEX]
bitterness_sum += ingredients[i][BITTERNESS_INDEX]
return abs(sourness_product - bitterness_sum)
n = int(input())
ingredients = [list(map(int, input().split())) for _ in range(n)]
min_difference = 1e8
for i in range(1, 2 ** n):
combination = decimal_to_binary_array(i, n)
difference = get_difference(ingredients, combination)
min_difference = min(difference, min_difference)
print(min_difference)