Advertisement
kadeyrov

Untitled

Nov 9th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  olymp_in
  4. //
  5. //  Created by Kadir Kadyrov on 19.10.2020.
  6. //  Copyright © 2020 Kadir Kadyrov. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <string>
  12. #include <cstdio>
  13. #include <queue>
  14. #include <map>
  15. #include <vector>
  16. #include <stack>
  17.  
  18.  
  19. using namespace std;
  20.  
  21. int main() {
  22.     int n;
  23.     cin >> n;
  24.    
  25.     int a[n];
  26.     for (int i = 0; i < n; i++) {
  27.         cin >> a[i];
  28.     }
  29.    
  30.     int l = 0, r = n - 1;
  31.     int ansS = 0, ansD = 0;
  32.     bool isTurnOfSergey = true;
  33.    
  34.     while (l <= r) {
  35.         if (isTurnOfSergey == true) {
  36.             if (a[l] > a[r]) {
  37.                 ansS += a[l];
  38.                 l++;
  39.             } else {
  40.                 ansS += a[r];
  41.                 r--;
  42.             }
  43.             isTurnOfSergey = false;
  44.         } else {
  45.             if (a[l] > a[r]) {
  46.                 ansD += a[l];
  47.                 l++;
  48.             } else {
  49.                 ansD += a[r];
  50.                 r--;
  51.             }
  52.             isTurnOfSergey = true;
  53.         }
  54.     }
  55.    
  56.     cout << ansS << ' ' << ansD << endl;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement