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.



Sample input:
2 5
4 9
2 4


Sample output:
12

Submission status: Accepted

Solution code:
import re
import numpy as np

np.seterr(over='raise')

N, M = [int(item) for item in re.split(r"\s", input())]

AB = []
for _ in range(N):
    AB.append([int(item) for item in re.split(r"\s", input())])

AB = np.array(AB)
A = AB[:, 0]
B = AB[:, 1]


sorted_B = B[np.argsort(A)]
sorted_A = np.sort(A)


cumB = np.cumsum(sorted_B)
index = np.argmax(cumB > M)

if N > 1 and index > 0:
    total = np.sum(sorted_A[:index] * sorted_B[:index])
    total = total + (M - cumB[index - 1]) * sorted_A[index]
else:
    total = M * sorted_A[0]

print(total)

Submission status: Accepted

Solution code:
mn,dn=map(int,input().split(" "))
ne_zai=[]
for _ in range(mn):
    ne_zai.append(list(map(int,input().split(" "))))
ne_zai.sort()
kin=0
for i in range(mn):
    if ne_zai[i][1]<dn:
        kin+=ne_zai[i][0]*ne_zai[i][1]
        dn-=ne_zai[i][1]
    else:
        kin+=dn*ne_zai[i][0]
        break
print(kin)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
bottle, cost, i = 0, 0, 0
while bottle < M:
    cost += AB[i][0] * AB[i][1]
    bottle += AB[i][1]
    i += 1

cost -= (bottle - M) * AB[i-1][0]
print(cost)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    AB = sorted([tuple(map(int, input().split()))
                 for _ in range(N)], key=lambda x: x[0])
    ans = 0
    for A, B in AB:
        ans += min(M, B) * A
        M -= B
        if 0 >= M:
            break
    print(ans)


main()

Submission status: Accepted

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

A=[0]*N
B=[0]*N

for i in range(0,N):
  tmp=list(map(int,input().split()))
  A[i]=tmp[0]
  B[i]=tmp[1]
c=zip(A,B)
c=sorted(c)
total=0
idx=0
totalcan=0
while totalcan<M:
  total+=c[idx][0]*c[idx][1]
  totalcan+=c[idx][1]
  idx+=1

while totalcan>M:
  total-=c[idx-1][0]
  totalcan-=1
print(str(total))

Submission status: Accepted

Solution code:
### coding: UTF-8
import sys

amount = 0

# 安い店から買える最大数分買う。ほしい個数を超えてる場合はほしい個数だけ
[n, m] = map(lambda x: long(x), sys.stdin.readline().split(" "))
for row in sorted([sys.stdin.readline() for _ in range(n)], key=lambda x: long(x.split(" ")[0])):
    [ai, bi] = map(lambda x: long(x), row.strip().split(" "))
    x = bi if m >= bi else m
    amount += ai * x
    m -= x
    if m == 0:
        break

print amount

Submission status: Accepted

Solution code:
### coding: UTF-8
import sys

head = sys.stdin.readline()
[n, m] = map(lambda x: long(x), head.split(" "))

amount = 0

# 店の件数分読み込み
rows = [sys.stdin.readline() for _ in range(n)]
rows.sort(key=lambda x: long(x.split(" ")[0]))
for row in rows:
    [ai, bi] = map(lambda x: long(x), row.strip().split(" "))
    # 購入可能な最大数まで買う
    x = bi if m >= bi else m
    amount += ai * x
    m -= x
    if m == 0:
        break

print amount

Submission status: Accepted

Solution code:
### coding: UTF-8
import sys

head = sys.stdin.readline()
[n, m] = head.split(" ")
n = long(n)  # 店の件数
m = long(m)  # 購入本数

amount = 0

# 店の件数分読み込み
rows = [sys.stdin.readline() for _ in range(n)]
rows.sort(key=lambda x: long(x.split(" ")[0]))
for row in rows:
    [a, b] = row.strip().split(" ")
    ai = long(a) # 金額
    bi = long(b) # 購入可能な数量
    # 購入可能な最大数まで買う
    x = bi if m >= bi else m
    for var in range(x):
        amount += ai
    m -= x
    if m == 0:
        break

print amount

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
money = 0
AB = sorted(AB, key=lambda x: x[0])
if M <= AB[0][1]:
  money += AB[0][0] * M
else:
  i = 0
  while M > 0:
    money += AB[i][0] * AB[i][1]
    M -= AB[i][1]
    if M <= 0:
      money += AB[i][0] * M
      break
    i += 1
print(money)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,M=[int(x) for x in input().strip().split()]
AN=[]
BN=[]
val={}
for i in range(N):
    Ai,Bi=[int(x) for x in input().strip().split()]
    val[Ai]=Bi+val.get(Ai,0)
    AN.append(Ai)
 
AN=sorted(set(AN))
m=0
ans=0
for Ai in AN:
    ans+=Ai*min(M-m,val[Ai])
    m+=min(M-m,val[Ai])
    if m==M:
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,M=[int(x) for x in input().strip().split()]
AN=[]
BN=[]
val={}
for i in range(N):
    Ai,Bi=[int(x) for x in input().strip().split()]
    val[Ai]=Bi+val.get(Ai,0)
    AN.append(Ai)

AN=sorted(set(AN))
m=0
ans=0
for Ai in AN:
    ans+=Ai*min(M-m,val[Ai])
    m+=min(M-m,val[Ai])
    if m==M:
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
N,M=[int(_) for _ in input().split()]
iCost = 0
for a, b in sorted([int(_) for _ in input().split()] for _ in range(N)):
    if b < M:
        iCost += a*b
        M -= b
    else:
        iCost += a*M
        print(iCost)
        exit()

Submission status: Accepted

Solution code:
n,m=list(map(int,input().split(" ")))
l=[]
l=[list(map(int,input().split(" "))) for i in range(n)]
have=0
yen=0
i=0
l.sort(key=lambda x: x[0])
while have<m:
    pri=l[i][0]
    num=l[i][1]
    i+=1
    have+=num
    yen+=pri*num
if have>m:
    yen-=(have-m)*pri
print(yen)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#abc121c

n,m=map(int,raw_input().split())
l=[]
for i in xrange(n):
 l.append(map(int,raw_input().split()))
l.sort()
b=0
s=0
for i in xrange(n):
 b+=l[i][1]
 s+=l[i][0]*l[i][1]
 if b>m:
  s-=l[i][0]*l[i][1]
  s+=l[i][0]*(l[i][1]-b+m)
  break
print s

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
plist = []
for _ in range(n):
    plist.append([int(x) for x in input().split()])

plist.sort()

cost=0
i = 0

while i < len(plist):
    tmp = plist[i]
    if tmp[1] < m:
        m -= tmp[1]
        cost += tmp[0] * tmp[1]
        i += 1
        continue
    else:
        cost += tmp[0] * m
        break
print(cost)

Submission status: Accepted

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

for _ in range(N):
  lst.append([int(x) for x in input().split()])

lst.sort()

cost = 0
i = 0

while i < len(lst):
  l = lst[i]
  if l[1] < M:
    M -= l[1]
    cost += l[0] * l[1]
    i += 1
    continue
  else:
    cost += l[0] * M
    break

print(cost)

Submission status: Accepted

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

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

cnt = 0
price = 0
i = 0
while cnt < m:
    price += ab[i][0] * ab[i][1]
    cnt += ab[i][1]
    if cnt > m:
        price -= ab[i][0] * (cnt - m)
        break
    i += 1

print(price)

Submission status: Accepted

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

Submission status: Accepted

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

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

cost = 0
remain = M
for a in Ai:
    if remain == 0:
        break
    buy_num = min(a[1], remain)
    cost += a[0] * buy_num
    remain -= buy_num
print(cost)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB_list = [list(map(int, input().split())) for i in range(N)]
AB_list.sort()
ans, cnt = 0, 0
for a, b in AB_list:
    ans += a*b
    cnt += b
    if cnt > M:
        ans = ans-(a*(cnt-M))
        break
print(ans)

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
cost = 0
amount =0
dic ={}
for _ in range(N):
    a,b = [int(i) for i in input().split()]
    if a in dic:
        dic[a] += b
    else:
        dic[a] = b

dic = sorted(dic.items())

for a,b in dic:
    if amount+b<=M:
        amount += b
        cost += a*b
    else:
        rest = M - amount
        cost += a*rest
        break

print(cost)

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
ary = [[int(i) for i in input().split()] for j in range(N)]
na = sorted(ary)
sum = 0
remain = M
for i in na:
    if remain >= i[1]:
        sum += i[1]*i[0]
        remain -= i[1]
    else:
        sum += remain * i[0]
        remain = 0
print(sum)

Submission status: Accepted

Solution code:
import numpy as np
N,M=map(int,input().split())
A=[]
 
for i in range(N):
    A+=[[i for i in map(int,input().split())]]
A=np.array(A)
A=A[A[:,0].argsort(),:]

count=0
price=0
for i in range(N):
    if A[i,1]>=M-count:
        price+=A[i,0]*(M-count)
        break
    else:
        price+=A[i,0]*A[i,1]
        count+=A[i,1]

print(price)

Submission status: Accepted

Solution code:
#Energy Drink Collector
N, M = map(int, input().strip().split())

list = [list(map(int, input().strip().split())) for i in range(N)]	
list.sort(key = lambda x : x[0])
sum = 0
cnt = 0
for a, b in list:
	if cnt + b < M:
		sum += a*b
		cnt += b
	else:
		sum += a*(M - cnt)
		break

print(sum)

Submission status: Accepted

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

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

list.sort()
sum = 0
cnt = 0

for A,B in list:
    if cnt + B >= M:
        sum += A*(M - cnt)
        cnt += M - cnt
        break
    sum += A*B
    cnt += B

print(sum)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
stores = [[int(j) for j in input().split()]for i in range(n)]
stores.sort()

drinks = 0
cost = 0
for store in stores:
    if drinks + store[1] <= m:
        drinks += store[1]
        cost += store[0]*store[1]
    elif drinks + store[1] > m:
        cost += (m - drinks)*store[0]
        break
print(cost)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
stores = []
for i in range(n):
    store = input().split()
    stores.append([int(store[i]) for i in range(2)])
stores.sort()

drinks = 0
cost = 0
for store in stores:
    if drinks + store[1] <= m:
        drinks += store[1]
        cost += store[0]*store[1]
    elif drinks + store[1] > m:
        cost += (m - drinks)*store[0]
        break
print(cost)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    N, M = list(map(int, input().split()))
    AB = []
    for _ in range(N):
        ab = list(map(int, input().split()))
        AB.append(ab)
    count = 0
    AB = sorted(AB, key=lambda ab:ab[0])

    for ab in AB:
        a = ab[0]
        for b in range(ab[1]):
            count += a
            M -= 1
            if M == 0:
                print(count)
                return 0

main()

Submission status: Accepted

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

sum = 0
remain_cnt = M
i = 0

while(True):
    if remain_cnt <= AB[i][1]:
        sum += remain_cnt * AB[i][0]
        break

    sum += AB[i][0] * AB[i][1]
    remain_cnt -= AB[i][1]
    i += 1

print(sum)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
Shop=[[int(x) for x in input().split()] for y in range(N)]
Shop=sorted(Shop,key = lambda x:x[0])

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

print(ans)

Submission status: Accepted

Solution code:
import sys


fin = sys.stdin.readline
N, M = [int(elem) for elem in fin().split()]
drinks = sorted([tuple(int(elem) for elem in fin().split()) for _ in range(N)], \
    key=lambda drink: (drink[0], -drink[1]))
assert len(drinks[0]) == 2

necessary_money = 0
num_bought_bottles = 0

for price, num_bottles in drinks:
    if num_bought_bottles + num_bottles < M:
        necessary_money += price * num_bottles
        num_bought_bottles += num_bottles
    else:
        necessary_money += price * (M - num_bought_bottles)
        break
print(necessary_money)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ab = []
for i in range(n):
        ab.append([int(j) for j in input().split()])
ab.sort()
cnt = 0
ans = 0
for i in ab:
    cnt += i[1]
    ans += i[0]*i[1]
    if cnt >= m:
        ans -= i[0]*(cnt-m)
        print(ans)
        exit()

Submission status: Accepted

Solution code:
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())

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

cost = 0
for i in range(N):
    if B[i] > M:
        cost += A[i] * M
        break
    cost += A[i] * B[i]
    M -= B[i]

print(cost)

Submission status: Accepted

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

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

AB.sort()

cost = 0
total_n = 0
i = 0

# print(AB)

while True:
    if total_n + AB[i][1] >= M:
        break
    cost += (AB[i][0] * AB[i][1])
    total_n += AB[i][1]
    # print("i:{}, cost:{}, total_n:{}".format(i, cost, total_n))
    i += 1

cost += (M - total_n) * AB[i][0]

print(cost)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
stores = list()
cost = 0

for i in range(n):
    stores.append(list(map(int,input().split())))

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

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

print(cost)

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
X = [[int(_) for _ in input().split()] for _ in range(N)]

X.sort()
ans = 0
k = M
for a, b in X:
     x = min(k, b)
     ans += a * x
     k -= x
     if k == 0:
         break
print(ans)

Submission status: Accepted

Solution code:
T=list(map(int,input().split()))
N = T[0]
M = T[1]
A = []
B = []
count = 0
num = 0
for i in range(N):
	P=list(map(int,input().split()))
	A.insert(i,P[0])
	B.insert(i,P[1])

d=zip(A,B)
d1 = sorted(d)

for a,b in d1:
	for j in range(b):
		if(count == M):
			break
		count = count + 1
		num = num + a
	else:
		continue
	break
print(num)

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
ab = np.zeros((N, 2))
for i in range(N):
    ab[i,:] = list(map(int, input().split()))
ab = ab[ab[:,0].argsort(), :]

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

print(int(cost))

Submission status: Accepted

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

#Sorted by first element
X.sort(key=lambda x: x[0])
i = 0
total = 0
ans = 0

while i <= N-1:
	total += X[i][1]
	ans += X[i][0] * X[i][1]
	if total > M:
		ans -= X[i][0] * (total-M)
		break
	i += 1
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = []
for i in range(N):
    X.append(list(map(int, input().split())))
X.sort(key=lambda x: x[0])
i = 0
num = 0
ans = 0
while i <= N - 1:
    num += X[i][1]
    ans += X[i][0] * X[i][1]
    if num > M:
        ans -= X[i][0] * (num - M)
        break
    i += 1
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ab=[]
for i in range(n):
    ab.append([int(j) for j in input().split()])
ab.sort()
cnt = 0
ans = 0
for i in ab:
    cnt += int(i[1])
    ans += int(i[0])*int(i[1])
    if cnt >= m:
        ans -= int(i[0])*(cnt-m)
        print(ans)
        break

Submission status: Accepted

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

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

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

honsuu = 0
price = 0
for i in arr:
	temp = min(i[1], m-honsuu)
	honsuu += temp
	price += i[0] * temp

print(price)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
drink = []
cost = 0
d_count = 0


for _ in range(N):
    drink.append(list(map(int, input().split())))
    
drink = sorted(drink)

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

print(cost)

Submission status: Accepted

Solution code:
def main(ab_list, m):
    ab_list.sort(key=lambda x:x[0])
    it = iter(ab_list)
    paid = 0
    buyed_drinks = 0
    while buyed_drinks < m:
        rest_num = m - buyed_drinks
        a, b = next(it)
        if rest_num <= b:
            paid += a*rest_num
            return paid
        else:
            paid += a*b
            buyed_drinks += b



if __name__ == "__main__":
    n, m = list(map(int, input().split(" ")))
    ab_list = []
    for _ in range(n):
        a, b = list(map(int, input().split(" ")))
        ab_list.append((a, b))
    print(main(ab_list, m))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a = []

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

a = sorted(a)

i = 0
num = 0
ans = 0
while i <= N - 1:
  ans += a[i][0] * a[i][1]
  num += a[i][1]
  if num > M:
    ans -= a[i][0] * (num - M)
    break
  i += 1
print(ans)

Submission status: Accepted

Solution code:
from operator import itemgetter

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


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

if N ==1:
    Z = M*a[0][0]
    print(Z)
else:
    a.sort(key = itemgetter(0))
    X = 0 
    D = 0
    var=0
    while M>D:
        D+=a[var][1]
        X+=a[var][0]*a[var][1]
        var+=1
        
    X += a[var-1][0]*(M-D)
    print(X)

Submission status: Accepted

Solution code:
N, M, *L = map(int, open(0).read().split())
ans = 0
for i, j in sorted(zip(L[::2], L[1::2])):
    ans += i * min(M, j)
    M -= j
    if M <= 0:
        print(ans)
        break

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

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

ABs.sort()
ans = 0
for a, b in ABs:
    num = min(b, M) 
    ans += num * a
    M -= num
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
shop = [list(map(int,input().split())) for _ in range(N)]
shop = sorted(shop,key=lambda x: x[0])
ans=0
for i in range(N):
    if M >= shop[i][1]:
        M = M - shop[i][1]
        ans = ans + shop[i][0] * shop[i][1]
        if M == 0:
            break
    else:
        ans = ans + shop[i][0] * M
        break
print(ans)

Submission status: Accepted

Solution code:
# refactored
n, m = map(int, input().split())
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
# a の少ない順
ab.sort(key=lambda a: a[0])
ans = 0
for i in ab:
    buy = min(m, i[1])
    ans += buy * i[0]
    m -= buy

print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
data = []
sum=0


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

data.sort()

while m>0:
    num = m if m<data[0][1] else data[0][1]
    sum+=data[0][0]*num
    m-=num
    del data[0]

print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-                                                                                 
import sys, heapq                                                                                       
# ----------------------------------------------------------------                                      
# Use Solve Function                                                                                    
                                                                                                        
def solve(lines):                                                                                       
    N,M = map(int, lines.pop(0).split(' '))                                                             
    NLIST = []                                                                                          
    for i in range(N):                                                                                  
        A,B = map(int, lines.pop(0).split(' '))                                                         
        heapq.heappush(NLIST, (A, B))                                                                   
                                                                                                        
    res = 0                                                                                             
    while(True):                                                                                        
        p = heapq.heappop(NLIST)                                                                        
        if ( p[1] >= M ):                                                                               
            res += M * p[0]                                                                             
            break                                                                                       
        else:                                                                                           
            res += p[0] * p[1]                                                                          
            M -= p[1]                                                                                   
                                                                                                        
                                                                                                        
    print(res)                                                                                          
                                                                                                        
lines = [x.strip() for x in sys.stdin.readlines()]                                                      
                                                                                                        
#                                                                                                       
# solve !!                                                                                              
#                                                                                                       
solve(lines)

Submission status: Accepted

Solution code:
n,m = (int(num) for num in input().split())
list_drink = []
for i in range(n):
    list_drink.append([int(num) for num in input().split()])
list_drink.sort(key=lambda array: array[0])

num_drink = 0
cost = 0
while m>0:
    price = list_drink[num_drink][0]
    quantity = list_drink[num_drink][1]
    if quantity < m:
        cost += price*quantity
        m -= quantity
        num_drink += 1
    else:
        cost += price*m
        m = 0
        num_drink += 1
print(cost)

Submission status: Accepted

Solution code:
import numpy as np

n, m = map(int, input().split())
a = np.zeros(n)
b = np.zeros(n)
for i in range(n):
  a[i], b[i] = map(int, input().split())
idx = np.argsort(a)
a = a[idx]
b = b[idx]
price = 0
for i in range(n):
  if m <= b[i]:
    price += m * a[i]
    m = 0
  else:
    price += b[i] * a[i]
    m -= b[i]
  if m == 0:
    break
print(int(price))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, M = map(int, input().split())
    drinks = [tuple(map(int, input().split())) for _ in range(N)]

    drinks.sort()
    ans = 0
    for drink in drinks:
        cost, num = drink
        buy_num = min(num, M)

        ans += cost * buy_num
        M -= buy_num

        if M == 0:
            break

    print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
shop = []
for i in range(n):
    shop.append(list(map(int, input().split())))
shop.sort()
i = 0
tot_b = 0
while tot_b < m:
    tot_b += shop[i][1]
    i += 1
i -= 1
if tot_b == m:
    cost = 0
    for k in range(i+1):
        cost += shop[k][0]*shop[k][1]
    print(cost)
if tot_b > m:
    cost = 0
    for k in range(i):
        cost+= shop[k][0]*shop[k][1]
    cost += shop[i][0]*(shop[i][1]+m-tot_b)
    print(cost)

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
A = []

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

dtype = [('key', int), ('value', int)]
arrayA = np.array(A, dtype=dtype)
arrayA = np.sort(arrayA, order=['key'])
cnt = 0
sum = 0
flag = 0
for j in range(0, N):
    pay = arrayA[j][0]
    num = arrayA[j][1]
    if cnt + num < M :
        cnt += num
        sum += pay*num
    else :
        diff = int(M - cnt)
        for t in range(0, diff):
            sum += pay
            cnt += 1
            if cnt == M :
                flag = 1
                print(int(sum))
                break
    if flag == 1:
        break

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = sorted([list(map(int, input().split())) for i in range(N)])
i = 0
price = 0
minprice = x[i][0]
minnumber = x[i][1]
while True:
    if minnumber >= M:
        price += minprice*M
        break
    else:
        price += minprice * minnumber
        M -= minnumber
        i += 1
        minprice = x[i][0]
        minnumber = x[i][1]
print(price)

Submission status: Accepted

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

ab.sort()

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
stores = []
for _ in range(N):
    price,stock = map(int,input().split())
    stores.append((price,stock))
stores = sorted(stores, key=lambda t: t[0])
total_cost = 0
for store in stores:
    price, stock = store[0], store[1]
    if M > stock:
        M -= stock
        total_cost += price * stock
    elif M <= stock:
        total_cost += price * M
        break
print(total_cost)

Submission status: Accepted

Solution code:
import sys
n, m,*l = map(int, sys.stdin.read().split())
#print(n,m,l)
a = 0
for p, q in sorted(zip(l[::2],l[1::2])):
  a += p*min(q,m)
  m -= q
  if m <= 0:
    print(a)
    break

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

tot = 0
i = 0
while True:
    if m == 0:
        break
    a, b = L[i]
    if m <= b:
        tot += a*m
        m = 0
    else:
        m -= b
        tot += a*b
    i += 1

print(tot)

Submission status: Accepted

Solution code:
n,m = (int(i) for i in input().split())
a = []
for i in range(n):
  tp1,tp2 = (int(i) for i in input().split())
  a.append([tp1,tp2])
drink = 0
ans = 0
a.sort()
for i in range(n):
  if drink < m:
    if drink + a[i][1] < m:
      drink += a[i][1]
      ans += a[i][0]*a[i][1]
    else:
      honsuu = m-drink
      drink += honsuu
      ans += a[i][0]*honsuu
      
  
 
print(ans)

Submission status: Accepted

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

def main():
  cost = 0
  got = 0
  for shop in shops:
    if got + shop[1] <= m:
      got += shop[1]
      cost += shop[0] * shop[1]
    else:
      cost += (m - got) * shop[0]
      return cost
  return cost
  
print(main())

Submission status: Accepted

Solution code:
def compute():
    from sys import stdin
    N, M = map(int, stdin.readline().split())
    ipt = sorted(
            [tuple(map(int, stdin.readline().split())) for _ in range(N)],
            key=lambda x: (-x[0], x[1])
          )

    out = 0
    while (M>0) and (len(ipt)>0):
      a, b = ipt.pop()
      b = min(b,M)
      out += a*b
      M -= b

    print(out)


if __name__ == "__main__":
    compute()

Submission status: Accepted

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

ans = 0

ab.sort()

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

print(ans)

Submission status: Accepted

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

shoplist= [tuple(map(int,input().split(" "))) for _ in range(n)]

shoplist.sort()


cost = 0

curr = 0


while(curr<m):

	for shop in shoplist:

		if(curr+shop[1] <= m):
			curr += shop[1]
			cost += shop[0]*shop[1]

		elif(curr+shop[1]>m):

			cost += (m-curr)*shop[0]
			curr = m

print(cost)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
L = sorted(list(map(int, input().split())) for _ in range(N))

ans = 0
cnt = 0
for A, B in L:
    if cnt + B > M:
        ans += A * (M - cnt) if cnt + B > M else B
        break
    ans += A * B
    cnt += B

print(ans)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
ShopList = [tuple(map(int, input().split())) for i in range(N)]
ShopList.sort()
Purchased = 0
Payment = 0

while Purchased < M:
    for shop in ShopList:
        if Purchased + shop[1] <= M:
            Payment += shop[0] * shop[1]
            Purchased += shop[1]
        elif Purchased + shop[1] > M:
            Payment += shop[0] * (M - Purchased)
            Purchased = M

print(Payment)