-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01 Subsequence.cpp
More file actions
54 lines (47 loc) · 921 Bytes
/
01 Subsequence.cpp
File metadata and controls
54 lines (47 loc) · 921 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
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve(){
ll n;
cin>>n;
ll a[n];
vector<ll> zero,one;
ll sum =0 ;
for(int i=0;i<n;i++){
cin>>a[i];
if(i%2==0)
zero.push_back(a[i]);
else
one.push_back(a[i]),sum+=a[i];
}
sort(one.begin(),one.end());
sort(zero.begin(),zero.end(),greater<>());
int z=0,o=0;
for(int i=0;i<n;i++){
if(i%2==0)
a[i]=zero[z++];
else
a[i]=one[o++];
}
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"\n";
ll ans=0;
for(int i=0;i<n;i++){
if(i%2==0)
ans += a[i]*sum;
else
sum-=a[i];
}
cout<<ans<<"\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}