Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <functional>
- #include <bits/stdc++.h>
- using namespace std;
- int min (int a, int b){
- if (a<b)
- return a;
- return b;
- }
- int max (int a, int b){
- if (a<b)
- return b;
- return a;
- }
- int main() {
- int ncases;
- cin>>ncases;
- while(ncases--){
- list<int> l1;
- list<int> l2;
- vector<int>v;
- int sum=0;
- int nelem,aux,n1,n2,n3;
- cin>>nelem;
- for (int i=0;i<nelem;i++){
- cin>>aux;
- l1.push_back(aux);
- }
- l1.sort();
- if (l1.size()==0)
- return 0;
- if (l1.size()==1){
- cout<<l1.front()<<endl;
- cout<<l1.front()<<endl;
- return 0;
- }
- int modo=0;
- while (!l1.empty()){
- if (l1.size()==2){
- n1=l1.front();
- l1.pop_front();
- n2=l1.front();
- l1.pop_front();
- //cout<<"holi "<<n1<<' '<<n2<<endl;
- sum=sum+n2;
- v.push_back(n1);
- v.push_back(n2);
- break;
- }
- if (modo==0){
- n1=l1.front();
- l1.pop_front();
- n2=l1.front();
- l1.pop_front();
- //cout<<"holi "<<n1<<' '<<n2<<endl;
- sum=sum+n2;
- v.push_back(n1);
- v.push_back(n2);
- l2.push_back(n1);
- l2.push_back(n2);
- l2.sort();
- n3=l2.front();
- //cout<<"je "<<n3<<endl;
- sum=sum+n3;
- l2.pop_front();
- v.push_back(n3);
- l1.push_front(n3);
- l1.sort();
- modo=1;
- l2.sort();
- //cout<<"menor de l2: "<<l2.front()<<endl;
- }
- else{
- n1=l1.back();l1.pop_back();
- n2=l1.back();l1.pop_back();
- //cout<<"holi "<<n2<<' '<<n1<<endl;
- //cout<<"je menor de l2: "<<l2.front()<<endl;
- sum=sum+n1;
- v.push_back(n2);
- v.push_back(n1);
- l2.push_back(n1);
- l2.push_back(n2);
- l2.sort();
- n3=l2.front();
- sum=sum+n3;
- //cout<<"je "<<n3<<endl;
- l2.pop_front();
- v.push_back(n3);
- l1.push_front(n3);
- l1.sort();
- modo=0;
- }
- }
- cout<<sum<<endl;
- int i=0;
- while ((i+2)<v.size()){
- cout<<v[i]<<' '<<v[i+1]<<endl;
- cout<<v[i+2]<<endl;
- i+=3;
- }
- cout<<v[i]<<' '<<v[i+1]<<endl;
- if (ncases>1)
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment