A particle contributes in total. With , only strengths satisfying need checking. A particle has peak , so also require .
Fix and scan from left to right. After subtracting contributions of triangles already started, the remaining value at the current position forces the number of triangles starting there: the leftmost coefficient is . Reject a negative count or a positive count at a position without room for a complete length- triangle.
To implement the scan in linear time, set and . Let count triangles starting at position , with out-of-range earlier counts zero. Then compute
Require and require for .
For candidate strengths, the time complexity is and the space complexity is . Subtask 1 permits checking all in time. In subtask 2, checking every strength without the sum filter gives a sufficient solution.
Proof
Consider the leftmost position not yet matched. A triangle starting later cannot affect it. After contributions of earlier triangles are removed, only triangles starting at can supply the remainder. Their leading coefficient is , so their count is forced. Repeating this argument never discards a feasible solution.
The second difference of a triangle is at its start, after positions, and after positions. Thus the recurrence computes precisely the forced counts. If every count is nonnegative and every start is allowed, all triangles lie within the line. The produced sequence and the target have the same left boundary values and second differences, hence agree at every position.
Solution written by GPT6