codeforces-scrape

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

1469B.md (2397B)


      1 Source: https://codeforces.com/problemset/problem/1469/B
      2 
      3 B. Red and Blue
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Monocarp had a sequence $a$ consisting of $n + m$ integers $a_1, a_2, \dots, a_{n + m}$. He painted the elements into two colors, red and blue; $n$ elements were painted red, all other $m$ elements were painted blue.
     22 
     23 After painting the elements, he has written two sequences $r_1, r_2, \dots, r_n$ and $b_1, b_2, \dots, b_m$. The sequence $r$ consisted of all red elements of $a$ in the order they appeared in $a$; similarly, the sequence $b$ consisted of all blue elements of $a$ in the order they appeared in $a$ as well.
     24 
     25 Unfortunately, the original sequence was lost, and Monocarp only has the sequences $r$ and $b$. He wants to restore the original sequence. In case there are multiple ways to restore it, he wants to choose a way to restore that maximizes the value of
     26 
     27 $$f(a) = \max(0, a_1, (a_1 + a_2), (a_1 + a_2 + a_3), \dots, (a_1 + a_2 + a_3 + \dots + a_{n + m}))$$
     28 
     29 Help Monocarp to calculate the maximum possible value of $f(a)$.
     30 
     31 Input
     32 
     33 The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then the test cases follow. Each test case consists of four lines.
     34 
     35 The first line of each test case contains one integer $n$ ($1 \le n \le 100$).
     36 
     37 The second line contains $n$ integers $r_1, r_2, \dots, r_n$ ($-100 \le r_i \le 100$).
     38 
     39 The third line contains one integer $m$ ($1 \le m \le 100$).
     40 
     41 The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($-100 \le b_i \le 100$).
     42 
     43 Output
     44 
     45 For each test case, print one integer — the maximum possible value of $f(a)$.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 4
     53 4
     54 6 -5 7 -3
     55 3
     56 2 3 -4
     57 2
     58 1 1
     59 4
     60 10 -3 2 2
     61 5
     62 -1 -2 -3 -4 -5
     63 5
     64 -1 -2 -3 -4 -5
     65 1
     66 0
     67 1
     68 0
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 13
     75 13
     76 0
     77 0
     78 ```
     79 
     80 Note
     81 
     82 In the explanations for the sample test cases, red elements are marked as bold.
     83 
     84 In the first test case, one of the possible sequences $a$ is $[\mathbf{6}, 2, \mathbf{-5}, 3, \mathbf{7}, \mathbf{-3}, -4]$.
     85 
     86 In the second test case, one of the possible sequences $a$ is $[10, \mathbf{1}, -3, \mathbf{1}, 2, 2]$.
     87 
     88 In the third test case, one of the possible sequences $a$ is $[\mathbf{-1}, -1, -2, -3, \mathbf{-2}, -4, -5, \mathbf{-3}, \mathbf{-4}, \mathbf{-5}]$.
     89 
     90 In the fourth test case, one of the possible sequences $a$ is $[0, \mathbf{0}]$.