Infosys | Power programmer role | Onsite

You are given arrays A and B of N integers. Alexa decided to choose a subsequence of indices i₁ < i₂ < ,..., < i such that

  • A[i₁] <= A[i₂] <= ... <= A[iₖ]
  • The sum (A[i₁] + B[i₁]) + (A[i₂] - B[i₂]) + ... + (A[iₖ] + (-1)ᵏ⁻¹B[iₖ]) is maximum possible

Find Maximum possible sum that Alexa can get. Answer it in modulo 10⁹ + 7

Example

N - 6
A - 4 6 4 1 3 3
B - 5 7 6 0 3 2
OUTPUT : 13

Choose index 2

Example

6
5 2 7 2 5 3
-6 7 -3 7 5 6
OUTPUT : 19

Choose index 2, 3
Result will be (A[2] + B[2]) + (A[3] - B[3])

Comments (2)