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 = (int(x) for x in input().split())

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

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

nd = 0
ncost = 0
for A,B in ab_arr:
    if nd+B >= M:
        ncost += (M-nd)*A
        break
    nd += B
    ncost += B*A

print(ncost)

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

cost = 0

for i in ab:
        if m > i[1]:
            m -= i[1]
            cost += i[0] * i[1]
            if m == 0:
                break
        else:
            cost += i[0] * m
            m = 0
            break

print(cost)

Solution (Python):
import sys
a = input().split()
a = [int(i) for i in a]
drink = []


for i in range(a[0]):
    b = input().split()
    b = [int(i) for i in b]
    drink.append(b)

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

while (a[1] > 0):
    if drink[0][1] > a[1]:
        count += drink[0][0] * a[1]
        a[1] = 0
    else:
        count += drink[0][0] * drink[0][1]
        a[1] -= drink[0][1]
    drink.pop(0)

print(count)

Solution (Python):


from heapq import heappop,heappush,heapify

input=open(0).readline
n,m=map(int,input().split())

que=[]
for i in range(n):
    que.append(tuple(map(int,input().split())))
heapify(que)

cnt=0
rem=m
while rem:
    a,b=heappop(que)
    c=min(b,rem)
    cnt+=a*c
    rem-=c

print(cnt)

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

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

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

x = 0
res = 0
i = 0
while True:
    a, b = ab[i]
    if x + b >= m:
        res += (m - x) * a
        break
    else:
        res += b * a
        x += b
    i += 1

print(res)

Solution (Python):
N, M = map(int, input().split())
store = [[0] * 2 for i in range(N)]

for i in range(N):

    a, b = (map(int, input().split()))
    store[i] = (a, b)

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



rest = M
index = 0
cost = 0
while (rest > 0):
    c, drink = store[index]
    if drink >= rest:
        drink = rest
        rest = 0
    else:
        rest -= drink
    cost += drink * c
    index += 1

print(cost)

Solution (Python):
N, M = map(int,input().split())
dic = {}
for _ in range(N):
    a, b = map(int,input().split())
    if a in dic:
        dic[a] += b
    else:
        dic[a] = b

ans = 0
m = 0
for a, b in sorted(dic.items()):
    if m + b >= M:
        ans += a * (M - m)
        break
    m += b
    ans += a * b

print(ans)

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

X = [list(map(int,input().split())) for _ in range(N)]

cheap = sorted(X, key=lambda X:X[0])

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

print(cost)

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

s = 0
ans = 0
li = 0
for i in range(n):
    if s + ab[i][1] < m:
        s += ab[i][1]
        temp = ab[i][0] * ab[i][1]
        ans += temp
    else:
        li = i
        break

ans = ans + (m-s) * ab[li][0]
print(ans)

Solution (Python):


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

arr = []

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

arr.sort()

sum = 0

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

print(sum)

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

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

ans = 0
for a,b in AB:
    if b < M:
        ans += a*b
        M -= b
    else:
        ans += a*M
        break
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
x.sort()
ans = 0
for i in range(n):
    if m - x[i][1] < 0:
        print(ans + m * x[i][0])
        exit()
    ans += x[i][0] * x[i][1]
    m -= x[i][1]
print(ans)

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

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

def temp(var):
    return var[0]

AB = sorted(AB, key=temp)


num = 0
amount = 0

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

print(amount)

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

m, p = 0, 0
for a, b in AB:
    if m+b>=M:
        p += (M-m)*a
        break
    m += b
    p += b*a

print(p)

Solution (Python):
import sys
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])
total,num=0,0
for i in range(n):
  for j in range(ab[i][1]):
    total+=ab[i][0]
    num+=1
    if num==m:
      print(total)
      sys.exit()

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, key=lambda x: x[0])
yen = 0
bottle = 0
for a, b in ab:
  if bottle + b < m:
    yen += a * b
    bottle += b
  else:
    yen += a * (m - bottle)
    break
print(yen)

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()
ans = 0
count = 0

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

Solution (Python):
a, b = map(int, input().split())
c = []
for i in range(a):
    d, e = map(int, input().split())
    c.append([d, e])
c.sort()
g = 0
for i in range(a):
    f = min(b, c[i][1])
    b -= f
    g += f * c[i][0]
    if b == 0:
        break
print(g)

Solution (Python):
n, m = list(map(int, input().split()))
a = []
b = []
data = []
for i in range(n):
    data.append(list(map(int, input().split())))
data.sort()
sum = 0
money = 0
index = 0
while sum + data[index][1]< m:
    sum += data[index][1]
    money += data[index][0] * data[index][1]
    del data[index]
money += (m - sum) * data[index][0]
print(money)

Solution (Python):
N,M=map(int,input().split())
AB=[[int(x) for x in input().split()] for i in range(N)]
AB.sort(key=lambda x: x[0])
out = 0
count = M
for i in range(N):
    if AB[i][1]<count+1:
        count-=AB[i][1]
        out+=AB[i][0]*AB[i][1]
    else:
        out+=count*AB[i][0]
        break
print(out)

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

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

drink_num=0
cost =0

for abi in ab:
  if drink_num + abi[1] >= m :
    cost += (m - drink_num)*abi[0]
    break
  else:
    drink_num += abi[1]    
    cost += abi[0]*abi[1]

print(cost)

Solution (Python):
n, m = (int(i) for i in input().split())
mise = []
for i in range(n):
  tmp = [int(i) for i in input().split()] 
  mise.append(tmp)
mise.sort()
hon = 0
kin = 0
for i in range(n):
  nokori = m - hon
  if nokori > mise[i][1]:
    hon += mise[i][1]
    kin += mise[i][0] * mise[i][1]
  else:
    hon += nokori
    kin += mise[i][0] * nokori
    break
print(kin)

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

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

li.sort()
ans = 0

for i in li:
    if m <= i[1]:
        ans += m * i[0]
        break
    ans += i[0] * i[1]
    m -= i[1]

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

s = 0
ans = 0
for abi in ab:
    a, b = map(int, abi)
    if s + b < m:
        s += b
        ans += a * b
    else:
        ans += a * (m - s)
        break

print(ans)

Solution (Python):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
cost = 0
count = 0
for A, B in AB:
    if M-count > B:
        cost += B*A
        count += B
    elif count < M:
        cost += (M-count)*A
        count += M - count
    else:
        break
print(cost)

Solution (Python):
N, M = map(int, input().split())
l = sorted([list(map(int, input().split())) for _ in range(N)])
c, ans = 0,0
for (yen, stock) in l:
    if c + stock < M:
        c += stock
        ans += stock * yen
    else:
        ans += (M - c) * yen
        c = M
    if c == M:
        print(ans)
        break

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

n_drink = 0
cost = 0
for A, B in AB:
    remaining = M - n_drink
    if remaining > B:
        n_drink += B
        cost += A * B
    elif remaining <= B:
        cost += A * remaining
        print(cost)
        exit()

Solution (Python):
def main():
    N,M = map(int,input().split())
    store_l = []
    for i in range(N):
        store_l.append(tuple(map(int,input().split())))

    store_l = sorted(store_l,key=lambda x:x[0])
    ans = 0
    buynum = 0
    for price,maxnum in store_l:
        if(buynum+maxnum<=M):
            buynum += maxnum
            ans += maxnum*price
        else:
            ans += (M-buynum)*price
            break
    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
import sys
import math
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    stores = [string_to_int(i) for i in inputs]
    wants = M
    money = 0
    for price, num in sorted(stores, key=lambda i: i[0]):
        if wants > num:
            money += price * num
            wants -= num
        else:
            money += price * wants
            break

    return money


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(N))
    print(ret)

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

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

l = []

for i in range(n):
  l.append(list(map(int, input().split())))
           
l.sort(key=lambda x: x[0])

d = 0
cost = 0

for i in range(n):
  d += l[i][1]
  cost += l[i][0]*l[i][1]
  if d >= m:
    print(cost-l[i][0]*(d-m))
    exit()

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

data = sorted(data, key=lambda x: x[0])
count = 0
total = 0
for d in data:
    count += d[1]
    if count >= M:
        total += d[0] * (d[1] - (count - M))
        break
    else:
        total += d[0] * d[1]
print(total)

Solution (Python):








N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
a.sort()
price = [a[i][0] for i in range(N)]
stock = [a[i][1] for i in range(N)]
bought = 0
total_price = 0

for i in range(N):
    if bought + stock[i] <= M:
        total_price += price[i] * stock[i]
        bought += stock[i]
    else:
        total_price += price[i] * (M - bought)
        break
print(total_price)

Solution (Python):








N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
a.sort()
price = [a[i][0] for i in range(N)]
stock = [a[i][1] for i in range(N)]
bought = 0
total_price = 0

for i in range(N):
    if bought + stock[i] <= M:
        total_price += price[i] * stock[i]
        bought += stock[i]
    else:
        total_price += price[i] * (M - bought)
        break
print(total_price)

Solution (Python):



import sys

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

N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
a.sort()
price = [a[i][0] for i in range(N)]
stock = [a[i][1] for i in range(N)]
bought = 0
total_price = 0

for i in range(N):
    if bought + stock[i] <= M:
        total_price += price[i] * stock[i]
        bought += stock[i]
    else:
        total_price += price[i] * (M - bought)
        break
print(total_price)

Solution (Python):



import sys

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

N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
a.sort()
price = [a[i][0] for i in range(N)]
stock = [a[i][1] for i in range(N)]
bought = 0
total_price = 0

for i in range(N):
    if bought + stock[i] <= M:
        total_price += price[i] * stock[i]
        bought += stock[i]
    else:
        total_price += price[i] * (M - bought)
        break
print(total_price)

Solution (Python):
def smaller(x, y):
	if x < y:
		return x
	return y

N, M = map(int, input().split())
drinks = []
for i in range(0, N):
	drinks.append(list(map(int, input().split())))
drinks.sort(key = lambda v: v[0])
sum = 0
left = M
for i in range(0, N):
	sum += drinks[i][0]*smaller(drinks[i][1], left)
	left -= smaller(drinks[i][1], left)
	if left == 0:
		break
print(sum)

Solution (Python):
def find(A,B,M):
    ans=0
    sofar=0
    C=sorted(zip(A,B))
    
    for i in range(len(C)):
        yen,num=C[i]
        if sofar+num<=M:
            ans+=yen*num
            sofar+=num
        else:
            need=M-sofar
            ans+=yen*need
            break
    return ans
A=[]
B=[]
N,M=list(map(int,input().strip().split(' ')))
for _ in range(N):
    a,b=list(map(int,input().strip().split(' ')))
    A+=[a]
    B+=[b]
print(find(A,B,M))

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

AB.sort()

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

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

Solution (Python):
def main():

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')

    
    
    
    N, M = R()
    AB = [R() for _ in range(N)]
    AB.sort()
    ans = 0
    for a, b in AB:
        if M - b > 0:
            ans += a*b
            M -= b
        else:
            ans += a*M
            M = 0
        if M == 0:
            print(ans)
            exit()
    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

Solution (Python):
class Shop:
    def __init__(self, price: int, limit: int):
        self.price = price
        self.limit = limit

    def __lt__(self, other):
        return self.price < other.price


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

shops.sort()

ret = 0
for s in shops:
    if s.limit > M:
        ret += s.price * M
        break
    ret += s.price * s.limit
    M -= s.limit

print(ret)

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
 
ab = [[int(x) for x in input().split()] for _ in range(n)]
 
ab.sort(key=lambda x: x[0])
 
count, cost, index = 0, 0, 0

for x in ab:
  bottle = x[1]
  if count + bottle < m:  
    count += bottle
    cost += ab[index][0] * bottle
    index += 1
  else:
    cost += ab[index][0] * (m - count)
    break

print(cost)

Solution (Python):

import sys
input = sys.stdin.readline
 
N, M = [int(i) for i in input().split()]
 
plices = [[int(i) for i in input().split()] for _ in range(N)]
 
plices.sort(key = lambda x:x[0])
 
m = 0
cost = 0
 
for i in range(N):
    if m < M:
      buy = min(M - m, plices[i][1])
      cost += plices[i][0] * buy
      m += buy
    else:
      break

print(cost)

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

    money = 0
    drink = 0
    for s in store:
        if drink + s[1] > m:
            money += (m - drink) * s[0]
            break
        money += s[1] * s[0]
        drink += s[1]
    print(money)


main()

Solution (Python):
from operator import itemgetter

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

AB = sorted(AB, key=itemgetter(0))

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

print(money)

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

sorted_ab = sorted(ab)
sum = 0
total_cost = 0
for a, b in sorted_ab:
    diff = m - sum
    if diff < b:
        sum += diff
        total_cost += a*diff
        break
    else:
        sum += b
        total_cost += a*b

print(total_cost)

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

total = 0
num = 0
i = 0
while num < m:
    if num + ab[i][1] <= m:
        num += ab[i][1]
        total += ab[i][0]*ab[i][1]
        i += 1
        
    elif num + ab[i][1] > m:
        total += ab[i][0]*(m - num)
        num = m
        i += 1
        
print(total)

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

shop.sort()
ans = 0
for s in shop:
    if M <= 0:
        break
    tmp = min(s[1],M)
    ans += tmp * s[0]
    M -= tmp
print(ans)

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

shop.sort()
ans = 0
for i in range(N):
    if M <= 0:
        break
    tmp = min(shop[i][1],M)
    ans += tmp * shop[i][0]
    M -= tmp
print(ans)

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

shop.sort(reverse=True)
ans = 0
while M > 0:
    a,b = shop.pop()
    tmp = min(b,M)
    ans += tmp * a
    M -= tmp
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n, m = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, input().split())) for _ in range(n))
    ans = 0
    for i in lst:
        while i[1] > 0 and m > 0:
            ans += i[0]
            i[1] -= 1
            m -= 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(N)]
 
AB.sort()
Ans = 0
for i in AB:
  if i[1] >= M:
    Ans += i[0] * M
    break
  else:
    Ans += i[0] * i[1]
    M -= i[1]
 
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()
money = 0
for a, b in drink:
    if b > M:
        money += a * M
        break
    else:
        money += a * b
        M -= b
print(money)

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

drink = sorted(drink)
money = 0
for a, b in drink:
    if b > M:
        money += a * M
        break
    else:
        money += a * b
        M -= b
print(money)

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

drink = sorted(drink)
money = 0
for a, b in drink:
    if b > M:
        money += a * M
        M = 0
    else:
        money += a * b
        M -= b
print(money)

Solution (Python):
N, M = map(int, input().split())
S = []
C = 0
cost = 0
for i in range(N):
  a, b = map(int, input().split()) 
  S.append((a, b))
S.sort()
for i in range(N):
  if S[i][1] + C < M:
    C += S[i][1]
    cost += S[i][0] * S[i][1]
  else:
    cost += (M-C) * S[i][0]
    break
print(cost)

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

C = sorted(C, key=lambda x: x[0], reverse=False)
i = 0
ans = 0
while M>0:
    if M-C[i][1]>=0:
        ans += C[i][0]*C[i][1]
        M -= C[i][1]
        i += 1
    else:
        ans += M*C[i][0]
        M = 0
print(ans)

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

sum_cost = 0
n_drink = 0
for AB in AB_list:
    A, B = AB
    if n_drink + B < M:
        n_drink += B
        sum_cost += A * B
    elif n_drink + B >= M:
        sum_cost += A * (M - n_drink)
        print(sum_cost)
        exit()

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

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

Solution (Python):
a, b = map(int, input().split())
c = [list(map(int, input().split())) for i in range(a)]
c.sort()
d = 0
e = 0
for i in range(a):
    if c[i][1] + d < b:
        e += c[i][1] * c[i][0]
        d += c[i][1]
    else:
        e += (b - d) * c[i][0]
        d += b - d
print(e)

Solution (Python):
n,k=map(int,input().split())
a=[]


for i in range(n):
    s,p=map(int,input().split())
    a+=[[s, p] ]
    

import operator as op
a.sort(key = op.itemgetter(0))

ans=0
get=0
look=0
while get<k:
    get=get+1
    a[look][1]=a[look][1]-1
    ans=ans+a[look][0]
    if a[look][1]==0:
        look=look+1

print(ans)

Solution (Python):
N,M = map(int,input().split())
table = []
for i in range(N):
    table.append(list(map(int,input().split())))
table.sort(key=lambda x: x[0])
num = 0
total = 0
i = 0
while num < M:
    num += table[i][1]
    total += table[i][1] * table[i][0]
    cost = table[i][0]
    i += 1
print(total-cost*(num-M))

Solution (Python):
N, M = map(int, input().split())
cost = 0  
numofdrink = 0  
price = [0] * N
for i in range(N):
    price[i] = [0] * 2
    price[i][0], price[i][1] = map(int, input().split())
price = sorted(price)

ite = 0
while (numofdrink < M):
    if numofdrink + price[ite][1] >= M:
        cost += (M - numofdrink) * price[ite][0]
    else:
        cost += price[ite][0] * price[ite][1]

    numofdrink += price[ite][1]
    ite += 1

print(cost)

Solution (Python):
store_num, needed_drink_num = tuple(int(i) for i in input().split(" "))
drink_list = []
for i in range(store_num):
    drink_list.append(tuple(int(i) for i in input().split(" ")))


def tuple_index_0(tup):
    return tup[0]


drink_list.sort(key=tuple_index_0)
ans = 0
for i in drink_list:
    if i[1] < needed_drink_num:
        needed_drink_num -= i[1]
        ans += i[0] * i[1]
    else:
        ans += i[0] * needed_drink_num
        print(ans)
        break

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