codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1882C.md (3187B)


      1 Source: https://codeforces.com/problemset/problem/1882/C
      2 
      3 C. Card Game
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 There are $n$ cards stacked in a deck. Initially, $a_{i}$ is written on the $i$-th card from the top. The value written on a card does not change.
     22 
     23 You will play a game. Initially your score is $0$. In each turn, you can do one of the following operations:
     24 
     25 * Choose an odd$^{\dagger}$ positive integer $i$, which is not greater than the number of cards left in the deck. Remove the $i$-th card from the top of the deck and add the number written on the card to your score. The remaining cards will be reindexed starting from the top.
     26 * Choose an even$^{\ddagger}$ positive integer $i$, which is not greater than the number of cards left in the deck. Remove the $i$-th card from the top of the deck. The remaining cards will be reindexed starting from the top.
     27 * End the game. You can end the game whenever you want, you do not have to remove all cards from the initial deck.
     28 
     29 What is the maximum score you can get when the game ends?
     30 
     31 $^{\dagger}$ An integer $i$ is odd, if there exists an integer $k$ such that $i = 2k + 1$.
     32 
     33 $^{\ddagger}$ An integer $i$ is even, if there exists an integer $k$ such that $i = 2k$.
     34 
     35 Input
     36 
     37 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). The description of the test cases follows.
     38 
     39 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{5}$).
     40 
     41 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^{9} \le a_i \le 10^{9}$).
     42 
     43 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^{5}$.
     44 
     45 Output
     46 
     47 For each test case, print a single integer — the maximum score you can get when the game ends.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 4
     55 
     56 4
     57 
     58 -4 1 -3 5
     59 
     60 4
     61 
     62 1 -2 3 -4
     63 
     64 3
     65 
     66 -1 3 -5
     67 
     68 1
     69 
     70 -1
     71 ```
     72 
     73 Output
     74 
     75 ```
     76 5
     77 4
     78 2
     79 0
     80 ```
     81 
     82 Note
     83 
     84 In the first test case, one can get the score of $5$ as follows:
     85 
     86 1. In the first turn, choose $i=2$. Your score remains $0$ and the numbers written on the cards from the top will become $[-4, -3, 5]$.
     87 2. In the second turn, choose $i=3$. Your score will become $5$ and the numbers written on the cards from the top will become $[-4, -3]$.
     88 3. In the third turn, end the game with the score of $5$.
     89 
     90 In the second test case, one can get the score of $4$ as follows:
     91 
     92 1. In the first turn, choose $i=3$. Your score will become $3$ and the numbers written on the cards from the top will become $[1, -2, -4]$.
     93 2. In the second turn, choose $i=1$. Your score will become $4$ and the numbers written on the cards from the top will become $[-2, -4]$.
     94 3. In the third turn, end the game with the score of $4$.
     95 
     96 In the third test case, one can get the score of $2$ as follows:
     97 
     98 1. In the first turn, choose $i=1$. Your score will become $-1$ and the numbers written on the cards from the top will become $[3, -5]$.
     99 2. In the second turn, choose $i=1$. Your score will become $2$ and the numbers written on the cards from the top will become $[-5]$.
    100 3. In the third turn, end the game with the score of $2$.