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:
import itertools

n,m = map(int, input().split())
AB = []
ans = 0
for n in range(n):
    a,b = map(int,input().split())
    AB.append([a,b])
AB.sort()
A,B = zip(*AB)
A,B = list(A), list(B)

i = list(_ for _ in itertools.accumulate(B) if _ <= m)
if i == []:
    ans = A[0]*m
else:
    B = B[:len(i)] + [m-i[-1]]
    A = A[:len(B)]

    ans = sum([a*b for a,b in zip(A,B)])
print(ans)

Submission status: Accepted

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

J.sort()
total = 0
for i in range(N):
    if M - J[i][1] < 0:
        total += M * J[i][0]
        break
    else:
        total += J[i][0]*J[i][1]
        M = M - J[i][1]
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
data = []
for n in range(N):
  A_n, B_n = map(int, input().split(' '))
  data.append([A_n,B_n])
data = sorted(data)
  
# calc answer
drink = 0
ans = 0
for d in data:
  a, b = d
  drink += b
  ans += a*b
  if drink > M:
    ans -= a*(drink-M)
    break
  
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

NM = list(map(int, input().split()))
M = NM[1]
Okane = 0

AB = [[0 for i in range(2)] for j in range(NM[0])]

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

AB.sort()

for i in range(NM[0]):
  if M >= AB[i][1]:
    Okane = Okane + AB[i][0] * AB[i][1]
    M = M - AB[i][1]
  elif M < AB[i][1]:
    Okane = Okane + AB[i][0] * M
    M = 0
    
  if M == 0:
    break
      
print(Okane)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

A = []
B = []
for i in range(N):
    a,b = sys.stdin.readline().strip().split()
    A.append(int(a))
    B.append(int(b))

#print(A)
#print(B)
comb = zip(A,B)
comb = sorted(comb)
#print(comb)
#print(comb[0][1])


num=0
money=0
num_tmp=0
while(num<M):
    if(len(comb)>1):
        if(M-num<comb[0][1]):
            num_tmp = M - num
            num += num_tmp
        else:
#            print("else num_tmp",B[cheap_index])
            num_tmp = comb[0][1]
            num += num_tmp
        money += comb[0][0] * num_tmp
        comb.pop(0)
#        print("num_tmp",num_tmp)
#        print("m,num", money,num)
    else:
        num_tmp = M - num
        num += num_tmp
        money += comb[0][0] * num_tmp
#        print(money,num)
        
print(money)

Submission status: Accepted

Solution code:
num = input()
N, M = [int(x) for x in num.split()]

dic = []
for n in range(N):
    num = input()
    a, b = [int(x) for x in num.split()]
    dic.append((a, b))

sort = sorted(dic, key=lambda x: x[0])
count = 0
money = 0
ind = 0
while count < M:

    if (M - count) > sort[ind][1]:
        money += sort[ind][0] * sort[ind][1]
        count += sort[ind][1]
    else:
        money += sort[ind][0] * (M - count)
        count += (M - count)

    ind += 1
print(money)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split())
t = [tuple(map(int, input().split())) for i in range(N)] 
t = sorted(t, key = lambda tt: tt[0])
n = 0
ans = 0
for a,b in t:
    if n+b < M:
        ans += a*b
        n += b
    else:
        ans += (M-n)*a
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
sumans = 0
ans = 0

list = [[0 for i in range(2)] for j in range(n)]

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

for i in range(n):
	for j in range(int(list[i][1])):
		sumans += list[i][0]
		ans += 1
		if ans == m:
			print(sumans)
			sys.exit()

Submission status: Accepted

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

total = 0
count = 0

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

print(total)

Submission status: Accepted

Solution code:


def main():
    n, m = [int(i) for i in input().split()]
    e = [[int(i) for i in input().split()] for i in range(n)]

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

    count = p_sum = 0
    for shop in e:
        for i in range(shop[1]):
            count += 1
            p_sum += shop[0]
            if count >= m:
                return p_sum


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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
drinks = [input().split() for i in range(n)]
drinks = [[int(j) for j in i] for i in drinks]
drinks.sort()
money = 0
mm = m
for i in drinks:
    if mm-i[1]<=0:
        money += i[0]*mm
        break
    else:
        money += i[0]*i[1]
        mm += -i[1]
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(key=lambda x: x[0])

cnt = 0
fee = 0
flg=0
for j in A:
    for k in range(j[1]):
        cnt += 1
        fee += j[0]
        if cnt == M:
            print(fee)
            flg=1
            break
    
    if flg==1:
        break

Submission status: Accepted

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

Submission status: Accepted

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

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

    ans,ct = 0,0
    for a,b in sorted(x):
        for _ in range(b):
            ans += a
            ct += 1
            if ct == m:
                return ans
    return -1

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

Submission status: Accepted

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

lis.sort()

count=0
money=0
ind=0
while count+lis[ind][1]<M:
    count+=lis[ind][1]
    money+=lis[ind][0]*lis[ind][1]
    ind+=1
    #print(ind)
    

money+=(M-count)*lis[ind][0]
print(int(money))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n, m = map(int, raw_input().split())

dic = {} # yen: many

for i in range(n):
    a, b = map(int, raw_input().split())
    if a in dic: dic[a] += b
    else: dic.update({a:b})

dic = sorted(dic.items())

count = 0
yen = 0
for k, v in dic:

    if v < m-count:
        yen += k*v
        count += v
    else:
        yen += k*(m-count)
        count += m-count
        print yen
        break

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
n, m = map(int, input().split())
a = []
b = []
ab = []
for i in range(n):
    abi = list(map(int, input().split()))
    ab.append(abi)
ab.sort(key = lambda x:(x[0]))
honsu = 0
kingaku = 0
j = 0
while honsu < m:
    honsu = honsu + ab[j][1]
    j = j + 1

honsu2 = 0
for i in range(j-1):
    honsu2 = honsu2 + ab[i][1]
    kingaku = kingaku + ab[i][0]*ab[i][1]
kingaku = kingaku + ab[j-1][0]*(m - honsu2)
print(kingaku)

Submission status: Accepted

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

ab_list = sorted(ab_list)

buy = 0
money = 0
j = 0


while (buy < m):
    buy += ab_list[j][1]
    money += ab_list[j][0] * ab_list[j][1]
    j += 1

print(money-(ab_list[j-1][0]*(buy-m)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split(" "))
A_list = []
B_list = []
total = 0

for i in range(N):
    A, B = map(int, input().split(" "))
    A_list.append(A)
    B_list.append(B)

z = list(zip(A_list, B_list))
z.sort()

for a, b in z:
    if b > M:
        total += M * a
        break
    else:
        total += b * a
        M -= b

print(total)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
shops = []
money = 0
for i in range(n):
  shops.append(list(map(int, input().split())))
shops.sort(key=lambda x: x[0])
for shop in shops:
  if shop[1] <= m:
    money += shop[0] * shop[1]
    m -= shop[1]
  elif shop[1] > m:
    money += shop[0] * m
    break
print(money)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ab = []
prod = [0]
prod_cum = [0]

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


#%%
ab.sort()

for i in range(n):
    a, b = ab[i][0],ab[i][1]
    # prod.append(a*b)
    prod_cum.append(a*b + prod_cum[-1])
    num_cum.append(b + num_cum[-1])
ab = [[0,0]] + ab
# print(ab)
# print(prod)
# print(prod_cum)
# print(num_cum)
# print(n, m, ab)
# n, m, ab = 4, 30, [[6, 18], [2, 5], [3, 10], [7, 9]]


#%%
ans = 0
prod = 0
for i in range(0, n+1):
    if num_cum[i] >= m:
        print(prod_cum[i] - ab[i][0] * (num_cum[i]-m))
        break



#%%
# ans = 0
# num = 0
# while num < m:
#     a0 = ab[0][0]
#     b0 = ab[0][1]
#     if b0 >= m - num:
#         ans += a0 * (m - num)
#         break
#     else:
#         ans += a0 * b0
#         num += b0
#         ab.remove([a0, b0])
#%%
# print(ans)
#%%

Submission status: Accepted

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

for i in range(n) :
    a,b = map(int,input().split())
    ls[i] = [a,b]
    
ls.sort(key=lambda x:x[0])

c = m + 0
ans = 0

while c > 0 :
    if c >= ls[0][1] :
        ans += ls[0][0] * ls[0][1]
        c -= ls[0][1]
        ls.pop(0)
    else :
        ans += ls[0][0] * c
        c = 0

print(ans)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
A = []
for i in range(N):
    A.append(tuple(map(int,input().split())))
f,s = 0,0
for i,j in sorted(A):
    f += j
    s += j * i
    #print(" ",j,s,dic[i],M)
    if(f >= M):
        print( s - i * (f-M) )
        break

Submission status: Accepted

Solution code:
import sys

if __name__ == "__main__":
    para_list = list(map(int, input().split()))
    N = para_list[0]
    M = para_list[1]

    A = [[0]*2]*N
    for i in range(N):
        A[i] = list(map(int, input().split()))
    B = sorted(A,key=lambda l:l[0])

    price = 0
    k = 0
    while(M>0):
        if M<B[k][1]:
            price +=B[k][0]*M
        else:
            price +=B[k][0]*B[k][1]
        M -= B[k][1]
        k += 1
    print(price)

Submission status: Accepted

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

pay=0
num=0
for a,b in AB:
    m=min(M-num, b)
    num=num+m
    pay=pay+a*m
    if num >= M:
        print(pay)
        break

Submission status: Accepted

Solution code:
N,M= map(int,input().split())
dptaples = []
for i in range(N):
    An,Bn = map(int,input().split())
    dptaples.append((An,Bn))
dptaples.sort()
maxnum = 0
maxvalue = 0
for key,value in dptaples:
    if M>value+maxnum:
        maxvalue+=key*value
        maxnum += value
    else:
        maxvalue+=((M-maxnum)*key)
        break
print(maxvalue)

Submission status: Accepted

Solution code:
from collections import OrderedDict
if __name__ == "__main__":
    NM = list(map(int, input().split()))
    n = NM[0]
    m = NM[1]
    mydict = {}
    for i in range(n):
        money_can = list(map(int, input().split()))
        money = money_can[0]
        can = money_can[1]
        if(money not in mydict.keys()):
            mydict[money] = []
        mydict[money].append(can)
    buynow = 0
    moneynow = 0

    mydict = OrderedDict(sorted(mydict.items()))
    for i in mydict.keys():
        if(buynow >= m):
            break
        for canNO in mydict[i]:
            if(buynow >= m):
                break
            if(canNO + buynow < m):
                moneynow += canNO*i
                buynow += canNO
            else:
                need = m - buynow
                moneynow += need*i
                buynow += need
        else:
            continue
    print(moneynow)

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[i] = a
    B[i] = b

C = [(k, A[k]) for k in sorted(A, key=A.get)]

total = 0

for key, value in C:
    if (M < 0):
        break
    if (M > B[key]):
        total = total + A[key]*B[key]
    else:
        total = total + A[key]*M
    M -= B[key]
    # print(key, A[key], B[key], total, M)

print(total)

Submission status: Accepted

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

rm = list(sorted(L))

n = 0
sum = 0
for i in range(M):
  if rm[n][1] == 0:
    n += 1
  rm[n][1] -= 1
  sum += rm[n][0]
  
print(sum)

Submission status: Accepted

Solution code:
N, M = map(long, raw_input().split())
A = list()
for i in xrange(N):
    a, b = map(long, raw_input().split())
    A.append((a,b))

A.sort()

count = 0L
cost = 0L

for (a, b) in A:
    if count + b >= M:
        cost += a * (M - count) 
        print str(cost)
        break
    else:
        count += b
        cost += a * b

Submission status: Accepted

Solution code:
import numpy as np
n, m = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    a_next, b_next = map(int, input().split())
    a.append(a_next)
    b.append(b_next)

a_sorted_index = np.argsort(a)

for i in range(1000000):
    min_index = a_sorted_index[i]
    if m - b[min_index] <=0:
        break
    m = m - b[min_index]
    ans += a[min_index]*b[min_index]

ans += m * a[min_index]
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]), int(input_raw[1])]
    D = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [A, B] = [int(input_raw[0]), int(input_raw[1])]
        D.append((A, B))
    D.sort()

    cost = 0L
    for i in range(N):
        if M <= 0:
            break
        M -= D[i][1]
        buy = D[i][1]
        if M < 0:
            buy += M

        cost += long(buy) * D[i][0]
    print cost

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = {}
for n in range(N):
    a, b = list(int(x) for x in input().split())
    if a in AB:
        AB[a] += b
    else:
        AB[a] = b
yen = 0
AB = sorted(AB.items())
for a, b in AB:
    if b <= M:
        yen += a*b 
        M -= b
    else:
        yen += a*M
        M = 0
print(yen)

Submission status: Accepted

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

bsum = 0
money.sort(key=lambda x: x["a"])
for i in range(n):
  bsum0 = bsum
  bsum += money[i]["b"]
  if m <= bsum:
    num = i
    break

ans = 0
for j in range(num):
  ans += money[j]["a"] * money[j]["b"]
ans += (m-bsum0) * money[num]["a"]
print(ans)

Submission status: Accepted

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

D = [C[i][0] for i in range(N)]

CNT_A = 0
CNT_B = 0
SUM = 0

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

for i in range(N):
    if C[i][1] < M - CNT_B:
        SUM = SUM + C[i][0] * C[i][1]
        CNT_B = CNT_B + C[i][1]
    else:
        SUM = SUM + C[i][0] * max(0, M - CNT_B)
        CNT_B = CNT_B +  max(0, M - CNT_B)

print(SUM)

Submission status: Accepted

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

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

for s in shops:
    if s[1] < m:
        m -= s[1]
        total += s[0]*s[1]
    else:
        total += s[0]*m
        print(total)
        exit()

Submission status: Accepted

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

def io_generator():
	return input()

#+++++++++++++++++++

def main(io):
	n,m= map(int, io().split())
	bb=[]
	for _ in range(n):
		(a,b)= map(int, io().split())
		bb.append((a,b))
	
	bb.sort(key=lambda a: a[0])
	zm=m
	cost=0
	for a,b in bb:
		if zm >b:
			zm=zm-b
			cost+=a*b
		else:
			cost+=zm*a
			break
		
	return cost

#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Submission status: Accepted

Solution code:
import numpy as np

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

vec = np.array(arr)
sort = vec[np.argsort(vec[:, 0])]

amnt = 0
num = 0
for i in range(len(sort)):
    if num + sort[i][1] >= M:
        x = M - num
        amnt += sort[i][0] * x
        break
    else:
        amnt += sort[i][0] * sort[i][1]
        num += sort[i][1]
print(amnt)

Submission status: Accepted

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

def main():
	N, M = map(int, input().split(" "))
	
	store = []
	for i in range(N):
		tmp = input().split(" ")
		store.append([int(s) for s in tmp])
	
	store.sort()
	
	ans = 0
	for i, val in enumerate(store):
		if val[1] >= M:
			ans = ans + (val[0] * M)
			M = 0
		if val[1] < M:
			ans = ans + (val[0] * val[1])
			M = M - val[1]
		if M <= 0 :
			break
	
	print(ans)
			
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from sys import stdin

N,M = map(int, input().split(' '))
shop = [stdin.readline().rstrip().split(' ') for i in range(N)]

for i in range(N):
  shop[i][0] = int(shop[i][0])
shop = sorted(shop)
  
sum = 0
for i in range(N) :
  if M > int(shop[i][1]) :
    sum += int(shop[i][0]) * int(shop[i][1])
    M -= int(shop[i][1])
  else :
    sum += int(shop[i][0]) * M
    M = 0
    break
    
print(sum)

Submission status: Accepted

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

c = p = i = 0

while c < M:
    if c + d[i][1] <= M:
        c += d[i][1]
        p += d[i][0] * d[i][1]
        i += 1
    else:
        p += d[i][0] * (M - c)
        break

print(p)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = []
b = []
raw = []
ans = 0
for i in range(n):
  raw.append(list(map(int,input().split())))
raw.sort(key=lambda x:x[0])
for i in range(n):
  ans = ans + raw[i][0] * min(raw[i][1],m)
  m = m - min(raw[i][1],m)
  if m == 0:
    break
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

import sys

def parse_input(lines_as_string = None):

    lines = []
    if lines_as_string is None:
        lines.append(input())
        tokens = lines[0].split(" ")
        n = int(tokens[0])
        m = int(tokens[1])
        for i in range(n):
            lines.append(input())
    else:
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        print("lines=%s" % lines)
        tokens = lines[0].split(" ")
        n = int(tokens[0])
        m = int(tokens[1])

    a = []
    b = []
    for i in range(n):
        tokens = lines[i+1].split(" ")
        a.append(int(tokens[0]))
        b.append(int(tokens[1]))

    return (n, m, a, b)

def solve(n, m, a, b):

    
    s_a = sorted([(i, e) for i, e in enumerate(a)], key=lambda e: e[1])
#    print("s_a=%s" % s_a)

    current = m
    total = 0
    tmp = 0
    for i, e in s_a:
        if current > b[i]:
            tmp = e * b[i]
            current = current - b[i]
        else:
            tmp = e * current
            current = 0

        total = total + tmp

#        print("i=%d, total=%d" % (i, total))
        if current == 0:
            break

    return total

def main():

    print("%d" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N)]
KOSU=0
money=0
flag=False
AB.sort()
for i in range(N):
  KOSU+=AB[i][1]
  money+=AB[i][0]*AB[i][1]
  if KOSU>=M:
    KOSU-=AB[i][1]
    money-=AB[i][0]*AB[i][1]
    money+=(M-KOSU)*AB[i][0]
    break
print(money)

Submission status: Accepted

Solution code:
def unzip(li):
  return map(list, zip(*li))

N, M = map(int, input().split())
tmp_C = [0 for i in range(N)]

for i in range(N):
  a,b = map(int, input().split())
  tmp_C[i] = (a,b)
  
C = sorted(tmp_C)
A, B = unzip(C)
ans = 0
count = 0
cur = 0
while(count < M):
  min_A = A[cur]
  B_min = B[cur]
  if count + B_min >= M:
    print(ans + min_A * (M - count))
    break
  else:
    ans += min_A * B_min
    count += B_min
    cur += 1

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
drinks = dict()
for _ in range(n):
    (a, b) = map(int, input().split())
    if not a in drinks:
        drinks[a] = 0
    drinks[a] += b
p = 0
keys = sorted(drinks.keys())
for k in keys:
    if drinks[k] < m:
        p += k * drinks[k]
        m -= drinks[k]
    else:
        p += k * m
        break
print(p)

Submission status: Accepted

Solution code:
import numpy as np

N,M = map(int, input().split())
C = np.array([list(map(int, input().split())) for i in range(N)])
A = C.T[0]
B = C.T[1]

B = B[np.argsort(A)]
A = np.sort(A)

s = 0
m = M
i = 0
while(m > 0):
    if B[i] <= m:
        s += A[i] * B[i]
        m -= B[i]
        i += 1
    else:
        s += A[i] * m
        m = 0
print(s)

Submission status: Accepted

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

A = []
B = []
for i in range(N):
	A.append(L[i][0])
	B.append(L[i][1])
num = 0
cost = 0
i = 0
while num < M:
	cost = cost + A[i]*min(B[i], M - num)
	num = num + min(B[i], M - num)
	i = i + 1

print(cost)

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
A_B = [list(map(int, input().split())) for i in range(N)]
A_B = sorted(A_B)

yen = 0
cnt = 0
i = 0

while M - (cnt + A_B[i][1]) > 0:
    yen += (A_B[i][0] * A_B[i][1])
    cnt += A_B[i][1]
    i += 1

yen += (M - (cnt)) * A_B[i][0]

print(yen)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    shop_drink = [[int(i) for i in input().split()] for n in range(N)]
    # ドリンクの金額でソートする
    # sorted(shop_drink)
    shop_drink.sort()
    buy_drinks = 0
    buy_amount = 0

    for sd in shop_drink:
        # 購入可能数と購入済数の和が，目標数に満たない場合
        if buy_drinks + sd[1] < M:
            # 購入済数を増やす
            buy_drinks += sd[1]
            # 合計金額に加算する
            buy_amount += sd[0] * sd[1]
        # 購入可能数と購入済み数の和が，目標数以上となる場合
        else:
            # 必要数を計算
            temp = M - buy_drinks
            buy_amount += sd[0] * temp
            print(buy_amount)
            return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a = []
b = []
ans = 0
for i in range(n):
    tempa,tempb = map(int,input().split())
    a.append(tempa)
    b.append(tempb)
test = list(zip(a,b))
test = sorted(test)
i=0
while m>0:
    if m-test[i][1] >=0:
        ans += test[i][0]*test[i][1]
        m -=test[i][1]
    else:
        ans += test[i][0]*m
        m=0
        print(ans)
        exit()
    i+=1
print(ans)

Submission status: Accepted

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

M_res = 0
M_res +=M

ANS = 0

while M_res > 0:
    for i in range(len(List)):
        if M_res <= List[i][1]:
            ANS +=  List[i][0] * M_res
            M_res -=M_res
        else:
            ANS += List[i][0] * List[i][1]
            M_res -=List[i][1]
        if M_res ==0:
            break
print(ANS)

Submission status: Accepted

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

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

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

pay = 0
hold = 0
for i in range(N):
    quantity = l[i][1]
    price = l[i][0]
    require = M - hold
    
    if require >= quantity :
        hold += quantity
        pay += price * quantity
    elif require < quantity :
        hold += require
        pay += price * require
            
    if hold == M:
        break

print(pay)

Submission status: Accepted

Solution code:
from math import ceil


def energy_drink_collector(N: int, M: int, AB: list)->int:
    total_price = 0
    total_num = 0
    for price, stock in sorted(AB, key=lambda x: x[0]):
        if total_num + stock >= M:
            total_price += price * (M - total_num)
            break
        else:
            total_price += price * stock
            total_num += stock
    return total_price


if __name__ == "__main__":
    N = 0
    N, M = map(int, input().split())
    AB = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = energy_drink_collector(N, M, AB)
    print(ans)

Submission status: Accepted

Solution code:
N, M = (int(i) for i in input().split())
L = []
for i in range(N):
  a, b = (int(i) for i in input().split())
  L.append((a, b))
L.sort()
ans = 0
for a, b in L:
  if M >= b:
    ans += a*b
    M -= b
  else:
    ans += M*a
    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)]
cnt = 0
yen = 0

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

for i in range(N):
    if M >= AB[i][1]:
        cnt += AB[i][1]
        yen += AB[i][0] * AB[i][1]
        M -= AB[i][1]
    elif M < AB[i][1]:
        cnt += M
        yen += AB[i][0] * M
        M = 0
    if M == 0:
        print(yen)
        break

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().split())
ab = sorted([map(int, raw_input().split()) for i in range(n)])
ret = 0
for (a, b) in ab:
    buy = min(m, b)
    ret += buy * a
    m   -= buy
print ret

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
A=[]
ab={}
s=0
for i in range(n):
    a,b=map(int,input().split())
    if a in ab:
        ab[a]+=b
    else:
        ab[a]=b
        A.append(a)
A=sorted(A)
k=0
while m>0:
    if m>ab[A[k]]:
        m-=ab[A[k]]
        s+=ab[A[k]]*A[k]
    else:
        s+=A[k]*m
        m=0
    k+=1
print(s)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
p = []
for _ in range(N):
    a, b = map(int, input().split())
    p.append((a, b))
p = sorted(p, key=lambda x: x[0])
ans = 0
for a, b in p:
    if b < M:
        ans += a * b
        M -= b
    else:
        ans += a * M
        print(ans)
        exit()

Submission status: Accepted

Solution code:
from sys import stdin


def a(input_string):
    N, M = [int(x) for x in input_string[0].split()]
    C = input_string[1:]
    temp = [string.split() for string in C]
    D = []
    for i in range(len(temp)):
        D.append([int(word) for word in temp[i]])
    #print(D)
    get_count = 0
    min_cost = 10000000000000000
    cost = 0
    D = sorted(D, key=lambda x: x[0])
    for i in range(N):
        for j in range(D[i][1]):
            get_count += 1
            cost += D[i][0]
            if get_count >= M:
                return cost
    return cost


def main():
    input_line = stdin.readlines()
    answer = a(input_line)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

money = 0
drink = 0
for ab in sorted(AB):
    for _ in range(ab[1]):
        money += ab[0]
        drink += 1
        if drink == M:
            print(money)
            quit()