#include <iostream>
#include<string>
#include<cmath>
#include<bits/stdc++.h>
#define ll long long
#define PI 3.14
#define pb push_back
#define pob pop_back
#define sv(a) sort(a.begin(),a.end())//sorting for vector//
#define sa(a,n) sort(a,a+n)//sorting for array//
#define MOD 1000000007
#define fi first
#define se second
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int c,x,sum;
sum=0;
vector<int>v1;
cin>>c;
while(c--)
{
cin>>x;
v1.pb(x);
}
sort(v1.begin(),v1.end());
while(!v1.empty())
{
int d=v1.size();
sum=sum+v1.at(d-2)+v1.at(d-1);
v1.pob();
v1.pob();
v1.pob();
v1.pob();
}
cout<<sum<<endl;
}
return 0;
}
Here you have to consider the case when d=1(v1.size()==1) then you can’t get element at (d-2)th position and another case that you have to look is when you popback, size of vector should be greater than 0.
1 Like