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):
N, M = map(int, input().split())
data = []

for i in range(N):
  tmp_a, tmp_b = map(int, input().split())
  data.append((tmp_a, tmp_b))

sorted_data = sorted(data, key=lambda x: x[0])
  
buy_count = 0
money_count = 0

while buy_count < M:
  tmp_a = sorted_data[0][0]
  tmp_b = sorted_data[0][1]

  if buy_count + tmp_b < M:
    buy_count += tmp_b
    money_count += tmp_b * tmp_a
    sorted_data.pop(0)
  else:
    while buy_count < M:
      buy_count += 1
      money_count += tmp_a
      
print(money_count)

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

ABs = []
for _ in range(N):
    A, B = map(int, input().split())
    ABs.append((A, B))

ABs = sorted(ABs, key=lambda t: t[0])

cost = 0
count_drink = 0
for A, B in ABs:
    if count_drink + B >= M:
        cost += A * (M - count_drink)
        break

    cost += A * B
    count_drink += B
print(cost)

Solution (Python):
N, M = map(int, input().split())
stores = []
for _ in range(N):
  stores.append(tuple(map(int, input().split())))
res = 0
for store in sorted(stores):
  if M <= 0:
    break
  num = min(M, store[1])
  res += num * store[0]
  M -= num
print(res)

Solution (Python):
import sys
import numpy as np

n, m = map(float, input().split())
lines = sys.stdin.readlines()
lines = [list(map(float, line.strip().split())) for line in lines]
lines.sort()
price = 0
for line in lines:
    if m - line[1] > 0:
        price += line[0] * line[1]
        m -= line[1]
    else:
        price += line[0] * m
        break
print(int(price))

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

price = 0
L = sorted(L, key=lambda x:x[0])
for l in L:
    if M <= l[1]:
        price += l[0] * M
        break
    else:
        price += l[0] * l[1]
        M -= l[1]
        
print(price)

Solution (Python):
from sys import stdin


N, M = [int(x) for x in stdin.readline().rstrip().split()]


AiBi_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N)]


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

bought_count = 0
fee = 0

for Ai, Bi in AiBi_list:
    want_to_buy_count = M - (bought_count + Bi)
    if want_to_buy_count < 0:
        fee += Ai * (M - bought_count)
        break
    else:
        fee += Ai * Bi
        bought_count += Bi

print(fee)

Solution (Python):


















import math
import numpy as np
import copy
from collections import defaultdict, Counter
from itertools import product
from bisect import bisect_left, bisect_right

def s_inpl(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))

N, M = s_inpl()
A, B = [], []
for _ in range(N):
    a, b = s_inpl()
    A.append(a)
    B.append(b)

A_sind = np.argsort(A)
ans = 0
cnt = 0
for i in A_sind:
    cnt += B[i]
    ans += A[i] * B[i]

    if cnt >= M:
        ans -= (cnt - M) * A[i]
        break

print(ans)

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

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

can = 0 
yen = 0 


for ab in AB:
  if can + ab[1] <= M: 
    yen += ab[0] * ab[1]
    can += ab[1]
  elif can < M < can + ab[1]:
    yen += (M-can) * ab[0]
    can += ab[1]
    break

print(yen)

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

AB.sort()
sum = 0

for ab in AB:
    if M <= ab[1]:
        sum += ab[0] * M
        print(sum)
        exit()
    else:
        sum += ab[0] * ab[1]
        M -= ab[1]

Solution (Python):


num=input().split()

N = int(num[0])
M = int(num[1])

AB = []

for a in range(1,N+1):
    date = []
    date2 = input().split()

    date.append(int(date2[0]))
    date.append(int(date2[1]))

    AB.append(date)

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

total_num = 0
price = 0


for i in range(1,N+1):
    if total_num < M:
        total_num += AB[i-1][1]
        price += AB[i-1][0] * AB[i-1][1]
        j = i

defference = total_num - M

price -= AB[j-1][0] * defference

print(price)

Solution (Python):
class Shop:
    price = 0
    number = 0

    def __init__(self,price,number):
        self.price = price
        self.number = number


n,m = [int(i) for i in input().split()]

shopList = []

for i in range(n) :
    a,b = [int(j) for j in input().split()]
    shopList.append(Shop(a,b))

shopList.sort(key = lambda x: x.price)

cost = 0
total = 0

for i in shopList:
    
    if(total + i.number < m):
        cost += i.price * i.number
        total += i.number
    else:
        cost += i.price * (m - total)
        break;

print(cost)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N,M = inpl()
A = []
for i in range(N):
  A.append(inpl()) 
A.sort(key=lambda x: x[0])
ans = 0
for i in range(N):
  if A[i][1] >= M:
    ans += A[i][0]*M
    break
  else:
    M -= A[i][1]
    ans += A[i][0]*A[i][1]
print(ans)

Solution (Python):
import numpy as np

def main():
    N, M = map(int, input().split())

    AB= np.array([list(map(int, list(input().split()))) for _ in range(N)])
    
    
    AB_sort_by_A = np.sort(AB.view('i8,i8'), order=['f0'], axis=0).view(np.int)

    
    buyNum = 0
    money = 0
    for i in range(N):
        if buyNum >= M:
            break
        else:
            drinkVal, drinkNum = AB_sort_by_A[i]
            if M - buyNum > drinkNum:
                buyNum += drinkNum
                money += drinkVal * drinkNum
            else:
                money += drinkVal * (M-buyNum)
                break
    print(money)
    
    
if __name__ == "__main__":
    main()

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

A.sort(key=lambda x: x[0])
Y = 0
for i in range(N):
    if A[i][1] >= M:
        Y += A[i][0]*M
        break
    else:
        Y += A[i][0]*A[i][1]
        M -= A[i][1]

print(Y)

Solution (Python):
from operator import itemgetter

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

arr_sort = sorted(arr, key=itemgetter(0))

money = 0
for A,B in arr_sort:
    M -= B
    money += A * B
    if M <= 0:
        print(money + M * A)
        exit()

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

Solution (Python):

import numpy as np
N, M = map(int, input().split())
AB = np.zeros((N, 2))
for i in range(N):
    AB[i, :] = np.array(list(map(int, input().split())))

AB_sorted = AB[np.argsort(AB[:, 0]), :]

total_drink = 0
total_cash  = 0
for i in range(N):
    if(total_drink + AB_sorted[i, 1] >= M):
        total_cash += (M-total_drink) * AB_sorted[i, 0]
        break
    else:
        total_cash += AB_sorted[i, 1] * AB_sorted[i, 0]
        total_drink += AB_sorted[i, 1]

print(int(total_cash))

Solution (Python):
import pdb

def main():
    param = list(map(int, input().split()))

    n = param[0]
    m = param[1]

    data = []
    for i in range(n):
        param = list(map(int, input().split()))
        data.append(param)

    data.sort(key=lambda data:data[0])

    ans = 0
    for i in range(n):
        if m <= data[i][1]:
            ans += m * data[i][0]
            break
        else:
            ans += data[i][1] * data[i][0]
            m -= data[i][1]

    print(ans)
    
    return

        
if __name__ == '__main__':
    main()

Solution (Python):
input_line = input()
info = input_line.split()
N = int(info[0])
M = int(info[1])

A_B = []
for i in range(N):
    input_line = input()
    temp = input_line.split()
    a_b = [int(x) for x in temp]
    A_B.append(a_b)

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

bought = [0, 0] 
p = 0
while (bought[1]<M):
    bought[0] += A_B[p][0]*A_B[p][1]
    bought[1] += A_B[p][1]
    if bought[1]>M:
        diff = bought[1]-M
        bought[0] -= A_B[p][0]*diff
        bought[1] -= diff
    p += 1
print(bought[0])

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

for i in range(N):
    a, b = (int(x) for x in input().split())
    AB.append((a,b))

AB = sorted(AB, key=lambda x:x[0], reverse=False)
sum_money = 0

for i in AB:
    if M <= i[1]:
        sum_money += M * i[0]
        break
    else:
        sum_money += i[1] * i[0]
        M -= i[1]
        
print(sum_money)

Solution (Python):
lx = list(map(int,input().split(" ")))
la = []
cnt = 0

for i in range(0, lx[0]):
    la.append(list(map(int,input().split(" "))))

la = sorted(la)

coin = 0
bottle = lx[1]
i = -1
while (bottle > 0 and i < lx[0]-1):
    i += 1
    while (bottle > 0 and la[i][1] > 0):
        coin += la[i][0]
        bottle -= 1
        la[i][1] -= 1

print(coin)

Solution (Python):
input_line = input()
info = input_line.split()
N = int(info[0])
M = int(info[1])

A_B = []
for i in range(N):
    input_line = input()
    temp = input_line.split()
    a_b = [int(x) for x in temp]
    A_B.append(a_b)

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

bought = [0, 0] 
p = 0
while (bought[1]<M):
    bought[0] += A_B[p][0]*A_B[p][1]
    bought[1] += A_B[p][1]
    if bought[1]>M:
        diff = bought[1]-M
        bought[0] -= A_B[p][0]*diff
        bought[1] -= diff
    p += 1
print(bought[0])

Solution (Python):
N,M = map(int,input().split())
drinkList = []
for _ in range(N):
    a,b = map(int,input().split())
    drinkList.append((a,b))
drinkList.sort()
neededMoney = 0
for price,num in drinkList:
    buy = min(M,num)
    neededMoney += buy*price
    M -= buy
    if M==0: break
print(neededMoney)

Solution (Python):
N, M = map(int, input().split())
energy = [[int(_) for _ in input().split()] for _ in range(N)]
energy.sort(key=lambda x: x[0])
Res = 0

for i in energy:
    if M - i[1] > 0:
        M -= i[1]
        Res += i[0] * i[1]
    elif M == i[1]:
        Res += i[0] * i[1]
        break
    else:
        Res += i[0] * M
        break

print(Res)

Solution (Python):

N,M = list(map(int,input().split()))

l = [list(map(int,input().split())) for _ in range(N)]
l = sorted(l, key=lambda x:x[0])
a_cnt = 0
b_cnt = 0
for a,b in l:
    if b <= M:
        b_cnt += b
        M -= b
        a_cnt += a*b
    elif b > M and M > 0:
        b_cnt += M
        a_cnt += a*M
        break
    else:
        break
print(a_cnt)

Solution (Python):
N,M = map(int,input().split())
sets = []
for _ in range(N):
    sets.append(list(map(int,input().split())))
    
def mergesort(sets):
    if len(sets) == 1:
        return [sets[0]]
    else:
        leng = len(sets)
        sets1 = mergesort(sets[:leng//2])
        sets2 = mergesort(sets[leng//2:])
        return merge(sets1,sets2)

def merge(set1,set2):
    i=0
    j=0
    result = []
    for _ in range(len(set1)+len(set2)):
        if i<len(set1) and j<len(set2):
            if set1[i][0] >= set2[j][0]:
                result.append(set2[j])
                j += 1
            elif set2[j][0] > set1[i][0]:
                result.append(set1[i])
                i += 1
        elif i == len(set1):
            result.append(set2[j])
            j += 1
        elif j == len(set2):
            result.append(set1[i])
            i += 1
    return result

def minimummoney(m,sets):
    sets = mergesort(sets)
    rest = m
    cost = 0
    for i in range(len(sets)):
        if rest > sets[i][1]:
            cost += sets[i][0]*sets[i][1]
            rest -= sets[i][1]
            continue
        if 0 < rest and rest <= sets[i][1]:
            cost += rest * sets[i][0]
        break
    return cost

print(minimummoney(M,sets))

Solution (Python):

import sys


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    pairs = []
    for i in range(N):
        pairs.append((A[i], B[i]))
    pairs.sort(key=lambda x: x[0])
    ret = 0
    for p, num in pairs:
        if M <= num:
            ret += p * M
            break
        else:
            ret += p * num
            M -= num
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    A = [int()] * (N)  
    B = [int()] * (N)  
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)

if __name__ == '__main__':
    main()

Solution (Python):

n,m = map(int, input().split())
ab = [[int(i) for i in input().split()] for i in range(n)]

ab.sort(key=lambda x:x[0])
cost = 0
for i in range(n):
    value = ab[i][0]
    num = ab[i][1]
    if num> m:
        cost += value*m
        break
    else:
        cost += value*num
        m -= num
print(cost)

Solution (Python):

n, m = map(int, input().split())
ab = [[int(i) for i in input().split()] for i in range(n)]

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

cost = 0
for i in range(n):
    value = ab[i][0]
    num = ab[i][1]
    if num > m:
        cost += value * m
        break
    else:
        cost += value * num
        m -= num
print(cost)

Solution (Python):
N,M = map(int,input().split())
info = [list(map(int,input().split())) for _ in range(N)]
info.sort()
money = 0
need = M
for a,b in info:
    if need <= b:
        money += a*need
        need = 0
        break
    else:
        money += a*b
        need -= b
print(money)

Solution (Python):
import heapq

def energy_drink_collector(M, heap):
    left = M
    ret = 0

    while left:
        ele = heapq.heappop(heap)
        deduct = min(left, ele[1])
        ret += ele[0] * deduct
        left -= deduct

    return ret


if __name__ == "__main__":
    N, M = map(int, input().split())

    heap = []
    for _ in range(N):
        a, b = map(int, input().split())
        heapq.heappush(heap, (a, b))

    result = energy_drink_collector(M, heap)
    print(result)

Solution (Python):
N,M=map(int,input().split())
shop_list = [0]*N
cost = 0

for i in range(N):
  shop_list[i]=list(map(int,input().split()))
  
shop_list.sort(key= lambda x: x[0])
for A,B in shop_list:
  if B < M:
    M -= B
    cost += A*B
  elif B >= M:
    cost += A*M
    M = 0
  if M==0:
    break

print(cost)

Solution (Python):
import sys

n, m = map(int, sys.stdin.readline().split())
matlist = []
for _ in range(n):
    matlist.append(list([int(i) for i in sys.stdin.readline().split()]))
matlist = sorted(matlist, key=lambda x: x[0])

ans = 0
for c, v in matlist:
    if v >= m:
        ans += c * m
        break
    else:
        ans += c * v
        m -= v

print(ans)

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


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


list.sort(AB,key=lambda x:x[0])


m=0 
cost=0 
for i in range(N):
 
 if M-m==AB[i][1]:
     m+=AB[i][1]
     cost+=AB[i][0]*AB[i][1]
     break
 elif M-m<AB[i][1] :
      for j in range(AB[i][1]+1):
         cost=cost+AB[i][0]*j
         m=m+j
         if m==M:
             break
         elif m<M:
             cost=cost-AB[i][0]*j
             m=m-j
 else:
      cost+=AB[i][0]*AB[i][1]
      m+=AB[i][1]
    
print(cost)

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

ABNs = sorted(ABNs)

r = M
newABNs = []
for i in range(len(ABNs)):
	if ABNs[i][1] <= r:
		r -= ABNs[i][1]
		newABNs.append(ABNs[i])
	else:
		newABNs.append([ABNs[i][0],r])
		break
		


ans = 0
for i in range(len(newABNs)):
	ans += (newABNs[i][0]*newABNs[i][1])
	
print(ans)

Solution (Python):
N, M = [int(x) for x in input().split()]
T = [tuple(int(x) for x in input().split()) for i in range(N)]

res = 0
for value, num in sorted(T):
  if M >= num:
    res += value*num
    M -= num
  else:
    res += value * M
    break

print(res)

Solution (Python):
n, m =map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab = sorted(ab, key=lambda x:x[0])

cnt = m
cost = 0
for a, b in ab:
    buy = min(cnt, b)
    cnt -= buy
    cost += a*buy
    
print(cost)

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

cur = 0
cost = 0
for i in drinks_s :
    if cur + i[1] <= M :
        cur += i[1]
        cost += i[0] * i[1]
    else :
        cost += (M - cur) * i[0]
        break
        
print(cost)

Solution (Python):
n, m = map(int, input().split())
t = [list(map(int, input().split())) for i in range(n)]

t = sorted(t, key=lambda v: v[0])

cost = 0
for i in range(n):
    if m-t[i][1] <= 0:
        cost += t[i][0]*m
        break
    cost += t[i][0]*t[i][1]
    m -= t[i][1]
print(cost)

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

A=[]
B=[]

bought = 0
price = 0
for i in range(0, N):
    a,b=list(map(int, input().split(" ")))
    A.append((a, i))
    B.append(b)

A.sort()

i = 0
p=0
n=0
while bought < M:
    p,n = A[i]
    price += p*B[n]
    bought += B[n]
    i += 1

if bought > M:
    price -= (bought-M)*p

print(price)

Solution (Python):



def solve(n, m, lst):


    lst.sort()

    ans = 0
    r = m
    for v in lst:
        a, b = v
        if r <= b:
            ans += a * r
            break
        r -= b
        ans += a * b

    return ans

def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    lst = []
    for _ in range(n):
        a, b = input().split()
        a = int(a)
        b = int(b)
        lst.append((a, b))


    print(solve(n, m, lst))


if __name__ == '__main__':
    main()

Solution (Python):
N,M = [int(s) for s in input().split()]
ABs = [[int(s) for s in input().split()] for i in range(N)]
n = M
p = 0

for A, B in sorted(ABs, key=lambda AB: AB[0]):
    nbuy = B if n-B >= 0 else n
    p += A*nbuy
    n -= nbuy
    if n <= 0:
        break
print(p)

Solution (Python):
from operator import itemgetter
n,m=map(int,input().split())
a=[]
for i in range (n):
    x,y=map(int,input().split())
    a.append([y,x])

a=sorted(a,key=itemgetter(1))

co=0
for i in range (n):
    if(m>=a[i][0]):
        co+=(a[i][0]*a[i][1])
        m-=a[i][0]

    else:
        co+=(m*a[i][1])
        m=0
print(co)

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

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

ans = 0
n_drinks = 0
for i in range(n):
    a, b = ab[i][0], ab[i][1]
    if n_drinks + b < m:
        ans += a * b
        n_drinks += b
    else:
        ans += (m - n_drinks) * a
        break

print(ans)

Solution (Python):
n, m = map(int, input().split())
a_li = []
b_li = []
cnt = 0

for i in range(n):
	a, b = map(int, input().split())
	a_li.append(a)
	b_li.append(b)

merge = zip(a_li, b_li)
merge_new = sorted(merge)

x = []
y = []

for a, b in merge_new:
	x.append(a)
	y.append(b)

for a, b in zip(x, y):
	if m > 0 and b <= m:
		cnt += a * b
		m -= b
		if m == 0:
			break
	elif m > 0 and b > m:
		cnt += a * m
		break

print(cnt)

Solution (Python):
n, m = map(int, input().split())
a = []
for i in range(n):
    i,j = (map(int, input().split()))
    a += [[i,j]]
a = sorted(a)
count = 0
value = 0
for (i,j) in a:
    if count + j >= m:
        value += (m - count) * i
        break
    else:
        value += i * j
        count += j
print(value)

Solution (Python):
n, m = map(int, input().split())
index=0
listD = []
while index < n:
    a, b = map(int, input().split())
    listD.append([a, b])
    index+=1
listD.sort()
count=0
money=0
index2=0
while index2 < len(listD):
    if count+listD[index2][1] <= m:
        count+=listD[index2][1]
        money+=listD[index2][0]*listD[index2][1]
    else:
        money+=listD[index2][0]*(m-count)
        count=m
    if count==m:
        break
    index2+=1
print(money)

Solution (Python):
N, M = map(int, input().split())
list = [list(map(int, input().split())) for x in range(N)]
list.sort(key=lambda x: x[0])
remain = M
cost = 0
for i in range(N):
    store = list[i][1]
    if remain>=store:
        remain -= store
        cost += list[i][0]*store
    elif remain<store:
        cost += list[i][0]*(remain)
        remain = 0
    if remain==0: break
print(cost)

Solution (Python):

import numpy as np

n, m = map(int, raw_input().split())
a = np.zeros((n, 2), dtype=np.uint64)
for i in np.arange(n):
    a[i] = np.array(map(int, raw_input().split()))

a = a[np.argsort(a[:,0])]

cost = 0L
for i in np.arange(n):
    if(a[i][1] < m):
        cost += a[i][0] * a[i][1]
        m -= a[i][1]
    else:
        cost += a[i][0] * m
        print(long(cost))
        exit()

Solution (Python):
def main():
    N, M = map(int, input().split())
    lis = []
    for i in range(N):
        lis.append(list(map(int, input().split())))
    
    
    lis.sort() 
    sum = 0
    count = 0
    flg = False
    for i in range(N):
        if (count+lis[i][1]<M) :
            sum = sum + lis[i][0]*lis[i][1]
            count = count + lis[i][1] 
        elif (count+lis[i][1]==M):
            sum = sum + lis[i][0]*lis[i][1]
            count = count + lis[i][1] 
            break
        else:
            for j in range(lis[i][1]):
                sum = sum + lis[i][0]
                count = count + 1
                if count==M:
                    flg = True
                    break
        if flg:
            break

    print(sum)
    

main()

Solution (Python):
NM = [int(i) for i in input().split()]
N = NM[0]
M = NM[1]
datalist = []
index = 0
cost = 0
num = 1
total_cost = 0
while(N > 0):
    data = [int(i) for i in input().split()]
    datalist.append(data)
    N -= 1
datalist.sort(key=lambda x:x[0])
while(M > 0):
    M -= datalist[index][num]
    total_cost += datalist[index][cost] * datalist[index][num]
    if(M < 0):
        n = abs(M)
        total_cost -= n * datalist[index][cost]
    index += 1
print(total_cost)

Solution (Python):
N, M = map(int, input().split())
AB = []
p = 0
for n in range(N):
    AB.append(list(map(int, input().split())))
AB.sort()
for s, b in AB:
    p += s * min(M, b)
    M -= min(M, b)
print(p)

Solution (Python):
N, M = [int(i) for i in input().split()]
ABList = [[int(i) for i in input().split()] for _ in range(N)]
ABList.sort(key=lambda x: x[0], reverse=False)

ans = 0
for AB in ABList:
    drink = min(AB[1], M)
    M -= drink
    ans += drink * AB[0]
    if M == 0:
        break

print(ans)

Solution (Python):
N_M = [int(x.strip()) for x in input().split()]
price_l = [[int(i) for i in input().split()] for i in range(0, N_M[0])]
price_l.sort(key=lambda x: x[0])
total = 0
remain = N_M[1]
while remain > 0:
    if len(price_l)<1:
        break
    price_n = price_l.pop(0)
    total += price_n[0] * min(price_n[1], remain)
    remain -= min(price_n[1], remain)
print(total)

Solution (Python):

N, M = map(int, input().split())

shops = []

for i in range(N):
    n, m = map(int, input().split())
    shops.append((n, m))

sorted_shops = sorted(shops)

remain = M
value = 0
for i in range(N):
    if sorted_shops[i][1] <= remain:
        value += sorted_shops[i][0] * sorted_shops[i][1]
        remain -= sorted_shops[i][1]
    else:
        value += sorted_shops[i][0] * remain
        break
print (value)

Solution (Python):
NM=list(map(int,input().split()))
list1=[[0 for i in range(2)] for j in range(NM[0])]

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

list1.sort()
flag=NM[1]
i=0
count=0
while flag>0:
 if flag>list1[i][1]:
   count=count+list1[i][0]*list1[i][1]
   flag=flag-list1[i][1]
 else:
   count=count+list1[i][0]*flag
   flag=0
 i=i+1

print(count)

Solution (Python):
N, M = [int(i) for i in input().split()]
ABS = [[int(i) for i in input().split()] for _ in range(N)]

lim = 0
s = 0
for a, b in sorted(ABS, key=lambda ab: ab[0]):
  if lim + b >= M:
    print(s + (M - lim) * a)
    exit()

  lim += b
  s += a * b

Solution (Python):

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

AB.sort()

ans = 0
for i, j in AB:
	ans += i * min(M, j)
	M -= min(M, j)

print(ans)

Solution (Python):
import numpy as np


N, M = map(int, input().split())
A, B = list(), list()
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
A = np.array(A)
B = np.array(B)

n_bins = 0
cost = 0
indices = np.argsort(A)
for idx in indices:
    
    if n_bins + B[idx] > M:
        cost += A[idx] * (M - n_bins)
        break
    
    else:
        n_bins += B[idx]
        cost += A[idx] * B[idx]
print(cost)

Solution (Python):
x = input().split(" ")
store = int(x[0])
need = int(x[1])
ans = 0
price = {}

for i in range(store):
    y = input().split(" ")
    pri = int(y[0])
    bottles = int(y[1])
    if pri in price.keys():
        price[pri] += bottles
    else:
        price[pri] = bottles

price_sort = sorted(price.items(), key=lambda x: x[0]) 

for price_bottles in price_sort: 
    if need == price_bottles[1]:
        ans += price_bottles[1] * int(price_bottles[0])
        break
    elif need > price_bottles[1]:
        ans += price_bottles[1] * int(price_bottles[0])
        need -= price_bottles[1]
    else:
        ans += need * int(price_bottles[0])
        break


print(ans)

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

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

    ans = 0
    for k, v in sorted(A.items(), key=lambda x: x[1]):
        if M > B[k]:
            ans += v * B[k]
            M -= B[k]
            if M < 1:
                break
        else:
            ans += v * M
            break

    print(ans)

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

abn.sort(key=lambda x:x[0])
ans=0
for ab in abn:
    if M < 0:
        break
    else:
        if M>ab[1]:
            M-=ab[1]
            ans+=ab[0]*ab[1]
        else:
            ans+=ab[0]*M
            break
print(ans)

Solution (Python):
n,m = map(int,input().split())
x = [list(map(int,input().split())) for i in range(n)]
x = sorted(x,key=lambda x:x[0])
t = x[0][1]
res = 0
ans = 0
for i in range(n):
    res += x[i][1]
    ans += x[i][0]*x[i][1]
    if m-res<=0:
        print(ans-((res-m)*x[i][0]))
        exit()

Solution (Python):



n, m = map(int, input().split(" "))

l = []

for i in range(n):
    t1, t2 = map(int, input().split(" "))
    l.append((t1, t2))
l.sort(key=lambda x:x[0])
sum = 0
for i in l:
    if m < i[1]:
        sum += (i[0] * m)
        break
    sum += (i[0] * i[1])
    m -= i[1]

print(sum)

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


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

ans = 0
sum_drinks = 0 
for i in range(N):
    drinks_at_shop = PNs[i][1] 
    ans += drinks_at_shop*PNs[i][0]
    sum_drinks += drinks_at_shop
    
    over = sum_drinks-M
    if over>=0:
        ans -= (over)*PNs[i][0]
        break

print(ans)

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


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

ans = 0
sum_drinks = 0 
for i in range(N):
    nos = PNs[i][1] 
    ans += nos*PNs[i][0] 
    sum_drinks += nos
    if sum_drinks>=M:
        ans -= (sum_drinks-M)*PNs[i][0]
        break

print(ans)

Solution (Python):
def main():
    N, M = map(int, input().split())
    l = [[0, 0] for _ in range(N)]
    for i in range(N):
        l[i] = list(map(int, input().split()))

    l.sort()

    ans = 0
    for i, j in l:
        for _ in range(j):
            if M < 1:
                return ans
            else:
                M -= 1
                ans += i
    return ans


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

Solution (Python):
def main():
    N, M = map(int, input().split())
    l = [[0, 0] for _ in range(N)]

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

    l.sort()

    ans = 0

    for i, j in l:
        for _ in range(j):
            if M < 1:
                print(ans)
                return 0
            else:
                M -= 1
                ans += i

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N , M = map(int,input().split())
L = []
for _ in range(N):
    L.append(list(map(int,input().split())))
L = sorted(L)
quantity = 0
cost = 0
for x in L:
    if quantity + x[1] < M:
        cost += x[1] * x[0]
        quantity += x[1]
    else:
        cost += max(M - quantity,0) * x[0]
        break
print(cost)

Solution (Python):
N, M = (int(x) for x in raw_input().split())
AB = [[int(x) for x in raw_input().split()] for i in xrange(N)]
AB.sort()
result = 0
for ab in AB:
	a, b = ab
	result += min(b, M) * a
	M -= min(b, M)
print(result)

Solution (Python):
N, M = map(int, input().split())
L = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
c = 0
for a, b in L:
    if b <= M:
        c += a * b
        M -= b
        continue
    c += a * M
    break
print(c)

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

for i in range(N):
    c,s = map(int,input().split())
    store.append([c,s])

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

cost = 0
for line in store:
    if M <= line[1]:
        cost += M*line[0]
        break
    else:
        cost += line[0]*line[1]
        M -= line[1]

print(cost)

Solution (Python):
from functools import reduce

N, M = map(int, input().split())
A, B = zip(*(map(int, input().split()) for _ in range(N)))


ans = reduce(
    lambda acc, t: (
        acc[0] + t[0] * min(t[1], M - acc[1]),
        acc[1] + t[1]
    ) if acc[1] < M else
    acc,
    sorted(zip(A, B)),
    (0, 0)
)[0]


print(ans)

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

for i in range(N):
  I.append(list(map(int, input().split())))
  
I.sort()

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

Solution (Python):
my_M = 0
kingaku = 0
A = []
B = []
x = 0
y = 0

N,M = map(int,input().split())
for i in range(N):
    x,y = map(int,input().split())
    A.append(x)
    B.append(y)
C = zip(A,B)
CC = sorted(C)

for j in range(N):
    my_M = my_M + CC[j][1]
    if my_M < M:
        kingaku = kingaku + CC[j][0]*CC[j][1]
    elif my_M == M:
        kingaku = kingaku +CC[j][0]*CC[j][1]
        break
    elif my_M > M:
        kingaku = kingaku +CC[j][0]*(M-(my_M-CC[j][1]))
        break

print(kingaku)