Discounts (Codeforces 1132B)
Tags: Greedy, sortings

You came to a local shop and want to buy some chocolate bars. There are $$$n$$$ bars in the shop, $$$i$$$-th of them costs $$$a_i$$$ coins (and you want to buy all of them).You have $$$m$$$ different coupons that allow you to buy chocolate bars. $$$i$$$-th coupon allows you to buy $$$q_i$$$ chocolate bars while you have to pay only for the $$$q_i - 1$$$ most expensive ones (so, the cheapest bar of those $$$q_i$$$ bars is for free).You can use only one coupon; if you use coupon $$$i$$$, you have to choose $$$q_i$$$ bars and buy them using the coupon, and buy all the remaining $$$n - q_i$$$ bars without any discounts.To decide which coupon to choose, you want to know what will be the minimum total amount of money you have to pay if you use one of the coupons optimally.

Input: The first line contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of chocolate bars in the shop.The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the cost of $$$i$$$-th chocolate bar.The third line contains one integer $$$m$$$ ($$$1 \le m \le n - 1$$$) — the number of coupons you have.The fourth line contains $$$m$$$ integers $$$q_1$$$, $$$q_2$$$, ..., $$$q_m$$$ ($$$2 \le q_i \le n$$$), where $$$q_i$$$ is the number of chocolate bars you have to buy using $$$i$$$-th coupon so that the least expensive of them will be for free. All values of $$$q_i$$$ are pairwise distinct.

Output: Print $$$m$$$ integers, $$$i$$$-th of them should be the minimum amount of money you have to pay if you buy $$$q_i$$$ bars with $$$i$$$-th coupon, and all the remaining bars one by one for their full price.

Note: Consider the first example.If we use the first coupon, we may choose chocolate bars having indices $$$1$$$, $$$6$$$ and $$$7$$$, and we pay $$$18$$$ coins for them and $$$9$$$ coins for all other bars.If we use the second coupon, we may choose chocolate bars having indices $$$1$$$, $$$5$$$, $$$6$$$ and $$$7$$$, and we pay $$$25$$$ coins for them and $$$5$$$ coins for all other bars.

Sample Inputs:
7
7 1 3 1 4 10 8
2
3 4

Sample Outputs:
27
30

Solution (Python):
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
kk = lambda : map(int, input().split())
ll = lambda : list(kk())
(n, cost, m, coupon) = (int(input()), ll(), int(input()), ll())
cost.sort()
s = sum(cost)
ans = [0] * m
for (i, v) in enumerate(coupon):
    ans[i] = s - cost[-v]
print('\n'.join(map(str, ans)))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
_ = int(input())
Choco = list(map(int, input().split()))
S = sum(Choco)
M = int(input())
Coup = list(map(int, input().split()))
Choco = sorted(Choco, reverse=True)
ans = []
for c in Coup:
    ans.append(S - Choco[c - 1])
print('\n'.join(map(str, ans)))

Solution (Python):
R = lambda : map(int, input().split())
n = int(input())
L = sorted(R(), reverse=True)
s = sum(L)
m = int(input())
res = []
for i in list(R()):
    res.append(str(s - L[i - 1]))
print('\n'.join(res))