fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. vector<int> qs9_ms(vector<int> arr){
  5. int n = arr.size();
  6. int i = 0, j = n - 1;
  7. int p1 = 0, p2 = 0;
  8. bool rev = false;
  9. int turn = 1;
  10.  
  11. while(i < j){
  12. int picked;
  13. if(!rev){
  14. picked = arr[i++];
  15. }
  16. else{
  17. picked = arr[j--];
  18. }
  19.  
  20. if(turn % 2 == 1)
  21. p1 += picked;
  22. else
  23. p2 += picked;
  24.  
  25. if(picked % 2 == 0)
  26. rev = !rev;
  27.  
  28. turn++;
  29. }
  30. return {p1, p2};
  31. }
  32. int main(){
  33. int n;
  34. vector<int>arr(n);
  35. cin>>n;
  36. for(int i=0;i<n;i++)
  37. { cin>>arr[i];
  38.  
  39. } vector<int>ans;
  40. ans=qs9_ms(arr);
  41. cout<<"Score of Player P1: "<<ans[0]<<" and Score of Player 2: "<<ans[1]<<endl;
  42.  
  43. }
  44.  
Success #stdin #stdout 0.01s 5312KB
stdin
5
3 6 2 3 5
stdout
Score of Player P1: 8 and Score of Player 2: 11