Problem:


Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.



Sample input:
2 5
4 9
2 4


Sample output:
12

Submission status: Accepted

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

ab.sort(key=lambda x: x[0])
bought = 0
budget = 0
i = 0
while bought < m:
    buy = min(ab[i][1], m-bought)
    budget += ab[i][0] * buy
    bought += buy
    i += 1

print(budget)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x,y=[],[]
x=sorted([list(map(int,input().split()))for i in range(n)],key=lambda x:x[0])
#小さい方から買うけど、余ったら減らす

#print(x)
for i in range(n):
    #print(m)
    c=m-x[i][1]
    if c<=0:
        s=0
        for j in range(i+1):
            #print(x[i])
            s+=x[j][0]*x[j][1]
        #print(s)
        print(s+c*x[i][0])
        break
    m=c

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
C = [list(map(int,input().split())) for i in range(n)]
C.sort()
drinks = 0
price = 0
for i in range(n):
  if drinks+C[i][1] < m:
    drinks += C[i][1]
    price += C[i][0]*C[i][1]
  else:
    price += C[i][0]*(m-drinks)
    print(price)
    exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ls = []
for i in range(N):
  A, B = map(int, input().split())
  ls += [(A,B)]
ls.sort(key=lambda x:x[0])
count = 0
ans = 0
for x in ls:
  a,b = x
  bcount = 0
  while count<M and bcount<b:
    count += 1
    bcount += 1
    ans += a
  if count==M:
    break
print(ans)

Submission status: Accepted

Solution code:
n, m = (int(i) for i in input().split())
abstore = [[int(i) for i in input().split()] for j in range(n)]
abstore.sort()
bought = 0
ans = 0
for a, b in abstore:
    if b < m - bought:
        bought += b
        ans += a * b
    else:
        ans += a * (m - bought)
        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])

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

s = 0
for i in range(len(ab)):
    if M - ab[i][1] > 0:
        # print(ab[i][0],ab[i][1],ab[i][0] * ab[i][1],M)
        M -= ab[i][1]
        s += ab[i][0] * ab[i][1]
    else:
        for j in range(ab[i][1]):
            # print(ab[i][0],ab[i][1],ab[i][0] * ab[i][1],M)
            M -= 1
            s += ab[i][0]
            if M == 0:
                break
        break

print(s)

Submission status: Accepted

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

for ab in sorted(AB):
    for i in range(ab[1]):
        if cnt < M:
            ans += ab[0]
            cnt += 1
        else:
            break
    if cnt >= 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] = tuple(map(int, input().split()))

AB.sort(key=lambda x: x[0])
s = 0
m = 0
for a, b in AB:
    r = min(M - m, b)
    m += r
    s += r * a
    if m == M:
        break
print(s)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import heapq as hq

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

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

while True:
    a,b=hq.heappop(A)
    ans+=(a*min(b,M))
    M-=min(b,M)
    if M==0:
        break
    
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N, M = map(int, input().split())
    C = [list(map(int, input().split())) for i in range(N)]
    C = sorted(C)
    s = 0
    ans = 0
    
    for i in range(N):
        if s == M:
            break
        ans += C[i][0] * min(M-s,C[i][1])
        s += min(M-s,C[i][1])

    print(ans)
    
    
main()

Submission status: Accepted

Solution code:
# 2019-08-27 22:28:29
N, M = list(map(int, input().split()))
l = []
for i in range(N):
    m = list(map(int, input().split()))
    l.append(m)

l = sorted(l)
j = 0
sum = 0
while M != 0:
    if M - l[j][1] > 0:
        sum = sum + l[j][0] * l[j][1]
        M = M - l[j][1]
    else:
        sum = sum + l[j][0] * M
        M = 0 
    j += 1 
print(sum)

Submission status: Accepted

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

    sort_A = sorted(A)
    cnt = 0
    money = 0
    for i in range(N):
        if cnt + sort_A[i][1] >= M:
            money += (M - cnt) * sort_A[i][0]
            break
        money += sort_A[i][0] * sort_A[i][1]
        cnt += sort_A[i][1]

    print(money)

main()

Submission status: Accepted

Solution code:
from operator import itemgetter

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

shops = sorted(shops, key=itemgetter(0))
rest = m
cost = 0
for a, b in shops:
    if rest > b:
        cost += a * b
        rest -= b
    else:
        cost += a * rest
        break

print(cost)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ans = 0
shop = [tuple(map(int, input().split())) for _ in range(n)]
shop.sort()
for price, num in shop:
    num = min(m, num)
    ans += price * num
    m -= num
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
total = [0] * (n + 1)
ans = 0

# 安い店から選ぶ方法が最適
shop = [tuple(map(int, input().split())) for _ in range(n)]
shop.sort()

for i in range(1, n + 1):
    a_i, b_i = shop[i - 1]
    # 累積和を作っておく
    # b[i]: i番目の店まで利用した際に購入できる栄養ドリンクの最大本数
    # total[i]: i番目の店まで利用した際に支払うお金の最大値
    b[i] = b[i - 1]
    total[i] = total[i - 1]
    b[i] += b_i
    total[i] += a_i * b_i

    # a[i]: i番目の店の栄養ドリンクの単価
    a[i] = a_i

for i in range(1, n + 1):
    if m <= b[i]:
        m = m - b[i - 1]
        # 足りない分を追加で計算
        ans = total[i - 1] + a[i] * m
        break
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
price = [0] * (n + 1)
ans = 0

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

for i in range(1, n + 1):
    a_i, b_i = shop[i - 1]
    a[i] = a[i - 1]
    b[i] = b[i - 1]
    price[i] = a_i
    a[i] += a_i * b_i
    b[i] += b_i

for i in range(1, n + 1):
    if m <= b[i]:
        m = m - b[i - 1]
        ans = a[i - 1] + price[i] * m
        break
print(ans)

Submission status: Accepted

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

shop.sort()

total = 0
for i in range(len(shop)):
    if shop[i][1] == M:
        total += shop[i][0] * shop[i][1]
        break
    elif shop[i][1] > M:
        total += shop[i][0] * M
        break
    else:
        total += shop[i][0] * shop[i][1]
        M -= shop[i][1]
print(total)

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(n):
    yen, num = map(int, input().split())
    shop.append([yen, num])

shop.sort()

ans = 0
for yen, num in shop:
    if num < m:
        ans += yen*num
        m -= num
    else:
        ans += yen*m
        break

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC121 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
h = []
for _ in range(n):
    a,b = LI()
    heappush(h,(a,b))

cnt = 0
ans = 0
while cnt < m:
    a,b = heappop(h)
    if b + cnt <= m:
        cnt += b
        ans += a*b
    else:
        ans += (m-cnt)*a
        cnt = m
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])
answer = 0
for ab in AB:
    if M - ab[1] <= 0:
        answer += M * ab[0]
        break
    else:
        answer += ab[0] * ab[1]
        M -= ab[1]
print(answer)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
import copy
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, m = map(int, input().split())
data = getSomeInputListInt(n)

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

dataSum = np.zeros(n)

for i in range(n):
    dataSum[i] = dataSorted[i][1]
    if i == 0:
        continue
    else:
        dataSum[i] += dataSum[i-1]

tmpNum = 0
tmpSa = 0
for i in range(n):
    if dataSum[i] >= m:
        tmpNum = i
        if i == 0:
            tmpSa = m
            break
        else:
            tmpSa = m - dataSum[i-1]
            break

ans = 0
for i in range(tmpNum):
    ans += dataSorted[i][0] * dataSorted[i][1]

ans += dataSorted[tmpNum][0] * tmpSa

print(int(ans))

Submission status: Accepted

Solution code:
from operator import itemgetter
n,m = map(int,input().split())
AB = []
sAB = []

s = 0
c = 0

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

sAB = sorted(AB,key=itemgetter(0))

for i,(a,b) in enumerate(sAB):
    if c + b < m:
        c += b
        s += a*b
    elif c + b >= m:
        for j in range(b):
            if c < m:
                c += 1
                s += a
            else:
                break

print(s)

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())
A, B = zip(*sorted(zip(A, B)))

n = 0
ans = 0
i = 0

while n != M:
    if n + B[i] > M:
        ans += (M - n) * A[i]
        n = M
    else:
        ans += A[i] * B[i]
        n += B[i]
    i += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

ans = 0
for k, v in sorted(d.items(), key=lambda x: x[0]):
    if M == 0:
        break
    q = min(v, M)
    M -= q
    ans += k * q

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(reverse = True)
count = 0
ans = 0
while count < m:
  l = ab.pop()
  for i in range(l[1]):
    count += 1
    ans += l[0]
    if count == m:
      break
print(ans)

Submission status: Accepted

Solution code:
N,M = [int(s) for s in input().split()]
lst=[]
for i in range(N):
  lst.append([int(s) for s in input().split()])
lst.sort()
count=0
cost=0
for e in lst:
  count+=e[1]
  cost+=e[0]*e[1]
  if(count>=M):
    cost-=e[0]*(count-M)
    break
print(cost)

Submission status: Accepted

Solution code:
N,M = [int(s) for s in input().split()]
lst=[]
for i in range(N):
  lst.append([int(s) for s in input().split()])
# lst = sorted(pre_lst, key=lambda x: x[0])
lst.sort()
count=0
cost=0
for e in lst:
  count+=e[1]
  cost+=e[0]*e[1]
  if(count>=M):
    cost-=e[0]*(count-M)
    break
print(cost)

Submission status: Accepted

Solution code:
N,M = [int(s) for s in input().split()]
pre_lst=[]
for i in range(N):
  pre_lst.append([int(s) for s in input().split()])
lst = sorted(pre_lst, key=lambda x: x[0])
count=0
cost=0
for e in lst:
  count+=e[1]
  cost+=e[0]*e[1]
  if(count>=M):
    cost-=e[0]*(count-M)
    break
print(cost)

Submission status: Accepted

Solution code:
n, m = map(lambda x: int(x), input().split())
drinks = []
for _ in range(n):
    a, b = map(lambda x: int(x), input().split())
    drinks.append((a, b))
    
sorted_drinks = sorted(drinks, key=lambda x: x[0])

num_drinks = 0
sum_prices = 0
for price, num in sorted_drinks:
    num_drinks += num
    sum_prices += num * price
    if num_drinks > m:
        sum_prices -= (num_drinks - m) * price
        break
    elif num_drinks == m:
        break

print(sum_prices)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
  from builtins import int, list, map, min
  readline = stdin.readline
  n, m = map(int, readline().split())
  data = [list(map(int, readline().split())) for _ in range(n)]
  data.sort(key = lambda x: x[0])
  purchased = 0
  amount = 0
  for a, b in data:
    t = min(m - purchased, b)
    amount += t * a
    purchased += t
    if purchased == m:
      break
  print(amount)
main()

Submission status: Accepted

Solution code:
from sys import stdin
def main():
  from builtins import int, list, map, min
  readline = stdin.readline
  n, m = map(int, readline().split())
  data = [list(map(int, readline().split())) for _ in range(n)]
  data.sort(key = lambda x: x[0])
  purchased = 0
  amount = 0
  for v in data:
    t = min(m - purchased, v[1])
    amount += t * v[0]
    purchased += t
    if purchased == m:
      break
  print(amount)
main()

Submission status: Accepted

Solution code:
import sys
import math

n,m = map(int, input().split())
ab = []

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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
N, M = map(int, input().split())
l = []
for _ in range(N):
    A, B = map(int, input().split())
    heappush(l, (A, B))
now = 0
ans = 0
for i in range(N):
    a, b = heappop(l)
    if now + b < M:
        now += b
        ans += a * b
    else:
        ans += a * (M - now)
        print(ans)
        exit()
print(ans)

Submission status: Accepted

Solution code:
def energy_drink_collector(n,m,dic):
    count = 0
    v = 0
    stores = sorted(dic.keys())

    for i in range(n):
        c = stores[i]
        s = dic[c]
        v += s

        if v >= m:
            _s = s - (v - m)
            count += c * _s
            break
        else:
            count += c * s

    return count

if __name__ == '__main__':
    n,m = map(int,raw_input().split())
    a = [0]*n
    b = [0]*n
    dic = {}
    for i in range(n):
        a[i],b[i] = map(int,raw_input().split())
        dic[a[i]] = 0

    for i in range(n):
        dic[a[i]] = dic[a[i]] + b[i]

    print energy_drink_collector(n,m,dic)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
list_A = []
list_B = []
for i in range(N):
    A,B = map(int,input().split())
    list_A.append(A)
    list_B.append(B)
lists = list(zip(list_A,list_B))
lists.sort()
num = 0
Sum = 0
for a,b in lists:
    num += b
    if  num > M:
        n = num - M
        b -= n 
        Sum += a * b
        break
    else:
        Sum += a * b
print(Sum)

Submission status: Accepted

Solution code:
n,m = list(map(int,input().split()))
a = [list(map(int,input().split())) for i in range(n)]
aa = sorted(a, key=lambda x:x[0])
pay = 0
drink = 0
for i in range(n):
    buy = 0
    while buy < aa[i][1]:
        pay += aa[i][0]
        buy += 1
        if drink + buy == m:
            break
    drink += buy
    if drink == m:
        break
print(pay)

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

counter = 0
ans = 0
for i in range(N):

    for j in range(ab[i][1]):
        counter += 1
        ans += ab[i][0]

        if counter >= M:
            print(ans)
            exit()

Submission status: Accepted

Solution code:
from operator import itemgetter
n, m = map(int, input().split())
abab = [list(map(int, input().split())) for _ in range(n)]
abab.sort(key=itemgetter(0))
y = h = 0
for a, b in abab :
    if h+b < m : y+=a*b ; h+=b
    elif h < m <= h+b : y+=a*(m-h) ; print(y) ; break

Submission status: Accepted

Solution code:
I = lambda: map(int, input().split())
n, m = I()
A = sorted([list(I()) for _ in range(n)])
c = p = 0
for a, b in A:
    if c >= m: break
    p += a*min(b, m-c)
    c += b
print(p)

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

    tot, budget = 0, 0
    for i in range(N):
        if tot + AB[i][1] <= M:
            tot += AB[i][1]
            budget += AB[i][0] * AB[i][1]
            if tot == M:
                return budget
        else:
            budget += (M - tot) * AB[i][0]
            return budget

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
counter = 0
X =[]
for i in range(N):
    A,B = map(int,input().split())
    X.append([A,B])
ansX = sorted(X)
rest = M
ans = 0
for i in range(N):
    if rest <= ansX[i][1]:
        ans +=rest*ansX[i][0]
        rest= 0
        break
    elif rest > ansX[i][1]:
        ans+= ansX[i][1]*ansX[i][0]
        rest-=ansX[i][1]

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ab = sorted([list(map(int, input().split())) for _ in range(n)])
cnt = 0
t = 0
for i in ab:
    mi = min(m - cnt, i[1])
    t += i[0] * mi
    cnt += mi
    if cnt == m:
        print(t)
        exit()

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split(' ')]
ab = sorted([[int(x) for x in input().split(' ')] for _ in range(int(n))], key=lambda x: x[0])
res = 0
num = 0
for a, b in ab:
    if num + b >= m:
        print(res + a * (m - num))
        exit()
    else:
        res += a * b
        num += b

Submission status: Accepted

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

def main():
    N,M = map(int, input().split())

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

    A.sort()
    ans = 0
    for a,b in A:
        if M == 0:
            break
        n = M if M <= b else b
        M -= n
        ans += n * a
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

A.sort()
ans = 0
for a,b in A:
    if M == 0:
        break
    n = M if M <= b else b
    M -= n
    ans += n * a
print(ans)

Submission status: Accepted

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

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

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

x = 0
for a in A:
    if M == 0:
        break
    while a[1] > 0:
        a[1] -= 1
        M -= 1
        x += a[0]
        if M == 0:
            break
print(x)

Submission status: Accepted

Solution code:

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

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

total = 0
remain = M
for x in A:
    yen, num = x
    purchase = min(num, remain)
    total += (yen * purchase)
    remain -= purchase
    if remain == 0:
        print(total)
        exit()

Submission status: Accepted

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

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:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,M = II()
A,B = Line(N)
c = []
for i in range(N):
    c.append([A[i],B[i]])

c.sort()
ans = 0
count = 0

for i in range(N):
    x = min(c[i][1], M-count)
    ans += x*c[i][0]
    count += x
    if count==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])

# print(N, M)
# print(AB)

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

count = 0
cost = 0
for a, b in AB:
    if count + b <= M:
        count += b
        cost += a * b
    else:
        diff = M - count
        count += diff
        cost += a * diff

    if count == M:
        break

print(cost)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
drink = {}
for _ in range(n):
    a, b = map(int, input().split())
    if a in drink:
        drink[a] = drink[a]+b
    else:
        drink[a] = b
drink = list(drink.items())
drink.sort()

num = m
cost = 0
for i in range(n):
    p = min(num, drink[i][1])
    cost += drink[i][0]*p
    num -= p
    if num == 0:
        break
print(cost)

Submission status: Accepted

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

sum1 = 0
left = M 
for i in range(N):
    a,b = Data[i]
    if left <= b:
        sum1 += a * left 
        break
    else:
        sum1 += a * b
        left -= b

print(sum1)

Submission status: Accepted

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

for i in range(n):
	a,b = map(int,input().split())
	x.append((a,b))
x.sort()

ans = 0

for j,k in x:
	if k<m:
		ans += j*k
		m -= k
	else:
		ans += j*m
		break
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())))
    
sab = sorted(ab,key=lambda x:x[0])
cost = 0
for i in sab:
	if m - i[1] > 0:
		m -= i[1]
		cost += i[0] * i[1]
	else:
		for j in range(m):
			cost += i[0]
		break
print(cost)

Submission status: Accepted

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

c = 0
ans = 0
for i,j in AB:
  c += j
  if c < m:
    ans += i*j
  else:
    ans += i*(m-(c-j))
    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()
a=0
ans=0
for i,j in l:
    for k in range(int(j)):
        if a>=m:
            break
        else:
            ans+=int(i)
            a+=1
 
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

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

l.sort()
ans=0
for i,j in l:
    a=min(m,int(j))
    ans+=a*int(i)
    m-=a
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(N)]
li=sorted(li,key=lambda x: x[0])
m_sum=0
for i in li:
	buy=min(M,i[1])
	m_sum+=i[0]*buy
	M-=buy
print(m_sum)

Submission status: Accepted

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

list.sort()
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:

n,m=map(int,input().split())
A=[[0 for i in range(2)] for j in range(n)]
ans=0

for i in range(n):
  A[i][0],A[i][1]=map(int,input().split())

A.sort()

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:
# ABC 121 C
from heapq import heappop, heappush
n, m = [int(el) for el in input().split(' ')]
X =[]
for i in range(n):
    a, b = [int(el) for el in input().split(' ')]
    heappush(X, (a,b))
total = 0
price = 0
for i in range(n):
    a, b = heappop(X)
    if total + b < m:
        price += a*b
        total += b
    else:
        price += a*(m-total)
        print(price)
        break

Submission status: Accepted

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

s=sorted(s,key=lambda x:x[0])
cost=0
for i in range(n):
  if m>s[i][1]:
    m-=s[i][1]
    cost+=s[i][0]*s[i][1]
  else:
    cost+=s[i][0]*m
    m=0
print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict as dd

# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(*args):
    return tuple(p(v) for p, v in zip(args, input().split()))

# エントリーポイント
def main():
    N, M = parse(int, int)

    AB = dd(int)
    for _ in range(N):
        a, b = map(int, input().split())  # parse(int, int)
        AB[a] += b
    shops = sorted(AB.keys())

    m, cost = M, 0
    for price in shops:
        count = AB[price]
        if m <= count:
            cost += price * m
            break
        else:
            cost += price * count
            m -= count
    print(cost)

main()

Submission status: Accepted

Solution code:
from collections import defaultdict as dd

# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(*args):
    return tuple(p(v) for p, v in zip(args, input().split()))

# エントリーポイント
def main():
    N, M = parse(int, int)

    AB = dd(int)
    for _ in range(N):
        a, b = parse(int, int)
        AB[a] += b
    shops = sorted(AB.keys())

    m, cost = M, 0
    for price in shops:
        count = AB[price]
        if m <= count:
            cost += price * m
            break
        else:
            cost += price * count
            m -= count
    print(cost)

main()

Submission status: Accepted

Solution code:
from sys import stdin
import math

N,M = [int(x) for x in stdin.readline().rstrip().split()]
ab_dict = {}
amount = 0
money = 0

for _ in range(N):
  a,b = [int(x) for x in stdin.readline().rstrip().split()]
  if a in ab_dict: ab_dict[a] += b
  else: ab_dict[a] = b

ab_dict = sorted(ab_dict.items())

for a, b in ab_dict:
  if(b > M - amount):
    money += a * (M - amount)
    break
  else:
    money += a * b
    amount += b

print(money)

Submission status: Accepted

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

ab.sort()
sum_money = 0
for i in range(n):
    if m > ab[i][1]:
        sum_money += ab[i][0]*ab[i][1]
        m -= ab[i][1]
    else:
        sum_money += ab[i][0]*m
        break

print(sum_money)

Submission status: Accepted

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