Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

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

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

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

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

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



Sample input:
2 5
4 9
2 4


Sample output:
12

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter

input = sys.stdin.readline

N, M = map(int, input().split())
C = [None] * N
for i in range(N):
    a, b = map(int, input().split())
    C[i] = [a, b]
C = sorted(C, key=itemgetter(0))

cnt, ans = 0, 0
for a, b in C:
    x = min(b, M - cnt)
    ans += a * x
    cnt += x
    if cnt == M:
        break
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
D=[]
for n in range(N):
    a,b=map(int,input().split())
    D.append((a,b))
D.sort()
c=0
m=0
for n in range(N):
    a,b=D[n]
    if m+b<M:
        m+=b
        c+=b*a
    else:
        bb=M-m
        m+=bb
        c+=bb*a
print(c)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
dic = {}
for i in range(N):
    A, B = map(int, input().split())
    if A in dic:
        dic[A] += B
    else:
        dic[A] = B


drink_num = 0
drink_cost = 0
for key, value in sorted(dic.items()):
    if drink_num + value >= M:
        drink_cost += (M - drink_num)*key
        break
    else:
        drink_cost += key*value
        drink_num += value

print(drink_cost)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N,M = [int(x) for x in input().split()]
ABdict = {}
for _ in range (N):
    A,B = [int(x) for x in input().split()]
    if A in ABdict.keys():
        ABdict[A] += B
    else:
        ABdict[A] = B

dct = sorted(ABdict.items())
ls = []
ans = 0
for i in dct:
    if M >= 0:
        ans += min(M,i[1])*i[0]    
        M -= i[1]  

print(ans)

Submission status: Accepted

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

import numpy as np

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

arr = np.array(list)

arrsort = arr[arr[:,0].argsort()]

hon = 0
yen2 = 0
yen3 = 0

for x in range(0,N):
            yen3 += yen2
            for i in range(0,arrsort[x,1]):
                yen2 = arrsort[x,0] * (i+1)
                hon += 1
                if hon == M:
                    break
                else:
                    continue
                break
            else:
                continue
            break

print(yen3 + yen2)

Submission status: Accepted

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

import numpy as np

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

#print(N, M)
#print(list)

#print(min(list, key=(lambda x: x[0])))

arr = np.array(list)

#print(arr)

#arrsort = np.sort(arr, axis=0)
arrsort = arr[arr[:,0].argsort()]

#print(arrsort)

yen = arrsort[0,0] * arrsort[0,1]

#print(yen)

#for i in range(0,arrsort[0,1]+1):
    #yen2 = arrsort[0,0] * i

#print(yen2)

hon = 0
yen2 = 0
yen3 = 0

for x in range(0,N):
            yen3 += yen2
            #print(x)
            #print("hon", hon)
            #print("yen3", yen3)
            for i in range(0,arrsort[x,1]):
                yen2 = arrsort[x,0] * (i+1)
                #print("yen2", yen2)
                hon += 1
                if hon == M:
                    break
                else:
                    continue
                break
            else:
                continue
            break

#print(hon)
#print(yen2)
#print(yen3)
print(yen3 + yen2)

Submission status: Accepted

Solution code:
n,m= map(int, input().split())
x = {}
for i in range(n):
    a,b = map(int, input().split())
    if a not in x:
        x[a] = b
    else:
        x[a] += b
# print(x)
s = sorted(x.keys())
# print(s)
ans = 0
c = 0
for ss in s:
    # print(ss, ans, c)
    if c + x[ss] <= m:
        ans += ss * x[ss]
        c += x[ss]
    else:
        ans += ss * (m-c)
        break
print(ans)

Submission status: Accepted

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

for i in range(N):
    Ai, Bi = map(int, input().split(" "))
    stores.append((Ai, Bi))    

price = 0
for a, b in sorted(stores, key=lambda x: x[0]):
    if M <= b:
        price += a * M
        break
    else:
        price += a * b
        M -= b
print(price)

Submission status: Accepted

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

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

d_list = sorted(d_list)

for j in range(len(d_list)):
  p1 = d_list[j][0]
  p2 = d_list[j][1]
  if m > p2:
    m -= p2
    total += p1 * p2
  else:
    total += p1 * m
    break

print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8

def main():
    N,M = map(int,input().split())
    stores = []
    cost = 0
    number = 0

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

    for store in stores:
        if number + store[1] >= M:
            cost += (M - number)*store[0]
            break
        else:
            number += store[1]
            cost += store[0]*store[1]

    print(cost)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

A_B_AB_list = []
for i in range(N):
    A,B = map(int,input().split())
    A_B_AB = [A,B,A*B]
    A_B_AB_list.append(A_B_AB)
A_B_AB_list.sort()
B_sums = [0]*(N+1)
AB_sums = [0]*(N+1)
for i in range(N):
    B_sums[i+1] = B_sums[i] + A_B_AB_list[i][1]
    AB_sums[i+1] = AB_sums[i] + A_B_AB_list[i][2]
for i in range(1,N+1):
    if B_sums[i] >= M:
        amari = M - B_sums[i-1] 
        ans = AB_sums[i-1] + amari*A_B_AB_list[i-1][0]
        print(ans)
        break

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
price = []
honsu = []
c = []
for i in range(n):
  a,b = map(int,input().split())
  c.append([a,b])
c = sorted(c)
price,honsu=zip(*c)
money = 0
l = 0
while m > 0:
  if(m-honsu[l]>0):
    m -= honsu[l]
    money += honsu[l] * price[l]
  else:
    c = abs(m-honsu[l])
    m -= honsu[l]-c
    money += (honsu[l]-c) * price[l]
  l += 1
print(money)

Submission status: Accepted

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

arr = sorted(arr)
sum = 0
i = 0
while M > 0:
    a = arr[i]
    b = min(M,a[1])
    sum = sum + b * a[0]
    M = M - b
    i = i + 1

print(sum)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
import numpy as np
data = np.zeros([n,2],dtype=np.int)
for i in range(n):
    a,b=map(int,input().split())
    data[i] = [a,b]
    
#print(data)

data_sort = data[np.argsort(data[:, 0])]
#print(data_sort)

honsu = 0
money = 0
idx = 0
while(honsu<m):
    if (m-honsu)>data_sort[idx,1]:
        money+=data_sort[idx,0]*data_sort[idx,1]
        honsu += data_sort[idx,1]
        idx+=1
    else:
        money+=data_sort[idx,0]*(m-honsu)
        honsu += data_sort[idx,1]
        idx+=1
    #print(money)
    #print(honsu)
        
print(money)

Submission status: Accepted

Solution code:
import sys
def main():
  N,M = map(int, input().split())
  AB = []
  for _ in range(N):
    AB.append(list(map(int,input().split()))) 
  AB.sort(key = lambda x: x[0])
  ans = 0
  sumB = 0
  for row in AB:
    if row[1] > (M - sumB):
      ans += row[0] * (M - sumB)
      print(ans)
      sys.exit()
    else:
      ans += row[0] * row[1]
      sumB += row[1]
  print(ans)
main()

Submission status: Accepted

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

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

cash = 0
for i in range(N):
    if M == 0:
        break
    elif M - a_sort[i][1] > 0:
        cash += a_sort[i][0] * a_sort[i][1]
        M -= a_sort[i][1]
    else:
        cash += M * a_sort[i][0]
        M = 0
print(cash)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
d = []
for i in range(N):
    a, b = map(int, input().split())
    d.append((a, b))
d = sorted(d)
num = 0
ans = 0
for i in range(N):
    a, b = d[i]
    if num + b < M:
        num += b
        ans += a * b
    else:
        ans += a * (M - num)
        break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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

ls = list(sorted(ls))

i = 0
_m = 0
total = 0

while _m < m:
    a, b = ls[i]
    _m += b
    total += a * b
    i += 1

a, b = ls[i - 1]
print(total - (_m - m) * a)

Submission status: Accepted

Solution code:
import heapq

N, M = map(int, input().split())
stores = []
for _ in range(N):
    a, b = map(int, input().split())
    heapq.heappush(stores, (a, b))

ans = 0
drinks = 0
while drinks < M:
    a, b = heapq.heappop(stores)
    if b <= M - drinks:
        ans += a*b
        drinks += b
    else:
        ans += a*(M - drinks)
        drinks += M - drinks
print(ans)

Submission status: Accepted

Solution code:
N,M = map(int, input().strip().split(' '))
AB = [list(map(int, input().strip().split(' '))) for _ in range(N)]
AB = sorted(AB, key=lambda x: x[0])
now_price = 0
remain_count = M
for a,b in AB:
  bay = min(remain_count, b)
  now_price += a*bay
  remain_count -= bay
print(now_price)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    L = []
    [L.append(list(map(int,input().split()))) for i in range(N)]
    L.sort(key=lambda x:x[0])
    
    drink = 0
    sougaku = 0
    
    #愚直に回す
    for i in range(N):
        nedan, kazu = L[i]
        for j in range(kazu):
            if drink == M:
                break
            else:
                drink = drink + 1
                sougaku = sougaku + nedan
    
    print(sougaku)
   
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

cost = 0
count = 0
for a, b in AB:
	if b < M - count:
		cost += a * b
		count += b
	else:
		t = M - count
		cost += a * t
		count += t
		break
print(cost)

Submission status: Accepted

Solution code:
N,m = list(map(int,input().split()))
data = [[0,0] for i in range(N)]
for i in range(N):
	data[i] = list(map(int,input().split()))
data.sort()
cost = 0
for i in range(N):
	prices,nums = data[i]
	if m > nums:
		cost += prices * nums
		m -= nums
	else:
		cost += prices * m
		print(cost)
		break

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(n)]
AB.sort()
res = 0
for a,b in AB:
    res += a*min(b,m)
    m -= b
    if m < 0:
        break 
print(res)

Submission status: Accepted

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

money = 0
count = 0

for a, b in V:
  if count + b < M:
    count += b
    money += a * b
  else:
    money += (M - count) * a
    break
print(money)

Submission status: Accepted

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, m = read_line(int, int)

x = []
for i in range(n):
    ai, bi = read_line(int, int)
    x.append((ai, bi))

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

cost = 0
for a, b in x:
    buy = min(m, b)
    m -= b
    cost += a * buy
    if m <= 0:
        print(cost)
        break

Submission status: Accepted

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

dic={}
for _ in range(n):
    a,b = map(int,input().split())
    if a in dic:
        dic[a]+=b
    else:
        dic[a]=b
dic = sorted(dic.items())

count=0
sum=0
for tup in dic:
    if tup[1] > m-count:
        sum+=tup[0]*(m-count)
        break
    sum+=tup[0]*tup[1]
    count+=tup[1]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

buy = 0
ans = 0

for price, num in Store:
    if buy+num < M:
        buy += num
        ans += price * num
    else:
        ans += (M - buy) * price
        break
print(ans)

Submission status: Accepted

Solution code:
text = input()
lines = text.split("\n")

lines = list(map(lambda x: x.split(" "), lines))
n_and_m = lines[0]
n = n_and_m[0]
m = n_and_m[1]
a_and_b = []
for i in range(int(n)):
    a_and_b.append(input().split(" "))


tmp = []

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


remain = int(m)

price = 0
for i in range(len(a_and_b)):
    for j in range(int(a_and_b[i][1])):
        remain = remain - 1
        price = price + int(a_and_b[i][0])
        if (remain == 0):
            break
    else:
        continue
    break
print(price)

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter
input = sys.stdin.readline

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

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

money = 0
for i in range(index):
    money += AB[i][0]*AB[i][1]
money += (M-count)*AB[index][0]
print(money)

Submission status: Accepted

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

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

# Aをkeyにしてソート
AB = sorted(AB, key=lambda x: x[0])

# 先頭からM本選ぶ
i = 0 # 店のインデックス
c = 0 # 現在の本数
p = 0 # 合計の値段
while c < M:
    a, b = AB[i]
    if c + b > M:
        d = (M - c) # 足りない本数
        c += d
        p += a * d
    else:
        c += b 
        p += a * b
    i += 1

print(p)

Submission status: Accepted

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

shop = {}
b = [0] * N


for i in range(N):
    tmp = list(map(int,input().split()))
    shop[i] = tmp[0]
    b[i] = tmp[1]


shop = sorted(shop.items(), key=lambda x:x[1])


ans = 0
tmp = 0
for i in range(N):
    if(tmp + b[shop[i][0]] >= M):
       ans += shop[i][1] * (M - tmp)
       break
    else:
        ans += shop[i][1] * b[shop[i][0]]
        tmp += b[shop[i][0]]


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.sort()
tmp,ans=0,0
for i in range(N):
    if tmp+x[i][1]<M:
        tmp+=x[i][1]
        ans+=x[i][0]*x[i][1]
    else:
        ans+=(M-tmp)*x[i][0]
        break
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=sorted(arr,key=lambda x:x[0])
ans=0
count=0
for i in range(n):
  if arr[i][1]<k-count:
    ans+=arr[i][0]*arr[i][1]
    count+=arr[i][1]
  else:
    ans+=arr[i][0]*(k-count)
    print(ans)
    break

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=sorted([list(map(int,input().split()))for i in range(n)],key=lambda x:x[0])
ans=0
for s,t in x:
#    print(s,t,m)
    if m>=t:
        m-=t
        ans+=s*t
    else:
        ans+=m*s
        break
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.sort()
rest = M
cost = 0
while True:
  if rest - ab[0][1] <= 0:
    cost += rest * ab[0][0]
    break
  else:
    rest -= ab[0][1]
    cost += ab[0][0] * ab[0][1]
    del ab[0]
print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
 
AB = []
 
for i in range(N) : 
    tmp_AB = [int(i) for i in input().split()]
    AB.append(tmp_AB)
    
P = sorted(AB)
 
sum = 0
cnt = 0 # Mに達したら終了

for i in range(len(P)) : 
    for j in range(P[i][1]) : 
        if cnt < M: 
            sum += P[i][0]
            cnt += 1
        else :
            break
        
    if cnt >= M : 
        break
    
print(sum)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
li=[]
for i in range (0,n):
  li.append(list(map(int,input().split())))
li.sort()
bought=0
pay=0
t=0
while bought<k:
  pay=pay+(li[t][0])*(li[t][1])
  bought=bought+li[t][1]
  t=t+1
pay=pay-li[t-1][0]*li[t-1][1]
bought=bought-li[t-1][1]
bought_2=k-bought
pay_2=li[t-1][0]*bought_2
pay=pay+pay_2
print(pay)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
list = [list(map(int,input().split())) for j in range(N)]
list = sorted(list)
tm = M
ans = 0
for li in list:
    if li[1] < tm:
        ans += li[0]*li[1]
        tm -= li[1]
    else:
        ans += li[0]*tm
        print(ans)
        exit()

Submission status: Accepted

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

shops = []
for _ in range(N):
    ab = [int(c) for c in input().split()]
    shops.append(ab)

cost = 0
bought = 0
s_shops = sorted(shops)

for shop in s_shops:
    buying = 0
    while buying < shop[1]:
        if bought == M:
            break
        cost += shop[0]
        buying += 1
        bought += 1

    if bought == M:
        break

print(cost)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
store = []
can_num = m
cost = 0

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

store.sort()

while can_num > 0:
    lowest_store = store.pop(0)
    buy_num = lowest_store[1] if lowest_store[1] < can_num else can_num
    cost += buy_num * lowest_store[0]
    can_num -= buy_num

print(cost)

Submission status: Accepted

Solution code:
N, M=[int(x) for x in input().split()]
shopl = [[int(x) for x in input().split()] for _ in range(N)]
sl =sorted(shopl,key=lambda x:(x[0])) 
cnt = 0
price =0
for i in range(N):
    amt = min(sl[i][1], M - cnt)
    price += sl[i][0] * amt
    cnt +=amt
    if cnt == M:
      break
print(price)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,M = map(int, input().rstrip().split())
AB = [list(map(int, input().rstrip().split())) for _ in range(N)]
AB.sort(key=lambda x: x[0])
count = 0
price = 0
for i in range(N):
    count += AB[i][1]
    price += AB[i][0]*AB[i][1]
    if count >= M:
        break
mod = count - M
if mod != 0:
    price -= AB[i][0]*mod
print(price)

Submission status: Accepted

Solution code:
x = list(map(int,input().split()))
a = [list(map(int,input().split())) for l in range(x[0])]

a.sort()
buy = 0
money = 0
for i in a:
  if (buy + i[1]) <= x[1]:
    money += i[0] * i[1]
    buy += i[1]
  else:
    money += i[0] * (x[1] - buy)
    break
    
print(money)

Submission status: Accepted

Solution code:
stores, target = [int(i) for i in input().split()]

values = []
for i in range(stores):
    values.append([int(j) for j in input().split()])

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

expence = 0

for i in range(len(values)):
    if values[i][1] < target:
        expence += values[i][0] * values[i][1]
        target -= values[i][1]
    elif values[i][1] == target:
        expence += values[i][0] * values[i][1]
        break
    elif values[i][1] > target:
        expence += values[i][0] * target
        break
print(expence)

Submission status: Accepted

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

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()
totl=0
x=0
for j,k in a:
  if x+k<m:
    totl+=j*k
    x+=k
  else:
    totl+=j*(m-x)
    break
print(totl)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    a.append(list(map(int, input().split())))
    
a = sorted(a, key=lambda x: x[0])
    
for i in range(N):
    while a[i][1] > 0:
        a[i][1] -= 1
        cost += a[i][0]
        M -= 1
        if M == 0:
            break
    if M == 0:
        break

print(cost)

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.sort()
x,ans=0,0
for i,j in l:
    if x+j<m:
        ans+=i*j
        x+=j
    else:
        ans+=i*(m-x)
        break
print(ans)

Submission status: Accepted

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

kane = 0
hon = 0
for i in range(N):
    if hon + AB[i][1] < M:
        hon += AB[i][1]
        kane += AB[i][0] * AB[i][1]
        continue
    elif hon + AB[i][1] == M:
        hon += AB[i][1]
        kane += AB[i][0] * AB[i][1]
        print(kane)
        break
    else:
        kane += (M - hon) * AB[i][0]
        print(kane)
        break

Submission status: Accepted

Solution code:
N, M= map(int, input().split())
A = []
for i in range(N):
    a = list(map(int, input().split()))
    A.append(a)
A = sorted(A)
count = 0
cost = 0
i = 0
while count < M:
    cost += A[i][0] * min(A[i][1],M-count)
    count += min(A[i][1],M-count)
    i += 1    
print(cost)

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

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

ans = 0
for i in range(N):
    a = sortedAB[i][0]
    b = sortedAB[i][1]
    if M == 0:
        print(ans)
        exit()
    if b <= M:
        ans += a*b
        M -= b
    else:
        ans += a*M
        M = 0

print(ans)

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  a, b = [int(i) for i in input().split()]
  if not a in dic:
    dic[a] = b
  else:
    dic[a] += b
ndic = sorted(dic.items())

ans = 0
for i, v in ndic:
  if m <= v:
    ans += m * i
    break
  else:
    ans += v * i
    m -= v
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A_B = []
for i in range(N):
    A_B.append(list(map(int, input().split())))
A_B.sort()
sum = 0
money = 0
i = 0
while sum <= M and i < N:
    money += A_B[i][0] * A_B[i][1]
    sum += A_B[i][1]
    i += 1
money -= A_B[i-1][0] * (sum - M)
print(money)

Submission status: Accepted

Solution code:
# C - Energy Drink Collector
# https://atcoder.jp/contests/abc121/tasks/abc121_c

import operator

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

stores = list()
for _ in range(N):
    stores.append( tuple(map(int, raw_input().split())) )

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

money = 0
cans_cnt = 0
for cost_i, cans_i in stores:
    buy_i = min(M - cans_cnt, cans_i)
    money += (cost_i * buy_i)
    cans_cnt += buy_i
    if (cans_cnt == M):
        break

print '{}'.format(money)

Submission status: Accepted

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

AB = sorted(AB,key = lambda k:k[0])
yen =0
num =0
for j in range(N):
    num += AB[j][1]
    if num >= M:
        yen += (M - (num - AB[j][1])) * AB[j][0]
        break
    else:
        yen += AB[j][1]*AB[j][0]

print(yen)

Submission status: Accepted

Solution code:
# 2150 start

import numpy

N,M=map(int, input().split())
price = numpy.empty((N,3))
sumcan = 0
sumprice = 0
lastshop = 0

for i in range(N):
    shop_data = input().split()
    A = int(shop_data[0])
    B = int(shop_data[1])
    price[i,0]= A * B
    price[i,1] = A
    price[i,2] = B

price = price[price[:,1].argsort(),:]

for j in range(M):
    sumcan = sumcan + price[j,2]
    if sumcan < M:
        sumprice = sumprice + price[j,0]
    elif sumcan >= M:
        sumcan = sumcan - price[j,2]
        lastshop = j
        break

sumprice = sumprice + (M - sumcan)*price[lastshop,1]

print(int(sumprice))

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
A = []
quantity = 0
price = 0

for i in range(N):
    a = list(map(int, input().split()))
    A.append(a)
A = sorted(A)
for i in range(M):
    quantity += A[i][1]
    if quantity < M:
        price += A[i][0]*A[i][1]
    else:
       price += A[i][0]*(M-(quantity-A[i][1]))
       break
print(price)

Submission status: Accepted

Solution code:
N, M = map(int, raw_input().split())
ans = 0
list = [map(int , raw_input().split()) for n in range(N)]
list = sorted(list, key = lambda list: list[0])
cnt = 0
for n in range(N):
    A, B = list[n]
    for b in range(B):
        ans += A
        cnt += 1
        if cnt == M:
            print(ans)
            exit()

Submission status: Accepted

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

ab = []

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

ab.sort()

sum_size = 0
total = 0
for i in range(n):
    price = ab[i][0]
    size = ab[i][1]
    sum_size += size
    total += price*size
    if sum_size > m:
        total -= (sum_size-m)*price
        break
print(total)

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

ans=0
quantity=0

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

print(ans)

Submission status: Accepted

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

AB.sort(key=lambda x:x[0])
tmp = M
ans = 0

for i in range(N):
    #break
    if tmp <= AB[i][1]:
        ans += tmp * AB[i][0]
        break
    
    ans += AB[i][0] * AB[i][1]
    tmp = tmp - AB[i][1]


print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())  # N軒の店舗, M本欲しい
# 1本Ai円のドリンクをBi本まで買うことができる
ans = 0

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

# print(shop_list)

shop_list = []
for _ in range(N):
    hoge = {}
    A, B = map(int, input().split())
    hoge['price'] = A
    hoge['amount'] = B
    shop_list.append(hoge)
    hoge = {}

shop_list.sort(key=lambda x: x['price'])
# print(shop_list)
quontity = 0
money = 0
for i in range(N):
    money += shop_list[i]['price']*shop_list[i]['amount']
    # print('money = ', money)
    quontity += shop_list[i]['amount']
    # print('quontity = ', quontity)
    if quontity >= M:
        money -= shop_list[i]['price']*(quontity-M)
        break

print(money)

Submission status: Accepted

Solution code:
from sys import stdin
import heapq
hq = []  

NM = input()
NM = NM.split()
N = int(NM[0])
M = int(NM[1])

hq = []
AB = {}
for i in range(N):
  temp = input()
  temp = temp.split()
  AB[int(temp[0])] = int(temp[1])
  heapq.heappush(hq, (int(temp[0]), int(temp[1])))

cost = 0
amount = 0
for i in range(N):
  temp = heapq.heappop(hq)
  if amount + temp[1] < M:
    amount += temp[1]
    cost += temp[0]*temp[1]
  else:
    temp1 = M - amount
    cost += temp[0]*temp1
    break
    
print(cost)

Submission status: Accepted

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

amount = 0
quantity = 0
for s in shop:
    if quantity + s[1] < M:
        amount += s[0] * s[1]
        quantity += s[1]
    else:
        amount += (M - quantity) * s[0]
        quantity = M
        break

print(amount)

Submission status: Accepted

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


if __name__ == '__main__':
    n, m = map(int, input().split())
    shops = []
    ans = 0
    for i in range(n):
        shops.append(list(map(int, input().split())))
    shops.sort(key=lambda x:x[0])

    for shop in shops:
        price = shop[0]
        available = shop[1]
        if m - available > 0:
            ans += price * available
            m -= available
        else:
            ans += price * m
            break

    print(ans)

Submission status: Accepted

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

amount = 0
quantity = 0
for s in shop:
    if quantity + s[1] < M:
        amount += s[0] * s[1]
        quantity += s[1]
    else:
        amount += (M - quantity) * s[0]
        quantity = M
        break

print(amount)