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())
p = []
for i in range(n):
    a,b = map(int,input().split())
    p.append([a,b])
p.sort()
ans = 0
cou = 0
for a,b in p:
    if cou + b < m:
        cou += b
        ans += a*b
    elif cou + b >= m:
        ans += a*(m-cou)
        break
print(ans)

Submission status: Accepted

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

stores = []
s_append = stores.append
for i in range(N):
    A, B = map(int, input().split())
    s_append(
        {
            "price": A,
            "stock": B
        }
    )
stores.sort(key=lambda x: x["price"])

money = 0
for drink in stores:
    if M <= drink["stock"]:
        money += drink["price"] * M
        break
    else:
        money += drink["price"] * drink["stock"]
        M -= drink["stock"]

print(money)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(n):
    a, b = AB[i]
    if b >= m:
        ans += a*m
        break
    else:
        ans += a*b
    m -= b
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()
cost = 0
while M > 0:
  if AB[0][1] < M:
    cost = cost + AB[0][0]*AB[0][1]
    M=M-AB[0][1]
    AB.pop(0)
  else:
    cost = cost + AB[0][0]*M
    M = 0
    
print(cost)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
shops = list(list(map(int, input().split())) for _ in range(N))
shops.sort()
ans = 0
for value, count in shops:
    tmp_M = max(M - count , 0)
    ans += (M - tmp_M) * value
    M = tmp_M
    if M == 0:
        break
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt = 0
pay = 0
for i in range(N):
    if cnt+A[i][1]<=M:
        pay += A[i][0]*A[i][1]
        cnt += A[i][1]
    else:
        m = M-cnt
        pay += A[i][0]*m
        break
print(pay)

Submission status: Accepted

Solution code:
# Aiでソートしてあげたほうがいいね？
n,m = map(int,input().split())
drink = []
for i in range(n):
    a,b = map(int,input().split())
    drink.append([a,b])
drink = sorted(drink,key = lambda x:x[0])
#print(drink)
total = 0
cnt = 0
for price,num in drink:
    if cnt == m:
        break
    if cnt + num <= m:
        cnt += num
        total += num * price
    else:
        nya = max(abs(m - (cnt)),0)
        #print(nya)
        #print("?",abs(m - (num+cnt)),num,cnt)
        cnt += nya
        total += nya * price
print(total)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
money = 0
drink = 0
for a,b in ab:
  money += a*b
  drink += b
  if drink >= m:
    money -= (drink-m) * a
    break
print(money)

Submission status: Accepted

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

AB.sort()

ans = 0
M_tmp = M
for i, (Ai, Bi) in enumerate(AB):
    cnt = min(M_tmp, Bi)
    ans += Ai * cnt
    M_tmp -= cnt

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N, M, *A = map(int, open(0).read().split())
*A, = zip(*[iter(A)]*2)
A.sort()

ans = 0
for a, b in A:
    ans += a*min(b, M)
    M -= min(b, M)

    if M == 0:
        break
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,M = IL()

d = ILL(N)

d.sort(reverse=True)
cost = 0
while M:
  a,b = d.pop()
  if M>=b:
    M -= b
    cost += a*b
  else:
    cost += a*M
    M = 0
P(cost)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
shops = [tuple(map(int, input().split())) for _ in range(N)]
shops.sort()
cost = 0
for shop in shops:
    value, amount = shop
    if amount < M:
        cost += value * amount
        M -= amount
    else:
        cost += value * M
        break
print(cost)

Submission status: Accepted

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

# In[14]:


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


# In[15]:


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

cnt = 0
ans = 0
for idx,lst in enumerate(AB_s):
#     print(lst)
    c = lst[0]
    n = lst[1]
    if n <= M-cnt:
        ans += c*n
        cnt += n
#         print(cnt)
    else:
        ans += c*(M-cnt)
        cnt += (M-cnt)
#         print(cnt)
        break
print(ans)


# In[ ]:

Submission status: Accepted

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

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

Submission status: Accepted

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

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

c = 0
for i in range(n):
  for j in range(l[i][1]):
    c += l[i][0]
    m -= 1
    if m == 0:
      print(c)
      exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n , m = map(int , input().split())
ans = []
total = 0
for i in range(n):
  a = list(map(int , input().split()))
  ans.append(a)
ans = sorted(ans, key=lambda x:x[0])
j = 0
while m > 0:
  small = min(ans[j][1] , m)
  total += ans[j][0] * small
  m -= small
  j += 1
print(total)

Submission status: Accepted

Solution code:
N, M, *A = map(int, open(0).read().split())
*A, = zip(*[iter(A)]*2)

A.sort()

ans = 0
for a, b in A:
    ans += a*min(b, M)
    M -= min(b, M)

    if M == 0:
        break
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

for _ in range(n):
    a, b = map(int, input().split())
    drinks[a] += b

ans = 0
sorted_drinks = sorted(drinks.items(), key=lambda x:x[0])

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

Submission status: Accepted

Solution code:
from operator import itemgetter
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=itemgetter(0, 1))
ans = 0
for ab in AB:
  if ab[1] >= M:
    ans += ab[0] * M
    break
  else:
    ans += ab[0] * ab[1]
    M -= ab[1]

print(ans)

Submission status: Accepted

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

while (res < M):
    if M - res >= AB[ind][1]:
        ans += AB[ind][0] * AB[ind][1]
        res += AB[ind][1]
    else:
        ans += AB[ind][0] * (M - res)
        res += (M - res)
    ind += 1

print(ans)

Submission status: Accepted

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

heapq.heapify(ab)

sm = 0
for _ in range(n):
    a, b = heapq.heappop(ab)
    if m >= b:
        m -= b
        sm += a * b
    else:
        sm += a * m
        break

print(sm)

Submission status: Accepted

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

ans=0
now=M
for l in L:
    a,b=l
    ans+=min(b, now)*a
    now-=b
    if now<=0:
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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

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

Submission status: Accepted

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

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

A.sort()
#print(A)

nokori = M
yen = 0
mise = 0
while nokori > 0:
  if A[mise][1] >= nokori: #i番目の店で何本買えるか
    yen += A[mise][0]*nokori
    break
  else:
    yen += A[mise][0]*A[mise][1]
    nokori -= A[mise][1]
    mise += 1
print(yen)

Submission status: Accepted

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

ans = 0
count = 0
for ab in AB:
    if count == M: break
    if M - count >= ab[1]:
        ans += ab[0] * ab[1]
        count += ab[1]
    else:
        ans += ab[0] * (M - count)
        count += (M - count)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:

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

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

AB.sort()

ans = 0
need = M
for a, b in AB:
    buy = min(need, b)
    need -= buy
    ans += buy * a

print(ans)

Submission status: Accepted

Solution code:
def main():
    n,m = map(int,input().split())
    ab = sorted([list(map(int,input().split())) for _ in range(n)], key=lambda x:(x[0],x[1]))
    res = 0
    for a,b in ab:
        if m == 0:
            break
        buy = min(m,b)
        res += a*buy
        m -= buy
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n,m = iim()
l =[]
for _ in range(n):
    l.append(iil())

l.sort()

num = 0
val = 0
for item in l:
    cst = item[0]
    cnt = item[1]
    if num + cnt < m:
        val += cst*cnt
        num += cnt
    else:
        val += cst*(m-num)
        break
print(val)
#100000000000000
#100000000000000

Submission status: Accepted

Solution code:
def solve():
    N, M = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(N)]
    A.sort()
    ans = 0
    cnt = 0
    for a in A:
        if cnt + a[1] <= M:
            ans += a[1]*a[0]
            cnt += a[1]
        else:
            ans += a[0]*(M-cnt)
            break
    return ans
print(solve())

Submission status: Accepted

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

  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b,c,d=list(map(int,input().split())),[],0,0
for i in range(a[0]):
    b.append(list(map(int,input().split())))
b.sort()
for i in range(a[1]):
    c+=b[d][0]
    b[d][1]-=1
    if b[d][1]==0:
        d+=1
print(c)

Submission status: Accepted

Solution code:
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

n,m = map(int,stdin.readline().rstrip().split())
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]

li.sort()

point = 0
yen = 0

for i in li:
    x,y = i
    if y + point >= m:
        yen += x*(m-point)
        break
    else:
        yen += x*y
        point += y

print(yen)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

drinks = []

for i in range(n):
  drinks.append(tuple(map(int,input().split())))
  
drinks = sorted(drinks)

ans = 0
t = 0
for a,b in drinks:
  if t+b<m:
    t+=b
    ans+=a*b
  else:
    ans+=a*(m-t)
    break
print(ans)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
AB = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x: x[0])
cost = 0
for ab in AB:
    if M >= ab[1]:
        cost += ab[0]*ab[1]
        M -= ab[1]
    else:
        cost += ab[0]*M
        break
print(cost)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(N)]
a=sorted(A, key=lambda x:x[0])
cst=0
for t in range(N):
    if a[t][1]>=M:
        cst+=(M*a[t][0])
        break
    else:
        cst+=(a[t][1]*a[t][0])
        M-=a[t][1]
print(cst)

Submission status: Accepted

Solution code:
import sys

N, M = map(int, sys.stdin.readline().split())
AB = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(N)])

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

S = 0
num = 0
n1 = 0
n2 = 0
while num < M:
  if n2 == C[n1][1]:
    n1 += 1
    n2 = 0
  S += C[n1][0]
  num += 1
  n2 += 1
 
print(S)

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
drinks = sorted(tuple(tuple(map(int, input().split(' '))) for _ in range(N)))

cost = 0
for price, n in drinks:
    n = min(n, M)
    cost += n * price
    M -= n
    if M == 0:
        break

print(cost)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

yen = 0
amount = 0
index = 0
for i in range(n):
    if amount < m:
        yen += ab[i][0] * ab[i][1]
        amount += ab[i][1]
        index = i
    else: break
b = 0
for i in range(index):
    b += ab[i][1]
print(yen - ab[index][1]*ab[index][0] + ab[index][0]*(m-b))

Submission status: Accepted

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

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

c = 0
ans = 0
for a, b in l:
  if m-c <= b:
    ans += a * (m-c)
    break
  else:
    c += b
    ans += a * b

print(ans)

Submission status: Accepted

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

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

cnt, ans = 0, 0
for i in range(n):
    if m <= cnt + drinks[i][1]:
        num = m - cnt
        ans += drinks[i][0] * num
        break
    
    cnt += drinks[i][1]
    ans += drinks[i][0] * drinks[i][1]

print(ans)

Submission status: Accepted

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

AB = []

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

AB.sort()

ans = 0
cnt = 0

for ab in AB:
    for b in range(ab[1]):
        ans += ab[0]
        cnt += 1
        if cnt==M:
            break
    if cnt==M:
        break

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
dl = []
for i in range(n):
  d = list(map(int, input().split()))
  dl.append(d)
dl.sort(key=lambda x: x[0])
rm = m
mc = 0
for i in range(n):
  if dl[i][1] >= rm:
    mc += rm * dl[i][0]
    break
  else:
    mc += dl[i][0] * dl[i][1]
    rm -= dl[i][1]
print(mc)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n,m=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(n)]
AB=sorted(AB,key=lambda x:x[0])
cnt=0
ans=0
for a,b in AB:
    tmp=cnt
    cnt=min(m,cnt+b)
    ans+=a*(cnt-tmp)
    if cnt==m:
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,m=map(int, input().split())
    l=[list(map(int,input().split())) for i in range(n)]
    l.sort(key=lambda x:x[0])
    ans=0
    cnt=0
    for i in range(n):
        if m>=l[i][1]:
            cnt+=l[i][1]
            m-=l[i][1]
            ans+=l[i][0]*l[i][1]
        else:
            cnt+=m
            ans+=l[i][0]*m
            break
    print(ans)
resolve()

Submission status: Accepted

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

for i in range(N):
  if Shops[i][1] < M:
    cost += Shops[i][0]*Shops[i][1]
    M -= Shops[i][1]
  else:
    cost += Shops[i][0]*M
    print(cost)
    exit()

Submission status: Accepted

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

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, m = mi()
    L = [lmi() for _ in range(n)]
    L.sort(key=lambda x: x[0])

    money = 0
    bottle = 0
    for a, b in L:
        if bottle + b < m:
            money += a * b
            bottle += b
        else:
            money += a * (m - bottle)
            bottle += (m - bottle)
            break
    print(money)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L = {}
for _ in range(n):
    a, b = map(int, input().split())
    if a not in L:
        L[a] = b
    else:
        L[a] += b
ans = 0
L = sorted(L.items())
for i in L:
    if i[1] >= m:
        ans += i[0] * m
        break
    else:
        m -= i[1]
        ans += i[0] * i[1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n,m=map(int,input().split())
l=[]
for i in range(n):
   l.append(list(map(int,input().split())))
l.sort()
#print(l)
total = 0
drink = 0
for i in range(0,n) :
    drink = drink + l[i][1]
    if drink>m:
        break
    #print('drink',drink,'l[i][1]',l[i][1])
noko = m-drink
#print('noko',noko,'m',m,'drink',drink)
d = i
#print('d',d)
for j in range(0,d+1):
    total += l[j][0]*l[j][1]
    #print('total',total,'l[j][0]',l[j][0],'l[j][1]',l[j][1])
total =total + l[d][0]*noko
#print('l[d][0]',l[d][0],'noko',noko)
print(total)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ab_list = [list(map(int,input().split())) for nesya in range(n)]
s_list = sorted(ab_list)
cost = 0
for ab in s_list:
  a = ab[0]
  b = ab[1]
  if b <= m:
    m -= b
    cost += a*b
  else:
    cost += a*m
    m = 0
print(cost)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
cnt = 0
ans = 0
i = 0
ab.sort()
while cnt < M:
    tmp = ab[i][1]
    prc = ab[i][0]
    ans += prc * tmp
    cnt += tmp
    i += 1
print(ans - (cnt-M)*prc)

Submission status: Accepted

Solution code:
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))
def main():
    import sys
    input = sys.stdin.readline
    n,m=L()
    dic={}
    a=[]
    for i in range(n):
        ai,bi=L()
        if ai not in dic:
            dic[ai]=0
        dic[ai]+=bi
        a.append(ai)
    a=list(set(a))
    a.sort()
    ans=0
    drink=0
    tmp=0
    while drink<m:
        tmp=a.pop(0)
        for i in range(dic[tmp]):
            drink+=1
            ans+=tmp
            if drink>=m:
                break
    print(ans)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
n, m = map(int, input().split())
shop = [list(map(int, input().split())) for _ in range(n)]

shop_s = sorted(shop, key=lambda x: x[0])
price = 0
drink = 0

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

print(price)