codeforces-scrape

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

1644C.md (2109B)


      1 Source: https://codeforces.com/problemset/problem/1644/C
      2 
      3 C. Increase Subarray Sums
      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 You are given an array $a_1, a_2, \dots, a_n$, consisting of $n$ integers. You are also given an integer value $x$.
     22 
     23 Let $f(k)$ be the maximum sum of a contiguous subarray of $a$ after applying the following operation: add $x$ to the elements on exactly $k$ distinct positions. An empty subarray should also be considered, it has sum $0$.
     24 
     25 Note that the subarray doesn't have to include all of the increased elements.
     26 
     27 Calculate the maximum value of $f(k)$ for all $k$ from $0$ to $n$ independently.
     28 
     29 Input
     30 
     31 The first line contains a single integer $t$ ($1 \le t \le 5000$) — the number of testcases.
     32 
     33 The first line of the testcase contains two integers $n$ and $x$ ($1 \le n \le 5000$; $0 \le x \le 10^5$) — the number of elements in the array and the value to add.
     34 
     35 The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$).
     36 
     37 The sum of $n$ over all testcases doesn't exceed $5000$.
     38 
     39 Output
     40 
     41 For each testcase, print $n + 1$ integers — the maximum value of $f(k)$ for all $k$ from $0$ to $n$ independently.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 3
     49 
     50 4 2
     51 
     52 4 1 3 2
     53 
     54 3 5
     55 
     56 -2 -7 -1
     57 
     58 10 2
     59 
     60 -6 -1 -2 4 -6 -1 -4 4 -5 -4
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 10 12 14 16 18
     67 0 4 4 5
     68 4 6 6 7 7 7 7 8 8 8 8
     69 ```
     70 
     71 Note
     72 
     73 In the first testcase, it doesn't matter which elements you add $x$ to. The subarray with the maximum sum will always be the entire array. If you increase $k$ elements by $x$, $k \cdot x$ will be added to the sum.
     74 
     75 In the second testcase:
     76 
     77 * For $k = 0$, the empty subarray is the best option.
     78 * For $k = 1$, it's optimal to increase the element at position $3$. The best sum becomes $-1 + 5 = 4$ for a subarray $[3, 3]$.
     79 * For $k = 2$, it's optimal to increase the element at position $3$ and any other element. The best sum is still $4$ for a subarray $[3, 3]$.
     80 * For $k = 3$, you have to increase all elements. The best sum becomes $(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$ for a subarray $[1, 3]$.