Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant.

Shop, where Noora is working, has a plan on the following n days. For each day sales manager knows exactly, that in i-th day k_{i} products will be put up for sale and exactly l_{i} clients will come to the shop that day. Also, the manager is sure, that everyone, who comes to the shop, buys exactly one product or, if there aren't any left, leaves the shop without buying anything. Moreover, due to the short shelf-life of the products, manager established the following rule: if some part of the products left on the shelves at the end of the day, that products aren't kept on the next day and are sent to the dump.

For advertising purposes manager offered to start a sell-out in the shop. He asked Noora to choose any f days from n next for sell-outs. On each of f chosen days the number of products were put up for sale would be doubled. Thus, if on i-th day shop planned to put up for sale k_{i} products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·k_{i} products. Consequently, there is an opportunity to sell two times more products on days of sell-out.

Noora's task is to choose f days to maximize total number of sold products. She asks you to help her with such a difficult problem.


-----Input-----

The first line contains two integers n and f (1 ≤ n ≤ 10^5, 0 ≤ f ≤ n) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out.

Each line of the following n subsequent lines contains two integers k_{i}, l_{i} (0 ≤ k_{i}, l_{i} ≤ 10^9) denoting the number of products on the shelves of the shop on the i-th day and the number of clients that will come to the shop on i-th day.


-----Output-----

Print a single integer denoting the maximal number of products that shop can sell.


-----Examples-----
Input
4 2
2 1
3 5
2 3
1 5

Output
10
Input
4 1
0 2
0 3
3 5
0 6

Output
5


-----Note-----

In the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2, 6, 2, 2] respectively. So on the first day shop will sell 1 product, on the second — 5, on the third — 2, on the fourth — 2. In total 1 + 5 + 2 + 2 = 10 product units.

In the second example it is possible to sell 5 products, if you choose third day for sell-out.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Greedy algorithms


Solutions:
(n, f) = map(int, input().split())
tally = []
for i in range(n):
	(a, b) = map(int, input().split())
	tally.append([a, b])
diff = []
total = 0
for i in range(n):
	total += min(tally[i][1], tally[i][0])
	diff.append(min(2 * tally[i][0], tally[i][1]) - min(tally[i][1], tally[i][0]))
diff.sort()
for i in range(f):
	total += diff[n - 1 - i]
print(total)


ni = list(map(int, input().strip().split(' ')))
n = ni[0]
f = ni[1]
s = 0
li = []
for i in range(n):
	arr = list(map(int, input().strip().split(' ')))
	k = arr[0]
	l = arr[1]
	s = s + min(l, k)
	if l > k:
		li.append(min(k, l - k))
li.sort(reverse=True)
if f >= len(li):
	s = s + sum(li)
else:
	s = s + sum(li[:f])
print(s)


(n, f) = map(int, input().split())
a = []
for i in range(n):
	temp = list(map(int, input().split()))
	if temp[0] >= temp[1]:
		temp.append(0)
	elif temp[0] * 2 >= temp[1]:
		temp.append(temp[1] - temp[0])
	else:
		temp.append(temp[0])
	a.append(temp)
a.sort(key=lambda x: x[2], reverse=True)
sum = 0
for i in range(n):
	if i < f:
		if a[i][0] * 2 >= a[i][1]:
			sum += a[i][1]
		else:
			sum += a[i][0] * 2
	elif a[i][0] >= a[i][1]:
		sum += a[i][1]
	else:
		sum += a[i][0]
print(sum)


(n, f) = map(int, input().split())
(a, ans) = ([0] * n, 0)
for i in range(n):
	(k, l) = map(int, input().split())
	ans += min(k, l)
	a[i] = min(2 * k, l) - min(k, l)
ans += sum(sorted(a)[n - f:])
print(ans)


(n, k) = map(int, input().split())
s = 0
c = []
l = 0
for i in range(n):
	(a, b) = map(int, input().split())
	if a >= b:
		s += b
	else:
		s += a
		if 2 * a >= b:
			c.append(b - a)
			l += 1
		else:
			c.append(a)
			l += 1
c = sorted(c)
c = c[::-1]
if l < k:
	k = l
for i in range(k):
	s += c[i]
print(s)


(n, f) = map(int, input().split())
plan = []
result = 0
for i in range(n):
	(k, l) = map(int, input().split())
	a = k if k <= l else l
	result += a
	k *= 2
	plan.append((k if k <= l else l) - a)
result += sum(sorted(plan, reverse=True)[:f])
print(result)


(n, f) = map(int, input().split())
t = []
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	if a > b:
		ans += b
	else:
		ans += a
		t.append(min(a * 2, b) - a)
t.sort(reverse=True)
f = min(f, len(t))
for i in range(f):
	ans += t[i]
print(ans)


import math
import re
(n, f) = list(map(int, input().split()))
k = []
l = []
d = []
d1 = []
for i in range(n):
	(k, l) = list(map(int, input().split()))
	if k >= l:
		d.append(0)
		d1.append(l)
	elif 2 * k >= l:
		d.append(l - k)
		d1.append(k)
	else:
		d.append(k)
		d1.append(k)
d = sorted(d, reverse=True)
print(sum(d[:f]) + sum(d1))


(n, f) = list(map(int, input().split()))
d = []
for i in range(n):
	d.append(list(map(int, input().split())))
d.sort(reverse=True, key=lambda x: min(2 * x[0], x[1]) - min(x[0], x[1]))
res = 0
for i in range(n):
	if i < f:
		res += min(2 * d[i][0], d[i][1])
	else:
		res += min(d[i][0], d[i][1])
print(res)


read = lambda : map(int, input().split())
(n, f) = read()
p = [tuple(read()) for i in range(n)]
d = sorted([min(2 * k, l) - min(k, l) for (k, l) in p])[::-1]
ans = sum((min(k, l) for (k, l) in p)) + sum(d[:f])
print(ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

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

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
(n, f) = value()
extra = []
amount = 0
for i in range(n):
	(k, peap) = value()
	cur = min(k, peap)
	amount += cur
	inc = min(2 * k, peap)
	extra.append(inc - cur)
extra.sort(reverse=True)
print(amount + sum(extra[0:f]))


get = lambda : map(int, input().split())
(n, f) = get()
arr = []
for i in range(n):
	(k, l) = get()
	arr.append([k, l])
A = []
for (i, j) in arr:
	A.append((min(2 * i, j), min(i, j)))
A.sort(reverse=True, key=lambda x: x[0] - x[1])
(A, B) = zip(*A)
print(sum(A[:f]) + sum(B[f:]))


(n, f) = list(map(int, input().split()))
r = []
for _ in range(n):
	(a, b) = list(map(int, input().split()))
	if 2 * a <= b:
		r.append((a, a))
	elif a <= b:
		r.append((b - a, a))
	else:
		r.append((0, b))
ret = 0
r1 = sorted(r)[::-1]
for (a, b) in r1[:f]:
	ret += a + b
for (a, b) in r1[f:]:
	ret += b
print(ret)


days = list(map(int, input().split()))
dub = []
sum = 0
for i in range(days[0]):
	a = list(map(int, input().split()))
	sum += min(a)
	dub.append(min([a[0] * 2, a[1]]) - min(a))
dub.sort()
for i in range(days[0] - 1, days[0] - days[1] - 1, -1):
	sum += dub[i]
print(sum)


(n, f) = map(int, input().split())
final = []
for i in range(n):
	(products, people) = map(int, input().split())
	final.append([products, people])
final.sort(key=lambda x: min(2 * x[0], x[1]) - min(x[0], x[1]), reverse=True)
pair = count = 0
while count < f and pair < len(final):
	if final[pair][0] != 0 and final[pair][1] != 0:
		final[pair][0] *= 2
		count += 1
	pair += 1
print(sum((min(i[0], i[1]) for i in final)))


import sys
import math
import collections

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
(n, f) = get_ints()
selected = []
double = []
for i in range(n):
	(k, l) = get_ints()
	s = min(k, l)
	selected.append(s)
	new_s = min(2 * k, l)
	change = new_s - s
	double.append(change)
double.sort(reverse=True)
ans = sum(selected) + sum(double[:f])
print(ans)


(n, f) = map(int, input().split())
abc = []
ans = 0
for i in range(n):
	x = list(map(int, input().split()))
	if x[0] >= x[1]:
		ans += x[1]
	else:
		abc.append(x)
abc.sort(key=lambda x: min(x[0], x[1] - x[0]), reverse=True)
for j in range(len(abc)):
	if f > 0:
		ans += min(2 * abc[j][0], abc[j][1])
		f -= 1
	else:
		ans += abc[j][0]
print(ans)


(n, f) = list(map(int, input().split()))
products_and_clients_per_day = []
for i in range(n):
	(k, l) = list(map(int, input().split()))
	normal_sales = min(k, l)
	double_sales = min(2 * k, l)
	sell_out_profit = double_sales - normal_sales
	products_and_clients_per_day.append((k, l, sell_out_profit))
products_and_clients_per_day.sort(key=lambda x: x[2], reverse=True)
res = 0
for i in range(f):
	(k, l, sell_out_profit) = products_and_clients_per_day[i]
	double_sales = min(2 * k, l)
	res += double_sales
for i in range(f, n):
	(k, l, sell_out_profit) = products_and_clients_per_day[i]
	normal_sales = min(k, l)
	res += normal_sales
print(res)


(n, f) = map(int, input().split())
a = []
for _ in range(n):
	(k, l) = map(int, input().split())
	a.append((-(min(l, k * 2) - min(l, k)), min(l, k)))
a.sort()
tot = 0
for i in range(0, f):
	tot += -a[i][0] + a[i][1]
for i in range(f, n):
	tot += a[i][1]
print(tot)


(n, f) = map(int, input().split())
total = 0
days = []
benifit = []
for i in range(n):
	(k, l) = map(int, input().split())
	days.append((k, l))
	benifit.append((min(k * 2, l) - min(k, l), i))
benifit.sort(reverse=True)
for ben in benifit:
	(k, l) = (days[ben[1]][0], days[ben[1]][1])
	if f > 0:
		total += min(k * 2, l)
		f -= 1
	else:
		total += min(k, l)
print(total)


(planDays, chosenDays) = map(int, input().split())
total = []
t = 0
for i in range(planDays):
	(products, clients) = map(int, input().split())
	t += min(products, clients)
	total.append(min(2 * products, clients) - min(products, clients))
total.sort()
print(t + sum(total[planDays - chosenDays:]))


(n, f) = input().strip().split(' ')
(n, f) = (int(n), int(f))
list1 = []
ans = 0
for i in range(n):
	(k, l) = input().strip().split(' ')
	(k, l) = (int(k), int(l))
	ans += min(k, l)
	if k < l:
		list1.append(min(2 * k, l) - k)
list1.sort()
list1.reverse()
if f > len(list1):
	ans1 = sum(list1)
else:
	ans1 = sum(list1[0:f])
print(ans + ans1)


import sys
(n, f) = map(int, sys.stdin.readline().split())
increments = []
without_increments = 0
for __ in range(n):
	(prod, cust) = map(int, sys.stdin.readline().split())
	increments.append(max(0, min(prod * 2, cust) - prod))
	without_increments += min(prod, cust)
print(without_increments + sum(sorted(increments, reverse=True)[:f]))


(n, f) = map(int, input().split())
z = 0
a = []
for i in range(n):
	(k, l) = map(int, input().split())
	z += min(k, l)
	a.append(min(2 * k, l) - min(k, l))
a.sort()
print(z + sum(a[n - f:]))


(n, f) = map(int, input().split())
ml = []
for i in range(n):
	(k, l) = map(int, input().split())
	t = (min(2 * k, l) - min(k, l), k, l)
	ml.append(t)
ml.sort(reverse=True)
r = 0
for i in range(f):
	r += min(2 * ml[i][1], ml[i][2])
for i in range(f, n):
	r += min(ml[i][1], ml[i][2])
print(r)


from collections import namedtuple
(n, f) = (int(i) for i in input().split())
Day = namedtuple('Day', ('cur', 'diff'))
a = []
for i in range(n):
	(k, l) = (int(j) for j in input().split())
	a.append(Day(min(k, l), min(2 * k, l) - min(k, l)))
a.sort(key=lambda day: day.diff, reverse=True)
print(sum((day.diff for day in a[:f])) + sum((day.cur for day in a)))


(n, f) = map(int, input().split())
q = []
a = []
for i in range(n):
	(k, l) = map(int, input().split())
	q.append((k, l))
	u = min(2 * k, l) - min(k, l)
	a.append((u, i))
a.sort(reverse=True)
ans = 0
for i in range(0, f):
	pos = a[i][1]
	ans += min(2 * q[pos][0], q[pos][1])
for i in range(f, n):
	pos = a[i][1]
	ans += min(q[pos][0], q[pos][1])
print(ans)


(n, f) = [int(i) for i in input().split()]
arr = [[0] * 3 for i in range(n)]
for m in arr:
	(m[0], m[1]) = [int(i) for i in input().split()]
	m[2] = min(m[0] * 2, m[1]) - min(m[0], m[1])
arr.sort(key=lambda x: x[2], reverse=True)
res = 0
for i in range(f):
	res += min(arr[i][0] * 2, arr[i][1])
for i in range(f, len(arr)):
	res += min(arr[i][0], arr[i][1])
print(res)


from sys import stdin

def get_ints():
	return list(map(int, stdin.readline().strip().split()))
(n, f) = get_ints()
a = []
for _ in range(n):
	a.append(get_ints())
sum = 0
a = sorted(a, key=lambda day: min(day[1], day[0] * 2) - min(day[1], day[0]), reverse=True)
for i in range(n):
	if f > 0:
		a[i][0] *= 2
		f -= 1
	sum += min(a[i][1], a[i][0])
print(sum)


(n, f) = map(int, input().split())
products = list()
customers = list()
profits = list()
sum = 0
for i in range(n):
	(k, l) = map(int, input().split())
	p = min(2 * k, l) - k
	products.append(k)
	customers.append(l)
	profits.append(p)
sp = sorted(profits)
for i in range(n):
	sum += min(products[i], customers[i])
for i in range(f):
	if sp[-1 - i] > 0:
		sum += sp[-1 - i]
print(sum)


(n, f) = [int(i) for i in input().split()]
a = list(sorted([[int(i) for i in input().split()] for j in range(n)], key=lambda t: min(t[0], t[1] - t[0]), reverse=True))
print(sum([max(0, min(a[i][0] + a[i][0], a[i][1])) if i < f else max(0, min(a[i][0], a[i][1])) for i in range(len(a))]))


a = [0] * 100005
b = [0] * 100005
c = [0] * 100005
(n, f) = map(int, input().split())
for i in range(1, n + 1):
	(a[i], b[i]) = map(int, input().split())
	k = a[i] * 2
	if k >= b[i]:
		c[i] = b[i] - a[i]
	else:
		c[i] = a[i]
	if c[i] < 0:
		c[i] = 0
c[1:] = sorted(c[1:], reverse=True)
tot = 0
for i in range(1, f + 1):
	tot += c[i]
for i in range(1, n + 1):
	if b[i] >= a[i]:
		tot += a[i]
	else:
		tot += b[i]
print(tot)


(n, f) = map(int, input().split())
ans = 0
L = []
for _ in range(n):
	(p, c) = map(int, input().split())
	s = min(p, c)
	sf = min(2 * p, c)
	ans += s
	L.append(sf - s)
L.sort(reverse=True)
ans += sum(filter(lambda x: x > 0, L[:f]))
print(ans)


(n, f) = [int(i) for i in input().split()]
extra = []
ans = 0
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	ans += min(a, b)
	extra_earned = min(2 * a, b) - min(a, b)
	extra.append(extra_earned)
extra.sort(reverse=True)
for i in range(f):
	ans += extra[i]
print(ans)


(n, f) = map(int, input().split())
mass = []
mass2 = []
summa = 0
for i in range(n):
	mass.append(list(map(int, input().split())))
	summa += min(mass[i][0], mass[i][1])
	mass2.append(-1 * min(mass[i][0], mass[i][1]) + min(2 * mass[i][0], mass[i][1]))
mass2.sort()
i = n - 1
while f != 0:
	summa += mass2[i]
	f -= 1
	i -= 1
print(summa)


j = input()
j = list(map(int, j.split(' ')))
n = j[0]
f = j[1]
delta = []
vendidos = 0
for i in range(n):
	j = input()
	j = list(map(int, j.split(' ')))
	pr = j[0]
	cl = j[1]
	if pr > cl:
		vendidos += cl
		delta.append(0)
	else:
		vendidos += pr
		if cl - pr < pr:
			delta.append(cl - pr)
		else:
			delta.append(pr)
delta.sort(reverse=True)
print(vendidos + sum(delta[:f]))


import heapq
(n, f) = map(int, input().split())
q = []
s = 0
for i in range(n):
	(products, clients) = map(int, input().split())
	s += min(products, clients)
	heapq.heappush(q, min(products * 2, clients) - min(products, clients))
largest = heapq.nlargest(f, q)
print(s + sum(largest))


import sys

def get_maximal_products(f, products, clients):
	pairs = sorted(zip(products, clients), key=lambda p: min(p[0] * 2, p[1]) - min(p[0], p[1]), reverse=True)
	new_pairs = [(p[0] * 2, p[1]) for p in pairs[:f]] + pairs[f:]
	return sum((min(p[0], p[1]) for p in new_pairs))
input = sys.stdin.read()
data = list(map(int, input.split()))
products = [x for x in data[2::2]]
clients = [x for x in data[3::2]]
print(get_maximal_products(data[1], products, clients))


from sys import stdin, stdout
import math
(n, k) = map(int, stdin.readline().split())
values = []
ans = 0
for i in range(n):
	(a, b) = map(int, stdin.readline().split())
	ans += min(a, b)
	if a < b:
		b -= a
		values.append(min(a, b))
values.sort()
for v in values[::-1]:
	if not k:
		break
	ans += v
	k -= 1
stdout.write(str(ans))


def calc_income(cnt, customers):
	return min(cnt, max(0, customers - cnt))
(n, f) = map(int, input().split())
days = [tuple(map(int, input().split())) for i in range(n)]
days.sort(key=lambda elem: -calc_income(*elem))
ans = sum(map(lambda elem: min(elem[0], elem[1]), days))
for (cnt, customers) in days[:f]:
	ans += calc_income(cnt, customers)
print(ans)


(n, f) = [int(i) for i in input().split()]
g = 0
kiar = []
for i in range(n):
	(k, l) = [int(g) for g in input().split()]
	g += min(k, l)
	kiar.append(min(2 * k, l) - min(k, l))
kiar.sort(reverse=True)
for i in range(f):
	g += kiar[i]
print(g)


numbers = [int(x) for x in input().split()]
n = numbers[0]
f = numbers[1]
sell = 0
doublesell = []
for i in range(0, n):
	numbers = [int(x) for x in input().split()]
	if numbers[0] == 0 or numbers[1] == 0:
		doublesell.append(0)
	else:
		sold = min(numbers)
		sell += sold
		numbers[1] -= sold
		doublesell.append(min(numbers))
doublesell.sort(reverse=True)
for i in range(0, f):
	sell += doublesell[i]
print(sell)


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	(n, f) = list(map(int, input.readline().split()))
	lis = []
	initsum = 0
	for i in range(n):
		lis.append(list(map(int, input.readline().split())))
		initsum += lis[i][0]
	results = []
	for (x, y) in lis:
		if x >= y:
			results.append([initsum + (y - x), y, y])
		elif x < y and x * 2 >= y:
			results.append([initsum + (y - x), y, x])
		else:
			results.append([initsum + (x * 2 - x), x * 2, x])
	results = sorted(results, reverse=True)
	sum = 0
	for i in range(n):
		if i + 1 > f:
			sum += results[i][2]
		else:
			sum += results[i][1]
	output = sum
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


(n, m) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([min(a * 2, b) - min(a, b), a, b])
l = sorted(l)
k = 0
for i in range(n):
	if i >= abs(m - n):
		k = k + min(l[i][1] * 2, l[i][2])
	else:
		k = k + min(l[i][1], l[i][2])
print(k)


(n, f) = map(int, input().split())
a = [0] * n
b = [0] * n
z = [0] * n
for i in range(n):
	(a1, b1) = map(int, input().split())
	a[i] = a1
	b[i] = b1
for i in range(n):
	z1 = min(2 * a[i], b[i]) - min(a[i], b[i])
	z[i] = z1
z.sort()
c = 0
for i in range(n):
	c += min(a[i], b[i])
for i in range(n - 1, n - f - 1, -1):
	c += z[i]
print(c)


def sold(goods, customers):
	if goods <= customers:
		return goods
	else:
		return customers
(n, sales) = (int(x) for x in input().split())
plan = []
wins = []
for i in range(n):
	(goods, customers) = (int(x) for x in input().split())
	plan.append((goods, customers))
	win = sold(2 * goods, customers) - sold(goods, customers)
	wins.append(win)
wins = sorted(wins, reverse=True)
ans = sum((sold(*x) for x in plan))
ans += sum((wins[i] for i in range(sales)))
print(ans)


(n, f) = map(int, input().split())
days = []
ans = 0
for i in range(n):
	(k, l) = map(int, input().split())
	if k < l:
		new_k = 2 * k
		if new_k >= l:
			days.append(k - l)
		else:
			days.append(-k)
		ans += k
	else:
		ans += l
days = sorted(days)
for i in range(min(f, len(days))):
	ans -= days[i]
print(ans)


(n, f) = list(map(int, input().split(' ')))
res = 0
wow = []
for a0 in range(n):
	(k, l) = list(map(int, input().split(' ')))
	res += min(k, l)
	wow.append(min(2 * k, l) - min(k, l))
wow = sorted(wow)
i = len(wow) - 1
for a0 in range(f):
	res += wow[i]
	i -= 1
print(res)


def mp():
	return map(int, input().split())

def lt():
	return list(map(int, input().split()))

def pt(x):
	print(x)

def ip():
	return input()

def it():
	return int(input())

def sl(x):
	return [t for t in x]

def spl(x):
	return x.split()

def aj(liste, item):
	liste.append(item)

def bin(x):
	return '{0:b}'.format(x)

def listring(l):
	return ' '.join([str(x) for x in l])

def ptlist(l):
	print(' '.join([str(x) for x in l]))
(n, f) = mp()
(k, l) = ([], [])
for _ in range(n):
	(a, b) = mp()
	k.append(a)
	l.append(b)
b = list(range(n))
b.sort(key=lambda x: min(k[x] * 2, l[x]) - min(k[x], l[x]), reverse=True)
for i in range(f):
	k[b[i]] *= 2
pt(sum((min(k[x], l[x]) for x in range(n))))


(n, f) = map(int, input().split())
sold = 0
extra = []
for i in range(n):
	(k, l) = map(int, input().split())
	sold += min(k, l)
	extra.append(min(k, l - min(k, l)))
extra.sort(reverse=True)
sold += sum(extra[:f])
print(sold)


(n, f) = map(int, input().split())
rec = []
diff = []
for i in range(n):
	(k, l) = map(int, input().split())
	rec.append(min(k, l))
	diff.append(min(2 * k, l) - min(k, l))
diff = sorted(diff, reverse=True)
print(sum(rec) + sum(diff[:f]))


(n, k) = map(int, input().split())
info = []
for _ in range(n):
	(item, buyer) = map(int, input().split())
	sale = item * 2
	profit_normal = min(item, buyer)
	profit_sale = min(sale, buyer)
	diff = profit_sale - profit_normal
	info.append([diff, profit_sale, profit_normal, item, buyer])
info.sort(reverse=True)
ans = 0
for (idx, value) in enumerate(info):
	if idx <= k - 1:
		ans += info[idx][1]
	else:
		ans += info[idx][2]
print(ans)


raw = input().split(' ')
days = int(raw[0])
f = int(raw[1])
res = 0
changed = []
for i in range(days):
	data = input().split(' ')
	if int(data[0]) < int(data[1]):
		changed.append(int(data[1]) - int(data[0]) - max(int(data[1]) - int(data[0]) * 2, 0))
		res += int(data[0])
	else:
		res += int(data[1])
list.sort(changed)
for idx in range(len(changed) - 1, -1, -1):
	if f == 0:
		break
	res += changed[idx]
	f -= 1
print(res)


import sys
read = lambda : sys.stdin.readline().rstrip()
readi = lambda : int(sys.stdin.readline())
writeln = lambda x: sys.stdout.write(str(x) + '\n')
write = lambda x: sys.stdout.write(x)

def getM(k, l):
	if k == 0:
		return 0
	if l >= 2 * k:
		return 2 * k
	else:
		return l

def getO(k, l):
	if k == 0:
		return 0
	if l >= k:
		return k
	else:
		return l
(N, F) = list(map(int, read().split()))
days = []
for i in range(N):
	(K, L) = list(map(int, read().split()))
	M = getM(K, L)
	O = getO(K, L)
	days.append((M - O, M, O))
days.sort(key=lambda x: x[0])
sm = 0
for i in range(F):
	(_, m, _) = days[-1 - i]
	sm += m
for i in range(N - F):
	(_, _, o) = days[-1 - F - i]
	sm += o
writeln(sm)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	(n, f) = map(int, input().split())
	arr = [tuple(map(int, input().split())) for _ in range(n)]
	l1 = sorted(arr, key=lambda x: min(x[0], x[1]) - min(x[0] << 1, x[1]))
	ans = 0
	for i in l1:
		if f != 0:
			ans += min(i[0] << 1, i[1])
			f -= 1
		else:
			ans += min(i[0], i[1])
	print(ans)
	return
main()


(n, f) = map(int, input().split())
finallist = []
for _ in range(n):
	(items, client) = map(int, input().split())
	check = min(2 * items, client) - min(items, client)
	finallist.append([check, items, client])
finallist.sort(reverse=True)
ans = 0
value = 0
for i in range(len(finallist)):
	if value < f:
		ans += min(2 * finallist[i][1], finallist[i][2])
		value += 1
	else:
		ans += min(finallist[i][1], finallist[i][2])
print(ans)


(n, f) = map(int, input().split())
sp = 0
p = [None] * n
for i in range(n):
	(k, l) = map(int, input().split())
	pt = min(k, l)
	sp += pt
	p[i] = min(l - pt, max(2 * k - pt, 0))
p.sort(reverse=True)
print(sp + sum(p[:f]))


(n, f) = list(map(int, input().split()))
days = []
for i in range(n):
	d = list(map(int, input().split()))
	days.append((d[0], d[1], min(2 * d[0], d[1]) - min(d[0], d[1])))
days_sorted = sorted(days, key=lambda x: x[2], reverse=True)
ans = 0
for i in range(n):
	if i < f:
		ans += min(2 * days_sorted[i][0], days_sorted[i][1])
	else:
		ans += min(days_sorted[i][0], days_sorted[i][1])
print(ans)


import sys
(n, f) = list(map(int, input().split()))
arr = []
ans = 0
for line in sys.stdin:
	(k, l) = list(map(int, line.split()))
	inc = min(k * 2, l) - k
	ans += min(k, l)
	arr.append(inc)
arr.sort(reverse=True)
for i in range(f):
	if arr[i] > 0:
		ans += arr[i]
print(ans)


(n, k) = map(int, input().split())
s = 0
e = []
for i in range(n):
	(a, b) = map(int, input().split())
	c = min(a, b)
	d = min(2 * a, b)
	s += c
	e.append(d - c)
e.sort()
for i in range(n - 1, n - k - 1, -1):
	s += e[i]
print(s)


from heapq import nlargest
[n, f] = map(int, input().split())
(seq, maxl) = ([], [])
for i in range(n):
	[k, l] = [int(x) for x in input().split()]
	mx = l if 2 * k >= l else 2 * k
	orgl = l if k >= l else k
	maxl.append([mx - orgl, i])
	seq.append([k, l])
(m, sub) = (0, [])
maxl = nlargest(f, maxl)
for item in maxl:
	i = item[1]
	if seq[i][0] >= seq[i][1]:
		m += 0
	elif 2 * seq[i][0] <= seq[i][1]:
		m += seq[i][0]
	else:
		m += seq[i][1] - seq[i][0]
for i in range(n):
	[k, l] = seq[i]
	m += k if l >= k else l
print(m)


(n, f) = map(int, input().split())
(products, limit) = (0, [])
for _ in range(n):
	(k, l) = map(int, input().split())
	products += min(k, l)
	limit.append(min(2 * k, l) - min(k, l))
limit.sort()
products += sum(limit[n - f:])
print(products)


import sys

def solve():
	(n, f) = map(int, input().split())
	k = [0] * n
	l = [0] * n
	d = [0] * n
	for i in range(n):
		(ki, li) = map(int, sys.stdin.readline().split())
		k[i] = ki
		l[i] = li
		if 2 * ki > li:
			d[i] = max(li - ki, 0)
		else:
			d[i] = ki
	d.sort(reverse=True)
	ans = sum(d[:f]) + sum((min(k[i], l[i]) for i in range(n)))
	print(ans)
solve()


(n, f) = map(int, input().split())
a = []
for i in range(n):
	(x, l) = map(int, input().split())
	a.append([abs(min(x, l) - min(x * 2, l)), min(x * 2, l)])
a.sort(reverse=True)
cnt = 0
for i in range(f):
	cnt += a[i][1]
for i in range(f, n):
	cnt += a[i][1] - a[i][0]
print(cnt)


(n, f) = input().strip().split(' ')
n = int(n)
f = int(f)
ar = []
ar1 = []
ar2 = []
count = 0
for i in range(n):
	temp = input().strip().split(' ')
	ar.append(int(temp[0]))
	ar1.append(int(temp[1]))
	ar2.append(min(2 * ar[i], ar1[i]) - min(ar[i], ar1[i]))
	count += min(ar1[i], ar[i])
ar2.sort(reverse=True)
for i in range(f):
	count += ar2[i]
print(count)


(a, b) = map(int, input().split())
x = []
y = []
for i in range(a):
	z = list(map(int, input().split()))
	x.append(z[0])
	y.append(z[1])
z = []
t = []
for i in range(a):
	first = min(y[i], x[i])
	second = min(y[i], 2 * x[i])
	z.append(second - first)
	t.append(first)
z.sort()
print(sum(t) + sum(z[a - b:]))


from sys import stdin, stdout
from collections import defaultdict
import heapq as hq

def get_int():
	return int(stdin.readline().strip())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_string():
	return stdin.readline().strip()
INF = 10000000000.0
(n, f) = get_ints()
k = [0] * n
l = [0] * n
pos = 0
a = []
for i in range(n):
	(k[i], l[i]) = get_ints()
	a.append(list(map(int, (min(2 * k[i], l[i]) - min(k[i], l[i]), i))))
a.sort(reverse=True)
ans = 0
for i in range(n):
	if i < f:
		pos = a[i][1]
		ans += min(2 * k[pos], l[pos])
	else:
		pos = a[i][1]
		ans += min(k[pos], l[pos])
print(ans)


import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def main():
	(n, m) = invr()
	res = 0
	a = []
	for _ in range(n):
		a.append(inlist())
	a = sorted(a, key=lambda d: min(d[0] * 2, d[1]) - min(d[0], d[1]), reverse=True)
	for i in range(n):
		d = a[i]
		if i < m:
			res += min(d[0] * 2, d[1])
		else:
			res += min(d[0], d[1])
	print(res)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


(n, f) = map(int, input().split())
gain = []
sold = 0
for i in range(n):
	(k, l) = map(int, input().split())
	sold += min(l, k)
	if l == 0 or k == 0 or l < k:
		gain.append(0)
	elif 2 * k > l:
		gain.append(l - k)
	else:
		gain.append(k)
gain = sorted(gain, reverse=True)
for i in range(f):
	sold += gain[i]
print(sold)


(n, f) = input().split()
(n, f) = (int(n), int(f))
arr = [input().split() for i in range(n)]
arr = [(int(dayStrings[0]), int(dayStrings[1])) for dayStrings in arr]
arr = sorted(arr, key=lambda x: min(2 * x[0], x[1]) - min(x[0], x[1]), reverse=True)
res = sum((min(2 * arr[ind][0], arr[ind][1]) if ind < f else min(arr[ind][0], arr[ind][1]) for ind in range(n)))
print(res)


(n, f) = map(int, input().split())
if f == 0:
	s = 0
	for _ in range(n):
		(k, l) = map(int, input().split())
		s += min(k, l)
	print(s)
else:
	s = 0
	d = []
	for _ in range(n):
		(k, l) = map(int, input().split())
		s += min(k, l)
		d.append(min(2 * k, l) - min(k, l))
	print(s + sum(sorted(d)[-f:]) if f > 0 else s)


(n, f) = map(int, input().split())
arr1 = []
arr2 = []
arr3 = []
for i in range(n):
	(x, y) = map(int, input().split())
	arr1.append((min(x * 2, y), i))
	arr2.append((min(x, y), i))
	arr3.append((arr1[i][0] - arr2[i][0], i))
arr3.sort(reverse=True)
ans = 0
for i in range(f):
	ans += arr1[arr3[i][1]][0]
for i in range(f, n):
	ans += arr2[arr3[i][1]][0]
print(ans)


def main():
	(n, f) = [int(x) for x in input().split()]
	a = []
	for i in range(n):
		(k, m) = [int(x) for x in input().split()]
		x = 0
		if k >= m:
			x = 0
		elif m < 2 * k:
			x = m - k
		else:
			x = k
		a.append([k, m, x])
	a.sort(key=lambda tup: tup[2], reverse=True)
	for i in range(f):
		a[i][0] += a[i][2]
	for i in range(n):
		a[i][2] = a[i][0] if a[i][0] < a[i][1] else a[i][1]
	print(sum((a[i][2] for i in range(n))))
main()


(d, t) = ([], 0)
(n, f) = map(int, input().split())
for _ in range(n):
	(k, l) = map(int, input().split())
	t += min(k, l)
	d.append(min(k * 2, l) - min(k, l))
d.sort()
t += sum(d[n - f:])
print(t)


(n, f) = map(int, input().split())
a = []
b = []
for i in range(0, n):
	(k, l) = map(int, input().split())
	a.append(min(2 * k, l) - min(k, l))
	b.append(min(k, l))
a.sort(reverse=True)
ans = 0
for i in range(0, f):
	ans += a[i]
for i in b:
	ans += i
print(ans)


(n, s) = map(int, input().split())
nn = n
info = []
result = 0
for i in range(nn):
	(k, l) = map(int, input().split())
	result = result + min(k, l)
	if k >= l:
		info.append(0)
	else:
		info.append(min(2 * k, l) - min(k, l))
info.sort()
info = info[::-1]
i = 0
while i < n and s:
	if info[i] > 0:
		result += info[i]
		s -= 1
	i += 1
print(result)


import sys
(n, f) = list(map(int, sys.stdin.readline().rstrip().split()))
li = []
li2 = []
for i in range(n):
	(k, l) = list(map(int, sys.stdin.readline().rstrip().split()))
	li.append((k, l))
li = sorted(li, key=lambda x: min(x[0] * 2, x[1]) - min(x[0], x[1]))
result = 0
for i in range(n):
	if i >= n - f:
		result += min(li[i][0] * 2, li[i][1])
	else:
		result += min(li[i][0], li[i][1])
sys.stdout.write(str(result))


def inp():
	return map(int, input().split())
(n, f) = inp()
(arr, org, ans) = ([], [], 0)
for i in range(n):
	(k, l) = inp()
	ins = 0
	if k < l:
		if l - k <= k:
			ins = l - k
		else:
			ins = k
	arr.append([i, ins])
	org.append([k, l])
arr.sort(key=lambda x: x[1], reverse=True)
for i in range(n):
	if i < f and arr[i][1]:
		ans += org[arr[i][0]][0] + arr[i][1]
	else:
		ans += min(org[arr[i][0]][0], org[arr[i][0]][1])
print(ans)


from operator import itemgetter
(n, f) = [int(x) for x in input().split()]
arr_of_diffs = []
for i in range(n):
	(k, l) = [int(x) for x in input().split()]
	arr_of_diffs.append([min(l, k), min(l, 2 * k), min(l, 2 * k) - min(l, k)])
arr_of_diffs = sorted(arr_of_diffs, key=itemgetter(2))
out = 0
for i in range(n - 1, -1, -1):
	if f > 0:
		f -= 1
		out += arr_of_diffs[i][1]
	else:
		out += arr_of_diffs[i][0]
print(out)


(n, k) = map(int, input().strip().split())
a = []
sum = 0
for i in range(n):
	(ki, li) = map(int, input().strip().split())
	t = 0
	if li > ki:
		if 2 * ki >= li:
			t = li - ki
		else:
			t = ki
		sum += ki
	else:
		sum += li
	a.append(t)
a.sort(reverse=True)
for i in range(k):
	sum += a[i]
print(sum)


def sell(k, l):
	return l if l <= k else k
(n, f) = map(int, input().split())
kl = [[int(i) for i in input().split()] for _ in range(n)]
totel = 0
d = []
if f == 0:
	for i in kl:
		totel += sell(i[0], i[1])
	print(totel)
else:
	for i in kl:
		gain = sell(i[0] * 2, i[1]) - sell(i[0], i[1])
		totel += sell(i[0], i[1])
		d.append(gain)
	d.sort()
	totel += sum(d[0 - f:])
	print(totel)


(x, y) = list(map(int, input().split()))
n = 0
d = [0] * x
f = []
for i in range(x):
	(a, b) = list(map(int, input().split()))
	if b < a:
		n = n + b
	else:
		n = n + a
	if b > a:
		if a * 2 < b:
			d[i] = a
		else:
			d[i] = b - a
if d != f:
	d.sort()
	for i in range(1, y + 1):
		n = n + d[-i]
print(n)


line = [int(x) for x in input().split()]
n = line[0]
f = line[1]
result = 0
diffs = []
for case in range(n):
	day = [int(x) for x in input().split()]
	k = day[0]
	l = day[1]
	sold = min(k, l)
	result += sold
	diffs.append(min(2 * k, l) - sold)
diffs.sort(reverse=True)
for i in range(f):
	result += diffs[i]
print(result)


a = list(map(int, input().split(' ')))
x = []
y = []
for i in range(a[0]):
	z = list(map(int, input().split(' ')))
	x.append(z[0])
	y.append(z[1])
maxList = []
sumX = 0
for i in range(len(x)):
	if x[i] * 2 > y[i]:
		if y[i] - x[i] >= 0:
			maxList.append(y[i] - x[i])
		else:
			maxList.append(0)
	else:
		maxList.append(x[i])
	if x[i] > y[i]:
		sumX += y[i]
	else:
		sumX += x[i]
maxList = sorted(maxList)[::-1]
for i in range(a[1]):
	sumX += maxList[i]
print(sumX)


def main():
	(n, f) = map(int, input().split())
	sold = 0
	double = []
	m = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		if a < b:
			double.append(min(2 * a, b) - min(a, b))
			sold += a
			m += 1
		else:
			sold += b
	double.sort()
	i = m - 1
	count = 0
	while i >= 0:
		if count < f:
			count += 1
			sold += double[i]
		i -= 1
	print(sold)
main()


(a, b) = map(int, input().split())
s = 0
p = []
for _ in ' ' * a:
	(c, d) = map(int, input().split())
	if c >= d:
		s += d
	elif c and d:
		s += c
		p += [min(d - c, c)]
p = sorted(p, reverse=True)
print(s + sum(p[:b]))


import sys, math, itertools, collections, bisect
input = lambda : sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
mod = 10 ** 9 + 7
mans = inf
ans = 0
count = 0
pro = 1
(n, f) = map(int, input().split())
array = []
for i in range(n):
	(k, l) = map(int, input().split())
	ans += min(k, l)
	array.append(min(l, 2 * k) - min(l, k))
array.sort(reverse=True)
for ai in array:
	if count < f:
		ans += ai
		count += 1
print(ans)


(n, f) = list(map(int, input().split()))
out = []
data = []
ans = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	ans += min(a, b)
	out.append(min(a * 2, b) - min(a, b))
out.sort(reverse=True)
for i in range(f):
	ans += out[i]
print(ans)


(n, k) = map(int, input().split())
l = []
sm = 0
for i in range(n):
	(p, c) = map(int, input().split())
	sm += min(p, c)
	l.append(min(2 * p, c) - min(p, c))
l = sorted(l)
c = k
i = len(l) - 1
while c > 0:
	c -= 1
	sm += l[i]
	i -= 1
print(sm)


import sys
import math
input = sys.stdin.readline

def foo(item):
	return min(item[0] * 2, item[1]) - min(item[0], item[1])
(n, f) = map(int, input().strip().split())
a = []
for _ in range(n):
	(x, y) = map(int, input().strip().split())
	a.append([x, y])
a.sort(key=foo, reverse=True)
ans = 0
for i in range(n):
	if f:
		f -= 1
		ans += min(2 * a[i][0], a[i][1])
	else:
		ans += min(a[i][0], a[i][1])
print(ans)


(days, sale_days) = map(int, input().split())
diff = []
ans = 0
for _ in range(days):
	(products, clients) = map(int, input().split())
	normal_sale = min(products, clients)
	ans += normal_sale
	diff.append(min(2 * products, clients) - normal_sale)
diff.sort(reverse=True)
ans += sum(diff[:sale_days])
print(ans)


(n, k) = map(int, input().split())
a = []
b = []
c = []
d = []
e = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	c.append(min(a[i], b[i]))
	d.append(min(2 * a[i], b[i]))
	e.append(d[i] - c[i])
e.sort(reverse=True)
print(sum(c) + sum(e[0:k]))


(n, f) = [int(i) for i in input().split()]
a = [0] * n
a2 = [0] * n
for i in range(n):
	(k, l) = [int(i) for i in input().split()]
	a[i] = (k, l)
a.sort(key=lambda t: min(t[0], t[1] - t[0]), reverse=True)
ans = 0
for i in range(len(a)):
	t = a[i]
	ans += max(0, min(t[0] + t[0], t[1])) if i < f else max(0, min(t[0], t[1]))
print(ans)


(n, f) = list(map(int, input().split()))
pairs = []
count = 0
for i in range(n):
	(k, l) = list(map(int, input().split()))
	if k >= l:
		count += l
	else:
		pairs.append(min(l - k, k))
		count += k
pairs.sort()
for i in range(min(f, len(pairs))):
	count += pairs.pop()
print(count)


(n, f) = map(int, input().split())
days = []
for i in range(n):
	(prod, peo) = map(int, input().split())
	sold = min(prod, peo)
	max_sold = min(2 * prod, peo)
	gain = max_sold - sold
	days.append([gain, max_sold, sold])
days.sort()
days.reverse()
ans = 0
for i in range(n):
	if f > 0:
		f -= 1
		ans += days[i][1]
	else:
		ans += days[i][2]
print(ans)


(n, f) = map(int, input().split())
count = 0
less = []
for i in range(n):
	(k, l) = map(int, input().split())
	if k >= l:
		count += l
	else:
		less.append([min(2 * k, l) - min(k, l), min(2 * k, l), k])
less.sort(reverse=True)
for i in range(min(f, len(less))):
	count += less[i][1]
i = min(f, len(less))
while i < len(less):
	count += less[i][2]
	i += 1
print(count)


(n, f) = map(int, input().split())
(s, d) = (0, [0] * n)
for i in range(n):
	(k, l) = map(int, input().split())
	s += min(l, k)
	d[i] = min(l, 2 * k) - min(l, k)
print(s + sum(sorted(d)[n - f:]))


(n, m) = map(int, input().split())
l = [list(map(int, input().split())) for i in range(n)]
l = sorted(l, key=lambda x: min(x[0] * 2, x[1]) - min(x[0], x[1]))
k = 0
i = n - 1
for i in range(n - 1, -1, -1):
	if m > 0:
		k += min(l[i][0] * 2, l[i][1])
		m -= 1
	else:
		k += min(l[i][0], l[i][1])
print(k)


from sys import stdin, stdout
(n, f) = stdin.readline().split(' ')
(n, f) = (int(n), int(f))
s = []
d = []
mtn = 0
for _ in range(n):
	(ki, li) = stdin.readline().strip().split(' ')
	(ki, li) = (int(ki), int(li))
	s.append(ki)
	d.append(li)
	mtn += min(ki, li)
tarr = []
for i in range(n):
	tarr.append(min(max(0, d[i] - s[i]), s[i]))
tarr.sort()
tarr = tarr[::-1]
for i in range(f):
	mtn += tarr[i]
stdout.write(str(mtn))


from collections import defaultdict
import bisect
from itertools import accumulate
import os
import sys
import math
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
(n, f) = map(int, input().split())
a = []
ans = 0
for i in range(n):
	(k, l) = map(int, input().split())
	ans += min(k, l)
	a.append(min(2 * k, l) - min(k, l))
a.sort(reverse=True)
for i in range(f):
	ans += a[i]
print(ans)


(n, f) = map(int, input().split())
p = [0] * n
s = 0
for i in range(n):
	(k, l) = map(int, input().split())
	if l >= 2 * k:
		p[i] = k
		s += k
	elif l > k:
		p[i] = l - k
		s += k
	else:
		s += l
s += sum(list(reversed(sorted(p)))[:f])
print(s)


(n, k) = map(int, input().split())
(a, b) = ([], [])
for _ in range(n):
	(products, clients) = map(int, input().split())
	a.append(min(products, clients))
	b.append(min(2 * products, clients) - min(products, clients))
result = sum(a)
b.sort()
result += sum(b[n - k:])
print(result)


(n, f) = map(int, input().split())
w = [[int(x) for x in input().split()] for i in range(n)]
sum1 = 0
final = [0] * n
for j in range(len(w)):
	sum1 = sum1 + min(w[j][0], w[j][1])
	final[j] = min(2 * w[j][0], w[j][1]) - min(w[j][0], w[j][1])
final.sort(reverse=True)
for i in range(f):
	sum1 = sum1 + final[i]
print(sum1)


def items(f, k, l):
	best_days = []
	for i in range(len(k)):
		best_days.append([min(2 * k[i], l[i]) - min(l[i], k[i]), i])
	best_days.sort()
	for i in range(f):
		k[best_days[~i][1]] += k[best_days[~i][1]]
	ans = 0
	for i in range(len(k)):
		ans += min(l[i], k[i])
	return ans
(n, f) = map(int, input().split())
k = []
l = []
for i in range(n):
	(x, z) = map(int, input().split())
	k.append(x)
	l.append(z)
print(items(f, k, l))


(n, f) = map(int, input().split())
(s, p) = (0, [])
for _ in range(n):
	(c, d) = map(int, input().split())
	if c >= d:
		s += d
	elif c and d:
		s += c
		p += [min(d - c, c)]
p = sorted(p, reverse=True)
print(s + sum(p[:f]))


import math
line = lambda : map(int, input().split())

def solve():
	(n, f) = line()
	ans = 0
	ar = []
	for i in range(n):
		(k, l) = line()
		ans += min(k, l)
		ar.append(min(k + k, l) - min(k, l))
	ar.sort(reverse=True)
	for i in range(f):
		ans += ar[i]
	print(ans)

def main():
	solve()
	exit(0)
main()


init = input()
init = [int(x) for x in init.split(' ')]
if init[1] > init[0]:
	exit()
arr = list()
added = list()
for i in range(init[0]):
	inp = input()
	inp_ar = tuple((int(x) for x in inp.split(' ')))
	possible_addition = inp_ar[0] if inp_ar[1] > 2 * inp_ar[0] else inp_ar[1] - inp_ar[0]
	inp_ar = inp_ar + (possible_addition,)
	arr.append(inp_ar)
arr.sort(key=lambda tup: tup[2])
summ = 0
for i in range(init[1]):
	x = arr.pop()
	summ += x[0] + x[2]
while arr:
	x = arr.pop()
	summ += x[0] if x[0] < x[1] else x[1]
print(summ)


arr = []
x = input()
xx = x.split(' ')
n = int(xx[0])
f = int(xx[1])
a = []
s = 0
for i in range(n):
	x = input()
	xx = x.split(' ')
	k = int(xx[0])
	l = int(xx[1])
	s += min(k, l)
	if k >= l:
		a.append(0)
	else:
		a.append(min(2 * k, l) - min(k, l))
a.sort(reverse=True)
i = 0
while f > 0 and i < n:
	if a[i] > 0:
		s += a[i]
		f -= 1
	i += 1
print(s)


(n, f) = (int(i) for i in input().split())
l = []
c = 0
for i in range(n):
	(a, b) = (int(i) for i in input().split())
	l.append(min(2 * a, b) - min(a, b))
	c += min(a, b)
l = sorted(l, reverse=True)
print(c + sum(l[:f]))


(num_days, sell_out) = list(map(int, input().split()))
inp = []
product_sold = 0
for i in range(num_days):
	inp.append(list(map(int, input().split())))

def compare(x):
	return min(2 * x[0], x[1]) - min(x[0], x[1])
for (product, client) in sorted(inp, key=compare, reverse=True):
	if sell_out >= 1:
		product_sold += min(product * 2, client)
		sell_out -= 1
	else:
		product_sold += min(product, client)
print(product_sold)


(n, f) = map(int, input().split())
r1 = []
sm = 0
for i in range(n):
	(x, y) = map(int, input().split())
	if x >= y:
		sm += y
	elif x > 0:
		x2 = 2 * x
		sm += min(x2, y)
		z1 = min(x2, y)
		z2 = min(x, y)
		r1.append(abs(z1 - z2))
r1 = sorted(r1)
f2 = len(r1) - f
for i in range(f2):
	sm -= r1[i]
print(sm)


(n, k) = map(int, input().strip().split())
sum = 0
A = [] * 100000
for _ in range(n):
	(a, b) = map(int, input().strip().split())
	sum = sum + min(a, b)
	temp = max(0, min(b - a, a))
	A.append(temp)
A.sort(reverse=True)
for i in range(k):
	sum = sum + A[i]
print(sum)


def main():
	(n, f) = map(int, input().split())
	store = []
	for i in range(n):
		(k, l) = map(int, input().split())
		store.append([k, l, min(k * 2, l) - min(k, l)])
	store = sorted(store, key=lambda t: t[2], reverse=True)
	sm = 0
	for i in range(n):
		if f > 0:
			sm += min(store[i][0] * 2, store[i][1])
			f -= 1
		else:
			sm += min(store[i][0], store[i][1])
	print(sm)
main()


def main():
	rd = lambda : tuple(map(int, input().split()))
	(n, f) = rd()
	arr = []
	cnt = 0
	for i in range(n):
		(p, c) = rd()
		s = min(p, c)
		sf = min(2 * p, c)
		cnt += s
		arr.append(sf - s)
	arr.sort(reverse=True)
	cnt += sum(filter(lambda x: x > 0, arr[:f]))
	print(cnt)
main()


(n, f) = map(int, input().split())
a = []
d = []
for i in range(n):
	(b, c) = map(int, input().split())
	a.append([b, c])
a.sort(key=lambda x: min(x[0] * 2, x[1]) - min(x[0], x[1]), reverse=True)
for i in range(f):
	a[i][0] = a[i][0] * 2
e = 0
for i in range(n):
	if a[i][0] >= a[i][1]:
		e += a[i][1]
	else:
		e += a[i][0]
print(e)


import sys
(z1, z2) = map(int, input().split())
a = 0
lst = []
for i in range(z1):
	(x1, x2) = map(int, input().split())
	if x1 < x2:
		lst.append(min(x1, x2 - x1))
	a += min(x1, x2)
if len(lst) == 0:
	print(a)
	sys.exit(0)
lst = list(reversed(sorted(lst)))
for j in range(z2):
	a += lst[j]
	if len(lst) - 1 == j:
		break
print(a)


import sys
line = sys.stdin.readline().split()
n = int(line[0])
f = int(line[1])
sobras = []
total = 0
sub_sobras = 0
for j in range(n):
	line = sys.stdin.readline().split()
	k = int(line[0])
	i = int(line[1])
	sub = i - k
	if sub > 0:
		s = 2 * k - i
		if s > 0:
			sobras.append(sub)
			sub_sobras = sub + sub_sobras
		else:
			sobras.append(k)
			sub_sobras = k + sub_sobras
		total = total + k
	else:
		total = total + i
if f < len(sobras):
	sobras.sort(reverse=True)
	for i in range(f):
		total = total + sobras[i]
else:
	total += sub_sobras
print(total)


import sys
input = sys.stdin.readline
(n, f) = map(int, input().split())
surplus = []
sold = []
for i in range(n):
	(p, c) = map(int, input().split())
	sold.append(min(p, c))
	unsold = max(0, c - p)
	more = min(unsold, p)
	surplus.append(more)
surplus.sort(reverse=True)
print(sum(surplus[:f]) + sum(sold))


(n, f) = map(int, input().split())
sold = 0
arr = []
for i in range(n):
	(k, l) = map(int, input().split())
	sold += min(k, l)
	arr.append(max(0, min(2 * k, l) - k))
sold += sum(sorted(arr)[n - f:n])
print(sold)


def minimum(a, b):
	if a < b:
		return a
	else:
		return b
(n, f) = input().split()
n = int(n)
f = int(f)
A = []
ans = 0
for i in range(0, n):
	(x, y) = input().split()
	x = int(x)
	y = int(y)
	A.append([minimum(x, y), minimum(2 * x, y) - minimum(x, y)])
A.sort(key=lambda x: x[1], reverse=True)
for i in range(0, f):
	ans += A[i][1] + A[i][0]
for i in range(f, n):
	ans += A[i][0]
print(ans)


(n, f) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append((a, b))
l.sort(key=lambda x: min(2 * x[0], x[1]) - min(x[0], x[1]), reverse=True)
count = 0
for i in l:
	if f > 0:
		count += min(2 * i[0], i[1])
	else:
		count += min(i[0], i[1])
	f -= 1
print(count)


import sys
input = sys.stdin.readline
read_tuple = lambda _type: map(_type, input().split(' '))

def solve():
	(n, f) = read_tuple(int)
	data = []
	for _ in range(n):
		(k_i, l_i) = read_tuple(int)
		data.append((k_i, l_i, min(2 * k_i, l_i) - min(k_i, l_i)))
	data.sort(key=lambda x: x[2], reverse=True)
	ans = 0
	for (k_i, l_i, _) in data:
		if f:
			k_i *= 2
			f -= 1
		ans += min(k_i, l_i)
	print(ans)
solve()


import sys

def main(mat, k):
	c = sum((min(a, b) for (a, b) in mat))
	p = [(e, min(2 * d[0], d[1]) - min(d[0], d[1])) for (e, d) in enumerate(mat)]
	p.sort(key=lambda x: x[1], reverse=True)
	i = 0
	while i < k:
		c += p[i][1]
		i += 1
	print(c)
mat = []
for (e, line) in enumerate(sys.stdin.readlines()):
	if e == 0:
		(_, k) = map(int, line.strip().split())
		continue
	mat.append(list(map(int, line.strip().split())))
main(mat, k)


(n, f) = [int(x) for x in input().split()]
l = []
k = []
for i in range(n):
	(x, o) = [int(i) for i in input().split()]
	k.append(x)
	l.append(o)
s = 0
z = []
for i in range(n):
	if k[i] >= l[i]:
		z.append(l[i])
	else:
		z.append(k[i])
y = []
for i in range(n):
	if 2 * k[i] >= l[i]:
		y.append(l[i])
	else:
		y.append(2 * k[i])
h = []
for i in range(n):
	h.append(y[i] - z[i])
h.sort(reverse=True)
for i in h:
	if f < 1:
		break
	s += i
	f -= 1
s += sum(z)
print(s)


import sys
import os
import math
(n, f) = map(int, input().split())
A = []
normal_sell_total = 0
for i in range(n):
	(product_num, client_num) = map(int, input().split())
	if product_num >= client_num:
		normal_sell_num = client_num
	else:
		normal_sell_num = product_num
	normal_sell_total += normal_sell_num
	if product_num * 2 >= client_num:
		double_sell_num = client_num
	else:
		double_sell_num = product_num * 2
	diff = double_sell_num - normal_sell_num
	A.append(diff)
A.sort(reverse=True)
increased_sell_num = sum(A[:f])
print(normal_sell_total + increased_sell_num)


(n, f) = map(int, input().split())
total = 0
data = []
ans = []
s = set()
for _ in range(n):
	(k, l) = map(int, input().split())
	if min(2 * k, l) != min(k, l):
		ans.append((_, min(2 * k, l) - min(k, l)))
	data.append((_, k, l))
ans = sorted(ans, key=lambda t: -t[1])
for _ in range(min(len(ans), f)):
	s.add(ans[_][0])
for t in data:
	if t[0] not in s:
		total += min(t[1], t[2])
	else:
		total += min(2 * t[1], t[2])
print(total)


import heapq

def maxSelledProducts(n, arr):
	extra = []
	MSP = 0
	for i in arr:
		if i[0] >= i[1]:
			MSP += min(i[0], i[1])
			continue
		if i[0] * 2 > i[1]:
			p = i[0] - (i[0] * 2 - i[1])
		elif i[0] > i[1]:
			p = 0
		elif i[0] * 2 <= i[1]:
			p = i[0]
		extra.append(p)
		MSP += min(i[0], i[1])
	g = heapq.nlargest(n, extra)
	return MSP + sum(g)
(k, n) = list(map(int, input().split()))
arr = []
for i in range(k):
	(a, b) = list(map(int, input().split()))
	arr.append((a, b))
print(maxSelledProducts(n, arr))


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return len(set(factors))

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def printp(p):
	for i in p:
		print(i)
MOD = 10 ** 9 + 7
T = 1
for _ in range(T):
	(n, f) = list(map(int, stdin.readline().split()))
	a = []
	(ka, la) = ([], [])
	d = {}
	for i in range(n):
		(k, l) = list(map(int, stdin.readline().split()))
		ka.append(k)
		la.append(l)
		a.append([min(2 * k, l) - min(k, l), i])
	a.sort(reverse=True)
	ans = 0
	for i in range(f):
		pos = a[i][1]
		ans += min(2 * ka[pos], la[pos])
	for i in range(f, n):
		pos = a[i][1]
		ans += min(ka[pos], la[pos])
	print(ans)


(n, f) = map(int, input().split())
f = min(f, n)
gain = []
for i in range(n):
	(k, l) = map(int, input().split())
	gain.append([max(min(k, l - k), 0), min(2 * k, l), min(k, l)])
gain.sort(reverse=True)
sum1 = 0
for i in range(f):
	sum1 += gain[i][1]
for i in range(f, n):
	sum1 += gain[i][2]
print(sum1)


import math
import sys
(n, c) = map(int, input().split())
li = []
sum = 0
flag = 0
for _ in range(n):
	(k, l) = map(int, input().split())
	li.append((k, l, min(2 * k, l) - k))
li.sort(key=lambda x: x[2], reverse=True)
for i in range(n):
	(k, l, s) = li[i]
	if c > 0:
		sum += min(2 * k, l)
	else:
		sum += min(k, l)
	c -= 1
print(sum)


(n, f) = map(int, input().split())
ans = 0
tmp = []
for i in range(n):
	(k, l) = map(int, input().split())
	if k >= l:
		ans += l
	else:
		ans += k
		x2 = l - k if 2 * k >= l else k
		tmp.append(x2)
tmp.sort(reverse=True)
ans += sum(tmp[:f])
print(ans)


ls = []
(n, f) = map(int, input().split())
s = 0
for i in range(n):
	(k, l) = map(int, input().split())
	s += min(k, l)
	ls.append(min([k * 2, l]) - min([k, l]))
ls = sorted(ls, reverse=True)
print(s + sum(ls[0:f]))


(n, f) = [int(i) for i in input().split()]
lis = []
for _ in range(n):
	(a, b) = [int(i) for i in input().split()]
	x1 = min(2 * a, b)
	y1 = x1 - min(a, b)
	lis.append([y1, x1, min(a, b)])
lis.sort(reverse=True)
ans = 0
start = 1
for i in range(n):
	if i == f:
		start += 1
	ans += lis[i][start]
print(ans)


(n, m) = map(int, input().split())
l = []
for _ in range(n):
	(x, y) = map(int, input().split())
	l.append((x, y))
dbl = []
ans = 0
for i in range(n):
	ans += min(l[i])
	if l[i][1] > l[i][0]:
		dbl += [i]
xx = sorted([min(l[i][0] * 2, l[i][1]) - min(l[i][0], l[i][1]) for i in dbl], reverse=True)
for i in range(min(m, len(xx))):
	ans += xx[i]
print(ans)


(N, k) = map(int, input().split())
days = []
for i in range(N):
	(p, l) = map(int, input().split())
	days.append([p, l])
dp = []
for i in range(N):
	dp.append(min(2 * days[i][0], days[i][1]) - min(days[i][0], days[i][1]))
dp.sort(reverse=True)
add = 0
add += sum(dp[:k])
for i in range(N):
	add += min(days[i][0], days[i][1])
print(add)


(n, f) = map(int, input().split())
a = []
res = []
for i in range(n):
	(k, l) = map(int, input().split())
	res.append(min(l, k))
	a.append(max(0, min(k, l - k)))
a.sort()
a.reverse()
print(sum(a[:f]) + sum(res))


(n, m) = input().split()
(n, m) = (int(n), int(m))
l = []
c = 0
for i in range(n):
	(x, y) = input().split()
	(x, y) = (int(x), int(y))
	c += min(x, y)
	l.append(min(y - x, x))
l.sort()
for i in range(1, m + 1):
	c += max(l[-i], 0)
print(c)


(n, f) = map(int, input().split())
l = []
temp = []
for i in range(n):
	temp = list(map(int, input().split()))
	l.append([min(2 * temp[0], temp[1]) - min(temp[0], temp[1]), min(temp[0], temp[1])])
l = sorted(l, reverse=True)
Total = 0
for i in range(f):
	Total = Total + l[i][0] + l[i][1]
for i in range(f, n):
	Total += l[i][1]
print(Total)


from sys import stdin, stdout

def sell(k, l):
	return min(k, l)
(n, f) = map(int, stdin.readline().split())
days = []
for _ in range(n):
	(k, l) = map(int, stdin.readline().split())
	days.append((k, l))
days.sort(key=lambda x: sell(2 * x[0], x[1]) - sell(x[0], x[1]), reverse=True)
count = 0
for (k, l) in days:
	if f:
		count += sell(2 * k, l)
		f -= 1
	else:
		count += sell(k, l)
print(count)


(n, f) = map(int, input().split())
total = 0
option = []
for _ in range(n):
	(k, l) = map(int, input().split())
	total += min(k, l)
	option += [min(2 * k, l) - min(k, l)]
total += sum(sorted(option, reverse=True)[:f])
print(total)


def solve(n, f, kls):
	base = sum((min(k, l) for (k, l) in kls))
	plus = sorted([min(2 * k, l) - min(k, l) for (k, l) in kls])[::-1]
	res = base + sum(plus[:f])
	print(res)

def main():
	(n, f) = map(int, input().split())
	kls = []
	for _ in range(n):
		(k, l) = map(int, input().split())
		kls.append((k, l))
	solve(n, f, kls)
main()


def summer_sell_off(n, f, l):
	total = 0
	a = [0] * n
	for i in range(n):
		total += min(l[i][0], l[i][1])
		if l[i][0] < l[i][1]:
			a[i] = min(2 * l[i][0], l[i][1]) - min(l[i][0], l[i][1])
	s = sorted(a, reverse=True)
	j = 0
	while f and j < n:
		if s[j] > 0:
			total += s[j]
			f -= 1
		j += 1
	print(total)
(n, f) = [int(x) for x in input().split()]
l = []
for i in range(n):
	l.append([int(x) for x in input().split()])
summer_sell_off(n, f, l)


def main():
	n = f = 0
	k = l = 0
	list1 = []
	ans = 0
	potential = 0
	(n, f) = map(int, input().split())
	for _ in range(n):
		(k, l) = map(int, input().split())
		ans += min(k, l)
		potential = max(l - k, 0)
		list1.append(min(k, potential))
	list1.sort(reverse=True)
	for x in range(f):
		ans += list1[x]
	print(ans)
	return
main()


inf = list(map(int, input().split()))
(n, f) = (inf[0], inf[1])
arr = [0] * n
res = 0
for i in range(n):
	arr[i] = list(map(int, input().split()))
	res += min(arr[i])

def func(x):
	return min(2 * x[0], x[1]) - min(x)
arr = sorted(arr, key=func, reverse=True)[:f]
for x in arr:
	res += min(2 * x[0], x[1]) - min(x)
print(res)


from sys import stdin
inFile = stdin
tokens = []
tokens_next = 0

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = inFile.readline().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())
n = nextInt()
f = nextInt()
days = [(nextInt(), nextInt()) for _ in range(n)]
days = sorted(days, key=lambda x: min(x[0], x[1] - x[0]))
days = reversed(days)
days = list(days)
days[:f] = [(days[i][0] * 2, days[i][1]) for i in range(f)]
s = sum([min(x[0], x[1]) for x in days])
print(s)


(n, f) = map(int, input().split())
plan = []
for i in range(n):
	(a, b) = map(int, input().split())
	plan.append((a, b))
plan.sort(key=lambda i: min(2 * i[0], i[1]) - min(i[0], i[1]))
total = 0
for i in range(n):
	(a, b) = plan[i]
	if n - i <= f:
		a *= 2
	if b >= a:
		total += a
	else:
		total += b
print(total)


(n, f) = map(int, input().split())
l = sorted((list(map(int, input().split())) for _ in range(n)))[::-1]
lf = []
ans = 0
for i in range(n):
	lf += [min(l[i][0] * 2, l[i][1]) - min(l[i][0], l[i][1])]
	ans += min(l[i][0], l[i][1])
lf = sorted(lf)[::-1]
for i in range(f):
	ans += lf[i]
print(ans)


l = input().strip().split()
(n, f) = (int(l[0]), int(l[1]))
lst = []
base = []
for i in range(n):
	l = input().split()
	(k, l) = (int(l[0]), int(l[1]))
	base.append(min(l, k))
	lst.append(max(0, min(k, l - k)))
lst = sorted(lst, reverse=True)
print(sum(lst[:f]) + sum(base))


def get_line():
	return [int(s) for s in input().strip().split()]
(n, f) = get_line()
kl = []
for _ in range(n):
	kl.append(get_line())

def get_extra(k, l):
	if l <= k:
		return 0
	if l <= 2 * k:
		return l - k
	return k
extras = [get_extra(k, l) for (k, l) in kl]
if f == 0:
	extras_sum = 0
else:
	extras_sum = sum(sorted(extras)[-f:])
print(extras_sum + sum((min(k, l) for (k, l) in kl)))


(n, f) = map(int, input().split())
s = 0
d = [0 for i in range(n)]
for i in range(n):
	(k, l) = map(int, input().split())
	if k < l and k != 0:
		s += k
		d[i] = min(k, l - k)
	elif k >= l:
		s += l
d.sort(reverse=True)
for i in range(f):
	s += d[i]
print(s)


(n, m) = map(int, input().split())
xx = []
yy = []
for i in range(n):
	(x, y) = map(int, input().split())
	xx.append((min(x * 2, y) - min(x, y), i))
	yy.append((min(x * 2, y), min(x, y)))
xx.sort(key=lambda x: x[0])
c = 0
for i in range(len(xx) - 1, -1, -1):
	ind = xx[i][1]
	if m > 0:
		c += yy[ind][0]
	else:
		c += yy[ind][1]
	m -= 1
print(c)


from sys import stdin, stdout
import collections
import itertools
import operator
import functools

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
(n, f) = mi()
l1 = []
l2 = []
ans = 0
for i in range(n):
	(p, c) = mi()
	l1.append([p, c])
	l2.append([min(2 * p, c) - min(p, c), i])
l2.sort(reverse=True)
for i in range(f):
	ind = l2[i][1]
	ans += min(2 * l1[ind][0], l1[ind][1])
for i in range(f, n):
	ind = l2[i][1]
	ans += min(l1[ind][0], l1[ind][1])
print(ans)


(n, f) = map(int, input().split())
not_sure_cases = []
sold = 0
for i in range(n):
	(k, l) = map(int, input().split())
	if k >= l:
		sold += l
	else:
		not_sure_cases.append((k, l, (l if 2 * k >= l else 2 * k) - k))
sorted_cases = sorted(not_sure_cases, key=lambda day: day[2], reverse=True)
for i in range(len(sorted_cases)):
	if i < f:
		(k, l, gain) = sorted_cases[i]
		sold += l if 2 * k >= l else 2 * k
	else:
		sold += sorted_cases[i][0]
print(sold)


(n, f) = map(int, input().split(' '))
(k, l, r) = ([], [], [])
p = n
while p > 0:
	p -= 1
	(x, y) = map(int, input().split(' '))
	k.append(x)
	l.append(y)
	r.append(min(y, x * 2) - min(x, y))
r.sort()
r.reverse()
sm = 0
for i in range(0, n):
	sm += min(k[i], l[i])
for i in range(0, f):
	sm += r[i]
print(sm)


def one_line_input():
	return list(map(int, input().split()))
(n, f) = one_line_input()
final_product = []
count = 0
for i in range(n):
	(k, l) = one_line_input()
	tup = (min(2 * k, l) - min(k, l), k, l)
	final_product.append(tup)
final_product = sorted(final_product)
for i in range(n - 1, -1, -1):
	if f > 0:
		count += min(final_product[i][1] * 2, final_product[i][2])
		f -= 1
		continue
	count += min(final_product[i][1], final_product[i][2])
print(count)


(n, f) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
print(sum((min(k, l) for (k, l) in a)) + sum(sorted((min(2 * k, l) - min(k, l) for (k, l) in a), reverse=True)[:f]))


from sys import stdin, stdout
from os import path
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
(a, b) = map(int, stdin.readline().strip().split())
out = []
w = []
for i in range(a):
	(x, y) = map(int, stdin.readline().strip().split())
	out.append((x, y))
	w.append((-1 * (min(x * 2, y) - min(x, y)), i))
w.sort()
sum = 0
for (key, val) in w:
	if b == 0:
		sum += min(out[val][0], out[val][1])
	else:
		sum += min(2 * out[val][0], out[val][1])
		b -= 1
print(sum)


I = lambda : map(int, input().split())
(n, f) = I()
kl = []
for i in range(n):
	s = list(I())
	kl.append((s[0], s[1], s[0] if s[0] * 2 <= s[1] else s[1] - s[0] if s[1] >= s[0] else 0))
kl.sort(key=lambda x: x[2], reverse=True)
print(sum((x[2] for x in kl[:f])) + sum((min(x[0], x[1]) for x in kl)))


(n, f) = map(int, input().split())
arr = [tuple(map(int, input().split())) for _ in range(n)]
arr2 = [max(min(t[1] - t[0], t[0]), 0) for (i, t) in enumerate(arr)]
arr2.sort()
arr2.reverse()
print(sum(arr2[:f]) + sum([min(t) for t in arr]))


def get_sorted_idx_vec(k, l):
	sorted_idx_vec = []
	for i in range(len(k)):
		sorted_idx_vec.append(min(2 * k[i], l[i]) - min(k[i], l[i]))
	return sorted(range(len(sorted_idx_vec)), key=lambda k: sorted_idx_vec[k], reverse=True)

def main():
	(n, f) = [int(x) for x in input().split()]
	k = []
	l = []
	for i in range(n):
		(_k, _l) = [int(x) for x in input().split()]
		k.append(_k)
		l.append(_l)
	f_i = 0
	sorted_idx = get_sorted_idx_vec(k, l)
	res = 0
	for i in range(len(sorted_idx)):
		idx = sorted_idx[i]
		if f_i < f:
			res += min(2 * k[idx], l[idx])
		else:
			res += min(k[idx], l[idx])
		f_i += 1
	print(res)
main()


(n, k) = input().split(' ')
n = int(n)
k = int(k)
a = []
s = 0
for i in range(n):
	(h, g) = input().split(' ')
	h = int(h)
	g = int(g)
	s = s + min(h, g)
	if h != 0 and g != 0 and (h < g) and (k > 0):
		a.append(min(2 * h, g) - h)
a.sort()
n = len(a)
k = min(n, k)
for i in range(k):
	s = s + a[n - i - 1]
print(s, end='')


(n, f) = map(int, input().split())
s = [[int(j) for j in input().split()] for i in range(n)]
c = 0
a = 0
x2 = []
x1 = list()
for i in range(n):
	x2.append(min(s[i][0] * 2, s[i][1]) - min(s[i][0], s[i][1]))
x2.sort(reverse=True)
for i in range(n):
	c += min(s[i][0], s[i][1])
	if a != f:
		c += x2[i]
		a += 1
print(c)


[n, f] = list(map(int, input().split()))
arr = []
s = 0
for i in range(n):
	arr.append(list(map(int, input().split())))
	s += min(arr[i][0], arr[i][1])
narr = []
for i in arr:
	if i[0] >= i[1]:
		continue
	else:
		narr.append(min(2 * i[0], i[1]) - i[0])
narr.sort(reverse=True)
s = s + sum(narr[0:f])
print(s)


(n, f) = [int(x) for x in input().split()]
a = []
for i in range(n):
	(k, l) = [int(x) for x in input().split()]
	a.append((k, l))
a.sort(reverse=True, key=lambda x: abs(min(x[1], x[0]) - min(x[0] * 2, x[1])))
products = 0
for i in range(f):
	curr = min(a[i][0] * 2, a[i][1])
	products += curr
for i in range(f, n):
	curr = min(a[i][0], a[i][1])
	products += curr
print(products)


(n, f) = map(int, input().split())
z = 0
a = []
for i in range(n):
	(products, clients) = map(int, input().split())
	z += min(products, clients)
	a.append(min(2 * products, clients) - min(products, clients))
a.sort()
print(z + sum(a[n - f:]))


(n, f) = map(int, input().split())
ls = []
sum = 0
for i in range(0, n):
	(x, y) = map(int, input().split())
	sum += min(x, y)
	ls.append(min(2 * x, y) - min(x, y))
ls = sorted(ls)
c = f
i = len(ls) - 1
while c > 0:
	c -= 1
	sum += ls[i]
	i -= 1
print(sum)


(n, f) = map(int, input().split())
prod = 0
lis = []
for i in range(n):
	(k, l) = map(int, input().split())
	prod += min(k, l)
	lis.append(min(2 * k, l) - min(k, l))
lis.sort(reverse=True)
i = 0
while f and i < len(lis):
	prod += lis[i]
	i += 1
	f -= 1
print(prod)


(n, f) = map(int, input().split())
ks = []
ls = []
diff = []
for _ in range(n):
	(k, l) = map(int, input().split())
	ks.append(k)
	ls.append(l)
	diff.append(min(l, 2 * k) - min(l, k))
sorted_diff = [i for (i, _) in sorted(list(enumerate(diff)), reverse=True, key=lambda x: x[1])]
for i in range(f):
	ks[sorted_diff[i]] *= 2
ans = sum((min(i, j) for (i, j) in zip(ks, ls)))
print(ans)


fl = input()
fl = fl.split(' ')
ml1 = []
ml2 = []
n = int(fl[0])
f = int(fl[1])
for i in range(n):
	t = input()
	t = t.split(' ')
	ml1.append(int(t[0]))
	ml2.append(int(t[1]))
nl = []
res = 0
for i in range(n):
	if ml1[i] >= ml2[i]:
		res += ml2[i]
	else:
		res += ml1[i]
	if ml1[i] * 2 > ml2[i]:
		t = ml2[i] - ml1[i]
		if t < 0:
			t = 0
		nl.append(t)
	else:
		nl.append(ml1[i])
nl = sorted(nl, reverse=True)
for i in range(f):
	res += nl[i]
print(res)


(a, b) = map(int, input().split())
c = []
d = 0
for i in range(0, a):
	(e, f) = map(int, input().split())
	d += min(e, f)
	c.append(min(2 * e, f) - min(e, f))
g = sorted(c)
h = b
i = len(g) - 1
while h > 0:
	h -= 1
	d += g[i]
	i -= 1
print(d)


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
(n, f) = rints()
sell = 0
over = []
for _ in range(n):
	(k, l) = rints()
	sell += min(k, l)
	over.append(max(0, min(2 * k, l) - min(k, l)))
over.sort(reverse=True)
sell += sum(over[:f])
print(sell)


(n, f) = map(int, input().split())
lst = [tuple(map(int, input().split())) for _ in range(n)]
lst.sort(key=lambda x: min(x[0] * 2, x[1]) - min(x[0], x[1]), reverse=True)
print(sum((min(x * 2, y) for (x, y) in lst[:f])) + sum((min(x, y) for (x, y) in lst[f:])))


(n, k) = map(int, input().split())
cnt = 0
arr2 = []
arr = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
	cnt += min(arr[i][0], arr[i][1])
	arr2.append(min(arr[i][0] * 2, arr[i][1]) - min(arr[i][0], arr[i][1]))
arr2 = list(reversed(sorted(arr2)))
for i in range(k):
	cnt += arr2[i]
print(cnt)


class P:

	def __init__(self, ki, li):
		self.ki = ki
		self.li = li
		self.val = min(2 * ki, li) - min(ki, li)
(n, f) = [int(i) for i in input().split()]
l = []
for i in range(0, n):
	(x, y) = [int(i) for i in input().split()]
	p = P(x, y)
	l.append(p)
l.sort(key=lambda x: x.val, reverse=True)
for i in range(0, f):
	l[i].ki = l[i].ki * 2
ans = 0
for i in range(0, n):
	ans = ans + min(l[i].ki, l[i].li)
print(ans)


(n, f) = list(map(int, input().split()))
(k, l) = ([0 for i in range(n)], [0 for i in range(n)])
for i in range(n):
	(k[i], l[i]) = list(map(int, input().split()))
p = [min(k[i], l[i]) for i in range(n)]
q = [min(2 * k[i], l[i]) for i in range(n)]
r = [q[i] - p[i] for i in range(n)]
r.sort(reverse=True)
print(sum(p) + sum(r[:f]))


(n, ff) = map(int, input().split())
L = []
L1 = []
for i in range(n):
	t = tuple(map(int, input().split()))
	L.append((min(2 * t[0], t[1]) - min(t[0], t[1]), min(2 * t[0], t[1]), min(t[0], t[1])))
L.sort(key=lambda x: x[0], reverse=True)
print(sum([L[i][1] for i in range(ff)]) + sum([L[i][2] for i in range(ff, n)]))


def sellOff(totalDays, sellOutDays, products, customers):
	quantity = 0
	increases = list()
	for i in range(totalDays):
		q = min(products[i], customers[i])
		quantity += q
		qq = min(products[i] * 2, customers[i])
		increases.append(qq - q)
	increases.sort(reverse=True)
	for i in range(sellOutDays):
		quantity += increases[i]
	return quantity
(n, f) = [int(p) for p in input().split()]
k = list()
l = list()
for i in range(n):
	(ki, li) = [int(p) for p in input().split()]
	k.append(ki)
	l.append(li)
print(sellOff(n, f, k, l))


R = lambda : map(int, input().rstrip().split())
(n, f) = R()
li = []
for _ in range(n):
	li.append(list(R()))
ans = 0
m = []
for l in li:
	(x, y) = l
	if x >= y:
		ans += y
		m.append(0)
	else:
		ans += x
		m.append(min(x, y - x))
m.sort(reverse=True)
for i in range(f):
	ans += m[i]
print(ans)


import os
import sys
from io import BytesIO, IOBase

def main():
	(n, f) = map(int, input().split(' '))
	ans = []
	ho = 0
	for x in range(n):
		(a, b) = map(int, input().split(' '))
		if b <= a:
			ho = ho + b
		else:
			ho = ho + a
			if 2 * a >= b:
				ans.append(b - a)
			else:
				ans.append(a)
	ans.sort(reverse=True)
	print(ho + sum(ans[:f]))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from atexit import register
from io import BytesIO
line1 = input()
line1 = line1.split()
n = int(line1[0])
f = int(line1[1])
clients = []
products = []
sell = []
diff = {}
min1 = 0
gain = 0
for i in range(n):
	line2 = input()
	line2 = line2.split()
	clients.append(int(line2[1]))
	products.append(int(line2[0]))
	gain = min(clients[i], products[i] * 2) - min(clients[i], products[i])
	diff[i] = gain
sort_diff = sorted(diff.items(), key=lambda x: x[1], reverse=True)
sum1 = 0
for (key, value) in sort_diff:
	if f > 0:
		sum1 += min(clients[key], 2 * products[key])
		f -= 1
	else:
		sum1 += min(clients[key], products[key])
print(sum1)


(n, f) = [int(i) for i in input().split()]
l = []
h = []
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l += [abs(min(a, b) - min(a * 2, b))]
	h += [min(a, b)]
l.sort()
l = l[::-1]
ans = 0
for i in range(f):
	ans += l[i]
print(ans + sum(h))


def single_integer():
	return int(input())

def multi_integer():
	return map(int, input().split())

def string():
	return input()

def multi_string():
	return input().split()
(n, f) = multi_integer()
ans = 0
f_list = list()
for i in range(n):
	(k, l) = multi_integer()
	if l <= k:
		ans += l
	else:
		ans += k
		f_list.append(min(k, l - k))
f_list.sort(reverse=True)
i = 0
if f_list:
	while i < f and i < len(f_list):
		ans += f_list[i]
		i += 1
print(ans)


(n, f) = list(map(int, input().split()))
total_products = 0
sale_list = []
for i in range(n):
	(p, c) = list(map(int, input().split()))
	total_products += min(p, c)
	sale_list.append((min(2 * p, c), min(p, c)))
sale_list.sort(key=lambda x: x[0] - x[1])
for i in range(f):
	add = sale_list.pop()
	total_products = total_products + add[0] - add[1]
print(total_products)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
f = defaultdict(int)
(n, f) = M()
v = []
s = 0
for i in range(n):
	(a, b) = M()
	if a < b and 2 * a < b:
		s += a
		v.append(a)
	if a < b and 2 * a >= b:
		s += a
		v.append(b - a)
	elif a >= b:
		s += b
if len(v):
	v.sort(reverse=True)
	for i in range(min(f, len(v))):
		s += v[i]
print(s)