#QHD0000016. Trò chơi lấy hai đầu (Deque)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trò chơi lấy hai đầu (Deque)
Deque
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Two players play a game with an ordered array of integers .
The first player moves first. On each turn, a player removes either the leftmost or the rightmost remaining element, adds its value to their own score, and leaves the other elements in their original order. The game ends when the array is empty.
Both players play optimally to maximize their score advantage. Compute the final score of the first player minus the score of the second player.
Input
The first line contains the integer , the number of elements.
The second line contains integers from left to right.
Output
Print the optimal score difference: first player’s score minus second player’s score. It may be negative.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
4
10 80 90 30
Output
10
Explanation
The first player takes from the right. From , the opponent takes from the right; the first player then takes and the opponent gets . The score difference is , the maximum guaranteed difference against optimal play.