Algorithm


A. Sereja and Dima
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.

Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.

Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.

Output

On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.

Examples
input
Copy
4
4 1 2 10
output
Copy
12 5
input
Copy
7
1 2 3 4 5 6 7
output
Copy
16 12
Note

In the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin>>n;
  list <int> listn;
  int x;
  for(int i = 0; i < n; i++){
    cin>>x;
    listn.push_back(x);
  }
  int sereja = 0, dima = 0;
  bool turn = true;
  while(n--){
    turn = !turn;
    if(turn){
      if(listn.front() >= listn.back()){
        dima += listn.front();
        listn.pop_front();
        continue;
      }
      dima += listn.back();
      listn.pop_back();
      continue;
    }
    if(listn.front() >= listn.back()){
      sereja += listn.front();
      listn.pop_front();
      continue;
    }
    sereja += listn.back();
    listn.pop_back();
    continue;
  }
  cout<<sereja<<" "<<dima<<endl;
  return 0;

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
4 1 2 10

Output

x
+
cmd
12 5
Advertisements

Demonstration


Codeforcess Solution 381-A A. Sereja and Dima ,C++, Java, Js and Python ,381-A,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+