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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
shops = []
for _ in range(N):
    a, b = map(int, input().split())
    shops.append((a, b))
shops = sorted(shops, key=lambda s: s[0])
m = 0
total = 0
for a, b in shops:
    if m + b >= M:
        total += (M - m) * a
        break
    else:
        total += a * b
        m += b
print(total)

Submission status: Accepted

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

total = 0
for shop in shops:
    if M != 0:
        if shop[1] <= M:
            total += shop[0] * shop[1]
            M -= shop[1]
        else:
            total += shop[0] * M
            M = 0
    else:
        break

print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
a = []
for i in range(N):
    a += [list(map(int, input().split()))]
a.sort(key=lambda x: x[0])
i, ans = 0, 0
while M != 0 or i < N:
    if M > a[i][1]:
        M -= a[i][1]
        ans += a[i][0]*a[i][1]
    else:
        ans += a[i][0]*M
        M = 0
        break
    i += 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()
ans = 0
for i in range(n):
    if m >= ab[i][1]:
        m -= ab[i][1]
        ans += ab[i][0] * ab[i][1]
    else:
        ans += ab[i][0] * m
        break
print(ans)

Submission status: Accepted

Solution code:
def main():
    N,M = map(int, input().split())
    AB = []
    for i in range(N):
        A,B =  map(int, input().split())
        AB.append((A,B))
    AB.sort()
    coin = 0

    for i in range(N):
        temp = min(M,AB[i][1])
        coin += temp * AB[i][0]
        M -= temp
        if M==0:
            break


    print(coin)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
L=list()
R=list()
T=0
N,M=map(int,input().split())
for i in range(N):
  A,B=map(int,input().split())
  L.append(A)
  R.append(B)
c = zip(L, R)
c=sorted(c)
L, R = zip(*c)
L=list(reversed(L))
R=list(reversed(R))
while True:
  c=L[-1]
  T+=c*min(M,R[-1])
  M-=min(M,R[-1])
  L.pop(-1)
  R.pop(-1)
  if M==0:
    break
print(T)

Submission status: Accepted

Solution code:
from operator import itemgetter
n, m = map(int, input().split())
lst = []
for i in range(n):
  lst.append(tuple(map(int, input().split())))
lst.sort(key=itemgetter(0))
res = 0
for i in range(n):
  if lst[i][1] <= m:
    m -= lst[i][1]
    res += (lst[i][0] * lst[i][1])
  else:
    res += (lst[i][0] * m)
    break
print(res)

Submission status: Accepted

Solution code:
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,m=MI()
lis=[LI() for i in range(n)]
lis.sort()
#print(lis)
price=0
i=0
while m>0:
    price+=lis[i][0]*min(m,lis[i][1])
    m-=min(m,lis[i][1])
    #print(price)
    i+=1
print(price)

Submission status: Accepted

Solution code:
from math import ceil
def solve():
    ans = 0
    N, M = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(N)]
    A.sort()
    for i in range(N):
        if M<=A[i][1]:
            ans += M*A[i][0]
            break
        else:
            ans += A[i][0]*A[i][1]
            M -= A[i][1]
    return ans
print(solve())

Submission status: Accepted

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

Submission status: Accepted

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

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

C.sort()

tmp = M
ans = 0
for cost,num in C:
    if tmp - num >= 0:##全部買う
        tmp -= num
        ans += num*cost
    else:
        ans += cost*tmp
        break

print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
ans = 0
tot = 0
for i in range(N):
    a,b = A[i]
    if tot+b<M:
        tot += b
        ans += a*b
    else:
        n = M-tot
        ans += a*n
        break
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N, M = inm()

prices = []
for i in range(N):
    a, b = inm()
    prices.append((a, b))
prices.sort()


def solve():
    cnt = 0
    amount = 0
    for a, b in prices:
        x = min(b, M - cnt)
        amount += x * a
        cnt += x
        if cnt >= M:
            break

    return amount


print(solve())

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
ans = 0
for a,b in ab:
    if b <= m:
        ans += a*b
        m -= b
    elif b > m:
        ans += m * a
        break
print(ans)

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

ans = 0
for i in range(m):
    a, b = AB[i]
    if b >= m:
        ans += a*m
        break
    else:
        ans += a*b
        m -= b
print(ans)

Submission status: Accepted

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

i = 0
ans = 0
while True:
    if store[i][1] < m:
        ans += store[i][0]*store[i][1]
        m -= store[i][1]
        i += 1
    else:
        ans += m*store[i][0]
        break
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
from itertools import accumulate

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

cnt = 0
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
ans = 0
index = 0
C = sorted(AB,key = lambda x:x[0])
while M > 0:
    if C[index][1] < M:
        ans += C[index][1]*C[index][0]
        M -= C[index][1]
        index += 1
    else:
        ans += M*C[index][0]
        M = 0
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ab = sorted([tuple(map(int, input().split())) for i in range(n)])
i, ans = 0, 0
while ab[i][1] < m:
    m -= ab[i][1]
    ans += ab[i][1] * ab[i][0]
    i += 1
ans += ab[i][0] * m
print(ans)

Submission status: Accepted

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

info.sort(key=lambda x:x[0])
ans=0
for a, b in info:
  if M>=b:
    ans+=a*b
    M-=b
  elif M<b and M>0:
    ans+=a*M
    break
  else:
    break
print(ans)

Submission status: Accepted

Solution code:
## C - Energy Drink Collector
N, M = map(int, input().split())
val = []
for n in range(N):
    val.append( list(map(int, input().split())) )
val = sorted(val, reverse=False, key=lambda x: x[0])
ans = 0
for n in range(N):
    if val[n][1] >= M:
        ans += val[n][0] * M
        break
    else:
        ans += val[n][0] * val[n][1]
        M -= val[n][1]
print(ans)

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline
N,M = (int(i) for i in input().split())
A=[0]*N
B=[0]*N
for i in range(N):
    a,b = (int(i) for i in input().split())
    A[i] =(a,b)

#　1本が安い順にソート、安店から順番に買う
A.sort()
num_sum =0
price_sum =0
for i in range(N):
    price,num =A[i]
    if num_sum < M -num:
        num_sum  +=num
        price_sum += price *num
    elif num_sum == M -num:
        price_sum +=price *num
        break
    else:
        price_sum +=price*(M -num_sum)
        break
print(price_sum)

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()
count = 0
money = 0
i = 0
while count != M:
    money += A_B[i][0] * min(M-count, A_B[i][1])
    count += min(M-count, A_B[i][1])
    i += 1
print(money)

Submission status: Accepted

Solution code:
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,m = readInts()
ab = [readInts() for i in range(n)]
ab.sort()
i = 0
ans = 0
while m>0:
	if m>=ab[i][1]:
		m-=ab[i][1]
		ans+=ab[i][0]*ab[i][1]
		i+=1
	else:
		ans += m*ab[i][0]
		m = 0
print(ans)

Submission status: Accepted

Solution code:
# 安いお店から買えるだけ買う
n,m = list(map(int, input().split()))
ab = sorted([tuple(map(int, input().split())) for _ in range(n)])

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

print(ans)

Submission status: Accepted

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

l.sort()
count = 0
i = 0
while(M > 0):
    M -= l[i][1]
    count += l[i][0]*l[i][1]
    if(M < 0):
        count -= abs(M)*l[i][0]
    i += 1
print(count)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
AB = [LIST() for _ in range(N)]

ans = 0
cnt = 0
AB = sorted(AB, reverse=True)
while cnt < M:
    A, B = AB[-1]
    if cnt+B <= M:
        cnt += B
        ans += A*B
    else:
        ans += A * (M-cnt)
        cnt = M
    AB.pop()
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
shops = [list(map(int,input().split())) for i in range(N)]
shops.sort()
get = 0
money = 0
for i in range(0,N,1):
    if get == M:
        break
    tmp_buy = min(M-get,shops[i][1])
    get += tmp_buy
    money += tmp_buy*(shops[i][0])
print(money)

Submission status: Accepted

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

# print(x)
x.sort()
sum = 0
yen = 0
for i in range(n):
    sum += x[i][1]
    yen += x[i][0] * x[i][1]
    if sum >= m:
        yen -= (sum-m)*x[i][0]
        break

print(yen)

Submission status: Accepted

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

drinks=M
cost = 0
for i in D:
    if drinks > i[1]:
        drinks -= i[1]
        cost += i[1] * i[0]
    else:
        cost += (drinks * i[0])
        break

print(cost)

Submission status: Accepted

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

D.sort()
ans = 0

while M > 0:
    for i in D:
        if i[1] >= M:
            ans += M*i[0]
            M = 0
        else:
            ans += i[1]*i[0]
            M -= i[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 = sorted(AB, key=lambda x:x[0])
ans = 0
for a, b in AB:
    if b <= M:
       ans += a * b
       M -= b
    else:
        ans += a * M
        break
    if M == 0:
        break
print(ans)

Submission status: Accepted

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

s = 0
for a, b in x:
  if m > b:
    s += a * b
    m += -b
  else:
    s += a * m
    break

print(s)

Submission status: Accepted

Solution code:
import collections
N,M = map(int,input().split())
l=[]
for _ in range(N):
    a,b = map(int,input().split())
    l.append((a,b))
l.sort()
L = collections.deque(l)
ans = 0
while M>0:
    a,b = L.popleft()
    if M>=b:
        ans += a*b
    else:
        ans +=a*M
    M-=b
print(ans)

Submission status: Accepted

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

drink.sort()
#print(drink)
money=0
number=0
for i in range(n):
    if number+drink[i][1]<=m:
        money+=drink[i][0]*drink[i][1]
        number+=drink[i][1]
    elif m<number+drink[i][1]:
        money+=drink[i][0]*(m-number)
        number+=m-number
        break
    if m==number:
        break
print(money)

Submission status: Accepted

Solution code:
def readinput():
    n,m=list(map(int,input().split()))
    #a=[]
    #b=[]
    ab=[]
    for _ in range(n):
        aa,bb=list(map(int,input().split()))
        #a.append(aa)
        #b.append(bb)
        ab.append((aa,bb))

    return n,m,ab

def main(n,m,ab):
    ab2=sorted(ab)
    price=0
    bought=0
    for i in range(n):
        a,b=ab2[i]
        if(bought+b>=m):
            buy=m-bought
            price+=a*buy
            return price
        bought+=b
        price+=a*b

if __name__=='__main__':
    n,m,ab=readinput()
    ans=main(n,m,ab)
    print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
l=sorted(list(tuple(map(int,input().split()))for i in range(N)),key=lambda x:x[0])
c=0
ans=0
for v,k in l:
    ans+=min(k,M-c)*v
    c+=min(k,M-c)
    if c==M:
        print(ans);exit()

Submission status: Accepted

Solution code:
# Problem C - Energy Drink Collector
# input
N, M = map(int, input().split())
ab_list = []
for i in range(N):
    a, b = map(int, input().split())
    ab_list.append([a, b])

# initialization
nokori = M
ab_list = sorted(ab_list, key= lambda x: x[0])
ans = 0

# count
i = 0
while nokori>0:
    ab = ab_list[i]
    a = ab[0]
    b = ab[1]
    if b<=nokori:
        ans += a * b
        nokori -= b
    else:
        ans += a * nokori
        break
    i += 1

# output
print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions
from operator import itemgetter
import itertools
import copy


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

n, m = map(int, input().split())
#print('m', m)
tmp = [None]*n
for i in range(n):
    tmp[i] = [None]*2
for i in range(n):
    tmp[i] = readints()
# print(tmp)
tmp_so = sorted(tmp, key=lambda x: x[0])
# print(tmp_so)
cnt = 0
cost = 0
for i in range(n):
    if tmp_so[i][1] <= m:
        m -= tmp_so[i][1]

        cost += tmp_so[i][1]*tmp_so[i][0]
    elif m <= tmp_so[i][1]:

        cost += m*tmp_so[i][0]
        m = 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(key=lambda x: x[0])
ans=0
for a,b in ab:
  if m>=b:
    m-=b
    ans+=b*a
  else:
    ans+=a*m
    break
print(ans)

Submission status: Accepted

Solution code:
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,m=MI()
lis=[LI() for i in range(n)]
lis.sort(key=lambda x:x[0])
#print(lis)
j=0
ans=0
i=0
while j<n and i<m:
    #print(i,j,ans)
    if lis[j][1]>0:
        lis[j][1]-=1
        ans+=lis[j][0]
        i+=1
    else:
        j+=1
print(ans)

Submission status: Accepted

Solution code:
import sys

N, M = map(int, input().split())
AB = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(N)]

AB.sort()

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

Submission status: Accepted

Solution code:
N, M, *A = map(int, open(0).read().split())
*A, = zip(*[iter(A)]*2)
A.sort()

sun = 0
cost = 0
for a, b in A:
    if sun + b <= M:
        sun += b
        cost += a*b
        if sun == M:
            print(cost)
            break
    else:
        cost += (M-sun)*a
        print(cost)
        break

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt = 0
cost = 0
for i in range(N):
    a,b = A[i]
    if cnt+b<=M:
        cnt += b
        cost += a*b
    else:
        d = M-cnt
        cost += a*d
        break
print(cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
costs=[]
for i in range(n):
  a,b=map(int,input().split())
  costs.append([a,b])
costs=sorted(costs, key=lambda student: student[0])
pointer=0
ans=0
while m>0:
  costs[pointer][1]-=1
  ans+=costs[pointer][0]
  m-=1
  if costs[pointer][1]==0:
    pointer+=1
print(ans)

Submission status: Accepted

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

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda t: t[0])
ans = 0

for A, B in AB:
    if M-B>=0:
        ans += A*B
        M -= B
    else:
        ans += A*M
        break

print(ans)

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.sort()
ans = 0
for a, b in AB:
    if b <= m:
        ans += a*b
        m -= b
    else:
        ans += a*m
        m = 0
    if m == 0:
        break
print(ans)

Submission status: Accepted

Solution code:

import collections
n,m = map(int,raw_input().split(' '))

costs = [map(int, raw_input().split(' ')) for _ in range(n)]
costs.sort(key = lambda x:-x[0])
q = collections.deque(costs)
r = 0
while(m):
	u,v = q.pop()
	t = min(v,m)
	m -=t
	r += t * u
print r

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
cnt = 0
ans = 0
i = 0
ab.sort()
while cnt < M:
    tmp = ab[i][1]
    prc = ab[i][0]
    ans += prc * tmp
    cnt += tmp
    i += 1
print(ans - (cnt-M)*prc)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(n)]
a.sort()
sm=0
c=0
for l in a:
  if l[1]<m-c:
    c+=l[1]
    sm+=l[1]*l[0]
  else:
    sm+=(m-c)*l[0]
    break
print(sm)

Submission status: Accepted

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

    ABs = sorted(ABs)
    Count = 0
    ans = 0
    for i in range(n):
        if Count == m:
            break
        if ABs[i][1] + Count <= m:
            ans += ABs[i][0] * ABs[i][1]
            Count += ABs[i][1]
        else:
            o = m - Count
            ans += ABs[i][0] * o
            Count += o

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n,m=map(int,input().split())
    s=[tuple(map(int,input().split())) for _ in range(n)]
    s.sort(key=lambda x: x[0])
    bought = 0
    cost = 0
    for yen, ko in s:
        cost += ko*yen
        bought += ko
        #print(cost, bought, m)
        if bought >= m:
            cost -= (bought-m)*yen
            break
    print(cost)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
d = [tuple(map(int, input().split())) for _ in range(n)]
d.sort()
ans = 0
for ai, bi in d:
    if m <= bi:
        ans += ai * m
        break
    ans += ai * bi
    m -= bi
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

input_n = lambda: int(readline())
input_nn = lambda: map(int, readline().split())
input_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N, M = input_nn()
    AB = sorted([list(input_nn()) for _ in range(N)])

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, m = map(int, input().split())
v = []
ans = 0
for _ in range(n):
    a, b = map(int, input().split())
    v += [(a, b)]
v.sort(key=lambda x: x[0])
for i in v:
    m -= i[1]
    if m >= 0:
        ans += i[0]*i[1]
    else:
        m += i[1]
        ans += m*i[0]
        break
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n,m=map(int,input().split())

price_list=[0]*n
num_dic=defaultdict(int)

def mergesort(a):
    def _merge_sort(a, left: int, right: int):
        if left < right:
            center = (left+right)//2

            _merge_sort(a,left,center)
            _merge_sort(a,center+1,right)

            #pは前半部をbuffにコピーする際のインデックス、jで取り出す
            p=j=0
            i=k=left

            #配列の前半部をbuffにコピー
            while i <= center:
                buff[p]=a[i]
                p+=1
                i+=1

            #後半部をコピーされた前半部と比較、小さい方をaに格納していく
            while i<= right and j < p:
                if buff[j] <= a[i]:
                    a[k]=buff[j]
                    j+=1
                else:
                    a[k]=a[i]
                    i+=1
                k+=1

            while j < p:
                a[k] =buff[j]
                k+=1
                j+=1

    n=len(a)
    #merge結果を一時的に格納
    buff=[None]*n
    _merge_sort(a,0,n-1)
    del buff

for i in range(n):
    a,b=map(int,input().split())
    price_list[i]=a
    if num_dic[price_list[i]] is None:
        num_dic[price_list[i]]=b
    else:
        num_dic[price_list[i]]+=b

mergesort(price_list)
#同じ値段と違う値段で場合分け
price_sum=0
drink_count=0
for i in range(n):
    if i!=0 and price_list[i-1]==price_list[i]:
        continue
    else:
        #i番目のドリンクを買い占め
        drink_count+=num_dic[price_list[i]]
        if drink_count>m:
            drink_count-=num_dic[price_list[i]]
            stop=i
            break
        p=price_list[i]*num_dic[price_list[i]]
        price_sum+=p

k=m-drink_count
price_sum+=price_list[i]*k

print(price_sum)

Submission status: Accepted

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

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

#print(a)
pay=0
i=0
while m>0:
    p=min(m,a[i][1])
    m-=p
    pay+=p*a[i][0]
    i+=1
print(pay)

Submission status: Accepted

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

    shops = []
    for i in range(N):
        aa, bb = map(int, input().split())
        shops.append([aa, bb])

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

    rem = M
    ans = 0
    for shop in shops:
        buy = min(rem, shop[1])
        ans += buy * shop[0]
        rem -= buy

    print(ans)


resolve()

Submission status: Accepted

Solution code:
N, M, *AB = map(int, open(0).read().split())
AB = list(zip(*[iter(AB)]*2))

num = 0
cost = 0

for a, b in sorted(AB):
    if num+b >= M:
        cost += (M-num)*a
        break
    else:
        num += b
        cost += b*a

print(cost)

Submission status: Accepted

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

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

cost = 0
for i in range(n):
  if m <= AB[i][1]:
    cost += m * AB[i][0]
    print(cost)
    exit()
  else:
    m = m - AB[i][1]
    cost += AB[i][1] * AB[i][0]
    
print(cost)

Submission status: Accepted

Solution code:
n,m,*l=map(int,open(0).read().split());c=0
for a,b in sorted(zip(*[iter(l)]*2)):t=min(b,m);c+=a*t;m-=t
print(c)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split());n,m=f();c=0
for a,b in sorted(list(f())for _ in range(n)):t=min(b,m);c+=a*t;m-=t
print(c)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,m=f()
l=sorted(list(f()) for _ in range(n))
c=0
for a,b in l:
  if b>m: c+=a*m; break
  c+=a*b; m-=b
print(c)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,m=f()
l=[]
for _ in range(n):
  l+=[tuple(f())]
l.sort()
c=0
for a,b in l:
  if b<m:
    m-=b
    c+=a*b
  else:
    c+=a*m
    break
print(c)

Submission status: Accepted

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

a.sort()

c = 0
for i in range(n):
  x = min(m,a[i][1])
  c += x * a[i][0]
  m -= x
  if m == 0 :
    break

print(c)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
apple = []
for _ in range(n):
    apple.append(list(map(int, input().split())))
apple = sorted(apple)
msum = 0
ans = 0
for i in apple:
    msum += i[1]
    if msum < m:
        ans += (i[0]*i[1])
    elif msum == m:
        ans += (i[0]*i[1])
        break
    else:
        a = m - (msum - i[1])
        ans += (i[0]*a)
        break
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
line = [list(map(int, input().split())) for _ in range(N)]
line.sort()
price = 0
for i in line:
    if M - i[1] > 0:
        price += i[0] * i[1]
        M = M - i[1]
    elif M - i[1] == 0:
        price += i[0] * i[1]
        break
    else:
        number = i[1] - abs(M - i[1])
        price += i[0] * number
        break
print(price)