Help me in solving OKLAMA problem

My issue

I am out of any idea for this now, help me get rid of the TLE error in the last testcase, I think it’s mainly due to the sort part in opeB function, but there is nothing by which i can replace it, so anyone please help me

My code

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

ll opeB(vector<ll>& a, ll n) {
    vector<ll> arr(a.begin(), a.begin() + n);

    sort(arr.begin(), arr.end());

    if (n == 1) return arr[0];
    if (n == 3) return arr[2] + arr[1] - arr[0];

    ll ans = arr[n-1];
    arr.pop_back();
    
    ans += accumulate(arr.begin() + n / 2, arr.end(), 0LL) - accumulate(arr.begin(), arr.begin() + n / 2, 0LL);

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    ll t;
    cin >> t;
    while (t--) {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n), qur(q);

        for (ll i = 0; i < n; i++) {
            cin >> arr[i];
        }
        for (ll i = 0; i < q; i++) {
            cin >> qur[i];
        }

        for (ll i = 0; i < q; i++) {
            ll ans = opeB(arr, qur[i]);
            cout << ans << " ";
        }
        cout << endl;
    }

    return 0;
}

Problem Link: 3out1in Practice Coding Problem - CodeChef