• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #223 Div2 A

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

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.

Sample test(s)
Input
4 
4 1 2 10
Output
12 5
Input
7 
1 2 3 4 5 6 7
Output
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.

 1 #pragma comment(linker,"/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cmath>
 5 #include <stack>
 6 #include <queue>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 #define maxn 1000005
12 int n, m;
13 int b, w, x, c, y;
14 //c--;
15 //b-=x;b>=x;
16 //a--;b=w-(x-b);b<x;
17 int a[maxn];
18 int main(){
19     int t = 0;
20     scanf("%d", &n);
21     for (int i = 0; i < n; i++){
22         scanf("%d", &a[i]);
23     }
24     int l = 0, r = n - 1;
25     int flag = 1;
26     x = y = 0;
27     while (l <= r){
28         if (a[l]>a[r])t = a[l++];
29         else t = a[r--];
30         if (flag > 0)x += t;
31         else y += t;
32         flag = -flag;
33     }
34     printf("%d %d\n", x, y);
35     return 0;
36 }
View Code
posted @ 2014-01-19 15:47  HaibaraAi  阅读(197)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3