Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class C{
- // static PrintWriter out=new PrintWriter(System.out);
- static BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
- static FastReader in=new FastReader();
- static int MAX=(int)1e6+10;
- public static void main(String[] args) throws IOException{
- int T;
- T=in.nextInt();
- // T=1;
- for(int t=1; t<=T; ++t) solve(t);
- out.flush();
- out.close();
- }
- static void decByOne(TreeMap<Integer, Integer> freq, int key){
- freq.put(key, freq.get(key)-1);
- if(freq.get(key)==0) freq.remove(key);
- }
- static void incByOne(TreeMap<Integer, Integer> freq, int key){
- freq.put(key, freq.getOrDefault(key, 0)+1);
- }
- static boolean possible(TreeMap<Integer, Integer> freq, int sum, boolean printSol) throws IOException {
- if(freq.size()==0) return true;
- int last=freq.lastKey();
- decByOne(freq, last);
- int required=sum-last;
- boolean isPossible;
- if(!freq.containsKey(required)){
- isPossible=false;
- }
- else{
- decByOne(freq, required);
- if(printSol) out.write(last+" "+required+"\n");
- isPossible=possible(freq, last, printSol);
- incByOne(freq, required);
- }
- incByOne(freq, last);
- return isPossible;
- }
- static void solve(int testCase) throws IOException{
- // out.print("Case #"+testCase+": ");
- TreeMap<Integer, Integer> freq=new TreeMap<>();
- int n=in.nextInt();
- int[] arr=new int[2*n];
- for(int i=0; i<2*n; ++i){
- arr[i]=in.nextInt();
- freq.put(arr[i], freq.getOrDefault(arr[i], 0)+1);
- }
- Utility.fastSort(arr);
- int last=arr[2*n-1];
- // Utility.debug(last);
- decByOne(freq, last);
- for(int j=2*n-2; j>=0; --j){
- int ele=arr[j];
- decByOne(freq, ele);
- int sum=last+ele;
- // Utility.debug("Sum is", sum);
- // out.println(freq.toString());
- if(possible(freq, last, false)){
- out.write("YES\n");
- out.write(sum+"\n");
- out.write(last+" "+ele+"\n");
- possible(freq, last, true);
- return;
- } else{
- incByOne(freq, ele);
- }
- }
- out.write("NO\n");
- }
- static class Utility{
- static final String sep="#".repeat(10);
- static int gcd(int a, int b){
- if(b==0) return a;
- return gcd(b, a%b);
- }
- static long gcd(long a, long b){
- if(b==0) return a;
- return gcd(b, a%b);
- }
- static void fastSort(int[] arr){
- ArrayList<Integer> a=new ArrayList<>();
- for(int x: arr) a.add(x);
- Collections.sort(a);
- for(int i=0; i<a.size(); ++i) arr[i]=a.get(i);
- }
- static void fastSort(long[] arr){
- ArrayList<Long> a=new ArrayList<>();
- for(long x: arr) a.add(x);
- Collections.sort(a);
- for(int i=0; i<a.size(); ++i) arr[i]=a.get(i);
- }
- // static void debug(int[] arr){
- // out.println(sep);
- // out.println("[DEBUG] " + Arrays.toString(arr));
- // out.println(sep);
- // }
- // static void debug(long[] arr){
- // out.println(sep);
- // out.println("[DEBUG] " + Arrays.toString(arr));
- // out.println(sep);
- // }
- // static void debug(Object... args){
- // out.println(sep); out.print("[DEBUG] ");
- // for(Object o: args) out.print(o + " ");
- // out.println(); out.println(sep);
- // }
- }
- static class FastReader{
- BufferedReader br;
- StringTokenizer st;
- public FastReader(){
- br=new BufferedReader(new InputStreamReader(System.in));
- }
- String next(){
- while(st==null||!st.hasMoreElements()){
- try{
- st=new StringTokenizer(br.readLine());
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- return st.nextToken();
- }
- int nextInt(){
- return Integer.parseInt(next());
- }
- long nextLong(){
- return Long.parseLong(next());
- }
- double nextDouble(){
- return Double.parseDouble(next());
- }
- String nextLine(){
- String str="";
- try{
- str=br.readLine();
- }catch(IOException e){
- e.printStackTrace();
- }
- return str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment