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())
AB = [list(map(int, input().split())) for i in range(N)]
AB.sort(key = lambda x: x[0])

total = 0
i = 0

while True:
    total += AB[i][1]
    if total < M:
        i += 1
    else:
        break

ans = 0
for j in range(i):
    ans += AB[j][0]*AB[j][1]
ans += (M - sum(AB[p][1] for p in range(0,i))) * AB[i][0]
print(ans)

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 x: x[0])
total = 0
i = 0
while True:
	total += ab[i][1]
	if total < m:
		i += 1
	else:
		break
ans = 0
for j in range(i):
	ans += ab[j][0] * ab[j][1]
ans += (m - sum(ab[p][1] for p in range(0,i))) * ab[i][0]
print(ans)

Submission status: Accepted

Solution code:
import sys;n,m=input().split();m=int(m);a=[];i=n=c=0;a=sorted(tuple(map(int,e.split()))for e in sys.stdin)
while m>0:c+=a[i][0]*a[i][1];m-=a[i][1];i+=1
print(c+m*a[~-i][0])

Submission status: Accepted

Solution code:
from collections import namedtuple

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

def main():
    n, m = map(int, input().split())
    shops = []
    for _ in range(n):
        a, b = map(int, input().split())
        shops.append(Shop(a, b))
    
    print(solve(shops, m))

def solve(shops, m):
    cost = 0
    count = 0

    for shop in sorted(shops):
        if count + shop.b >= m:
            cost += shop.a * (m - count)
            return cost
        
        count += shop.b
        cost += shop.a * shop.b

main()

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
A = {}
B = [0] * N
for i in range(N):
    A[i], B[i] = list(map(int, input().split()))
A = sorted(A.items(), key=lambda x:x[1])

ans = 0

for i in range(N):
    if M > B[A[i][0]]:
        ans += A[i][1] * B[A[i][0]]
        M -= B[A[i][0]]
    else:
        ans += A[i][1] * M
        break;
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)])
drink = 0
cost = 0

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

Submission status: Accepted

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

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

print(cost)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
drink_dict = {}
for i in range(N):
  A,B = map(int,input().split())
  if A not in drink_dict:
  	drink_dict[A] = B
  else:
    drink_dict[A] += B 
drink_list = sorted(drink_dict.items())
total_number = 0
total_price = 0
for i in drink_list:
  if total_number + i[1] >= M:
    print(total_price + (M-total_number) * i[0])
    break;
  else:
    total_price+=i[0]*i[1]
    total_number+= i[1]

Submission status: Accepted

Solution code:
n,m,*t = map(int, open(0).read().split())
ans = 0
for a, b in sorted(zip(t[::2], t[1::2])):
  ans += min(b, m)*a
  m -= b
  if m <= 0:
    print(ans)
    break

Submission status: Accepted

Solution code:
import itertools

N, M  = tuple([int(x) for x in input().split(' ')])
AB = [tuple([int(x) for x in input().split(' ')]) for i in range(N)]

ans = 0
count = 0
for a, b in sorted(AB, key=lambda x: x[0]):
        if M - count <= b:
                ans +=  (M - count) * a
                break
        else:
                count += b
                ans += a * b

print(ans)

Submission status: Accepted

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

class Shop():
    def __init__(self, yen, num):
        self.yen = yen  # ドリンクの単価
        self.num = num  # 販売している個数


def solve():
    N, M = list(map(int, input().split(" ")))
    A, B = [], []
    shops = []
    for i in range(N):
        a, b = list(map(int, input().split(" ")))
        A.append(a)
        B.append(b)
        shops.append(Shop(a, b))
    
    shops = sorted(shops, key=lambda x: x.yen)

    ans = 0
    rest_count = M
    for i in range(N):
        if rest_count <= shops[i].num:
            ans += shops[i].yen * rest_count
            break
        ans += shops[i].yen * shops[i].num
        rest_count -= shops[i].num
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ab.sort()

ans = 0

for (a, b) in ab:
    if m == 0:
        break
    for _ in range(b):
        if m == 0:
            break
        m -= 1
        ans += a

print(ans)

Submission status: Accepted

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

src.sort(key = lambda x:-x[0])
sum_pay = 0
while 1:
    a,b = src.pop()
    if m > b:
        sum_pay += a*b
        m -= b
    else:
        sum_pay += a*m
        break
print(sum_pay)

Submission status: Accepted

Solution code:
N,M=map(int,(input()).split())
List=[]
for i in range(1,N+1):
  AB=[int(j) for j in (input()).split()]
  List.append(AB)
List.sort()
S=0
P=0
k=0
while S<M:
  a=List[k][0]
  b=List[k][1]
  if S+b<=M:
    P=P+a*b
    S=S+b
  else:
    P=P+a*(M-S)
    S=M
  k=k+1
print(P)

Submission status: Accepted

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

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

num = 0
paied = 0

for AB_i in AB_sorted:
    a = AB_i[0]
    b = AB_i[1]
    
    paied += a * b
    num += b
    
    if num >= M:
        paied -= (num - M) * a
        break

print(paied)

Submission status: Accepted

Solution code:
from sys import stdin

N, M = map(int, stdin.readline().split())
arr = []
for i in range(N):
  arr.append(list(map(int, stdin.readline().split())))

arr.sort(key=lambda a: a[0])
ans = 0
index = 0
bought = 0
while True:
    p, q = arr[index]
    q = min(q, M - bought)

    ans += p * q
    bought += q

    if bought == M:
        break

    index += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
A_B=[]
for i in range(N):
  A_B.append(tuple(map(int, input().split())))
A_B.sort()
cnt = 0
m = 0
price = 0
while m < M:
  if A_B[cnt][1] > M-m:
    price += A_B[cnt][0]*(M-m)
    m = M
  else:
    price += A_B[cnt][0]*A_B[cnt][1]
    m += A_B[cnt][1]
  cnt += 1
print(price)

Submission status: Accepted

Solution code:
n,m=map(int, input().split())
shop=sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[0])

ans = 0
for (val,amo) in shop:
	if amo > m:
		ans += m * val
		break
	else:
		m -= amo
		ans += amo * val

print(ans)

Submission status: Accepted

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

# 安い順に取り扱えばいいはず
shop_info.sort(key=lambda x:x[0])

cost = 0
current_kan = 0
for info in shop_info:
    price = info[0]
    stock = info[1]
    if (current_kan + stock > M) :
        # 買占めはやりすぎなケース
        want_kan = M - current_kan 
        cost += price * want_kan
        break
    else :
        # 全部
        current_kan += stock
        cost += price * stock

print(cost)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
import numpy as np
AB = np.array([list(map(int, input().split())) for _ in range(N)])
idx = np.argsort(AB[:,0],axis=0)
AB = AB[idx]
l = []
for a,b in AB:
  l.extend([a]*b)
  if len(l) > M:
    break
print(sum(l[:M]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
a.sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if pm > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if pm > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
b = sorted(a)
pm = s = 0
m_minus_1 = m - 1
for item in b:
    tmp = pm + item[1]
    if tmp > m_minus_1:
        print(s + item[0] * (m - pm))
        break
    else:
        pm = tmp
        s += item[0] * item[1]

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    tmp = pm + item[1]
    if tmp > m_minus_1:
        print(s + item[0] * (m - pm))
        break
    else:
        pm = tmp
        s += item[0] * item[1]

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
for item in a:
    if pm + item[1] >= m:
        print(s + item[0] * (m - pm))
        break
    else:
        pm += item[1]
        s += item[0] * item[1]

Submission status: Accepted

Solution code:
from operator import itemgetter

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

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

A.sort(key=itemgetter(0))

num = 0
sum = 0

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
number = M
ans = 0
ABlist = []
for i in range(N):
    A, B = map(int,input().split())
    ABlist.append([A,B])
ABlist.sort()
for x in ABlist:
    if x[1] >= number:
        ans += x[0] * number
        break
    else:
        ans += x[0] * x[1]
        number -= x[1]
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
number = M
answer = 0
ABlist = []

for x in range(0,N):
    A,B = map(int,input().split())
    ABlist.append([A,B])

ABlist.sort()

for x in ABlist:
    if x[1] >= number:
        answer += x[0] * number
        break
    else:
        answer += x[0] * x[1]
        number -= x[1]

print(answer)

Submission status: Accepted

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

sorted_ab = sorted(ab_list)

count = 0
current_price = 0
flag = False
for sab in sorted_ab :
    if count + sab[1] <= m :
        count += sab[1]
        current_price += sab[0] * sab[1]
    else :
        for i in range(sab[1]):
            if count == m :
                flag = True
                break
            count += 1
            current_price += sab[0]
    
    if flag == True:
        break
print(current_price)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import heapq

def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    l = [(A[i],B[i]) for i in range(N)]
    heapq.heapify(l)
    cost = 0
    while True:
        next = heapq.heappop(l)
        buyCount = min(M,next[1])
        cost += buyCount * next[0]
        M-= buyCount
        if M == 0:
            break
    print(cost)
    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]" 
    B = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from operator import itemgetter

N, M = map(int, raw_input().split())
drinklist = []
for i in range(0, N):
	drinklist.append(map(int, raw_input().split()))
drinklist = sorted(drinklist, key=itemgetter(0))
sum = 0
drink = M
for i in range(0, N):
	if drink < drinklist[i][1]:
		sum += drink * drinklist[i][0]
		break
	else:
		sum += drinklist[i][1] * drinklist[i][0]
		drink -= drinklist[i][1]

print sum

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
a = []
for i in range(N):
	a.append(list(map(int, input().split())))
x = sorted(a)
cost = 0
k = 0
while x[k][1] < M:
	cost = cost +x[k][0] * x[k][1]
	M = M - x[k][1]
	k = k + 1
cost = cost + x[k][0] * M
print(cost)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
 
AB.sort()
cost = 0
for i in range(N):
    M -= AB[i][1]
    cost += AB[i][1]*AB[i][0]
    if M<=0:
        store_num = i
        break
if M<0:
    m = abs(M)
    cost -= m*AB[store_num][0]
print(cost)

Submission status: Accepted

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

AB.sort()
cost = 0
for i in range(N):
    M -= AB[i][1]
    cost += AB[i][1]*AB[i][0]
    if M<=0:
        store_num = i
        break
if M<0:
    m = abs(M)
    cost -= m*AB[store_num][0]
print(cost)

Submission status: Accepted

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

shops = []
class Shop:
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def __lt__(self, other):
        return self.a < other.a

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

shops = sorted(shops)

m = 0
idx = 0
ans = 0
while m < M:
    buy = min(M-m, shops[idx].b)
    ans += buy * shops[idx].a
    m += buy
    idx += 1
print(ans)

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

Submission status: Accepted

Solution code:
from operator import itemgetter


N, M = map(int, input().split(" "))
AB = [list(map(int, input().split(" "))) for _ in range(N)]
AB = sorted(AB, key=itemgetter(0))

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

Submission status: Accepted

Solution code:
import numpy as np
n, m = list(map(int, input().split()))

price = np.zeros(n)
limit = np.zeros(n)

for i in range(n):
    a, b = list(map(int, input().split()))
    price[i] = a
    limit[i] = b

p = 0
for i in price.argsort():
    l = limit[i]
   # print(price[i], m, l, p)

    if l < m:
        #print("T")
        p += price[i] * l
        m-=l
    else:
        #print("F")
        p += price[i] * m
        break
print(int(p))

Submission status: Accepted

Solution code:
n, m = map(int, input().strip().split(' '))
store = []
for i in range(n):
    a, b = map(int, input().strip().split(' '))
    store.append([a,b])
store.sort()
price = 0
kosuu = 0
for i in range(n):
    if kosuu + store[i][1] > m:
        price = price + store[i][0]*(m-kosuu)
        break
    else:
        price = price + store[i][0]*store[i][1]
        kosuu = kosuu + store[i][1]
print(price)

Submission status: Accepted

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

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

A.sort()
p = 0
v = 0
for a in A:
    p += a[1]
    v += a[0] * a[1]
    if p >= M:
        v -= a[0] * (p - M)
        print(v)
        exit()

Submission status: Accepted

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

drinks = sorted(drinks, key = lambda x: x[0])
ans = 0
for a, b in drinks:
    if b < m:
        ans += a*b
    elif b == m:
        ans += a*b
        break
    else:
        ans += a*m
        break
    m -= b
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
c=[[0 for i in range(2)] for i in range (n)]
for i in range(n):
    c[i][0],c[i][1] = map(int, input().split())
c.sort()
price=0
i=0
while(m>0):
    if(m>c[i][1]):
        price+=c[i][0]*c[i][1]
        m-=c[i][1]
    else:
        price+=c[i][0]*m
        m=0
    i+=1
print(price)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
from operator import itemgetter
    
n, m = map(int, input().split())

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

price = 0
count = 0

for j in range(n):
    if count == m:
        break
    for k in range(int(stores[j][1])):
        if count < m:
            price += int(stores[j][0])
            count += 1
        else:
            break

print(price)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
AB = [[int(i) for i in input().split()] for j in range(n)]
AB.sort()
res = 0
for a,b in AB:
	m -= b
	if m >= 0:
		res += a*b
	else:
		m += b
		res += a*m
		break
print(res)

Submission status: Accepted

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

remain = M
price = 0
for a,b in shops:
  if b > remain:
    price += remain*a
    remain = 0
  else:
    price += a*b
    remain -= b
  if remain == 0:
  	break
print(price)

Submission status: Accepted

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


l = [None] * N

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

l = sorted(l, key=lambda x: x[0], reverse=False)

p = 0
m = 0

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

Submission status: Accepted

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

nap = []

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

nap.sort()


cost = 0
for i in range(N):
    if M == 0:
        break
    for j in range(nap[i][1]):
        cost += nap[i][0]
        M -= 1
        if M == 0:
            break
    
    

print(cost)

Submission status: Accepted

Solution code:
from operator import itemgetter

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

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

AB.sort(key=itemgetter(0, 1))

ct = 0
money = 0
for i in AB:
    A, B = i[0], i[1]
    money += A*B
    ct += B
    if ct >= M:
        money -= (ct - M)*A
        break

print(money)

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(key=lambda x: (x[0], x[1]))

ct = 0
money = 0
for i in AB:
    A, B = i[0], i[1]
    money += A*B
    ct += B
    if ct >= M:
        money -= (ct - M)*A
        break

print(money)

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(key=lambda x: (x[0], x[1]))

ct = 0
money = 0
for i in AB:
    A, B = i[0], i[1]
    for i in range(B):
        money += A
        ct += 1
        if ct == M:
            break
    if ct == M:
        break

print(money)

Submission status: Accepted

Solution code:
n,m=[int(i) for i in input().split(' ')]
class P:
  def __init__(self, a, b):
    self.a = a
    self.b = b

p = []
for i in range(n):
  a,b=[int(i) for i in input().split(' ')]
  p.append(P(a, b))

  
mon = 0
p.sort(key=lambda x: x.a)
for e in p:
  if m <= e.b:
    mon += e.a * m
    m = 0
  else:
    m -= e.b
    mon += e.a * e.b

print(mon)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
AB.sort()
m=0
c=0
for i,x in enumerate(AB):
    m += x[1]
    c += x[0]*x[1]
    if m >= M:
        c -= (m-M)*x[0]
        print(c)
        exit()

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 x:x[0])
m=0
c=0
for i,x in enumerate(AB):
    m += x[1]
    c += x[0]*x[1]
    if m >= M:
        c -= (m-M)*x[0]
        print(c)
        exit()

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
dat = [[int(i) for i in input().split()] for i in range(n)]
dat.sort()
sum = 0
_m = 0
for i in dat:
    if _m + i[1] >= m:
        print(sum + i[0]*(m - _m))
        break
    _m += i[1]
    sum += i[0] * i[1]

Submission status: Accepted

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

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

cost = 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:
import numpy as np
N,M=[int(i) for i in input().split()]
a=[]
for j in range(N):
  a.append([int(i) for i in input().split()])
 
a.sort(key=lambda x:x[0])
 
cost=0
shop=0
for j in range(M):
  if a[shop][1]==0:
    shop+=1
  a[shop][1]-=1
  cost+=a[shop][0]
print(cost)

Submission status: Accepted

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

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)
def ans(x, y, List):
  ans = 0
  for i in range(x):
    if y > List[i][1]:
      ans = ans + List[i][0] * List[i][1]
      y = y - List[i][1]
    else:
      ans = ans + List[i][0] * y
      return(ans)
  return(ans)
print(ans(N, M, AB))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
shops = [[int(i) for i in input().split()] for i in range(N)]
from operator import itemgetter
shops = sorted(shops, key=itemgetter(0))

ans = 0
k = 0
while M > 0:
    if M >= shops[k][1]:
        ans += shops[k][0] * shops[k][1]
        M -= shops[k][1]
    elif M < shops[k][1]:
        ans += shops[k][0] * M
        M = 0
    k += 1
print(ans)

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 x:x[0])
#print(AB)

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

Submission status: Accepted

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

cost = 0
count = M
i = 0

while count > 0 :
    if count >= AB[i][1]:
        count -= AB[i][1]
        cost += AB[i][1]*AB[i][0]
        i += 1
    else:
        cost += AB[i][0]*count
        count == 0
        break
    
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())
x=np.argsort(a)
ans=0
i=0
while m>0:
    ans+=a[x[i]]*min(b[x[i]],m)
    m-=b[x[i]]
    i+=1
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
AB = np.empty((N, 2), dtype=np.int64)
for n in range(N):
    AB[n, :] = list(map(int, input().split()))
AB = AB[AB[:, 0].argsort(), :]
n = 0
ans = 0
while M > 0:
    if M > AB[n, 1]:
        ans += AB[n, 0] * AB[n, 1]
        M -= AB[n, 1]
    else:
        ans += AB[n, 0] * M
        M = 0
    n += 1
print(ans)

Submission status: Accepted

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

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

cost = 0
count = M
i = 0
while count > 0:
    if count >= nums[i][1]:
        count -= nums[i][1]
        cost += nums[i][1] * nums[i][0]
        i += 1
    else:
        cost += nums[i][0] * count
        count == 0
        break

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 = sorted(A, key=lambda x: x[0])
money = 0
i = 0
while M > 0 and i < len(A):
    if M >= A[i][1]:
        M -= A[i][1]
        money += A[i][1] * A[i][0]
        i += 1
    else:
        money += M * A[i][0]
        i += 1
        M -= M
print(money)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[0]*N
for i in range(N):
    A[i] =[int(i) for i in input().split()]
A.sort()
mysum=0
i=0
while M>0:
    if A[i][1]>0:
        A[i][1]-=1
        M-=1
        mysum+=A[i][0]
    else:
        i+=1
print(mysum)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
List = []
N, M = (int(x) for x in input().split())
for i in range(N):
    a, b = (int(x) for x in input().split())
    List.append([a, b])

num_of_drink, total_fee = 0, 0
List = sorted(List, key=lambda x:x[0])

temp_M = M
list_of_num = []
for i in range(N):
    if temp_M <= List[i][1]:
        list_of_num.append(temp_M)
        break
    else:
        list_of_num.append(List[i][1])
        temp_M += -List[i][1]
index = 0
for x in list_of_num:
    total_fee += List[index][0] * x
    index += 1
print(total_fee)

Submission status: Accepted

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

num_of_drink, total_fee = 0, 0
List = sorted(List, key=lambda x:x[0])

temp_M = M
list_of_num = []
for i in range(N):
    if temp_M <= List[i][1]:
        list_of_num.append(temp_M)
        break
    else:
        list_of_num.append(List[i][1])
        temp_M += -List[i][1]
index = 0
for x in list_of_num:
    total_fee += List[index][0] * x
    index += 1
print(total_fee)

Submission status: Accepted

Solution code:
from operator import itemgetter
N,M = map(int,input().split(' '))
tuple_drink = []
#各店舗の情報を格納
for i in range(N):
  tuple_drink.append(tuple(map(int,input().split(' '))))
tuple_drink.sort(key=itemgetter(0))
#購入したドリンクの本数
num_drink = 0
#合計金額
sum = 0
for i in range(N):
  if tuple_drink[i][1] + num_drink <= M:
      sum += tuple_drink[i][0]*tuple_drink[i][1]
      num_drink += tuple_drink[i][1]
  else:
    sum += tuple_drink[i][0]*(M-num_drink)
    num_drink += M - num_drink
    break
print(sum)

Submission status: Accepted

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

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

# A[i], B[i] はまとめてしまう
stores = [list(map(int, input().split())) for n in range(N)]

# 安さ順にソート
stores.sort(key = lambda s: s[0])

remaining = M
cost = 0

while True:
  store = stores[0]
  if store[1] >= remaining: # もしこの店で買いきれるなら
    cost += store[0] * remaining
    break
  else: #買い切れないなら
    cost += store[0] * store[1]
    remaining -= store[1]
    stores.pop(0)

print(cost)

Submission status: Accepted

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

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

ans = 0
num = 0
i = 0
while True:
    if num + ABs[i][1] >= M:
        ans += (M - num) * ABs[i][0]
        break
    ans += ABs[i][1] * ABs[i][0]
    num += ABs[i][1]
    i += 1

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
s = 0
stock = {}
for i in range(n):
    yen, s = map(int, input().split())
    if yen in stock:
        stock[yen] += s
    else:
        stock[yen] = s
        a.append(yen)
a.sort()
ans = 0
for i in range(n):
    s = stock[a[i]]
    if s < m:
        m -= s
        ans += s * a[i]
    else:
        ans += m * a[i]
        break
print(ans)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
AB = []
for i in range(N):
  ab = tuple(map(int,input().split()))
  AB.append(ab)
AB.sort()
res = 0
drnk = 0
for i in AB:
  if drnk + i[1] >= M:
    res = res + i[0]*(M-drnk)
    break
  else:
    drnk = drnk + i[1]
    res = res + i[0]*i[1]
print(res)

Submission status: Accepted

Solution code:
import heapq


def solve(priority_queue, amount):
    need_price = 0

    while amount > 0:
        price, count = heapq.heappop(priority_queue)
        need_price += price * min(amount, count)
        amount -= count

    return need_price


def main():
    n, m = map(int, input().split())

    priority_queue = list()
    for _ in range(n):
        item = list(map(int, input().split()))
        heapq.heappush(priority_queue, item)

    ans = solve(priority_queue, m)
    print(ans)


if __name__ == "__main__":
    main()