fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.  
  6. // vector<int> v={10,20,10,15,5,30,20};
  7. vector<int> v={17,12,10,2,7,2,11,20};
  8. // int k=3,team_size=2;
  9. int k=4,team_size=3;
  10.  
  11. //logic
  12. priority_queue<int> f_pq;
  13. priority_queue<int> b_pq;
  14. int f=0,b=v.size()-1;
  15.  
  16. //pushing k elements from start in f_pq
  17. for(int i=0;i<k;i++)
  18. {
  19. f_pq.push(v[i]);
  20. f++;
  21. }
  22.  
  23. //pushing k element from end in b_pq
  24. while(k-- and f<=b)
  25. {
  26. b_pq.push(v[b]);
  27. b--;
  28. }
  29. b++; //adjustment factor due to indicies
  30.  
  31.  
  32. int ans=0;
  33. while(team_size--)
  34. {
  35. if(f_pq.top() and b_pq.top()){ //checking max out of b_pq and f_pq top
  36. ans+=max(f_pq.top(),b_pq.top());
  37.  
  38. if(b_pq.top()>f_pq.top())
  39. {
  40. b_pq.pop();
  41.  
  42. if(f<=b)
  43. {
  44. b_pq.push(v[b]);
  45. b--;
  46. }
  47.  
  48. }
  49. else{
  50. f_pq.pop();
  51.  
  52. if(f<=b)
  53. {
  54. f_pq.push(v[f]);
  55. f++;
  56. }
  57.  
  58. }
  59.  
  60. }
  61. else if(f_pq.top() and !b_pq.top())
  62. {
  63. ans+=f_pq.top();
  64.  
  65. f_pq.pop();
  66.  
  67. if(f<=b)
  68. {
  69. f_pq.push(v[f]);
  70. f++;
  71. }
  72.  
  73. }
  74.  
  75.  
  76. else if(!f_pq.top() and b_pq.top())
  77. {
  78. ans+=b_pq.top();
  79. b_pq.pop();
  80.  
  81. if(f<=b)
  82. {
  83. b_pq.push(v[b]);
  84. b--;
  85. }
  86.  
  87. }
  88. }
  89.  
  90.  
  91. cout<<ans<<endl;
  92. }
  93.  
  94.  
  95.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
49