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())
list1 = []
cost = 0
for i in range(N):
  A, B = map(int, input().split())
  list1.append((A, B))
list2 = sorted(list1, key= lambda x:x[0])
for i in range(N):
  if M >= list2[i][1]:
    M -= list2[i][1]
    cost += list2[i][0] * list2[i][1]
  else:
    break
cost += list2[i][0] * M
print(cost)

Submission status: Accepted

Solution code:
from operator import itemgetter
n,m=map(int,input().split())
li=[list(map(int, input().split())) for i in range(n)]
li.sort(key=itemgetter(0))
ans=0
i=0
for i in li:
    a,b=i[0],i[1]
    if m>=b:
        ans+=a*b
        m-=b
    else:
        ans+=a*m
        break
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
pAc = [list(map(int, input().split())) for _ in range(N)]
pAc.sort()
count = 0
price = 0
for p,c in pAc:
    if count + c >= M:
        print(price+(M-count)*p)
        exit()
    count += c
    price += p*c

Submission status: Accepted

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

import sys
if n ==1:
    print(ca[0][0]*ca[0][1])
    sys.exit()
money = 0
i = 0
num = 0
while True:
    #print(str(i)+"だよ")
    if num + ca[i][1] < m:
        num += ca[i][1]
        money += ca[i][0] * ca[i][1]
        i += 1
        #print(num,money)
    else:
        money += ca[i][0]*(m-num)
        break    
print(money)

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

Submission status: Accepted

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

for _ in range(n):
  A, B = map(int, input().split())
  lis.append((A, B))

lis.sort()

for x in lis:
  a, b = x
  if m >= b:
    m -= b
    ans = ans + (a*b)
  else:
    ans = ans + (a*m)
    break
  
print(ans)

Submission status: Accepted

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

AB = sorted(AB, key = lambda x: x[0])
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
        M = 0
    if M == 0:
        break

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = sorted([list(map(int,input().split())) for n in range(N)])
G = 0
D = 0

for m in range(M):
  if D+X[m][1]<M:
    D+=X[m][1]
    G+=X[m][0]*X[m][1]
  else:
    G+=X[m][0]*(M-D)
    print(G)
    exit()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=sorted([tuple(map(int,input().split())) for i in range(N)])
ans=0
for a,b in L:
    if M>b:
        ans+=a*b
        M-=b
    else:
        ans+=a*M
        break
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
xy.sort(key = lambda x:x[0])
x,y = [list(i) for i in zip(*xy)]
nokori=M
ans=0
for i in range(N):
  ans+=x[i]*min(y[i],nokori)
  nokori-=y[i]
  if nokori<=0:
    break
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(key = lambda x:x[0])
ans = 0
for a, b in ab:
    ans += a * min(b, m)
    m -= b
    if m <= 0:
        break
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)

money = 0
for i in ab:
    money += i[0]*min(m,i[1])
    m -= min(m,i[1])
    
    if m<=0:
        break

print(money)

Submission status: Accepted

Solution code:

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

X.sort()
res = M
ans = 0
for a, b in X:
    if res <= 0:
        break

    tmp = min(b, res)
    res -= tmp
    ans += tmp * a

print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
dic = {}
for i in range(n):
    a,b = map(int,input().split())
    dic.setdefault(a,0)
    dic[a] += b
#print(dic)
nedan = sorted(dic)
#print(nedan)
ans = 0
for yen in nedan:
    if dic[yen] < m:
        #print(yen,dic[yen],m)
        ans += yen * dic[yen]
        m -= dic[yen]
    else:
        #print(yen,dic[yen],m)
        ans += yen * m
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C - Energy Drink Collector
N,M = map(int,input().split())
A_B = [tuple(map(int,input().split())) for _ in range(N)]

A_B = sorted(A_B, key = lambda x:x[1],reverse = True)
A_B = sorted(A_B, key = lambda x:x[0], reverse = False)

pay = 0
drink = M
for A, B in A_B:
    if drink <= B:
        pay += A*drink
        break
    else:
        pay += A*B
        drink -= B
print(pay)

Submission status: Accepted

Solution code:
def resolve():
    N, M = map(int, input().split())
    A = []
    for _ in range(N):
        a = list(map(int, input().split()))
        A.append(a)
    ans = 0
    for i, j in sorted(A, key=lambda x: x[0]):
        if j >= M:
            ans += M * i
            print(ans)
            exit()
        ans += i * j
        M -= j
resolve()

Submission status: Accepted

Solution code:
from operator import itemgetter

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
#print(ab)
i = 0
ans = 0
while m >0:
  if ab[i][1]<=m:
    m -= ab[i][1]
    ans += ab[i][0]*ab[i][1]
  else:
    ans += ab[i][0]*m
    m = 0
  i+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N, M = map(int,input().rstrip().split())
d = defaultdict(lambda: 0)
for _ in range(N):
    A, B = map(int,input().rstrip().split())
    d[A] += B
ans=0
for k in sorted(d.keys()):
    if d[k]<=M:
        ans += k*d[k]
    else:
        ans += k*M
    M -= d[k]
    if M<=0:break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
dic={}
doll=0
AB=[list(map(int,input().split())) for _ in range(n)]
AB.sort()
for k,v in AB:
  if v<m:
    doll+=k*v
    m-=v
  else:
    doll+=k*m
    print(doll)
    exit()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
dic={}
doll=0
AB=[list(map(int,input().split())) for _ in range(n)]

AB.sort()

for k,v in AB:
  if v<m:
    doll+=k*v
    m-=v
  else:
    doll+=k*m
    break
print(doll)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
cnt=0
for i in range(n):
    k,j=input().split()

    a.append([int(k),int(j)])
a=sorted(a)
for i in range(n):
    if a[i][1]>=m:
        cnt+=a[i][0]*m
        break
    else:
        cnt+=a[i][0]*a[i][1]
        m-=a[i][1]
print(cnt)

Submission status: Accepted

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

for i in range(N):
    buy = min(M,A[i][1])
    ans += buy * A[i][0]
    M -= buy
print(ans)

Submission status: Accepted

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

ans = bought = 0
for a, b in AB:
    m = min(M - bought, b)
    bought += m
    ans += a * m
print(ans)

Submission status: Accepted

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

ans = 0
bought = 0
for a, b in AB:
    m = min(M - bought, b)
    bought += m
    ans += a * m
print(ans)

Submission status: Accepted

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

a = sorted([[int(x) for x in input().split()] for _ in range(n)], key=lambda x: x[0])

result = 0
for x in a:
  cnt = min(m, x[1])
  result += x[0] * cnt
  m -= cnt
print(result)

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

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

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

if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
C=[]
ans=0
cnt=0
for i in range(N):
  a,b=map(int,input().split())
  C.append((a,b))
C=sorted(C)
for i in range(N):
  if cnt+C[i][1]<=M:
    cnt+=C[i][1]
    ans+=C[i][1]*C[i][0]
  else:
    break
print((M-cnt)*C[i][0]+ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N,M=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N)]
A=sorted(A,key=lambda x:x[0])
cost=0
num=0
for i in range(N):
    if num+A[i][1]>M:
        print(cost+A[i][0]*(M-num))
        sys.exit()
    else:
        cost+=A[i][0]*A[i][1]
        num+=A[i][1]
print(cost)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, *AB = map(int, read().split())
    X = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    X.sort()

    ans = 0
    n = 0
    for a, b in X:
        if n + b >= M:
            ans += (M - n) * a
            break
        else:
            ans += a * b
            n += b

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
price = [[] for i in range(N)]
for i in range(N):
    a,b = map(int,input().split())
    price[i] = [a,b]
price.sort()
#print(price)
m = 0
money = 0
for i in range(N):
    if m + price[i][1] < M:
        money += price[i][0] * price[i][1]
        m += price[i][1]
    else:
        money += price[i][0] * (M-m)
        break
print(money)

Submission status: Accepted

Solution code:
#a値段,b本数
#n軒,m本
n,m = list(map(int, input().split()))

ab_list = [list(map(int, input().split())) for _ in range(n)]
#print(ab_list)
ab_list.sort()
ans = 0
num = 0
for ab in ab_list:
    ans += ab[0]*ab[1]
    num += ab[1]
    if num >= m:
        i_stop=ab_list.index(ab)
        break
diff_num = num - m
ans -= diff_num * ab_list[i_stop][0]
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = [list(map(int,input().split())) for l in range(n)]
x = sorted(x)
count = m
p = 0
for i in x:
  if count >= i[1]:
    count -= i[1]
    p += i[0] * i[1]
  elif count ==0:
    break
  else:
    p += i[0] * count
    count = 0

print(p)

Submission status: Accepted

Solution code:

def main():
	n, m =  map(int , input().split())
	ab = []
	for i in range(0 , n):
		ab.append(list(map(int , input().split())))
	ab.sort()
	ans = 0
	for i in range(0 , n):
		if (m-ab[i][1]>=0):
			m = m-ab[i][1]
			ans = ans+(ab[i][0]*ab[i][1])
		elif(m>0):
			ans = ans+(m*ab[i][0])
			m  = 0
	print(ans)
if __name__ =="__main__":
	main()

Submission status: Accepted

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

c=sorted(c,key=lambda x:x[0])
ans=0
for i in range(a):
    if b==0:
        break
    elif b>=c[i][1]:
        b-=c[i][1]
        ans+=c[i][1]*c[i][0]
    else:
        ans+=b*c[i][0]
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
for _ in range(n):
  b=list(map(int,input().split()))
  a.append(b)
  
a.sort(key=lambda x: x[0])
ans=0
h=0

for y in a:
  d=y[0]
  e=y[1]
  if h+e>m:
    ans+=(m-h)*d
    h+=(m-h)
  else:
    h+=e
    ans+=d*e

print(ans)

Submission status: Accepted

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

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

ab.sort()

p = 0
x = 0

for i in range(n):
    if x + ab[i][1] < m:
        x += ab[i][1]
        p += ab[i][0] * ab[i][1]
    else:
        p += ab[i][0] * (m - x)
        break

print(p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = list(map(lambda n: int(n), input().split(" ")))
E = []
for i in range(N):
  tmp = input().split(" ")
  E.append({
    "price": int(tmp[0]),
    "count": int(tmp[1])
  })

E.sort(key=lambda e: e["price"])

totP = 0
totC = 0
for i in range(len(E)):
  if totC + E[i]["count"] >= M:
    totP += (M - totC) * E[i]["price"]
    break
  else:
    totC += E[i]["count"]
    totP += E[i]["price"] * E[i]["count"]

print(totP)

Submission status: Accepted

Solution code:
from operator import itemgetter
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=itemgetter(0))
ans = 0
for i in range(N):
    ans += min(M, AB[i][1]) * AB[i][0]
    M -= min(M, AB[i][1])
    if M==0:
        break
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]:
        ans+=ab[i][0]*ab[i][1]
        m-=ab[i][1]
    else:
        ans+=ab[i][0]*m
        print(ans)
        break

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int, input().split())
A = np.array([list(map(int, input().split())) for _ in range(n)], dtype='int64')
ind = np.argsort(A[:,0])
A = A[ind]
num = 0
money = 0
for a,b in A:
    num += b
    if num < m:
        money += a * b
    else:
        money += a * (b - (num - m))
        break
print(money)

Submission status: Accepted

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

for _ in range(N):
  A, B = map(int, input().split())
  AB.append((A, B))

AB.sort()

i = 0
ans = 0

while M > 0:
  A, B = AB[i]
  if B <= M:
    ans += A * B
    M -= B
  else:
    ans += A * M
    M = 0
  i += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

N,M = MI()
AB = [tuple(MI()) for _ in range(N)]
AB.sort()

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, M = LI()
    AB = [LI() for _ in range(N)]
    AB.sort()

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

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

C={}
S1=[]
for i in range(n):
    a,b = map(int,input().split())
    S1.append(a)
    if a in C:
        C[a]+=b
    else:
        C[a]=b
S=list(set(S1))
S.sort()

ans=0
for s in S:
    # print(S,C,ans)
    if m<C[s] :
        ans+=m*s
        break
    else:
        ans+=C[s]*s
        m-=C[s]

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
dict1 = {}
for _ in range(n):
    price, b = map(int, input().split())
    if price in dict1:
        dict1[price] += b
    else:
        dict1[price] = b

money_cnt = 0
for price, b in sorted(dict1.items()):
    if b < m:
        m -= b
        money_cnt += price * b
    else:
        money_cnt += price * m
        break
print(money_cnt)

Submission status: Accepted

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

ab=sorted(ab)
ans=0
d=m

for i in range(n):
    if ab[i][1]<=d:
        ans+=ab[i][0]*ab[i][1]
        d-=ab[i][1]
    else:
        
        ans+=ab[i][0]*d
        
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
cnt = 0
cnt_money = 0
for i in sorted([rl() for _ in range(n)]):
  a, b = i
  if cnt + b < m:
    cnt += b
    cnt_money += a*b
  else:
    cnt_money += (m - cnt) * a
    break
print(cnt_money)

Submission status: Accepted

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

ans=0
for a,b in S:
    if M<=0: break
    x=min(M,b)
    ans+=a*x
    M-=x
print(ans)

Submission status: Accepted

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

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

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

A, B = zip(*c)

count = 0
money = 0
for i in range(N):
    count += B[i]
    money += A[i] * B[i]
    if count < M:
        continue
    else:
        while count != M:
            count -= 1
            money -= A[i]
        print(money)
        exit()

Submission status: Accepted

Solution code:
#python 2.7
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab=sorted(ab,key=lambda x:x[0])
res=ans=0
for i in range(n):
  if res+ab[i][1]>=m:
    ans+=(ab[i][0])*(m-res)
    break
  else:
    ans+=(ab[i][0])*ab[i][1]
    res+=ab[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.sort(key=lambda x:x[0])
now=0
ans=0
for i in range(n):
  if now+ab[i][1]<=m:
    now += ab[i][1]
    ans += ab[i][0]*ab[i][1]
  else:
    ans += ab[i][0]*(m-now)
    break
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB = sorted(AB, key=lambda x: x[0])
r = 0
for a, b in AB:
    if M <= 0:
        break
    if b <= M:
        r += a*b
        M -= b
    elif b >= M:
        r += a*M
        break
print(r)

Submission status: Accepted

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

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

ret = 0
money = 0

for a, b in AB:
    if ret >= M:
        break

    while ret < M and b >= 1:
        b -= 1
        ret += 1
        money += a
        
print(money)

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

m = 0
for ab in AB:
    a = ab[0]
    b = ab[1]

    if m + b <= M:
        money += a * b
        m += b
    
    else:
        money += min(b, (M - m)) * a
        m += min(b, (M - m))

    if m == M:
        break

print(money)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, m = nm()
    AB = [tuple(map(int, input().split())) for _ in range(n)]
    AB = sorted(AB, key=lambda x: x[0])
    num = 0
    ans = 0
    for a, b in AB:
        w_num = m - num
        if w_num <= b:
            num += w_num
            ans += w_num * a
            break
        else:
            num += b
            ans += b * a
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
drink = []
for _ in range(n):
    a,b = map(int,input().split())
    drink.append((a,b))
d = sorted(drink)

ans = 0
x = 0

for t in d:
    if x+t[1] < m:
        ans += t[0]*t[1]
        x += t[1]
    else:
        ans += t[0]*(m-x)
        break

print (ans)

Submission status: Accepted

Solution code:
import heapq
n, m = map(int, input().split())
a_b = []

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

heapq.heapify(a_b)

res = 0
for i in range(m):
    tmp_a, tmp_b = heapq.heappop(a_b)
    res += tmp_a 
    tmp_b -= 1
    if tmp_b > 0:
        heapq.heappush(a_b, [tmp_a, tmp_b])

print(res)

Submission status: Accepted

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

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

a_b.sort()

res = 0
counter = 0
for i in range(n):
    counter = min(m, a_b[i][1])
    m -= counter
    res += counter * a_b[i][0]
    if m == 0:
        break
print(res)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
# 単価が安い順に店を並べる
sorted_AB = sorted(AB, key=lambda x: x[0])

money = 0
cnt = 0
for a, b in sorted_AB:
    tmp = min(b, M - cnt)
    cnt += tmp
    money += a * tmp
    if cnt == M:
        break
print(money)

Submission status: Accepted

Solution code:
n,m=list(map(int,input().split()))
p=[]
for i in range(n):
  a=list(map(int,input().split()))
  p.append(a)
p.sort(key=lambda x:x[0])
num=0
ans=0
i=0
while num<m:
  ans=ans+p[i][0]*p[i][1]
  num=num+p[i][1]
  
  i=i+1
  
print(ans-(-m+num)*p[i-1][0])

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a_b = {}
for i in range(n):
    x,y = map(int,input().split())
    if x in a_b.keys():
        a_b[x] += y
        continue
    a_b[x] = y
a_b = sorted(a_b.items(), key=lambda x:x[0])

#print(a_b)
c = 0
ans = 0
t = 0
for i in range(int(len(a_b))):
    c += a_b[i][1]
    if c > m:
        ans += a_b[i][0] * (m - t)
        break
    else:
        ans += a_b[i][0] * a_b[i][1]
        t += a_b[i][1]

print(ans)

Submission status: Accepted

Solution code:
f=lambda:[*map(int,input().split())]
n,m=f()
c=0
for a,b in sorted((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()
p=sorted((f()) for _ in range(n))
c=0
for a,b in p:
  if m>b:
    c+=a*b
    m-=b
  else:
    c+=a*m
    break
print(c)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
d={}
A=[0]*n
B=[0]*n
for i in range(n):
    A[i],B[i]=map(int,input().split())
    d[str(i)]=A[i]
a=sorted(d.items(),key=lambda x:x[1])
ans=0
for i in range(n):
    if m>B[int(a[i][0])]:
        m-=B[int(a[i][0])]
        ans+=a[i][1]*B[int(a[i][0])]
    else:
        ans+=a[i][1]*m
        break
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ab = sorted([list(map(int,input().split())) for i in range(n)])
shop,cost,rest=0,0,m
while True:
    if ab[shop][1]<rest:
        cost += ab[shop][1]*ab[shop][0]
        rest -= ab[shop][1]
        shop+=1
    else:
        cost+=ab[shop][0]*rest
        break
print(cost)

Submission status: Accepted

Solution code:
import sys

N, M = map(int, sys.stdin.readline().split())

shops = []
for _ in range(N):
    a, b = map(int, sys.stdin.readline().split())
    shops.append((a, b))

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

print(ans)

Submission status: Accepted

Solution code:
store_num,bottle_num = map(int,input().split())

drink_dict = {}

for i in range(store_num):
    pay,num = map(int,input().split())
    drink_dict.setdefault(pay,0)
    drink_dict[pay] += num
    

drink_sorted = sorted(drink_dict.items(),key=lambda x:x[0])


cost = 0

for i in range(store_num):
    value = drink_sorted[i][0]
    num = drink_sorted[i][1]
    cost += value * min(num,bottle_num)
    bottle_num -= num
    
    if bottle_num <= 0:
        print(cost)
        break

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ab = []
for i in range(n):
    AB = list(map(int, input().split()))
    ab.append(AB)
x = 0
val = 0
ab.sort(key=lambda x:x[0])
for i in range(n):
    x += ab[i][1]
    val += ab[i][0]*ab[i][1]
    if x < m:
        continue
    else:
        val = val - ab[i][0]*(x - m)
        break
print(val)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
d={};price=0
for _ in range(n):
  a,b=map(int,input().split())
  if a not in d:
    d[a] = b
  else:
    d[a] += b
d=sorted(d.items(),key=lambda x:x[0])
for i in d:
  for _ in range(i[1]):
    if m == 0:
      print(price)
      exit()
    m-=1
    price+=i[0]
else:
  print(price)

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
A = [0]*N
B = [0]*N
d = {}

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

a = sorted(d.items(), key=lambda x:x[1])
ans=0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l)
drink=0
money=0
index=0
while(drink<m):
    if l[index][1]+drink<m:
        drink+=l[index][1]
        money+=l[index][0]*l[index][1]
        index+=1
    else:
        money+=l[index][0]*(m-drink)
        drink=m
        print(money)
        break