atcoder#ABC270D. [ABC270D] Stones

[ABC270D] Stones

Score : 400400 points

Problem Statement

Takahashi and Aoki will play a game of taking stones using a sequence (A1,,AK)(A_1, \ldots, A_K).

There is a pile that initially contains NN stones. The two players will alternately perform the following operation, with Takahashi going first.

  • Choose an AiA_i that is at most the current number of stones in the pile. Remove AiA_i stones from the pile.

The game ends when the pile has no stones.

If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove?

Constraints

  • 1N1041 \leq N \leq 10^4
  • 1K1001 \leq K \leq 100
  • 1=A1<A2<<AKN1 = A_1 < A_2 < \ldots < A_K \leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN KK

A1A_1 A2A_2 \ldots AKA_K

Output

Print the answer.

10 2
1 4
5

Below is one possible progression of the game.

  • Takahashi removes 44 stones from the pile.
  • Aoki removes 44 stones from the pile.
  • Takahashi removes 11 stone from the pile.
  • Aoki removes 11 stone from the pile.

In this case, Takahashi removes 55 stones. There is no way for him to remove 66 or more stones, so this is the maximum.

Below is another possible progression of the game where Takahashi removes 55 stones.

  • Takahashi removes 11 stone from the pile.
  • Aoki removes 44 stones from the pile.
  • Takahashi removes 44 stones from the pile.
  • Aoki removes 11 stone from the pile.
11 4
1 2 3 6
8

Below is one possible progression of the game.

  • Takahashi removes 66 stones.
  • Aoki removes 33 stones.
  • Takahashi removes 22 stones.

In this case, Takahashi removes 88 stones. There is no way for him to remove 99 or more stones, so this is the maximum.

10000 10
1 2 4 8 16 32 64 128 256 512
5136