Hocolate Hame

시간 제한2초메모리 제한2048 MB

요약
두 사람이 양 끝에서 번갈아 조각을 먹는다. 처음에는 1개 또는 2개를 먹고, 그다음부터는 직전에 먹은 개수 k 또는 k+1개를 먹는다. 둘 다 자신이 먹은 단맛 총합에서 상대의 총합을 뺀 값을 최대화하도록 최선으로 두며, 최종 차이를 출력한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 게임 이론, 그리디
정답자
아직 제출이 없습니다

문제

Azizkhan and Temirulan love Swiss chocolate. Recently they bought a chocolate bar which is a row of nn pieces. Each piece has a certain amount of sweetness. Moreover, the sweetness of a piece can be negative.

To divide the chocolate bar in a fair way, they devised some rules for eating the pieces:

  • The pieces are numbered 1,2,…,n1, 2, \ldots, n from left to right.
  • Azizkhan will eat the chocolate bar from the left side, and Temirulan from the right side.
  • Azizkhan and Temirulan will eat the chocolate in turns.
  • Each player eats one or more pieces in his turn.
  • Azizkhan eats first, and can eat either 11 or 22 pieces.
  • If kk pieces were eaten on the previous move, then the current player should eat either kk or k+1k + 1 pieces.
  • They stop eating if it is impossible to make the next move.

Azizkhan and Temirulan are both competitive persons. Each of them wants to consume more sweetness than the other. In other words, each player tries to maximize the difference between the total sweetness of the pieces he ate himself and the total sweetness of the pieces eaten by the opponent. Help them to find the difference between the total sweetness consumed by Azizkhan and Temirulan if both players are super-puper-monstro-smart-optimal persons.

입력

The first line contains an integer nn (1≤n≤40001 \le n \le 4000): the number of pieces in the chocolate bar.

The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (−104≤a_i≤104-10^4 \le a\_i \le 10^4): the sweetness values of the pieces.

출력

Output a line with a single integer: the difference between the total sweetness consumed by Azizkhan and the total sweetness consumed by Temirulan (in this order) if both play optimally.

예제2

  1. 예제 1

    입력
    5
    1 2 3 4 5
    
    예상 출력
    -3
    
  2. 예제 2

    입력
    6
    -2 5 100 6 3 4
    
    예상 출력
    90