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 = [0] * N, [0] * N
for i in range(N):
	A[i], B[i] = map(int, input().split())

#  値段Aの昇順でソート
C = sorted(zip(A, B))
A, B = zip(*C)

ans, cnt, i = 0, 0, 0
while cnt < M:
	tmp = min(B[i], M - cnt)
	cnt += tmp
	ans += A[i] * tmp
	i += 1
print(ans)

Submission status: Accepted

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

count = [0]*(n+1)
for i in range(n):
    count[i+1] = count[i] + ab_l[i][1]

for i in range(n+1):
    if count[i] >= m:
        to_here = i
        minus_count = count[to_here] - m
        break

all_money = [0]*n
for i in range(to_here):
    all_money[i] = ab_l[i][0] * ab_l[i][1]

ans = sum(all_money) - ab_l[to_here-1][0] * minus_count

print(ans)

Submission status: Accepted

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

a.sort()
#print(a)

money = 0
for i in range(n):
    for j in range(a[i][1]):
        money += a[i][0]
        m -= 1
        #print(money,m,a)
        if m == 0:
            break
    if m == 0:
        break
print(money)

Submission status: Accepted

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

ds.sort(key=lambda d: d[0])

cost = 0
for d in ds:
    if d[1] >= m:
        cost += m * d[0]
        break

    cost += d[0] * d[1]
    m -= d[1]

print(cost)

Submission status: Accepted

Solution code:
N,M = map(int, input().strip().split())
k = []
for i in range(N):
    a,b = map(int, input().strip().split())
    k.append((a,b))
k.sort(key = lambda x:x[0])
price = 0
for i in range(N):
    M -= k[i][1]
    price += k[i][0]*k[i][1]
    if M <1:
        break
print(price + M*k[i][0])

Submission status: Accepted

Solution code:
from operator import itemgetter

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

lists.sort(key=itemgetter(0))
mon = 0
for shop in lists:
    if shop[1] < M:
        mon += shop[0]*shop[1]
        M -= shop[1]
    else:
        mon += shop[0]*M
        break
print(mon)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
pair = [[0, 0]]*N
cost = 0
num = 0
for i in range(N):
    a, b = map(int, input().split())
    pair[i] = [a, b]

pair.sort(key=lambda x:x[0])
#print(pair)
for i in range(N):
    cost += pair[i][0] * pair[i][1]
    num += pair[i][1]
    if num >= M:
        cost -= pair[i][0] * (num - M)
        break

print(cost)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# input
N, M= map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]

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

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

print(ans)

Submission status: Accepted

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

dic_drink = {}
for i in range(N):
  ai,bi = map(int,input().split())
  if ai not in dic_drink:
    dic_drink[ai] = bi
  else:
    dic_drink[ai] += bi

price = 0
for ai,bi in sorted(dic_drink.items()):
  if M <= bi:
    price += ai*M
    break
  else:
    price += ai*bi
    M -= bi
    
print(price)

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int,input().split())
S = np.empty((N,2))
sum_buy = 0
sum_money = 0

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

S = S[S[:,0].argsort(),:]

j = 0

while True:
    if sum_buy + S[j,1] < M:
        sum_buy += S[j,1]
        sum_money += S[j,1] * S[j,0]
        j += 1
    else:
        break

sum_money = sum_money + (M - sum_buy) * S[j,0]

print(int(sum_money))

Submission status: Accepted

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

data.sort()
#print(data)
cost = 0
lost = M
for c in data:
    if lost != 0:
        if lost >= c[1]:
            lost -= c[1]
            cost += c[0] * c[1]
        else:
            cost += c[0] * lost
            lost = 0
    else:
        break
print(cost)

Submission status: Accepted

Solution code:
N,M= [ int(it) for it in input().split() ]
li = []
for i in range(N):
  li.append( [ int(it) for it in input().split() ] )
li.sort( key = lambda x:x[0] )
c = 0
for i in range(N):
  m = min( li[i][1], M )
  c += m*li[i][0]
  M -= m
print ( c )

Submission status: Accepted

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

while True:
  ans += AB[i][0] * AB[i][1]
  count += AB[i][1]
  if count >= M:
    break
  i += 1
ans -= AB[i][0] * (count - M)
print(ans)

Submission status: Accepted

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

for i in range(N):
    AB[i] = [int(n) for n in input().split()]
AB = sorted(AB, key=lambda x: x[0])
# print(AB)
ans = 0
for i in range(len(AB)):
    while(M != 0 and AB[i][1] != 0):
        M -= 1
        AB[i][1] -= 1
        ans += AB[i][0]
print(ans)

Submission status: Accepted

Solution code:
def key_func(n):
    return n[0]

n,m = map(int, input().split(" "))
a_b = []
for i in range(n):
    a_b.append(list(map(int,input().split(" "))))

a_b.sort(key=key_func)

money = 0

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

print(money)

Submission status: Accepted

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

shop_list = [[0 for i in range(2)] for j in range(N)]
for i in range(N):
    shop_list[i][0] = A[i]
    shop_list[i][1] = B[i]

shop_list.sort()

count = 0
money = 0
for i in range(N):
    if count + shop_list[i][1] < M:
        money += shop_list[i][0] * shop_list[i][1]
        count += shop_list[i][1]
    else:
        for j in range(shop_list[i][1]):
            if count == M:
                break
            money += shop_list[i][0]
            count += 1

print(money)

Submission status: Accepted

Solution code:
from collections import deque
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
ab.sort()

ab = deque(ab)

ans = 0
m = 0
while m < M:
    a, b = ab.popleft()
    tm = min([b, M - m])
    ans += tm * a
    m += tm
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from operator import itemgetter


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

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


def main():
    b = sorted(a, key=itemgetter(0))
    m = 0
    cost = 0
    for i in range(N):
        if (m + b[i][1]) <= M:
            m += b[i][1]
            cost += (b[i][0] * b[i][1])
        else:
            cost += (b[i][0] * (M-m))
            return cost
    return cost


print(main())

Submission status: Accepted

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

Submission status: Accepted

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

money = 0
count = 0

drink.sort()
for i in range(n):
    if m <= drink[i][1]:
        break
    m -= drink[i][1]
    money += drink[i][0] * drink[i][1]
    count += 1

money += drink[count][0] * m
print(money)

Submission status: Accepted

Solution code:
import sys
rr = lambda: sys.stdin.readline().strip()
ri = lambda: int(rr())
rs = lambda: [int(_) for _ in rr().split()]

from operator import itemgetter

N, M = rs()
p = [tuple(rs()) for _ in range(N)]

p = sorted(p, key=itemgetter(0))

ans = 0
m = 0

for pi in p:
	m += pi[1]
	ans += pi[0] * pi[1]
	if m >= M:
		ans -= (m - M) * pi[0]
		break

print(ans)

Submission status: Accepted

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

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

drinks.sort()

ans = 0
req = M
for a, c in drinks:
    cnt = min(c, req)
    ans += a * cnt
    req -= cnt

print(ans)

Submission status: Accepted

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

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

p = 0
q = 0
for a, b in sorted(price):
    for i in range(b):
        p += a
        q += 1
        if q == M:
            break
    if q == M:
        break

print(p)

Submission status: Accepted

Solution code:
def main():
  N, M = map(int, input().split())
  arr = []
  for i in range(N):
    cost, num = map(int, input().split())
    arr.append((cost, num))
  arr.sort(key=lambda x: x[0])

  cost = 0
  num = 0
  for i in range(N):
    n = arr[i][1]
    cost += arr[i][0] * min(n, M - num)
    num += n
    if num >= M:
      print(cost)
      return


if __name__ == "__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [None] * N
B = [None] * N
C = [None] * N
for i in range(N):
  A[i], B[i] = map(int, input().split())
  C[i] = [A[i], B[i]]
C = sorted(C)

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

price -= (count - M) * C[i][0]


print(price)

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 = sorted(X)
num = 0
cost = 0
for a, b in X:
    cost += a * min(M - num, b)
    num += min(M - num, b)
    if num == M:
        print(cost)
        break

Submission status: Accepted

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

value_nums = [] # 価格、買える本数の順番
for _ in range(N):
  v,n = map(int, input().split())
  value_nums.append([v,n])
  
  
vn_sorted = sorted(value_nums, key = lambda x: x[0])
#print(vn_sorted)

ans = 0
for value, num in vn_sorted:
  if M >= num:
    ans += value * num
    M -= num
  elif 0 <= M < num:
    ans += value * M
    M = 0
    break
  
print(ans)

Submission status: Accepted

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

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

l = sorted(l, key = lambda x: x[0])
p = 0
for v in l:
    if m >= v[1]:
        m = m - v[1]
        p += v[0] * v[1]
    elif m < v[1]:
        p += v[0] * m
        m = 0
        
    if m == 0:
        break
        
print(p)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
drinks = []  # (price, quantity)
for i in range(N):
    drinks.append(tuple(map(int, input().split())))

drinks = sorted(drinks, key=lambda d: d[0])
i = 0
while M > 0:
    M -= drinks[i][1]
    i += 1
ans = sum([d[0] * d[1] for d in drinks[:i]])
if M < 0:
    ans -= abs(M) * drinks[i - 1][0]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m, *l= map(int, open(0).read().split())

ans = 0
for price, amount in sorted(zip(l[::2], l[1::2])):
    ans += price * min(m, amount)
    m -= amount
    if m <= 0:
        break
print(ans)

Submission status: Accepted

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

ans = 0
for price, amount in sorted(zip(A, B)):
    ans += price * min(m, amount)
    m -= amount
    if m <= 0:
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    input_list = list(map(int, input().split()))
    N = input_list[0]
    M = input_list[1]

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

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

    answer = 0
    cnt = 0
    for e in sorted_AB:
        pre_cnt = cnt

        Ai = e[0]
        Bi = e[1]

        cnt = min(M, cnt+Bi)
        answer += Ai * (cnt - pre_cnt)

        if cnt == M:
            break

    print(answer)

Submission status: Accepted

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

count = 0
money = 0
for ab in ordered:
    a = ab[0]
    b = ab[1]
    if M >= count + b:
        count += b
        money += b * a
    else:
        c = (M - count)
        count += c
        money += c * a
        break

print(money)

Submission status: Accepted

Solution code:
from operator import itemgetter
N,M = map(int, input().split())

all_lst = []
for i in range(N):
  all_lst.append(list(map(int, input().split())))
  
all_lst.sort(key=itemgetter(0))

m = 0
ans = 0

for i in all_lst:
  a = i[0]
  b = i[1]
  if (b <= (M - m)):
    ans += a * b
    m += b
  else:
    ans += a * (M - m)
    m += (M - m)
    break
print(ans)

Submission status: Accepted

Solution code:
from operator import itemgetter
N,M = map(int, input().split())

all_lst = []
for i in range(N):
  all_lst.append(list(map(int, input().split())))
  
all_lst.sort(key=itemgetter(0))

m = 0
ans = 0

for i in all_lst:
  a = i[0]
  b = i[1]
  if (m < M):
    if (b <= (M - m)):
      ans += a * b
      m += b
    else:
      ans += a * (M - m)
      m += (M - m)
  else:
    break
print(ans)

Submission status: Accepted

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

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

i = 0
while num < m:
    num += lst[i][1]
    price += lst[i][0]*lst[i][1]
    i += 1

print(price - (num-m)*lst[i-1][0])

Submission status: Accepted

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

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

amt = 0
money = 0
i=0
while amt<M:
    money+=List[i][0]*List[i][1]
    amt+=List[i][1]
    i+=1

ans = money-(amt-M)*List[i-1][0]
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

def dfs(m,cnt):
    if m <= 0 or cnt == len(list):
        return 0
    b = list[cnt][1] if list[cnt][1] < m else m
    a = b*list[cnt][0]

    return a + dfs(m-b,cnt+1)

print(dfs(M,0))

Submission status: Accepted

Solution code:
from operator import itemgetter

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

# N = 2
# M = 5
# A = [[4, 9], [2, 4]]

A.sort(key=itemgetter(0))

sum = 0
for shop in A:
    num = min(shop[1], M)
    sum += shop[0] * num
    M -= num
    # print(num, shop[0] * num, M)

    if M <= 0:
        break
print(sum)

Submission status: Accepted

Solution code:
###############################################################################

from bisect import bisect_left as binl

def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return map(int, input_tuple)


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


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


def main():
    n, m = intin()
    ablist = intinl(n)
    ablist = [(a, b) for a, b in ablist]

    ablist.sort()

    count = 0
    yen = 0
    for a, b in ablist:
        if count + b >= m:
            yen += a * (m - count)
            count = m
            break
        yen += a * b
        count += b

    print(yen)


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)]
ab.sort(key=lambda x:x[0])
now = 0
ans = 0
for a, b in ab:
    if now == M:
        break
    buy = min(b, M-now)
    now += buy
    ans += buy * a
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
AB=[]
for i in range(N):
    ab=list(map(int,input().split()))
    AB.append(ab)
AB=sorted(AB)
c=-1
while M>0:
    M-=AB[c+1][1]
    c+=1
S=[(AB[c][1]+M)*AB[c][0]]
for k in range(c):
    S.append(AB[k][0]*AB[k][1])
print(sum(S))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
AB=[]
for i in range(N):
    ab=list(map(int,input().split()))
    AB.append(ab)
AB=sorted(AB)
K=M
c=-1
while K>0:
    K-=AB[c+1][1]
    c+=1
S=[(AB[c][1]+K)*AB[c][0]]
for k in range(c):
    S.append(AB[k][0]*AB[k][1])
print(sum(S))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
e=[]
for i in range(a):
    c,d=map(int, input().split())
    e+=[[c,d]]
e.sort()
a=i=j=0
while i < b:
    a+=(e[j][0]*e[j][1])
    i+=e[j][1]
    j+=1
print(a-((i-b)*e[j-1][0]))

Submission status: Accepted

Solution code:
from operator import itemgetter
n,m=map(int,input().split())
l=[]
for i in range(n):
  a,b=map(int,input().split())
  l.append([a,b])
l.sort(key=itemgetter(0))
s=0
for j in range(n):
  s=s+l[j][0]*min(l[j][1],m)
  m=m-l[j][1]
  if m<0:
    break
print(s)

Submission status: Accepted

Solution code:
M,N=(int(i) for i in input().split())
AB=[]
for i in range(M):
  AB.append([int(i) for i in input().split()])
sortedAB=sorted(AB,key=lambda x:x[0])
t=0
i=0
money=0
while t<N:
  if N-t<sortedAB[i][1]:
    money=money+(N-t)*sortedAB[i][0]
    t=N
  else:
    t=t+sortedAB[i][1]
    money=money+sortedAB[i][0]*sortedAB[i][1]
  i+=1
print(money)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
A = []
for i in range(a):
    array = list(map(int, input().split()))
    A.append(array)
A.sort()
ans = 0
buy = 0
for i in A:
    buy = min(b, i[1])
    ans += i[0]*buy
    b -= buy
    if b == 0:
        break
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
array = np.array([list(map(int, input().split())) for i in range(N)])

A_array = array[:, 0]
B_array = array[:, 1]

buy_total = 0
money_count = 0
buy_order = np.argsort(A_array)
# print(buy_order)
for buy_index in buy_order:
    buy_count = min(M - buy_total, B_array[buy_index])
    buy_total += buy_count
    money_count += buy_count * A_array[buy_index]
    if buy_total == M:
        break
print(money_count)

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
ab = [0] * n
for j in range(n):
  ab[j] = [int(i) for i in input().split()]
ab.sort(key = lambda x:x[0])
k = 0
ans = 0
while True:
  if m > ab[k][1]:
    ans = ans + ab[k][0] * ab[k][1]
    m = m - ab[k][1]
  else:
    ans = ans + ab[k][0] * m
    break
  k += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split())
list_drink = []
for n in range(N):
    list_drink.append(list(map(int, input().split())))
    
list_drink.sort()

sum_bottle = 0
sum_money = 0
cnt_lp = 0
while(sum_bottle <= M - 1):
    num_money = list_drink[cnt_lp][0]
    num_bottle = list_drink[cnt_lp][1]
    if sum_bottle + num_bottle >= M:
        sum_money += num_money * (M - sum_bottle)
        sum_bottle = M
    else:
        sum_money += num_money * num_bottle
        sum_bottle += num_bottle
    cnt_lp += 1

print(sum_money)

Submission status: Accepted

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

A = sorted(store)

sumQ = 0
sumP = 0
for k in range(N):
    p = A[k][0]
    q = A[k][1]
    sumQ = sumQ + q
    sumP = sumP + p * q
    if M < sumQ:
        print(sumP-(sumQ-M)*p)
        break
    elif M == sumQ:
        print(sumP)
        break

Submission status: Accepted

Solution code:
import numpy as np

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

B = sorted(A)

cnt = 0
amount = 0
for i in range(N):
  if cnt+B[i][1] < M:
    cnt += B[i][1]
    amount += B[i][0]*B[i][1]
  elif cnt+B[i][1] >= M:
    #cnt = M
    amount += B[i][0]*(M-cnt)
    break

print(amount)

Submission status: Accepted

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

array.sort(key=lambda x: x[0])
ans = 0
cnt = 0
for i in range(N):
  #何本買うか
  buy = min([array[i][1], M-cnt])
  cnt += buy
  ans += array[i][0]*buy

print(ans)

Submission status: Accepted

Solution code:
import sys

N, M = map(int, input().split())
lst = []
for i in range(N):
    lst += [list(map(int, input().split()))]
lst.sort()    
#print (lst)
count = M
yen = 0

for i in range(N):
    yen += lst[i][0] * min(count, lst[i][1])  
    count -= min(count, lst[i][1])
#    print (count)
    if count <= 0:
        print (yen)
        sys.exit()

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
a = [[0, 0] for _ in range(n)]
for i in range(n):
    a[i] = [int(i) for i in input().split()]
a.sort()
_sum = 0
for i in range(len(a)):
    _sum += a[i][0] * min(m, a[i][1])
    m -= min(m, a[i][1])
print(_sum)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 121
# https://atcoder.jp/contests/abc121
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, M = i2nn()
    AB = ii2nnn(N)
    AB.sort(key=lambda v: v[0])
    cost = 0
    m = 0
    i = 0
    while True:
        a, b = AB[i]
        if m + b < M:
            m += b
            i += 1
            cost += a * b
            continue
        for j in range(b):
            m += 1
            cost += a
            if m >= M:
                print(cost)
                return

main()

Submission status: Accepted

Solution code:
N, M = (int(x) for x in input().split())
A = [0]*N
B = [0]*N
cost = 0
remain = M
for i in range(N):
  A[i], B[i] = (int(x) for x in input().split())
A = sorted(enumerate(A), key=lambda x:x[1])
for i in range(N):
  if remain > 0:
    buy = min(B[A[i][0]], remain)
    cost += A[i][1] * buy
    remain -= buy
  else:
    break
print(cost)

Submission status: Accepted

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

ab.sort(key=lambda ab:(ab[0]))
s = 0
cnt = 0
for i in range(M):
    s += ab[cnt][0]
    if ab[cnt][1]>1:
        ab[cnt][1] -= 1
    else:
        cnt += 1
print(s)

Submission status: Accepted

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

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])
res=0
c=0
for a,b in ab:
    if c+b<=m:
        c+=b
        res+=a*b
    else:
        res+=a*(m-c)
        break
print(res)

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

remain = M
cost = 0

for a, b in AB:
    if remain <= b:
        cost += remain * a
        break
    else:
        cost += a * b
        remain -= b


print(cost)

Submission status: Accepted

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

class Shop():
    def __init__(self, v, n):
        self.value = v
        self.zaiko = n

def solve():
    N, M = list(map(int, input().split()))
    shops = []
    for i in range(N):
        a, b = list(map(int, input().split()))
        shops.append(Shop(a, b))
    
    shops.sort(key=lambda x: x.value)

    rest = M
    ans = 0
    for i in range(N):
        if shops[i].zaiko <= rest:
            ans += shops[i].value * shops[i].zaiko
            rest -= shops[i].zaiko
        else:
            ans += shops[i].value * rest
            break
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = sorted(list(map(int, input().split()))for i in range(N))
ans = 0
for A, B in AB:
    if B >= M:
        print(ans + A * M)
        exit()
    else:
        ans += A * B
        M -= B

Submission status: Accepted

Solution code:
from operator import itemgetter

N, M = map(int, input().split())
AB = sorted([list(map(int, input().split()))for i in range(N)], key=itemgetter(0))
ans = 0
cm = 0
for i in range(N):
    if cm + AB[i][1] >= M:
        print(ans + AB[i][0] * (M - cm))
        exit()
    else:
        ans += AB[i][0] * AB[i][1]
        cm += AB[i][1]

Submission status: Accepted

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

Submission status: Accepted

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

ds.sort(key=lambda x: x[0])
ans = 0
for a, b in ds:
    if m < b:
        ans += a * m
        break
    else:
        ans += a * b
        m -= b
print(ans)

Submission status: Accepted

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

a.sort()
ans = 0
for _a, _m in a:
    if _m <= m:
        ans += _m * _a
        m -= _m
    else:
        ans += m * _a
        m = 0
        break

print(ans)

Submission status: Accepted

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

data =[]
for i in range(n):
    tmp_a, tmp_b = list(map(int, input().split()))
    data.append((tmp_a, tmp_b))


total =0
cost = 0
for a, b in sorted(data, key=lambda x:x[0]):
    #print(a, b)
    if total + b < m:
        total += b
        cost += a*b
    else:
        cost += a*(m-total)
        break
print(cost)

Submission status: Accepted

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

count = 0
cost = 0
index = 0

while True:
  if index > n - 1:
    break
  if count + sorteddrink[index][1] > m:
    break
  cost += sorteddrink[index][0] * sorteddrink[index][1]
  count += sorteddrink[index][1]
  index += 1

if index <= n - 1:
  cost += sorteddrink[index][0] * (m - count)
print(cost)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    rem = M
    ret = 0
    for a, b in sorted(zip(A, B)):
        ret += a * min(b, rem)
        rem -= min(b, rem)
        if rem == 0:
            break
    return ret


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]" 
    B = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    result = solve(N, M, A, B)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from operator import itemgetter
N, M = map(int,input().split())
lists = []
num = 0
amount = 0

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

lists = sorted(lists, key=itemgetter(0))

for i in range(N):
    if num + lists[i][1] < M:
        num = num + lists[i][1]
        amount = amount + lists[i][0]*lists[i][1]
    else:
        balance = M - num
        amount = amount + lists[i][0] * balance
        break

print(amount)

Submission status: Accepted

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

m = sorted([tuple(map(int, input().split())) for _ in range(N)])
need = M #残りの欲しいエナドリの数
totalprice = 0 #現時点での金額

for i, nowmer in enumerate(m):
    nowprice = nowmer[0]
    stock = nowmer[1]
    totalprice += nowprice * min(need, stock)
    need -= min(need, stock)
    if need <= 0: break

print(totalprice)