def insertionSort(A,index):
for j in range(1,len(A)):
key = A[j]
key2 = index[j]
i=j-1
while i>=0 and key > A[i]:
A[i+1] = A[i]
index[i+1] = index[i]
i-=1
A[i+1] = key
index[i+1] = key2
def fractional_knapsack(value, weight, capacity):
index = list(range(len(value)))
ratio = [v/w for v, w in zip(value, weight)]
insertionSort(ratio,index)
max_value = 0
fractions = [0]*len(value)
for i in index:
if weight[i] <= capacity:
fractions[i] = 1
max_value += value[i]
capacity -= weight[i]
else:
fractions[i] = capacity/weight[i]
max_value += value[i]*capacity/weight[i]
break
return max_value, fractions
n = int(input('Enter number of items: '))
value = input('Enter the values of the {} item(s) in order: '
.format(n)).split()
value = [int(v) for v in value]
weight = input('Enter the positive weights of the {} item(s) in order: '
.format(n)).split()
weight = [int(w) for w in weight]
capacity = int(input('Enter maximum weight: '))
max_value, fractions = fractional_knapsack(value, weight, capacity)
print('The maximum value of items that can be carried:', max_value)
print('The fractions in which the items should be taken:', fractions)