D - Choose Me Editorial by CoderAnshu


Let \(\Delta = Voters\,for\,Takahashi - Voters\,for\,Aoki\). Intially , if we are not giving any speech , then \(\Delta = \sum_{i=1}^{n} \left(-A_i \right)\) which is a constant value for one configuration. Now we have to make \(\Delta \gt 0\), by giving minimum number of speeches.

Let \(Benefit_i = benefit\,of\,giving\,speech\,in\,i'th\,town\) .

When we give speech in \(i'th\) town, we get \(A_i + B_i\) voters and remove \(A_i\) voters for Aoki.

\(\therefore Benefit_i = 2*A_i + B_i\), now we will sort all towns according to their \(Benefit\) values in decreasing order and greedily start picking the largest ones until our \(\Delta\) becomes \(\gt 0\) and in the end \(\Delta\) will become \(\gt 0\) for sure. (For example , If we give speech in all towns, then \(\Delta\) is obviously positive.) The total time complexity is \(O(N*logN)\).

Sample Code (C++)

posted:
last update: