Shovels Shop (Codeforces 1154F)
Tags: Greedy, sortings

There are $$$n$$$ shovels in the nearby shop. The $$$i$$$-th shovel costs $$$a_i$$$ bourles.Misha has to buy exactly $$$k$$$ shovels. Each shovel can be bought no more than once.Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset.There are also $$$m$$$ special offers in the shop. The $$$j$$$-th of them is given as a pair $$$(x_j, y_j)$$$, and it means that if Misha buys exactly $$$x_j$$$ shovels during one purchase then $$$y_j$$$ most cheapest of them are for free (i.e. he will not pay for $$$y_j$$$ most cheapest shovels during the current purchase).Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers).Your task is to calculate the minimum cost of buying $$$k$$$ shovels, if Misha buys them optimally.

Input: The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$1 \le n, m \le 2 \cdot 10^5, 1 \le k \le min(n, 2000)$$$) — the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly.The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the cost of the $$$i$$$-th shovel.The next $$$m$$$ lines contain special offers. The $$$j$$$-th of them is given as a pair of integers $$$(x_i, y_i)$$$ ($$$1 \le y_i \le x_i \le n$$$) and means that if Misha buys exactly $$$x_i$$$ shovels during some purchase, then he can take $$$y_i$$$ most cheapest of them for free.

Output: Print one integer — the minimum cost of buying $$$k$$$ shovels if Misha buys them optimally.

Note: In the first example Misha can buy shovels on positions $$$1$$$ and $$$4$$$ (both with costs $$$2$$$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $$$3$$$ and $$$6$$$ (with costs $$$4$$$ and $$$3$$$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $$$7$$$ with cost $$$1$$$. So the total cost is $$$4 + 2 + 1 = 7$$$.In the second example Misha can buy shovels on positions $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$8$$$ (costs are $$$6$$$, $$$8$$$, $$$5$$$, $$$1$$$ and $$$2$$$) and get three cheapest (with costs $$$5$$$, $$$1$$$ and $$$2$$$) for free. And then he can buy shovels on positions $$$6$$$, $$$7$$$ and $$$9$$$ (all with costs $$$1$$$) without using any special offers. So the total cost is $$$6 + 8 + 1 + 1 + 1 = 17$$$.In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $$$17$$$.

Sample Inputs:
7 4 5
2 5 4 2 6 3 1
2 1
6 5
2 1
3 1

9 4 8
6 8 5 1 8 1 1 2 1
9 2
8 4
5 3
9 7

5 1 4
2 5 7 4 6
5 4

Sample Outputs:
7

17

17

Solution (Python):
from collections import defaultdict

def input2int():
    return map(int, input().split())
(n, m, k) = input2int()
cost = list(input2int())
cost = sorted(cost)[:k]
preSum = defaultdict(int)
for i in range(k):
    preSum[i] = preSum[i - 1] + cost[i]
preSum[k] = preSum[k - 1]
offer = []
for i in range(m):
    (x, y) = input2int()
    if x <= k:
        offer.append((x, y))
dp = defaultdict(lambda : int(1000000000.0))
dp[0] = 0
for i in range(k + 1):
    if i < k:
        dp[i + 1] = min(dp[i] + cost[i], dp[i + 1])
    for _ in offer:
        (x, y) = _
        if i + x > k:
            continue
        dp[i + x] = min(dp[i + x], dp[i] + preSum[i + x - 1] - preSum[i + y - 1])
print(dp[k])

Solution (Python):
IN = input
rint = lambda : int(IN())
rmint = lambda : map(int, IN().split())
rlist = lambda : list(rmint())
(n, m, k) = rmint()
a = rlist()
t = []
for i in range(m):
    (x, y) = rmint()
    t.append((x, y))
t.sort()
a.sort()
pr = [0]
for i in a:
    pr.append(pr[-1] + i)
f = 4096 * [0]

def s(l, r):
    if l > r:
        return 0
    else:
        return pr[r + 1] - pr[l]
ans = s(0, k - 1)

def upd(x, y):
    global ans
    f[x] = min(f[x], y)
    ans = min(ans, y + s(x, k - 1))
for i in range(1, k + 1):
    f[i] = f[i - 1] + a[i - 1]
    for p in t:
        (x, y) = p
        if i - x < 0:
            break
        upd(i, f[i - x] + s(i - x + y, i - 1))
print(ans)

Solution (Python):
from audioop import reverse
(n, m, k) = map(int, input().split())
price = list(map(int, input().split()))
offer = [0] * (k + 1)
for i in range(m):
    (x, y) = map(int, input().split())
    if x <= k:
        offer[x] = max(offer[x], y)
price.sort()
for _ in range(n - k):
    price.pop()
price.sort(reverse=True)
prep = [0] * (k + 1)
for i in range(k):
    prep[i + 1] = prep[i] + price[i]
min_price = [prep[k]] * (k + 1)
min_price[0] = 0
for i in range(k):
    min_price[i + 1] = min(min_price[i + 1], min_price[i] + price[i])
    for j in range(1, k + 1):
        if offer[j] == 0:
            continue
        if i + j > k:
            break
        min_price[i + j] = min(min_price[i + j], min_price[i] + prep[i + j - offer[j]] - prep[i])
print(min_price[k])