fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n, k;
  7. cin>>n>>k;
  8. set<long int> s1, s2;
  9. for(int i=0;i<n;i++)
  10. {int input;cin>>input;s1.insert(input);}
  11. for(int i=0;i<n;i++)
  12. {int input;cin>>input;s2.insert(input);}
  13. set<long int> os1(s1), os2(s2);
  14. //case1: largest in s2 and smallest in s1
  15. set<long int>::iterator shelf1;set<long int>::iterator shelf2;
  16. for(int c=0;c<k;c++)
  17. {
  18. shelf2=s2.begin();shelf1=s1.end();shelf1--;
  19. long int ex1=*shelf1;long int ex2=*shelf2;
  20. if(ex1<ex2)
  21. {break;}
  22. else
  23. {s2.erase(s2.begin());s1.insert(ex2);s1.erase(shelf1);s2.insert(ex1);}
  24. }
  25. shelf1=s1.end();shelf2=s2.end();shelf2--;shelf1--;
  26. long int ans=(*shelf1) + (*shelf2);
  27. //case2: largest in s1 and smallest in s2
  28. for(int c=0;c<k;c++)
  29. {
  30. shelf1=os1.begin();shelf2=os2.end();shelf2--;
  31. long int ex1=*shelf1;long int ex2=*shelf2;
  32. if(ex1>ex2)
  33. {break;}
  34. else
  35. {os1.erase(s1.begin());os1.insert(ex2);os2.erase(shelf2);os2.insert(ex1);}
  36. }
  37. shelf1=os1.end();shelf2=os2.end();shelf2--;shelf1--;
  38. ans=min(ans, (*shelf1) + (*shelf2));
  39. cout<<ans;
  40. return 0;
  41. }
Runtime error #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Standard output is empty