A. Sereja and Dima

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 inteinputers. 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
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.

My Answer Code:

/*
Author:Albert Tesla Wizard
Time:2021/2/17 22:27
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,cnt=0;
    cin>>n;
    vector<int>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    int sum1=0,sum2=0;
    int left=0,right=n-1;
    while(n--)
    {
        if(cnt%2==0)
        {
            if(a[right]>a[left]){sum1+=a[right];right--;}
            else {sum1+=a[left];left++;}
        }
        else if(cnt%2==1)
        {
            if(a[left]>a[right]){sum2+=a[left];left++;}
            else {sum2+=a[right];right--;}
        }
        cnt++;
    }
    cout<<sum1<<" "<<sum2<<'\n';
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值