Maximum Sum (Codeforces 1946B)
Tags: Greedy, dp

You have an array $$$a$$$ of $$$n$$$ integers.You perform exactly $$$k$$$ operations on it. In one operation, you select any contiguous subarray of the array $$$a$$$ (possibly empty) and insert the sum of this subarray anywhere in the array.Your task is to find the maximum possible sum of the array after $$$k$$$ such operations.As this number can be very large, output the answer modulo $$$10^9 + 7$$$.Reminder: the remainder of a number $$$x$$$ modulo $$$p$$$ is the smallest non-negative $$$y$$$ such that there exists an integer $$$q$$$ and $$$x = p \cdot q + y$$$.

Input: Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$ and the number of operations, respectively.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$$$) — the array $$$a$$$ itself.It is guaranteed that the sum of the values of $$$n$$$ and $$$k$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test, output a single integer — the maximum sum of the array that can be obtained after $$$k$$$ operations modulo $$$10^9 + 7$$$.

Note: In the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be $$$(-4) + (-7) + 0 + 0 = -11$$$, modulo $$$10^9 + 7$$$ this is $$$999\,999\,996$$$.In the second test case, it is advantageous to take the sum of the entire array three times and place it anywhere in the array, then one of the possible sequences of actions: [$$$2, 2, 8$$$] $$$\rightarrow$$$ [$$$2, 2, 8, 12$$$] $$$\rightarrow$$$ [$$$2, 2, 8, 12, 24$$$] $$$\rightarrow$$$ [$$$2, 2, 8, 12, 24, 48$$$], the sum of the final array is $$$2 + 2 + 8 + 12 + 24 + 48 = 96$$$.In the fourth test case, it is advantageous to take a subarray of the array consisting of the first three numbers (i.e. consisting of the numbers $$$4, -2$$$ and $$$8$$$) and insert its sum at the beginning of the array, thereby obtaining the array [$$$10, 4, -2, 8, -12, 9$$$], the sum of this array is $$$17$$$.In the seventh test case, it will always be advantageous for us to take an empty subarray of the array. In this case, the sum of the resulting array will not differ from the sum of the original. The answer will be the sum of the original array, taken modulo — $$$42$$$, because $$$(-6 \cdot (10^9 + 7) + 42 = -6\,000\,000\,000)$$$.

Sample Inputs:
12
2 2
-4 -7
3 3
2 2 8
1 7
7
5 1
4 -2 8 -12 9
7 4
8 14 -9 6 0 -1 3
7 100
5 3 -8 12 -5 -9 3
6 1000
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000
2 1
1000000000 8
5 4
0 0 0 0 0
6 10
48973 757292 58277 -38574 27475 999984
7 1
-1000 1000 -1000 1000 -1000 1000 -1000
10 10050
408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045

Sample Outputs:
999999996
96
896
17
351
716455332
42
2
0
897909241
0
416571966

Solution (Python):
import sys
import math
input = sys.stdin.readline
MOD = 10 ** 9 + 7

def main():
    out = []
    for _ in range(int(input())):
        (n, k) = map(int, input().split())
        a = list(map(int, input().split()))
        s = maxSubArraySum(a, len(a)) % MOD
        res = sum(a) % MOD
        tot = (2 ** k - 1) % MOD
        res = res + s * tot
        print(res % MOD)

def maxSubArraySum(a, size):
    max_so_far = 0
    max_ending_here = 0
    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if max_so_far < max_ending_here:
            max_so_far = max_ending_here
        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
mod = int(1000000000.0 + 7)

def maxSubArraySum(a, size):
    max_so_far = -float('inf')
    max_ending_here = 0
    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if max_so_far < max_ending_here:
            max_so_far = max_ending_here
        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far

def comding():
    (n, k) = tup()
    a = ils()
    p = maxSubArraySum(a, n)
    c = sum(a)
    if p < 0:
        print(c % mod)
    else:
        print((c - p + p * 2 ** k) % mod)
    return

def i():
    return input()

def ii():
    return int(input())

def tup():
    return map(int, input().split())

def ls():
    return [x for x in input().split()]

def ils():
    return [int(x) for x in input().split()]

def sils():
    return sorted([int(x) for x in input().split()])
for __ in range(ii()):
    comding()

Solution (Python):
import sys
MOD = 10 ** 9 + 7

def solution(n, k, a):
    max_continue = 0
    max_score = 0
    for i in a:
        if max_continue + i < 0:
            max_continue = 0
        else:
            max_continue += i
            max_score = max(max_score, max_continue)
    result = max_score * (2 ** k - 1) + sum(a)
    result = result % (10 ** 9 + 7)
    return result
t = int(sys.stdin.readline().rstrip())
for i in range(t):
    (n, k) = map(int, sys.stdin.readline().rstrip().split(' '))
    a = list(map(int, sys.stdin.readline().rstrip().split(' ')))
    print(solution(n, k, a))