Sereja and Dima
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
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.
题意:输入n个不相同的数,A,B轮流取,只能从两边取,而且两个人都取大的数,问最后两人手中数的和分别为多少。
思路:水题,只是想用一下双端队列而已。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<set>
#include<vector>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#define maxn 1005
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define E 1e-8
#define mod 1000000007
#define P pair<int,int>
#define MID(l,r) (l+(r-l)/2)
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
using namespace std;
int main()
{
int n,x;
deque<int> dq;
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; ++i)
{
scanf("%d",&x);
dq.push_back(x);
}
// for(int i=0;i<dq.size();++i) cout<<dq[i]<<endl;
int sum1=0, sum2 = 0;
for(int i=1; i<=n; ++i)
{
int fir = dq.front();
int las = dq.back();
if(i%2==1)
{
if(fir>las)
{
sum1 += fir;
dq.pop_front();
}
else
{
sum1 += las;
dq.pop_back();
}
}
else
{
if(fir>las)
{
sum2 += fir;
dq.pop_front();
}
else
{
sum2 += las;
dq.pop_back();
}
}
}
printf("%d %d\n",sum1,sum2);
}
return 0;
}