Statement
You're given an array 𝑎 of non-negative integers, of length 𝑛, and two integers 𝑓,𝑔, such that 𝑓 <𝑔.
You start off with a score of 0 and can repeatedly perform the following operation any number of times:
Choose some subarray 𝑎[𝑙,𝑟] such that (∑𝑙≤𝑖≤𝑟𝑎𝑖)mod 𝑔 =𝑓. Then, add (∑𝑙≤𝑖≤𝑟𝑎𝑖) −𝑓 to your score and delete the subarray 𝑎[𝑙,𝑟] (note that this results in the (potentially non-existent) element to the left of the subarray now becoming adjacent to the (potentially non-existent) element to the right of the subarray).
What's the maximum score you can achieve?
Constraints:
- 𝑛 ≤500
- 0 ≤𝑎𝑖 ≤108
- 1 ≤𝑔 ≤108
- 1 ≤𝑓 <𝑔
Solution
Firstly, it's easy to see that each deleted segment corresponds to a subsequence in the original array. For every deleted subsequence 𝑖1,𝑖2,𝑖3…𝑖𝑚 (indices of the elements in the original array 𝑎), we create a segment [𝑖1,𝑖𝑚], and add it to a set 𝑆. Let 𝐼[𝑙,𝑟] ={𝑖1 =𝑙,𝑖2,…,𝑖𝑚 =𝑟} be the subsequence corresponding to [𝑙,𝑟].
Claim 1: No two segments in 𝑆 can partially intersect, or equivalently, 𝑆 is a laminar family.
Proof
Let's say we have two segments [𝑙1,𝑟1] and [𝑙2,𝑟2] in 𝑆 which partially intersect. Then 𝑙1 <𝑙2 <𝑟1 <𝑟2 holds.
Notice that at all points of time, relative order of these four elements with original indices 𝑙1,𝑟1,𝑙2,𝑟2 will be preserved.
Assume WLOG that we delete 𝐼[𝑙1,𝑟1] first. 𝐼[𝑙2,𝑟2] hasn't been deleted, implying that element with original index 𝑙2 hasn't been deleted and still lies between elements with original index 𝑙1 and 𝑟1. Therefore, if we delete 𝐼[𝑙1,𝑟1] now, it would result in the deletion of 𝑙2, which we earlier stated to definitionally be deleted when we delete 𝐼[𝑙2,𝑟2]. This is a contradiction and no such pair of segments can therefore exist.
Why do we make this reduction? Because doing dp on an optimal set of nested segments is a standard subproblem and easy to reason about.
Define the subtree of [𝑙,𝑟] ∈𝑆 to be 𝑇(𝑙,𝑟) ={[𝑥,𝑦] :([𝑥,𝑦] ∈𝑆) ∧(𝑙 ≤𝑥 ≤𝑦 ≤𝑟) ∧([𝑥,𝑦] ≠[𝑙,𝑟])}. Define the child-set of [𝑙,𝑟] ∈𝑆 to be 𝐶(𝑙,𝑟) ={[𝑥,𝑦] :([𝑥,𝑦] ∈𝑇(𝑙,𝑟)) ∧(∄[𝑎,𝑏] ∈𝑇(𝑙,𝑟) :[𝑥,𝑦] ∈𝑇(𝑎,𝑏))}. These definitions obviously draw from the underlying "tree-structure" of any laminar family of intervals.
Let's say that a segment [𝑙,𝑟] ∈𝑆 is a "root segment" if it isn't in the child-set of any segment in 𝑆. Define 𝑅 to be the set of root segments. The final score must be:
∑[𝑙,𝑟]∈𝑆(∑𝑖∈𝐼[𝑙,𝑟](𝑎𝑖)−𝑓)
=∑[𝑙,𝑟]∈𝑆∑𝑖∈𝐼[𝑙,𝑟](𝑎𝑖)−|𝑆|⋅𝑓
Notice that any 𝑖 ∈𝐼[𝑙,𝑟] where [𝑙,𝑟] ∈𝑆 will lie in exactly one root segment.
⟹∑[𝑙,𝑟]∈𝑆∑𝑖∈𝐼[𝑙,𝑟](𝑎𝑖)−|𝑆|⋅𝑓=∑[𝑙,𝑟]∈𝑅∑𝑙≤𝑖≤𝑟(𝑎𝑖)−|𝑆|⋅𝑓
⟹score=∑[𝑙,𝑟]∈𝑅∑𝑙≤𝑖≤𝑟(𝑎𝑖)−|𝑆|⋅𝑓
Let's analyse the validity condition during deletions. When deleting 𝐼[𝑙,𝑟] (where [𝑙,𝑟] ∈𝑆), we require:
(∑𝑖∈𝐼[𝑙,𝑟]𝑎𝑖)mod𝑔=𝑓(1)
Further, we add ∑𝑖∈𝐼[𝑙,𝑟]𝑎𝑖 −𝑓 to our score.
Now, let's try and understand the relation of the second term in the expression of the final score (|𝑆| ⋅𝑓) with 𝑅. We can rewrite the expression in the following way:
∑[𝑙,𝑟]∈𝑅∑𝑙≤𝑖≤𝑟(𝑎𝑖)−|𝑆|⋅𝑓
=∑[𝑙,𝑟]∈𝑅(∑𝑙≤𝑖≤𝑟(𝑎𝑖)−𝑓−|𝑇(𝑙,𝑟)|⋅𝑓)
Consider a root segment [𝑙,𝑟]. We have:
(∑𝑖∈𝐼[𝑙,𝑟]𝑎𝑖)mod𝑔=𝑓(from (1))
⟹(∑𝑙≤𝑖≤𝑟𝑎𝑖−∑(𝑙≤𝑖≤𝑟)∧(𝑖∉𝐼)𝑎𝑖)mod𝑔=𝑓
⟹(∑𝑙≤𝑖≤𝑟𝑎𝑖−∑[𝑥,𝑦]∈𝑇(𝑙,𝑟)∑𝑖∈𝐼[𝑥,𝑦]𝑎𝑖)mod𝑔=𝑓
⟹(∑𝑙≤𝑖≤𝑟𝑎𝑖−∑[𝑥,𝑦]∈𝑇(𝑙,𝑟)𝑓)mod𝑔=𝑓(from (1))
⟹(∑𝑙≤𝑖≤𝑟𝑎𝑖−|𝑇(𝑙,𝑟)|⋅𝑓)mod𝑔=𝑓
⟹|𝑇(𝑙,𝑟)|⋅𝑓mod𝑔=(∑𝑙≤𝑖≤𝑟𝑎𝑖−𝑓)mod𝑔(2)
We now observe that the segments in 𝑇(𝑙,𝑟) for root-segment [𝑙,𝑟] (which are obviously disjoint for different root segments) affect us in only two ways:
- They have a direct contribution of −|𝑇(𝑙,𝑟)| ⋅𝑓 to the final score.
- |𝑇(𝑙,𝑟)| must satisfy (2).
It's therefore optimal to choose 𝑇(𝑙,𝑟) such that it:
- Is internally valid.
- Satisfies (2).
- Has the smallest possible size.
Also note that we only care about the number of segments in 𝑇(𝑙,𝑟), and not about their actual structure.
Let's define 𝑝(𝑙,𝑟,𝑥) to be true if it's possible to delete 𝑥 subsequences only from the subarray 𝑎[𝑙,𝑟] in a valid manner w.r.t. 𝑓 and 𝑔.
Claim 2: If 𝑝(𝑙,𝑟,𝑥) is true for 𝑥 >1, then 𝑝(𝑙,𝑟,𝑥 −1) must be true.
Proof
Let the set of segments corresponding to the 𝑥 subsequences here be 𝑆. Then there must exist at least one root segment [𝑙,𝑟] ∈𝑆, and 𝑆 ∖{[𝑙,𝑟]} is also valid, because [𝑙,𝑟] ∉𝑇(𝑥,𝑦)∀[𝑥,𝑦] ∈𝑆.
This motivates us to try and find the greatest number of valid subsequences we can delete from subarray [𝑙,𝑟], dp(𝑙,𝑟) for all subarrays, because upon doing that, one can find the answer in the following manner:
compute dp[][] #for convenience, ensure that dp[l][r] = 0 if l > r
initialize dp2[n + 1] with 0s #dp2[i] = maximum score we can get from prefix i
for r from 1 to n:
dp2[r] = dp2[r - 1]
for l from 1 to r:
sum = sum of subarray a[l, r]
sub = dp[l + 1][r - 1]
y = smallest x such that (x * f) % g = (sum - f) % g
if y exists and y <= sub:
dp2[r] = max(dp2[r], dp2[l - 1] + sum - (y + 1) * f)
ans = dp2[n]
Ok, so how do we compute dp? Well, it's rather simple:
initialize dp[n + 1][n + 1] with 0s
for s from 1 to n:
for r from s to n:
l = r - s + 1
sum = sum of subarray a[l, r]
#we do not delete the segment [l, r]
for m from l to r - 1:
sub = dp[l][m] + dp[m + 1][r]
dp[l][r] = max(dp[l][r], sub)
#we try to delete the segment [l, r] with an optimal choice of T(l, r)
sub = dp[l + 1][r - 1]
y = the greatest x <= sub such that x and sum satisfy (2) from earlier
if such y exists:
dp[l][r] = max(dp[l][r], y + 1)
Now, the only subproblem left unsolved is finding the greatest solution <= a threshold to (2) in our innermost loop. More formally, solving the following suffices:
Given 𝑎,𝑠,𝑓,𝑔, find the greatest 𝑥 ≤𝑎 such that 𝑥 ⋅𝑓mod𝑔 =(𝑠 −𝑓)mod𝑔.
Ignoring the ≤𝑎 constraint, it's easy to see that some solution exists iff gcd(𝑓,𝑔)|(𝑠 −𝑓). Let 𝑑 =gcd(𝑓,𝑔), 𝐺 =𝑔/𝑑,𝐹 =𝑓/𝑑,𝐴 =(𝑠 −𝑓)/𝑑, then the solution set is defined by the arithmetic progression (((𝐴/𝐹)mod𝐺) +𝑐 ⋅𝐺). Finding the greatest term less than a given threshold for an arithmetic progression is trivial, and we have therefore solved this subproblem.
Our solution is finally complete and takes 𝑂(𝑛3) time.
Note: the official problem also allows 𝑓 =0, but I've omitted its consideration here since it's tedious and trivially follows from the analysis for 𝑓 >0.