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
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
ab = [LI() for _ in range(n)]
ab.sort()
ans = 0
cnt = 0
for a,b in ab:
    if cnt + b <= m:
        cnt += b
        ans += a*b
    else:
        ans += (m-cnt)*a
        break
print(ans)

Solution (Python):
def main():
    n, m, *l = map(int, open(0).read().split())
    ab = list(zip(l[::2], l[1::2]))
    c = sorted(ab, key=lambda x: x[0])

    sum = 0
    ans = 0
    for i, j in c:
        if sum + j < m:
            sum += j
            ans += i * j
        else:
            ans += (m - sum) * i
            break
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, m, *l = map(int, open(0).read().split())
    ab = list(zip(l[::2], l[1::2]))
    ab.sort(key = lambda x: x[0])
    sum = 0
    ans = 0
    for i, j in ab:
        if sum + j < m:
            sum += j
            ans += i * j
        else:
            ans += (m - sum) * i
            break
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
ab_list = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[0])
ans = 0
for a, b in ab_list:
    if b >= m:
        print(ans + a * m)
        exit()
    else:
        ans += a * b
        m -= b

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

ans=0
while m>0:
    now=A.pop(0)
    if now[1]>m:
        ans+=now[0]*m
        m=0
    else:
        ans+=now[0]*now[1]
        m-=now[1]
print(ans)

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

Solution (Python):
n, m = map(int, input().split())
A = {}
for i in range(n):
  a, b = map(int, input().split())
  if not a in A:
    A[a] = b
  else:
    A[a] += b
ans = 0
num = 0
A = sorted(A.items())
for k, v in A:
  if num < m and m <= num+v:
    ans += (m-num)*k
    break
  else:
    ans += v*k
    num += v
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab_list = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[0])
ans = 0
for a, b in ab_list:
    if b >= m:
        print(ans + a * m)
        exit()
    else:
        ans += a * b
        m -= b

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

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

l=sorted(l,key=lambda x:x[0])
ans=0
for i in l:
    if m>i[1]:
        ans+=(i[0]*i[1])
        m-=i[1]
    else:
        ans+=(i[0]*m)
        break
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort()
ans = 0
for a, b in ab:
    buy = min(m, b)
    m -= buy
    ans += a*buy
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort()
ans = 0
for a, b in ab:
    buy = [m, b][0 <= m-b]
    m -= buy
    ans += a*buy
print(ans)

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

ans = 0
for a, b in ab:
    buy = [m, b][0 <= m-b]
    m -= buy
    ans += a*buy
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = []
for _ in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
ab.sort()
ans = 0
for a, b in ab:
    buy = [m, b][0 <= m-b]
    m -= buy
    ans += a*buy
print(ans)

Solution (Python):
from collections import defaultdict

n, m = map(int, input().split())
ls = []

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


ls.sort()

i = 0
price = 0

for num in range(n):
    i += ls[num][1]
    price += ls[num][0]*ls[num][1]
    num1 = num
    if i > m:
        break

now_cnt = i - ls[num1][1]
answer = price - ls[num1][0]*ls[num1][1] + (m - now_cnt)*ls[num1][0]

print(answer)

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

S = [[0]*2 for i in range(N)]
for i in range(N):
    S[i] = list(map(int, input().split()))
     
S = sorted(S)

quantitys = 0
total = 0

for i in range(N):
    
    
    
    
    
    
    
    need = M - quantitys

    
    
    
    if(need - S[i][1])>=0:
        
        
        
        
        quantitys += S[i][1]
        total += S[i][0] * S[i][1]

    
    
    else:
        
        
        
        quantitys += need
        total += S[i][0] * need

    
    
    if M-quantitys == 0:
        break

print(str(total))

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

S = [[0]*2 for i in range(N)]
for i in range(N):
    S[i] = list(map(int, input().split()))
     
S = sorted(S)

quantitys = 0
total = 0

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

print(str(total))

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

Solution (Python):
n,m=map(int,input().split())
l=[]
for _ in range(n):
    x,y=map(int,input().split())
    l.append([x,y])
l.sort(key=lambda x:x[0])
cnt=0
cost=0
for i in range(n):
    if cnt+l[i][1]>=m:
        diff=m-cnt
        cost+=diff*l[i][0]
        cnt+=diff
        break
    else:
        cnt+=l[i][1]
        cost+=l[i][0]*l[i][1]
print(cost)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
n, m = map(int, readline().split())
ab = map(int, read().split())
ab = sorted(zip(ab, ab))

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

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

d = []
res = 0

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

d.sort(reverse = True)

while M:
    x = d.pop()
    n = min(M,x[1])
    res += n * x[0]
    M -= n
print(res)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
n,m = map(int,input().split())
drinks = []
cost = 0
flag = True
for i in range(n):
    a,b = map(int,input().split())
    drinks.append((a,b))
drinks.sort(reverse = True)
while m:
    x = drinks.pop()
    num = min(m,x[1])
    cost += num * x[0]
    m -= num 
print(cost)

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

    AB.sort(key=lambda x: x[0])
    ans = 0
    cnt = 0
    for ab in AB:
        cnt += ab[1]
        ans += ab[0]*ab[1]
        if cnt >= m:
            ans -= ab[0]*(cnt - m)
            break

    print(ans)

if __name__ == "__main__":
    main()

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


bill=0
num=0
AB=sorted(AB, key=lambda val:val[0])

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

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

Solution (Python):
n, m = map(int, input().split())
shop = []
for _ in range(n):
    a, b = map(int, input().split())
    shop.append([a, b])
 
shop = sorted(shop) 
ans, num = 0, 0
for i in range(n):
    a, b = shop[i]
    num += b
    ans += a*b
    if num>=m:
        ans -= (num-m)*a 
        break
 
print(ans)

Solution (Python):
N,M = (int(x) for x in input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
rem = M
min_price = 0
for i in range(N):
    if rem > AB[i][1]:
        min_price += AB[i][0] * AB[i][1]
        rem -= AB[i][1]
    else:
        min_price += AB[i][0] * rem
        break
print(min_price)

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

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

Solution (Python):
class Drink(object):
    """docstring for."""

    def __init__(self,value,unit):
        self.value = value
        self.unit = unit
    def __repr__(self):
        return "%d,%d" %(self.value,self.unit)

n,m = (int(x) for x in input().split())
a = []
ans = 0;i = 0
for i in range(n):
    ai,bi = (int(x) for x in input().split())
    a.append(Drink(ai,bi))

a.sort(key=lambda x: x.value)

for i in range(n):
    mazai = a[i]
    if mazai.unit>=m:
        ans += mazai.value * m
        print(ans)
        break
    else:
        ans += mazai.unit * mazai.value
        m = m - mazai.unit

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 a:a[0])
bought = 0
price = 0
for i in range(n):
    remain = m - bought
    if a[i][1] < remain:
        bought += a[i][1]
        price += a[i][0] * a[i][1]
    else:
        price += a[i][0]*remain
        print(price)
        exit()

Solution (Python):
n,m = map(int,input().split())
li = [list(map(int,input().split())) for _ in range(n)]
li = sorted(li, key=lambda x:x[0])
cnt = 0
coin = 0
for a,b in li:
    c_cnt = cnt + b
    if c_cnt < m:
        cnt += b
        coin += a*b
    else:
        cnt += b-(c_cnt - m)
        coin += a*(b-(c_cnt-m))
        break
print(coin)

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


    
    
    
AB = sorted(AB)

A, B = [], []
for a, b in AB:
    A.append(a)
    B.append(b)

sa = [0] * (N + 1)
sb = [0] * (N + 1)
for i in range(N):
    sa[i + 1] = sa[i] + B[i]
    sb[i + 1] = sb[i] + B[i] * A[i]

from bisect import bisect_left
aim = bisect_left(sa, M)

print(sb[aim] - A[aim - 1] * (sa[aim] - M))

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


    
    
    
AB = sorted(AB)

A, B = [], []
for a, b in AB:
    A.append(a)
    B.append(b)

sa = [0] * (N + 1)
sb = [0] * (N + 1)
for i in range(N):
    sa[i + 1] = sa[i] + B[i]
    sb[i + 1] = sb[i] + B[i] * A[i]

from bisect import bisect_left
aim = bisect_left(sa, M)

print(sb[aim] - A[aim - 1] * (sa[aim] - M))

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

Solution (Python):

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

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

N, M = map(int, input().split())
h = []
for _ in range(N):
    A, B = map(int, input().split())
    heappush(h, (A, B))

count = 0
ans = 0
while M > 0:
    a, b = heappop(h)
    ans += a
    if b - 1 > 0:
        heappush(h, (a, b - 1))
    M -= 1

print(ans)

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

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

shop = sorted(shop)
ans, num = 0, 0
for i in range(n):
    a, b = shop[i]
    num += b
    ans += a*b
    if num>=m:
        ans -= (num-m)*a
        break

print(ans)

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

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

AB.sort()

cart = 0
price = 0
j = 0
for i in range(N):
    if cart + AB[i][1] > M:
        j = i
        break
    cart += AB[i][1]
    price += AB[i][0] * AB[i][1]

price += (M - cart) * AB[j][0]
print(price)

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

lis_A = []
lis_B = []

for _ in range(N):
    A, B = map(int, input().split())
    lis_A.append(A)
    lis_B.append(B)

c = zip(lis_A, lis_B)
sorted_c = sorted(c)
lis_A, lis_B = zip(*sorted_c)


cost  = 0
count  = 0

while True:
    if N == 1:
        print(lis_A[0] * lis_B[0])
        sys.exit()


    for i in range(len(lis_A)):
        if (lis_B[i] + count) <= M:
            cost = (lis_A[i]*lis_B[i]) + cost
            count = lis_B[i] + count
        else:
            cost = (((M - count) * lis_A[i]) + cost)
            print(cost)
            sys.exit()

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

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

a.sort()

c = 0
i = 0
p = 0
while c < M: 
    c += 1
    if a[i][1]:
        p += a[i][0]
        a[i][1] -= 1
    else:
        i += 1
        p += a[i][0]
        a[i][1] -= 1

print(p)

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])
money = 0

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

Solution (Python):











def resolve():
    N,M=map(int,input().split())
    L=sorted([list(map(int,input().split())) for i in range(N)],key=lambda x:x[1], reverse=True)
    L.sort()
    def main():
        cnt = 0
        val = 0

        while True:
            for i in range(N):
                cnt+=L[i][1]
                val+=L[i][0]*L[i][1]
                if cnt>M:
                    val-=(cnt-M)*L[i][0]
                    return val


    print(main())






resolve()

Solution (Python):
from collections import defaultdict

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

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

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

for k, v in drinks:
    purchases = min(v, M - cnt)
    cnt += purchases
    price += purchases * k

print(price)

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

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

print(ans)

Solution (Python):
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()
c = 0
i = 0
while m != 0:
    if ab[i][1] <= m:
        c += ab[i][0] * ab[i][1]
        m -= ab[i][1]
    else:
        c += ab[i][0] * m
        break
    if i == n - 1:
        break
    else:
        i += 1
print(c)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
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 fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
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 = sys.maxsize
mod = 10 ** 9 + 7

n, m = MAP()
ab = [LIST() for _ in range(n)]
ab.sort()
num = 0
ans = 0
for i in range(n):
    if num + ab[i][1] < m:
        num += ab[i][1]
        ans += ab[i][0] * ab[i][1]
    else:
        ans += ab[i][0] * (m - num)
        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])

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 = [[0, 0] for _ in range(n)]
for i in range(n):
    ab[i][0], ab[i][1] = map(int, input().split())
ab.sort()
c = 0
i = 0
while m != 0:
    if ab[i][1] <= m:
        c += ab[i][0] * ab[i][1]
        m -= ab[i][1]
    else:
        c += ab[i][0] * m
        break
    if i == n - 1:
        break
    else:
        i += 1
print(c)

Solution (Python):
from collections import defaultdict
N, M = map(int, input().split())
d = defaultdict(lambda: 0)
for i in range(N):
    a, b = map(int, input().split())
    d[a] += b
keys = sorted(d.keys())
t = 0
ans = 0
for k in keys:
    v = d[k]
    if t + v > M:
        ans += (M-t)*k
        break
    else:
        t += v
        ans += k*v
print(ans)

Solution (Python):
N,M=map(int,input().split())
C=[0]*N
for i in range(N):
  C[i]=tuple(map(int,input().split()))
  
C.sort(key=lambda x:x[0])
cnt=0
cost=0

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

Solution (Python):

import os
from sys import stdin
from functools import total_ordering







@total_ordering
class struct(object):
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def __lt__	(self,other):
        return self.a < other.a
    def __eq__	(self,other):
        return self.a == other.a

def main():
    
    
    input_line = stdin.readline().rstrip().split(' ')
    N, M = map(int, input_line)
    A = list()
    for _ in range(N):
        input_line = stdin.readline().rstrip().split(' ')
        a,b=map(int, input_line)
        st=struct(a,b)
        A.append(st)
    A.sort()
    ans=0
    time=0
    for x in A:
        
        if(time>M):
            continue
        if((time+x.b)<=M):
            ans += x.a*x.b
            time += x.b
        else:
            ans += x.a*(M-time)
            time = M
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):

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

ab_pairs = [list(map(int, input().split(' '))) for _ in range(N)]
ab_pairs.sort(key=lambda x: x[0])

rest = M
ret = 0
for a, b in ab_pairs:
    n = min(rest, b)
    ret += a * n
    rest -= n

print(ret)

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

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

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

print(ans)

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

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

Drinks_Fee = 0
Drinks = 0

for shop in sorted(Shops):
    if Drinks+shop[1]>=M:
        Drinks_Fee+=shop[0]*(M-Drinks)
        break
    else:
        Drinks_Fee+=shop[0]*shop[1]
        Drinks+=shop[1]
print(Drinks_Fee)

Solution (Python):
from operator import itemgetter

n,m= map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
ab = sorted(ab)
money = 0
drink = 0
for i in range(n):
         drink += ab[i][1]
         if drink > m:
                  drink -= ab[i][1]
                  break
         else:
                  money += ab[i][1] * ab[i][0]
                  continue
         break
money += (m - drink)*ab[i][0]
print(money)

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

Solution (Python):
import heapq

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

heapq.heapify(L)


drink = 0
yen = 0
while drink < M:
    a, b = heapq.heappop(L)
    if drink + b <= M:
        yen += a * b
        drink += b
    else:
        yen += a * (M - drink)
        break
print(yen)

Solution (Python):
import numpy as np
N, M = map(int, input().split())
shop = []
for _ in range(N):
    shop.append(list(map(int, input().split())))
i = 0
num = 0
ans = 0
shop = sorted(shop)
while num < M:
    ans += shop[i][0] * shop[i][1]
    num += shop[i][1]
    i += 1
ans -= (num - M) * shop[i -1][0]
print(ans)

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)
i = 0
num = 0
ans = 0
A_argsort= np.argsort(A)
while num < M:
    ans += A[A_argsort[i]] * B[A_argsort[i]]
    num += B[A_argsort[i]]
    i += 1
ans -= (num - M) * A[A_argsort[i - 1]]
print(ans)

Solution (Python):
N,M=map(int,input().split())
shop=[]
for _ in range(N):
    a=list(map(int,input().split()))
    shop.append(a)
shop.sort(key=lambda x: x[0])
cnt=0
p=0
i=0

while cnt<M:
    a=shop[i][0]
    b=shop[i][1]
    cnt+=b
    p+=a*b
    i+=1
su=cnt-M

a = shop[i-1][0]
p-=a*su
print(p)

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

AB=sorted(AB)
sum=0
answer=0
i=0
while sum<M:
    plus=min(AB[i][1],M-sum)
    sum+=plus
    answer+=plus*AB[i][0]
    i+=1

print(answer)

Solution (Python):
n ,m = map(int, input().split())
a = [0] * n
b = [0] * n
arr = [[0] * 2 for _ in range(n)]

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

arr.sort()

count = 0
cost = 0

for i in range(n):
    rem = m - count 
    cost += arr[i][0] * min(arr[i][1], rem)
    count += min(arr[i][1], rem)
    if count >= m:
        break

print(cost)

Solution (Python):
import numpy as np
N, M = map(int, input().split())

store = {}
for i in range(N):
    a, b = map(int, input().split())
    if a in store: store[a] += b
    else: store[a] = b
key = list(store.keys())
key.sort()

bins = 0
total = 0
for k in key:
    if bins + store[k] < M:
        bins += store[k]
        total += k * store[k]
    else:
        total += k * (M-bins)
        break
print(total)

Solution (Python):
n,m = map(int,input().split())
li = []
count = 0
ans = 0

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

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

for i in range(len(li)):
    if count > m:
        break
    if count + li[i][1] <= m:
        ans += li[i][0] * li[i][1]
    elif count + li[i][1] > m:
        buf = m - count
        ans += buf * li[i][0]
    count += li[i][1]

print(ans)

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

Solution (Python):




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

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

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


if __name__ == "__main__":
    main()

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

ab.sort()

ans = 0
drink = 0
i = 0

while drink < M:
    if ab[i][1] == 0:
        i += 1

    ans += ab[i][0]
    ab[i][1] -= 1
    drink += 1

print(ans)

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

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 yen,kosu in ab:
    if m==0:
        break
    x=min(kosu,m)
    m-=x
    ans+=yen*x
print(ans)

Solution (Python):


def submit():
    n, m = map(int, input().split())
    price_stocks = [tuple(map(int, input().split())) for _ in range(n)]
    price_stocks.sort(key=lambda x: x[0])

    pay = 0
    for price, stock in price_stocks:
        if stock < m:
            m -= stock
            pay += price * stock
        else:
            pay += price * m
            break

    print(pay)
            

if __name__ == "__main__":
    submit()

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

data = []

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

data.sort(key = lambda x: x[0])
buy = 0
ans = 0
for i in range(n):
    buy = min(data[i][1], m)
    ans += buy * data[i][0]
    m -= buy
    if m == 0:
        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()
cnt = 0
ans = 0
for a,b in AB:
    if cnt+b >= M:
        break
    cnt += b
    ans += a*b
ans += a*(M-cnt)
print(ans)

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

cost, num, want = 0, M, M
for i in range(N):
  num = min(want, AB[i][1])
  cost += AB[i][0] * num
  want -= num
  if num == 0:
    break
print(cost)

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(key=lambda x: x[0])
ans = 0
for ai in drink:
  ans += ai[0] * min(ai[1], m)
  m -= ai[1]
  if m <= 0:
    print(ans)
    break

Solution (Python):
N, M = map(int, input().split())
A = [0]*N
for i in range(N):
  A[i] = [0,0]
  A[i][0],A[i][1] = map(int, input().split())
A.sort()
sum = 0
for a in A:
  if a[1]<M:
    sum += a[0]*a[1]
    M -= a[1]
  else:
    sum += a[0]*M
    print(sum)
    exit()

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

i = 0 
price = 0 
for a, b in ab:
    price += a * min(b, m-i)
    i += min(b, m-i)
    if i == m:
        print(price)
        exit()

Solution (Python):
x, y= map(int, input().split())
s=[list(map(int,list(input().split()))) for i in range(x)]
k= list(s)
k.sort()
a=0
t=0
for i in range(x):
	if y-a>=k[i][1]:
		t=t+k[i][0]*k[i][1]
		a=a+k[i][1]
	else:
		t=t+((y-a)*k[i][0])
		a=y
print(t)

Solution (Python):
from collections import deque
def solve():
    N, M = map(int, input().split())
    data = []
    for i in range(N):
        a, b = map(int, input().split())
        data.append((a, b))
    
    data.sort(key=lambda x: x[0])
    data = deque(data)

    ans = 0
    cnt = 0
    while cnt < M:
        d = data.popleft()
        for i in range(d[1]):
            cnt += 1 
            ans += d[0]
            if cnt >= M:
                break
    
    print(ans)
        

if __name__ == '__main__':
    solve()