Help me in solving TWOCLOSE problem

My issue

This question is giving wrong answer for java , please fix it.

My code

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int t;
  cin >> t;

  while (t--) {
    int n;
    cin >> n;

    vector<int> a(n + 1);
    for (int i = 0; i <= n; i++) cin >> a[i];

    sort(a.begin(), a.begin() + n);
    for (int i = 0; i < n; i++) {
      if (a[i] <= a[n]) continue;
      if (a[i] > 2 * a[n]) break;
      swap(a[i], a[n]);
    }

    int ans = accumulate(a.begin(), a.end(), 0) - a[n];
    cout << ans << "\n";
  }
  
  return 0;
}

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