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())
cnt = 0
L = []
for i in range(n):
    a,b = map(int,input().split())
    L.append([a,b])
L.sort()

cnt = 0
sum1 = 0
for i in range(n):
    price  = L[i][0]*L[i][1]
    num = L[i][1]
    if cnt + num <= m:
        cnt +=num
        sum1 +=price
    elif cnt + num > m:
        num = m - cnt
        sum1 += num*L[i][0]
        break
print(sum1)

Solution (Python):


import numpy as np

n, m = map(int, input().split())
a = [0] * n
b = [0] * n
for i in range(n):
    a[i], b[i] = list(map(int, input().split()))
a = np.array(a)
b = np.array(b)
c = np.argsort(a)
a = a[c]
b = b[c]
cost = 0
for i in range(len(a)):
    buy = min(b[i], m)
    cost += buy * a[i]
    
    m -= buy
    if m == 0:
        break
print(cost)

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

cnt=0
ans=0
for a,b in AB:

    temp=min(m-cnt,b)
    ans+=a*temp
    cnt+=temp
    if cnt==m:
        print(ans)
        break

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

Solution (Python):
import numpy as np
N, M = map(int, input().split())
A = []
B = []
for _ in range(N):
  a, b = map(int, input().split())
  A.append(a)
  B.append(b)
cnt = 0
ans = 0
ind = np.argsort(A)
for i in range(N):
  m = min(M - cnt, B[ind[i]])
  ans += A[ind[i]] * m
  cnt += m
  if cnt >= M:
    break
print(ans)

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

for _ in range(n):
	ab.append(list(map(int,input().split())))

ab.sort()

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

Solution (Python):

import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
d_list = []
for i in range(N):
    a, b = lr()
    d_list.append([a, b, i])

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

cnt = 0
ans = 0
for d in d_list:
    if (M - cnt) > d[1]:
        ans += d[1] * d[0]
        cnt += d[1]
    elif (M - cnt) > 0:
        ans += (M - cnt) * d[0]
        cnt += (M - cnt)
    else:
        break

print(ans)

Solution (Python):
N, M = map(int, input().split())
AB = list()
for i in range(N):
    AB.append(tuple(map(int, input().split())))
AB.sort()
money = 0
i = 0
while M > AB[i][1]:
    M -= AB[i][1]
    money += AB[i][0]*AB[i][1]
    
    i += 1

money += AB[i][0]*M
M = 0
print(money)

Solution (Python):
n, m = [int(x) for x in input().split()]
temp_list = sorted([[int(x) for x in input().split()] for _ in range(n)])

ans = 0
for a, b in temp_list:
    ans += a * min(b, m)
    m -= b
    if m <= 0:
        break
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x:x[0])
ans = 0
count = 0
i = 0
while count < m:
    ans += ab[i][0]
    count += 1
    ab[i][1] -= 1
    if ab[i][1] == 0:
        i += 1
print(ans)

Solution (Python):
N,M=map(int,input().split())
mylist=[]
for i in range(N):
    A,B=map(int,input().split())
    temp=[A,B]
    mylist.append(temp)
    


mylist.sort()


drink_sum=0
i=0
fee=0
while drink_sum<M and i<=N-1:
    if drink_sum+mylist[i][1]>=M:
        
        
        
        fee+=mylist[i][0]*(M-drink_sum)
        drink_sum+=M-drink_sum
        
        
    else:
        fee+=mylist[i][0]*mylist[i][1]
        drink_sum+=mylist[i][1]
        
        
        
    i+=1
    
print(fee)

Solution (Python):
n, m = map(int, input().split())
lst = [list(map(int, input().split())) for i in range(n)]
lst.sort()
lst = lst[::-1]
ans = 0

while m > 0:
    if lst[-1][1] <= m:
        ans += lst[-1][0] * lst[-1][1]
        m -= lst[-1][1]
    else:
        break
    lst.pop()

if m > 0:
    ans += lst[-1][0] * m

print(ans)

Solution (Python):

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

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

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

Solution (Python):
from collections import defaultdict

def multi_input():
    return map(int, input().split())

N, M = multi_input()
AB = [list(multi_input()) for _ in range(N)]
d = defaultdict(int)

for value, num in AB:
    d[value] += num

ans = 0
for value, num in sorted(d.items(), key=lambda x:x[0]):
    ans += value * min(num, M)
    M -= num
    if M <= 0:
        break
print(ans)

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

while m>0:
  ab = AB.pop(0)
  for _ in range(ab[1]):
    m-=1
    cost += ab[0]
    if m==0: break
    
print(cost)

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

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

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

Solution (Python):
n, m = map(int, input().split())
d = [tuple(map(int, input().split())) for i in range(n)]
d.sort()
x = 0
for i in range(len(d)):
    x += d[i][0] * min(d[i][1], m)
    m -= min(d[i][1], m)
    if m == 0:
        break
print(x)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]




N, M = mi()
A = li2(N)

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

ans = 0
i = 0
while M > 0:
    if M >= A[i][1]:
        ans += A[i][0] * A[i][1]
        M -= A[i][1]
        i += 1
    else:
        ans += A[i][0] * M
        break

print(ans)

Solution (Python):

import heapq

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

cnt = 0
ans = 0
for i in range(N):
    price, bottles = heapq.heappop(AB)
    cnt = min(M, bottles)
    M -= cnt
    ans += price * cnt
    if M==0:
        break

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)

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

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

print(ans)

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

for i in range(n):
    bottle += AB[i][1]
    yen += AB[i][0] * AB[i][1]
    
    if bottle >= m:
        yen -= (bottle - m) * AB[i][0]
        break
        
print(yen)

Solution (Python):
def energy_drink_collector(n, m, abli):

    ans = 0
    count = m
    abli.sort(key=lambda x: x[0])

    for i in abli:
        if i[1] <= count:
            ans += i[0]*i[1]
            count -= i[1]
        else:
            ans += count*i[0]
            count = 0
        if not count:
            break
    return ans

def main():
    n, m = map(int, input().split())
    abli = [list(map(int, input().split()))for i in range(n)]
    print(energy_drink_collector(n, m, abli))

if __name__ == '__main__':
    main()

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

cnt=0
res=0

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

print(res)

Solution (Python):

import sys
input = sys.stdin.readline
n,m= map(int,input().split())
A =[list(map(int,input().split())) for _ in range(n)]
pay = 0
A.sort()
for i in range(n):
    if A[i][1] > m:
        pay += A[i][0]*m
        break
    else :
        m -= A[i][1] 
        pay += A[i][0]*A[i][1]
print(pay)

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

Solution (Python):
n, m = map(int, input().split())
shops = []
for i in range(n):
    a, b = map(int, input().split())
    shops.append((a, b))
shops.sort()

i = 0
total = 0
price = 0
while True:
    a, b = shops[i]
    if b >= m - total:
        price += (m - total) * a
        break
    else:
        total += b
        price += a * b
        i += 1

print(price)

Solution (Python):
import numpy as np

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

mk = np.array([a,b])

index = np.argsort(mk)

mn = np.array([mk[0,index[0,:]],mk[1,index[0,:]]])


sum = 0
for i in range(n):
    if mn[1][i] <= m:
        sum += mn[0][i] * mn[1][i]
        m = m - mn[1][i]

    else:
        sum += mn[0][i] * m
        break
print(sum)

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

Solution (Python):
n,m = map(int,input().split())
drink=[]
for _ in range(n):
  a,b = map(int,input().split())
  drink.append((a,b))
drink.sort(reverse=1)

ans = 0
while m>0:
  a,b = drink.pop(-1)
  ans += a*min(m, b)
  m -= b
print(ans)

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



AB = sorted(AB)

cnt = 0
ans = 0
for ab in AB:
    a, b = ab[0], ab[1]
    if M >= b:
        ans += a*b
        M -= b
    else:
        ans += a*M
        M -= M
    if M == 0:
        break

print(ans)

Solution (Python):
def main():
    N, M = map(int, input().split())
    store = list()
    for i in range(N):
        A, B = map(int, input().split())
        store.append([A, B])
    store.sort(key = lambda x : x[0])
    cost = 0
    drink = 0
    for i in range(N):
        if drink < M:
            if drink + store[i][1] < M:
                drink += store[i][1]
                cost += store[i][0] * store[i][1]
            else:
                cost += store[i][0] * (M - drink)
                drink += M - drink
        else:
            break
    print(cost)
if __name__ == "__main__":
    main()

Solution (Python):
N , M = (map(int , input().split()))
sc = [list(map(int, input().split())) for i in range(N)]
sorted_cs = sorted(sc,key = lambda x:x[0])
sorted_cs
ans = 0
cnt = 0
for i in range(N):
    for j in range(sorted_cs[i][1]):
        ans += sorted_cs[i][0]
        cnt += 1
        if cnt >= M:
            break
    if cnt >= M:
        break
print(ans)

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

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

cnt = 0
cost = 0

for arr in price:
  p,n = arr
  if n+cnt <= M:
    cost+=p*n
    cnt+=n
  else:
    cost+=(M-cnt)*p
    break
    
print(cost)

Solution (Python):
import sys
import os
import math
import string

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


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

    N, M = il()
    AB = [il() for _ in range(N)]
    AB.sort(key=lambda x: x[0])
    ret = 0
    for a, b in AB:
        if M - b > 0:
            ret += b * a
            M = M - b
        else:
            ret += M * a
            break
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
stores = sorted([[int(i) for i in input().split()] for j in range(n)], key=lambda x: x[0])
sum = 0
for store in stores:
    price, cans = store
    sum += min(cans, m) * price
    m -= min(cans, m)
    if m == 0:
        break
print(sum)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N, M = map(int, input().split())
    AB = sorted([tuple(map(int, input().split())) for _ in range(N)])
    money = 0
    count = 0
    for a, b in AB:
        if count + b >= M:
            money += a * (M - count)
            break
        else:
            money += a * b
            count += b
    print(money)


if __name__ == '__main__':
    main()

Solution (Python):

n, m = map(int, input().split())
a = [0] * n
b = [0] * n
arr = []
s = 0
ans = 0
for i in range(n):
    a, b = map(int, input().split())
    arr.append((a,  b))
arr.sort()
for d in arr:
    s += d[1]
    if s >= m:
        ans += d[0] * (m - (s - d[1]))
        print(ans)
        exit()
    else:
        ans += d[0] * d[1]

Solution (Python):
n, m=map(int, input().split())
ab=[list(map(int, input().split())) for _ 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):
import numpy as np

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

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

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

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N, M = MAP()
AB = [LIST() for _ in range(N)]
 
AB.sort(key = lambda x: (x[0], x[1]))
 
cnt = 0
ans = 0
for i in range(N):
	if cnt+AB[i][1] >= M:
		ans += (M-cnt)*AB[i][0]
		print(ans)
		break
	else:
		cnt += AB[i][1]
		ans += AB[i][0] * AB[i][1]

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

Solution (Python):
n, m = map(int, input().split())
ls = []
for i in range(n):
    a, b = map(int, input().split())
    ls.append([a, b])
ls.sort()
price = 0

for i in ls:
    if i[1] >= m:
        price += i[0] * m
        m = 0
    else:
        price += i[0] * i[1]
        m -= i[1]

    if m == 0:
        break

print(price)

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

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

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

cost = 0
for si in s:
  if m >= si[1]:
    cost += si[1] * si[0] 
    m -= si[1]
    
  else:
    cost += int(m * si[0])
    break
    
print(cost)

Solution (Python):
















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

arrAB = sorted(arrAB, key=lambda lis: lis[0])

drink = M
ans = 0
for lis in arrAB:
    if drink<=0:
        break
    num = min(drink,lis[1])
    ans += lis[0] * num
    drink -= num

print(ans)

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

ab_hash = {}
count = 0
price_sum = 0
for x in range(len(ab)):
    ab_hash[x] = [ab[x][0], ab[x][1]]
ab_key_sorted = sorted(ab_hash.items(), key=lambda x: x[1][0])

for y in ab_key_sorted:
    if M > y[1][1]:
        price_sum += y[1][0] * y[1][1]
    else:
        price_sum += y[1][0] * M
    M -= y[1][1]
    if M <= 0:
        break

print(price_sum)

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

ans=0
drink.sort()
cnt=0
while(m>0 and cnt<n):
    if m>=drink[cnt][1]:
        m-=drink[cnt][1]
        ans+=drink[cnt][0]*drink[cnt][1]
    else:
        ans+=drink[cnt][0]*m
        m=0
    cnt+=1

print(ans)

Solution (Python):


nim, mike = map(int, input().split())
array = [list(map(int, input().split())) for _ in range(nim)]
array.sort()

ans = 0
for i in range(nim):
    ans += array[i][0] * min(mike, array[i][1])
    mike -= array[i][1]
    if mike <= 0:
        break

print(ans)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,m = iim()
l = []
for i in range(n):
    l.append(iil())

l.sort()

cost = 0
num = 0
for item in l:
    num += item[1]
    cost += item[0]*item[1]

    if num >= m:
        cost -= (num-m)*item[0]
        print(cost)
        exit()

Solution (Python):
def myAnswer(N:int,M:int,A:list,B:list) -> int:
   dic ={}
   for i in range(N):
      if(A[i] in dic.keys()):
         dic[A[i]] += B[i]
      else:
         dic[A[i]] = B[i]
   dic2 = sorted(dic.items())
   total = 0
   for key,value in dic2:
      if(M - value < 0):
         total += M * key
         break
      else:
         total += key * value
         M -= value
   return total

def modelAnswer():
   tmp=1
def main():
   N,M = map(int,input().split())
   A = []
   B = []
   for _ in range(N):
      a,b=map(int,input().split())
      A.append(a)
      B.append(b)
   print(myAnswer(N,M,A[:],B[:]))
if __name__ == '__main__':
   main()

Solution (Python):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[0])
ans = 0
for i in range(N):
    if M > AB[i][1]:
        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())
d = [list(map(int,input().split())) for i in range(n)]
d.sort()
i = 0
cnt = 0
while m>0:
  if m-d[i][1] >= 0:
    cnt += d[i][0]*d[i][1]
    m -= d[i][1]
    i += 1
  else:
    cnt += d[i][0]*m
    m = 0
    i += 1
    
print(cnt)

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

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

drink = []

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

drink.sort()

cost = 0
bought = 0

for i in range(n):
    a, b = drink[i]

    if bought + b < m:
        bought += b
        cost += a * b
    else:
        cost += a * (m - bought)
        break

print(cost)

Solution (Python):
from functools import cmp_to_key


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


def compare(item1, item2):
    if item1[0] == item2[0]: return 0
    return -1 if item1[0] < item2[0] else 1
sorted_AB = sorted(AB, key=cmp_to_key(compare))


remaining = M
total_amount = 0
for ABi in sorted_AB:
    price = ABi[0]
    number_of_stocks = ABi[1]
    number_of_purchases = min(number_of_stocks, remaining)
    sub_total = price * number_of_purchases

    total_amount += sub_total
    remaining -= number_of_purchases

    if remaining <= 0: break

print(total_amount)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = [list(map(int,input().split())) for _ in range(n)]
a = sorted(a,key=lambda x:x[0])
ans = 0
for i,j in a:
  if m > j:
    ans += i*j
    m -= j
  else:
    ans += i*m
    break
print(ans)

Solution (Python):
def main():
    from operator import itemgetter
    import sys
    input = sys.stdin.readline

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

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

    ds.sort(key=itemgetter(0))

    rest = M
    mi = 0
    for a, b in ds:
        use = min(rest, b)
        mi += a * use
        rest -= use
        if not use:
            break

    print(mi)


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):













mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,m = readInts()
    shop = []
    for i in range(n):
        mon,num = readInts()
        shop.append([mon,num])
    shop = sorted(shop,key = lambda x:x[0])
    now = 0
    cost = 0
    for i in range(n):
        mon,num = shop[i]
        
        if now < m: 
            buy = min(num,m - now) 
            cost += mon * buy
            now += buy
        if now == m:
            break
    print(cost)

if __name__ == '__main__':
  main()

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
AB.sort()
answer = 0
for cost, num in AB:
    if M - num > 0:
        answer += cost * num
        M -= num
    else:
        answer += cost * M
        break

print(answer)

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
AB.sort()
drink = 0
answer = 0
for i in range(N):
    cost = AB[i][0]
    limit = AB[i][1]
    if drink + limit < M:
        answer += cost * limit
        drink += limit
    else:
        x = M - drink
        answer += cost * x
        break

print(answer)

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

yen = 0

for i in li:
    j,k = i
    if k >= m:
        yen += j*m
        break
    yen += j*k
    m -= k

print(yen)

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

ab.sort(key=lambda x: x[0])
ans = 0
c = 0
for i in range(n):
    for j in range(ab[i][1]):
        ans += ab[i][0]
        c += 1
        if c == m:
            print(ans)
            quit()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
L = [LIST() for i in range(N)]

L.sort()

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

Solution (Python):



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


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


i = 0
nokori = M
while nokori>0:
    a, b = ab_list[i]

    if nokori-b<0:
        b = nokori
        nokori = 0
    else:
        nokori -= b

    min_cost += a * b
    i += 1


print(min_cost)

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

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

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


abl = sorted(abl)

ans = 0
for a,b in abl:
    if m <= 0:
        break
    ans += min(b, m) * a
    m -= b
print(ans)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    g = LIR(n)
    g.sort()
    ans = 0
    for a,b in g:
        if m <= b:
            ans += a*m
            break
        else:
            ans += a*b
            m -= b
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
n,m=map(int,input().split())
L1=[]
L2=[]
for i in range(n):
    a=list(map(int,input().split()))
    L1.append(a)
L1.sort()
money=0
for x,y in L1:
    if y <= m:
        money = money + y*x
        m -= y
    elif y > m >= 0:
        money = money + x*m
        break
print(money)

Solution (Python):
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt = 0
ind = N-1
for i in range(N):
    if cnt+A[i][1]>M:
        ind = i-1
        break
    cnt += A[i][1]
yen = 0
for i in range(ind+1):
    yen += A[i][0]*A[i][1]
if ind+1<N:
    yen += A[ind+1][0]*(M-cnt)
print(yen)

Solution (Python):
def resolve():
    n, m = map(int, input().split())
    l = [list(map(int, input().split())) for _ in range(n)]
    l.sort()
    ans = 0
    drink = 0
    for i in range(n):
        cost = l[i][0]
        num = l[i][1]
        if drink + num >=m:
            ans += cost*(m-drink)
            print(ans)
            exit()
        else:
            drink += num
            ans += num*cost

resolve()

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

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

ans = 0
cnt = 0
for A,B in AB:
    ans += A * min(B, M - cnt)
    cnt += min(B, M - cnt)
    if cnt == M:
        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])
ans=0
t=10**10
for a,b in ab:
  t=min(b,m)
  if t<=0:break
  m-=t
  ans+=a*t
print(ans)

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