Advertisement
Ankit_132

D

Feb 28th, 2024
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int t;
  6.     cin>>t;
  7.    
  8.     while(t--){
  9.         int n, q;
  10.         cin>>n>>q;
  11.        
  12.         vector<int> a(n);
  13.         for(auto &e: a)
  14.             cin>>e;
  15.            
  16.         map<int, int> poss;
  17.         map<int, pair<int, int>> ind;
  18.        
  19.         for(int i=0; i<n; i++)
  20.         {
  21.             for(int j=i+1; j<n; j++)
  22.             {
  23.                 ind[a[i] + a[j]] = {i, j};
  24.                 poss[a[i] + a[j]] = 1;
  25.             }
  26.         }
  27.        
  28.         int tot = accumulate(a.begin(), a.end(), 0);
  29.        
  30.         while(q--)
  31.         {
  32.             int x;
  33.             cin>>x;
  34.            
  35.             if(poss[2*tot-x])
  36.             {
  37.                 cout<<a[ind[2*tot-x].first]<<" ";
  38.                 for(int i=0; i<n; i++)
  39.                 {
  40.                     if(i==ind[2*tot-x].first || i==ind[2*tot-x].second)
  41.                         continue;
  42.                        
  43.                     cout<<a[i]<<" ";
  44.                 }
  45.                
  46.                 cout<<a[ind[2*tot-x].second]<<"\n";
  47.             }
  48.             else
  49.                 cout<<"-1\n";
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement