The problem states that we have to find the min no of students to remove so that the ith student can pass the exam. So I am basically adding the students in a multiset as it stores sorted values and while the sorted sum is greater than required marks we subtract it out and move to the next one.
The problem comes with the input:
3 4 3 9 1 1 9 8 9
with m : required marks to pass being 14
Here at the 6th index of input which is 9 which has not been added to the multiset is being deleted somehow.
The output that i am getiing when running the troubled input:
0 0 0 ;4--;3-- 2 ;9-- 1 ;9-- 1 ;9--;4--;9-- 3 ;9--;9--;9-- 3 ;9--;9--;9--;9-- 4
The values in :""-- contain the *x which being subtracted from sum there is an extra 9 but i don't know how?
multiset<int> st;
int setsum =0;
for(int i=0;i<n;i++)
{
int sum = setsum+ar[i];
if((sum)<=m)
{
cout<<"0 ";
}
else
{
//cout<<sum<<"-*";
int cnt = 0;
auto x = st.rbegin();
while(sum>m)
{
sum -= *x;
//cout<<";"<<*x<<"--";
x--;
//if(i==3)
//cout<<*x<<"++";
cnt++;
}
cout<<" "<<cnt<<" ";
}
st.emplace(ar[i]);
setsum += ar[i];
}