DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

OrBiTal (easy)

Back to problem

Editorial

A particle contributes K2K^2K2 in total. With S=∑AiS=\sum A_iS=∑Ai​, only strengths satisfying K2∣SK^2\mid SK2∣S need checking. A particle has peak KKK, so also require K≤max⁡AiK\leq\max A_iK≤maxAi​.

Fix KKK 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 111. Reject a negative count or a positive count at a position without room for a complete length-2K−12K-12K−1 triangle.

To implement the scan in linear time, set A0=A−1=0A_0=A_{-1}=0A0​=A−1​=0 and Ci=Ai−2Ai−1+Ai. Let count triangles starting at position , with out-of-range earlier counts zero. Then compute

Xi=Ci+2Xi−K−Xi−2K.X_i=C_i+2X_{i-K}-X_{i-2K}.Xi​=Ci​+2X

Require 0≤Xi≤Ai0\leq X_i\leq A_i0≤Xi​≤Ai​ and require Xi=0X_i=0X for .

For DDD candidate strengths, the time complexity is O(ND+N)O(ND+N)O(ND+N) and the space complexity is O(N)O(N)O(N). Subtask 1 permits checking all K≤10K\leq10K≤ in time. In subtask 2, checking every strength without the sum filter gives a sufficient solution.

Proof

Consider the leftmost position iii not yet matched. A triangle starting later cannot affect it. After contributions of earlier triangles are removed, only triangles starting at iii can supply the remainder. Their leading coefficient is 111, so their count is forced. Repeating this argument never discards a feasible solution.

The second difference of a triangle is 111 at its start, −2-2−2 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

−2C_i=A_i-2A_{i-1}+A_{i-2}
Ci​=Ai​−2Ai−1​+Ai−2​
XiX_iXi​
iii
i−K
​
−
Xi−2K​.
i​
=
0
i>N−2K+2i>N-2K+2i>N−2K+2
10
O(N)O(N)O(N)
O(N2)O(N^2)O(N2)
KK
K
111
2K2K2K