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:
N, M = map(int, input().split())

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

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

money, drink_hon = 0, 0

for a_b in A_B_yasui:
    a, b = a_b[0], a_b[1]
    if drink_hon + b >= M:
        money += (M - drink_hon) * a
        drink_hon += M - drink_hon
        print(money)
        exit()
    else:
        drink_hon += b
        money += b * a

Submission status: Accepted

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

Submission status: Accepted

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

AB = sorted(AB)
cost = 0
tobuy = M
for ab in AB:
    if ab[1] < tobuy:
        cost += ab[0] * ab[1]
        tobuy = tobuy - ab[1]
    else:
        cost += ab[0] * tobuy
        tobuy = 0

    if tobuy == 0:
        break

print(cost)

Submission status: Accepted

Solution code:
li1 = list(map(int,input().split()))
cnt=0
j=0
drink=[]
for i in range(li1[0]):
    drink.append(list(map(int,input().split())))
drink=sorted(drink,key=lambda x:x[0])
while li1[1]>0:
    if li1[1]>drink[j][1]:
        li1[1]-=drink[j][1]
        cnt+=drink[j][0]*drink[j][1]
    else:
        cnt+=drink[j][0]*li1[1]
        li1[1]=0
    j+=1
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np

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

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

print(ans)

Submission status: Accepted

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

pon = 0
cost = 0
cnt = 0

while pon < M:
    for i in range(AB[cnt][1]):
        if pon < M:
            cost += AB[cnt][0]
            pon += 1
        else:
            break
    cnt += 1

print(cost)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
A=[]
for i in range(n):
    a,b=map(int,input().split())
    A.append([a,b])
A.sort()
katta=0
okane=0
while katta<m:
    
    if A[0][1]+katta<m:
        okane+=A[0][0]*A[0][1]
        katta+=A[0][1]
        A.pop(0)
    else:
        okane+=(m-katta)*A[0][0]
        break
print(okane)

Submission status: Accepted

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

shops = []
min_money = 0

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

    shops.append({'price': A, 'stock': B})

shops.sort(key=lambda x: x['price'])

for shop in shops:

    # 在庫がある
    if M < shop['stock']:
        min_money += shop['price'] * M
        break
    else:
        min_money += shop['price'] * shop['stock']
        M -= shop['stock']

print(min_money)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    # M本買う

    shop_list = []

    for n in range(N):
        price, quantity = map(int, input().split())
        shop_list.append([price, quantity])

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

    count = 0
    paid = 0
    for price, quantity in shop_list:
        count += quantity
        paid += price*quantity

        if count >= M:
            over_count = count - M
            count -= over_count
            paid -= over_count*price

    print(paid)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

import math
import copy
 
 
 
 
def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:
 
            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l
 
a=nCast(input().split());

b=nCast(inputArr1(a[0]))


def bkey(n):
    return n[0]
 
def func():
    b.sort(key=bkey)
    sum=0
    count=0
    for idx in range(0,len(b)):

        if count+b[idx][1]<a[1]:
            count+=b[idx][1]
            sum+=b[idx][0]*b[idx][1]
        else:
            sum+=b[idx][0]*(a[1]-count)
            return sum
    return sum
    
print(func())

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split(' '))
A = []
B = []
for i in range(N):
    Ai, Bi = map(int, input().split(' '))
    A.append(Ai)
    B.append(Bi)
A, B = zip(*(sorted(zip(A, B))))
A = np.array(A)
B = np.array(B)
temp_B = 0
for i, num in enumerate(B):
    temp_B += num
    if(temp_B >= M):
        break
ans = 0
ans += sum(A[:i+1]*B[:i+1])
ans -= A[i] * (temp_B - M)
print(ans)

Submission status: Accepted

Solution code:
num_shops, target_drinks = map(int, input().split())
sell = {}

for _ in range(num_shops):
    a,b = map(int, input().split())

    try:
        sell[a] += b
    except:
        sell[a] = b

ans, bought = 0, 0

for price in sorted(sell):

    selling = sell[price]

    if bought + selling < target_drinks:
        ans += price * selling    
        bought += selling
    else:
        ans += price * (target_drinks - bought)
        break

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

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

sorted_AB = sorted(AB, key=lambda x: x[0])
remaining = M
value = 0
for a, b in sorted_AB:
    for i in range(b):
        remaining -= 1
        value += a
        if remaining <= 0:
            break
    else:
        continue
    break

print(value)

Submission status: Accepted

Solution code:
import sys
DBG = not True
n,m =      map(int, input().split())
c = []
for i in range(n):
  a,b = map(int, input().split())
  c.append([a,b])

c.sort(key=lambda x: x[0])
if DBG:
    print(n)
    print(m)
    print(c)
money = 0
drink = 0
for i in range(n):
    ai = c[i][0]
    bi = c[i][1]
    if DBG:
        print("i {} d {} m {}".format(i, drink,money))
        print("ai {} bi {}".format(ai, bi))
    if drink + bi >= m:
        print(money + ai*(m-drink))
        break
    else:
        drink += bi
        money += ai * bi

Submission status: Accepted

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

res = 0
for a,b in ab:
  if m <= b:
    res += a*m
    break
  res += a*b
  m -= b

print(res)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n,m = [int(i) for i in input().split()]
a = [[int(i) for i in input().split()] for _ in range(n)]

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

#print(a)

c=0
ans = 0
i=0
while(c<m):
    if m-c <= a[i][1]:
        ans += (m-c)*a[i][0]
        c += m-c
    else:
        ans += a[i][1]*a[i][0]
        c+=a[i][1]
    i+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=[int(x) for x in input().split()]
ab=[]
for _ in [0]*n:
    ai,bi=[int(x) for x in input().split()]
    ab.append((ai,bi))
ans=0
ab=sorted(ab,key=lambda x :x[0])
for a,b in ab:
    if b>=m:
        ans+=m*a
        break
    else:
        ans+=a*b
        m-=b
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
d = {}
for _ in range(N):
    a,b = map(int,input().split())
    if not a in d.keys():
        d[a] = b
    else:
        d[a] += b
#print(d)
cost = 0
for c in sorted(d.keys()):
    #print(c,d[c])
    if M < d[c]:
        cost += c * M
        break
    else:
        cost += c * d[c]
        M -= d[c] 
print(cost)

Submission status: Accepted

Solution code:
from operator import itemgetter
n, m = [int(i) for i in input().split()]
A = []
for i in range(n):
    A.append([int(j) for j in input().split()])
A = sorted(A, key=itemgetter(0))
cur_drink = 0
cur_cost = 0
for i in range(m):
    if cur_drink < m:
        more_drink = min(m - cur_drink, A[i][1])
        cur_drink += more_drink
        cur_cost += more_drink * A[i][0]
    if cur_drink == m:
        break
print(cur_cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
AB_list = np.zeros((N, 2))

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

if N==1:
    print(int(M*AB_list[0, 0]))

elif np.sum(AB_list[:, 1]) == M:
    tmpsum = 0
    for i in range(N):
        tmpsum += AB_list[i, 0] * AB_list[i, 1]
    print(int(tmpsum))

else:
    AB_list = AB_list[np.argsort(AB_list[:, 0])]
    i = 0
    money = 0
    while(AB_list[i, 1] <= M):
        money += AB_list[i,0] * AB_list[i, 1]
        M -= AB_list[i, 1]
        i += 1

    money += AB_list[i, 0] * M
    print(int(money))

Submission status: Accepted

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

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

ab = [heapq.heappop(ab) for i in range(len(ab))]
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:
from collections import namedtuple

Store = namedtuple('Store', 'a, b')

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

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

stores.sort(key=lambda s: s.a)

m = 0
cost = 0
for store in stores:
    for i in range(store.b):
        m += 1
        cost += store.a
        if m >= M:
            print(cost)
            exit(0)

Submission status: Accepted

Solution code:
import heapq

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

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

count = 0
ans = 0
for i in range(N):
    A, B = heapq.heappop(h)
    if count + B >= M:
        ans += A * (M - count)
        break
    else:
        ans += A * B
        count += B
        
print(ans)

Submission status: Accepted

Solution code:
#C
N, M = map(int, input().split())
ShopAB = []
sapp = ShopAB.append
for _ in range(N):
    sapp(list(map(int, input().split())))
ShopAB.sort()
cost = 0
i = 0
while M > 0: # and i < N:
    price, maxquant = ShopAB[i][0], ShopAB[i][1]
    if M >= maxquant:
        cost += price * maxquant
        M -= maxquant
    else:
        cost += price * M
        M -= M
    i += 1
print(cost)

Submission status: Accepted

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

cur = 0
cur_v = 0
for a, b in shop:
    if cur + b > M:
        cur_v += (M - cur) * a
        break
    else:
        cur += b
        cur_v += a * b
print(cur_v)

Submission status: Accepted

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

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

ans=0

for a,b in A:
	if b<M:
		ans+=a*b
		M-=b
	else:
		ans+=a*M
		break
print ans

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import deque, defaultdict

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, m = LI()
    AB = [LI() for _ in range(n)]
    AB.sort()
    AB = deque(AB)

    cnt = 0
    res = 0
    while cnt < m:
        a, b = AB.popleft()
        cnt += b
        res += a * b

    if cnt > m:
        res -= a * (cnt - m)

    return res


print(main())

Submission status: Accepted

Solution code:
import random
 
def quick_sort(array):
    low_array = []
    high_array = []
    if len(array) <= 1:
        return array
    pivot = random.choice(array)[0]
    pivot_count = 0
    for a in range(len(array)):
        if pivot > array[a][0]:
            low_array.append(array[a])
        elif pivot < array[a][0]:
            high_array.append(array[a])
        else:
            pivot_count += 1
    print(pivot, low_array, high_array, pivot_count)
    low_array = quick_sort(low_array)
    high_array = quick_sort(high_array)
    return low_array + [pivot, ] * pivot_count + high_array
    #return low_array + high_array
 
def main():
    list1=input().split(' ')
    n=int(list1[0])
    m=int(list1[1])
    biglist=[]
    for i in range(n):
        list=input().split(' ')
        biglist.append([int(list[0]), int(list[1])])
    #array = [3, 4, 2, 6, 8, 1, 7]
    array=[[5,1], [2,3], [3,4], [7,2], [6,6]]
    #new_array=quick_sort(array)
    #print(new_array)
    newlist=sorted(biglist)
    #print(sorted(biglist))
    #print(newlist, m, n)
    money=0
    jun=0
    while m>0:
        if newlist[jun][1]>=m:
            money+=m*newlist[jun][0]
            print(money)
            return
        else:
            money+=newlist[jun][0]*newlist[jun][1]
            #print(money)
            m-=newlist[jun][1]
        jun+=1

main()

Submission status: Accepted

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

for i in range(n):
    ai, bi = list(map(int, input().split()))
    ab[i] = [ai, bi]
    
ab.sort(key = lambda x: x[0])
cnt = 0
yen = 0
i = 0
while True:
    if cnt + ab[i][1] >= m:
        yen += ab[i][0]*(m-cnt)
        #print(cnt, yen)
        break
    else:
        cnt += ab[i][1]
        yen += ab[i][1]*ab[i][0]
        #print(cnt, yen)
    i += 1

print(yen)

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.sort()
res = 0
ans = 0
for i in range(N):
    res += A[i][1]
    if M-res == 0:
        ans += A[i][0]*A[i][1]
        print(ans)
        break
    elif M-res > 0:
        ans += A[i][0]*A[i][1]
    else:
        res -= A[i][1]
        ans += A[i][0]*(M-res)
        print(ans)
        break

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
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())

pair_list = []
for i in range(N):
    pair_list.append((A[i],B[i]))
pair_list.sort(key=lambda tup: tup[0],reverse=False)
sum_price = 0
sum_num = 0
for price, num in pair_list:
    if(sum_num + num >= M):
        sum_price += price * (M - sum_num)
        sum_num = N
        #print(sum_price)
        break
    else:
        sum_price += price * num
        sum_num += num
        #print(sum_price)
# 出力
print(sum_price)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = []
B = []
ans = 0
for i in range(N):
    a, b = map(int, input().split())
    A.append([a, i])
    B.append(b)
A = sorted(A)
for i in range(len(A)):
    bn =  B[A[i][1]]
    if M > bn:
        M -= bn
        ans += A[i][0]  * bn
    else:
        ans += A[i][0] * M
        print(ans)
        exit()

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
list_ = []
for _ in range(N):
  list_.append([int(x) for x in input().split()])
list_.sort()

NUM = 0
PRICE = 0
for idx in range(N):
  if list_[idx][1] < (M - NUM):
    PRICE += list_[idx][0] * list_[idx][1]
    NUM += list_[idx][1]
  else:
    PRICE += list_[idx][0] * (M - NUM)
    print(PRICE)
    exit()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ab = {}
for i in range(n):
  a, b = map(int, input().split())
  if a in ab.keys():
  	ab[a] += b
  else:
    ab[a] = b

ab = sorted(ab.items(), key=lambda x: x[0])
res = 0
count = 0
for k, v in ab:
  if m - count >= v:
    count += v
    res += v * k
  else:
    res += k * (m - count)
    count += m - count
  
  if count == m:
    break
print(res)

Submission status: Accepted

Solution code:
import numpy as np
N,M = map(int,input().split())
A = np.empty(N,dtype=np.int64)
B = np.empty(N,dtype=np.int64)
for i in range(N):
    A[i],B[i] = map(np.int64, input().split())

order = A.argsort()
A = A[order]
B = B[order]
Y = 0
for i in range(N):
    if B[i] >= M:
        Y += A[i]*M
        break
    else:
        Y += A[i]*B[i]
        M -= B[i]

print(Y)

Submission status: Accepted

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

X.sort()

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

print(cost)

Submission status: Accepted

Solution code:

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

for i in range(N):
    Aap, Bap = map(int, input().split())
    listap = [Aap, Bap]
    shopList.append(listap);

cost = 0;
shopList.sort();

while(M>0):
    lowestPrice = shopList[0][0];
    if (shopList[0][1] >= M):
        cost += M*lowestPrice;
        M = 0;
    else:
        cost += shopList[0][1]*lowestPrice;
        M -= shopList[0][1];
        del shopList[0];

print(cost);

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)
N, M = map(int, input().split())
A = [list(map(int,input().split())) for i in range(N)]
A.sort()
hon_li = [a[0] for a in A]
hon = 0
pre_hon = 0
turn = 0
for i in range(N):
    pre_hon = hon
    hon += A[i][1]
    if hon >= M:
        turn = i
        break
li = [A[k][0]*A[k][1] for k in range(turn)]
amari = A[turn][0] * (M - pre_hon)
ans = sum(li) + amari
print(ans)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
A = list()
B = list()
for i in range(0, N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)
zipped = zip(A, B)
zipped_sorted = sorted(zipped, key=lambda x:x[0])
A, B = zip(*zipped_sorted)
num = 0
total = 0
i = 0
while(True):
    if num + B[i] >= M:
        total += (M - num) * A[i]
        break
    num += B[i]
    total += A[i] * B[i]
    # print(i, A[i], B[i], num, total)
    i += 1
print(total)

Submission status: Accepted

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

a.sort()

i=0
while m>0:
  if m>=a[i][1]:
    m-=a[i][1]
    ans+=a[i][0]*a[i][1]
  else:
    ans+=a[i][0]*m
    m=0
  i+=1
print(ans)

Submission status: Accepted

Solution code:
def main() -> None:
    N, M = map(int, input().split())
    l = [tuple(map(int, input().split())) for _ in range(N)]
    l.sort()
    yen = 0
    hon = M
    for v in l:
        if hon - v[1] > 0:
            hon -= v[1]
            yen += v[0] * v[1]
        elif hon - v[1] == 0:
            hon -= v[1]
            yen += v[0] * v[1]
            break
        else:
            yen += v[0] * hon
            break
    print(yen)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split(" "))
list_ = []
for i in range(N):
  a, b = map(int, input().split(" "))
  list_.append([a, b])
  
list_.sort()  
total = 0
while M > 0:
  if M >= list_[0][1]:
    M -= list_[0][1]
    total += list_[0][0] * list_[0][1]
    del list_[0]
  else:
    total += list_[0][0] * M
    M = 0
print(total)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(n)]
AB.sort()
c = 0
ans = 0
i = 0
while c < m:
  if  c + AB[i][1] <= m:
    c += AB[i][1]
    ans += AB[i][0]*AB[i][1]
    i += 1
  else:
    ans += AB[i][0]*(m-c)
    break

print(ans)

Submission status: Accepted

Solution code:
"""
C - Energy Drink Collector
"""


def solve():
    N, M = map(int, input().split())
    AB = [list(map(int, input().split())) for n in range(N)]
    AB.sort()
    ans = 0
    for a, b in AB:
        if M > b:
            ans += a * b
            M -= b
        else:
            ans += a * M
            break
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.readline


def readln():
    return list(map(int, read().split()))


N, M = readln()

A = []
B = []
data = {}
for _ in range(N):
    a, b = readln()
    A.append(a)
    B.append(b)
    data[a] = 0

for a, b in zip(A, B):
    data[a] += b

ans = 0
drink = 0
for k, v in sorted(data.items()):
    ans += k * v
    drink += v
    if drink > M:
        break
print(ans-k*(drink-M))

Submission status: Accepted

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

A=sorted(A,key=lambda x:x[0])
ko=0
pri=0
i=0
while ko<=m:
  pri+=A[i][0]*A[i][1]
  ko+=A[i][1]
  i+=1
  if ko==m:
    print(pri)
    exit()
print(pri-(ko-m)*A[i-1][0])

Submission status: Accepted

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

print(money)

Submission status: Accepted

Solution code:
data = input().split()
N = int(data[0])
M = int(data[1])

AB = []

for lop in range(N):
    data = input().split()
    AB.append( [int(data[0]),int(data[1])] )


AB = sorted(AB)

money = 0
drink = 0
for lop in range(N):
    if AB[lop][1] + drink <= M:
        drink += AB[lop][1]
        money += AB[lop][0] * AB[lop][1]
    else:
        money += AB[lop][0] * (M - drink)
        break

print(money)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
a.sort()
v = 0
for row in a:
    if m > row[1]:
        m -= row[1]
        v += row[0] * row[1]
    else:
        v += row[0] * m
        break
print(v)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
X=[[int(i) for i in input().split()] for j in range(n)]
X.sort(key=lambda X:X[0])
ans_sum=0
for i in range(n):
        for j in range(X[i][1]):
                m-=1
                ans_sum+=X[i][0]
                if m==0:
                        break
        if m==0:
                break
print(ans_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A_B=[list(map(int,input().split())) for _ in range(N)]
A_B.sort(key=lambda x:x[0])
ans=0
sum=0
for a_b in A_B:
    M-=a_b[1]
    if M<=0:
        ans+=(a_b[0]*a_b[1]+M*a_b[0])
        break
    else:
        ans+=a_b[0]*a_b[1]
print(ans)

Submission status: Accepted

Solution code:
n,m = (int(i) for i in input().split())
tuple_list = []

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

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

cost = 0
cans = 0
for t in tuple_sorted:
  if cans + t[1] < m:
    cost += t[0]*t[1]
    cans += t[1]
  else:
    buy_cans = m - cans
    cost += t[0] * buy_cans
    break

print(cost)

Submission status: Accepted

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

drinks = []

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

drinks.sort()

ans = 0

for drink in drinks:
    if m <= 0:
        break

    ans += min(m, drink[1]) * drink[0]
    m -= drink[1]

print(ans)

Submission status: Accepted

Solution code:
n, m = [int(elem) for elem in input().split()]
lst = []
for i in range(0, n):
    a, b = [int(elem) for elem in input().split()]
    lst.append((a, b))
sort_lst = sorted(lst)

cost = 0
j = 0
while m != 0:
    a, b = sort_lst[j]
    if m >= b:
        m -= b
        cost += a * b
        j += 1
    else:
        cost += a * m
        break
print(cost)

Submission status: Accepted

Solution code:
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:
    for a in range(i[1]):
        m -= 1
        if m < 0:
            break
        ans += i[0]
    
    if m < 0:
        break

print(ans)

Submission status: Accepted

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

a = sorted([tuple(map(int,input().split(' '))) for i in range(N)])
def f(a, M):
    cost = 0 
    for i in a:
        if M > i[1]:
            cost += i[0]*i[1]
            M -= i[1]
            continue
        else :
            cost += i[0]*M
            return cost 

print(f(a, M))

Submission status: Accepted

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

i = 0
while(num_of_drinks < m):
    if num_of_drinks + a[i][1] < m:
      total += a[i][0] * a[i][1]
      num_of_drinks += a[i][1]
    else:
      total += a[i][0] * (m - num_of_drinks)
      num_of_drinks = m
    i += 1
    
print(total)

Submission status: Accepted

Solution code:
N,m = map(int,input().split())
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append([a,b])

A = sorted(A)
money = 0
for n in range(N):
    if m > A[n][1]:
        m -= A[n][1]
        money += A[n][1] * A[n][0]
    elif m <= A[n][1]:
        money += m * A[n][0]
        break

print(money)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
potion=[]
for i in range(n):
    potion_R = list(map(int,input().split()))
    potion.append(potion_R)
potion.sort()
ans=0
number=m
for i in potion:
    if i[1]<=number:
        number-= i[1]
        ans+= i[0]*i[1]
    else:
        ans+= i[0]*number
        break
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(n)]
cost = 0
temp = 0
s_AB = sorted(AB)
for ab in s_AB:
    cost += ab[0]*ab[1]
    temp += ab[1]
    if(temp > m):
        cost -= ab[0]*(temp - m)
        break
    elif(temp == m):
        break
print(cost)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[[int(i) for i in input().split()]for i in range(n)]
a.sort()
q=0
t=0
for i in range(n):
    q+=a[i][1]
    if q>=m:
        t=i
        break
ans=0
z=0
for i in range(t):
    ans+=a[i][0]*a[i][1]
    z+=a[i][1]
ans+=a[t][0]*(m-z)
print(ans)

Submission status: Accepted

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

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])
can = 0
money = 0
i = 0
while can < M:
    if can + A[i][1] <= M:
        can += A[i][1]
        money += A[i][0] * A[i][1]
    else:
        money += A[i][0] * (M - can)
        can = M
    i += 1

print(money)

Submission status: Accepted

Solution code:
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])   #値段でソート
ans = 0
for p in data:
    ans += p[0] * min(m, p[1])
    m -= p[1]
    if m <= 0:
        break

print(ans)

Submission status: Accepted

Solution code:
import sys


def main():
    line = sys.stdin.readline()
    lst = [ int(s) for s in line.split()]
    n = lst[0]
    m = lst[1]

    shop_lst = []

    for i in range(n):
        line = sys.stdin.readline()
        lst = [ int(s) for s in line.split()]
        a = lst[0]
        b = lst[1]
        p = (a, b)

        shop_lst.append(p)

    sorted_lst = sorted(shop_lst, key=lambda p: p[0])

    total_cost = 0
    bottles = 0

    for a, b in sorted_lst:
        if bottles + b < m:
            # 全部買っても足りないなら、全部買って次へ
            total_cost += a * b
            bottles += b
            continue
        elif bottles + b == m:
            # ぴったりm本なら、そこでおしまい
            total_cost += a * b
            bottles += b
            break
        else:
            # あまっちゃう場合 → (m - 今の本数) だけ買う
            total_cost += a * (m - bottles)
            bottles += (m - bottles)
            break

    print(total_cost)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

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

b.sort()

su = 0
for i in range(len(b)):
    if b[i][1] >= m:
        su += b[i][0]*m
        m = 0
    else:
        su += b[i][0]*b[i][1]
        m -= b[i][1]

    if m == 0:
        break

print(su)