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

Some Tanks Are in Siege Mode, Others Go Boom Boom Boom Boom Boom Boom!

Back to problem

Editorial

Removing wasted siege attacks

A siege attack targeting either endpoint decreases total health by at most 333, or at most 222 when N=1N=1N=1. Attacks in tank mode can reproduce the same decreases for a cost of at most 333. Therefore, an optimal solution exists that never targets an endpoint in siege mode.

A siege attack decreases the total health by at most 444. If any damage is lost because health is clipped at 000, its actual total decrease is at most 333. Replace that attack with attacks in tank mode producing exactly the same decreases. This reaches the same state for a cost of at most 333. Thus, an optimal solution exists in which every siege attack deals its full damage.

Maximizing the number of siege attacks

Let xix_ixi​ be the number of siege attacks targeting position iii. By the observation above, omit endpoint attacks and set x0=x1=xN=xN+1=0x_0=x_1=x_N=x_{N+1}=0x0​. The following condition must hold at each position:

xi−1+2xi+xi+1≤ai.x_{i-1}+2x_i+x_{i+1}\le a_i.xi−1​+2xi​+x

Conversely, if these conditions hold, we can perform the siege attacks in any order without wasting damage, then remove the remaining health with attacks in tank mode. If K=∑ixiK=\sum_i x_iK=∑i​xi​, the siege attacks remove total health 4K4K4K, and the total cost is

3K+∑i=1Nai−4K=∑i=1Nai−K.3K+\sum_{i=1}^{N}a_i-4K=\sum_{i=1}^{N}a_i-K.3K+i=1∑N​ai

It remains to maximize the sum of nonnegative integer values xix_ixi​ satisfying the conditions.

Dynamic programming

Let M=max⁡iaiM=\max_i a_iM=maxi​ai​ and B=⌊M/2⌋B=\lfloor M/2\rfloorB=⌊. Every lies between and .

Immediately before processing position iii, let dp[p][q]\mathrm{dp}[p][q]dp[p][q] be the maximum sum of the chosen values x1,⋯ ,xix_1,\cdots,x_ix1​,⋯,, assuming that all conditions before position hold and , . Impossible states have value negative infinity. Initially, and all other states are impossible.

Choose r=xi+1r=x_{i+1}r=xi+1​. The current condition is p+2q+r≤aip+2q+r\le a_ip+2q+r≤, giving the transition

next[q][r]=r+max⁡0≤p≤min⁡(B,ai−2q−r)dp[p][q].\mathrm{next}[q][r]=r+\max_{0\le p\le\min(B,a_i-2q-r)}\mathrm{dp}[p][q].next[q][r]=r+0≤

Only r=0r=0r=0 is allowed when i+1≥Ni+1\ge Ni+1≥N. For each fixed qqq, compute prefix maxima along ppp to evaluate each transition in time. After processing position , is the maximum number of siege attacks.

The transitions enumerate every possible next count and check exactly the condition at the current position. Induction therefore proves that the DP preserves every feasible placement and its maximum count. The answer is ∑iai−dp[0][0]\sum_i a_i-\mathrm{dp}[0][0]∑i​ai​−dp[0][0].

The time complexity is O(N(B+1)2)O(N(B+1)^2)O(N(B+1)2) and the space complexity is O((B+1)2)O((B+1)^2)O((B+1). Since , we have . Using also covers an all-zero input.

Subtasks

  • Subtask 111: Use the current health vector as a state and try both attack types directly. Time is O(N∏i(ai+1))O(N\prod_i(a_i+1))O(N∏i​(ai​+ and space is . With and , there are at most states.

Solution written by GPT6

=
x1​=
xN​=
xN+1​=
0
i+1
​
≤
ai​.
​
−
4K=
i=1∑N​ai​−
K.
M
/2
⌋
xix_ixi​
000
BBB
xi​
iii
xi−1=px_{i-1}=pxi−1​=p
xi=qx_i=qxi​=q
dp[0][0]=0\mathrm{dp}[0][0]=0dp[0][0]=0
ai​
p
≤
min
(
B
,
ai​
−
2
q
−
r
)
max
​
dp
[
p
]
[
q
]
.
O(1)O(1)
O(1)
NNN
dp[0][0]\mathrm{dp}[0][0]dp[0][0]
2
)
M≤100M\le100M≤100
B≤50B\le50B≤50
B+1B+1B+1
1
))
O(∏i(ai+1))O(\prod_i(a_i+1))O(∏i​(ai​+1))
N≤8N\le8N≤8
ai≤4a_i\le4ai​≤4
58=390 6255^8=390\,62558=390625
  • Subtask 222: Enumerate p,q,rp,q,rp,q,r without prefix maxima. Time is O(N(B+1)3)O(N(B+1)^3)O(N(B+1)3) and space is O((B+1)2)O((B+1)^2)O((B+1)2).
  • Subtask 333: Use the full solution with prefix maxima.