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

A=[]
B=[]

bought = 0
price = 0
for i in range(0, N):
    a,b=list(map(int, input().split(" ")))
    A.append((a, i))
    B.append(b)

A.sort()

i = 0
p=0
n=0
while bought < M:
    p,n = A[i]
    price += p*B[n]
    bought += B[n]
    i += 1

if bought > M:
    price -= (bought-M)*p

print(price)

Submission status: Accepted

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


def solve(n, m, lst):


    lst.sort()

    ans = 0
    r = m
    for v in lst:
        a, b = v
        if r <= b:
            ans += a * r
            break
        r -= b
        ans += a * b

    return ans

def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    lst = []
    for _ in range(n):
        a, b = input().split()
        a = int(a)
        b = int(b)
        lst.append((a, b))


    print(solve(n, m, lst))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M = [int(s) for s in input().split()]
ABs = [[int(s) for s in input().split()] for i in range(N)]
n = M
p = 0

for A, B in sorted(ABs, key=lambda AB: AB[0]):
    nbuy = B if n-B >= 0 else n
    p += A*nbuy
    n -= nbuy
    if n <= 0:
        break
print(p)

Submission status: Accepted

Solution code:
from operator import itemgetter
n,m=map(int,input().split())
a=[]
for i in range (n):
    x,y=map(int,input().split())
    a.append([y,x])

a=sorted(a,key=itemgetter(1))

co=0
for i in range (n):
    if(m>=a[i][0]):
        co+=(a[i][0]*a[i][1])
        m-=a[i][0]

    else:
        co+=(m*a[i][1])
        m=0
print(co)

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

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a_li = []
b_li = []
cnt = 0

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

merge = zip(a_li, b_li)
merge_new = sorted(merge)

x = []
y = []

for a, b in merge_new:
	x.append(a)
	y.append(b)

for a, b in zip(x, y):
	if m > 0 and b <= m:
		cnt += a * b
		m -= b
		if m == 0:
			break
	elif m > 0 and b > m:
		cnt += a * m
		break

print(cnt)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for i in range(n):
    i,j = (map(int, input().split()))
    a += [[i,j]]
a = sorted(a)
count = 0
value = 0
for (i,j) in a:
    if count + j >= m:
        value += (m - count) * i
        break
    else:
        value += i * j
        count += j
print(value)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
index=0
listD = []
while index < n:
    a, b = map(int, input().split())
    listD.append([a, b])
    index+=1
listD.sort()
count=0
money=0
index2=0
while index2 < len(listD):
    if count+listD[index2][1] <= m:
        count+=listD[index2][1]
        money+=listD[index2][0]*listD[index2][1]
    else:
        money+=listD[index2][0]*(m-count)
        count=m
    if count==m:
        break
    index2+=1
print(money)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
list = [list(map(int, input().split())) for x in range(N)]
list.sort(key=lambda x: x[0])
remain = M
cost = 0
for i in range(N):
    store = list[i][1]
    if remain>=store:
        remain -= store
        cost += list[i][0]*store
    elif remain<store:
        cost += list[i][0]*(remain)
        remain = 0
    if remain==0: break
print(cost)

Submission status: Accepted

Solution code:
#coding: utf-8
import numpy as np

n, m = map(int, raw_input().split())
a = np.zeros((n, 2), dtype=np.uint64)
for i in np.arange(n):
    a[i] = np.array(map(int, raw_input().split()))

a = a[np.argsort(a[:,0])]

cost = 0L
for i in np.arange(n):
    if(a[i][1] < m):
        cost += a[i][0] * a[i][1]
        m -= a[i][1]
    else:
        cost += a[i][0] * m
        print(long(cost))
        exit()

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    lis = []
    for i in range(N):
        lis.append(list(map(int, input().split())))
    # 最小のコスト順に買っていけばよい
    # 同じコストのときはどうするか
    lis.sort() # 一列目をソート，二列目を対応してくれる，同じ値のときは二列目が低い方を優先
    sum = 0
    count = 0
    flg = False
    for i in range(N):
        if (count+lis[i][1]<M) :
            sum = sum + lis[i][0]*lis[i][1]
            count = count + lis[i][1] # Bi全部
        elif (count+lis[i][1]==M):
            sum = sum + lis[i][0]*lis[i][1]
            count = count + lis[i][1] # Bi全部
            break
        else:
            for j in range(lis[i][1]):
                sum = sum + lis[i][0]
                count = count + 1
                if count==M:
                    flg = True
                    break
        if flg:
            break

    print(sum)
    # print(count)

main()

Submission status: Accepted

Solution code:
NM = [int(i) for i in input().split()]
N = NM[0]
M = NM[1]
datalist = []
index = 0
cost = 0
num = 1
total_cost = 0
while(N > 0):
    data = [int(i) for i in input().split()]
    datalist.append(data)
    N -= 1
datalist.sort(key=lambda x:x[0])
while(M > 0):
    M -= datalist[index][num]
    total_cost += datalist[index][cost] * datalist[index][num]
    if(M < 0):
        n = abs(M)
        total_cost -= n * datalist[index][cost]
    index += 1
print(total_cost)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = []
p = 0
for n in range(N):
    AB.append(list(map(int, input().split())))
AB.sort()
for s, b in AB:
    p += s * min(M, b)
    M -= min(M, b)
print(p)

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
ABList = [[int(i) for i in input().split()] for _ in range(N)]
ABList.sort(key=lambda x: x[0], reverse=False)

ans = 0
for AB in ABList:
    drink = min(AB[1], M)
    M -= drink
    ans += drink * AB[0]
    if M == 0:
        break

print(ans)

Submission status: Accepted

Solution code:
N_M = [int(x.strip()) for x in input().split()]
price_l = [[int(i) for i in input().split()] for i in range(0, N_M[0])]
price_l.sort(key=lambda x: x[0])
total = 0
remain = N_M[1]
while remain > 0:
    if len(price_l)<1:
        break
    price_n = price_l.pop(0)
    total += price_n[0] * min(price_n[1], remain)
    remain -= min(price_n[1], remain)
print(total)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split())

shops = []

for i in range(N):
    n, m = map(int, input().split())
    shops.append((n, m))

sorted_shops = sorted(shops)

remain = M
value = 0
for i in range(N):
    if sorted_shops[i][1] <= remain:
        value += sorted_shops[i][0] * sorted_shops[i][1]
        remain -= sorted_shops[i][1]
    else:
        value += sorted_shops[i][0] * remain
        break
print (value)

Submission status: Accepted

Solution code:
NM=list(map(int,input().split()))
list1=[[0 for i in range(2)] for j in range(NM[0])]

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

list1.sort()
flag=NM[1]
i=0
count=0
while flag>0:
 if flag>list1[i][1]:
   count=count+list1[i][0]*list1[i][1]
   flag=flag-list1[i][1]
 else:
   count=count+list1[i][0]*flag
   flag=0
 i=i+1

print(count)

Submission status: Accepted

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

lim = 0
s = 0
for a, b in sorted(ABS, key=lambda ab: ab[0]):
  if lim + b >= M:
    print(s + (M - lim) * a)
    exit()

  lim += b
  s += a * b

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]

AB.sort()

ans = 0
for i, j in AB:
	ans += i * min(M, j)
	M -= min(M, j)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np


N, M = map(int, input().split())
A, B = list(), list()
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
A = np.array(A)
B = np.array(B)

n_bins = 0
cost = 0
indices = np.argsort(A)
for idx in indices:
    # enough bins @idx
    if n_bins + B[idx] > M:
        cost += A[idx] * (M - n_bins)
        break
    # buy all bins
    else:
        n_bins += B[idx]
        cost += A[idx] * B[idx]
print(cost)

Submission status: Accepted

Solution code:
x = input().split(" ")
store = int(x[0])
need = int(x[1])
ans = 0
price = {}

for i in range(store):
    y = input().split(" ")
    pri = int(y[0])
    bottles = int(y[1])
    if pri in price.keys():
        price[pri] += bottles
    else:
        price[pri] = bottles
#print(price)
price_sort = sorted(price.items(), key=lambda x: x[0]) #安い順に並び変え
#print(price_sort)
for price_bottles in price_sort: #price_bottles=(価格、本数)になる
    if need == price_bottles[1]:
        ans += price_bottles[1] * int(price_bottles[0])
        break
    elif need > price_bottles[1]:
        ans += price_bottles[1] * int(price_bottles[0])
        need -= price_bottles[1]
    else:
        ans += need * int(price_bottles[0])
        break


print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, M = map(int, input().split())
    A = {}
    B = {}

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

    ans = 0
    for k, v in sorted(A.items(), key=lambda x: x[1]):
        if M > B[k]:
            ans += v * B[k]
            M -= B[k]
            if M < 1:
                break
        else:
            ans += v * M
            break

    print(ans)

Submission status: Accepted

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

abn.sort(key=lambda x:x[0])
ans=0
for ab in abn:
    if M < 0:
        break
    else:
        if M>ab[1]:
            M-=ab[1]
            ans+=ab[0]*ab[1]
        else:
            ans+=ab[0]*M
            break
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = [list(map(int,input().split())) for i in range(n)]
x = sorted(x,key=lambda x:x[0])
t = x[0][1]
res = 0
ans = 0
for i in range(n):
    res += x[i][1]
    ans += x[i][0]*x[i][1]
    if m-res<=0:
        print(ans-((res-m)*x[i][0]))
        exit()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8-*-

n, m = map(int, input().split(" "))

l = []

for i in range(n):
    t1, t2 = map(int, input().split(" "))
    l.append((t1, t2))
l.sort(key=lambda x:x[0])
sum = 0
for i in l:
    if m < i[1]:
        sum += (i[0] * m)
        break
    sum += (i[0] * i[1])
    m -= i[1]

print(sum)

Submission status: Accepted

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

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

ans = 0
sum_drinks = 0 #sum of drinks which Takahashi has bought
for i in range(N):
    drinks_at_shop = PNs[i][1] #number of drinks at i_th shop
    ans += drinks_at_shop*PNs[i][0]
    sum_drinks += drinks_at_shop
    
    over = sum_drinks-M
    if over>=0:
        ans -= (over)*PNs[i][0]
        break

print(ans)

Submission status: Accepted

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

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

ans = 0
sum_drinks = 0 #sum of drinks which Takahashi has bought
for i in range(N):
    nos = PNs[i][1] #number of drinks at i_th shop
    ans += nos*PNs[i][0] #add money he spent at the shop
    sum_drinks += nos
    if sum_drinks>=M:
        ans -= (sum_drinks-M)*PNs[i][0]
        break

print(ans)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    l = [[0, 0] for _ in range(N)]
    for i in range(N):
        l[i] = list(map(int, input().split()))

    l.sort()

    ans = 0
    for i, j in l:
        for _ in range(j):
            if M < 1:
                return ans
            else:
                M -= 1
                ans += i
    return ans


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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    l = [[0, 0] for _ in range(N)]

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

    l.sort()

    ans = 0

    for i, j in l:
        for _ in range(j):
            if M < 1:
                print(ans)
                return 0
            else:
                M -= 1
                ans += i

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N , M = map(int,input().split())
L = []
for _ in range(N):
    L.append(list(map(int,input().split())))
L = sorted(L)
quantity = 0
cost = 0
for x in L:
    if quantity + x[1] < M:
        cost += x[1] * x[0]
        quantity += x[1]
    else:
        cost += max(M - quantity,0) * x[0]
        break
print(cost)

Submission status: Accepted

Solution code:
N, M = (int(x) for x in raw_input().split())
AB = [[int(x) for x in raw_input().split()] for i in xrange(N)]
AB.sort()
result = 0
for ab in AB:
	a, b = ab
	result += min(b, M) * a
	M -= min(b, M)
print(result)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
c = 0
for a, b in L:
    if b <= M:
        c += a * b
        M -= b
        continue
    c += a * M
    break
print(c)

Submission status: Accepted

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

for i in range(N):
    c,s = map(int,input().split())
    store.append([c,s])

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

cost = 0
for line in store:
    if M <= line[1]:
        cost += M*line[0]
        break
    else:
        cost += line[0]*line[1]
        M -= line[1]

print(cost)

Submission status: Accepted

Solution code:
from functools import reduce
# 入力
N, M = map(int, input().split())
A, B = zip(*(map(int, input().split()) for _ in range(N)))

# 1本あたりの値段が安い店から順に購入
ans = reduce(
    lambda acc, t: (
        acc[0] + t[0] * min(t[1], M - acc[1]),
        acc[1] + t[1]
    ) if acc[1] < M else
    acc,
    sorted(zip(A, B)),
    (0, 0)
)[0]

# 出力
print(ans)

Submission status: Accepted

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

for i in range(N):
  I.append(list(map(int, input().split())))
  
I.sort()

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

Submission status: Accepted

Solution code:
my_M = 0
kingaku = 0
A = []
B = []
x = 0
y = 0

N,M = map(int,input().split())
for i in range(N):
    x,y = map(int,input().split())
    A.append(x)
    B.append(y)
C = zip(A,B)
CC = sorted(C)

for j in range(N):
    my_M = my_M + CC[j][1]
    if my_M < M:
        kingaku = kingaku + CC[j][0]*CC[j][1]
    elif my_M == M:
        kingaku = kingaku +CC[j][0]*CC[j][1]
        break
    elif my_M > M:
        kingaku = kingaku +CC[j][0]*(M-(my_M-CC[j][1]))
        break

print(kingaku)

Submission status: Accepted

Solution code:
N, M = map(int, input().split(" "))
price_amount_list = []
for _ in range(N):
    A, B = map(int, input().split(" "))
    price_amount_list.append((A, B))

# 価格順にソート
price_amount_list.sort(key=lambda x: x[0])

buy = 0
sum_money = 0
for i in range(N):
    price = price_amount_list[i][0]
    amount = price_amount_list[i][1]
    if buy + amount < M:
        sum_money += price * amount
        buy += amount
    else:
        sum_money += price * (M-buy)
        buy += (M-buy)
        break

print(sum_money)

Submission status: Accepted

Solution code:

def main():
	bought = 0
	money = 0
	DrinkList = []
	N,M = map(int,input().split())
	for i in range(N) :
		a,b = map(int,input().split())
		DrinkList.append( (a,b) )
	DrinkList.sort()
	for price,drink_max in DrinkList :
		for i in range(drink_max) :
			if bought < M :
				money += price
				bought += 1
			if bought == M :
				print(money)
				return 0
	
if __name__ == "__main__" :
	main()

Submission status: Accepted

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

m = 0
money = 0

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

for store in stores:
    money += min(store[1], M - m) * store[0]
    m += min(store[1], M - m)
    
print(money)

Submission status: Accepted

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

A_B = sorted(A_B, key=lambda a_b: a_b[0]) # 価格の安い順に並べ替え

count = 0
amt = 0
for a_b in A_B:
    a = a_b[0]
    b = a_b[1]
    if count + b < M:
        count += b
        amt += a * b
        continue
    else:
        amt += ((M - count) * a)
        break

print(amt)

Submission status: Accepted

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

# Insert all the data into dictionary
stores = []
for _ in range(N):
  a, b = list(map(int, input().split()))
  stores.append([a,b])

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

m = 0
money = 0

for store in stores:
  money += min(store[1], M - m) * store[0]
  m += min(store[1], M - m)
  if m == M:
    break
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 = sorted(ab, key=lambda x: x[0])

n_drinks = 0
ans = 0

for i in range(n):
    a_yen = ab[i][0]
    b_hon = ab[i][1]

    if (m - n_drinks) >= b_hon:
        n_drinks += b_hon
        ans += a_yen * b_hon
    else:
        n_buy = m - n_drinks
        ans += a_yen * n_buy
        break

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
stores = []
for i in range(N):
    stores.append(list(map(int, input().split())))
stores.sort()
answer = 0
for store in stores:
    yasui = min(M, store[1])
    M -= yasui
    answer += store[0] * yasui
    if M == 0:
        break
print(answer)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A_B = []
for n in range(N):
  ai, bi = map(int, input().split())
  A_B.append((ai, bi))

A_B.sort()
count = 0
cost = 0 
for abi in A_B:
  if count == M:
    print(cost)
    exit()
  else:
    for i in range(abi[1]):
      count += 1
      cost += abi[0]
      if count == M:
        print(cost)
        exit()

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

y=d=0
for i in ab:
    y+=i[0]*i[1]
    d+=i[1]
    if(d>=M):
        print(y-(d-M)*i[0])
        exit()

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

sorted_ab = sorted(ab)
collected = 0
cost = 0
for i in range(n):
    a, b = sorted_ab[i]
    if b > m - collected:
        cost += (m - collected) * a
        break
    else:
        cost += a * b
        collected += b

print("{:d}".format(cost))

Submission status: Accepted

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

AB.sort()

cnt=0
total=0
i = 0
while cnt < M:
	cnt += AB[i][1]
	total += AB[i][0]*AB[i][1]
	i+=1
total -=  AB[i-1][0]*(cnt-M)
print(total)

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

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

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

num_drink=0
price=0
a.sort()
for i in a:

    if num_drink+i[1]<m:
        
        num_drink+=i[1]
        price+=i[1]*i[0]

    else:
        price+=(m-num_drink)*i[0]

        break
    
print(price)

Submission status: Accepted

Solution code:
nm = list(map(int, input().split()))
n = nm[0]
m = nm[1]
abs = []

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

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

money = 0
count = 0

for i in range(n):
    money = money + abs[i][0] * abs[i][1]
    count = count + abs[i][1]
    if count >= m:
        money = money - abs[i][0] * (count - m)
        break

print(money)

Submission status: Accepted

Solution code:
import numpy as np

N,M = map(int,input().split())
data = []
for i in range(N):
    b = list(map(int,input().split())) # data = [[cost,max_num],...]
    data.append(b)
# print(data)

# N=2 
# M=5
# data = [[4, 9], [2, 4]]
# data.sort()
# npsorted = np.sort(np.array(data),axis=0)
data.sort()

num_drink=0
cost=0
shop_index=0
while (shop_index < N and num_drink < M):
    num_drink += data[shop_index][1]
    cost += data[shop_index][0]*data[shop_index][1]
    shop_index += 1

# num_drink >= M by buying data[shop_index-1][0]yen drink from shop_index
excess = num_drink - M
ans = cost - data[shop_index-1][0] * excess
print(ans)
# print(data)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().rstrip(" ").split(" "))
    shops = []
    for i in range(n):
        shops.append(list(map(int, input().rstrip(" ").split(" "))))

    shops.sort()

    total_price = 0
    total_bottles = 0
    for price, bottles in shops:
        if (total_bottles + bottles) < m:
            total_price += price * bottles
            total_bottles += bottles
        else:
            total_price += price * (m - total_bottles)
            break
    print(total_price)


if __name__ == '__main__':
    main()

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()  
i=0
cost=0
while m>0:
  if a[i][1]<m:
    cost+=a[i][0]*a[i][1]
    m-=a[i][1]
    i+=1
  else:
    cost+=a[i][0]*m
    break
print(cost)

Submission status: Accepted

Solution code:
import numpy as np
#N,M=2,5
#AB=np.array([[4,9],[2,4]])
N,M=map(int,input().split())
AB=np.zeros((N,2))
for i in range(N):
    AB[i,0],AB[i,1]=map(int,input().split())
AB = AB[np.argsort(AB[:, 0])]
numd=0
ans=0
for i in range(len(AB)):
    if numd==M:
        break
    else:
        if AB[i][1] < M-numd:
            ans+=AB[i][0]*AB[i][1]
            numd+=AB[i][1]
        else:
            ans+=AB[i][0]*(M-numd)
            numd+=M-numd
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
lis = []
for _ in range(N):
  a, b = map(int, input().split())
  lis.append((a, b))
def eval(data):
  return data[0]
lis.sort(key=eval)
total = 0
num = 0
mark = 0
for i in range(N):
  num = num + lis[i][1]
  total = total + lis[i][0]*lis[i][1]
  if num > M:
    mark = i
    total = total - lis[i][0]*lis[i][1]
    num = num - lis[i][1]
    break
total = total + (M - num)*lis[mark][0]
print(total)

Submission status: Accepted

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

import bisect

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

Aacc, Bacc = [0], [0]
for i in range(N):
    Aacc.append(Aacc[-1] + AB[i][0] * AB[i][1])
    Bacc.append(Bacc[-1] + AB[i][1])

idx = bisect.bisect(Bacc, M)
print(Aacc[idx - 1] if Bacc[idx - 1] == M else Aacc[idx - 1] + AB[idx - 1][0] * (M - Bacc[idx - 1]))

Submission status: Accepted

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

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

c = sorted(zip(a,b))
ans = 0
for (s, t) in c:
    if m >= t:
        ans += s * t
        m -= t
    else:
        ans += s * m
        break

print(ans)

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter


def parse_input(input_lines):
    return tuple(map(lambda sl: tuple(map(int, sl)),
                     map(lambda s: s.strip().split(),
                         input_lines)))


def main(inps):
    (N, M), *shops = inps
    shops = sorted(shops, key=itemgetter(0))
    result = 0
    for cost, count in shops:
        if M > count:
            result += cost * count
            M -= count
        else:
            result += cost * M
            return result


if __name__ == '__main__':
    inps = parse_input(sys.stdin.readlines())
    print(main(inps))

Submission status: Accepted

Solution code:
# coding: utf-8

data = input().split()
shoplist = []
for i in range(int(data[0])):
    shopdata = list(map(int, input().split()))
    shoplist.append(shopdata)
shoplist.sort()

payment = 0
wantdrink = int(data[1])
while wantdrink > 0 :
    restdrink = shoplist[0][1]
    if restdrink >= wantdrink:
        payment += wantdrink * shoplist[0][0]
        break
    else:
        payment += restdrink * shoplist[0][0]
        wantdrink -= restdrink
        del shoplist[0]
print(payment)

Submission status: Accepted

Solution code:
total_cost = 0

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

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

AB_list = sorted(AB_list)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split(" "))
ar = []
for i in range(n):
    l = list(map(int,input().split(" ")))
    ar.append(l)
ar.sort()
mo = 0
count = 0
i = 0
while True:
    if count + ar[i][1] > m:
        mo += ar[i][0] * (m - count)
        break
    else:
        mo += ar[i][0] * ar[i][1]
        count += ar[i][1]
    if count == m:
        break
    i += 1
print(mo)

Submission status: Accepted

Solution code:
n,m=list(map(int,input().split(" ")))
l=[]
l=[list(map(int,input().split(" "))) for i in range(n)]
drink=0
price=0
i=0
l.sort(key=lambda x: x[0])
while drink<m:
    pri=l[i][0]
    num=l[i][1]
    i+=1
    drink+=num
    price+=pri*num
if drink>m:
    price-=(drink-m)*pri
print(price)

Submission status: Accepted

Solution code:
n,m = list(map(int,input().split()))
x=[list(map(int,input().split())) for i in  range(n)]
#for i in range(n):
    #x.append(list(map(int,input().split())))
x.sort()
result=0
while 1:
    if x[0][1]>=m:
        result+=m*x[0][0]
        break
    else:
        result+=x[0][0]*x[0][1]
        m-=x[0][1]
        del x[0]
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n, m = [int(x) for x in stdin.readline().rstrip().split()]
data = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(n)]

data = sorted(data)
ans = 0
current = 0
i = 0
while True:
    if m > current + data[i][1]:
        ans += data[i][0] * data[i][1]
        current += data[i][1]
        i += 1
    else:
        ans += data[i][0] * (m - current)
        print(ans)
        exit()

Submission status: Accepted

Solution code:
import sys

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

a = []

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

a.sort()

i = 0
ans = 0

while m > 0:
    if m > a[i][1]:
        ans += a[i][0] * a[i][1]
        m = m - a[i][1]
        i += 1
    else:
        ans += a[i][0] * m
        print(ans)
        sys.exit()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
# ここの入力方法調べる
mat = list(list(map(int, input().split())) for i in range(N))
mat = sorted(mat, key=lambda x: x[0])

i = 0
cost = 0
num = 0
while num<M:
    if M-num >= mat[i][1]:
        num += mat[i][1]
        cost += mat[i][1] * mat[i][0]
        i += 1
    else:
        cost += mat[i][0] * (M-num)
        num += M-num
print(cost)

Submission status: Accepted

Solution code:
#ABC121C-EnergyDrinkCollector.py

from sys import stdin

input = stdin.readline

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

AB.sort()

amount = 0
charge = 0

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

print(charge)

Submission status: Accepted

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

li_sorted = sorted(li)
num_bought = 0
answer = 0
j=0
while(num_bought<M):
    if li_sorted[j][1]>M-num_bought:
        answer += li_sorted[j][0]*(M-num_bought)
    else:
        answer += li_sorted[j][0]*li_sorted[j][1]
    num_bought += li_sorted[j][1]
    j += 1
    
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

stores.sort(key=lambda sotre:sotre[0])

ans = 0
for a, b in stores:
  buy = min(b, m)
  m -= buy
  ans += a * buy
  if m == 0:
    break
print(ans)