We are given n days, each day i we have ki products (before any sell-out) and li clients. Each client buys at most one product. So sales on day i are min(available products, li). Available products: if not a sell-out day, it's ki (since products from previous days are discarded). If it is a sell-out day, available products become 2ki (since they double the stock). The unsold products are discarded at end of day. So each day is independent in terms of stock, except the effect of sell-out doubling that day's stock. So we need to choose exactly f days (or up to f? It says "choose any f days" implying exactly f days) to maximize total sum over days of min(available products, li). Since stock is independent per day, the total sales is sum_i of min(modified ki, li) where modified ki = ki if not chosen, and 2ki if chosen.
So it's a straightforward problem: we have baseline sum without any sell-out: baseline = sum_i min(ki, li). If we choose a day for sell-out, its contribution changes from min(ki, li) to min(2ki, li). So the gain for day i if we choose it is: gain_i = min(2ki, li) - min(ki, li). Our goal is to select f days to maximize total gain + baseline, which is equivalent to selecting f days with largest gains (since baseline is fixed). However we must be careful: gains can be negative? Let's check: min(2ki, li) is always >= min(ki, li). So gains are non-negative. So we just pick the f days with the largest gains. But is there any interdependence? No, because each day's contribution does not affect others. So it's just sort by gain descending and pick top f.
But wait, is there any catch? The problem statement says: "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." So indeed, leftover products are discarded, so no carryover. So each day independent.
Thus answer = baseline + sum of top f gains.
Check examples:
Example 1:
n=4, f=2
Days:
1: k=2, l=1 -> min(2,1)=1, min(4,1)=1, gain=0
2: k=3, l=5 -> min(3,5)=3, min(6,5)=5, gain=2
3: k=2, l=3 -> min(2,3)=2, min(4,3)=3, gain=1
4: k=1, l=5 -> min(1,5)=1, min(2,5)=2, gain=1
Baseline = 1+3+2+1=7
Top two gains: 2 and 1 (choose days 2 and 3 or 2 and 4). But note that if we choose day2 and day4, gains: 2+1=3 => total=10. They got 10. Yes.
But also days 2 and 3 yields gains 2+1=3 => total=10. So it's consistent. But what if there is day with large l? Gains are capped by li. But that's fine.
Example 2:
n=4, f=1
Days:
1: k=0,l=2 -> min(0,2)=0, min(0,2)=0, gain=0
2: k=0,l=3 -> gain=0
3: k=3,l=5 -> min(3,5)=3, min(6,5)=5, gain=2
4: k=0,l=6 -> gain=0
Baseline=0+0+3+0=3, top gain=2, total=5. Yes.
So straightforward.
But wait, the constraints: n up to 1e5, ki, li up to 1e9, so use 64-bit integers.
Thus answer is computed by:
baseline = sum_i min(ki, li)
gains = [ min(2*ki, li) - min(ki, li) for each i ]
sort gains descending
add top f gains to baseline.
Edge case: f=0 => baseline only.
Thus solution is simple.
But why would this problem be considered "difficult"? Possibly because there is nuance: The products that are left over are discarded, so we cannot accumulate. Yes independent. So it's easy. But maybe there is a trick: The manager knows exactly that ki products will be put up for sale and li clients. Everyone buys one product or leaves if none left. That's covered.
Alternatively, perhaps they intend that the sell-out days are chosen before the actual sales, and the doubling affects only the stock, not the client number. So it's correct.
But wait, there's a twist: The problem says: "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 ki products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·ki products." So it's exactly doubling the stock.
Thus solution is trivial: sort by gain.
But is it always optimal to choose the days with largest gains? Yes, because the total sum is baseline plus sum of selected gains. Gains are independent and non-negative. So we just pick f largest.
But wait, could there be days where min(ki, li) is already equal to li, so min(2ki, li)=li as well, gain=0? That's fine.
Edge case: f can be up to n, and we can select any f days. So if f > number of days with positive gain? Gains are zero for many days. But we still select f days; gains zero don't hurt.
Thus answer = baseline + sum_{i=1}^{f} sorted_gains_desc[i].
Implementation: read n,f; for i=1..n: read ki, li; compute base_i = min(ki, li); gain_i = min(2*ki, li) - base_i; accumulate baseline; push gain_i into a list. After loop, sort gains descending; sum the first f (if f>0, ensure we don't index out-of-bounds; if f>len(gains), just sum all gains). Output baseline + sum_top.
Time O(n log n). Space O(n).
But maybe there's a more efficient method using selection algorithm but n log n is fine.
Thus final answer.
But wait, maybe there is nuance: The problem says "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 ki products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·ki products." So the wording "were put up for sale would be doubled" means the initial stock is doubled for those days. So yes.
Thus we can produce solution.
But then why is the problem posted? It might be from a contest where many participants overcomplicate or there is a trick about overflow? Use 64-bit.
So answer.
But maybe there is a subtlety: The products that are put up for sale: they are on the shelves. Clients come and buy if available. At the end of day, unsold products are discarded. So stock is exactly the number of products put up for sale that day, not the number sold. So min.
Thus solved.
But maybe there is another nuance: the number of products ki can be zero, li can be zero, etc. Works.
So I'll write solution accordingly.
But I see that the problem statement also says: "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 ki products will be put up for sale and exactly li clients will come to the shop that day." So it's deterministic.
Thus answer.
Given that this is a trivial problem, maybe I'm missing something like: The sell-out days might affect future days because perhaps unsold products from a sell-out day can be sold on next day? But it says "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." So no carryover. So independent.
Thus solution stands.
But why would they present such a simple problem? Perhaps it's from an easy contest or maybe the difficulty is to realize that sorting gains works.
I'll produce final answer in Python.
Edge: Input size up to 1e5, use sys.stdin.
Implementation:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
n = int(next(it))
f = int(next(it))
baseline = 0
gains = []
for i in range(n):
k = int(next(it))
l = int(next(it))
normal = k if k < l else l
doubled = 2k if 2k < l else l
baseline += normal
gain = doubled - normal
gains.append(gain)
gains.sort(reverse=True)
total_gain = sum(gains[:f])
print(baseline + total_gain)
That's it.
But note: Python's ints are arbitrary precision, so fine.
Thus answer.
But maybe there's a nuance: The baseline sum might overflow 32-bit, but we use Python.
Alright.
Thus final output.
But let's double-check example 1:
n=4, f=2
k,l: (2,1): normal=1, doubled=1 -> gain 0.
(3,5): normal=3, doubled=5 -> gain2.
(2,3): normal=2, doubled=3 -> gain1.
(1,5): normal=1, doubled=2 -> gain1.
Baseline=7, top2 gains: 2+1=3, total=10. Yes.
Second example: (0,2): normal=0, doubled=0 gain0; (0,3):0; (3,5):3,5 gain2; (0,6):0; baseline=3, top1 gain=2 =>5.
Thus works.
So final answer.
But wait, is it always optimal to choose days with largest gain? Yes. Because there is no constraint other than count.
Thus solution.
But perhaps the problem is a trick: we might think of choosing days where li > ki, but not exactly; we need to compute gain.
Edge: If ki is huge, like 10^9, li maybe 10^9, then doubled may overflow 32-bit but not 64-bit. Use 64-bit.
Thus final.
Now I'll produce final ChatGPT answer with explanation and code. We need to maximize total sales over n days. Each day i we have ki products and li customers. Sales that day are min(available products, li). If we choose the day for a sell‑out, available products become 2·ki, otherwise ki. Unsold products are discarded daily, so days are independent. Therefore the total sales = Σ min(ki, li) + Σ (min(2ki, li) – min(ki, li)) over the chosen days.
Define baseline = Σ min(ki, li) and gain_i = min(2ki, li) – min(ki, li) (non‑negative). To maximize the sum we simply pick the f days with the largest gains and add their gains to the baseline.
Complexity: O(n log n) time, O(n) memory.
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
it = iter(data)
n = int(next(it))
f = int(next(it))
baseline = 0
gains = []
for _ in range(n):
k = int(next(it))
l = int(next(it))
normal = k if k < l else l
doubled = (2 * k) if (2 * k < l) else l
baseline += normal
gains.append(doubled - normal)
gains.sort(reverse=True)
total_gain = sum(gains[:f])
print(baseline + total_gain)
if __name__ == "__main__":
solve()