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:
I=lambda:list(map(int,input().split()))
n,m=I()
A=[]
for _ in range(n):
  a,b=I()
  A+=[[a,b]]
A.sort()
s=0
for i,j in A:
  s+=i*min(m,j)
  m-=j
  if m<=0:
    break
print(s)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
AB=[[int(j) for j in input().split()] for i in range(N)]
ans=0
AB = sorted(AB)
num, ans, i,j =0,0,0,1
while num<M:
    if j<=AB[i][1]:
        ans += AB[i][0]
        num+=1
        j+=1
    else:
        i+=1
        j=1
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int, raw_input().split())
e_list=[]
result=0
m=M

for i in range(N):
    e_list.append(map(int, raw_input().split()))

e_list.sort()

#print N,M,e_list

for i in range(N):
    cost=e_list[i][0]
    mnum=e_list[i][1]
#    print cost,mnum,":",m
    if m == 0:
#        print result
        break
    elif m >= mnum:
        result = result + cost * mnum
        m = m - mnum
    elif m < mnum:
        result = result + cost * m
        m = 0

print result

Submission status: Accepted

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

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

amount = 0
num = 0
for price, max_num in sorted_A:
  if M > 0:
    if max_num < M:
      num += max_num
      amount += price * max_num
      M -= max_num
    else:
      num += M
      amount += price * M
      M -= max_num

print(amount)

Submission status: Accepted

Solution code:
#coding: utf-8
import numpy as np
N, M = map(int, input().split())
AB = np.array([list(map(int, input().split())) for _ in range(N)])
money = 0
col_num = 0
AB = AB[np.argsort(AB[:, col_num])]
if M <= AB[np.argmin(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
        i += 1
print(money)

Submission status: Accepted

Solution code:
from pprint import pprint
N, M = map(int, input().split())
shop = []
for i in range(N):
    AB = list(map(int, input().split()))
    shop.append(AB)
shop.sort()
money = 0
for j in shop:
    A = int(j[0])
    B = int(j[1])
    if B < M:
        money += A*B
        M -= B
    elif B >= M:
        money += A*M
        break
print(money)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

N,M = list(map(int, input().split()))
A = [0] * N
B = [0] * N

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

sum = 0
ans = 0

idx = np.argsort(A)

for i in range(N):
	if(sum+B[idx[i]] >= M):
		for j in range(B[idx[i]]):
			ans += A[idx[i]]
			sum += 1
			if(sum >= M):
				break
		break
	ans += A[idx[i]]*B[idx[i]]
	sum += B[idx[i]]
print(ans)

Submission status: Accepted

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

t = 0

for ab_i in ab:
    if m == 0:
        break
    for i in range(ab_i[1]):
        if m == 0:
            break
        m -= 1
        t += ab_i[0]

print(t)

Submission status: Accepted

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

p=0
for yen,can in r:
    if can<=M:
        M-=can
        p+=yen*can
    else:
        p+=M*yen
        break
print(p)

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(key=lambda x: x[0])
ans = 0
for A, B in shop:
    if M <= B:
        ans += A * M
        break
    else:
        ans += A * B
        M -= B
print(ans)

Submission status: Accepted

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

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

result = 0
cnt = 0

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

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(key=lambda x:x[0],reverse=False)
for i in range(N):
    if M - AB[i][1] > 0:
        ans += AB[i][0]*AB[i][1]
        M -= AB[i][1]
    elif M > 0:
        ans += AB[i][0]*M
        M = 0
print(ans)

Submission status: Accepted

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

a_list = []
b_list = []

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

b_list.sort()

buy = 0
money = 0

for i in range(n):
    if buy + b_list[i][1] <= m:
        buy = buy + b_list[i][1]
        money = money + b_list[i][0] * b_list[i][1]
    else:
        need = m - buy
        money = money + b_list[i][0] * need
        break

print(money)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split(" "))
AB=["" for i in range(N)]
for i in range(N):
    A,B = map(int, input().split(" "))
    AB[i] = [A,B]
total_num=0
total_mon=0
AB = sorted(AB)
for i,t in enumerate(AB):
    add = t[1]
    total_num+=add
    if total_num>=M:
        add = add - (total_num-M)
        total_mon+=add*t[0]
        break
    total_mon+=add*t[0]
print(total_mon)

Submission status: Accepted

Solution code:
class Shop():
    def __init__(self, a, b):
        self.a = a
        self.b = b

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

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

cost = 0
bought = 0
for i in range(N):
    shop = shops[i]
    if (shop.b <= M - bought):
        cost += shop.a * shop.b
        bought += shop.b
    else:
        cost += shop.a * (M - bought)
        bought += (M - bought)
    if (bought == M):
        break

print(cost)

Submission status: Accepted

Solution code:

N,M = [int(x) for x in input().split(' ')]
A=[]
for n in range(N):
    A.append([int(x) for x in input().split(' ')])
total = 0
for price,stock in sorted(A,key= lambda x:x[0]):
    if stock <= M:
        total += stock * price
        M -= stock
    else:
        total += M * price
        break
print(total)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
c=[]
for i in range(n):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
c=list(zip(a,b))
c.sort()
a,b=zip(*c)
a=list(a)
b=list(b)
#sprint(a,b)
M=m
for i in range(n):
    if m>=0:
        m=m-b[i]
        #print(i)
        I=i
    else:
        b[i]=0

#print(b)
b[I]=b[I]-(sum(b)-M)
#print(b)

ans=0
for i in range(n):
    ans+=a[i]*b[i]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
x = [int(i) for i in input().split()]
cost = x[1]
value = 0
li = []
for i in range(x[0]):
  tmp = [int(j) for j in input().split()]
  li.append(tmp)
t = np.array(li)
index = np.lexsort([t[:,0]])
data = t[index,:]
for i in range(x[0]):
  if data[i][1] <= cost:
    value += data[i][0]*data[i][1]
    cost -= data[i][1]
  else:
    value += data[i][0]*cost
    break
print(value)

Submission status: Accepted

Solution code:
import numpy as np
def main():
    N,M = map(int, input().split())
    Aij = [list(map(int, input().split())) for i in range(N)]
    
    Aij.sort()
    
    Ai = [ Aij[i][0] for i in range(N)]
    Aj = [ Aij[i][1] for i in range(N)]
    
    a = np.cumsum(Aj)
    num = 0
    dif = 0
    for i in range(N):
        if a[i] >= M:
            num = i
            if i == 0:
                dif = M
                break
            dif = M - a[i-1]
            break

    i = 0
    gokei = 0
    for i in range(num+1):
        if i == num:
            #print(Ai[i],5555)
            gokei = gokei + Ai[i]*dif
            break
        gokei = gokei + Ai[i]*Aj[i]
        
    print(gokei)
    
    
main()

Submission status: Accepted

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

for i in range(N):
    ab.append(list(map(int, input().split())))
ab = sorted(ab)
nokori = M
for i in ab:
    if nokori - i[1] >= 0:
        ans += i[0] * i[1]
        nokori -= i[1]
    else:
        ans += i[0] * nokori
        break

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
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:
        break
    buy = min(M - m, plices[i][1])
    cost += plices[i][0] * buy
    m += buy

print(cost)

Submission status: Accepted

Solution code:
import numpy as np

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

AB = np.array(A)

AB = AB[np.argsort(AB[:,0])]

yen = 0
cnt = 0
i = 0
while(1):
    cnt_ = cnt + AB[i,1]
    if(cnt_ >= M):
        yen += (M - cnt)*AB[i,0]
        break
    yen += AB[i,0]*AB[i,1]
    cnt = cnt_
    i += 1


print(yen)

Submission status: Accepted

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

drinks = 0
cost = 0
for A, B in stores:
    if drinks == M:
        break
    num_to_buy = min(B, M - drinks)
    cost += A * num_to_buy
    drinks += num_to_buy
print(cost)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
store=[list(map(int,input().split())) for x in range(a)]
store.sort()
price=0
for x in store:
    if b>x[1]:
        b-=x[1]
        price+=x[1]*x[0]
    else:
        price+=b*x[0]
        break
print(price)

Submission status: Accepted

Solution code:
import numpy as np

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

X=np.array(X_lst)
X=X[np.argsort(X[:, 0])]

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a = [list(map(int,input().split())) for i in range(N)]
a.sort(key=lambda x:x[0])
ans = 0
nokori = M
for j in range(N):
	ans += a[j][0]*min(a[j][1],nokori)
	nokori -= a[j][1]
	if nokori <= 0:
		print(ans)
		exit()

Submission status: Accepted

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

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

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

HON = 0
MON = 0

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

print(MON)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
arr = [[0 for i in range(2)] for j in range(N)]
goukei=0
count=0
for i in range(N):
    arr[i][0],arr[i][1]=map(int,input().split())
    
    
k=0    
arr.sort()
while M-count>0:
    goukei+=arr[k][0]
    arr[k][1]-=1
    
    if arr[k][1]==0:
        k+=1
    
    count+=1

print(goukei)

Submission status: Accepted

Solution code:
N,M=[int(i) for i in input().split()]
Ai = []
Bi = []
for i in range(N):
  A,B = list(map(int, input().strip().split(' ')))
  Ai.append(A)
  Bi.append(B)
  c = zip(Ai,Bi)
C=sorted(c)
Ai,Bi=zip(*C)

n=0
p=0
r=M

while 1:
  if r<=Bi[n]:
    p += Ai[n]*r
    break
  else :
    p += Ai[n]*Bi[n]
    r -= Bi[n]
    n += 1
print(p)

Submission status: Accepted

Solution code:
N,M=[int(x) for x in input().split()]
S=[]
for i in range(N):
    a,b=[int(x) for x in input().split()]
    S.append((a,b))
S.sort()
a=0
s=0
while M>0:
    a+=S[s][0]*min(S[s][1],M)
    M-=S[s][1]
    s+=1
print(a)

Submission status: Accepted

Solution code:
from operator import itemgetter

n, m = map(int, input().split())

e = [[int(i) for i in input().split()] for i in range(n)] 

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

money = 0
mcount = m
isend = True

for i in e:
    mcount -= i[1]
    if mcount >= 0:
        money += i[0] * i[1]
    else :
        money += i[0] * (i[1] + mcount) 
        print(money)
        isend = False
        break

if isend == True:
    print(money)

Submission status: Accepted

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

class Drink:
	def __init__(self,price,qty):
		self.price=price
		self.qty=qty

drinks = []

for n in range(N):
	a,b=map(int,input().split())
	drinks.append(Drink(a,b))

count = 0
ans = 0

# greedy approach
priceSortedDrink=sorted(drinks, key=lambda d:d.price)

for d in priceSortedDrink:
	if M <= count + d.qty:
		ans = ans + d.price * (M-count)
		break
	else:
		ans += d.price * d.qty
		count += d.qty

print(ans)

Submission status: Accepted

Solution code:
N,M = list(map(int, input().split()))
data = []
for _ in range(N):
    data.append(list(map(int, input().split())))
data.sort(key=lambda d: d[0])
total = 0
for i in range(N):
    M -= data[i][1]
    total += data[i][1] * data[i][0]
    if M <= 0:
        total -= data[i][0] * -M
        break
print(total)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
for i in range(n):
    c,d=map(int,input().split())
    a.append(c)
    b.append(d)
lis=sorted(list(zip(a, b)))
ans=0
tmp=0
i=0
flag=False
while True:
    ans+=lis[i][0]*lis[i][1]
    tmp+=lis[i][1]
    if tmp>=m:
        flag=True
        ans-=(tmp-m)*lis[i][0]
        break
    i+=1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

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

#print(N)
#print(M)
#print(A)
#print(B)
x = 0
cost = 0

p = np.argsort(A)
A = A[p]
B = B[p]
#print(A)
#print(B)

i = 0
#print(A)
#print(B)

while x < M:
    # i = np.argmin(A)
        #A = np.delete(A, i)
        #B= np.delete(B, i)
    
    if x + B[i] <= M:
        cost += A[i] * B[i]
        x += B[i]
        i += 1
        #print(A)
        #print(B)
    else:
        cost += A[i] * (M-x)
        x += (M-x)
        i += 1
        #print(A)
        #print(B)


print(cost)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = []
for i in range(n):
    a.append(tuple(map(int, input().split())))
s_list = sorted(a)
x = 0
y = 0
for i in range(n):
    x += s_list[i][1]
    y += s_list[i][0] * s_list[i][1]
    if x >= m:
        y -= s_list[i][0]*(x-m)
        break
print(y)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
A,B = [],[]
for i in range(N):
    A_next, B_next = map(int,input().split())
    A.append(A_next)
    B.append(B_next)
l = sorted(list(zip(A,B)), key=lambda x:x[0])
count = 0
value = 0
i = 0
while(count<M):
    if(M-count<l[i][1]):
        value = value + l[i][0]*(M-count)
        break
    else:
        value = value + l[i][0]*l[i][1]
        count = count + l[i][1]
        i = i + 1
print(value)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [[int(j) for j in input().split()] for i in range(N)]
AB_sorted = sorted(AB)
money = 0
honsuu = 0
for i, ab in enumerate(AB_sorted):
    for j in range(ab[1]):
        money += ab[0]
        honsuu += 1
        if honsuu == M:
            print(money)
            quit()

Submission status: Accepted

Solution code:
import numpy as np
n, m = map(int, input().split())
a = []
b = []
bot = m
ans = 0
for i in range(n):
  l,m = map(int, input().split())
  a.append(l)
  b.append(m)
  
a = np.array(a)
b = np.array(b)
s = np.argsort(a)
for i in s:
  if(bot >= b[i]):
    bot -= b[i]
    ans += a[i] * b[i]
  else: 
    ans += a[i] * (bot)
    bot = 0
    break

print(ans)

Submission status: Accepted

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

a_b_list = []
for i in range(n):
    a, b = map(int, input().split())
    a_b_list.append({'yen':a, 'n_drink':b})
a_b_list.sort(key=lambda x: x['yen'])

n_leftover = m
yen_spent = 0
for i in range(n):
    n_drink = a_b_list[i]['n_drink']
    yen = a_b_list[i]['yen']
    if a_b_list[i]['n_drink'] < n_leftover:
        yen_spent += n_drink * yen
        n_leftover -= n_drink
    else:
        yen_spent += n_leftover * yen
        print(yen_spent)
        exit()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(N)]
lst = sorted(lst)
S, cnt = 0, 0
for x in lst:
    S += x[0] * min(M - cnt, x[1])
    cnt += min(M - cnt, x[1])
print(S)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import numpy as np

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

E = np.array(e)
E = E[E[:,0].argsort()]

drink = 0
money = 0
for m, i in E:
    drink += i
    money += m*i
    if drink > M:
        break

yobun = drink - M
money -= yobun * m

print(money)

Submission status: Accepted

Solution code:
N, M = [int(i) for i in raw_input().split()]
R = [tuple(int(i) for i in raw_input().split()) for i in range(N)]
R = sorted(R, key=lambda x: x[0])
money = 0
for a, b in R:
	n = min(M, b)
	M -= n
	money += n * a
print money

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


import sys
import math

# 1整数
# n = int(input())

# 空白区切り2整数
n, m = list(map(int, input().split()))

ll = []

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

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

print(sum)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
listA=[] #appendのために宣言が必要

for i in range(N):
    listA.append(list(map(int,input().split())))
listA.sort()
#print(listA)

cans = 0
money = 0
j = 0
while cans < M:
    cans += listA[j][1]
    money += listA[j][0]*listA[j][1]
    j += 1

print(money-(cans-M)*listA[j-1][0])

Submission status: Accepted

Solution code:
from collections import defaultdict

n, m = map(int, input().split())
d = defaultdict(int)
for _ in range(n):
  a_, b_ = map(int, input().split())
  d[a_] += b_

res = 0
for k in sorted(d):
  if d[k] <= m:
    res += d[k]*k
    m -= d[k]
  elif m > 0:
    res += m*k
    m = 0
  else:
    break
print(res)

Submission status: Accepted

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

mise = []
for i in range(N):
    array = list(map(int, input().strip().split(' ')))
    mise.append(array)
mise.sort()

keihi = 0

for ima in range(N):
    if M < mise[ima][1]:
        keihi += mise[ima][0] * M
        break
    else:
        M = M - mise[ima][1]
        keihi += mise[ima][0] * mise[ima][1]
    
print(keihi)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
buffer = []
count = 0
money = 0

for i in range(N):
    buffer.append(list(map(int, input().split())))
buffer.sort()
for i in range(N):
    if(count + buffer[i][1] < M):
        count += buffer[i][1]
        money += buffer[i][0] * buffer[i][1]
    else:
        money += buffer[i][0] * (M - count)
        break

print(money)

Submission status: Accepted

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

dct_a_tmp = {}
dct_b = {}
for n in range(N):
  a,b = map(int, input().split())
  dct_a_tmp[n] = a
  dct_b[n] = b

# 安い順にkeyをソート
list_a = sorted(dct_a_tmp.items(), key=lambda x: x[1])


# 安い順に買っていく
'''
1.不足本数を計算
2.不足本数と購入できる本数を比較する
a.全部購入していいようなら通常購入
b.ダメなら、購入していい本数だけ購入
'''
cnt_buied = 0
cost = 0
inf = 10 ** 18


for i in range(inf):
  want_buy = M - cnt_buied 
  
  if dct_b[list_a[i][0]] <= want_buy:
    cost += list_a[i][1] * dct_b[list_a[i][0]]
    cnt_buied += dct_b[list_a[i][0]] 
  else:
    cost += list_a[i][1] * want_buy
    cnt_buied += want_buy
    
  if cnt_buied >= M:
    break

print(cost)

Submission status: Accepted

Solution code:
N, M =  map(int, (input().split(' ')))
A_B_list = []
money = 0
while len(A_B_list) < N:
    A, B =  map(int, (input().split(' ')))
    A_B_list.append([A, B])
A_B_list = sorted(A_B_list)
    

for A, B in A_B_list:
    if B < M:
        money += A * B
        M -= B
    else:
        money += A * M
        M = 0 
    if M == 0:
        print(money)
        exit()

Submission status: Accepted

Solution code:
import numpy as np
from operator import itemgetter
N,M=(int(x) for x in input().split())
A=[]
 
for i in range(N):
  D=[int(x) for x in input().split()]
  A+=[D]    
result=A.sort(key=itemgetter(0))
count=M
sum=0
for i in range(N):
  if count-A[i][1]>0:
    count=count-A[i][1]
    sum+=A[i][0]*A[i][1]
  else:
    sum+=count*A[i][0]
    break
print(sum)

Submission status: Accepted

Solution code:
import numpy as np
a = input().split()
b = []
money = 0
for i in range(int(a[0])):
  temp = []
  temp = [int(j) for j in input().split()]
  b.append(temp)
c = np.array(b)
idex=np.lexsort([c[:,0]])
sorted_data = c[idex, :]
n = int(a[1])
for i in range(int(a[0])):
  if sorted_data[i][1] <= n:
    money = money + sorted_data[i][0] * sorted_data[i][1]
    n = n - sorted_data[i][1]
  else:
    money = money + n*sorted_data[i][0]
    break
    
print(money)

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math

import array
import bisect
import collections
from collections import Counter, defaultdict
import fractions
import heapq
import re

sys.setrecursionlimit(1000000)


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]

def argsort(l, reverse=False):
    return sorted(range(len(l)), key=lambda i: l[i], reverse=reverse)

def argmin(l):
    return l.index(min(l))

def YESNO(ans, yes="YES", no="NO"):
    print([no, yes][ans])

II = lambda: int(input())
MI = lambda: map(int, input().split())
MIL = lambda: list(MI())
MIT = lambda: tuple(MI())
MIS = lambda: input().split()



def main():
    N, M = MI()
    shops = []
    for i in range(N):
        heapq.heappush(shops, MIT())
    ans = 0
    while M > 0:
        a, b = heapq.heappop(shops)
        ans += a * min(b, M)
        M -= b
    return ans


if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
import sys
from sys import stdin
n,m = [int(x) for x in stdin.readline().rstrip().split()]
data = [stdin.readline().rstrip().split() for _ in range(n)]
data = [[int(i) for i in l] for l in data]
data.sort(key=lambda x:x[0])
cost=0
for d in data:
    for i in range(d[1]):
        cost+=d[0]
        m-=1
        if m==0:
            print(cost)
            sys.exit()

Submission status: Accepted

Solution code:
import numpy as np
n = input().split()
N = int(n[0])
M = int(n[1])

num_list = []
pr_list = []
for i in range(N):
    a = input().split()
    num_list.append(int(a[1]))
    pr_list.append(int(a[0]))

x = np.argsort(np.array(pr_list))

tmp = 0
for i in range(100000):
    key = int(num_list[x[i]])
    if M - key > 0:
        tmp += int(pr_list[x[i]])*key
        M -= key
    else:
        if i == 0:
            tmp = int(pr_list[x[i]])*M
            break
        tmp += int(pr_list[x[i]])*M
        break

print(str(tmp))

Submission status: Accepted

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

AB_list = []
ans = 0
counter = 0

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

AB_list.sort()

for i in range(M):
    ans = AB_list[0][0] + ans
    AB_list[0][1] = AB_list[0][1] - 1
    if AB_list[0][1] == 0 :
        del AB_list[0]

print(ans)

Submission status: Accepted

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

prices = []
numdict = {}

for _ in range(n):
  price, num = map(int, input().split())
  prices.append(price)
  if price in numdict:
    numdict[price] += num
  else:
    numdict[price] = num

prices_l = list(set(prices))
prices_l.sort()

result = 0
looped = 0

for mprice in prices_l:
  if numdict[mprice] <= (m - looped):
    result += mprice * numdict[mprice]
    looped += numdict[mprice]
  elif (m - looped) == 0:
    pass
  else:
    result += (m - looped) * mprice
    looped = m
  
print(result)

Submission status: Accepted

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

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

ab_list = sorted(ab_list)

res = M
ans = 0
for i in itertools.count(0):
    res -= ab_list[i][1]
    if res > 0:
        ans += ab_list[i][0] *  ab_list[i][1]
    else:
        ans += ab_list[i][0] * (ab_list[i][1] + res)
        break

print(ans)

Submission status: Accepted

Solution code:
import bisect
import numpy as np
N, M = map(int, input().split())
AB = []
for i in range(N):
  A, B = map(int, input().split())
  AB.append([A, B])
AB = np.array(AB)
arg_sort = np.argsort(AB[:, 0])
a = AB[arg_sort, 0]
b = AB[arg_sort, 1]
s = np.cumsum(b)
sect_id = bisect.bisect_left(s, M)
out = np.dot(a[:sect_id], b[:sect_id]) + (M - sum(b[:sect_id])) * a[sect_id]
out = int(out)
print(out)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = list()
B = list()
shop = []

for n in range(N):
  a,b = map(int, input().split())
  shop.append({'price' : a, 'zaiko' : b})

shop.sort(key=lambda x: (x['price'], -1*x['zaiko']))

ret = 0
cnt = 0
for s in shop:
  for i in range(s['zaiko']):
    cnt += 1
    ret += s['price']
    if M <= cnt:
      break
  if M <= cnt:
    break

print(ret)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from operator import itemgetter as ite
N,M = map(int,input().split())
alllist = []
for i in range(N):
    A,B = map(int,input().split())
    alllist.append([A,B])
alllist.sort(key=ite(0))
cost = 0
for j in range(M):
    cost += alllist[0][0]
    alllist[0][1] -= 1
    if alllist[0][1] == 0:
        del alllist[0]

print(cost)

Submission status: Accepted

Solution code:

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

ab.sort()

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


print(cost)

Submission status: Accepted

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

td = [[0 for i in range(2)] for j in range(N)]

for n in range(N):
    A, B = list(map(int, input().split()))
    td[n][0] = A
    td[n][1] = B

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

m = M
kin = 0
for t in range(len(td)):  
    kosu = min(m, td[t][1])
    kin += td[t][0] * kosu
    m -= kosu

print(kin)

Submission status: Accepted

Solution code:
import numpy as np
N , M = map(int,input().split())
A_B = []
for i in range(N):
	A_B.append(list(map(int,input().split())))
A_B = np.array(A_B).T

arg_A_B = np.argsort(A_B[0])

sort_A_B = A_B[:,arg_A_B]

money = 0
i = 0
while M > 0:
	money += sort_A_B[0][i] * sort_A_B[1][i]
	M -= sort_A_B[1][i]
	i += 1
print(money + M * sort_A_B[0][i - 1])

Submission status: Accepted

Solution code:
a = list(map(int,input().split()))
N = a[0]
M = a[1]
dct = {}
for i in range(N):
    temp = list(map(int,input().split()))
    if temp[0] in dct:
        val = dct[temp[0]] + temp[1]
        dct[temp[0]] = val
    else:
        dct[temp[0]] = temp[1]

dct = sorted(dct.items())
ruiseki = 0
loop = 0

cost = []
nums = []
ans = 0
lendct = len(dct)
for i in range(lendct):
    cost.append(dct[i][0])
    nums.append(dct[i][1])
if nums[0] >= M:
    print(str(cost[0]*M))
else:
    ans += cost[0]*nums[0]
    for i in range(1, 100001):
        nums[i] += nums[i-1]
        if nums[i] >= M:
            print(str(ans + cost[i]*(M - nums[i-1])))
            break
        ans += cost[i]*(nums[i]-nums[i-1])

Submission status: Accepted

Solution code:
N, m = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for __ in range(N)]
pay = 0

for v, n in sorted(AB):
    if m < n:
        pay += v * m
        break
    else:
        pay += v * n
        m -= n

print(pay)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
A.sort()
su = 0
mo = 0
cnt = 0
for i in range(N):
  if M <= su + A[i][1]:
    break
  else:
    su += A[i][1]
    mo += A[i][0]*A[i][1]
    cnt += 1
print(mo + (M-su)*A[cnt][0])

Submission status: Accepted

Solution code:
import numpy
n, m = map(int, input().split())
a = []
b = []

value = [] 

for i in range(n):
    ta, tb = map(int, input().split())
    value.append((ta, tb))

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

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

print(cost)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
AB = []
count = 0
sum = 0

for i in range(N):
  a,b = map(int,input().split())
  s = [a,b]
  AB.append(s)
  
AB.sort()
i = 0
while(1):
  if(count >= M):
    break  
  if((count + AB[i][1]) <= M):
   sum += AB[i][0]*AB[i][1]
   count += AB[i][1]
  else:
    rest = M - count
    sum += AB[i][0]*rest
    break
  i += 1

print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n,m = map(int, input().split())
l=[]
for i in range(0,n):
  a,b = map(int, input().split())
  l.append([a,b])
l.sort()
r=0
nn=0
for i in range(0,n):
  if nn+l[i][1]>=m:
    r=r+l[i][0]*(m-nn)
    break
  else:
    nn=nn+l[i][1]
    r=r+l[i][0]*l[i][1]

print(r)

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

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

print(ans)