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:
from collections import defaultdict, deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
# MOD = 10 ** 9 + 7
# INF = float("inf")


def main():
    n, m = map(int, input().split())
    dd = defaultdict(int)
    for _ in range(n):
        a, b = map(int, input().split())
        dd[a] += b
    dd_sort = sorted(dd.items(), key=lambda x: x[0])
    ans = 0
    r = m
    for cost, num in dd_sort:
        if r >= num:
            ans += cost * num
            r -= num
        else:
            ans += cost * r
            break
    print(ans)
if __name__ == '__main__':
    main()

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 = sorted(L)
#print(L)
ans = 0
for i in range(N):
    a = L[i][0]
    b = L[i][1]
    if M >= b:
        ans += a * b
        M -= b
    elif M >0:
        ans += a * M
        M = 0

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()
cnt = 0
ans = 0
for i in range(n):
    a = ab[i][0]
    b = ab[i][1]
    if cnt +b >= m:
        ans += (m-cnt)*a
        break
    else:
        ans +=a*b
        cnt +=b
print(ans)

Submission status: Accepted

Solution code:
def resolve():
  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 ab in l:
    if ab[1] < m:
      m -= ab[1]
      ans += ab[0] * ab[1]
    else:
      ans += ab[0] * m
      m = 0
      break
  print(ans)
  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for i 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)

Submission status: Accepted

Solution code:
n,m = list(map(int,input().split()))
ab = []
for i in range(n):
  a,b = list(map(int,input().split()))
  ab.append([a,b])
from operator import itemgetter
ab = sorted(ab,key=itemgetter(0))

ans = 0
for i in range(n):
  if m <= ab[i][1]:
    ans += m*ab[i][0]
    break
  else:
    ans += ab[i][1]*ab[i][0]
    m -= 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()

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

Submission status: Accepted

Solution code:
def examA():
    N = I()
    L = LI()
    L.sort()
    if sum(L)<=L[-1]*2:
        print("No")
    else:
        print("Yes")
    return

def examB():
    N, M = LI()
    A = [LI()for _ in range(N)]
    A.sort(key=lambda x:x[0])
    ans = 0
    for a,b in A:
        if M<=b:
            ans += a*M
            break
        ans += a*b
        M -= b
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    answer = 0
    cost = [list(map(int, input().split())) for _ in range(n)]
    cost.sort(key=lambda x: x[0])
    for c, num in cost:
        if m < 0:
            break
        answer += c * min(m, num)
        m -= min(m, num)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

mise={}
for i in range(n):
    a,b=[int(j) for j in input().split()]
    if a in mise:
        mise[a]+=b
    else:
        mise[a]=b
nedan=list(mise.keys())
nedan.sort()
i=0
ans=0
while m:
    honnsuu=mise[nedan[i]]
    kauno=min(honnsuu,m)
    ans+=kauno*nedan[i]
    i+=1
    m=m-kauno

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

Submission status: Accepted

Solution code:
import heapq
def resolve():
    n,m=map(int,input().split())
    drink=[]
    ans=0
    count=0
    for i in range(n):
        tmp=list(map(int,input().split()))
        drink.append(tmp)
    heapq.heapify(drink)
    while(True):
        tmp=heapq.heappop(drink)
        if m>tmp[1]:
            ans+=tmp[0]*tmp[1]
            m-=tmp[1]
        else:
            ans+=tmp[0]*m
            break
    print(ans)


resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n,m = map(int,input().split())
l =deque(sorted([list(map(int,input().split())) for i in range(n)]))
money = 0
while m!=0:
  a,b = l.popleft()
  t = min(b,m) 
  m -= t
  money += a*t
print(money)

Submission status: Accepted

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

ab = []
ans = 0
sum = 0

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

ab.sort()

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

    if ans == m:
        break


print(sum)

Submission status: Accepted

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

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()
yen = 0
cnt = 0
for i, a_b in enumerate(ab):
    if cnt + a_b[1] < m:
        yen += a_b[0] * a_b[1]
        cnt += a_b[1]
    else:
        tmp = m - cnt
        cnt += tmp
        yen += tmp * a_b[0]
print(yen)

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 ab:ab[0])

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

Submission status: Accepted

Solution code:
import sys

n, m, *ab = map(int, sys.stdin.read().split())
ab = sorted(zip(*[iter(ab)] * 2))

def main():
    res = 0
    cnt = 0
    for a, b in ab:
        if cnt + b >= m:
            res += a * (m - cnt)
            print(res)
            return
        res += a * b
        cnt += b
    
if __name__ ==  '__main__':
    main()

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)
#print(a)
#print(b)
ans=0
for i in range(n):
  if (b[i]<m):
    ans+=a[i]*b[i]
    m-=b[i]
  else:
    ans+=a[i]*m
    break
    
print(ans)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,m = readInts()
    AB = []
    for i in range(n):
        a,b = readInts()
        AB.append((a,b))
    AB = sorted(AB,key = lambda x:x[0]) # Aに着目して安いやつ Aが値段でBが在庫や！！
    ans = 0
    cnt = 0
    ok = False
    #print(AB)
    for i in range(n):
        a,b = AB[i]
        if ok:
            break
        #print(m,cnt,m-cnt,b,ans)
        if cnt <= m and b <= m - cnt:
            ans += a * b
            cnt += b
        elif cnt <= m:
            ans += a * (m - cnt)
            cnt = m
            ok = True
    print(ans)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
store_ls = [0] * n
for i in range(n):
    store_ls[i] = list(map(int,input().split()))
store_ls.sort(key = lambda x: x[0])
count = 0
price = 0
for i in range(n):
    if count + store_ls[i][1] < m:
        count += store_ls[i][1]
        price += store_ls[i][0] * store_ls[i][1] 
    else:
        rest = m - count
        price += store_ls[i][0] * rest
        break
print(price)

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
a_ls,b_ls = [0] * n, [0]*n
for i in range(n):
    a_ls[i], b_ls[i] = map(int,input().split())
arg = list(np.argsort(a_ls))
count = 0
sumprice = 0
t = 0
target = arg[t]
while True:
    b_ls[target] -= 1
    count += 1
    sumprice += a_ls[target]
    if count >= m:
        break
    if b_ls[target] == 0:
        t += 1
        target = arg[t]
        
print(sumprice)

Submission status: Accepted

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

e, f = [], []
for i, j in sorted(zip(a, b)):
    e.append(i)
    f.append(j)

ans = 0
for i in range(n):
    if m - f[i] <= 0:
        ans += e[i] * m
        print(ans)
        exit()
    else:
        ans += f[i] * e[i]
        m -= f[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    shops = []
    for _ in range(N):
        shops.append(tuple(map(int, input().split())))
    shops = sorted(shops, key=lambda x:x[0])

    cnt = 0
    ans = 0
    for s in shops:
        pay, lim = s
        if cnt+lim <= M:
            cnt += lim
            ans += lim*pay
        else:
            nokori = M-cnt
            ans += nokori*pay
            break
    print(ans)
main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import heapq
N, M = [int(i) for i in input().split()]
zaiko={}
value = []
heapq.heapify(value)
for i in range(N):
    #在庫の本数。一本あたりの代金
    b,a=[int(i)for i in input().split()]
    if  b not in zaiko:
        heapq.heappush(value,b)
        zaiko[b]=a
    else:
        zaiko[b]+=a

cnt=0
sumval=0
while True:
    if cnt == M: 
       print(sumval)
       exit()
    minival = heapq.heappop(value)
    minicnt = zaiko[minival]
    #print("minival,minicnt={},{}".format(minival,minicnt))
    if cnt + minicnt <= M:
       cnt+=minicnt
       sumval += minival * minicnt
    else:
       sumval += minival * (M-cnt)
       cnt=M

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
drinks = []
for _ in range(N):
    drinks.append(list(map(int,input().split())))
drinks.sort()
rest = M
ans = 0
i = 0
while rest != 0:
    temp = min(rest, drinks[i][1])
    ans += drinks[i][0] * temp
    rest -= temp
    i += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,m=map(int,input().split())
    C=[tuple(map(int,input().split())) for i in range(n)]
    C.sort()
    honsu=m
    cost=0
    for i,c in enumerate(C):
        cost+=c[0]*min(honsu,c[1])
        honsu-=c[1]
        if honsu<=0:
            break
    print(cost)
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from queue import PriorityQueue


N, M = map(int, input().split())
que = PriorityQueue()
for _ in range(N):
    A, B = map(int, input().split())
    que.put([A, B])

ans = 0
while not que.empty():
    a, b = que.get()
    if M > b:
        M -= b
        ans += a * b
    else:
        ans += a * M
        break

print(ans)

Submission status: Accepted

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

ab.sort()
total = 0
num = 0

for i in range(n):
    if num+ab[i][1] <= m:
        total += ab[i][0]*ab[i][1]
        num += ab[i][1]
    else:
        a = m-num
        total += ab[i][0]*a
        print(total)
        exit()

print(total)

Submission status: Accepted

Solution code:
n,m = [int(i) for i in input().split()]
ab = []
for i in range(n):
  a = [int(i) for i in input().split()]
  ab.append(a)
from operator import itemgetter
ab = sorted(ab,key=itemgetter(0))

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from sys import stdin
import numpy as np
#import math
#from functools import reduce
    
N, M = [int(x) for x in stdin.readline().rstrip().split()]
#N = int(input())
#S = list(input())

costs = []
num = []

for n in range(N):
    ai, bi = [int(x) for x in stdin.readline().rstrip().split()]
    costs += [ai]
    num += [bi]
    
sorted_idx = np.argsort(costs)
costs = list(np.array(costs)[sorted_idx])
num = list(np.array(num)[sorted_idx])

count_num = 0
count_cost = 0
for i, n in enumerate(num):
    if count_num + n < M:
        count_cost += n*costs[i]
        count_num += n
    else:
        count_cost += costs[i]*(M-count_num)
        break

print(count_cost)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
drink=[tuple(map(int,input().split())) for i in range(N)]
drink.sort(key=lambda x:(x[0],-x[1]))
ans=0
tmp=0
for i in range(N):
    if tmp+drink[i][1]<=M:
        tmp+=drink[i][1]
        ans+=drink[i][0]*drink[i][1]
    else:
        #M<tmp+drink[i][1]
        #tmp+x=M
        ans+=drink[i][0]*(M-tmp)
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = []
for _ in range(N):
  x, y = map(int, input().split())
  a.append((x, y))
a.sort(reverse = True)
res = 0
while True:
  x, y = a.pop()
  if M <= y:
    res += x * M
    break
  else:
    res += x * y
    M -= y
print(res)

Submission status: Accepted

Solution code:
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])
s=0 #本数
t=0 #値段
for i in range(N):
  s+=A[i][1]
  if M>s:
    t+=A[i][0]*A[i][1]
  else:
    over=s-M
    t+=A[i][0]*A[i][1]-A[i][0]*over
    break
print(t)

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
A = sorted([[int(_) for _ in input().split()] for i in range(n)], key=lambda x:x[0])
res = 0
for l in A:
  if m == 0:
    break
  a, b = l
  res += a * min(b, m)
  m = max(0, m - b)
print(res)

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(key=lambda x:x[0] )
i=-1
t=0
ans=0
while t < m:
    i+=1
    t+=ab[i][1]
    ans+=ab[i][0]*ab[i][1]
ans -= (t-m) * ab[i][0]
print(ans)

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

AB.sort()

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

print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[0]*N;p=0;q=M
for i in range(N):
  a,b=map(int,input().split())
  A[i]=[b,a]

A.sort(key=lambda x: x[1])
#print(A)
i=0
while q>0:
  if q>=A[i][0]:
    p=p+A[i][0]*A[i][1]
    q=q-A[i][0]
    i=i+1
  else:
    p=p+q*A[i][1]
    q=q-q
    break
print(p)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
drink = [list(map(int,input().split())) for _ in range(n)]
drink = sorted(drink, key=lambda drink:drink[0])
money = 0
total = 0
for i in range(n):
  if total+drink[i][1]<m:
    total += drink[i][1]
    money += drink[i][1]*drink[i][0]
  else:
    money += drink[i][0]*(m-total)
    print(money)
    exit()

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 m - b > 0:
    m -= b
    ans += a * b
    
  else:
    ans += a * m
    print(ans)
    exit()

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)
ans=0
d=0
for i in range(n):
  s=min(m-d,ab[i][1])
  ans+=ab[i][0]*s
  d+=s
  if d==m:
    break
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[]
for x in range(n):
    a,b=map(int,input().split())
    l.append([a,b])
l=sorted(l)
r=0
for x in l:
    r+=min(m,x[1])*x[0]
    m-=x[1]
    if m<=0:break
print(r)

Submission status: Accepted

Solution code:
from functools import reduce
N, M = map(int,input().split())
ary1 =[]
for x in range(N):
  ary1.append(list(map(int,input().split())))

ary2 = sorted(ary1)
count = 0
total_sum = 0
for i in range(N):
  if count + ary2[i][1] <= M:
    total_sum += ary2[i][0] * ary2[i][1]
    count += ary2[i][1]
  elif count > M :
    break
  else:
    zan = M - count
    total_sum += ary2[i][0] * zan
    count += zan

print(total_sum)

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
ab = [[int(i) for i in input().split()] for _ in range(n)]
ab.sort()
ans = 0
for i in ab:
    if m <= i[1]:
        ans += m * i[0]
        m = 0
    else:
        m -= i[1]
        ans += i[0] * 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)]
k=0
c=0
ab.sort()
for i in range(n):
    if k+ab[i][1]>=m:
        c+=ab[i][0]*(m-k)
        #k+=m-k
        print(c)
        exit()
    else:
        k+=ab[i][1]
        c+=ab[i][0]*ab[i][1]

Submission status: Accepted

Solution code:
s,p=map(int,input().split())
l=sorted([list(map(int,input().split())) for i in range(s)])
now,budget=0,0
for  price,bottles in l:
    if now+bottles>=p:print(budget+price*(p-now));break
    else:now+=bottles;budget+=price*bottles

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = [input().split() for _ in range(N)]
L = [[int(x[0]),int(x[1])] for x in L]
L = sorted(L)
c = 0
s = 0
for i in range(N):
  A = L[i][0]
  B = L[i][1]
  s += B
  if s <= M:
    c += A*B
  else:
    c += (B-(s-M))*A
    break
print(c)

Submission status: Accepted

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

N, M = [int(x) for x in input().split()]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PRICE = 0
NBOTTLE = 1

AB.sort() # price昇順でソート
bought = 0
paid = 0
for i in range(N):
    if bought >= M: break
    buy = min(M - bought, AB[i][NBOTTLE])
    bought += buy
    paid += buy * AB[i][PRICE]

print(paid)

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PRICE = 0
NBOTTLE = 1

AB.sort() # price昇順でソート
bought = 0
paid = 0
for i in range(N):
    if bought >= M: break
    buy = min(M - bought, AB[i][NBOTTLE])
    bought += buy
    paid += buy * AB[i][PRICE]

print(paid)

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

pt,money = 0,0
while M > 0 and pt < N:
    if M > AB[pt][1]:
        money += AB[pt][0] * AB[pt][1]
        M -= AB[pt][1]
    else:
        money += AB[pt][0] * M
        M = 0
    pt += 1

print(money)

Submission status: Accepted

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

Submission status: Accepted

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

cnt,ans,j=0,0,0

while cnt<m:
  if ab[j][1]>=m-cnt:
    ans+=ab[j][0]*(m-cnt)
    cnt=m
  else:
    ans+=ab[j][0]*ab[j][1]
    cnt+=ab[j][1]
  j+=1
print(ans)

Submission status: Accepted

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

AB.sort()

for i in range(N):
  if m==M:
    break
  if m+AB[i][1]>=M:
    for j in range(AB[i][1]):
      ans +=AB[i][0]
      m +=1
      if m==M:
        break
  else:
    ans += AB[i][0]*AB[i][1]
    m=m+AB[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)]
'''
a　価格
b　上限本数
'''
ans = 0
ab.sort()
i = 0
while m > 0:
  a = ab[i][0]
  b = ab[i][1]
  if m >= b:
    m -= b
    ans += a*b
  else:
    ans += a*m
    m = 0
  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
d = 0
i = 0
while d < m:
  ans += ab[i][0]*min(m-d,ab[i][1])
  d += ab[i][1]
  i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
Drink = [[] for i in range(n)]
for i in range(n):
    Drink[i] = list(map(int, input().split()))
# 値段安い店順にソート
Drink.sort(key=lambda x: x[0])
ans = 0 # 値段
count = 0 # 総数
ind = 0 # 回る店
while count < m:
    p,num = Drink[ind] # 価格と数
    if count+num < m:
        count+=num
        ans+=p*num
        ind+=1
        continue
    else:
        ans+=p*(m-count)
        count=m
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for a, b in sorted(v):
    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())
AB = [list(map(int, input().split())) for i in range(N)]

AB.sort()

price = 0
store = 0

while M > 0:
  if M - AB[store][1] < 0:
    price += AB[store][0]*M
    M = 0
  else:
    price += AB[store][0]*AB[store][1]
    M -= AB[store][1]
    store += 1

print(price)

Submission status: Accepted

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

base.sort(key = lambda x:x[0])
sign = 0
ans = 0
judge = 0
for i in range(n):
  if  judge + base[i][1] <= m:
    judge = judge + base[i][1]
    ans = ans + base[i][1]*base[i][0]
  
  else:
    ans = ans + (m - judge)*base[i][0]
    break
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

shop = []

for i in range(N):
    a,b = map(int, input().split())
    shop.append((a, b))

shop.sort()

cnt = 0
ans = 0
for i in range(N):
    cost, count = shop[i]
    if cnt == M:
        break
    if cnt < M:
        if cnt + count <= M:
            cnt += count
            ans += cost * count
        else:
            ex = M - cnt
            cnt = M
            ans += cost * ex 
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
from collections import deque

def main():
    N, M = input_nums()
    drinks = []
    for i in range(N):
        A, B = input_nums()
        drinks.append((A, B))
    drinks.sort()
    drinks = deque(drinks)
    cnt = 0
    money = 0
    while cnt < M:
        a, b = drinks.popleft()
        cnt += b
        money += a*b
    money -= (cnt-M)*a
    print(money)
    


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for n in range(N):
  mon+=D[n][0]*D[n][1]
  num+=D[n][1]
  
  if M<=num:
    print(mon-D[n][0]*(num-M))
    break

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
shops = []
for i in range(N):
    price,B = map(int,input().split())
    shops.append([price,B])
shops.sort()
i = 0
money = 0
while M > 0:
    if shops[i][1] > M:
        money += M*shops[i][0]
        break
    else:
        money += shops[i][1] * shops[i][0]
        M -= shops[i][1]
    i += 1
print(money)

Submission status: Accepted

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

i = 0
while m > 0:
  if m > ab[i][1]:
    ans = ans + ab[i][0]*ab[i][1]
  else:
    ans = ans + ab[i][0]*m
  m -= ab[i][1]
  i += 1

print(ans)

Submission status: Accepted

Solution code:
inputs = open(0).readlines()
n, m = map(int, inputs[0].split())
AB = [list(map(int, AB.split())) for AB in inputs[1:]] 
AB.sort()
ans, count = 0, 0
for a, b in AB:
  if count + b >= m:
    ans += a * (m - count)
    break
  count += b
  ans += a * b
print(ans)