Help me in solving CURSED problem

My issue

My code is failing task#4. When I debugged my code it showed that my code is failing at input
1
2
6 5
but my output is correct for that input.
0
5 6
what is the issue?

My code

#include <bits/stdc++.h>
using namespace std;
int f(int x,int y){
    return x>=y;
}
int main() {
	int t;
	cin>>t;
	while(t--){
	    int n,sum=0,c=0,s=0;
	    cin>>n;
	    vector<int>a,b;
	   // map<int,int>m;
	    for(int i=0;i<n;i++){
	        int x;
	        cin>>x;
	       // a.push_back(x);
	        b.push_back(x);
	    }
	    sort(b.begin(),b.end());
	   // swap(a[0],*min_element(a.begin(),a.end()));
	    for(int i=0;i<n-1;i++){
	        int l=0,r=n;
	        sum+=b[i];
	        while(r-l>1){
	            int m=(l+r)/2;
	            if(f(sum,b[m])) l=m;
	            else r=m;
	        }
	        if(r==n) break;
	        else swap(b[i+1],b[r]);
	    }
	    for(int i=0;i<n;i++){
	        s+=b[i];
	        a.push_back(s);
	    }
	    for(int i=1;i<n;i++){
	        if(a[i-1]>=b[i]) c++;
	    }
	    cout<<c<<endl;
	    for(auto x:b)cout<<x<<" ";cout<<endl;
	}
	return 0;
}

Problem Link: Cursed Indices Practice Coding Problem - CodeChef

@pankaj8433
debug my code won’t work are problems that have multiple correct answers.
plzz refer my c++ code for debugging

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    long long int n;
	    cin>>n;
	    long long int a[n];
	    vector<long long int> v;
	    map<int,int> mp;
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	    }
	    sort(a,a+n);
	    long long int val=a[0];
	    v.push_back(a[0]);
	    mp[0]=1;
	    while(1)
	    {
	        int u=upper_bound(a,a+n,val)-a;
	        if(u!=n)
	        {
	            v.push_back(a[u]);
	            mp[u]=1;
	            val+=a[u];
	        }
	        else
	        break;
	    }
	    cout<<n-v.size()<<endl;
	    for(int i=0;i<v.size();i++)
	    {
	        cout<<v[i]<<" ";
	    }
	    for(int i=0;i<n;i++)
	    {
	        if(mp[i]==0)
	        cout<<a[i]<<" ";
	    }
	    cout<<endl;
	}
	return 0;
}

Thanx for the soution.
But I have already solved the problem. The problem was that I have sum as int but it will be long long.
I posted this problem because when I submitted this problem it showed that my problem is failing at 1 2 6 5 but my code was giving correct answer at that time as well because there was no role of long long for this test case