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

#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll mod=1e9+7;
ll a[300010];
ll c[300010];

ll tc,i,j,k,n,m;
string s,s1,s2;

map<ll,ll> mp;

int main(){
    IOS

    cin>>tc;
    while(tc--){
            mp.clear();
            cin>>n;
            ll s=0;
            for(i=0;i<n;i++){
                cin>>a[i];
                s+=a[i];
                mp[a[i]]++;
            }

            ll avg=ceil(s/(double)n);

            ll all=n;
            ll days=n;
            bool ok=1;

            ll indx=0;

            while( days-- ){
                    ll ch;
                    for( auto x:mp){
                            ll ff=x.first;
                            ll ss=x.second;

                            if(ff<avg){
                                s-=(ff*ss);
                                all-=ss;
                                mp[ff]=0;
                            }
                    }
                    avg=ceil(s/(double)all);

            }

            for(i=0;i<n;i++)
                if(mp[a[i]])
                cout<<a[i]<<' ';

            cout<<endl;
    }

}
Copy
To-do List AshcoOoOdel
GNU G++17
3070 ms
3.0 MB
Time Limit Exceeded