My Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin>>N;
int Ai[N], Bi[N];
int sumofA=0;
int sumofboth=1;
for(int i=0;i<N;i++)
{
cin>>Ai[i]>>Bi[i];
int x; //cities he gives speeches in
//N-x cities he does not give any speeches
//sum of Ai[_] in (N-x) cities should be lesser than sum of Ai[_] and Bi[_] in x cities.
// x is least => Ai+ Bi should be maximum in the cities he should give speech
int sum[N];
sum[i]=Ai[i]+Bi[i];
sort(sum,sum+N);
sort(Ai,Ai+N);
reverse(Ai,Ai+N);
while(sumofboth>sumofA)
{
sumofA=sumofA+Ai[i];
sumofboth=sumofboth+sum[i];
i++;
}
x=i;
cout<<x;
}
}
The above is giving me the wrong answer. I suspect that something might be wrong with my declaring “sumofboth=1” but I couldn’t think of any other way for going forward with this code. (the while loop wouldn’t work otherwise) Could someone please give any suggestions?