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

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

drink_num=0
cost =0

for abi in ab:
  if drink_num + abi[1] >= m :
    cost += (m - drink_num)*abi[0]
    break
  else:
    drink_num += abi[1]    
    cost += abi[0]*abi[1]

print(cost)

Submission status: Accepted

Solution code:
n, m = (int(i) for i in input().split())
mise = []
for i in range(n):
  tmp = [int(i) for i in input().split()] 
  mise.append(tmp)
mise.sort()
hon = 0
kin = 0
for i in range(n):
  nokori = m - hon
  if nokori > mise[i][1]:
    hon += mise[i][1]
    kin += mise[i][0] * mise[i][1]
  else:
    hon += nokori
    kin += mise[i][0] * nokori
    break
print(kin)
  
  
#一番安い店で買う
#足りたら終わり
#次に安い店で買う

Submission status: Accepted

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

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

li.sort()
ans = 0

for i in li:
    if m <= i[1]:
        ans += m * i[0]
        break
    ans += i[0] * i[1]
    m -= 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.sort(key=lambda x: x[0])

s = 0
ans = 0
for abi in ab:
    a, b = map(int, abi)
    if s + b < m:
        s += b
        ans += a * b
    else:
        ans += a * (m - s)
        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()
cost = 0
count = 0
for A, B in AB:
    if M-count > B:
        cost += B*A
        count += B
    elif count < M:
        cost += (M-count)*A
        count += M - count
    else:
        break
print(cost)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l = sorted([list(map(int, input().split())) for _ in range(N)])
c, ans = 0,0
for (yen, stock) in l:
    if c + stock < M:
        c += stock
        ans += stock * yen
    else:
        ans += (M - c) * yen
        c = M
    if c == M:
        print(ans)
        break

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

n_drink = 0
cost = 0
for A, B in AB:
    remaining = M - n_drink
    if remaining > B:
        n_drink += B
        cost += A * B
    elif remaining <= B:
        cost += A * remaining
        print(cost)
        exit()

Submission status: Accepted

Solution code:
def main():
    N,M = map(int,input().split())
    store_l = []
    for i in range(N):
        store_l.append(tuple(map(int,input().split())))

    store_l = sorted(store_l,key=lambda x:x[0])
    ans = 0
    buynum = 0
    for price,maxnum in store_l:
        if(buynum+maxnum<=M):
            buynum += maxnum
            ans += maxnum*price
        else:
            ans += (M-buynum)*price
            break
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    stores = [string_to_int(i) for i in inputs]
    wants = M
    money = 0
    for price, num in sorted(stores, key=lambda i: i[0]):
        if wants > num:
            money += price * num
            wants -= num
        else:
            money += price * wants
            break

    return money


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(N))
    print(ret)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
e = [tuple(map(int, input().split())) for _ in range(n)]
e.sort()  # 金額昇順
ans = 0
rest = m
for a, b in e:
    use = min(rest, b)
    ans += use * a
    rest -= use
    if rest == 0:
        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.sort(key=lambda x: x[0])

d = 0
cost = 0

for i in range(n):
  d += l[i][1]
  cost += l[i][0]*l[i][1]
  if d >= m:
    print(cost-l[i][0]*(d-m))
    exit()

Submission status: Accepted

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

data = sorted(data, key=lambda x: x[0])
count = 0
total = 0
for d in data:
    count += d[1]
    if count >= M:
        total += d[0] * (d[1] - (count - M))
        break
    else:
        total += d[0] * d[1]
print(total)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

#import sys

#def input():
#    return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
a.sort()
price = [a[i][0] for i in range(N)]
stock = [a[i][1] for i in range(N)]
bought = 0
total_price = 0

for i in range(N):
    if bought + stock[i] <= M:
        total_price += price[i] * stock[i]
        bought += stock[i]
    else:
        total_price += price[i] * (M - bought)
        break
print(total_price)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys

def input():
    return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
a.sort()
price = [a[i][0] for i in range(N)]
stock = [a[i][1] for i in range(N)]
bought = 0
total_price = 0

for i in range(N):
    if bought + stock[i] <= M:
        total_price += price[i] * stock[i]
        bought += stock[i]
    else:
        total_price += price[i] * (M - bought)
        break
print(total_price)

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y

N, M = map(int, input().split())
drinks = []
for i in range(0, N):
	drinks.append(list(map(int, input().split())))
drinks.sort(key = lambda v: v[0])
sum = 0
left = M
for i in range(0, N):
	sum += drinks[i][0]*smaller(drinks[i][1], left)
	left -= smaller(drinks[i][1], left)
	if left == 0:
		break
print(sum)

Submission status: Accepted

Solution code:
def find(A,B,M):
    ans=0
    sofar=0
    C=sorted(zip(A,B))
    #print(C)
    for i in range(len(C)):
        yen,num=C[i]
        if sofar+num<=M:
            ans+=yen*num
            sofar+=num
        else:
            need=M-sofar
            ans+=yen*need
            break
    return ans
A=[]
B=[]
N,M=list(map(int,input().strip().split(' ')))
for _ in range(N):
    a,b=list(map(int,input().strip().split(' ')))
    A+=[a]
    B+=[b]
print(find(A,B,M))

Submission status: Accepted

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

AB.sort()

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

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().split())
arr = []
for _ in range(n):
	a, b = map(int, raw_input().split())
	arr.append((a,  b))
arr.sort()
money = 0
i = 0
while m > 0:
	a, b = arr[i]
	if m < b:
		money += a*m
		m -= m
	else:
		money += a*b
		m -= b
	i += 1
print money

Submission status: Accepted

Solution code:
def main():

    """ convenient functions
    # for i, a in enumerate(iterable)
    # q, mod = divmod(a, b)
    # divmod(x, y) returns the tuple (x//y, x%y)
    # Higher-order function: reduce(operator.mul, xyz_count, 1)
    # manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
    """

    """convenient decorator
    # @functools.lru_cache():
    # to facilitate use of recursive function
        # ex:
        # from functools import lru_cache
        # import sys
        # sys.setrecursionlimit(10**9)
        # @lru_cache(maxsize=None)
        # def fib(n):
        #     if n < 2:
        #         return n
        #     return fib(n-1) + fib(n-2)
        # print(fib(1000))
    """
    
    # import numpy as np
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
    # accumulate() returns iterator! to get list: list(accumulate())
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  # prime factor
        b, e = 2, 0  # base, exponent
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        # TODO: How should I do when n - r is negative?
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
    from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
    import operator
    from operator import itemgetter
    # ex1: List.sort(key=itemgetter(1))
    # ex2: sorted(tuples, key=itemgetter(1,2))
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    # first create factorial_list
    # fac_list = mod_factorial_list(n)
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
    def modinv(a, p = MOD):
        # evaluate reciprocal using Fermat's little theorem:
        # a**(p-1) is identical to 1 (mod p) when a and p is coprime
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        # fac_list = modfactorial_list(100)
        # print(modcomb(100, 5, modfactorial_list(100)))
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    # initialize variables
        # to initialize list, use [0] * n
        # to initialize two dimentional array, use [[0] * N for _ in range(N)]
    # set inputs
        # open(0).read() is a convenient method:
        # ex) n, m, *x = map(int, open(0).read().split())
        #     min(x[::2]) - max(x[1::2])
        # ex2) *x, = map(int, open(0).read().split())
        #     don't forget to add comma after *x if only one variable is used
    # preprocessing
        # transpose = [x for x in zip(*data)]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
        # flat = [flatten for inner in data for flatten in inner]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # calculate and output
        # output pattern
        # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
    """

    # functions used
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    # single int: int(r())
    # single string: r()
    # single float: float(r())
    # line int: R()
    # line string: r().split()
    # line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
    # lines int: [R() for _ in range(n)]
    """

    if __name__ != '__main__':
        # for test
        sys.stdin = open('sample.txt')

    # ----------------------------------
    # main
    
    N, M = R()
    AB = [R() for _ in range(N)]
    AB.sort()
    ans = 0
    for a, b in AB:
        if M - b > 0:
            ans += a*b
            M -= b
        else:
            ans += a*M
            M = 0
        if M == 0:
            print(ans)
            exit()
    
    # end of main
    # ----------------------------------

    """memo: how to use defaultdict of list
    # initialize
    Dic = defaultdict(list)
    # append / extend
    Dic[x].append(y)
    # for
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
class Shop:
    def __init__(self, price: int, limit: int):
        self.price = price
        self.limit = limit

    def __lt__(self, other):
        return self.price < other.price


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

shops.sort()

ret = 0
for s in shops:
    if s.limit > M:
        ret += s.price * M
        break
    ret += s.price * s.limit
    M -= s.limit

print(ret)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
 
ab = [[int(x) for x in input().split()] for _ in range(n)]
 
ab.sort(key=lambda x: x[0])
 
count, cost, index = 0, 0, 0

for x in ab:
  bottle = x[1]
  if count + bottle < m:  
    count += bottle
    cost += ab[index][0] * bottle
    index += 1
  else:
    cost += ab[index][0] * (m - count)
    break

print(cost)

Submission status: Accepted

Solution code:
# cf. https://rion778.hatenablog.com/entry/2019/03/10/004006
import sys
input = sys.stdin.readline
 
N, M = [int(i) for i in input().split()]
 
plices = [[int(i) for i in input().split()] for _ in range(N)]
 
plices.sort(key = lambda x:x[0])
 
m = 0
cost = 0
 
for i in range(N):
    if m < M:
      buy = min(M - m, plices[i][1])
      cost += plices[i][0] * buy
      m += buy
    else:
      break

print(cost)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    store = [[int(a) for a in input().split()] for _ in range(n)]
    store = sorted(store, key=lambda x: x[0])

    money = 0
    drink = 0
    for s in store:
        if drink + s[1] > m:
            money += (m - drink) * s[0]
            break
        money += s[1] * s[0]
        drink += s[1]
    print(money)


main()

Submission status: Accepted

Solution code:
from operator import itemgetter

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

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

money = 0
i = 0
while M > 0:
    if M > AB[i][1]:
        M -= AB[i][1]
        money += AB[i][0] * AB[i][1]
    else:
        money += AB[i][0] * M
        break
    i += 1

print(money)

Submission status: Accepted

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

sorted_ab = sorted(ab)
sum = 0
total_cost = 0
for a, b in sorted_ab:
    diff = m - sum
    if diff < b:
        sum += diff
        total_cost += a*diff
        break
    else:
        sum += b
        total_cost += a*b

print(total_cost)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
ab.sort()
#print(n,m,ab)
total = 0#合計金額
num = 0#本数
i = 0
while num < m:
    if num + ab[i][1] <= m:
        num += ab[i][1]
        total += ab[i][0]*ab[i][1]
        i += 1
        #print(num,total,i,0)
    elif num + ab[i][1] > m:
        total += ab[i][0]*(m - num)
        num = m
        i += 1
        #print(num,total,i,1)
print(total)

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()
ans = 0
for s in shop:
    if M <= 0:
        break
    tmp = min(s[1],M)
    ans += tmp * s[0]
    M -= tmp
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()
ans = 0
for i in range(N):
    if M <= 0:
        break
    tmp = min(shop[i][1],M)
    ans += tmp * shop[i][0]
    M -= tmp
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(reverse=True)
ans = 0
while M > 0:
    a,b = shop.pop()
    tmp = min(b,M)
    ans += tmp * a
    M -= tmp
print(ans)

Submission status: Accepted

Solution code:
# ABC121C - Energy Drink Collector
import sys
input = sys.stdin.readline

def main():
    n, m = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, input().split())) for _ in range(n))
    ans = 0
    for i in lst:
        while i[1] > 0 and m > 0:
            ans += i[0]
            i[1] -= 1
            m -= 1
    print(ans)


if __name__ == "__main__":
    main()

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

drink.sort()
money = 0
for a, b in drink:
    if b > M:
        money += a * M
        break
    else:
        money += a * b
        M -= b
print(money)

Submission status: Accepted

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

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

Submission status: Accepted

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

drink = sorted(drink)
money = 0
for a, b in drink:
    if b > M:
        money += a * M
        M = 0
    else:
        money += a * b
        M -= b
print(money)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
S = []
C = 0
cost = 0
for i in range(N):
  a, b = map(int, input().split()) 
  S.append((a, b))
S.sort()
for i in range(N):
  if S[i][1] + C < M:
    C += S[i][1]
    cost += S[i][0] * S[i][1]
  else:
    cost += (M-C) * S[i][0]
    break
print(cost)

Submission status: Accepted

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

C = sorted(C, key=lambda x: x[0], reverse=False)
i = 0
ans = 0
while M>0:
    if M-C[i][1]>=0:
        ans += C[i][0]*C[i][1]
        M -= C[i][1]
        i += 1
    else:
        ans += M*C[i][0]
        M = 0
print(ans)

Submission status: Accepted

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

sum_cost = 0
n_drink = 0
for AB in AB_list:
    A, B = AB
    if n_drink + B < M:
        n_drink += B
        sum_cost += A * B
    elif n_drink + B >= M:
        sum_cost += A * (M - n_drink)
        print(sum_cost)
        exit()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=[]


for i in range(n):
    s,p=map(int,input().split())
    a+=[[s, p] ]
    

import operator as op
a.sort(key = op.itemgetter(0))

ans=0
get=0
look=0
while get<k:
    get=get+1
    a[look][1]=a[look][1]-1
    ans=ans+a[look][0]
    if a[look][1]==0:
        look=look+1

print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
table = []
for i in range(N):
    table.append(list(map(int,input().split())))
table.sort(key=lambda x: x[0])
num = 0
total = 0
i = 0
while num < M:
    num += table[i][1]
    total += table[i][1] * table[i][0]
    cost = table[i][0]
    i += 1
print(total-cost*(num-M))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
cost = 0  # 支払うお金
numofdrink = 0  # 手に入れたドリンクの数
price = [0] * N
for i in range(N):
    price[i] = [0] * 2
    price[i][0], price[i][1] = map(int, input().split())
price = sorted(price)

ite = 0
while (numofdrink < M):
    if numofdrink + price[ite][1] >= M:
        cost += (M - numofdrink) * price[ite][0]
    else:
        cost += price[ite][0] * price[ite][1]

    numofdrink += price[ite][1]
    ite += 1

print(cost)

Submission status: Accepted

Solution code:
store_num, needed_drink_num = tuple(int(i) for i in input().split(" "))
drink_list = []
for i in range(store_num):
    drink_list.append(tuple(int(i) for i in input().split(" ")))


def tuple_index_0(tup):
    return tup[0]


drink_list.sort(key=tuple_index_0)
ans = 0
for i in drink_list:
    if i[1] < needed_drink_num:
        needed_drink_num -= i[1]
        ans += i[0] * i[1]
    else:
        ans += i[0] * needed_drink_num
        print(ans)
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
B = sorted(A, key=lambda x:x[0])
cnt = 0
ans = 0
for i,j in B:
    for k in range(j):
        if cnt == M:
            break
        cnt += 1
        ans += i
else:
    print(ans)

Submission status: Accepted

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

pc_list.sort(key=lambda x:(x[0],x[1]))

pc_list

cnt = M
sum_price = 0
for p,c in pc_list:
    
    if c <= cnt:
        sum_price += p*c
        cnt -= c
    else:
        sum_price += p*cnt
        break
#     print(sum_price)
print(sum_price)

Submission status: Accepted

Solution code:
from operator import itemgetter
import numpy as np

N, M = map(int, input().split()) # 横に2個
stores = [list(map(int, input().split())) for _ in range(N)]

astores = np.array(sorted(stores, key=itemgetter(0)))

stock = 0
price = 0
for i in range(N):
    if stock + astores[i, 1] > M:
        price += astores[i, 0] * (M - stock)
        break
    else:
        price += astores[i, 0] * astores[i, 1]
        stock += astores[i, 1]

print(price)

Submission status: Accepted

Solution code:
# Aiで貪欲する
# kが何度も登場するっていう罠？

n, m = map(int, input().split())
h = dict()
for _ in range(n):
    k, v = map(int, input().split())
    if k in h:
        h[k] += v
    else:
        h[k] = v
h = sorted(h.items(), key=lambda x: x[0])


ans = 0
for k, v in h:
    while v > 0:
        if m <= 0:
            print(ans)
            exit()
        else:
            v -= 1
            ans += k
            m -= 1
print(ans)

Submission status: Accepted

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

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

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

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

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
c = 0
for a, b in AB:
    if c + b < M:
        c += b
        ans += a * b
    else:
        i = M - c
        ans += a * i
        break

print(ans)

Submission status: Accepted

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

ab.sort()

count=0
cost=0

while count!=M:
    for i in range(N):
        if count+ab[i][1]<=M:
            cost=cost+ab[i][0]*ab[i][1]
            count+=ab[i][1]
        else:
            cost=cost+(M-count)*ab[i][0]
            count=M

print(cost)

Submission status: Accepted

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

total = 0
for i in drink_list:
    buy = min(m,i[1])
    total += i[0] * buy
    m -= buy

print(total)

Submission status: Accepted

Solution code:
"""
n = int(sys.stdin.readline())
a, b = map(int, sys.stdin.readline().split())
c = c
s = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

a = 100
b = 0.987654321
print('{0:06d}-{1:6f}'.format(a,b))  # 0埋め, 小数点出力桁指定時のときの出力
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

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


mod = 1000000007

if __name__ == "__main__":
    N, M = map(int, sys.stdin.readline().split())
    list_2d = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
    list_2d = sorted(list_2d)
    bought = 0
    total = 0
    for l in list_2d:
        for i in range(l[1]):
            bought += 1
            total += l[0]
            if bought == M:
                print(total)
                exit()