Help me in solving CONCUSSIVE problem

My issue

when i press the button debug my code
and see the ouput that is shown by my code it is right only but it sais the solution failed on the following input ,but it didnt

My code

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


int main() {
	long long int T;
	cin>>T;
	for (long long int i = 0; i < T; i++) {
	    long long int n;
	    cin>>n;
	    vector<long long int> v;
	    for(long long int i=0;i<n;i++){
	        long long int val;
	        cin>>val;
	        v.push_back(val);
	    }
	    sort(v.begin(),v.end());
	    vector<long long int> ans(n);
	    long long int first=0;
	    long long int second=(n+1)/2;
	    long long int check=1;
	    for(long long int i=0;i<n;i++){
	        if(v[first]>=v[second] or (i!=0 and v[first]>=ans[i-1])){
	            cout<< -1;
	            check=0;
	            break;
	        }
	        else{
	            ans[i]=v[first];
	            i++;
	            if(i<n){
	                ans[i]=v[second];
	            }
	            first++;
	            second++;
	        }
	        
	    }
	    if(check){
	        for(long long int i=0;i<n;i++){
	            cout<<ans[i]<<" ";
	        }
	    }
	    cout<<endl;
	}
	return 0;
}

Problem Link: CONCUSSIVE Problem - CodeChef

You failed this test case: n = 7 and the array is: [1,2,3,4,4,4,4]
Your output is -1, which is wrong since a possible rearrangement is: 4 1 4 2 4 3 4