codeforces-scrape

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

2064C.md (2558B)


      1 Source: https://codeforces.com/problemset/problem/2064/C
      2 
      3 C. Remove the Ends
      4 
      5 time limit per test
      6 
      7 3 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 You have an array $a$ of length $n$ consisting of non-zero integers. Initially, you have $0$ coins, and you will do the following until $a$ is empty:
     22 
     23 * Let $m$ be the current size of $a$. Select an integer $i$ where $1 \le i \le m$, gain $|a_i|$$^{\text{∗}}$ coins, and then:
     24   + if $a_i \lt 0$, then replace $a$ with $[a_1,a_2,\ldots,a_{i - 1}]$ (that is, delete the suffix beginning with $a_i$);
     25   + otherwise, replace $a$ with $[a_{i + 1},a_{i + 2},\ldots,a_m]$ (that is, delete the prefix ending with $a_i$).
     26 
     27 Find the maximum number of coins you can have at the end of the process.
     28 
     29 $^{\text{∗}}$Here $|a_i|$ represents the absolute value of $a_i$: it equals $a_i$ when $a_i \gt 0$ and $-a_i$ when $a_i \lt 0$.
     30 
     31 Input
     32 
     33 The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
     34 
     35 The first line of each testcase contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$.
     36 
     37 The second line of each testcase contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$, $a_i \ne 0$).
     38 
     39 The sum of $n$ across all testcases does not exceed $2 \cdot 10^5$.
     40 
     41 Output
     42 
     43 For each test case, output the maximum number of coins you can have at the end of the process.
     44 
     45 Example
     46 
     47 Input
     48 
     49 ```
     50 3
     51 
     52 6
     53 
     54 3 1 4 -1 -5 -9
     55 
     56 6
     57 
     58 -10 -3 -17 1 19 20
     59 
     60 1
     61 
     62 1
     63 ```
     64 
     65 Output
     66 
     67 ```
     68 23
     69 40
     70 1
     71 ```
     72 
     73 Note
     74 
     75 An example of how to get $23$ coins in the first testcase is as follows:
     76 
     77 * $a = [3, 1, 4, -1, -5, \color{red}{-9}] \xrightarrow{i = 6} a = [3, 1, 4, -1, -5] $, and get $9$ coins.
     78 * $a = [\color{red}{3}, 1, 4, -1, -5] \xrightarrow{i = 1} a = [1, 4, -1, -5] $, and get $3$ coins.
     79 * $a = [\color{red}{1}, 4, -1, -5] \xrightarrow{i = 1} a = [4, -1, -5] $, and get $1$ coin.
     80 * $a = [4, -1, \color{red}{-5}] \xrightarrow{i = 3} a = [4, -1] $, and get $5$ coins.
     81 * $a = [4, \color{red}{-1}] \xrightarrow{i = 2} a = [4] $, and get $1$ coin.
     82 * $a = [\color{red}{4}] \xrightarrow{i = 1} a = [] $, and get $4$ coins.
     83 
     84 After all the operations, you have $23$ coins.
     85 
     86 An example of how to get $40$ coins in the second testcase is as follows:
     87 
     88 * $a = [-10, -3, -17, \color{red}{1}, 19, 20] \xrightarrow{i = 4} a = [19, 20] $, and get $1$ coin.
     89 * $a = [\color{red}{19}, 20] \xrightarrow{i = 1} a = [20] $, and get $19$ coins.
     90 * $a = [\color{red}{20}] \xrightarrow{i = 1} a = [] $, and get $20$ coins.
     91 
     92 After all the operations, you have $40$ coins.