Problem:
 Score : 300 points    Problem Statement Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.  There are N stores that sell energy drinks. In the i -th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.  What is the minimum amount of money with which he can buy M cans of energy drinks?  It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.      Constraints  All values in input are integers.  1 \leq N, M \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^5  B_1 + ... + B_N \geq M         Input Input is given from Standard Input in the following format:  N  M  A_1  B_1  A_2  B_2  \vdots  A_N  B_N       Output Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.        Sample Input 1 2 5
4 9
2 4      Sample Output 1 12  With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.       Sample Input 2 4 30
6 18
2 5
3 10
7 9      Sample Output 2 130       Sample Input 3 1 100000
1000000000 100000      Sample Output 3 100000000000000  The output may not fit into a 32 -bit integer type.

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    aa = sorted([LI() for _ in range(n)])
    r = 0
    for a,b in aa:
        t = min(b,m)
        r += a * t
        m -= t

    return r


print(main())

Solution (Python):
N,M=map(int,input().split())
lst=sorted([list(map(int,input().split())) for i in range(N)])
_sum_drink=0
money=0
for i in lst:
    if _sum_drink+i[1] < M:
        money += i[0]*i[1]
        _sum_drink += i[1]
    else:
        money += i[0]*(M-_sum_drink)
        _sum_drink += M-_sum_drink
    if _sum_drink == M:
        print(money)
        exit()

Solution (Python):
def main():
	N, M = map(int, input().split())
	S = []
	for i in range(N):
		a, b = map(int, input().split())
		S.append([a, b])
	sorted_shop_list = sorted(S, key=lambda x: x[0])
	cost = 0
	for i in sorted_shop_list:
		a = i[0]
		b = i[1]
		if b < M:
			M -= b
			cost += a * b
		else:
			cost += a * M
			break
	print(cost)
	return

if __name__ == '__main__':
	main()

Solution (Python):
n,m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
a.sort()
ans = 0
for i in a:
  money = i[0]
  num = i[1]
  if m <= num:
    ans += money * m
    break
  else:
    ans += money * num
    m -= num
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = sorted([tuple(map(int, input().split())) for i in range(n)])
ans = 0
for i in range(n):
    if m == 0:
        break
    if m - ab[i][1] >= 0:
        m -= ab[i][1]
        ans += ab[i][0] * ab[i][1]
    else:
        ans += ab[i][0] * m
        m = 0
print(ans)

Solution (Python):
n,m=map(int,input().split())
ans=[0]*2
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append((a,b))
l.sort()
for i,j in l:
    if ans[1]+j<=m:
        ans[0]+=i*j
        ans[1]+=j
    else:
        ans[0]+=i*(m-ans[1])
        break
print(ans[0])

Solution (Python):
import numpy as np

N, M = [int(i) for i in input().split()]
A = []
B = []
for n in range(0, N):
  a, b = [int(i) for i in input().split()]
  A.append(a)
  B.append(b)

A = np.array(A)
B = np.array(B)
B = B[np.argsort(A)]
A = A[np.argsort(A)]

m = 0
n = 0
cost = 0
while True:
  if m >= M:
    break
  m = m + B[n]
  cost = cost + A[n] * B[n]
  n = n + 1
if m != 0:
  cost = cost - A[n - 1] * (m - M)

print(cost)

Solution (Python):
n,m = map(int,input().split())


list_A=[]
list_B=[]
sum_num=0
for i in range(n):
    A,B = map(int,input().split())
    list_A.append(A)
    list_B.append(B)


list_AB = []
for i in range(n):
    list_AB.append( (list_A[i], list_B[i]))

list_AB.sort()
list_A , list_B = zip(*list_AB)






m_1=m
for i in range(n):
    m_2=m_1
    m_1=m_1-int(list_B[i])
    if m_1<0:
        sum_num=sum_num + int(list_A[i])*m_2
    else:
        sum_num=sum_num +int(list_A[i])*int(list_B[i])
    
    
    if m_1<0:
        break

print(sum_num)

Solution (Python):

"""
Created on Sat Mar  9 21:40:34 2019

@author: iwata
"""

N,M = map(int, input().split())
numlist =[]
money = 0
count = 0
count1 = 0
count2 = 0

for i in range(N):
    AB = list(map(int, input().split()))
    numlist.append(AB)

numlist.sort()

while count < M:
    while numlist[count1][1] > count2:
        money = money + numlist[count1][0]
        count = count + 1
        count2 = count2 + 1
        if count == M:
            break
    count1 = count1 + 1
    count2 = 0
    
print(money)

Solution (Python):
import numpy as np

N, M = map(int, input().split())
AB = []
for i in range(N):
    AB.append(list(map(int, input().split())))
    
AB = np.array(AB)
AB_sorted = AB[AB[:,0].argsort(),:]

cs = np.cumsum(AB_sorted[:,1])
n_full_store = (cs <= M).sum()
money_a = np.sum(AB_sorted[:n_full_store,0] * AB_sorted[:n_full_store,1])
if M - cs[n_full_store-1] > 0:
    money_b = (M - cs[n_full_store-1]) * AB_sorted[n_full_store,0]
elif M - cs[n_full_store-1] < 0:
               money_b = AB_sorted[0,0] * M
else:
    money_b = 0
print(money_a + money_b)

Solution (Python):

"""
Created on Sat Mar  9 22:21:07 2019

@author: yuta
"""

NM = list(map(int,input().split()))
N = NM[0]
M = NM[1]
AB = [list(map(int,input().split())) for _ in range(N)]

def main(n,m,ab):
    ab.sort(key=lambda x:x[0])
    count = 0
    price = 0
    for i in range(n):
        for j in range(ab[i][1]):
            if count == m:
                break
            else:
                price += ab[i][0]
                count += 1
    print(price)
main(N,M,AB)

Solution (Python):
N,M=[int(z) for z in input().split(" ")]
d=[]

for z in range(0,N):
  d.append([int(z) for z in input().split(" ")])

r=M
ans=0
for z in sorted(d):
  if r==0:
    break
  if z[1]<=r:
     ans+=z[0]*z[1]
     r-=z[1]
  else:
    ans+=z[0]*r
    r=0

print("{:d}".format(ans))

Solution (Python):
init = input().split(' ')
init = [int(i) for i in init]
n,m= init[0],init[1]

ab = []

try:
	while True:
		inp1 = input().split(' ')
		inp1 = [int(i) for i in inp1]
		ab.append([inp1[0],inp1[1]])
except:
	pass

ab.sort()
total = sum([ab[j][0]*ab[j][1] for j in range(n)])
deltarget = sum([ab[j][1] for j in range(n)]) - m

while deltarget > 0:
	if deltarget - ab[len(ab)-1][1] > 0:
		total -= ab[len(ab)-1][0]*ab[len(ab)-1][1]
		deltarget -= ab[len(ab)-1][1]
		ab.pop(len(ab)-1)
	else:
		total -= ab[len(ab)-1][0]*deltarget
		break

print(total)

Solution (Python):
import itertools

n,m = map(int, input().split())
AB = []
ans = 0
for n in range(n):
    a,b = map(int,input().split())
    AB.append([a,b])
AB.sort()
A,B = zip(*AB)
A,B = list(A), list(B)

i = list(_ for _ in itertools.accumulate(B) if _ <= m)
if i == []:
    ans = A[0]*m
else:
    B = B[:len(i)] + [m-i[-1]]
    A = A[:len(B)]

    ans = sum([a*b for a,b in zip(A,B)])
print(ans)

Solution (Python):
N, M = [int(i) for i in input().split()]
J = []
for i in range(N):
    A = list([int(j) for j in input().split()])
    J.append(A)

J.sort()
total = 0
for i in range(N):
    if M - J[i][1] < 0:
        total += M * J[i][0]
        break
    else:
        total += J[i][0]*J[i][1]
        M = M - J[i][1]
print(total)

Solution (Python):
n, m = map(int, input().split())
sum = 0
money = 0
list_ab = list()
for i in range(n):
    list_ab.append(list(map(int, input().split())))
list_ab = sorted(list_ab)
for i in range(n):
    if(list_ab[i][1] >= m - sum):
        money += list_ab[i][0] * (m - sum)
        break
    else:
        sum += list_ab[i][1]
        money += list_ab[i][0]*list_ab[i][1]
print(money)

Solution (Python):
N, M = map(int, input().split(' '))
data = []
for n in range(N):
  A_n, B_n = map(int, input().split(' '))
  data.append([A_n,B_n])
data = sorted(data)
  

drink = 0
ans = 0
for d in data:
  a, b = d
  drink += b
  ans += a*b
  if drink > M:
    ans -= a*(drink-M)
    break
  
print(ans)

Solution (Python):


NM = list(map(int, input().split()))
M = NM[1]
Okane = 0

AB = [[0 for i in range(2)] for j in range(NM[0])]

for i in range(NM[0]):
    AB[i] = list(map(int, input().split()))

AB.sort()

for i in range(NM[0]):
  if M >= AB[i][1]:
    Okane = Okane + AB[i][0] * AB[i][1]
    M = M - AB[i][1]
  elif M < AB[i][1]:
    Okane = Okane + AB[i][0] * M
    M = 0
    
  if M == 0:
    break
      
print(Okane)

Solution (Python):
N,M=map(int,input().split())
sum=0
A=[[int(i) for i in input().split()]for j in range(N)]
A.sort()
i=0
while True:
  if A[i][1]>=M:
    sum+=A[i][0]*M
    break
  else:
    sum+=A[i][0]*A[i][1]
  M-=A[i][1]
  i+=1
print(sum)

Solution (Python):

import sys
 
 
N,M = sys.stdin.readline().strip().split()
N = int(N)
M = int(M)

A = []
B = []
for i in range(N):
    a,b = sys.stdin.readline().strip().split()
    A.append(int(a))
    B.append(int(b))



comb = zip(A,B)
comb = sorted(comb)




num=0
money=0
num_tmp=0
while(num<M):
    if(len(comb)>1):
        if(M-num<comb[0][1]):
            num_tmp = M - num
            num += num_tmp
        else:

            num_tmp = comb[0][1]
            num += num_tmp
        money += comb[0][0] * num_tmp
        comb.pop(0)


    else:
        num_tmp = M - num
        num += num_tmp
        money += comb[0][0] * num_tmp

        
print(money)

Solution (Python):
num = input()
N, M = [int(x) for x in num.split()]

dic = []
for n in range(N):
    num = input()
    a, b = [int(x) for x in num.split()]
    dic.append((a, b))

sort = sorted(dic, key=lambda x: x[0])
count = 0
money = 0
ind = 0
while count < M:

    if (M - count) > sort[ind][1]:
        money += sort[ind][0] * sort[ind][1]
        count += sort[ind][1]
    else:
        money += sort[ind][0] * (M - count)
        count += (M - count)

    ind += 1
print(money)

Solution (Python):

N, M = map(int, input().split())
t = [tuple(map(int, input().split())) for i in range(N)] 
t = sorted(t, key = lambda tt: tt[0])
n = 0
ans = 0
for a,b in t:
    if n+b < M:
        ans += a*b
        n += b
    else:
        ans += (M-n)*a
        break
print(ans)

Solution (Python):
import sys
n, m = map(int, input().split())
sumans = 0
ans = 0

list = [[0 for i in range(2)] for j in range(n)]

for i in range(n):
	list[i][0], list[i][1]= map(int, input().split())
	
list = sorted(list)

for i in range(n):
	for j in range(int(list[i][1])):
		sumans += list[i][0]
		ans += 1
		if ans == m:
			print(sumans)
			sys.exit()

Solution (Python):
i = list(map(int, input().split()))
N = i[0]
M = i[1]
AB = [list(map(int, input().split())) for l in range(N)]
AB.sort(key=lambda x:(x[0]))

total = 0
count = 0

for i in range(N):
  if (M - AB[i][1]) >= 0:
    total = total + (AB[i][0] * AB[i][1])
    M = M - AB[i][1]
  else:
    total = total + (AB[i][0] * M)
    count = count + 1
    if count == 1:
      break

print(total)

Solution (Python):


def main():
    n, m = [int(i) for i in input().split()]
    e = [[int(i) for i in input().split()] for i in range(n)]

    e = sorted(e, key=lambda x: x[0])

    count = p_sum = 0
    for shop in e:
        for i in range(shop[1]):
            count += 1
            p_sum += shop[0]
            if count >= m:
                return p_sum


if __name__ == "__main__":
    print(main())

Solution (Python):
n, m = map(int, input().split())
drinks = [input().split() for i in range(n)]
drinks = [[int(j) for j in i] for i in drinks]
drinks.sort()
money = 0
mm = m
for i in drinks:
    if mm-i[1]<=0:
        money += i[0]*mm
        break
    else:
        money += i[0]*i[1]
        mm += -i[1]
print(money)

Solution (Python):
N,M = map(int,input().split())

A=[]
for i in range(N):
    A.append(list(map(int,input().split())))

A.sort(key=lambda x: x[0])

cnt = 0
fee = 0
flg=0
for j in A:
    for k in range(j[1]):
        cnt += 1
        fee += j[0]
        if cnt == M:
            print(fee)
            flg=1
            break
    
    if flg==1:
        break

Solution (Python):
n, m = map(int, input().split())
shop = [0 for i in range(n)]
for i in range(n):
    shop[i] = list(map(int, input().split()))
shop = sorted(shop)
can = 0
cost = 0
while 1:
    for i in range(n):
        if shop[i][1] < m:
            can += shop[i][1]
            cost += shop[i][0]*shop[i][1]
            m -= shop[i][1]
        else:
            cost += shop[i][0]*m
            break
    break
print(cost)

Solution (Python):
def main():
    n,m = map(int,input().split())

    x = []
    for i in range(n):
        a,b = map(int,input().split())
        x.append((a,b))

    ans,ct = 0,0
    for a,b in sorted(x):
        for _ in range(b):
            ans += a
            ct += 1
            if ct == m:
                return ans
    return -1

if __name__ == "__main__":
    print(main())

Solution (Python):
N,M = list(map(int, input().split()))
lis=[]
for i in range(N):
    a,b = list(map(int, input().split()))
    lis.append([a,b])

lis.sort()

count=0
money=0
ind=0
while count+lis[ind][1]<M:
    count+=lis[ind][1]
    money+=lis[ind][0]*lis[ind][1]
    ind+=1
    
    

money+=(M-count)*lis[ind][0]
print(int(money))

Solution (Python):


n, m = map(int, raw_input().split())

dic = {} 

for i in range(n):
    a, b = map(int, raw_input().split())
    if a in dic: dic[a] += b
    else: dic.update({a:b})

dic = sorted(dic.items())

count = 0
yen = 0
for k, v in dic:

    if v < m-count:
        yen += k*v
        count += v
    else:
        yen += k*(m-count)
        count += m-count
        print yen
        break

Solution (Python):

n, m = map(int, input().split())
a = []
b = []
ab = []
for i in range(n):
    abi = list(map(int, input().split()))
    ab.append(abi)
ab.sort(key = lambda x:(x[0]))
honsu = 0
kingaku = 0
j = 0
while honsu < m:
    honsu = honsu + ab[j][1]
    j = j + 1

honsu2 = 0
for i in range(j-1):
    honsu2 = honsu2 + ab[i][1]
    kingaku = kingaku + ab[i][0]*ab[i][1]
kingaku = kingaku + ab[j-1][0]*(m - honsu2)
print(kingaku)

Solution (Python):
n, m = (int(_) for _ in input().split())
ab_list = [[int(i) for i in input().split()] for i in range(n)]

ab_list = sorted(ab_list)

buy = 0
money = 0
j = 0


while (buy < m):
    buy += ab_list[j][1]
    money += ab_list[j][0] * ab_list[j][1]
    j += 1

print(money-(ab_list[j-1][0]*(buy-m)))

Solution (Python):
n,m = map(int,input().split())
a = list()
for i in range(n):
    b,c = map(int,input().split())
    a.append([b,c])
a.sort(key=lambda x:x[0])
ans = 0
for x in a:
    if x[1] <= m:
        ans += x[1] * x[0]
        m-=x[1]
    else:
        ans += m * x[0]
        m = 0
    if m == 0:
        break
print(ans)

Solution (Python):
N, M = map(int, input().split(" "))
A_list = []
B_list = []
total = 0

for i in range(N):
    A, B = map(int, input().split(" "))
    A_list.append(A)
    B_list.append(B)

z = list(zip(A_list, B_list))
z.sort()

for a, b in z:
    if b > M:
        total += M * a
        break
    else:
        total += b * a
        M -= b

print(total)

Solution (Python):
n, m = map(int, input().split())
shops = []
money = 0
for i in range(n):
  shops.append(list(map(int, input().split())))
shops.sort(key=lambda x: x[0])
for shop in shops:
  if shop[1] <= m:
    money += shop[0] * shop[1]
    m -= shop[1]
  elif shop[1] > m:
    money += shop[0] * m
    break
print(money)

Solution (Python):
n, m = map(int, input().split())
ab = []
prod = [0]
prod_cum = [0]

num_cum = [0]
for i in range(n):
    a, b = map(int, input().split())
    ab.append([a,b])



ab.sort()

for i in range(n):
    a, b = ab[i][0],ab[i][1]
    
    prod_cum.append(a*b + prod_cum[-1])
    num_cum.append(b + num_cum[-1])
ab = [[0,0]] + ab









ans = 0
prod = 0
for i in range(0, n+1):
    if num_cum[i] >= m:
        print(prod_cum[i] - ab[i][0] * (num_cum[i]-m))
        break

Solution (Python):
n,m = map(int,input().split())
ls = [0]*n

for i in range(n) :
    a,b = map(int,input().split())
    ls[i] = [a,b]
    
ls.sort(key=lambda x:x[0])

c = m + 0
ans = 0

while c > 0 :
    if c >= ls[0][1] :
        ans += ls[0][0] * ls[0][1]
        c -= ls[0][1]
        ls.pop(0)
    else :
        ans += ls[0][0] * c
        c = 0

print(ans)

Solution (Python):
N,M = list(map(int,input().split()))
A = []
for i in range(N):
    A.append(tuple(map(int,input().split())))
f,s = 0,0
for i,j in sorted(A):
    f += j
    s += j * i
    
    if(f >= M):
        print( s - i * (f-M) )
        break

Solution (Python):
import sys

if __name__ == "__main__":
    para_list = list(map(int, input().split()))
    N = para_list[0]
    M = para_list[1]

    A = [[0]*2]*N
    for i in range(N):
        A[i] = list(map(int, input().split()))
    B = sorted(A,key=lambda l:l[0])

    price = 0
    k = 0
    while(M>0):
        if M<B[k][1]:
            price +=B[k][0]*M
        else:
            price +=B[k][0]*B[k][1]
        M -= B[k][1]
        k += 1
    print(price)

Solution (Python):
N,M=[int(s) for s in input().split()]
AB=[[int(s) for s in input().split()] for i in range(N)]
AB.sort(key=lambda it: it[0])

pay=0
num=0
for a,b in AB:
    m=min(M-num, b)
    num=num+m
    pay=pay+a*m
    if num >= M:
        print(pay)
        break

Solution (Python):
N,M= map(int,input().split())
dptaples = []
for i in range(N):
    An,Bn = map(int,input().split())
    dptaples.append((An,Bn))
dptaples.sort()
maxnum = 0
maxvalue = 0
for key,value in dptaples:
    if M>value+maxnum:
        maxvalue+=key*value
        maxnum += value
    else:
        maxvalue+=((M-maxnum)*key)
        break
print(maxvalue)

Solution (Python):
from collections import OrderedDict
if __name__ == "__main__":
    NM = list(map(int, input().split()))
    n = NM[0]
    m = NM[1]
    mydict = {}
    for i in range(n):
        money_can = list(map(int, input().split()))
        money = money_can[0]
        can = money_can[1]
        if(money not in mydict.keys()):
            mydict[money] = []
        mydict[money].append(can)
    buynow = 0
    moneynow = 0

    mydict = OrderedDict(sorted(mydict.items()))
    for i in mydict.keys():
        if(buynow >= m):
            break
        for canNO in mydict[i]:
            if(buynow >= m):
                break
            if(canNO + buynow < m):
                moneynow += canNO*i
                buynow += canNO
            else:
                need = m - buynow
                moneynow += need*i
                buynow += need
        else:
            continue
    print(moneynow)

Solution (Python):
N, M = map(int, input().split())
A = {}
B = {}

for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

C = [(k, A[k]) for k in sorted(A, key=A.get)]

total = 0

for key, value in C:
    if (M < 0):
        break
    if (M > B[key]):
        total = total + A[key]*B[key]
    else:
        total = total + A[key]*M
    M -= B[key]
    

print(total)

Solution (Python):
N, M = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(N)]

rm = list(sorted(L))

n = 0
sum = 0
for i in range(M):
  if rm[n][1] == 0:
    n += 1
  rm[n][1] -= 1
  sum += rm[n][0]
  
print(sum)

Solution (Python):
N, M = map(long, raw_input().split())
A = list()
for i in xrange(N):
    a, b = map(long, raw_input().split())
    A.append((a,b))

A.sort()

count = 0L
cost = 0L

for (a, b) in A:
    if count + b >= M:
        cost += a * (M - count) 
        print str(cost)
        break
    else:
        count += b
        cost += a * b

Solution (Python):
import numpy as np
n, m = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    a_next, b_next = map(int, input().split())
    a.append(a_next)
    b.append(b_next)

a_sorted_index = np.argsort(a)

for i in range(1000000):
    min_index = a_sorted_index[i]
    if m - b[min_index] <=0:
        break
    m = m - b[min_index]
    ans += a[min_index]*b[min_index]

ans += m * a[min_index]
print(ans)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]), int(input_raw[1])]
    D = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [A, B] = [int(input_raw[0]), int(input_raw[1])]
        D.append((A, B))
    D.sort()

    cost = 0L
    for i in range(N):
        if M <= 0:
            break
        M -= D[i][1]
        buy = D[i][1]
        if M < 0:
            buy += M

        cost += long(buy) * D[i][0]
    print cost

if __name__ == '__main__':
    main()

Solution (Python):
N, M = map(int, input().split())
AB = {}
for n in range(N):
    a, b = list(int(x) for x in input().split())
    if a in AB:
        AB[a] += b
    else:
        AB[a] = b
yen = 0
AB = sorted(AB.items())
for a, b in AB:
    if b <= M:
        yen += a*b 
        M -= b
    else:
        yen += a*M
        M = 0
print(yen)

Solution (Python):
n, m = map(int, input().split())
money = []
for i in range(n):
  a, b = map(int, input().split())
  money.append({"a":a, "b":b})

bsum = 0
money.sort(key=lambda x: x["a"])
for i in range(n):
  bsum0 = bsum
  bsum += money[i]["b"]
  if m <= bsum:
    num = i
    break

ans = 0
for j in range(num):
  ans += money[j]["a"] * money[j]["b"]
ans += (m-bsum0) * money[num]["a"]
print(ans)

Solution (Python):
N, M = map(int, input().split())
C = [list(map(int, input().split())) for i in range(N)]

D = [C[i][0] for i in range(N)]

CNT_A = 0
CNT_B = 0
SUM = 0

C.sort(key=lambda x:x[0])

for i in range(N):
    if C[i][1] < M - CNT_B:
        SUM = SUM + C[i][0] * C[i][1]
        CNT_B = CNT_B + C[i][1]
    else:
        SUM = SUM + C[i][0] * max(0, M - CNT_B)
        CNT_B = CNT_B +  max(0, M - CNT_B)

print(SUM)

Solution (Python):
n, m = map(int,input().split())
total = 0
shops = [list(map(int,input().split())) for _ in range(n)]

shops.sort(key=lambda x:x[0])

for s in shops:
    if s[1] < m:
        m -= s[1]
        total += s[0]*s[1]
    else:
        total += s[0]*m
        print(total)
        exit()

Solution (Python):


def io_generator():
	return input()



def main(io):
	n,m= map(int, io().split())
	bb=[]
	for _ in range(n):
		(a,b)= map(int, io().split())
		bb.append((a,b))
	
	bb.sort(key=lambda a: a[0])
	zm=m
	cost=0
	for a,b in bb:
		if zm >b:
			zm=zm-b
			cost+=a*b
		else:
			cost+=zm*a
			break
		
	return cost



if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Solution (Python):
import numpy as np

N, M = map(int, input().split())
arr = []
for i in range(N):
    a, b = map(int, input().split())
    arr.append([a,b])

vec = np.array(arr)
sort = vec[np.argsort(vec[:, 0])]

amnt = 0
num = 0
for i in range(len(sort)):
    if num + sort[i][1] >= M:
        x = M - num
        amnt += sort[i][0] * x
        break
    else:
        amnt += sort[i][0] * sort[i][1]
        num += sort[i][1]
print(amnt)

Solution (Python):

import sys

def main():
	N, M = map(int, input().split(" "))
	
	store = []
	for i in range(N):
		tmp = input().split(" ")
		store.append([int(s) for s in tmp])
	
	store.sort()
	
	ans = 0
	for i, val in enumerate(store):
		if val[1] >= M:
			ans = ans + (val[0] * M)
			M = 0
		if val[1] < M:
			ans = ans + (val[0] * val[1])
			M = M - val[1]
		if M <= 0 :
			break
	
	print(ans)
			
if __name__ == "__main__":
	main()

Solution (Python):
from sys import stdin

N,M = map(int, input().split(' '))
shop = [stdin.readline().rstrip().split(' ') for i in range(N)]

for i in range(N):
  shop[i][0] = int(shop[i][0])
shop = sorted(shop)
  
sum = 0
for i in range(N) :
  if M > int(shop[i][1]) :
    sum += int(shop[i][0]) * int(shop[i][1])
    M -= int(shop[i][1])
  else :
    sum += int(shop[i][0]) * M
    M = 0
    break
    
print(sum)

Solution (Python):
N, M = map(int, input().split())
d = [list(map(int, input().split())) for i in range(N)]
d.sort(key=lambda x:(x[0],x[1]))

c = p = i = 0

while c < M:
    if c + d[i][1] <= M:
        c += d[i][1]
        p += d[i][0] * d[i][1]
        i += 1
    else:
        p += d[i][0] * (M - c)
        break

print(p)

Solution (Python):
n,m = map(int,input().split())
a = []
b = []
raw = []
ans = 0
for i in range(n):
  raw.append(list(map(int,input().split())))
raw.sort(key=lambda x:x[0])
for i in range(n):
  ans = ans + raw[i][0] * min(raw[i][1],m)
  m = m - min(raw[i][1],m)
  if m == 0:
    break
print(ans)

Solution (Python):
N,M = map(int, input().split())
a = []
for i in range(N):
    a.append([int(j) for j in input().split()])
a = sorted(a)

num = 0
val = 0
for i in range(N):
    num += a[i][1]
    if num >= M:
        val += a[i][0] * (M - num + a[i][1])
        break
    else:
        val += a[i][0] * a[i][1]
print(val)

Solution (Python):
N,M = list(map(int,input().split()))
C = []
for i in range(N):
    C.append(list(map(int,input().split())))
    
C = sorted(C,key=lambda x: x[0])

count = 0
ans = 0
for i in range(N):
    if count + C[i][1] > M:
        ans+=(M-count)*C[i][0]
        break
    else:
        ans+=C[i][0]*C[i][1]
        count+=C[i][1]
print(ans)

Solution (Python):


import sys

def parse_input(lines_as_string = None):

    lines = []
    if lines_as_string is None:
        lines.append(input())
        tokens = lines[0].split(" ")
        n = int(tokens[0])
        m = int(tokens[1])
        for i in range(n):
            lines.append(input())
    else:
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        print("lines=%s" % lines)
        tokens = lines[0].split(" ")
        n = int(tokens[0])
        m = int(tokens[1])

    a = []
    b = []
    for i in range(n):
        tokens = lines[i+1].split(" ")
        a.append(int(tokens[0]))
        b.append(int(tokens[1]))

    return (n, m, a, b)

def solve(n, m, a, b):

    
    s_a = sorted([(i, e) for i, e in enumerate(a)], key=lambda e: e[1])


    current = m
    total = 0
    tmp = 0
    for i, e in s_a:
        if current > b[i]:
            tmp = e * b[i]
            current = current - b[i]
        else:
            tmp = e * current
            current = 0

        total = total + tmp


        if current == 0:
            break

    return total

def main():

    print("%d" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Solution (Python):
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N)]
KOSU=0
money=0
flag=False
AB.sort()
for i in range(N):
  KOSU+=AB[i][1]
  money+=AB[i][0]*AB[i][1]
  if KOSU>=M:
    KOSU-=AB[i][1]
    money-=AB[i][0]*AB[i][1]
    money+=(M-KOSU)*AB[i][0]
    break
print(money)

Solution (Python):
def unzip(li):
  return map(list, zip(*li))

N, M = map(int, input().split())
tmp_C = [0 for i in range(N)]

for i in range(N):
  a,b = map(int, input().split())
  tmp_C[i] = (a,b)
  
C = sorted(tmp_C)
A, B = unzip(C)
ans = 0
count = 0
cur = 0
while(count < M):
  min_A = A[cur]
  B_min = B[cur]
  if count + B_min >= M:
    print(ans + min_A * (M - count))
    break
  else:
    ans += min_A * B_min
    count += B_min
    cur += 1

Solution (Python):
n, m = map(int, input().split())
drinks = dict()
for _ in range(n):
    (a, b) = map(int, input().split())
    if not a in drinks:
        drinks[a] = 0
    drinks[a] += b
p = 0
keys = sorted(drinks.keys())
for k in keys:
    if drinks[k] < m:
        p += k * drinks[k]
        m -= drinks[k]
    else:
        p += k * m
        break
print(p)

Solution (Python):
import numpy as np

N,M = map(int, input().split())
C = np.array([list(map(int, input().split())) for i in range(N)])
A = C.T[0]
B = C.T[1]

B = B[np.argsort(A)]
A = np.sort(A)

s = 0
m = M
i = 0
while(m > 0):
    if B[i] <= m:
        s += A[i] * B[i]
        m -= B[i]
        i += 1
    else:
        s += A[i] * m
        m = 0
print(s)

Solution (Python):
N, M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(N)]
L.sort()

A = []
B = []
for i in range(N):
	A.append(L[i][0])
	B.append(L[i][1])
num = 0
cost = 0
i = 0
while num < M:
	cost = cost + A[i]*min(B[i], M - num)
	num = num + min(B[i], M - num)
	i = i + 1

print(cost)

Solution (Python):
import numpy as np

N, M = map(int, input().split())
A_B = [list(map(int, input().split())) for i in range(N)]
A_B = sorted(A_B)

yen = 0
cnt = 0
i = 0

while M - (cnt + A_B[i][1]) > 0:
    yen += (A_B[i][0] * A_B[i][1])
    cnt += A_B[i][1]
    i += 1

yen += (M - (cnt)) * A_B[i][0]

print(yen)

Solution (Python):
def main():
    N, M = map(int, input().split())
    shop_drink = [[int(i) for i in input().split()] for n in range(N)]
    
    
    shop_drink.sort()
    buy_drinks = 0
    buy_amount = 0

    for sd in shop_drink:
        
        if buy_drinks + sd[1] < M:
            
            buy_drinks += sd[1]
            
            buy_amount += sd[0] * sd[1]
        
        else:
            
            temp = M - buy_drinks
            buy_amount += sd[0] * temp
            print(buy_amount)
            return


if __name__ == '__main__':
    main()

Solution (Python):
ans = 0
x = input().split()
x = list(map(int, x))
a = [0] * (x[0])
for n in range(x[0]):
	a[n] = input().split()
	a[n] = list(map(int, a[n]))
a.sort()
counter = 0
while x[1] > 0:
	ans += a[counter][0]
	a[counter][1] -= 1
	x[1] -= 1
	if a[counter][1] == 0:
		counter += 1
print(ans)

Solution (Python):
n,m=map(int,input().split())
ab=[list(map(int,input().split()))for i in range(n)]
ab=sorted(ab,key=lambda x:x[0])
cnt=0
cost=0
for i in range(n):
  if (cnt+ab[i][1])>=m:
    cost+=ab[i][0]*(m-cnt)
    break
  cost+=ab[i][0]*ab[i][1]
  cnt+=ab[i][1]
print(cost)

Solution (Python):
n,m=map(int,input().split())
a = []
b = []
ans = 0
for i in range(n):
    tempa,tempb = map(int,input().split())
    a.append(tempa)
    b.append(tempb)
test = list(zip(a,b))
test = sorted(test)
i=0
while m>0:
    if m-test[i][1] >=0:
        ans += test[i][0]*test[i][1]
        m -=test[i][1]
    else:
        ans += test[i][0]*m
        m=0
        print(ans)
        exit()
    i+=1
print(ans)

Solution (Python):
N,M =map(int,input().split())
List=[[int(j) for j in input().split()] for i in range(N)]
List = sorted(List)

M_res = 0
M_res +=M

ANS = 0

while M_res > 0:
    for i in range(len(List)):
        if M_res <= List[i][1]:
            ANS +=  List[i][0] * M_res
            M_res -=M_res
        else:
            ANS += List[i][0] * List[i][1]
            M_res -=List[i][1]
        if M_res ==0:
            break
print(ANS)

Solution (Python):
N, M = map(int, input().split())
l = []

for i in range(N):
    ab = list(map(int, input().split()))
    l.append(ab)

l.sort(key=lambda x:x[0])

pay = 0
hold = 0
for i in range(N):
    quantity = l[i][1]
    price = l[i][0]
    require = M - hold
    
    if require >= quantity :
        hold += quantity
        pay += price * quantity
    elif require < quantity :
        hold += require
        pay += price * require
            
    if hold == M:
        break

print(pay)