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 sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, m = na()
ab = []
for _ in range(n):
    abi = na()
    ab.append(abi)

ab.sort()
ai, bi = ab[0][0], ab[0][1]
c = 0
ans = 0
while m > 0:
    bi -= 1
    ans += ai
    m -= 1
    if bi == 0 and m != 0:
        c += 1
        ai, bi = ab[c][0], ab[c][1]

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,M=map(int,input().split())
    l=[list(map(int,input().split())) for _ in range(N)]
    l.sort()
    c=0
    ans=0
    for i in range(N):
        if c>M:
            break
        ans+=min(M-c,l[i][1])*l[i][0]
        c+=l[i][1]
    print(ans)

if __name__ == '__main__':
    main()

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])


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

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N, M = [int(x) for x in input().split()]
    ab = []
    for _ in range(N):
        ab.append([int(x) for x in input().split()])
    ab.sort()

    ans = 0
    for a, b in ab:
        if b > M:
            ans += a * M
            break
        else:
            ans += a * b
            M -= b

    print(ans)


    


if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
n,m = map(int,input().split())
ablista = []
for y in range(n):
    ab = tuple(map(int,input().split()))
    ablista.append(ab)
ablista.sort()    
k = 0
c = 0
for x in ablista:
    if k+x[1] <m:
        c = c+ x[0]*x[1]
        k = k + x[1]
       
    else:
        c = c+(m-k)*x[0]
        
        break
print (c)

Solution (Python):
n,m=map(int, input().split())
li=[]
for i in range(n):
    a,b=map(int,input().split())
    li.append((a,b))
li.sort()
pp=0
cj=0
while m>0:
    cj+= min(li[pp][1],m)*li[pp][0]
    m-=min(li[pp][1],m)
    pp+=1
print(cj)

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

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

Solution (Python):
import sys

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

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

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

sum = 0
for i in range(len(L)):
    l = L[i]
    if l[1]<M:
        M = M - l[1]
        sum = sum + l[0]*l[1]
    else:
        sum = sum + l[0]*M
        print(sum)
        sys.exit()      

print(sum)

Solution (Python):
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N)]
B=sorted(A)
cnt=0
m=0
for i in range(N):
  if B[i][1]>=M-cnt:
    m+=(M-cnt)*B[i][0]
    print(m)
    exit()
  else:
    cnt+=B[i][1]
    m+=B[i][0]*B[i][1]

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 in range(n):
    if ab[i][1]<=m:
        ans+=ab[i][0]*ab[i][1]
        m-=ab[i][1]
    else:
        ans+=ab[i][0]*m
        break
print(ans)

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

ans=0
cnt=0
for i in range(N):
    cnt+=s[i][1]
    if M >= cnt:
        ans+=s[i][0]*s[i][1]
    elif M < cnt:
        cnt-=s[i][1]
        ans+=(M-cnt)*s[i][0]
        break
print(ans)

Solution (Python):


(shop_count, require) = [int(i) for i in input().rstrip().split(" ")]
shop_list = sorted([[int(i) for i in input().rstrip().split(" ")] for j in range(shop_count)])

ans = 0
count = 0
for i in shop_list:
    if count + i[1] >= require:
        ans += i[0] * (require - count)
        break
    else:
        ans += i[0] * i[1]
        count += i[1]

print(ans)

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

data = sorted(data, key=lambda d: d[0])

shop = 0
cost = 0
bought = 0

while bought < m:
    price = data[shop][0]
    stock = data[shop][1]

    if m - bought <= stock:
        buy = m - bought
    else:
        buy = stock

    cost += price * buy
    bought += buy
    shop += 1

print(cost)

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

count=0

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

print(count)

Solution (Python):
def main():
	n,m = map(int,input().split())
	
	
	
	
	l = [list(map(int, input().split())) for _ in range(n)]
	
	
	
	l_s = sorted(l)
	

	ans_yen = 0
	ans_can = 0
	stock = 0
	i = 0
	while m > ans_can and i <= int(len(l_s))-1:
		while stock < l_s[i][1] and m > ans_can:
			
			ans_yen += l_s[i][0]
			stock += 1
			ans_can += 1
			
			
		
		stock = 0
		i += 1
		
		
	print(ans_yen)


 
if __name__ == '__main__':
	main()

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

a.sort()
now = m
ans = 0
for i in range(n):
    ans += a[i][0] * min(now,a[i][1])
    now -= a[i][1]
    if now <= 0:
        break
print(ans)

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


from operator import itemgetter
def main():
    AB.sort(key = itemgetter(0))
    cnt = 0
    ans = 0
    for a,b in AB:
        if b + cnt < m:
            cnt += b 
            ans += b*a
        else:
            ans += (m - cnt)*a
            print(ans)
            return



if __name__ == "__main__":
    main()

Solution (Python):
n_store, n_drink = [int(i) for i in input().split()]
stores = [[int(i) for i in input().split()] for _ in range(n_store)]  
stores.sort(key=lambda x: x[0], reverse=True)  

ans = 0
while n_drink:
    store = stores.pop()
    price, n_stock = store

    n_buy = min(n_stock, n_drink)  
    ans += n_buy * price
    n_drink -= n_buy
print(ans)

Solution (Python):
n_store, n_drink = [int(i) for i in input().split()]
stores = [[int(i) for i in input().split()] for _ in range(n_store)]  
stores.sort(key=lambda x: x[0], reverse=True)  

ans = 0
while True:
    if n_drink == 0:
        
        break
    store = stores.pop()
    price, n_stock = store

    n_buy = min(n_stock, n_drink)  
    ans += n_buy * price
    n_drink -= n_buy
print(ans)

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

Solution (Python):
n, m = map(int, input().split())
d = []
c = 0
last = 0

for i in range(n):
    d.append(tuple(map(int, input().split())))
d.sort()

while m > 0:
    a, b = d[last]
    c += min(a * b, m * a)
    m -= b
    last+=1

print(c)

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

ls_ab.sort()
ans = 0
kosu = 0

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

print(ans)

Solution (Python):
n,m=map(int,input().split())
l=[]
for _ in range(n):
    l.append(list(map(int,input().split())))
l.sort()
s=0
i=0
while m>0:
    s+=min(l[i][1],m)*l[i][0]
    m-=min(l[i][1],m)
    i+=1
print(s)

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

for _ in range(N):
    a, b = map(int, input().split())
    AB.append([a, b])

AB = sorted(AB)


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

print(ans)

Solution (Python):
n,k = map(int, input().split())
a = sorted(tuple(map(int,input().split())) for i in range(n))
ans = 0
for x in a:
  temp = min(x[1],k)
  ans += temp*x[0]
  k = max(0,k-temp)
print(ans)

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


ans = 0
k.sort()
for i in range(n):
    ml = min(k[i][1],m)
    ans += k[i][0] * ml
    m -= k[i][1]
    if m <= 0:
        break
print(ans)

Solution (Python):
N, M = map(int, input().split())
AB = sorted(tuple(map(int, input().split())) for _ in range(N))
ans = 0
for a, b in AB:
    k = min(b, M)
    ans += k * a
    M -= k
print(ans)

Solution (Python):
n, m = map(int, input().split())
li = []
for _ in range(n):
    a, b = map(int, input().split())
    li.append((a, b))
li.sort()
money = 0
for a, b in li:
    if m <= 0: break
    if b > m:
        money += a * m
    else:
        money += a * b
    m -= b
print(money)

Solution (Python):
from sys import stdin
input = stdin.readline

n, k = map(int, input().split())
arr = list(tuple(map(int, input().split())) for _ in range(n))
arr = sorted(arr, key=lambda x:x[0])
res = 0
for i in range(n):
    tk = min(k, arr[i][1])
    res += tk*arr[i][0]
    k -= tk
print(res)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,m=map(int,readline().split())
d = map(int,read().split())
data = list(zip(d,d))
data.sort(key=lambda x:x[0])
c=0
cost=0
for x,y in data:
  if m-c<=y:
    cost+=(m-c)*x
    break
  else:
    c+=y
    cost+=y*x
print(cost)

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

l.sort()

drink_cnt = 0
ans = 0
for cost, cnt in l:
    if drink_cnt + cnt <= m:
        ans += cost * cnt
        drink_cnt += cnt
        if drink_cnt == m:
            break
    else:
        ans += cost * (m - drink_cnt)
        break

print(ans)

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

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

C = zip(A, B)
C = sorted(C)
A, B = zip(*C)

total_price = 0
total_num = 0
for i in range(n):
    if total_num < m:
        buy = min(B[i], m - total_num)
        total_num += buy
        total_price += buy*A[i]
    else:
        break
print(total_price)

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])

con = 0
yen = 0

flag = 0

for i in range(N):
  amount = AB[i][1]
  price = AB[i][0]
  for j in range(0,amount):
    con += 1
    yen += price
    if con == M:
      flag = 1
      break
  if flag == 1:
    break
    
print(yen)

Solution (Python):
N, M = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N)]
 
ABs.sort()
 
ans = 0
for A, B in ABs:
    ans += A * min(M, B)
    M -= min(M, B)
print(ans)

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

AB=[]
for n in range(N):
    a,b=[int(j) for j in input().split()]
    AB.append((a,b))

AB=sorted(AB)


money=0
bottle=M
for a in AB:
    if bottle>=a[1]:
        money+=a[0]*a[1]
        bottle-=a[1]
    else:
        money+=a[0]*bottle
        bottle-=bottle
        break
print(money)

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

ab.sort()

ans = 0
c = 0
for a, b in ab:
  if c + b >= m:
    ans += (m - c) * a
    break
  else:
    ans += a * b
    c += b
print(ans)

Solution (Python):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N, M = list(map(int, sys.stdin.buffer.readline().split()))
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]

AB.sort()
AB = np.array(AB, dtype=int)
cs = AB[:, 1].cumsum()
i = np.searchsorted(cs, M, side='left')
ans = (AB[:i, 0] * AB[:i, 1]).sum()
ans += (M - AB[:i, 1].sum()) * AB[i, 0]
print(ans)

Solution (Python):
import numpy as np

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

cnt = 0
sum_A = 0
for a, b in AB:
    sum_A += a * b
    cnt += b
    if cnt > M:
        ans = sum_A - (cnt - M) * a
        break
else:
    ans = sum_A

print(ans)

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

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

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

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

ab.sort(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
    else:
        cnt += ab[i][1]
        cost += ab[i][0]*ab[i][1]

ans = cost
print(ans)

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

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

A.sort(key = lambda x: x[0])
num = M
idx = 0
ans = 0
while num != 0:
    n = min(num, A[idx][1])
    ans += n * A[idx][0]
    num -= n
    idx += 1

print(ans)

Solution (Python):
from operator import itemgetter


def solve():
    N, M = map(int, input().split())
    AB = [[0, 0] for _ in range(N)]
    for i in range(N):
        AB[i][0], AB[i][1] = map(int, input().split())

    AB.sort(key=itemgetter(0))

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


if __name__ == '__main__':
    solve()

Solution (Python):

import sys


def solve(N: int, M: int, AB: "List[[int, int]]"):
    AB = sorted(AB)
    ans = 0
    bought_drink = 0
    for ab in AB:
        if M - bought_drink > ab[1]:
            ans += ab[0] * ab[1]
            bought_drink += ab[1]
        else:
            ans += ab[0] * (M - bought_drink)
            bought_drink = M
            break
    print(ans)



def main():
    N, M = map(int, input().split())
    AB = []
    for n in range(N):
        AB.append(list(map(int, input().split())))
    solve(N, M, AB)

if __name__ == '__main__':
    main()

Solution (Python):
ans=0
n,m=map(int,input().split())
lst=[0]*n
zan=m
for i in range(n):
    a,b=map(int,input().split())
    lst[i]=[a,b]
lst.sort(key=lambda x: x[0])

for i in range(n):
    if lst[i][1]<=zan:
        zan-=lst[i][1]
        ans+=lst[i][1]*lst[i][0]
    elif lst[i][1]>zan:
        ans+=lst[i][0]*zan
        break
print(ans)

Solution (Python):
n, need = map(int, input().split())
a = sorted(tuple(map(int, input().split())) for _ in range(n))
spent = 0
for price, count in a:
    buy = min(count, need)
    spent += buy * price
    need -= buy
    if need == 0:
        break
print(spent)

Solution (Python):
from heapq import heappush, heappop
N, M = map(int, input().split())

queue = []

for i in range(N):
  a, b = map(int, input().split())
  heappush(queue, (a, b))

count = 0
ans = 0

while count < M:
  a, b = heappop(queue)
  
  if count + b < M:
    count += b
    ans += a * b
  else:
    c = (M - count)
    count += c
    ans += c * a
  
print(ans)

Solution (Python):
from operator import itemgetter
n,m=map(int,input().split())
lis=[list(map(int,input().split())) for i in range(n)]
lis.sort()
s=0
for i in range(n):
  if m>lis[i][1]:
    s+=lis[i][0]*lis[i][1]
    m-=lis[i][1]
  else:
    s+=lis[i][0]*m
    break
print(s)

Solution (Python):
n, m = [int(i) for i in input().split()]
ab = []
for a in range(n):
	a, b = [int(i) for i in input().split()]
	ab.append((a, b))
cnt_m = 0
sum_money = 0
while(True):
	for a, b in sorted(ab):
		buy_n = min(m - cnt_m, b)
		sum_money += buy_n * a
		cnt_m += buy_n
	if cnt_m == m:
		break
print (sum_money)

Solution (Python):
import os, sys, re, math

(N, M) = [int(n) for n in input().split()]

stores = []
for i in range(N):
    stores.append([int(n) for n in input().split()])

stores = sorted(stores, key=lambda x: x[0])
total = 0
for (price, stock) in stores:
    total += price * min(stock, M)
    M -= stock
    if M <= 0:
        break

print(total)

Solution (Python):
N, M = [int(s) for s in input().split(' ')]
count = 0
ABs = []
for n in range(N):
    ab =  [int(s) for s in input().split(' ')]
    ABs.append(ab)
ABs.sort(key=lambda x: x[0])
m = M
yen = 0
for a, b in ABs:
    drinks = min(m, b)
    yen += drinks * a
    m -= drinks
    if m == 0:
        break
print(yen)

Solution (Python):
n, m = list(map(int, input().split(' ')))
aabb = []

for i in range(n):
    a, b = list(map(int, input().split(' ')))
    aabb.append((a, b))
aabb = sorted(aabb)
ans = 0
for (a, b) in aabb:
    hoge = min(m, b)
    ans += a * hoge
    m -= hoge
    if not m:
        break
print(ans)

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 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 main():
    N, M = LI()
    A = []
    for _ in range(N):
        a, b = LI()
        A.append((a, b))
    A = sorted(A, key=lambda x:x[0])
    bought = 0
    ix = 0
    money = 0
    while bought < M:
        if bought + A[ix][1] > M:
            money += A[ix][0] * (M - bought)
            break
        bought += A[ix][1]
        money += A[ix][0] * A[ix][1]
        ix += 1
    print(money)


main()

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





ans = 0
i = 0
mat.sort()

while True:
    if m - mat[i][1] > 0:
        ans += mat[i][1]*mat[i][0]
        m -= mat[i][1]
        i += 1
    else:
        ans += mat[i][0] *m
        break
print(ans)

Solution (Python):
N, M = map(int, input().split())
List = []
for _ in range(N):
  P, Q = map(int,input().split())
  List.append([P,Q])
List = sorted(List)
total = 0
for item in List:
  for _ in range(item[1]):
    total += item[0]
    M -= 1
    if M == 0:
      break
  if M == 0:
    break
print(total)

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
ary = []
for _ in range(n):
    ary.append(list(map(int, input().split())))
ary.sort()

res = 0
for (a, b) in ary:
    if m-b>0:
        m -= b
        res += a*b
    else:
        res += a*m
        break
print(res)

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
pn = 0
for i in range(N):
  if (cnt + A[i][1]) >=  M:
    pn += A[i][0]*(M-cnt)
    break
  else:
    cnt += A[i][1]
    pn += A[i][0]*A[i][1]
print(pn)

Solution (Python):
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N)]
AB=sorted(AB, key=lambda a:a[0]  )
ans=0
l=0
for ab in AB:
  if l+ab[1]<=M:
    l+=ab[1]
    ans+=ab[0]*ab[1]
  else:
    for i in range(ab[1]):
      if l<M:
        l+=1
        ans+=ab[0]
  if l==M:
    print(ans);break

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

cnt=0
i=-1
ans=0
while cnt<m:
    i+=1
    ans+=AB[i][0]*AB[i][1]
    cnt+=AB[i][1]
    
ans-=AB[i][0]*AB[i][1]
cnt-=AB[i][1]

while cnt<m:
    ans+=AB[i][0]
    cnt+=1
print(ans)

Solution (Python):
N,M = map(int,input().split())
AB = [tuple(int(i) for i in input().split()) for j in range(N)]
AB.sort()
ans = 0
for i in range(N):
    a,b = AB[i]
    if M -b > 0:
        ans += a*b
        M -= b
    else:
        ans += M*a
        break
print(ans)

Solution (Python):
M, N = map(int, input().split())
COST, STOCK = range(2)
energy_drinks = sorted((tuple(map(int, input().split())) for _ in range(M)), key=lambda tuple_: tuple_[COST])

cost = 0
for energy_drink in energy_drinks:
    buy = min(energy_drink[STOCK], N)
    N -= buy
    cost += buy * energy_drink[COST]
print(cost)

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 in range(N):
    if AB[i][1] < M:
        ans += AB[i][0]*AB[i][1]
        M -= AB[i][1]
    else:
        ans += AB[i][0]*M
        break
print(ans)

Solution (Python):
N, M = map(int, input().split())
AB = []
for i in range(N):
    AB.append([int(i) for i in input().split()])
AB.sort()
ans = 0
for ab in AB:
    if ab[1] >= M:
        ans += ab[0] * M
        print(ans)
        exit()
    else:
        ans += ab[0] * ab[1]
        M -= ab[1]

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

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, string
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = 0 ;count = 0 ;pro = 1


n,m=map(int,input().split())
AB=[tuple(map(int,input().split())) for i in range(n)]
AB.sort(key=lambda tup:tup[0])
i=0
while count<m:
    ans+=min(m-count,AB[i][1])*AB[i][0]
    count += min(m-count, AB[i][1])
    i+=1
print(ans)

Solution (Python):
from operator import itemgetter
N,M = map(int,input().split())
ls = [list(map(int,input().split())) for _ in range(N)]
ls = sorted(ls,key=itemgetter(0))
ans = 0
pon = 0
for i in range(N):
    if pon < M:
        pon += ls[i][1]
        ans += ls[i][1]*ls[i][0]
    else:
        i = i-1
        break
if pon == M:
    print(ans)
else:
    ans -= ls[i][0]*(pon - M)
    print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = []
for i in range(n):
    a = list(map(int, input().split()))
    ab += [a]
ab.sort(key=lambda x: x[0])
count = 0
sum_drink = 0
for i in range(n):
    for j in range(ab[i][1]):
        sum_drink += ab[i][0]
        count+=1
        if count >= m:
            print(sum_drink)
            exit()

Solution (Python):
from sys import stdin


def main():
    N, M = map(int, input().split())
    *AB, = map(int, stdin.read().split())
    A = AB[::2]
    B = AB[1::2]
    num = 0
    yen = 0
    for a, b in sorted(zip(A, B)):
        if M < num + b:
            yen += a * (M - num)
            break
        else:
            yen += a * b
            num += b
    print(yen)
    return


main()

Solution (Python):
from sys import stdin


def main():
    N, M = map(int, input().split())
    *AB, = map(int, stdin.read().split())
    A = AB[::2]
    B = AB[1::2]
    Z = zip(A, B)
    Z = sorted(Z)
    num = 0
    yen = 0
    for a, b in Z:
        if M < num + b:
            yen += a * (M - num)
            break
        else:
            yen += a * b
            num += b
    print(yen)
    return


main()

Solution (Python):
from sys import stdin

N, M = map(int, input().split())
*AB, = map(int, stdin.read().split())


def main():
    A = AB[::2]
    B = AB[1::2]
    Z = zip(A, B)
    Z = sorted(Z)
    num = 0
    yen = 0
    for a, b in Z:
        if M < num + b:
            yen += a * (M - num)
            break
        else:
            yen += a * b
            num += b
    print(yen)
    return


main()

Solution (Python):
N,M = map(int,input().split())
array = []
suma = 0
summ = 0
for i in range(N):
    ab = list(map(int,input().split()))
    array.append(ab)
array.sort()

for i in range(N):
    if suma + array[i][1] <= M:
        suma += array[i][1]
        summ += array[i][1]*array[i][0]
    else:
        summ += (M-suma)*array[i][0]
        break
print(summ)

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])
tmp=0
ans=0
for x in ab:
  if m-tmp<x[1]:
    ans+=x[0]*(m-tmp)
    break
  else:
    ans+=x[0]*x[1]
    tmp+=x[1]
print(ans)

Solution (Python):
def p_c():
    n, m = map(int, input().split())
    ab = [tuple(map(int, input().split())) for _ in range(n)]
    ab.sort()
    ans = 0
    for i in range(n):
        a, b = ab[i]
        b = min(m, b)
        ans += a * b
        m -= b
        if m == 0:
            break
    print(ans)
    
p_c()

Solution (Python):
n,m=map(int,input().split())
x=[list(map(int,input().split())) for _ in range(n)]
x.sort()
p,c=0,0

for a,b in x:
  if c+b<=m:
    p+=a*b
    c+=b
  else:
    p+=a*(m-c)
    c+=m-c
    break
print(p)

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

cnt = ans = 0
for i in range(n):
    ans += min(lst[i][1], m - cnt) * lst[i][0]
    cnt += min(lst[i][1], m - cnt)
    if cnt==m:
        break

print(ans)

Solution (Python):
def solve(n, dst):
    xs = []
    ans_cost = 0

    for _ in range(n):
        c, q = map(int, input().split())
        xs.append((c, q))
    xs.sort()

    for cost, quant in xs:
        if quant >= dst:
            ans_cost += cost * dst
            return ans_cost
        else:
            dst -= quant
            ans_cost += cost * quant


if __name__ == '__main__':
    n, dst = map(int, input().split())
    print(solve(n, dst))

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


for i in range(m):
    if ab[ind][1] > 0:
        cnt += ab[ind][0]
        ab[ind][1] -= 1
    else:
        ind += 1
        cnt += ab[ind][0]
        ab[ind][1] -= 1
print(cnt)

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

    for i in range(N):
        A, B = map(int, input().split())
        temp.append([A, B])
    
    temp.sort()
    ans = 0
    
    for i in range(N):
        if M == 0:
            break
        if M - temp[i][1] >= 0:
            M -= temp[i][1]
            ans += temp[i][0] * temp[i][1]
        else:
            ans += M  * temp[i][0]
            M = 0
        
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, M = MAP()
SHOP = [MAP() for _ in range(N)]
SHOP.sort()

A = [s[0] for s in SHOP]
B = [s[1] for s in SHOP]



bought = 0
money = 0
for i in range(N):
	if bought+B[i] < M:
		money += A[i]*B[i]
		bought += B[i]
	else:
		money += A[i]*(M-bought)
		bought += M-bought

print(money)

Solution (Python):
n,m=map(int,input().split())
ab=sorted([list(map(int,input().split())) for _ in range(n)])
ans=0
for a,b in ab:
    ans+=a*min(b,m)
    m-=b
    if m<=0:
        break

print(ans)

Solution (Python):
n, m = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]
l.sort(key=lambda x: x[0])
num = 0
ansum = 0
for i in range(n):
    if num + l[i][1] < m:
        ansum += l[i][0] * l[i][1]
        num += l[i][1]
    else:
        ansum += l[i][0] * (m - num)
        break
print(ansum)

Solution (Python):
N,M = map(int,input().split())
L = []
for i in range(N):
    a,b = map(int,input().split())
    s = (a,b)
    L.append(s)
L.sort()
ans = 0
cnt = 0
for i,j in L:
    if(M> (cnt+ j)):
        ans += i * j
        cnt += j
    elif (M <= (cnt + j)):
        ans += i * (M - cnt)
        break
print(ans)

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

for n in range(N):
    a, b = map(int, input().split())
    C[n] = (a, b)

C.sort()
m = 0
res = 0

for c in C:
    buy = min(c[1], M-m)
    m += buy
    res += c[0] * buy
    if m == M:
        break

print(res)

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


print(ans)

Solution (Python):
n, m = (int(i) for i in input().split())  
a = [[int(i) for i in input().split()] for i in range(n)] 
dic = {}
lis = []

a.sort()
ans = 0
for x in a:
  if x[1] >= m:
    ans += x[0] * m
    break
  else:
    ans += x[0] * x[1]
    m -= x[1]

print(ans)

Solution (Python):
from operator import itemgetter

n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]

num = sorted(a)
total = 0
ans = 0

for i in range(n):
  total += num[i][1]
  if total > m:
    ans += num[i][0]*(num[i][1] -(total - m)) 
    break
  ans += num[i][0]*num[i][1] 
  
print(ans)