Помогите максимально оптимизировать код:
n = int(input())
summa = []
procent = []
itog = 0
def mes(summa, procent):
mes = []
for i in range(len(summa)):
mes.append(summa * procent)
l = mes.index(max(mes))
return l
for i in range:
a = input().split()
summa.append(int(a[0]))
procent.append(int(a[1])/100)
a = []
p = int(input())
while sum(summa) != 0:
l = mes(summa, procent)
m = p
while m != 0:
if summa[l] >= m:
itog += m
summa[l] = summa[l] - m
m = 0
else:
itog += summa[l]
m = m - summa[l]
summa[l] = 0
l = mes(summa, procent)
if sum(summa) == 0:
break
for i in range(len(summa)):
summa = summa * (1+procent)
print(itog)
n = int(input())
summa = []
procent = []
itog = 0
def mes(summa, procent):
mes = []
for i in range(len(summa)):
mes.append(summa * procent)
l = mes.index(max(mes))
return l
for i in range:
a = input().split()
summa.append(int(a[0]))
procent.append(int(a[1])/100)
a = []
p = int(input())
while sum(summa) != 0:
l = mes(summa, procent)
m = p
while m != 0:
if summa[l] >= m:
itog += m
summa[l] = summa[l] - m
m = 0
else:
itog += summa[l]
m = m - summa[l]
summa[l] = 0
l = mes(summa, procent)
if sum(summa) == 0:
break
for i in range(len(summa)):
summa = summa * (1+procent)
print(itog)