Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

Choose two NON-OVERLAPPING non-empty contiguous subarrays of [4, -1, 2, 6, -3, 5, -2] (the first must end before the second starts) and maximise the sum of their sums. What is the maximum?

  1. A. 16
  2. B. 13
  3. C. 26
  4. D. 12

Correct answer: A. 16

Prefix DP: best1[i] is the best subarray ending by i, best2[i] the best starting after i, and the answer combines them at each cut. Taking the two best subarrays independently can make them overlap, so the cut is essential.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic