Official
C - Reindeer and Sleigh 2 Editorial by en_translator
Let \(S\) be the set of deer that ride on the sled. Then the condition is
\[\displaystyle \sum_{i\notin S}P_i\geq \sum_{i \in S}W_i.\]
Plugging \(\displaystyle \sum_{i\notin S}P_i=\sum_{i=1}^N P_i - \sum_{i \in S}P_i\), we obtain
\[\displaystyle \sum_{i\in S}(W_i+P_i)\leq \sum_{i=1}^N P_i.\]
Since the right hand side is a constant, it is optimal to put deer in ascending order of \(W_i+P_i\) greedily.
The computational complexity is \(O(N\log N)\), where the bottleneck is sorting by \(W_i+P_i\).
def solve():
n = int(input())
wp = [list(map(int, input().split())) for i in range(n)]
wp.sort(key=lambda x: x[0] + x[1]) # Sort by w_i + p_i
sum_p = 0
for w, p in wp:
sum_p += p
res = 0
for i in range(n):
w, p = wp[i]
res += w + p
if sum_p < res:
print(i)
return
for _ in range(int(input())):
solve()
posted:
last update: