PROBLEM 39: Sereja and Dima
Problem Reference: Codeforces
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.
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.
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.
Soutions:
- n = int(input())
- a = [int(a) for a in input().split()]
- s = []
- d = []
- i = 0
- j = len(a) - 1
- t = 0
- while i <= j :
- if t == 0:
- if a[i] > a[j]:
- s.append(a[i])
- i = i + 1
- else:
- s.append(a[j])
- j = j - 1
- t = 1
- else:
- if a[i] > a[j]:
- d.append(a[i])
- i = i + 1
- else:
- d.append(a[j])
- j = j - 1
- t = 0
- print(sum(s),sum(d))
4
4 1 2 10
12 5
7
1 2 3 4 5 6 7
16 12
No comments:
Post a Comment