RubrikOA #12
Starlight
Estimated MediumJudge-ready
Topics (AI-labelled)DP Basics
Given an integer array and a multiplier z, you may at most once choose a contiguous segment and multiply every number in it by z; then find the maximum contiguous subarray sum of the transformed array. Skipping the amplification and taking the empty subarray are both allowed, so the answer is at least 0. The output of fixed-source example 2 is corrected from 30 to 63.
Input
The first line contains n z (1≤n≤200000, -100≤z≤100); the second line contains n integers (-10^9≤a[i]≤10^9).
Output
Output the maximum possible contiguous subarray sum.
Example 1
Input:
2 2
3 2
Output:
10
Example 2
Input:
5 -3
-5 -9 -2 1 -6
Output:
63
Example 3
Input:
4 -2
1 2 4 5
Output:
12
Example notes
Use three states to record the maximum sum of the scored segment before, inside, and after the amplified segment. In source example 2, multiplying the whole array by -3 gives 63, so the original answer 30 is wrong.
Editorial
Loading solution…