-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3out1in.cpp
More file actions
45 lines (39 loc) · 882 Bytes
/
3out1in.cpp
File metadata and controls
45 lines (39 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n,q;
cin>>n>>q;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int que[q];
for(int i=0;i<q;i++) cin>>que[i];
for(int i=0;i<q;i++){
vector<int> v;
for(int j=0;j<que[i];j++){
v.push_back(arr[j]);
}
sort(v.begin(),v.end());
if(v.size()>=3){
long long sum1=0;
int p=v.size()-2;
long long sum2=v[p+1];
int k=0;
while(k<p){
sum1-=v[k];
sum2+=v[p];
p--;
k++;
}
cout<<sum2+sum1<<" ";
}
else cout<<v[0]<<" ";
}
cout<<endl;
}
}