Help me in solving TWOCLOSE problem

My issue

first i sorted the array then compared all the values two test cases are right but two of them are wrong

My code

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

int main() {

	int t;
	cin>>t;
	while(t--){
	    int arr[50];
	   
	    int n ;
	    cin>>n; 
	    int sum =0;
	    for(int i =0 ; i<=n ; i++){
	      cin>>arr[i];
	      
	    }
	    for(int i =0 ; i<n+1 ; i++){
	        
	      for(int j =i+1 ; j<n +1; j++){
	          if(arr[i]>arr[j]){
	        swap( arr[i] , arr[j]);
	          }
	          
	      }
	        
	          
	      }
	        
	      
	     int i = 0;
                while (i < n) {
                        if (arr[i] <= arr[n] * 2 && arr[i] > arr[n]) {
                                swap(arr[i], arr[n]); 
                              
                        }
                        i++;
                }
	   
	    
	    for(int i =0 ; i<n ; i++){
	   
	    sum = sum +arr[i];
	   
	      
	    }
	    cout<<sum<<endl;
	    
	    
	    
	}

}


Problem Link: Not-too-far Replacement Practice Coding Problem